brintos

brintos / linux-shallow public Read only

0
0
Text · 9.6 KiB · cb1758e Raw
279 lines · c
1/* SPDX-License-Identifier: GPL-2.0 */2 3#ifndef _LINUX_OBJPOOL_H4#define _LINUX_OBJPOOL_H5 6#include <linux/types.h>7#include <linux/refcount.h>8#include <linux/atomic.h>9#include <linux/cpumask.h>10#include <linux/irqflags.h>11#include <linux/smp.h>12 13/*14 * objpool: ring-array based lockless MPMC queue15 *16 * Copyright: wuqiang.matt@bytedance.com,mhiramat@kernel.org17 *18 * objpool is a scalable implementation of high performance queue for19 * object allocation and reclamation, such as kretprobe instances.20 *21 * With leveraging percpu ring-array to mitigate hot spots of memory22 * contention, it delivers near-linear scalability for high parallel23 * scenarios. The objpool is best suited for the following cases:24 * 1) Memory allocation or reclamation are prohibited or too expensive25 * 2) Consumers are of different priorities, such as irqs and threads26 *27 * Limitations:28 * 1) Maximum objects (capacity) is fixed after objpool creation29 * 2) All pre-allocated objects are managed in percpu ring array,30 *    which consumes more memory than linked lists31 */32 33/**34 * struct objpool_slot - percpu ring array of objpool35 * @head: head sequence of the local ring array (to retrieve at)36 * @tail: tail sequence of the local ring array (to append at)37 * @last: the last sequence number marked as ready for retrieve38 * @mask: bits mask for modulo capacity to compute array indexes39 * @entries: object entries on this slot40 *41 * Represents a cpu-local array-based ring buffer, its size is specialized42 * during initialization of object pool. The percpu objpool node is to be43 * allocated from local memory for NUMA system, and to be kept compact in44 * continuous memory: CPU assigned number of objects are stored just after45 * the body of objpool_node.46 *47 * Real size of the ring array is far too smaller than the value range of48 * head and tail, typed as uint32_t: [0, 2^32), so only lower bits (mask)49 * of head and tail are used as the actual position in the ring array. In50 * general the ring array is acting like a small sliding window, which is51 * always moving forward in the loop of [0, 2^32).52 */53struct objpool_slot {54	uint32_t            head;55	uint32_t            tail;56	uint32_t            last;57	uint32_t            mask;58	void               *entries[];59} __packed;60 61struct objpool_head;62 63/*64 * caller-specified callback for object initial setup, it's only called65 * once for each object (just after the memory allocation of the object)66 */67typedef int (*objpool_init_obj_cb)(void *obj, void *context);68 69/* caller-specified cleanup callback for objpool destruction */70typedef int (*objpool_fini_cb)(struct objpool_head *head, void *context);71 72/**73 * struct objpool_head - object pooling metadata74 * @obj_size:   object size, aligned to sizeof(void *)75 * @nr_objs:    total objs (to be pre-allocated with objpool)76 * @nr_possible_cpus: cached value of num_possible_cpus()77 * @capacity:   max objs can be managed by one objpool_slot78 * @gfp:        gfp flags for kmalloc & vmalloc79 * @ref:        refcount of objpool80 * @flags:      flags for objpool management81 * @cpu_slots:  pointer to the array of objpool_slot82 * @release:    resource cleanup callback83 * @context:    caller-provided context84 */85struct objpool_head {86	int                     obj_size;87	int                     nr_objs;88	int                     nr_possible_cpus;89	int                     capacity;90	gfp_t                   gfp;91	refcount_t              ref;92	unsigned long           flags;93	struct objpool_slot   **cpu_slots;94	objpool_fini_cb         release;95	void                   *context;96};97 98#define OBJPOOL_NR_OBJECT_MAX	(1UL << 24) /* maximum numbers of total objects */99#define OBJPOOL_OBJECT_SIZE_MAX	(1UL << 16) /* maximum size of an object */100 101/**102 * objpool_init() - initialize objpool and pre-allocated objects103 * @pool:    the object pool to be initialized, declared by caller104 * @nr_objs: total objects to be pre-allocated by this object pool105 * @object_size: size of an object (should be > 0)106 * @gfp:     flags for memory allocation (via kmalloc or vmalloc)107 * @context: user context for object initialization callback108 * @objinit: object initialization callback for extra setup109 * @release: cleanup callback for extra cleanup task110 *111 * return value: 0 for success, otherwise error code112 *113 * All pre-allocated objects are to be zeroed after memory allocation.114 * Caller could do extra initialization in objinit callback. objinit()115 * will be called just after slot allocation and called only once for116 * each object. After that the objpool won't touch any content of the117 * objects. It's caller's duty to perform reinitialization after each118 * pop (object allocation) or do clearance before each push (object119 * reclamation).120 */121int objpool_init(struct objpool_head *pool, int nr_objs, int object_size,122		 gfp_t gfp, void *context, objpool_init_obj_cb objinit,123		 objpool_fini_cb release);124 125/* try to retrieve object from slot */126static inline void *__objpool_try_get_slot(struct objpool_head *pool, int cpu)127{128	struct objpool_slot *slot = pool->cpu_slots[cpu];129	/* load head snapshot, other cpus may change it */130	uint32_t head = smp_load_acquire(&slot->head);131 132	while (head != READ_ONCE(slot->last)) {133		void *obj;134 135		/*136		 * data visibility of 'last' and 'head' could be out of137		 * order since memory updating of 'last' and 'head' are138		 * performed in push() and pop() independently139		 *140		 * before any retrieving attempts, pop() must guarantee141		 * 'last' is behind 'head', that is to say, there must142		 * be available objects in slot, which could be ensured143		 * by condition 'last != head && last - head <= nr_objs'144		 * that is equivalent to 'last - head - 1 < nr_objs' as145		 * 'last' and 'head' are both unsigned int32146		 */147		if (READ_ONCE(slot->last) - head - 1 >= pool->nr_objs) {148			head = READ_ONCE(slot->head);149			continue;150		}151 152		/* obj must be retrieved before moving forward head */153		obj = READ_ONCE(slot->entries[head & slot->mask]);154 155		/* move head forward to mark it's consumption */156		if (try_cmpxchg_release(&slot->head, &head, head + 1))157			return obj;158	}159 160	return NULL;161}162 163/**164 * objpool_pop() - allocate an object from objpool165 * @pool: object pool166 *167 * return value: object ptr or NULL if failed168 */169static inline void *objpool_pop(struct objpool_head *pool)170{171	void *obj = NULL;172	unsigned long flags;173	int i, cpu;174 175	/* disable local irq to avoid preemption & interruption */176	raw_local_irq_save(flags);177 178	cpu = raw_smp_processor_id();179	for (i = 0; i < pool->nr_possible_cpus; i++) {180		obj = __objpool_try_get_slot(pool, cpu);181		if (obj)182			break;183		cpu = cpumask_next_wrap(cpu, cpu_possible_mask, -1, 1);184	}185	raw_local_irq_restore(flags);186 187	return obj;188}189 190/* adding object to slot, abort if the slot was already full */191static inline int192__objpool_try_add_slot(void *obj, struct objpool_head *pool, int cpu)193{194	struct objpool_slot *slot = pool->cpu_slots[cpu];195	uint32_t head, tail;196 197	/* loading tail and head as a local snapshot, tail first */198	tail = READ_ONCE(slot->tail);199 200	do {201		head = READ_ONCE(slot->head);202		/* fault caught: something must be wrong */203		WARN_ON_ONCE(tail - head > pool->nr_objs);204	} while (!try_cmpxchg_acquire(&slot->tail, &tail, tail + 1));205 206	/* now the tail position is reserved for the given obj */207	WRITE_ONCE(slot->entries[tail & slot->mask], obj);208	/* update sequence to make this obj available for pop() */209	smp_store_release(&slot->last, tail + 1);210 211	return 0;212}213 214/**215 * objpool_push() - reclaim the object and return back to objpool216 * @obj:  object ptr to be pushed to objpool217 * @pool: object pool218 *219 * return: 0 or error code (it fails only when user tries to push220 * the same object multiple times or wrong "objects" into objpool)221 */222static inline int objpool_push(void *obj, struct objpool_head *pool)223{224	unsigned long flags;225	int rc;226 227	/* disable local irq to avoid preemption & interruption */228	raw_local_irq_save(flags);229	rc = __objpool_try_add_slot(obj, pool, raw_smp_processor_id());230	raw_local_irq_restore(flags);231 232	return rc;233}234 235 236/**237 * objpool_drop() - discard the object and deref objpool238 * @obj:  object ptr to be discarded239 * @pool: object pool240 *241 * return: 0 if objpool was released; -EAGAIN if there are still242 *         outstanding objects243 *244 * objpool_drop is normally for the release of outstanding objects245 * after objpool cleanup (objpool_fini). Thinking of this example:246 * kretprobe is unregistered and objpool_fini() is called to release247 * all remained objects, but there are still objects being used by248 * unfinished kretprobes (like blockable function: sys_accept). So249 * only when the last outstanding object is dropped could the whole250 * objpool be released along with the call of objpool_drop()251 */252int objpool_drop(void *obj, struct objpool_head *pool);253 254/**255 * objpool_free() - release objpool forcely (all objects to be freed)256 * @pool: object pool to be released257 */258void objpool_free(struct objpool_head *pool);259 260/**261 * objpool_fini() - deref object pool (also releasing unused objects)262 * @pool: object pool to be dereferenced263 *264 * objpool_fini() will try to release all remained free objects and265 * then drop an extra reference of the objpool. If all objects are266 * already returned to objpool (so called synchronous use cases),267 * the objpool itself will be freed together. But if there are still268 * outstanding objects (so called asynchronous use cases, such like269 * blockable kretprobe), the objpool won't be released until all270 * the outstanding objects are dropped, but the caller must assure271 * there are no concurrent objpool_push() on the fly. Normally RCU272 * is being required to make sure all ongoing objpool_push() must273 * be finished before calling objpool_fini(), so does test_objpool,274 * kretprobe or rethook275 */276void objpool_fini(struct objpool_head *pool);277 278#endif /* _LINUX_OBJPOOL_H */279