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.

116 lines
3.9 KiB

  1. commit 31470e2ba2aabb4c6340fbc15cb5486ceb8c69c8
  2. Author: Olivier Houchard <ohouchard@haproxy.com>
  3. Date: Mon Apr 29 18:52:06 2019 +0200
  4. BUG/MEDIUM: port_range: Make the ring buffer lock-free.
  5. Port range uses a ring buffer, and unfortunately, when making haproxy
  6. multithreaded, it's been overlooked, and the ring buffer is not thread-safe.
  7. When specifying a source range, 2 or more threads could pick the same
  8. port, and of course only one of them could use the port, the others would
  9. always fail the connection.
  10. To fix this, make it a lock-free ring buffer. This is easier than usual
  11. because we know the ring buffer can never be full.
  12. This should be backported to 1.8 and 1.9.
  13. (cherry picked from commit 07425de71777b688e77a9c70a7088c13e66e41e9)
  14. Signed-off-by: Olivier Houchard <cognet@ci0.org>
  15. (cherry picked from commit bffb51147a4a5939e344b3c838628f9a944febef)
  16. Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
  17. diff --git a/include/proto/port_range.h b/include/proto/port_range.h
  18. index 8c63faca..f7e3f1d5 100644
  19. --- a/include/proto/port_range.h
  20. +++ b/include/proto/port_range.h
  21. @@ -24,18 +24,22 @@
  22. #include <types/port_range.h>
  23. +#define GET_NEXT_OFF(range, off) ((off) == (range)->size - 1 ? 0 : (off) + 1)
  24. +
  25. /* return an available port from range <range>, or zero if none is left */
  26. static inline int port_range_alloc_port(struct port_range *range)
  27. {
  28. int ret;
  29. + int get;
  30. + int put;
  31. - if (!range->avail)
  32. - return 0;
  33. - ret = range->ports[range->get];
  34. - range->get++;
  35. - if (range->get >= range->size)
  36. - range->get = 0;
  37. - range->avail--;
  38. + get = HA_ATOMIC_LOAD(&range->get);
  39. + do {
  40. + put = HA_ATOMIC_LOAD(&range->put_t);
  41. + if (unlikely(put == get))
  42. + return 0;
  43. + ret = range->ports[get];
  44. + } while (!(HA_ATOMIC_CAS(&range->get, &get, GET_NEXT_OFF(range, get))));
  45. return ret;
  46. }
  47. @@ -45,14 +49,28 @@ static inline int port_range_alloc_port(struct port_range *range)
  48. */
  49. static inline void port_range_release_port(struct port_range *range, int port)
  50. {
  51. + int put;
  52. +
  53. if (!port || !range)
  54. return;
  55. - range->ports[range->put] = port;
  56. - range->avail++;
  57. - range->put++;
  58. - if (range->put >= range->size)
  59. - range->put = 0;
  60. + put = range->put_h;
  61. + /* put_h is reserved for producers, so that they can each get a
  62. + * free slot, put_t is what is used by consumers to know if there's
  63. + * elements available or not
  64. + */
  65. + /* First reserve or slot, we know the ring buffer can't be full,
  66. + * as we will only ever release port we allocated before
  67. + */
  68. + while (!(HA_ATOMIC_CAS(&range->put_h, &put, GET_NEXT_OFF(range, put))));
  69. + HA_ATOMIC_STORE(&range->ports[put], port);
  70. + /* Wait until all the threads that got a slot before us are done */
  71. + while ((volatile int)range->put_t != put)
  72. + __ha_compiler_barrier();
  73. + /* Let the world know we're done, and any potential consumer they
  74. + * can use that port.
  75. + */
  76. + HA_ATOMIC_STORE(&range->put_t, GET_NEXT_OFF(range, put));
  77. }
  78. /* return a new initialized port range of N ports. The ports are not
  79. @@ -62,8 +80,10 @@ static inline struct port_range *port_range_alloc_range(int n)
  80. {
  81. struct port_range *ret;
  82. ret = calloc(1, sizeof(struct port_range) +
  83. - n * sizeof(((struct port_range *)0)->ports[0]));
  84. - ret->size = ret->avail = n;
  85. + (n + 1) * sizeof(((struct port_range *)0)->ports[0]));
  86. + ret->size = n + 1;
  87. + /* Start at the first free element */
  88. + ret->put_h = ret->put_t = n;
  89. return ret;
  90. }
  91. diff --git a/include/types/port_range.h b/include/types/port_range.h
  92. index 1d010f77..33455d2d 100644
  93. --- a/include/types/port_range.h
  94. +++ b/include/types/port_range.h
  95. @@ -25,8 +25,7 @@
  96. #include <netinet/in.h>
  97. struct port_range {
  98. - int size, get, put; /* range size, and get/put positions */
  99. - int avail; /* number of available ports left */
  100. + int size, get, put_h, put_t; /* range size, and get/put positions */
  101. uint16_t ports[0]; /* array of <size> ports, in host byte order */
  102. };