kernel/bpf/kmem_cache_iter.c

Source file repositories/reference/linux-study-clean/kernel/bpf/kmem_cache_iter.c

File Facts

System
Linux kernel
Corpus path
kernel/bpf/kmem_cache_iter.c
Extension
.c
Size
5685 bytes
Lines
239
Domain
Core OS
Bucket
Scheduler, Processes, Timers, Sync, And Syscalls
Inferred role
Core OS: implementation source
Status
source implementation candidate

Why This File Exists

Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.

Dependency Surface

Detected Declarations

Annotated Snippet

struct bpf_iter_kmem_cache {
	__u64 __opaque[1];
} __attribute__((aligned(8)));

struct bpf_iter_kmem_cache_kern {
	struct kmem_cache *pos;
} __attribute__((aligned(8)));

#define KMEM_CACHE_POS_START  ((void *)1L)

__bpf_kfunc_start_defs();

__bpf_kfunc int bpf_iter_kmem_cache_new(struct bpf_iter_kmem_cache *it)
{
	struct bpf_iter_kmem_cache_kern *kit = (void *)it;

	BUILD_BUG_ON(sizeof(*kit) > sizeof(*it));
	BUILD_BUG_ON(__alignof__(*kit) != __alignof__(*it));

	kit->pos = KMEM_CACHE_POS_START;
	return 0;
}

__bpf_kfunc struct kmem_cache *bpf_iter_kmem_cache_next(struct bpf_iter_kmem_cache *it)
{
	struct bpf_iter_kmem_cache_kern *kit = (void *)it;
	struct kmem_cache *prev = kit->pos;
	struct kmem_cache *next;
	bool destroy = false;

	if (!prev)
		return NULL;

	mutex_lock(&slab_mutex);

	if (list_empty(&slab_caches)) {
		mutex_unlock(&slab_mutex);
		return NULL;
	}

	if (prev == KMEM_CACHE_POS_START)
		next = list_first_entry(&slab_caches, struct kmem_cache, list);
	else if (list_last_entry(&slab_caches, struct kmem_cache, list) == prev)
		next = NULL;
	else
		next = list_next_entry(prev, list);

	/* boot_caches have negative refcount, don't touch them */
	if (next && next->refcount > 0)
		next->refcount++;

	/* Skip kmem_cache_destroy() for active entries */
	if (prev && prev != KMEM_CACHE_POS_START) {
		if (prev->refcount > 1)
			prev->refcount--;
		else if (prev->refcount == 1)
			destroy = true;
	}

	mutex_unlock(&slab_mutex);

	if (destroy)
		kmem_cache_destroy(prev);

	kit->pos = next;
	return next;
}

__bpf_kfunc void bpf_iter_kmem_cache_destroy(struct bpf_iter_kmem_cache *it)
{
	struct bpf_iter_kmem_cache_kern *kit = (void *)it;
	struct kmem_cache *s = kit->pos;
	bool destroy = false;

	if (s == NULL || s == KMEM_CACHE_POS_START)
		return;

	mutex_lock(&slab_mutex);

	/* Skip kmem_cache_destroy() for active entries */
	if (s->refcount > 1)
		s->refcount--;
	else if (s->refcount == 1)
		destroy = true;

	mutex_unlock(&slab_mutex);

	if (destroy)
		kmem_cache_destroy(s);
}

Annotation

Implementation Notes