mempool: do not overwrite same per-cpu values
[librseq.git] / src / rseq-mempool.c
index 1562a75a4ea25b404d15707039046dc85792727d..23b64f06435445c69d4513a663a2a77a7309d2af 100644 (file)
@@ -13,6 +13,7 @@
 #include <stdint.h>
 #include <stdbool.h>
 #include <stdio.h>
+#include <fcntl.h>
 
 #ifdef HAVE_LIBNUMA
 # include <numa.h>
 
 #define RANGE_HEADER_OFFSET    sizeof(struct rseq_mempool_range)
 
+#if RSEQ_BITS_PER_LONG == 64
+# define DEFAULT_POISON_VALUE  0x5555555555555555ULL
+#else
+# define DEFAULT_POISON_VALUE  0x55555555UL
+#endif
+
 struct free_list_node;
 
 struct free_list_node {
@@ -83,6 +90,8 @@ struct rseq_mempool_attr {
 
        bool poison_set;
        uintptr_t poison;
+
+       enum rseq_mempool_populate_policy populate_policy;
 };
 
 struct rseq_mempool_range;
@@ -90,9 +99,37 @@ struct rseq_mempool_range;
 struct rseq_mempool_range {
        struct rseq_mempool_range *next;        /* Linked list of ranges. */
        struct rseq_mempool *pool;              /* Backward reference to container pool. */
+
+       /*
+        * Memory layout of a mempool range:
+        * - Header page (contains struct rseq_mempool_range at the very end),
+        * - Base of the per-cpu data, starting with CPU 0.
+        *   Aliases with free-list for non-robust populate all pool.
+        * - CPU 1,
+        * ...
+        * - CPU max_nr_cpus - 1
+        * - init values (unpopulated for RSEQ_MEMPOOL_POPULATE_ALL).
+        *   Aliases with free-list for non-robust populate none pool.
+        * - free list (for robust pool).
+        *
+        * The free list aliases the CPU 0 memory area for non-robust
+        * populate all pools. It aliases with init values for
+        * non-robust populate none pools. It is located immediately
+        * after the init values for robust pools.
+        */
        void *header;
        void *base;
+       /*
+        * The init values contains malloc_init/zmalloc values.
+        * Pointer is NULL for RSEQ_MEMPOOL_POPULATE_ALL.
+        */
+       void *init;
        size_t next_unused;
+
+       /* Pool range mmap/munmap */
+       void *mmap_addr;
+       size_t mmap_len;
+
        /* Track alloc/free. */
        unsigned long *alloc_bitmap;
 };
@@ -132,6 +169,26 @@ struct rseq_mempool_set {
        struct rseq_mempool *entries[POOL_SET_NR_ENTRIES];
 };
 
+/*
+ * This memfd is used to implement the user COW behavior for the page
+ * protection scheme. memfd is a sparse virtual file. Its layout (in
+ * offset from beginning of file) matches the process address space
+ * (pointers directly converted to file offsets).
+ */
+struct rseq_memfd {
+       pthread_mutex_t lock;
+       size_t reserved_size;
+       unsigned int refcount;
+       int fd;
+};
+
+static struct rseq_memfd memfd = {
+       .lock = PTHREAD_MUTEX_INITIALIZER,
+       .reserved_size = 0,
+       .refcount = 0,
+       .fd = -1,
+};
+
 static
 const char *get_pool_name(const struct rseq_mempool *pool)
 {
@@ -145,64 +202,223 @@ void *__rseq_pool_range_percpu_ptr(const struct rseq_mempool_range *range, int c
        return range->base + (stride * cpu) + item_offset;
 }
 
+static
+void *__rseq_pool_range_init_ptr(const struct rseq_mempool_range *range,
+               uintptr_t item_offset)
+{
+       if (!range->init)
+               return NULL;
+       return range->init + item_offset;
+}
+
+static
+void __rseq_percpu *__rseq_free_list_to_percpu_ptr(const struct rseq_mempool *pool,
+               struct free_list_node *node)
+{
+       void __rseq_percpu *p = (void __rseq_percpu *) node;
+
+       if (pool->attr.robust_set) {
+               /* Skip cpus. */
+               p -= pool->attr.max_nr_cpus * pool->attr.stride;
+               /* Skip init values */
+               if (pool->attr.populate_policy != RSEQ_MEMPOOL_POPULATE_ALL)
+                       p -= pool->attr.stride;
+
+       } else {
+               /* Populate none free list is in init values */
+               if (pool->attr.populate_policy != RSEQ_MEMPOOL_POPULATE_ALL)
+                       p -= pool->attr.max_nr_cpus * pool->attr.stride;
+       }
+       return p;
+}
+
+static
+struct free_list_node *__rseq_percpu_to_free_list_ptr(const struct rseq_mempool *pool,
+               void __rseq_percpu *p)
+{
+       if (pool->attr.robust_set) {
+               /* Skip cpus. */
+               p += pool->attr.max_nr_cpus * pool->attr.stride;
+               /* Skip init values */
+               if (pool->attr.populate_policy != RSEQ_MEMPOOL_POPULATE_ALL)
+                       p += pool->attr.stride;
+
+       } else {
+               /* Populate none free list is in init values */
+               if (pool->attr.populate_policy != RSEQ_MEMPOOL_POPULATE_ALL)
+                       p += pool->attr.max_nr_cpus * pool->attr.stride;
+       }
+       return (struct free_list_node *) p;
+}
+
+static
+off_t ptr_to_off_t(void *p)
+{
+       return (off_t) (uintptr_t) p;
+}
+
+static
+int memcmpbyte(const char *s, int c, size_t n)
+{
+       int res = 0;
+
+       while (n-- > 0)
+               if ((res = *(s++) - c) != 0)
+                       break;
+       return res;
+}
+
 static
 void rseq_percpu_zero_item(struct rseq_mempool *pool,
                struct rseq_mempool_range *range, uintptr_t item_offset)
 {
+       char *init_p = NULL;
        int i;
 
+       init_p = __rseq_pool_range_init_ptr(range, item_offset);
+       if (init_p)
+               memset(init_p, 0, pool->item_len);
        for (i = 0; i < pool->attr.max_nr_cpus; i++) {
                char *p = __rseq_pool_range_percpu_ptr(range, i,
                                item_offset, pool->attr.stride);
+
+               /*
+                * If item is already zeroed, either because the
+                * init range update has propagated or because the
+                * content is already zeroed (e.g. zero page), don't
+                * write to the page. This eliminates useless COW over
+                * the zero page just for overwriting it with zeroes.
+                *
+                * This means zmalloc() in populate all policy pool do
+                * not trigger COW for CPUs which are not actively
+                * writing to the pool. This is however not the case for
+                * malloc_init() in populate-all pools if it populates
+                * non-zero content.
+                */
+               if (!memcmpbyte(p, 0, pool->item_len))
+                       continue;
                memset(p, 0, pool->item_len);
        }
 }
 
+static
+void rseq_percpu_init_item(struct rseq_mempool *pool,
+               struct rseq_mempool_range *range, uintptr_t item_offset,
+               void *init_ptr, size_t init_len)
+{
+       char *init_p = NULL;
+       int i;
+
+       init_p = __rseq_pool_range_init_ptr(range, item_offset);
+       if (init_p)
+               memcpy(init_p, init_ptr, init_len);
+       for (i = 0; i < pool->attr.max_nr_cpus; i++) {
+               char *p = __rseq_pool_range_percpu_ptr(range, i,
+                               item_offset, pool->attr.stride);
+
+               /*
+                * If the update propagated through a shared mapping,
+                * or the item already has the correct content, skip
+                * writing it into the cpu item to eliminate useless
+                * COW of the page.
+                */
+               if (!memcmp(init_ptr, p, init_len))
+                       continue;
+               memcpy(p, init_ptr, init_len);
+       }
+}
+
+static
+void rseq_poison_item(void *p, size_t item_len, uintptr_t poison)
+{
+       size_t offset;
+
+       for (offset = 0; offset < item_len; offset += sizeof(uintptr_t))
+               *((uintptr_t *) (p + offset)) = poison;
+}
+
+static
+intptr_t rseq_cmp_poison_item(void *p, size_t item_len, uintptr_t poison, intptr_t *unexpected_value)
+{
+       size_t offset;
+       intptr_t res = 0;
+
+       for (offset = 0; offset < item_len; offset += sizeof(uintptr_t)) {
+               intptr_t v = *((intptr_t *) (p + offset));
+
+               if ((res = v - (intptr_t) poison) != 0) {
+                       if (unexpected_value)
+                               *unexpected_value = v;
+                       break;
+               }
+       }
+       return res;
+}
+
 static
 void rseq_percpu_poison_item(struct rseq_mempool *pool,
                struct rseq_mempool_range *range, uintptr_t item_offset)
 {
        uintptr_t poison = pool->attr.poison;
+       char *init_p = NULL;
        int i;
 
+       init_p = __rseq_pool_range_init_ptr(range, item_offset);
+       if (init_p)
+               rseq_poison_item(init_p, pool->item_len, poison);
        for (i = 0; i < pool->attr.max_nr_cpus; i++) {
                char *p = __rseq_pool_range_percpu_ptr(range, i,
                                item_offset, pool->attr.stride);
-               size_t offset;
 
-               for (offset = 0; offset < pool->item_len; offset += sizeof(uintptr_t))
-                       *((uintptr_t *) (p + offset)) = poison;
+               /*
+                * If the update propagated through a shared mapping,
+                * or the item already has the correct content, skip
+                * writing it into the cpu item to eliminate useless
+                * COW of the page.
+                *
+                * It is recommended to use zero as poison value for
+                * populate-all pools to eliminate COW due to writing
+                * poison to unused CPU memory.
+                */
+               if (rseq_cmp_poison_item(p, pool->item_len, poison, NULL) == 0)
+                       continue;
+               rseq_poison_item(p, pool->item_len, poison);
        }
 }
 
+/* Always inline for __builtin_return_address(0). */
+static inline __attribute__((always_inline))
+void rseq_check_poison_item(const struct rseq_mempool *pool, uintptr_t item_offset,
+               void *p, size_t item_len, uintptr_t poison)
+{
+       intptr_t unexpected_value;
+
+       if (rseq_cmp_poison_item(p, item_len, poison, &unexpected_value) == 0)
+               return;
+
+       fprintf(stderr, "%s: Poison corruption detected (0x%lx) for pool: \"%s\" (%p), item offset: %zu, caller: %p.\n",
+               __func__, (unsigned long) unexpected_value, get_pool_name(pool), pool, item_offset, (void *) __builtin_return_address(0));
+       abort();
+}
+
 /* Always inline for __builtin_return_address(0). */
 static inline __attribute__((always_inline))
 void rseq_percpu_check_poison_item(const struct rseq_mempool *pool,
                const struct rseq_mempool_range *range, uintptr_t item_offset)
 {
        uintptr_t poison = pool->attr.poison;
+       char *init_p;
        int i;
 
-       if (!pool->attr.robust_set || !pool->attr.poison_set)
+       if (!pool->attr.robust_set)
                return;
+       init_p = __rseq_pool_range_init_ptr(range, item_offset);
+       if (init_p)
+               rseq_check_poison_item(pool, item_offset, init_p, pool->item_len, poison);
        for (i = 0; i < pool->attr.max_nr_cpus; i++) {
                char *p = __rseq_pool_range_percpu_ptr(range, i,
                                item_offset, pool->attr.stride);
-               size_t offset;
-
-               for (offset = 0; offset < pool->item_len; offset += sizeof(uintptr_t)) {
-                       uintptr_t v;
-
-                       /* Skip poison check for free-list pointer. */
-                       if (i == 0 && offset == 0)
-                               continue;
-                       v = *((uintptr_t *) (p + offset));
-                       if (v != poison) {
-                               fprintf(stderr, "%s: Poison corruption detected (0x%lx) for pool: \"%s\" (%p), item offset: %zu, caller: %p.\n",
-                                       __func__, (unsigned long) v, get_pool_name(pool), pool, item_offset, (void *) __builtin_return_address(0));
-                               abort();
-                       }
-               }
+               rseq_check_poison_item(pool, item_offset, p, pool->item_len, poison);
        }
 }
 
@@ -308,9 +524,10 @@ int create_alloc_bitmap(struct rseq_mempool *pool, struct rseq_mempool_range *ra
 }
 
 static
-bool addr_in_pool(const struct rseq_mempool *pool, void *addr)
+bool percpu_addr_in_pool(const struct rseq_mempool *pool, void __rseq_percpu *_addr)
 {
        struct rseq_mempool_range *range;
+       void *addr = (void *) _addr;
 
        for (range = pool->range_list; range; range = range->next) {
                if (addr >= range->base && addr < range->base + range->next_unused)
@@ -341,8 +558,6 @@ void check_free_list(const struct rseq_mempool *pool)
             prev = node,
             node = node->next) {
 
-               void *node_addr = node;
-
                if (traversal_iteration >= max_list_traversal) {
                        fprintf(stderr, "%s: Corrupted free-list; Possibly infinite loop in pool \"%s\" (%p), caller %p.\n",
                                __func__, get_pool_name(pool), pool, __builtin_return_address(0));
@@ -350,7 +565,7 @@ void check_free_list(const struct rseq_mempool *pool)
                }
 
                /* Node is out of range. */
-               if (!addr_in_pool(pool, node_addr)) {
+               if (!percpu_addr_in_pool(pool, __rseq_free_list_to_percpu_ptr(pool, node))) {
                        if (prev)
                                fprintf(stderr, "%s: Corrupted free-list node %p -> [out-of-range %p] in pool \"%s\" (%p), caller %p.\n",
                                        __func__, prev, node, get_pool_name(pool), pool, __builtin_return_address(0));
@@ -389,7 +604,7 @@ void check_pool_poison(const struct rseq_mempool *pool)
 {
        struct rseq_mempool_range *range;
 
-       if (!pool->attr.robust_set || !pool->attr.poison_set)
+       if (!pool->attr.robust_set)
                return;
        for (range = pool->range_list; range; range = range->next)
                check_range_poison(pool, range);
@@ -417,6 +632,7 @@ void destroy_alloc_bitmap(struct rseq_mempool *pool, struct rseq_mempool_range *
        }
 
        free(bitmap);
+       range->alloc_bitmap = NULL;
 }
 
 /* Always inline for __builtin_return_address(0). */
@@ -424,10 +640,23 @@ static inline __attribute__((always_inline))
 int rseq_mempool_range_destroy(struct rseq_mempool *pool,
                struct rseq_mempool_range *range)
 {
+       int ret = 0;
+
        destroy_alloc_bitmap(pool, range);
+
+       /*
+        * Punch a hole into memfd where the init values used to be.
+        */
+       if (range->init) {
+               ret = fallocate(memfd.fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
+                       ptr_to_off_t(range->init), pool->attr.stride);
+               if (ret)
+                       return ret;
+               range->init = NULL;
+       }
+
        /* range is a header located one page before the aligned mapping. */
-       return pool->attr.munmap_func(pool->attr.mmap_priv, range->header,
-                       (pool->attr.stride * pool->attr.max_nr_cpus) + rseq_get_page_len());
+       return pool->attr.munmap_func(pool->attr.mmap_priv, range->mmap_addr, range->mmap_len);
 }
 
 /*
@@ -512,6 +741,26 @@ alloc_error:
        return ptr;
 }
 
+static
+int rseq_memfd_reserve_init(void *init, size_t init_len)
+{
+       int ret = 0;
+       size_t reserve_len;
+
+       pthread_mutex_lock(&memfd.lock);
+       reserve_len = (size_t) ptr_to_off_t(init) + init_len;
+       if (reserve_len > memfd.reserved_size) {
+               if (ftruncate(memfd.fd, (off_t) reserve_len)) {
+                       ret = -1;
+                       goto unlock;
+               }
+               memfd.reserved_size = reserve_len;
+       }
+unlock:
+       pthread_mutex_unlock(&memfd.lock);
+       return ret;
+}
+
 static
 struct rseq_mempool_range *rseq_mempool_range_create(struct rseq_mempool *pool)
 {
@@ -519,6 +768,7 @@ struct rseq_mempool_range *rseq_mempool_range_create(struct rseq_mempool *pool)
        unsigned long page_size;
        void *header;
        void *base;
+       size_t range_len;       /* Range len excludes header. */
 
        if (pool->attr.max_nr_ranges &&
                        pool->nr_ranges >= pool->attr.max_nr_ranges) {
@@ -527,16 +777,53 @@ struct rseq_mempool_range *rseq_mempool_range_create(struct rseq_mempool *pool)
        }
        page_size = rseq_get_page_len();
 
+       range_len = pool->attr.stride * pool->attr.max_nr_cpus;
+       if (pool->attr.populate_policy != RSEQ_MEMPOOL_POPULATE_ALL)
+               range_len += pool->attr.stride; /* init values */
+       if (pool->attr.robust_set)
+               range_len += pool->attr.stride; /* free list */
        base = aligned_mmap_anonymous(pool, page_size,
-                       pool->attr.stride * pool->attr.max_nr_cpus,
+                       range_len,
                        pool->attr.stride,
                        &header, page_size);
        if (!base)
                return NULL;
        range = (struct rseq_mempool_range *) (base - RANGE_HEADER_OFFSET);
        range->pool = pool;
-       range->base = base;
        range->header = header;
+       range->base = base;
+       range->mmap_addr = header;
+       range->mmap_len = page_size + range_len;
+
+       if (pool->attr.populate_policy != RSEQ_MEMPOOL_POPULATE_ALL) {
+               range->init = base + (pool->attr.stride * pool->attr.max_nr_cpus);
+               /* Populate init values pages from memfd */
+               if (rseq_memfd_reserve_init(range->init, pool->attr.stride))
+                       goto error_alloc;
+               if (mmap(range->init, pool->attr.stride, PROT_READ | PROT_WRITE,
+                               MAP_SHARED | MAP_FIXED, memfd.fd,
+                               ptr_to_off_t(range->init)) != (void *) range->init) {
+                       goto error_alloc;
+               }
+               assert(pool->attr.type == MEMPOOL_TYPE_PERCPU);
+               /*
+                * Map per-cpu memory as private COW mappings of init values.
+                */
+               {
+                       int cpu;
+
+                       for (cpu = 0; cpu < pool->attr.max_nr_cpus; cpu++) {
+                               void *p = base + (pool->attr.stride * cpu);
+                               size_t len = pool->attr.stride;
+
+                               if (mmap(p, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED,
+                                               memfd.fd, ptr_to_off_t(range->init)) != (void *) p) {
+                                       goto error_alloc;
+                               }
+                       }
+               }
+       }
+
        if (pool->attr.robust_set) {
                if (create_alloc_bitmap(pool, range))
                        goto error_alloc;
@@ -573,6 +860,48 @@ error_alloc:
        return NULL;
 }
 
+static
+int rseq_mempool_memfd_ref(struct rseq_mempool *pool)
+{
+       int ret = 0;
+
+       if (pool->attr.populate_policy == RSEQ_MEMPOOL_POPULATE_ALL)
+               return 0;
+
+       pthread_mutex_lock(&memfd.lock);
+       if (memfd.refcount == 0) {
+               memfd.fd = memfd_create("mempool", MFD_CLOEXEC);
+               if (memfd.fd < 0) {
+                       perror("memfd_create");
+                       ret = -1;
+                       goto unlock;
+               }
+       }
+       memfd.refcount++;
+unlock:
+       pthread_mutex_unlock(&memfd.lock);
+       return ret;
+}
+
+static
+void rseq_mempool_memfd_unref(struct rseq_mempool *pool)
+{
+       if (pool->attr.populate_policy == RSEQ_MEMPOOL_POPULATE_ALL)
+               return;
+
+       pthread_mutex_lock(&memfd.lock);
+       if (memfd.refcount == 1) {
+               if (close(memfd.fd)) {
+                       perror("close");
+                       abort();
+               }
+               memfd.fd = -1;
+               memfd.reserved_size = 0;
+       }
+       memfd.refcount--;
+       pthread_mutex_unlock(&memfd.lock);
+}
+
 int rseq_mempool_destroy(struct rseq_mempool *pool)
 {
        struct rseq_mempool_range *range, *next_range;
@@ -589,9 +918,10 @@ int rseq_mempool_destroy(struct rseq_mempool *pool)
                /* Update list head to keep list coherent in case of partial failure. */
                pool->range_list = next_range;
        }
+       rseq_mempool_memfd_unref(pool);
        pthread_mutex_destroy(&pool->lock);
        free(pool->name);
-       memset(pool, 0, sizeof(*pool));
+       free(pool);
 end:
        return ret;
 }
@@ -639,12 +969,18 @@ struct rseq_mempool *rseq_mempool_create(const char *pool_name,
                }
                break;
        case MEMPOOL_TYPE_GLOBAL:
+               /* Override populate policy for global type. */
+               attr.populate_policy = RSEQ_MEMPOOL_POPULATE_ALL;
                /* Use a 1-cpu pool for global mempool type. */
                attr.max_nr_cpus = 1;
                break;
        }
        if (!attr.stride)
                attr.stride = RSEQ_MEMPOOL_STRIDE;      /* Use default */
+       if (attr.robust_set && !attr.poison_set) {
+               attr.poison_set = true;
+               attr.poison = DEFAULT_POISON_VALUE;
+       }
        if (item_len > attr.stride || attr.stride < (size_t) rseq_get_page_len() ||
                        !is_pow2(attr.stride)) {
                errno = EINVAL;
@@ -660,6 +996,9 @@ struct rseq_mempool *rseq_mempool_create(const char *pool_name,
        pool->item_len = item_len;
        pool->item_order = order;
 
+       if (rseq_mempool_memfd_ref(pool))
+               goto error_alloc;
+
        pool->range_list = rseq_mempool_range_create(pool);
        if (!pool->range_list)
                goto error_alloc;
@@ -702,26 +1041,32 @@ void set_alloc_slot(struct rseq_mempool *pool, struct rseq_mempool_range *range,
 }
 
 static
-void __rseq_percpu *__rseq_percpu_malloc(struct rseq_mempool *pool, bool zeroed)
+void __rseq_percpu *__rseq_percpu_malloc(struct rseq_mempool *pool,
+               bool zeroed, void *init_ptr, size_t init_len)
 {
        struct rseq_mempool_range *range;
        struct free_list_node *node;
        uintptr_t item_offset;
        void __rseq_percpu *addr;
 
+       if (init_len > pool->item_len) {
+               errno = EINVAL;
+               return NULL;
+       }
        pthread_mutex_lock(&pool->lock);
        /* Get first entry from free list. */
        node = pool->free_list_head;
        if (node != NULL) {
-               uintptr_t ptr = (uintptr_t) node;
-               void *range_base = (void *) (ptr & (~(pool->attr.stride - 1)));
+               void *range_base, *ptr;
 
+               ptr = __rseq_free_list_to_percpu_ptr(pool, node);
+               range_base = (void *) ((uintptr_t) ptr & (~(pool->attr.stride - 1)));
                range = (struct rseq_mempool_range *) (range_base - RANGE_HEADER_OFFSET);
                /* Remove node from free list (update head). */
                pool->free_list_head = node->next;
-               item_offset = (uintptr_t) ((void *) node - range_base);
+               item_offset = (uintptr_t) (ptr - range_base);
                rseq_percpu_check_poison_item(pool, range, item_offset);
-               addr = (void __rseq_percpu *) node;
+               addr = __rseq_free_list_to_percpu_ptr(pool, node);
                goto end;
        }
        /*
@@ -749,19 +1094,31 @@ end:
        if (addr)
                set_alloc_slot(pool, range, item_offset);
        pthread_mutex_unlock(&pool->lock);
-       if (zeroed && addr)
-               rseq_percpu_zero_item(pool, range, item_offset);
+       if (addr) {
+               if (zeroed)
+                       rseq_percpu_zero_item(pool, range, item_offset);
+               else if (init_ptr) {
+                       rseq_percpu_init_item(pool, range, item_offset,
+                                       init_ptr, init_len);
+               }
+       }
        return addr;
 }
 
 void __rseq_percpu *rseq_mempool_percpu_malloc(struct rseq_mempool *pool)
 {
-       return __rseq_percpu_malloc(pool, false);
+       return __rseq_percpu_malloc(pool, false, NULL, 0);
 }
 
 void __rseq_percpu *rseq_mempool_percpu_zmalloc(struct rseq_mempool *pool)
 {
-       return __rseq_percpu_malloc(pool, true);
+       return __rseq_percpu_malloc(pool, true, NULL, 0);
+}
+
+void __rseq_percpu *rseq_mempool_percpu_malloc_init(struct rseq_mempool *pool,
+               void *init_ptr, size_t len)
+{
+       return __rseq_percpu_malloc(pool, false, init_ptr, len);
 }
 
 /* Always inline for __builtin_return_address(0). */
@@ -804,11 +1161,11 @@ void librseq_mempool_percpu_free(void __rseq_percpu *_ptr, size_t stride)
        head = pool->free_list_head;
        if (pool->attr.poison_set)
                rseq_percpu_poison_item(pool, range, item_offset);
-       /* Free-list is in CPU 0 range. */
-       item = (struct free_list_node *) ptr;
+       item = __rseq_percpu_to_free_list_ptr(pool, _ptr);
        /*
         * Setting the next pointer will overwrite the first uintptr_t
-        * poison for CPU 0.
+        * poison for either CPU 0 (populate all) or init data (populate
+        * none).
         */
        item->next = head;
        pool->free_list_head = item;
@@ -864,7 +1221,8 @@ end:
 }
 
 static
-void __rseq_percpu *__rseq_mempool_set_malloc(struct rseq_mempool_set *pool_set, size_t len, bool zeroed)
+void __rseq_percpu *__rseq_mempool_set_malloc(struct rseq_mempool_set *pool_set,
+               void *init_ptr, size_t len, bool zeroed)
 {
        int order, min_order = POOL_SET_MIN_ENTRY;
        struct rseq_mempool *pool;
@@ -888,7 +1246,7 @@ again:
 found:
        pthread_mutex_unlock(&pool_set->lock);
        if (pool) {
-               addr = __rseq_percpu_malloc(pool, zeroed);
+               addr = __rseq_percpu_malloc(pool, zeroed, init_ptr, len);
                if (addr == NULL && errno == ENOMEM) {
                        /*
                         * If the allocation failed, try again with a
@@ -907,12 +1265,18 @@ found:
 
 void __rseq_percpu *rseq_mempool_set_percpu_malloc(struct rseq_mempool_set *pool_set, size_t len)
 {
-       return __rseq_mempool_set_malloc(pool_set, len, false);
+       return __rseq_mempool_set_malloc(pool_set, NULL, len, false);
 }
 
 void __rseq_percpu *rseq_mempool_set_percpu_zmalloc(struct rseq_mempool_set *pool_set, size_t len)
 {
-       return __rseq_mempool_set_malloc(pool_set, len, true);
+       return __rseq_mempool_set_malloc(pool_set, NULL, len, true);
+}
+
+void __rseq_percpu *rseq_mempool_set_percpu_malloc_init(struct rseq_mempool_set *pool_set,
+               void *init_ptr, size_t len)
+{
+       return __rseq_mempool_set_malloc(pool_set, init_ptr, len, true);
 }
 
 struct rseq_mempool_attr *rseq_mempool_attr_create(void)
@@ -1014,6 +1378,17 @@ int rseq_mempool_attr_set_poison(struct rseq_mempool_attr *attr,
        return 0;
 }
 
+int rseq_mempool_attr_set_populate_policy(struct rseq_mempool_attr *attr,
+               enum rseq_mempool_populate_policy policy)
+{
+       if (!attr) {
+               errno = EINVAL;
+               return -1;
+       }
+       attr->populate_policy = policy;
+       return 0;
+}
+
 int rseq_mempool_get_max_nr_cpus(struct rseq_mempool *mempool)
 {
        if (!mempool || mempool->attr.type != MEMPOOL_TYPE_PERCPU) {
This page took 0.033131 seconds and 4 git commands to generate.