500-0002-don-t-use-libc.threads_minus_1-as-relaxed-atomic-for.patch 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. From e01b5939b38aea5ecbe41670643199825874b26c Mon Sep 17 00:00:00 2001
  2. From: Rich Felker <dalias@aerifal.cx>
  3. Date: Thu, 21 May 2020 23:32:45 -0400
  4. Subject: [PATCH 2/4] don't use libc.threads_minus_1 as relaxed atomic for
  5. skipping locks
  6. after all but the last thread exits, the next thread to observe
  7. libc.threads_minus_1==0 and conclude that it can skip locking fails to
  8. synchronize with any changes to memory that were made by the
  9. last-exiting thread. this can produce data races.
  10. on some archs, at least x86, memory synchronization is unlikely to be
  11. a problem; however, with the inline locks in malloc, skipping the lock
  12. also eliminated the compiler barrier, and caused code that needed to
  13. re-check chunk in-use bits after obtaining the lock to reuse a stale
  14. value, possibly from before the process became single-threaded. this
  15. in turn produced corruption of the heap state.
  16. some uses of libc.threads_minus_1 remain, especially for allocation of
  17. new TLS in the dynamic linker; otherwise, it could be removed
  18. entirely. it's made non-volatile to reflect that the remaining
  19. accesses are only made under lock on the thread list.
  20. instead of libc.threads_minus_1, libc.threaded is now used for
  21. skipping locks. the difference is that libc.threaded is permanently
  22. true once an additional thread has been created. this will produce
  23. some performance regression in processes that are mostly
  24. single-threaded but occasionally creating threads. in the future it
  25. may be possible to bring back the full lock-skipping, but more care
  26. needs to be taken to produce a safe design.
  27. ---
  28. src/internal/libc.h | 2 +-
  29. src/malloc/malloc.c | 2 +-
  30. src/thread/__lock.c | 2 +-
  31. 3 files changed, 3 insertions(+), 3 deletions(-)
  32. --- a/src/internal/libc.h
  33. +++ b/src/internal/libc.h
  34. @@ -21,7 +21,7 @@ struct __libc {
  35. int can_do_threads;
  36. int threaded;
  37. int secure;
  38. - volatile int threads_minus_1;
  39. + int threads_minus_1;
  40. size_t *auxv;
  41. struct tls_module *tls_head;
  42. size_t tls_size, tls_align, tls_cnt;
  43. --- a/src/malloc/malloc.c
  44. +++ b/src/malloc/malloc.c
  45. @@ -26,7 +26,7 @@ int __malloc_replaced;
  46. static inline void lock(volatile int *lk)
  47. {
  48. - if (libc.threads_minus_1)
  49. + if (libc.threaded)
  50. while(a_swap(lk, 1)) __wait(lk, lk+1, 1, 1);
  51. }
  52. --- a/src/thread/__lock.c
  53. +++ b/src/thread/__lock.c
  54. @@ -18,7 +18,7 @@
  55. void __lock(volatile int *l)
  56. {
  57. - if (!libc.threads_minus_1) return;
  58. + if (!libc.threaded) return;
  59. /* fast path: INT_MIN for the lock, +1 for the congestion */
  60. int current = a_cas(l, 0, INT_MIN + 1);
  61. if (!current) return;