You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
7.6 KiB

  1. commit 0ff395c154ad827c0c30eefc9371ba7f7c171027
  2. Author: Willy Tarreau <w@1wt.eu>
  3. Date: Tue Jul 30 11:59:34 2019 +0200
  4. BUG/MAJOR: queue/threads: avoid an AB/BA locking issue in process_srv_queue()
  5. A problem involving server slowstart was reported by @max2k1 in issue #197.
  6. The problem is that pendconn_grab_from_px() takes the proxy lock while
  7. already under the server's lock while process_srv_queue() first takes the
  8. proxy's lock then the server's lock.
  9. While the latter seems more natural, it is fundamentally incompatible with
  10. mayn other operations performed on servers, namely state change propagation,
  11. where the proxy is only known after the server and cannot be locked around
  12. the servers. Howwever reversing the lock in process_srv_queue() is trivial
  13. and only the few functions related to dynamic cookies need to be adjusted
  14. for this so that the proxy's lock is taken for each server operation. This
  15. is possible because the proxy's server list is built once at boot time and
  16. remains stable. So this is what this patch does.
  17. The comments in the proxy and server structs were updated to mention this
  18. rule that the server's lock may not be taken under the proxy's lock but
  19. may enclose it.
  20. Another approach could consist in using a second lock for the proxy's queue
  21. which would be different from the regular proxy's lock, but given that the
  22. operations above are rare and operate on small servers list, there is no
  23. reason for overdesigning a solution.
  24. This fix was successfully tested with 10000 servers in a backend where
  25. adjusting the dyncookies in loops over the CLI didn't have a measurable
  26. impact on the traffic.
  27. The only workaround without the fix is to disable any occurrence of
  28. "slowstart" on server lines, or to disable threads using "nbthread 1".
  29. This must be backported as far as 1.8.
  30. (cherry picked from commit 5e83d996cf965ee5ac625f702a446f4d8c80a220)
  31. Signed-off-by: Willy Tarreau <w@1wt.eu>
  32. diff --git a/include/types/proxy.h b/include/types/proxy.h
  33. index ca24dbfe..2518f88d 100644
  34. --- a/include/types/proxy.h
  35. +++ b/include/types/proxy.h
  36. @@ -487,7 +487,7 @@ struct proxy {
  37. * name is used
  38. */
  39. struct list filter_configs; /* list of the filters that are declared on this proxy */
  40. - __decl_hathreads(HA_SPINLOCK_T lock);
  41. + __decl_hathreads(HA_SPINLOCK_T lock); /* may be taken under the server's lock */
  42. };
  43. struct switching_rule {
  44. diff --git a/include/types/server.h b/include/types/server.h
  45. index 4a077268..e0534162 100644
  46. --- a/include/types/server.h
  47. +++ b/include/types/server.h
  48. @@ -319,7 +319,7 @@ struct server {
  49. } ssl_ctx;
  50. #endif
  51. struct dns_srvrq *srvrq; /* Pointer representing the DNS SRV requeest, if any */
  52. - __decl_hathreads(HA_SPINLOCK_T lock);
  53. + __decl_hathreads(HA_SPINLOCK_T lock); /* may enclose the proxy's lock, must not be taken under */
  54. struct {
  55. const char *file; /* file where the section appears */
  56. struct eb32_node id; /* place in the tree of used IDs */
  57. diff --git a/src/proxy.c b/src/proxy.c
  58. index ae761ead..a537e0b1 100644
  59. --- a/src/proxy.c
  60. +++ b/src/proxy.c
  61. @@ -1940,9 +1940,12 @@ static int cli_parse_enable_dyncookie_backend(char **args, char *payload, struct
  62. if (!px)
  63. return 1;
  64. + /* Note: this lock is to make sure this doesn't change while another
  65. + * thread is in srv_set_dyncookie().
  66. + */
  67. HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
  68. -
  69. px->ck_opts |= PR_CK_DYNAMIC;
  70. + HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
  71. for (s = px->srv; s != NULL; s = s->next) {
  72. HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
  73. @@ -1950,8 +1953,6 @@ static int cli_parse_enable_dyncookie_backend(char **args, char *payload, struct
  74. HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
  75. }
  76. - HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
  77. -
  78. return 1;
  79. }
  80. @@ -1971,9 +1972,12 @@ static int cli_parse_disable_dyncookie_backend(char **args, char *payload, struc
  81. if (!px)
  82. return 1;
  83. + /* Note: this lock is to make sure this doesn't change while another
  84. + * thread is in srv_set_dyncookie().
  85. + */
  86. HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
  87. -
  88. px->ck_opts &= ~PR_CK_DYNAMIC;
  89. + HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
  90. for (s = px->srv; s != NULL; s = s->next) {
  91. HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
  92. @@ -1984,8 +1988,6 @@ static int cli_parse_disable_dyncookie_backend(char **args, char *payload, struc
  93. HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
  94. }
  95. - HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
  96. -
  97. return 1;
  98. }
  99. @@ -2021,10 +2023,13 @@ static int cli_parse_set_dyncookie_key_backend(char **args, char *payload, struc
  100. return 1;
  101. }
  102. + /* Note: this lock is to make sure this doesn't change while another
  103. + * thread is in srv_set_dyncookie().
  104. + */
  105. HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
  106. -
  107. free(px->dyncookie_key);
  108. px->dyncookie_key = newkey;
  109. + HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
  110. for (s = px->srv; s != NULL; s = s->next) {
  111. HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
  112. @@ -2032,8 +2037,6 @@ static int cli_parse_set_dyncookie_key_backend(char **args, char *payload, struc
  113. HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
  114. }
  115. - HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
  116. -
  117. return 1;
  118. }
  119. diff --git a/src/queue.c b/src/queue.c
  120. index f4a94530..6aa54170 100644
  121. --- a/src/queue.c
  122. +++ b/src/queue.c
  123. @@ -312,16 +312,16 @@ void process_srv_queue(struct server *s)
  124. struct proxy *p = s->proxy;
  125. int maxconn;
  126. - HA_SPIN_LOCK(PROXY_LOCK, &p->lock);
  127. HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
  128. + HA_SPIN_LOCK(PROXY_LOCK, &p->lock);
  129. maxconn = srv_dynamic_maxconn(s);
  130. while (s->served < maxconn) {
  131. int ret = pendconn_process_next_strm(s, p);
  132. if (!ret)
  133. break;
  134. }
  135. - HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
  136. HA_SPIN_UNLOCK(PROXY_LOCK, &p->lock);
  137. + HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
  138. }
  139. /* Adds the stream <strm> to the pending connection queue of server <strm>->srv
  140. @@ -424,7 +424,8 @@ int pendconn_redistribute(struct server *s)
  141. /* Check for pending connections at the backend, and assign some of them to
  142. * the server coming up. The server's weight is checked before being assigned
  143. * connections it may not be able to handle. The total number of transferred
  144. - * connections is returned.
  145. + * connections is returned. It must be called with the server lock held, and
  146. + * will take the proxy's lock.
  147. */
  148. int pendconn_grab_from_px(struct server *s)
  149. {
  150. diff --git a/src/server.c b/src/server.c
  151. index a96f1ef6..236d6bae 100644
  152. --- a/src/server.c
  153. +++ b/src/server.c
  154. @@ -125,7 +125,7 @@ static inline void srv_check_for_dup_dyncookie(struct server *s)
  155. }
  156. /*
  157. - * Must be called with the server lock held.
  158. + * Must be called with the server lock held, and will grab the proxy lock.
  159. */
  160. void srv_set_dyncookie(struct server *s)
  161. {
  162. @@ -137,15 +137,17 @@ void srv_set_dyncookie(struct server *s)
  163. int addr_len;
  164. int port;
  165. + HA_SPIN_LOCK(PROXY_LOCK, &p->lock);
  166. +
  167. if ((s->flags & SRV_F_COOKIESET) ||
  168. !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
  169. s->proxy->dyncookie_key == NULL)
  170. - return;
  171. + goto out;
  172. key_len = strlen(p->dyncookie_key);
  173. if (s->addr.ss_family != AF_INET &&
  174. s->addr.ss_family != AF_INET6)
  175. - return;
  176. + goto out;
  177. /*
  178. * Buffer to calculate the cookie value.
  179. * The buffer contains the secret key + the server IP address
  180. @@ -174,7 +176,7 @@ void srv_set_dyncookie(struct server *s)
  181. hash_value = XXH64(tmpbuf, buffer_len, 0);
  182. memprintf(&s->cookie, "%016llx", hash_value);
  183. if (!s->cookie)
  184. - return;
  185. + goto out;
  186. s->cklen = 16;
  187. /* Don't bother checking if the dyncookie is duplicated if
  188. @@ -183,6 +185,8 @@ void srv_set_dyncookie(struct server *s)
  189. */
  190. if (!(s->next_admin & SRV_ADMF_FMAINT))
  191. srv_check_for_dup_dyncookie(s);
  192. + out:
  193. + HA_SPIN_UNLOCK(PROXY_LOCK, &p->lock);
  194. }
  195. /*