]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Actually release buffer freelists when handling OOM conditions.
authorNick Mathewson <nickm@torproject.org>
Sun, 12 Jan 2014 17:26:45 +0000 (12:26 -0500)
committerNick Mathewson <nickm@torproject.org>
Wed, 12 Feb 2014 17:38:20 +0000 (12:38 -0500)
Otherwise freeing buffers won't help for a little while.

src/or/buffers.c
src/or/buffers.h
src/or/circuitlist.c

index 856c9b6060d8f20c43ede0480a2084693e44e244..352e60a9796a93a90f439b2009111de48abbc6a7 100644 (file)
@@ -276,12 +276,14 @@ preferred_chunk_size(size_t target)
 }
 
 /** Remove from the freelists most chunks that have not been used since the
- * last call to buf_shrink_freelists(). */
-void
+ * last call to buf_shrink_freelists().   Return the amount of memory
+ * freed. */
+size_t
 buf_shrink_freelists(int free_all)
 {
 #ifdef ENABLE_BUF_FREELISTS
   int i;
+  size_t total_freed = 0;
   disable_control_logging();
   for (i = 0; freelists[i].alloc_size; ++i) {
     int slack = freelists[i].slack;
@@ -315,6 +317,7 @@ buf_shrink_freelists(int free_all)
         chunk_t *next = chunk->next;
         tor_assert(total_bytes_allocated_in_chunks >= CHUNK_ALLOC_SIZE(chunk->memlen));
         total_bytes_allocated_in_chunks -= CHUNK_ALLOC_SIZE(chunk->memlen);
+        total_freed += CHUNK_ALLOC_SIZE(chunk->memlen);
         tor_free(chunk);
         chunk = next;
         --n_to_free;
@@ -343,8 +346,10 @@ buf_shrink_freelists(int free_all)
   }
  done:
   enable_control_logging();
+  return total_freed;
 #else
   (void) free_all;
+  return 0;
 #endif
 }
 
index 677d68d83df65602a59f04f7912397ac41cd3e42..b3b90034923bc924abf9d87b39a387f2ed7dfd93 100644 (file)
@@ -18,7 +18,7 @@ void buf_free(buf_t *buf);
 void buf_clear(buf_t *buf);
 buf_t *buf_copy(const buf_t *buf);
 void buf_shrink(buf_t *buf);
-void buf_shrink_freelists(int free_all);
+size_t buf_shrink_freelists(int free_all);
 void buf_dump_freelist_sizes(int severity);
 
 size_t buf_datalen(const buf_t *buf);
index 2e135416c2f80bd7d95e4a62b8de92d6d1ae927e..ffb5e0cd0e944fb6e518557b0745e629b72f1aac 100644 (file)
@@ -1532,6 +1532,17 @@ circuits_handle_oom(size_t current_allocation)
              "over-long queues. (This behavior is controlled by "
              "MaxMemInQueues.)");
 
+  {
+    const size_t recovered = buf_shrink_freelists(1);
+    if (recovered >= current_allocation) {
+      log_warn(LD_BUG, "We somehow recovered more memory from freelists "
+               "than we thought we had allocated");
+      current_allocation = 0;
+    } else {
+      current_allocation -= recovered;
+    }
+  }
+
   {
     size_t mem_target = (size_t)(get_options()->MaxMemInQueues *
                                  FRACTION_OF_DATA_TO_RETAIN_ON_OOM);
@@ -1575,6 +1586,8 @@ circuits_handle_oom(size_t current_allocation)
   } SMARTLIST_FOREACH_END(circ);
 
   clean_cell_pool(); /* In case this helps. */
+  buf_shrink_freelists(1); /* This is necessary to actually release buffer
+                              chunks. */
 
   log_notice(LD_GENERAL, "Removed "U64_FORMAT" bytes by killing %d circuits.",
              U64_PRINTF_ARG(mem_recovered),