[PATCH] D56621: [hwasan] Madvise away thread aux data

Evgenii Stepanov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 24 18:06:03 PST 2019


This revision was automatically updated to reflect the committed changes.
Closed by commit rL352151: [hwasan] Madvise away thread aux data (authored by eugenis, committed by ).
Herald added a subscriber: delcypher.

Changed prior to commit:
  https://reviews.llvm.org/D56621?vs=183194&id=183465#toc

Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D56621/new/

https://reviews.llvm.org/D56621

Files:
  compiler-rt/trunk/lib/hwasan/hwasan_thread_list.h


Index: compiler-rt/trunk/lib/hwasan/hwasan_thread_list.h
===================================================================
--- compiler-rt/trunk/lib/hwasan/hwasan_thread_list.h
+++ compiler-rt/trunk/lib/hwasan/hwasan_thread_list.h
@@ -108,38 +108,52 @@
 class HwasanThreadList {
  public:
   HwasanThreadList(uptr storage, uptr size)
-      : free_space_(storage),
-        free_space_end_(storage + size),
-        ring_buffer_size_(RingBufferSize()) {}
+      : free_space_(storage), free_space_end_(storage + size) {
+    // [storage, storage + size) is used as a vector of
+    // thread_alloc_size_-sized, ring_buffer_size_*2-aligned elements.
+    // Each element contains
+    // * a ring buffer at offset 0,
+    // * a Thread object at offset ring_buffer_size_.
+    ring_buffer_size_ = RingBufferSize();
+    thread_alloc_size_ =
+        RoundUpTo(ring_buffer_size_ + sizeof(Thread), ring_buffer_size_ * 2);
+  }
 
   Thread *CreateCurrentThread() {
     Thread *t;
     {
       SpinMutexLock l(&list_mutex_);
       t = free_list_.Pop();
-      if (t)
-        internal_memset((void *)t, 0, sizeof(Thread) + ring_buffer_size_);
-      else
+      if (t) {
+        uptr start = (uptr)t - ring_buffer_size_;
+        internal_memset((void *)start, 0, ring_buffer_size_ + sizeof(Thread));
+      } else {
         t = AllocThread();
+      }
       live_list_.Push(t);
     }
-    t->Init((uptr)(t + 1), ring_buffer_size_);
+    t->Init((uptr)t - ring_buffer_size_, ring_buffer_size_);
     AddThreadStats(t);
     return t;
   }
 
+  void DontNeedThread(Thread *t) {
+    uptr start = (uptr)t - ring_buffer_size_;
+    ReleaseMemoryPagesToOS(start, start + thread_alloc_size_);
+  }
+
   void ReleaseThread(Thread *t) {
-    // FIXME: madvise away the ring buffer?
     RemoveThreadStats(t);
     t->Destroy();
     SpinMutexLock l(&list_mutex_);
     live_list_.Remove(t);
     free_list_.Push(t);
+    DontNeedThread(t);
   }
 
   Thread *GetThreadByBufferAddress(uptr p) {
-    uptr align = ring_buffer_size_ * 2;
-    return (Thread *)(RoundDownTo(p, align) - sizeof(Thread));
+    return (Thread *)(RoundDownTo(p, ring_buffer_size_ * 2) +
+                      ring_buffer_size_);
   }
 
   uptr MemoryUsedPerThread() {
@@ -175,15 +189,17 @@
  private:
   Thread *AllocThread() {
     uptr align = ring_buffer_size_ * 2;
-    uptr ring_buffer_start = RoundUpTo(free_space_ + sizeof(Thread), align);
-    free_space_ = ring_buffer_start + ring_buffer_size_;
+    CHECK(IsAligned(free_space_, align));
+    Thread *t = (Thread *)(free_space_ + ring_buffer_size_);
+    free_space_ += thread_alloc_size_;
     CHECK(free_space_ <= free_space_end_ && "out of thread memory");
-    return (Thread *)(ring_buffer_start - sizeof(Thread));
+    return t;
   }
 
   uptr free_space_;
   uptr free_space_end_;
   uptr ring_buffer_size_;
+  uptr thread_alloc_size_;
 
   ThreadListHead free_list_;
   ThreadListHead live_list_;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D56621.183465.patch
Type: text/x-patch
Size: 2937 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190125/1b69964c/attachment.bin>


More information about the llvm-commits mailing list