chiark / gitweb /
journal: consistently allow type==0 to mean "any type"
[elogind.git] / src / journal / mmap-cache.c
index 6c9194a1a5faa9ca24161f1a8d34d747e176f1e7..f34d260856127e6493ff08db3ba5052515f25f47 100644 (file)
@@ -72,6 +72,9 @@ struct MMapCache {
         int n_ref;
         unsigned n_windows;
 
+        unsigned n_hit, n_missed;
+
+
         Hashmap *fds;
         Hashmap *contexts;
 
@@ -80,7 +83,13 @@ struct MMapCache {
 };
 
 #define WINDOWS_MIN 64
-#define WINDOW_SIZE (8ULL*1024ULL*1024ULL)
+
+#ifdef ENABLE_DEBUG_MMAP_CACHE
+/* Tiny windows increase mmap activity and the chance of exposing unsafe use. */
+# define WINDOW_SIZE (page_size())
+#else
+# define WINDOW_SIZE (8ULL*1024ULL*1024ULL)
+#endif
 
 MMapCache* mmap_cache_new(void) {
         MMapCache *m;
@@ -184,11 +193,17 @@ static void context_detach_window(Context *c) {
 
         if (!w->contexts && !w->keep_always) {
                 /* Not used anymore? */
+#ifdef ENABLE_DEBUG_MMAP_CACHE
+                /* Unmap unused windows immediately to expose use-after-unmap
+                 * by SIGSEGV. */
+                window_free(w);
+#else
                 LIST_PREPEND(unused, c->cache->unused, w);
                 if (!c->cache->last_unused)
                         c->cache->last_unused = w;
 
                 w->in_unused = true;
+#endif
         }
 }
 
@@ -224,7 +239,7 @@ static Context *context_add(MMapCache *m, unsigned id) {
         if (c)
                 return c;
 
-        r = hashmap_ensure_allocated(&m->contexts, trivial_hash_func, trivial_compare_func);
+        r = hashmap_ensure_allocated(&m->contexts, NULL);
         if (r < 0)
                 return NULL;
 
@@ -278,7 +293,7 @@ static FileDescriptor* fd_add(MMapCache *m, int fd) {
         if (f)
                 return f;
 
-        r = hashmap_ensure_allocated(&m->fds, trivial_hash_func, trivial_compare_func);
+        r = hashmap_ensure_allocated(&m->fds, NULL);
         if (r < 0)
                 return NULL;
 
@@ -375,7 +390,7 @@ static int try_context(
                 return 0;
         }
 
-        c->window->keep_always = c->window->keep_always || keep_always;
+        c->window->keep_always |= keep_always;
 
         *ret = (uint8_t*) c->window->ptr + (offset - c->window->offset);
         return 1;
@@ -399,7 +414,6 @@ static int find_mmap(
         assert(m->n_ref > 0);
         assert(fd >= 0);
         assert(size > 0);
-        assert(ret);
 
         f = hashmap_get(m->fds, INT_TO_PTR(fd + 1));
         if (!f)
@@ -419,7 +433,7 @@ static int find_mmap(
                 return -ENOMEM;
 
         context_attach_window(c, w);
-        w->keep_always = w->keep_always || keep_always;
+        w->keep_always += keep_always;
 
         *ret = (uint8_t*) w->ptr + (offset - w->offset);
         return 1;
@@ -494,15 +508,15 @@ static int add_mmap(
 
         c = context_add(m, context);
         if (!c)
-                return -ENOMEM;
+                goto outofmem;
 
         f = fd_add(m, fd);
         if (!f)
-                return -ENOMEM;
+                goto outofmem;
 
         w = window_add(m);
         if (!w)
-                return -ENOMEM;
+                goto outofmem;
 
         w->keep_always = keep_always;
         w->ptr = d;
@@ -519,6 +533,10 @@ static int add_mmap(
 
         *ret = (uint8_t*) w->ptr + (offset - w->offset);
         return 1;
+
+outofmem:
+        munmap(d, wsize);
+        return -ENOMEM;
 }
 
 int mmap_cache_get(
@@ -542,13 +560,19 @@ int mmap_cache_get(
 
         /* Check whether the current context is the right one already */
         r = try_context(m, fd, prot, context, keep_always, offset, size, ret);
-        if (r != 0)
+        if (r != 0) {
+                m->n_hit ++;
                 return r;
+        }
 
         /* Search for a matching mmap */
         r = find_mmap(m, fd, prot, context, keep_always, offset, size, ret);
-        if (r != 0)
+        if (r != 0) {
+                m->n_hit ++;
                 return r;
+        }
+
+        m->n_missed++;
 
         /* Create a new mmap */
         return add_mmap(m, fd, prot, context, keep_always, offset, size, st, ret);
@@ -578,3 +602,15 @@ void mmap_cache_close_context(MMapCache *m, unsigned context) {
 
         context_free(c);
 }
+
+unsigned mmap_cache_get_hit(MMapCache *m) {
+        assert(m);
+
+        return m->n_hit;
+}
+
+unsigned mmap_cache_get_missed(MMapCache *m) {
+        assert(m);
+
+        return m->n_missed;
+}