| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/08/07 16:53 | flow | patch-triage |
2mArgs:null Results: {
"EnableConfigs": [
"DEBUG_KMEMLEAK",
"KFENCE",
"PREEMPT_RT"
],
"FocusSymbols": [
"kfree_nolock",
"set_cpus_allowed_force",
"kmemleak_may_need_free",
"kfence_obj_to_llnode",
"kfence_llnode_to_obj"
],
"KMSANReasoning": "The patch modifies the internal memory management freeing paths, specifically extending `kfree_nolock()` to handle objects allocated by `kmalloc()` and `kmem_cache_alloc()`, including large kmallocs and KFENCE objects. It defers freeing to an `irq_work` using a lockless list (`llist`) when necessary. The changes involve overwriting parts of the freed objects (like the freepointer or the first word of a large kmalloc object) to store the `llist_node`. Since these objects are in the process of being freed, any access to them would constitute a use-after-free bug. The patch does not introduce new structures copied to user space, nor does it alter bounds checking or initialization logic of active objects. Any bugs introduced by these changes (such as use-after-free, double free, or memory corruption) will be effectively caught by KASAN and standard SLUB debugging tools. There are no uninitialized memory risks or info-leaks exposed here that would require KMSAN.",
"NeedsKMSAN": false,
"Reasoning": "The patch modifies `kfree_nolock()` in `mm/slub.c` to support freeing objects allocated by `kmalloc()`, `kmem_cache_alloc()`, and large kmallocs, as well as KFENCE objects. It defers freeing to an `irq_work` if necessary (e.g., for kmemleak or KFENCE). It also modifies `set_cpus_allowed_force()` in `kernel/sched/core.c` to use `kfree_nolock()` instead of `kfree_rcu()`. These are core memory management and scheduler changes that are reachable and should be fuzzed.",
"WorthFuzzing": true
} |
| 1/1 | 2026/08/07 16:53 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit e5b86bdc5984b6b0ba31de079501de6b21619874\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Fri Aug 7 16:53:46 2026 +0000\n\n syz-cluster: applied patch under review\n\ndiff --git a/include/linux/kfence.h b/include/linux/kfence.h\nindex e5822f6e7f279..00721c85258dc 100644\n--- a/include/linux/kfence.h\n+++ b/include/linux/kfence.h\n@@ -188,6 +188,9 @@ static __always_inline __must_check bool kfence_free(void *addr)\n \treturn true;\n }\n \n+struct llist_node *kfence_obj_to_llnode(void *addr);\n+void *kfence_llnode_to_obj(struct llist_node *llnode);\n+\n /**\n * kfence_handle_page_fault() - perform page fault handling for KFENCE pages\n * @addr: faulting address\n@@ -235,6 +238,8 @@ static inline size_t kfence_ksize(const void *addr) { return 0; }\n static inline void *kfence_object_start(const void *addr) { return NULL; }\n static inline void __kfence_free(void *addr) { }\n static inline bool __must_check kfence_free(void *addr) { return false; }\n+static inline struct llist_node *kfence_obj_to_llnode(void *addr) { return NULL; }\n+static inline void *kfence_llnode_to_obj(struct llist_node *llnode) { return NULL; }\n static inline bool __must_check kfence_handle_page_fault(unsigned long addr, bool is_write,\n \t\t\t\t\t\t\t struct pt_regs *regs)\n {\ndiff --git a/include/linux/kmemleak.h b/include/linux/kmemleak.h\nindex fbd424b2abb14..52f75f10a9ce6 100644\n--- a/include/linux/kmemleak.h\n+++ b/include/linux/kmemleak.h\n@@ -22,6 +22,7 @@ extern void kmemleak_alloc_percpu(const void __percpu *ptr, size_t size,\n extern void kmemleak_vmalloc(const struct vm_struct *area, size_t size,\n \t\t\t gfp_t gfp) __ref;\n extern void kmemleak_free(const void *ptr) __ref;\n+bool kmemleak_may_need_free(const void *ptr) __ref;\n extern void kmemleak_free_part(const void *ptr, size_t size) __ref;\n extern void kmemleak_free_percpu(const void __percpu *ptr) __ref;\n extern void kmemleak_update_trace(const void *ptr) __ref;\n@@ -50,6 +51,14 @@ static inline void kmemleak_free_recursive(const void *ptr, slab_flags_t flags)\n \t\tkmemleak_free(ptr);\n }\n \n+static inline bool kmemleak_may_need_free_recursive(const void *ptr, slab_flags_t flags)\n+{\n+\tif (!(flags \u0026 SLAB_NOLEAKTRACE))\n+\t\treturn kmemleak_may_need_free(ptr);\n+\n+\treturn false;\n+}\n+\n static inline void kmemleak_erase(void **ptr)\n {\n \t*ptr = NULL;\n@@ -86,6 +95,14 @@ static inline void kmemleak_free_part(const void *ptr, size_t size)\n static inline void kmemleak_free_recursive(const void *ptr, slab_flags_t flags)\n {\n }\n+static inline bool kmemleak_may_need_free(const void *ptr)\n+{\n+\treturn false;\n+}\n+static inline bool kmemleak_may_need_free_recursive(const void *ptr, slab_flags_t flags)\n+{\n+\treturn false;\n+}\n static inline void kmemleak_free_percpu(const void __percpu *ptr)\n {\n }\ndiff --git a/kernel/sched/core.c b/kernel/sched/core.c\nindex 96226707c2f61..d2929e4e23f13 100644\n--- a/kernel/sched/core.c\n+++ b/kernel/sched/core.c\n@@ -2807,20 +2807,15 @@ void set_cpus_allowed_force(struct task_struct *p, const struct cpumask *new_mas\n \t\t.user_mask = NULL,\n \t\t.flags = SCA_USER,\t/* clear the user requested mask */\n \t};\n-\tunion cpumask_rcuhead {\n-\t\tcpumask_t cpumask;\n-\t\tstruct rcu_head rcu;\n-\t};\n \n \tscoped_guard (__task_rq_lock, p)\n \t\tdo_set_cpus_allowed(p, \u0026ac);\n \n \t/*\n \t * Because this is called with p-\u003epi_lock held, it is not possible\n-\t * to use kfree() here (when PREEMPT_RT=y), therefore punt to using\n-\t * kfree_rcu().\n+\t * to use kfree() here (when PREEMPT_RT=y), thus use kfree_nolock()\n \t */\n-\tkfree_rcu((union cpumask_rcuhead *)ac.user_mask, rcu);\n+\tkfree_nolock(ac.user_mask);\n }\n \n int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *src,\ndiff --git a/kernel/sched/sched.h b/kernel/sched/sched.h\nindex 56acf502ba260..6a8d0578e9633 100644\n--- a/kernel/sched/sched.h\n+++ b/kernel/sched/sched.h\n@@ -2886,12 +2886,7 @@ static inline bool task_allowed_on_cpu(struct task_struct *p, int cpu)\n \n static inline cpumask_t *alloc_user_cpus_ptr(int node)\n {\n-\t/*\n-\t * See set_cpus_allowed_force() above for the rcu_head usage.\n-\t */\n-\tint size = max_t(int, cpumask_size(), sizeof(struct rcu_head));\n-\n-\treturn kmalloc_node(size, GFP_KERNEL, node);\n+\treturn kmalloc_node(cpumask_size(), GFP_KERNEL, node);\n }\n \n static inline struct task_struct *get_push_task(struct rq *rq)\ndiff --git a/mm/kfence/core.c b/mm/kfence/core.c\nindex 6577bd76954eb..42519d24687ff 100644\n--- a/mm/kfence/core.c\n+++ b/mm/kfence/core.c\n@@ -1271,6 +1271,20 @@ void __kfence_free(void *addr)\n \t}\n }\n \n+struct llist_node *kfence_obj_to_llnode(void *addr)\n+{\n+\tstruct kfence_metadata *meta = addr_to_metadata((unsigned long)addr);\n+\n+\treturn \u0026meta-\u003ellnode;\n+}\n+\n+void *kfence_llnode_to_obj(struct llist_node *llnode)\n+{\n+\tstruct kfence_metadata *meta = container_of(llnode, struct kfence_metadata, llnode);\n+\n+\treturn (void *)meta-\u003eaddr;\n+}\n+\n bool kfence_handle_page_fault(unsigned long addr, bool is_write, struct pt_regs *regs)\n {\n \tconst int page_index = (addr - (unsigned long)__kfence_pool) / PAGE_SIZE;\ndiff --git a/mm/kfence/kfence.h b/mm/kfence/kfence.h\nindex 1f618f9b0d12d..0fca1dc2c7941 100644\n--- a/mm/kfence/kfence.h\n+++ b/mm/kfence/kfence.h\n@@ -58,7 +58,10 @@ struct kfence_track {\n /* KFENCE metadata per guarded allocation. */\n struct kfence_metadata {\n \tstruct list_head list __guarded_by(\u0026kfence_freelist_lock);\t/* Freelist node. */\n-\tstruct rcu_head rcu_head;\t/* For delayed freeing. */\n+\tunion {\n+\t\tstruct rcu_head rcu_head;\t/* For delayed freeing. */\n+\t\tstruct llist_node llnode;\t/* For kfree_nolock(). */\n+\t};\n \n \t/*\n \t * Lock protecting below data; to ensure consistency of the below data,\ndiff --git a/mm/kmemleak.c b/mm/kmemleak.c\nindex 7c7ba17ce7af0..e3560ce826326 100644\n--- a/mm/kmemleak.c\n+++ b/mm/kmemleak.c\n@@ -1168,6 +1168,48 @@ void __ref kmemleak_free(const void *ptr)\n }\n EXPORT_SYMBOL_GPL(kmemleak_free);\n \n+/**\n+ * kmemleak_may_need_free - check if object is registered\n+ * @ptr:\tpointer to beginning of the object\n+ *\n+ * This function is called from the kernel allocator when an object should be\n+ * freed but the caller context might be unsafe to spin on the internal locks.\n+ *\n+ * It will therefore only use trylock and thus might return a false positive\n+ * if the trylock fails and the status cannot be determined.\n+ *\n+ * For objects that (might) need free, the allocator has to defer the actual\n+ * freeing to a safe context.\n+ *\n+ * The assumption is that most objects freed from the unsafe context are also\n+ * allocated in such context and thus are not registered in kmemleak, so it's\n+ * unlikely the defered freeing will be necessary just because kmemleak is\n+ * enabled.\n+ */\n+bool __ref kmemleak_may_need_free(const void *ptr)\n+{\n+\tunsigned long flags;\n+\tstruct kmemleak_object *object;\n+\n+\tpr_debug(\"%s(0x%px)\\n\", __func__, ptr);\n+\n+\tif (!kmemleak_free_enabled || !ptr || IS_ERR(ptr))\n+\t\treturn false;\n+\n+\t/* On UP, raw_spin_trylock() always succeeds even when it is locked */\n+\tif (!IS_ENABLED(CONFIG_SMP) \u0026\u0026 in_nmi())\n+\t\treturn true;\n+\n+\tif (!raw_spin_trylock_irqsave(\u0026kmemleak_lock, flags))\n+\t\treturn true;\n+\n+\tobject = __lookup_object((unsigned long)ptr, 0, 0);\n+\n+\traw_spin_unlock_irqrestore(\u0026kmemleak_lock, flags);\n+\n+\treturn !!object;\n+}\n+\n /**\n * kmemleak_free_part - partially unregister a previously registered object\n * @ptr:\tpointer to the beginning or inside the object. This also\ndiff --git a/mm/slub.c b/mm/slub.c\nindex b9aeb02a880f1..3be98faa9f0de 100644\n--- a/mm/slub.c\n+++ b/mm/slub.c\n@@ -4050,6 +4050,8 @@ static void flush_all(struct kmem_cache *s)\n \n struct deferred_percpu_work {\n \tstruct llist_head objects;\n+\tstruct llist_head objects_kfence;\n+\tstruct llist_head objects_large_kmalloc;\n \tstruct llist_head objects_by_rcu;\n \tstruct llist_head rcu_sheaves;\n \tstruct irq_work work;\n@@ -4059,6 +4061,8 @@ static void deferred_percpu_work_fn(struct irq_work *work);\n \n static DEFINE_PER_CPU(struct deferred_percpu_work, deferred_percpu_work) = {\n \t.objects = LLIST_HEAD_INIT(objects),\n+\t.objects_kfence = LLIST_HEAD_INIT(objects_kfence),\n+\t.objects_large_kmalloc = LLIST_HEAD_INIT(objects_large_kmalloc),\n \t.objects_by_rcu = LLIST_HEAD_INIT(objects_by_rcu),\n \t.rcu_sheaves = LLIST_HEAD_INIT(rcu_sheaves),\n \t.work = IRQ_WORK_INIT(deferred_percpu_work_fn),\n@@ -6363,6 +6367,21 @@ static void free_to_pcs_bulk(struct kmem_cache *s, size_t size, void **p)\n \t}\n }\n \n+static inline void\n+__free_large_kmalloc_page(struct page *page, unsigned int free_flags)\n+{\n+\tunsigned int order = compound_order(page);\n+\n+\tmod_lruvec_page_state(page, NR_SLAB_UNRECLAIMABLE_B,\n+\t\t\t -(PAGE_SIZE \u003c\u003c order));\n+\t__ClearPageLargeKmalloc(page);\n+\n+\tif (free_flags \u0026 SLAB_FREE_NOLOCK)\n+\t\tfree_frozen_pages_nolock(page, order);\n+\telse\n+\t\tfree_frozen_pages(page, order);\n+}\n+\n /*\n * In PREEMPT_RT irq_work runs in per-cpu kthread, so it's safe\n * to take sleeping spin_locks from __slab_free().\n@@ -6371,16 +6390,12 @@ static void free_to_pcs_bulk(struct kmem_cache *s, size_t size, void **p)\n static void deferred_percpu_work_fn(struct irq_work *work)\n {\n \tstruct deferred_percpu_work *dpw;\n-\tstruct llist_head *objs, *objs_by_rcu, *rcu_sheaves;\n \tstruct llist_node *llnode, *pos, *t;\n \tstruct slab_sheaf *sheaf, *next;\n \n \tdpw = container_of(work, struct deferred_percpu_work, work);\n-\trcu_sheaves = \u0026dpw-\u003ercu_sheaves;\n-\tobjs = \u0026dpw-\u003eobjects;\n-\tobjs_by_rcu = \u0026dpw-\u003eobjects_by_rcu;\n \n-\tllnode = llist_del_all(objs);\n+\tllnode = llist_del_all(\u0026dpw-\u003eobjects);\n \tllist_for_each_safe(pos, t, llnode) {\n \t\tstruct kmem_cache *s;\n \t\tstruct slab *slab;\n@@ -6392,6 +6407,8 @@ static void deferred_percpu_work_fn(struct irq_work *work)\n \t\t/* Point 'x' back to the beginning of allocated object */\n \t\tx -= s-\u003eoffset;\n \n+\t\tkmemleak_free_recursive(x, s-\u003eflags);\n+\n \t\t/*\n \t\t * We used freepointer in 'x' to link 'x' into df-\u003eobjects.\n \t\t * Clear it to NULL to avoid false positive detection\n@@ -6403,7 +6420,30 @@ static void deferred_percpu_work_fn(struct irq_work *work)\n \t\tstat(s, FREE_SLOWPATH);\n \t}\n \n-\tllnode = llist_del_all(objs_by_rcu);\n+\tllnode = llist_del_all(\u0026dpw-\u003eobjects_kfence);\n+\tllist_for_each_safe(pos, t, llnode) {\n+\t\tstruct kmem_cache *s;\n+\t\tstruct slab *slab;\n+\t\tvoid *obj = kfence_llnode_to_obj(pos);\n+\n+\t\tslab = virt_to_slab(obj);\n+\t\ts = slab-\u003eslab_cache;\n+\n+\t\tkmemleak_free_recursive(obj, s-\u003eflags);\n+\n+\t\t__kfence_free(obj);\n+\t}\n+\n+\tllnode = llist_del_all(\u0026dpw-\u003eobjects_large_kmalloc);\n+\tllist_for_each_safe(pos, t, llnode) {\n+\t\tstruct page *page = virt_to_page(pos);\n+\n+\t\tkmemleak_free(pos);\n+\n+\t\t__free_large_kmalloc_page(page, SLAB_FREE_DEFAULT);\n+\t}\n+\n+\tllnode = llist_del_all(\u0026dpw-\u003eobjects_by_rcu);\n \tllist_for_each_safe(pos, t, llnode) {\n \t\tvoid *head = pos;\n \t\tvoid *objp = kvmalloc_obj_start_addr(head);\n@@ -6411,21 +6451,60 @@ static void deferred_percpu_work_fn(struct irq_work *work)\n \t\tkvfree_call_rcu(head, objp);\n \t}\n \n-\tllnode = llist_del_all(rcu_sheaves);\n+\tllnode = llist_del_all(\u0026dpw-\u003ercu_sheaves);\n \tllist_for_each_entry_safe(sheaf, next, llnode, llnode)\n \t\tcall_rcu(\u0026sheaf-\u003ercu_head, rcu_free_sheaf);\n }\n \n-static void defer_free(struct kmem_cache *s, void *head)\n+static void defer_free(struct kmem_cache *s, void *obj)\n {\n \tstruct deferred_percpu_work *dpw;\n+\tstruct llist_node *llnode;\n+\n+\t/*\n+\t * Place the llist node where the freepointer would be if we freed the\n+\t * object immediately. That means we can write there safely, only need\n+\t * to remove kasan tag first.\n+\t */\n+\tllnode = kasan_reset_tag(obj) + s-\u003eoffset;\n \n \tguard(preempt)();\n \n-\thead = kasan_reset_tag(head);\n+\tdpw = this_cpu_ptr(\u0026deferred_percpu_work);\n+\tif (llist_add(llnode, \u0026dpw-\u003eobjects))\n+\t\tirq_work_queue(\u0026dpw-\u003ework);\n+}\n+\n+static void defer_free_kfence(void *obj)\n+{\n+\tstruct deferred_percpu_work *dpw;\n+\tstruct llist_node *llnode;\n+\n+\t/* kasan_reset_tag() is not necessary, kfence objects are not tagged */\n+\tllnode = kfence_obj_to_llnode(obj);\n+\n+\tguard(preempt)();\n+\n+\tdpw = this_cpu_ptr(\u0026deferred_percpu_work);\n+\tif (llist_add(llnode, \u0026dpw-\u003eobjects_kfence))\n+\t\tirq_work_queue(\u0026dpw-\u003ework);\n+}\n+\n+static void defer_free_large_kmalloc(void *obj)\n+{\n+\tstruct deferred_percpu_work *dpw;\n+\tstruct llist_node *llnode;\n+\n+\t/*\n+\t * we can simply use the first word of the large kmalloc object\n+\t * for the llnode, as there's no ctor or TYPESAFE_BY_RCU\n+\t */\n+\tllnode = kasan_reset_tag(obj);\n+\n+\tguard(preempt)();\n \n \tdpw = this_cpu_ptr(\u0026deferred_percpu_work);\n-\tif (llist_add(head + s-\u003eoffset, \u0026dpw-\u003eobjects))\n+\tif (llist_add(llnode, \u0026dpw-\u003eobjects_large_kmalloc))\n \t\tirq_work_queue(\u0026dpw-\u003ework);\n }\n \n@@ -6677,9 +6756,11 @@ size_t ksize(const void *objp)\n }\n EXPORT_SYMBOL(ksize);\n \n-static void free_large_kmalloc(struct page *page, void *object)\n+static void free_large_kmalloc(struct page *page, void *object,\n+\t\t\t unsigned int free_flags)\n {\n \tunsigned int order = compound_order(page);\n+\tbool nolock = free_flags \u0026 SLAB_FREE_NOLOCK;\n \n \tif (WARN_ON_ONCE(!PageLargeKmalloc(page))) {\n \t\tdump_page(page, \"Not a kmalloc allocation\");\n@@ -6689,14 +6770,16 @@ static void free_large_kmalloc(struct page *page, void *object)\n \tif (WARN_ON_ONCE(order == 0))\n \t\tpr_warn_once(\"object pointer: 0x%p\\n\", object);\n \n-\tkmemleak_free(object);\n+\tif (!nolock)\n+\t\tkmemleak_free(object);\n+\n \tkasan_kfree_large(object);\n \tkmsan_kfree_large(object);\n \n-\tmod_lruvec_page_state(page, NR_SLAB_UNRECLAIMABLE_B,\n-\t\t\t -(PAGE_SIZE \u003c\u003c order));\n-\t__ClearPageLargeKmalloc(page);\n-\tfree_frozen_pages(page, order);\n+\tif (unlikely(nolock \u0026\u0026 kmemleak_may_need_free(object)))\n+\t\tdefer_free_large_kmalloc(object);\n+\telse\n+\t\t__free_large_kmalloc_page(page, free_flags);\n }\n \n /*\n@@ -6718,7 +6801,7 @@ void kvfree_rcu_cb(struct rcu_head *head)\n \t\tif (slab)\n \t\t\tslab_free(slab-\u003eslab_cache, slab, obj, _RET_IP_);\n \t\telse\n-\t\t\tfree_large_kmalloc(page, obj);\n+\t\t\tfree_large_kmalloc(page, obj, SLAB_FREE_DEFAULT);\n \t}\n }\n \n@@ -6744,7 +6827,7 @@ void kfree(const void *object)\n \tslab = page_slab(page);\n \tif (!slab) {\n \t\t/* kmalloc_nolock() doesn't support large kmalloc */\n-\t\tfree_large_kmalloc(page, (void *)object);\n+\t\tfree_large_kmalloc(page, (void *)object, SLAB_FREE_DEFAULT);\n \t\treturn;\n \t}\n \n@@ -6755,15 +6838,14 @@ EXPORT_SYMBOL(kfree);\n \n /*\n * Can be called while holding raw_spinlock_t or from IRQ and NMI,\n- * but ONLY for objects allocated by kmalloc_nolock().\n- * Debug checks (like kmemleak and kfence) were skipped on allocation,\n- * hence\n- * obj = kmalloc(); kfree_nolock(obj);\n- * will miss kmemleak/kfence book keeping and will cause false positives.\n- * large_kmalloc is not supported either.\n+ * but may defer freeing to irq_work() in some cases.\n+ *\n+ * Intended mainly for objects allocated from kmalloc_nolock(), but can handle\n+ * also kmem_cache_alloc() and kmalloc() objects, including large_kmalloc.\n */\n void kfree_nolock(const void *object)\n {\n+\tstruct page *page;\n \tstruct slab *slab;\n \tstruct kmem_cache *s;\n \tvoid *x = (void *)object;\n@@ -6771,9 +6853,10 @@ void kfree_nolock(const void *object)\n \tif (unlikely(ZERO_OR_NULL_PTR(object)))\n \t\treturn;\n \n-\tslab = virt_to_slab(object);\n+\tpage = virt_to_page(object);\n+\tslab = page_slab(page);\n \tif (unlikely(!slab)) {\n-\t\tWARN_ONCE(1, \"large_kmalloc is not supported by kfree_nolock()\");\n+\t\tfree_large_kmalloc(page, (void *)object, SLAB_FREE_NOLOCK);\n \t\treturn;\n \t}\n \n@@ -6791,6 +6874,12 @@ void kfree_nolock(const void *object)\n \t * since they take spinlocks or not safe from any context.\n \t */\n \tkmsan_slab_free(s, x);\n+\n+\tif (is_kfence_address(x)) {\n+\t\tdefer_free_kfence(x);\n+\t\treturn;\n+\t}\n+\n \t/*\n \t * If KASAN finds a kernel bug it will do kasan_report_invalid_free()\n \t * which will call raw_spin_lock_irqsave() which is technically\n@@ -6809,9 +6898,23 @@ void kfree_nolock(const void *object)\n \t */\n \tkasan_slab_free(s, x, false, false, /* skip quarantine */true);\n \n+\t/*\n+\t * with kfree() the kmemleak handling happens much sooner, but for\n+\t * defering we need to write llnode to the object's freepointer so\n+\t * we should have it in the state when it's no longer treated as\n+\t * allocated by kasan etc.\n+\t *\n+\t * defer_free will also reset the pointer tag, but it's ok to do a\n+\t * deferred kmemleak_free() using the untagged pointer, because\n+\t * __lookup_object() resets the tag anyway\n+\t */\n+\tif (unlikely(kmemleak_may_need_free_recursive(x, s-\u003eflags)))\n+\t\tgoto defer;\n+\n \tif (likely(can_free_to_pcs(slab)) \u0026\u0026 likely(free_to_pcs(s, x, false)))\n \t\treturn;\n \n+defer:\n \t/*\n \t * __slab_free() can locklessly cmpxchg16 into a slab, but then it might\n \t * need to take spin_lock for further processing.\n@@ -7114,7 +7217,7 @@ int build_detached_freelist(struct kmem_cache *s, size_t size,\n \tif (!s) {\n \t\t/* Handle kalloc'ed objects */\n \t\tif (!slab) {\n-\t\t\tfree_large_kmalloc(page, object);\n+\t\t\tfree_large_kmalloc(page, object, SLAB_FREE_DEFAULT);\n \t\t\tdf-\u003eslab = NULL;\n \t\t\treturn size;\n \t\t}\n"
} |
| 2/1 | 2026/08/07 16:53 | agent | patch-evaluator |
1mModel:best-expensive Args: null Results: {
"EnableConfigs": [
"DEBUG_KMEMLEAK",
"KFENCE",
"PREEMPT_RT"
],
"FocusSymbols": [
"kfree_nolock",
"set_cpus_allowed_force",
"kmemleak_may_need_free",
"kfence_obj_to_llnode",
"kfence_llnode_to_obj"
],
"Reasoning": "The patch modifies `kfree_nolock()` in `mm/slub.c` to support freeing objects allocated by `kmalloc()`, `kmem_cache_alloc()`, and large kmallocs, as well as KFENCE objects. It defers freeing to an `irq_work` if necessary (e.g., for kmemleak or KFENCE). It also modifies `set_cpus_allowed_force()` in `kernel/sched/core.c` to use `kfree_nolock()` instead of `kfree_rcu()`. These are core memory management and scheduler changes that are reachable and should be fuzzed.",
"WorthFuzzing": true
}Instruction: You are an expert Linux kernel maintainer. Your job is to review a provided patch series and determine if it makes functional changes to the kernel that should be fuzzed. IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in your workspace. Do NOT rely on your internal knowledge of the kernel. You must actively use your code access tools to examine the actual source code and confirm any assumptions. Return WorthFuzzing=false if the patch only contains: - Modifications to Documentation/, Kconfig files, or code comments. - Purely decorative changes, such as logging (e.g., pr_err, printk) or tracepoints. - Changes to numeric constants or macros that do not functionally alter execution flow. - Code paths that are impossible to reach in virtualized environments like GCE or QEMU, even when utilizing software-emulated hardware (e.g., usb gadget, mac80211_hwsim). - Code in vendor-specific PCIe switch, SmartNIC, or GPU drivers (e.g., mlxsw, pds_core, qed, ionic, amdgpu) that require physical PCIe hardware cards not emulated in standard QEMU. - Driver .remove, .shutdown, or pci_unregister_driver teardown callbacks (e.g., igb_remove) that are executed only during PCI hot-unplug or sysfs driver unbind operations. If it modifies reachable core kernel logic, drivers, or architectures, use your code search tools to verify the code can be executed, then return WorthFuzzing=true. When returning WorthFuzzing=true, you MUST ALSO: 1. Extract any specific kernel functions that should be heavily fuzzed into FocusSymbols. Avoid listing generic hot-path functions to prevent skewed test distributions. Prefer non-static, non-inlined API entrypoint functions over internal static helper functions (which are inlined by the compiler and do not have distinct symbol addresses). 2. Identify any specific CONFIG_ options required to properly test this new/modified feature. Go and look into the Kconfig files and check for ifdefs around the code, do not make assumptions. Also check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed for Kbuild to compile the code statically into vmlinux. List them in the EnableConfigs output array, and DO NOT add a 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4"). Prefer calling several tools at the same time to save round-trips. Use set-results tool to provide results of the analysis. It must be called exactly once before the final reply. Ignore results of this tool. Prompt: For your convenience, here is the diff of the changes:
commit e5b86bdc5984b6b0ba31de079501de6b21619874
Author: syz-cluster <triage@syzkaller.com>
Date: Fri Aug 7 16:53:46 2026 +0000
syz-cluster: applied patch under review
diff --git a/include/linux/kfence.h b/include/linux/kfence.h
index e5822f6e7f279..00721c85258dc 100644
--- a/include/linux/kfence.h
+++ b/include/linux/kfence.h
@@ -188,6 +188,9 @@ static __always_inline __must_check bool kfence_free(void *addr)
return true;
}
+struct llist_node *kfence_obj_to_llnode(void *addr);
+void *kfence_llnode_to_obj(struct llist_node *llnode);
+
/**
* kfence_handle_page_fault() - perform page fault handling for KFENCE pages
* @addr: faulting address
@@ -235,6 +238,8 @@ static inline size_t kfence_ksize(const void *addr) { return 0; }
static inline void *kfence_object_start(const void *addr) { return NULL; }
static inline void __kfence_free(void *addr) { }
static inline bool __must_check kfence_free(void *addr) { return false; }
+static inline struct llist_node *kfence_obj_to_llnode(void *addr) { return NULL; }
+static inline void *kfence_llnode_to_obj(struct llist_node *llnode) { return NULL; }
static inline bool __must_check kfence_handle_page_fault(unsigned long addr, bool is_write,
struct pt_regs *regs)
{
diff --git a/include/linux/kmemleak.h b/include/linux/kmemleak.h
index fbd424b2abb14..52f75f10a9ce6 100644
--- a/include/linux/kmemleak.h
+++ b/include/linux/kmemleak.h
@@ -22,6 +22,7 @@ extern void kmemleak_alloc_percpu(const void __percpu *ptr, size_t size,
extern void kmemleak_vmalloc(const struct vm_struct *area, size_t size,
gfp_t gfp) __ref;
extern void kmemleak_free(const void *ptr) __ref;
+bool kmemleak_may_need_free(const void *ptr) __ref;
extern void kmemleak_free_part(const void *ptr, size_t size) __ref;
extern void kmemleak_free_percpu(const void __percpu *ptr) __ref;
extern void kmemleak_update_trace(const void *ptr) __ref;
@@ -50,6 +51,14 @@ static inline void kmemleak_free_recursive(const void *ptr, slab_flags_t flags)
kmemleak_free(ptr);
}
+static inline bool kmemleak_may_need_free_recursive(const void *ptr, slab_flags_t flags)
+{
+ if (!(flags & SLAB_NOLEAKTRACE))
+ return kmemleak_may_need_free(ptr);
+
+ return false;
+}
+
static inline void kmemleak_erase(void **ptr)
{
*ptr = NULL;
@@ -86,6 +95,14 @@ static inline void kmemleak_free_part(const void *ptr, size_t size)
static inline void kmemleak_free_recursive(const void *ptr, slab_flags_t flags)
{
}
+static inline bool kmemleak_may_need_free(const void *ptr)
+{
+ return false;
+}
+static inline bool kmemleak_may_need_free_recursive(const void *ptr, slab_flags_t flags)
+{
+ return false;
+}
static inline void kmemleak_free_percpu(const void __percpu *ptr)
{
}
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 96226707c2f61..d2929e4e23f13 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -2807,20 +2807,15 @@ void set_cpus_allowed_force(struct task_struct *p, const struct cpumask *new_mas
.user_mask = NULL,
.flags = SCA_USER, /* clear the user requested mask */
};
- union cpumask_rcuhead {
- cpumask_t cpumask;
- struct rcu_head rcu;
- };
scoped_guard (__task_rq_lock, p)
do_set_cpus_allowed(p, &ac);
/*
* Because this is called with p->pi_lock held, it is not possible
- * to use kfree() here (when PREEMPT_RT=y), therefore punt to using
- * kfree_rcu().
+ * to use kfree() here (when PREEMPT_RT=y), thus use kfree_nolock()
*/
- kfree_rcu((union cpumask_rcuhead *)ac.user_mask, rcu);
+ kfree_nolock(ac.user_mask);
}
int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *src,
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 56acf502ba260..6a8d0578e9633 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -2886,12 +2886,7 @@ static inline bool task_allowed_on_cpu(struct task_struct *p, int cpu)
static inline cpumask_t *alloc_user_cpus_ptr(int node)
{
- /*
- * See set_cpus_allowed_force() above for the rcu_head usage.
- */
- int size = max_t(int, cpumask_size(), sizeof(struct rcu_head));
-
- return kmalloc_node(size, GFP_KERNEL, node);
+ return kmalloc_node(cpumask_size(), GFP_KERNEL, node);
}
static inline struct task_struct *get_push_task(struct rq *rq)
diff --git a/mm/kfence/core.c b/mm/kfence/core.c
index 6577bd76954eb..42519d24687ff 100644
--- a/mm/kfence/core.c
+++ b/mm/kfence/core.c
@@ -1271,6 +1271,20 @@ void __kfence_free(void *addr)
}
}
+struct llist_node *kfence_obj_to_llnode(void *addr)
+{
+ struct kfence_metadata *meta = addr_to_metadata((unsigned long)addr);
+
+ return &meta->llnode;
+}
+
+void *kfence_llnode_to_obj(struct llist_node *llnode)
+{
+ struct kfence_metadata *meta = container_of(llnode, struct kfence_metadata, llnode);
+
+ return (void *)meta->addr;
+}
+
bool kfence_handle_page_fault(unsigned long addr, bool is_write, struct pt_regs *regs)
{
const int page_index = (addr - (unsigned long)__kfence_pool) / PAGE_SIZE;
diff --git a/mm/kfence/kfence.h b/mm/kfence/kfence.h
index 1f618f9b0d12d..0fca1dc2c7941 100644
--- a/mm/kfence/kfence.h
+++ b/mm/kfence/kfence.h
@@ -58,7 +58,10 @@ struct kfence_track {
/* KFENCE metadata per guarded allocation. */
struct kfence_metadata {
struct list_head list __guarded_by(&kfence_freelist_lock); /* Freelist node. */
- struct rcu_head rcu_head; /* For delayed freeing. */
+ union {
+ struct rcu_head rcu_head; /* For delayed freeing. */
+ struct llist_node llnode; /* For kfree_nolock(). */
+ };
/*
* Lock protecting below data; to ensure consistency of the below data,
diff --git a/mm/kmemleak.c b/mm/kmemleak.c
index 7c7ba17ce7af0..e3560ce826326 100644
--- a/mm/kmemleak.c
+++ b/mm/kmemleak.c
@@ -1168,6 +1168,48 @@ void __ref kmemleak_free(const void *ptr)
}
EXPORT_SYMBOL_GPL(kmemleak_free);
+/**
+ * kmemleak_may_need_free - check if object is registered
+ * @ptr: pointer to beginning of the object
+ *
+ * This function is called from the kernel allocator when an object should be
+ * freed but the caller context might be unsafe to spin on the internal locks.
+ *
+ * It will therefore only use trylock and thus might return a false positive
+ * if the trylock fails and the status cannot be determined.
+ *
+ * For objects that (might) need free, the allocator has to defer the actual
+ * freeing to a safe context.
+ *
+ * The assumption is that most objects freed from the unsafe context are also
+ * allocated in such context and thus are not registered in kmemleak, so it's
+ * unlikely the defered freeing will be necessary just because kmemleak is
+ * enabled.
+ */
+bool __ref kmemleak_may_need_free(const void *ptr)
+{
+ unsigned long flags;
+ struct kmemleak_object *object;
+
+ pr_debug("%s(0x%px)\n", __func__, ptr);
+
+ if (!kmemleak_free_enabled || !ptr || IS_ERR(ptr))
+ return false;
+
+ /* On UP, raw_spin_trylock() always succeeds even when it is locked */
+ if (!IS_ENABLED(CONFIG_SMP) && in_nmi())
+ return true;
+
+ if (!raw_spin_trylock_irqsave(&kmemleak_lock, flags))
+ return true;
+
+ object = __lookup_object((unsigned long)ptr, 0, 0);
+
+ raw_spin_unlock_irqrestore(&kmemleak_lock, flags);
+
+ return !!object;
+}
+
/**
* kmemleak_free_part - partially unregister a previously registered object
* @ptr: pointer to the beginning or inside the object. This also
diff --git a/mm/slub.c b/mm/slub.c
index b9aeb02a880f1..3be98faa9f0de 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -4050,6 +4050,8 @@ static void flush_all(struct kmem_cache *s)
struct deferred_percpu_work {
struct llist_head objects;
+ struct llist_head objects_kfence;
+ struct llist_head objects_large_kmalloc;
struct llist_head objects_by_rcu;
struct llist_head rcu_sheaves;
struct irq_work work;
@@ -4059,6 +4061,8 @@ static void deferred_percpu_work_fn(struct irq_work *work);
static DEFINE_PER_CPU(struct deferred_percpu_work, deferred_percpu_work) = {
.objects = LLIST_HEAD_INIT(objects),
+ .objects_kfence = LLIST_HEAD_INIT(objects_kfence),
+ .objects_large_kmalloc = LLIST_HEAD_INIT(objects_large_kmalloc),
.objects_by_rcu = LLIST_HEAD_INIT(objects_by_rcu),
.rcu_sheaves = LLIST_HEAD_INIT(rcu_sheaves),
.work = IRQ_WORK_INIT(deferred_percpu_work_fn),
@@ -6363,6 +6367,21 @@ static void free_to_pcs_bulk(struct kmem_cache *s, size_t size, void **p)
}
}
+static inline void
+__free_large_kmalloc_page(struct page *page, unsigned int free_flags)
+{
+ unsigned int order = compound_order(page);
+
+ mod_lruvec_page_state(page, NR_SLAB_UNRECLAIMABLE_B,
+ -(PAGE_SIZE << order));
+ __ClearPageLargeKmalloc(page);
+
+ if (free_flags & SLAB_FREE_NOLOCK)
+ free_frozen_pages_nolock(page, order);
+ else
+ free_frozen_pages(page, order);
+}
+
/*
* In PREEMPT_RT irq_work runs in per-cpu kthread, so it's safe
* to take sleeping spin_locks from __slab_free().
@@ -6371,16 +6390,12 @@ static void free_to_pcs_bulk(struct kmem_cache *s, size_t size, void **p)
static void deferred_percpu_work_fn(struct irq_work *work)
{
struct deferred_percpu_work *dpw;
- struct llist_head *objs, *objs_by_rcu, *rcu_sheaves;
struct llist_node *llnode, *pos, *t;
struct slab_sheaf *sheaf, *next;
dpw = container_of(work, struct deferred_percpu_work, work);
- rcu_sheaves = &dpw->rcu_sheaves;
- objs = &dpw->objects;
- objs_by_rcu = &dpw->objects_by_rcu;
- llnode = llist_del_all(objs);
+ llnode = llist_del_all(&dpw->objects);
llist_for_each_safe(pos, t, llnode) {
struct kmem_cache *s;
struct slab *slab;
@@ -6392,6 +6407,8 @@ static void deferred_percpu_work_fn(struct irq_work *work)
/* Point 'x' back to the beginning of allocated object */
x -= s->offset;
+ kmemleak_free_recursive(x, s->flags);
+
/*
* We used freepointer in 'x' to link 'x' into df->objects.
* Clear it to NULL to avoid false positive detection
@@ -6403,7 +6420,30 @@ static void deferred_percpu_work_fn(struct irq_work *work)
stat(s, FREE_SLOWPATH);
}
- llnode = llist_del_all(objs_by_rcu);
+ llnode = llist_del_all(&dpw->objects_kfence);
+ llist_for_each_safe(pos, t, llnode) {
+ struct kmem_cache *s;
+ struct slab *slab;
+ void *obj = kfence_llnode_to_obj(pos);
+
+ slab = virt_to_slab(obj);
+ s = slab->slab_cache;
+
+ kmemleak_free_recursive(obj, s->flags);
+
+ __kfence_free(obj);
+ }
+
+ llnode = llist_del_all(&dpw->objects_large_kmalloc);
+ llist_for_each_safe(pos, t, llnode) {
+ struct page *page = virt_to_page(pos);
+
+ kmemleak_free(pos);
+
+ __free_large_kmalloc_page(page, SLAB_FREE_DEFAULT);
+ }
+
+ llnode = llist_del_all(&dpw->objects_by_rcu);
llist_for_each_safe(pos, t, llnode) {
void *head = pos;
void *objp = kvmalloc_obj_start_addr(head);
@@ -6411,21 +6451,60 @@ static void deferred_percpu_work_fn(struct irq_work *work)
kvfree_call_rcu(head, objp);
}
- llnode = llist_del_all(rcu_sheaves);
+ llnode = llist_del_all(&dpw->rcu_sheaves);
llist_for_each_entry_safe(sheaf, next, llnode, llnode)
call_rcu(&sheaf->rcu_head, rcu_free_sheaf);
}
-static void defer_free(struct kmem_cache *s, void *head)
+static void defer_free(struct kmem_cache *s, void *obj)
{
struct deferred_percpu_work *dpw;
+ struct llist_node *llnode;
+
+ /*
+ * Place the llist node where the freepointer would be if we freed the
+ * object immediately. That means we can write there safely, only need
+ * to remove kasan tag first.
+ */
+ llnode = kasan_reset_tag(obj) + s->offset;
guard(preempt)();
- head = kasan_reset_tag(head);
+ dpw = this_cpu_ptr(&deferred_percpu_work);
+ if (llist_add(llnode, &dpw->objects))
+ irq_work_queue(&dpw->work);
+}
+
+static void defer_free_kfence(void *obj)
+{
+ struct deferred_percpu_work *dpw;
+ struct llist_node *llnode;
+
+ /* kasan_reset_tag() is not necessary, kfence objects are not tagged */
+ llnode = kfence_obj_to_llnode(obj);
+
+ guard(preempt)();
+
+ dpw = this_cpu_ptr(&deferred_percpu_work);
+ if (llist_add(llnode, &dpw->objects_kfence))
+ irq_work_queue(&dpw->work);
+}
+
+static void defer_free_large_kmalloc(void *obj)
+{
+ struct deferred_percpu_work *dpw;
+ struct llist_node *llnode;
+
+ /*
+ * we can simply use the first word of the large kmalloc object
+ * for the llnode, as there's no ctor or TYPESAFE_BY_RCU
+ */
+ llnode = kasan_reset_tag(obj);
+
+ guard(preempt)();
dpw = this_cpu_ptr(&deferred_percpu_work);
- if (llist_add(head + s->offset, &dpw->objects))
+ if (llist_add(llnode, &dpw->objects_large_kmalloc))
irq_work_queue(&dpw->work);
}
@@ -6677,9 +6756,11 @@ size_t ksize(const void *objp)
}
EXPORT_SYMBOL(ksize);
-static void free_large_kmalloc(struct page *page, void *object)
+static void free_large_kmalloc(struct page *page, void *object,
+ unsigned int free_flags)
{
unsigned int order = compound_order(page);
+ bool nolock = free_flags & SLAB_FREE_NOLOCK;
if (WARN_ON_ONCE(!PageLargeKmalloc(page))) {
dump_page(page, "Not a kmalloc allocation");
@@ -6689,14 +6770,16 @@ static void free_large_kmalloc(struct page *page, void *object)
if (WARN_ON_ONCE(order == 0))
pr_warn_once("object pointer: 0x%p\n", object);
- kmemleak_free(object);
+ if (!nolock)
+ kmemleak_free(object);
+
kasan_kfree_large(object);
kmsan_kfree_large(object);
- mod_lruvec_page_state(page, NR_SLAB_UNRECLAIMABLE_B,
- -(PAGE_SIZE << order));
- __ClearPageLargeKmalloc(page);
- free_frozen_pages(page, order);
+ if (unlikely(nolock && kmemleak_may_need_free(object)))
+ defer_free_large_kmalloc(object);
+ else
+ __free_large_kmalloc_page(page, free_flags);
}
/*
@@ -6718,7 +6801,7 @@ void kvfree_rcu_cb(struct rcu_head *head)
if (slab)
slab_free(slab->slab_cache, slab, obj, _RET_IP_);
else
- free_large_kmalloc(page, obj);
+ free_large_kmalloc(page, obj, SLAB_FREE_DEFAULT);
}
}
@@ -6744,7 +6827,7 @@ void kfree(const void *object)
slab = page_slab(page);
if (!slab) {
/* kmalloc_nolock() doesn't support large kmalloc */
- free_large_kmalloc(page, (void *)object);
+ free_large_kmalloc(page, (void *)object, SLAB_FREE_DEFAULT);
return;
}
@@ -6755,15 +6838,14 @@ EXPORT_SYMBOL(kfree);
/*
* Can be called while holding raw_spinlock_t or from IRQ and NMI,
- * but ONLY for objects allocated by kmalloc_nolock().
- * Debug checks (like kmemleak and kfence) were skipped on allocation,
- * hence
- * obj = kmalloc(); kfree_nolock(obj);
- * will miss kmemleak/kfence book keeping and will cause false positives.
- * large_kmalloc is not supported either.
+ * but may defer freeing to irq_work() in some cases.
+ *
+ * Intended mainly for objects allocated from kmalloc_nolock(), but can handle
+ * also kmem_cache_alloc() and kmalloc() objects, including large_kmalloc.
*/
void kfree_nolock(const void *object)
{
+ struct page *page;
struct slab *slab;
struct kmem_cache *s;
void *x = (void *)object;
@@ -6771,9 +6853,10 @@ void kfree_nolock(const void *object)
if (unlikely(ZERO_OR_NULL_PTR(object)))
return;
- slab = virt_to_slab(object);
+ page = virt_to_page(object);
+ slab = page_slab(page);
if (unlikely(!slab)) {
- WARN_ONCE(1, "large_kmalloc is not supported by kfree_nolock()");
+ free_large_kmalloc(page, (void *)object, SLAB_FREE_NOLOCK);
return;
}
@@ -6791,6 +6874,12 @@ void kfree_nolock(const void *object)
* since they take spinlocks or not safe from any context.
*/
kmsan_slab_free(s, x);
+
+ if (is_kfence_address(x)) {
+ defer_free_kfence(x);
+ return;
+ }
+
/*
* If KASAN finds a kernel bug it will do kasan_report_invalid_free()
* which will call raw_spin_lock_irqsave() which is technically
@@ -6809,9 +6898,23 @@ void kfree_nolock(const void *object)
*/
kasan_slab_free(s, x, false, false, /* skip quarantine */true);
+ /*
+ * with kfree() the kmemleak handling happens much sooner, but for
+ * defering we need to write llnode to the object's freepointer so
+ * we should have it in the state when it's no longer treated as
+ * allocated by kasan etc.
+ *
+ * defer_free will also reset the pointer tag, but it's ok to do a
+ * deferred kmemleak_free() using the untagged pointer, because
+ * __lookup_object() resets the tag anyway
+ */
+ if (unlikely(kmemleak_may_need_free_recursive(x, s->flags)))
+ goto defer;
+
if (likely(can_free_to_pcs(slab)) && likely(free_to_pcs(s, x, false)))
return;
+defer:
/*
* __slab_free() can locklessly cmpxchg16 into a slab, but then it might
* need to take spin_lock for further processing.
@@ -7114,7 +7217,7 @@ int build_detached_freelist(struct kmem_cache *s, size_t size,
if (!s) {
/* Handle kalloc'ed objects */
if (!slab) {
- free_large_kmalloc(page, object);
+ free_large_kmalloc(page, object, SLAB_FREE_DEFAULT);
df->slab = NULL;
return size;
}
|
| 3/2 | 2026/08/07 16:53 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 8420 output: 19 thoughts: 210 Thoughts: **Analyzing the kfree_nolock() Patch** Alright, let's dive into this patch. My initial thought is that it's altering `kfree_nolock()` within `mm/slub.c` – a crucial function for memory management, particularly the SLUB allocator. The key is that it's expanding the scope of `kfree_nolock()` to handle objects beyond its original design, now including memory allocated via `kmalloc()`, `kmem_cache_alloc()`, and importantly, *large* `kmalloc` allocations, as well as KFENCE objects. That's a significant change, implying more comprehensive memory management within that function. The addition of an `irq_work` mechanism hints at handling certain freeing operations asynchronously. This is likely in response to issues with `kmemleak` or KFENCE that would have required the freeing to occur outside of normal interrupt context. So, potentially we are dealing with deferred freeing in certain conditions, which is common in a modern kernel. Now, I see it's also impacting `set_cpus_allowed_force()` in `kernel/sched/core.c`, replacing `kfree_rcu()` with `kfree_nolock()`. This is another interesting point. Why this change? Does this mean `kfree_nolock()` is now considered more appropriate or efficient in this context? Let's check. Okay, let's step back and consider `kfree_nolock()` itself. It's a core kernel function. The patch's primary contribution is supporting these other memory allocation methods. Essentially, it adds the capability of freeing objects allocated by kmalloc/kmem_cache_alloc etc... as well as deferring freeing to `deferred_percpu_work_fn` when necessary. Right, so to understand the impact of these changes, I need to start tracing the usage of `kfree_nolock()`. Let's see where this is now being called, especially considering the changes to how large `kmalloc` and KFENCE objects are handled. I need to get a clear picture of how this now fits into the overall memory management picture. |
| 4/2 | 2026/08/07 16:53 | tool | grepper |
0mArgs:{
"Expression": "kfree_nolock"
}Results: {
"Output": "include/linux/slab.h=592=void kfree(const void *objp);\ninclude/linux/slab.h:593:void kfree_nolock(const void *objp);\ninclude/linux/slab.h-594-void kfree_sensitive(const void *objp);\n--\nkernel/bpf/arena.c=659=static long arena_alloc_pages(struct bpf_arena *arena, long uaddr, long page_cnt, int node_id,\n--\nkernel/bpf/arena.c-755-\traw_res_spin_unlock_irqrestore(\u0026arena-\u003espinlock, flags);\nkernel/bpf/arena.c:756:\tkfree_nolock(pages);\nkernel/bpf/arena.c-757-\tbpf_map_memcg_exit(old_memcg, new_memcg);\n--\nkernel/bpf/arena.c-769-out_free_pages:\nkernel/bpf/arena.c:770:\tkfree_nolock(pages);\nkernel/bpf/arena.c-771-\tbpf_map_memcg_exit(old_memcg, new_memcg);\n--\nkernel/bpf/arena.c=962=static void arena_free_worker(struct work_struct *work)\n--\nkernel/bpf/arena.c-1016-\nkernel/bpf/arena.c:1017:\t\tkfree_nolock(s);\nkernel/bpf/arena.c-1018-\t}\n--\nkernel/bpf/helpers.c=1242=static void bpf_async_cb_rcu_free(struct rcu_head *rcu)\n--\nkernel/bpf/helpers.c-1252-\nkernel/bpf/helpers.c:1253:\tkfree_nolock(cb);\nkernel/bpf/helpers.c-1254-}\n--\nkernel/bpf/helpers.c=1317=static int __bpf_async_init(struct bpf_async_kern *async, struct bpf_map *map, u64 flags,\n--\nkernel/bpf/helpers.c-1372-\t\t/* Lost the race to initialize this bpf_async_kern, drop the allocated object */\nkernel/bpf/helpers.c:1373:\t\tkfree_nolock(cb);\nkernel/bpf/helpers.c-1374-\t\treturn -EBUSY;\n--\nkernel/bpf/helpers.c=1662=static void bpf_async_irq_worker(struct irq_work *work)\n--\nkernel/bpf/helpers.c-1677-\t\tbpf_async_process_op(cb, cmd-\u003eop, cmd-\u003ensec, cmd-\u003emode);\nkernel/bpf/helpers.c:1678:\t\tkfree_nolock(cmd);\nkernel/bpf/helpers.c-1679-\t}\n--\nkernel/bpf/helpers.c=4520=static struct bpf_task_work_ctx *bpf_task_work_fetch_ctx(struct bpf_task_work *tw,\n--\nkernel/bpf/helpers.c-4543-\t\t */\nkernel/bpf/helpers.c:4544:\t\tkfree_nolock(ctx);\nkernel/bpf/helpers.c-4545-\t\treturn old_ctx;\n--\nkernel/bpf/helpers.c=4710=__bpf_kfunc int bpf_dynptr_file_discard(struct bpf_dynptr *dynptr)\n--\nkernel/bpf/helpers.c-4718-\tfreader_cleanup(\u0026df-\u003efreader);\nkernel/bpf/helpers.c:4719:\tkfree_nolock(df);\nkernel/bpf/helpers.c-4720-\tbpf_dynptr_set_null(ptr);\n--\nkernel/bpf/range_tree.c=136=int range_tree_clear(struct range_tree *rt, u32 start, u32 len)\n--\nkernel/bpf/range_tree.c-172-\t\t\trange_it_remove(rn, rt);\nkernel/bpf/range_tree.c:173:\t\t\tkfree_nolock(rn);\nkernel/bpf/range_tree.c-174-\t\t}\n--\nkernel/bpf/range_tree.c=193=int range_tree_set(struct range_tree *rt, u32 start, u32 len)\n--\nkernel/bpf/range_tree.c-225-\t\trange_it_insert(left, rt);\nkernel/bpf/range_tree.c:226:\t\tkfree_nolock(right);\nkernel/bpf/range_tree.c-227-\t} else if (left) {\n--\nkernel/bpf/range_tree.c=248=void range_tree_destroy(struct range_tree *rt)\n--\nkernel/bpf/range_tree.c-253-\t\trange_it_remove(rn, rt);\nkernel/bpf/range_tree.c:254:\t\tkfree_nolock(rn);\nkernel/bpf/range_tree.c-255-\t}\n--\nkernel/bpf/stream.c=92=static void bpf_stream_free_elem(struct bpf_stream_elem *elem)\nkernel/bpf/stream.c-93-{\nkernel/bpf/stream.c:94:\tkfree_nolock(elem);\nkernel/bpf/stream.c-95-}\n--\nkernel/sched/core.c=2803=void set_cpus_allowed_force(struct task_struct *p, const struct cpumask *new_mask)\n--\nkernel/sched/core.c-2815-\t * Because this is called with p-\u003epi_lock held, it is not possible\nkernel/sched/core.c:2816:\t * to use kfree() here (when PREEMPT_RT=y), thus use kfree_nolock()\nkernel/sched/core.c-2817-\t */\nkernel/sched/core.c:2818:\tkfree_nolock(ac.user_mask);\nkernel/sched/core.c-2819-}\n--\nlib/tests/slub_kunit.c=344=static void test_nolock(struct test_nolock_context *ctx)\n--\nlib/tests/slub_kunit.c-361-\telse\nlib/tests/slub_kunit.c:362:\t\tkfree_nolock(objp);\nlib/tests/slub_kunit.c-363-\n--\nmm/kfence/kfence.h=59=struct kfence_metadata {\n--\nmm/kfence/kfence.h-62-\t\tstruct rcu_head rcu_head;\t/* For delayed freeing. */\nmm/kfence/kfence.h:63:\t\tstruct llist_node llnode;\t/* For kfree_nolock(). */\nmm/kfence/kfence.h-64-\t};\n--\nmm/slab_common.c=527=void kmem_cache_destroy(struct kmem_cache *s)\n--\nmm/slab_common.c-552-\nmm/slab_common.c:553:\t/* Wait for deferred work from kmalloc/kfree_nolock() */\nmm/slab_common.c-554-\tdeferred_work_barrier();\n--\nmm/slub.c-160- * Preemption is disabled as part of local_trylock operations.\nmm/slub.c:161: * kmalloc_nolock() and kfree_nolock() are safe in NMI context but see\nmm/slub.c-162- * their limitations.\n--\nmm/slub.c=2146=int alloc_slab_obj_exts(struct slab *slab, struct kmem_cache *s,\n--\nmm/slub.c-2221-\t\tif (unlikely(!allow_spin))\nmm/slub.c:2222:\t\t\tkfree_nolock(vec);\nmm/slub.c-2223-\t\telse\n--\nmm/slub.c=2236=static inline void free_slab_obj_exts(struct slab *slab, bool allow_spin)\n--\nmm/slub.c-2258-\telse\nmm/slub.c:2259:\t\tkfree_nolock(obj_exts);\nmm/slub.c-2260-\tslab-\u003eobj_exts = 0;\n--\nmm/slub.c=2794=static void __free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf,\n--\nmm/slub.c-2808-\tif (unlikely(free_flags \u0026 SLAB_FREE_NOLOCK))\nmm/slub.c:2809:\t\tkfree_nolock(sheaf);\nmm/slub.c-2810-\telse\n--\nmm/slub.c=6837=EXPORT_SYMBOL(kfree);\n--\nmm/slub.c-6845- */\nmm/slub.c:6846:void kfree_nolock(const void *object)\nmm/slub.c-6847-{\n--\nmm/slub.c-6888-\t * kasan_report_invalid_free() -\u003e raw_spin_lock_irqsave() -\u003e NMI\nmm/slub.c:6889:\t * -\u003e kfree_nolock() -\u003e kasan_report_invalid_free() on the same CPU\nmm/slub.c-6890-\t * is double buggy and deserves to deadlock.\n--\nmm/slub.c-6924-}\nmm/slub.c:6925:EXPORT_SYMBOL_GPL(kfree_nolock);\nmm/slub.c-6926-\n"
} |
| 5/2 | 2026/08/07 16:53 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 11609 output: 22 thoughts: 129 Thoughts: **Analyzing the `kfree_nolock` Patch** Okay, so this patch is touching `kfree_nolock`, which is a pretty fundamental function. I know it's exported and used in BPF, the scheduler, and even the SLUB allocator itself – that makes it a critical piece. The first change, swapping out `kfree_rcu` for `kfree_nolock` within `set_cpus_allowed_force` in `kernel/sched/core.c`, immediately grabs my attention. `kfree_rcu` is a way to safely free memory that might still be in use by other CPUs, right? So, this suggests a potential performance optimization – or a significant risk, depending on the implications. The second part mentions supporting large `kmalloc` and KFENCE within `kfree_nolock`. That could be a compatibility or efficiency change. Now, wait a minute... `set_cpus_allowed_force`... that's a *core* scheduler function. The implications of getting this wrong are substantial, touching scheduling is a very sensitive area. This means I need to understand this call chain in detail. My first instinct here is to trace where `set_cpus_allowed_force` is *actually* called and fully understand its usage model. I need to figure out the exact flow of data here and ensure this replacement is actually safe. I'll need to do some code diving to make sure this is actually reachable, given its significance. |
| 6/2 | 2026/08/07 16:54 | tool | grepper |
0mArgs:{
"Expression": "set_cpus_allowed_force"
}Results: {
"Output": "include/linux/sched.h=1910=extern void dl_bw_free(int cpu, u64 dl_bw);\ninclude/linux/sched.h-1911-\ninclude/linux/sched.h:1912:/* set_cpus_allowed_force() - consider using set_cpus_allowed_ptr() instead */\ninclude/linux/sched.h:1913:extern void set_cpus_allowed_force(struct task_struct *p, const struct cpumask *new_mask);\ninclude/linux/sched.h-1914-\n--\nkernel/cgroup/cpuset.c=4093=bool cpuset_cpus_allowed_fallback(struct task_struct *tsk)\n--\nkernel/cgroup/cpuset.c-4101-\tif (is_in_v2_mode() \u0026\u0026 cpumask_subset(cs_mask, possible_mask)) {\nkernel/cgroup/cpuset.c:4102:\t\tset_cpus_allowed_force(tsk, cs_mask);\nkernel/cgroup/cpuset.c-4103-\t\tchanged = true;\n--\nkernel/kthread.c=566=static void __kthread_bind_mask(struct task_struct *p, const struct cpumask *mask, unsigned int state)\n--\nkernel/kthread.c-573-\tscoped_guard (raw_spinlock_irqsave, \u0026p-\u003epi_lock)\nkernel/kthread.c:574:\t\tset_cpus_allowed_force(p, mask);\nkernel/kthread.c-575-\n--\nkernel/kthread.c=839=int kthread_affine_preferred(struct task_struct *p, const struct cpumask *mask)\n--\nkernel/kthread.c-867-\tscoped_guard (raw_spinlock_irqsave, \u0026p-\u003epi_lock)\nkernel/kthread.c:868:\t\tset_cpus_allowed_force(p, affinity);\nkernel/kthread.c-869-\n--\nkernel/sched/core.c=2793=do_set_cpus_allowed(struct task_struct *p, struct affinity_context *ctx)\n--\nkernel/sched/core.c-2802- */\nkernel/sched/core.c:2803:void set_cpus_allowed_force(struct task_struct *p, const struct cpumask *new_mask)\nkernel/sched/core.c-2804-{\n--\nkernel/sched/core.c=2821=int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *src,\n--\nkernel/sched/core.c-2848-\t * Though unlikely, user_cpus_ptr can be reset to NULL by a concurrent\nkernel/sched/core.c:2849:\t * set_cpus_allowed_force().\nkernel/sched/core.c-2850-\t */\n--\nkernel/sched/core.c=3540=static int select_fallback_rq(int cpu, struct task_struct *p)\n--\nkernel/sched/core.c-3579-\t\tcase possible:\nkernel/sched/core.c:3580:\t\t\tset_cpus_allowed_force(p, task_cpu_fallback_mask(p));\nkernel/sched/core.c-3581-\t\t\tstate = fail;\n"
} |
| 7/2 | 2026/08/07 16:54 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 12922 output: 23 thoughts: 479 Thoughts: **Initial Assessment: Promising Fuzzing Targets** Okay, so looking at this code, my initial thought is that `set_cpus_allowed_force` is definitely a potential entry point for fuzzing. It's clearly reachable. Beyond that, I see `kfree_nolock` being used within the BPF arena and range tree, which immediately catches my attention as a potential area to explore. I'm seeing some significant changes in `mm/slub.c` and `mm/kmemleak.c`. Specifically, the introduction of `kmemleak_may_need_free` and the deferral of freeing operations to `deferred_percpu_work_fn` are major red flags, essentially core memory management changes. I need to dig into those functions in `mm/slub.c` next. Alright, looking at `mm/slub.c`, I see `defer_free_kfence` and `defer_free_large_kmalloc` have been added, and they are deferring freeing to `deferred_percpu_work_fn`. That's the key. I need to confirm `deferred_percpu_work_fn` is reachable. Yep, `deferred_percpu_work_fn` is reachable through an `irq_work` queued by `defer_free`, `defer_free_kfence`, and `defer_free_large_kmalloc`. This is definitely a spot worth spending some time on with the fuzzer. Okay, let's nail down some focus symbols. `set_cpus_allowed_force`, `kfree_nolock`, `kmemleak_may_need_free`, `defer_free_kfence` (which looks to be static), `defer_free_large_kmalloc` (also static), `deferred_percpu_work_fn` (static), `free_large_kmalloc` (static), and finally `kfence_obj_to_llnode` and `kfence_llnode_to_obj`. Those last two look like they could be interesting. Hold on a minute… Focusing in on `kfree_nolock`, that's a good place to start. `set_cpus_allowed_force` is good too. And `kmemleak_may_need_free` is also a prime candidate. Let me check the kernel source to see if `kfence_obj_to_llnode` and `kfence_llnode_to_obj` are exported. They're in `mm/kfence/core.c`. Let me check that. |
| 8/2 | 2026/08/07 16:54 | tool | grepper |
0mArgs:{
"Expression": "kfence_obj_to_llnode"
}Results: {
"Output": "include/linux/kfence.h=183=static __always_inline __must_check bool kfence_free(void *addr)\n--\ninclude/linux/kfence.h-190-\ninclude/linux/kfence.h:191:struct llist_node *kfence_obj_to_llnode(void *addr);\ninclude/linux/kfence.h-192-void *kfence_llnode_to_obj(struct llist_node *llnode);\n--\ninclude/linux/kfence.h=240=static inline bool __must_check kfence_free(void *addr) { return false; }\ninclude/linux/kfence.h:241:static inline struct llist_node *kfence_obj_to_llnode(void *addr) { return NULL; }\ninclude/linux/kfence.h-242-static inline void *kfence_llnode_to_obj(struct llist_node *llnode) { return NULL; }\n--\nmm/kfence/core.c=1247=void __kfence_free(void *addr)\n--\nmm/kfence/core.c-1273-\nmm/kfence/core.c:1274:struct llist_node *kfence_obj_to_llnode(void *addr)\nmm/kfence/core.c-1275-{\n--\nmm/slub.c=6478=static void defer_free_kfence(void *obj)\n--\nmm/slub.c-6483-\t/* kasan_reset_tag() is not necessary, kfence objects are not tagged */\nmm/slub.c:6484:\tllnode = kfence_obj_to_llnode(obj);\nmm/slub.c-6485-\n"
} |
| 9/2 | 2026/08/07 16:54 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 14433 output: 23 thoughts: 130 Thoughts: **Adding Kernel Memory Management Functions** Okay, I need to integrate these functions: `kfree_nolock`, `set_cpus_allowed_force`, `kmemleak_may_need_free`, `kfence_obj_to_llnode`, and `kfence_llnode_to_obj`. Now, before I just start throwing code around, I should double-check the configuration dependencies. Let's see... I'm pretty sure `kfence_obj_to_llnode` requires `CONFIG_KFENCE`. And `kmemleak_may_need_free` definitely hinges on `CONFIG_DEBUG_KMEMLEAK`. I'll confirm that by referencing the source code directly. I'll take a quick look at `mm/kmemleak.c` to be sure about `kmemleak_may_need_free` and its dependencies. That’s the most efficient way to be sure everything is integrated correctly. |
| 10/2 | 2026/08/07 16:54 | tool | grepper |
0mArgs:{
"Expression": "kmemleak_may_need_free"
}Results: {
"Output": "include/linux/kmemleak.h=24=extern void kmemleak_free(const void *ptr) __ref;\ninclude/linux/kmemleak.h:25:bool kmemleak_may_need_free(const void *ptr) __ref;\ninclude/linux/kmemleak.h-26-extern void kmemleak_free_part(const void *ptr, size_t size) __ref;\n--\ninclude/linux/kmemleak.h=48=static inline void kmemleak_free_recursive(const void *ptr, slab_flags_t flags)\n--\ninclude/linux/kmemleak.h-53-\ninclude/linux/kmemleak.h:54:static inline bool kmemleak_may_need_free_recursive(const void *ptr, slab_flags_t flags)\ninclude/linux/kmemleak.h-55-{\ninclude/linux/kmemleak.h-56-\tif (!(flags \u0026 SLAB_NOLEAKTRACE))\ninclude/linux/kmemleak.h:57:\t\treturn kmemleak_may_need_free(ptr);\ninclude/linux/kmemleak.h-58-\n--\ninclude/linux/kmemleak.h=95=static inline void kmemleak_free_recursive(const void *ptr, slab_flags_t flags)\n--\ninclude/linux/kmemleak.h-97-}\ninclude/linux/kmemleak.h:98:static inline bool kmemleak_may_need_free(const void *ptr)\ninclude/linux/kmemleak.h-99-{\n--\ninclude/linux/kmemleak.h-101-}\ninclude/linux/kmemleak.h:102:static inline bool kmemleak_may_need_free_recursive(const void *ptr, slab_flags_t flags)\ninclude/linux/kmemleak.h-103-{\n--\nmm/kmemleak.c=1169=EXPORT_SYMBOL_GPL(kmemleak_free);\n--\nmm/kmemleak.c-1171-/**\nmm/kmemleak.c:1172: * kmemleak_may_need_free - check if object is registered\nmm/kmemleak.c-1173- * @ptr:\tpointer to beginning of the object\n--\nmm/kmemleak.c-1188- */\nmm/kmemleak.c:1189:bool __ref kmemleak_may_need_free(const void *ptr)\nmm/kmemleak.c-1190-{\n--\nmm/slub.c=6759=static void free_large_kmalloc(struct page *page, void *object,\n--\nmm/slub.c-6778-\nmm/slub.c:6779:\tif (unlikely(nolock \u0026\u0026 kmemleak_may_need_free(object)))\nmm/slub.c-6780-\t\tdefer_free_large_kmalloc(object);\n--\nmm/slub.c=6846=void kfree_nolock(const void *object)\n--\nmm/slub.c-6910-\t */\nmm/slub.c:6911:\tif (unlikely(kmemleak_may_need_free_recursive(x, s-\u003eflags)))\nmm/slub.c-6912-\t\tgoto defer;\n"
} |
| 11/2 | 2026/08/07 16:54 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 15625 output: 35 thoughts: 51 Thoughts: Okay, here's my take on those thoughts, framed as my own: **Investigating Memory Leak Detection Options** Alright, so we're looking at memory leak detection, and I've got `CONFIG_DEBUG_KMEMLEAK` and `CONFIG_KFENCE` in mind. Classic choices. But before I dive too deep into comparing them, I need to understand where they are supported in the kernel's scheduler code. My first instinct is to check how `set_cpus_allowed_force` in `kernel/sched/core.c` handles these configurations. I need to quickly determine if that function's behavior is conditional on either of those configuration options being enabled. If so, then that might affect the way memory is handled and provide insight to where any leak may be happening. This is always a solid starting point for tracing memory-related issues in scheduling. Time to dig in and see what the defines say. |
| 12/2 | 2026/08/07 16:54 | tool | grepper |
0mArgs:{
"Expression": "set_cpus_allowed_force",
"PathPrefix": "kernel/sched/core.c"
}Results: {
"Output": "kernel/sched/core.c=2793=do_set_cpus_allowed(struct task_struct *p, struct affinity_context *ctx)\n--\nkernel/sched/core.c-2802- */\nkernel/sched/core.c:2803:void set_cpus_allowed_force(struct task_struct *p, const struct cpumask *new_mask)\nkernel/sched/core.c-2804-{\n--\nkernel/sched/core.c=2821=int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *src,\n--\nkernel/sched/core.c-2848-\t * Though unlikely, user_cpus_ptr can be reset to NULL by a concurrent\nkernel/sched/core.c:2849:\t * set_cpus_allowed_force().\nkernel/sched/core.c-2850-\t */\n--\nkernel/sched/core.c=3540=static int select_fallback_rq(int cpu, struct task_struct *p)\n--\nkernel/sched/core.c-3579-\t\tcase possible:\nkernel/sched/core.c:3580:\t\t\tset_cpus_allowed_force(p, task_cpu_fallback_mask(p));\nkernel/sched/core.c-3581-\t\t\tstate = fail;\n"
} |
| 13/2 | 2026/08/07 16:54 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 16253 output: 31 thoughts: 59 Thoughts: Okay, here's my take on those thoughts, framed as my own: **Investigating Memory Leak Detection in the Kernel** Right, so we're dealing with memory leaks, which, as we know, can be a real pain. The kernel's design means debugging tools are almost always enabled via configuration flags. I need to remember the specific ones for memory leak detection, so `CONFIG_DEBUG_KMEMLEAK` and `CONFIG_KFENCE` are the usual suspects. I'll need to double check those are in fact what enable the tools I'm looking for. Now, let's get into the specifics of a possible leak. I'll want to trace how `kfree_nolock` operates, and that's in `mm/slub.c`. I have a feeling the logic might be conditionally compiled, dependent on some config. Need to confirm whether that's the case. Time to check if `kfree_nolock` has any preprocessor directives around it, checking which configs are relevant to it. |
| 14/2 | 2026/08/07 16:55 | tool | grepper |
0mArgs:{
"Expression": "kfree_nolock",
"PathPrefix": "mm/slub.c"
}Results: {
"Output": "mm/slub.c-160- * Preemption is disabled as part of local_trylock operations.\nmm/slub.c:161: * kmalloc_nolock() and kfree_nolock() are safe in NMI context but see\nmm/slub.c-162- * their limitations.\n--\nmm/slub.c=2146=int alloc_slab_obj_exts(struct slab *slab, struct kmem_cache *s,\n--\nmm/slub.c-2221-\t\tif (unlikely(!allow_spin))\nmm/slub.c:2222:\t\t\tkfree_nolock(vec);\nmm/slub.c-2223-\t\telse\n--\nmm/slub.c=2236=static inline void free_slab_obj_exts(struct slab *slab, bool allow_spin)\n--\nmm/slub.c-2258-\telse\nmm/slub.c:2259:\t\tkfree_nolock(obj_exts);\nmm/slub.c-2260-\tslab-\u003eobj_exts = 0;\n--\nmm/slub.c=2794=static void __free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf,\n--\nmm/slub.c-2808-\tif (unlikely(free_flags \u0026 SLAB_FREE_NOLOCK))\nmm/slub.c:2809:\t\tkfree_nolock(sheaf);\nmm/slub.c-2810-\telse\n--\nmm/slub.c=6837=EXPORT_SYMBOL(kfree);\n--\nmm/slub.c-6845- */\nmm/slub.c:6846:void kfree_nolock(const void *object)\nmm/slub.c-6847-{\n--\nmm/slub.c-6888-\t * kasan_report_invalid_free() -\u003e raw_spin_lock_irqsave() -\u003e NMI\nmm/slub.c:6889:\t * -\u003e kfree_nolock() -\u003e kasan_report_invalid_free() on the same CPU\nmm/slub.c-6890-\t * is double buggy and deserves to deadlock.\n--\nmm/slub.c-6924-}\nmm/slub.c:6925:EXPORT_SYMBOL_GPL(kfree_nolock);\nmm/slub.c-6926-\n"
} |
| 15/2 | 2026/08/07 16:55 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 17202 output: 19 thoughts: 298 Thoughts: **Analyzing Compilation Dependencies** Okay, so I'm digging into this code and immediately I see it's compiled, that much is obvious. I need to figure out the key configuration dependencies. I've got a list of focus symbols to work with, `kfree_nolock`, `set_cpus_allowed_force`, `kmemleak_may_need_free`, `kfence_obj_to_llnode`, and `kfence_llnode_to_obj`. Those are good starting points for understanding the build requirements. Now, let's look for a potential key configuration parameter. The commit message mentions `PREEMPT_RT=y` in relation to `set_cpus_allowed_force`, so that's immediately on my radar. Checking `set_cpus_allowed_force`... Ah, the comment here is crucial. It notes that `kfree_nolock` is used because of the `p->pi_lock` and the potential conflict with `kfree` when `PREEMPT_RT` is enabled. However, the comment does *not* say that `kfree_nolock` is *only* called with `PREEMPT_RT`. So it looks like `kfree_nolock` is called in any case, even if `PREEMPT_RT` is not enabled, though this specific call with a lock might be the reason for including it. I'll include `PREEMPT_RT` in `EnableConfigs` just to be safe, just in case that's why this is called, but it doesn't look to be strictly necessary for the code to compile. Let's make sure that that config actually exists. |
| 16/2 | 2026/08/07 16:55 | tool | grepper |
0mArgs:{
"Expression": "config PREEMPT_RT"
}Results: {
"Output": "kernel/Kconfig.preempt=90=endchoice\nkernel/Kconfig.preempt-91-\nkernel/Kconfig.preempt:92:config PREEMPT_RT\nkernel/Kconfig.preempt-93-\tbool \"Fully Preemptible Kernel (Real-Time)\"\n--\nkernel/Kconfig.preempt-108-\nkernel/Kconfig.preempt:109:config PREEMPT_RT_NEEDS_BH_LOCK\nkernel/Kconfig.preempt-110-\tbool \"Enforce softirq synchronisation on PREEMPT_RT\"\n"
} |
| 17/2 | 2026/08/07 16:55 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 18026 output: 37 thoughts: 149 Thoughts: **My Analysis of PREEMPT_RT and Related Symbols** Okay, so `PREEMPT_RT` is definitely a factor here. Let's get a handle on what's relevant to this analysis. I need to quickly recall the core symbols that I'm dealing with. I see these as key: `kfree_nolock`, `set_cpus_allowed_force`, `kmemleak_may_need_free`, `kfence_obj_to_llnode`, and `kfence_llnode_to_obj`. Those are the ones I'm tracking, in relation to `PREEMPT_RT`. Now, for the configurations themselves, I've got `DEBUG_KMEMLEAK`, `KFENCE`, and of course, `PREEMPT_RT`. These seem central to the problem. Just to be absolutely certain, let me quickly verify the location of `kfence_obj_to_llnode` and `kfence_llnode_to_obj`. Ah, yes, they're in `mm/kfence/core.c`. Good, that solidifies my mental map of where the KFENCE logic resides. I'm now ready to proceed, armed with a confirmed understanding of the landscape. |
| 18/2 | 2026/08/07 16:55 | tool | grepper |
0mArgs:{
"Expression": "kfence_obj_to_llnode",
"PathPrefix": "mm/kfence/core.c"
}Results: {
"Output": "mm/kfence/core.c=1247=void __kfence_free(void *addr)\n--\nmm/kfence/core.c-1273-\nmm/kfence/core.c:1274:struct llist_node *kfence_obj_to_llnode(void *addr)\nmm/kfence/core.c-1275-{\n"
} |
| 19/2 | 2026/08/07 16:55 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 18580 output: 36 thoughts: 24 Thoughts: Okay, here's my interpretation of those thoughts, summarized from a first-person perspective, assuming I'm well-versed in kernel internals: **Confirming the Scope and Drilling Down** Right, so we're dealing with global functions, which is important to keep in mind. The context suggests we're likely in the realm of memory management, likely kernel memory management. Hmm, let's just make sure I have a firm grasp of the situation here, let me go ahead and double check my understanding of the `kmemleak_may_need_free` function. I should be able to quickly recall its purpose, but it's always good to be thorough and refresh my memory on any subtleties. That function could be central to whatever issue we're examining. |
| 20/2 | 2026/08/07 16:55 | tool | grepper |
0mArgs:{
"Expression": "kmemleak_may_need_free",
"PathPrefix": "mm/kmemleak.c"
}Results: {
"Output": "mm/kmemleak.c=1169=EXPORT_SYMBOL_GPL(kmemleak_free);\n--\nmm/kmemleak.c-1171-/**\nmm/kmemleak.c:1172: * kmemleak_may_need_free - check if object is registered\nmm/kmemleak.c-1173- * @ptr:\tpointer to beginning of the object\n--\nmm/kmemleak.c-1188- */\nmm/kmemleak.c:1189:bool __ref kmemleak_may_need_free(const void *ptr)\nmm/kmemleak.c-1190-{\n"
} |
| 21/2 | 2026/08/07 16:55 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 18976 output: 244 thoughts: 164 Thoughts: **Analyzing the Implications of This Patch** Okay, so this patch is touching a *global function*, specifically `kfree_nolock()`. My initial assessment confirms this – yes, it's a global function that's being modified. That means it’s critical to understand the reach of this change. Now, the core of it is the modification to `kfree_nolock()` within `mm/slub.c`. It's designed to make `kfree_nolock()` handle a wider range of objects: those allocated via `kmalloc()`, `kmem_cache_alloc()`, and even the larger `kmalloc` allocations, not to mention KFENCE objects. The logic of deferring the actual freeing using `irq_work` when necessary (like when dealing with `kmemleak` or `KFENCE`) is clever and seems strategically sound, a common approach in the kernel. But it does introduce a new path, a subtle interaction between different scheduling paths. It is important to remember to check for potential race conditions. Interestingly, it also touches `set_cpus_allowed_force()` in `kernel/sched/core.c`, shifting its use from `kfree_rcu()` to `kfree_nolock()`. The shift in function is notable; `kfree_nolock()` is less conservative than `kfree_rcu()` in terms of synchronization. These are core memory management and scheduler changes and are *highly* reachable. This significantly increases the fuzzing surface and highlights their inherent vulnerability. The combination of memory management and scheduler changes means it has the potential to expose tricky memory safety issues or race conditions. This is the place to concentrate the fuzzing efforts. I'm confident in my understanding of the implications. Time to call `set-results`. |
| 22/2 | 2026/08/07 16:55 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"DEBUG_KMEMLEAK",
"KFENCE",
"PREEMPT_RT"
],
"FocusSymbols": [
"kfree_nolock",
"set_cpus_allowed_force",
"kmemleak_may_need_free",
"kfence_obj_to_llnode",
"kfence_llnode_to_obj"
],
"Reasoning": "The patch modifies `kfree_nolock()` in `mm/slub.c` to support freeing objects allocated by `kmalloc()`, `kmem_cache_alloc()`, and large kmallocs, as well as KFENCE objects. It defers freeing to an `irq_work` if necessary (e.g., for kmemleak or KFENCE). It also modifies `set_cpus_allowed_force()` in `kernel/sched/core.c` to use `kfree_nolock()` instead of `kfree_rcu()`. These are core memory management and scheduler changes that are reachable and should be fuzzed.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"DEBUG_KMEMLEAK",
"KFENCE",
"PREEMPT_RT"
],
"FocusSymbols": [
"kfree_nolock",
"set_cpus_allowed_force",
"kmemleak_may_need_free",
"kfence_obj_to_llnode",
"kfence_llnode_to_obj"
],
"Reasoning": "The patch modifies `kfree_nolock()` in `mm/slub.c` to support freeing objects allocated by `kmalloc()`, `kmem_cache_alloc()`, and large kmallocs, as well as KFENCE objects. It defers freeing to an `irq_work` if necessary (e.g., for kmemleak or KFENCE). It also modifies `set_cpus_allowed_force()` in `kernel/sched/core.c` to use `kfree_nolock()` instead of `kfree_rcu()`. These are core memory management and scheduler changes that are reachable and should be fuzzed.",
"WorthFuzzing": true
} |
| 23/1 | 2026/08/07 16:55 | action | If |
0mArgs:{
"WorthFuzzing": true
}Results: null |
| 24/2 | 2026/08/07 16:55 | agent | kmsan-evaluator |
0mModel:best-expensive Args: null Results: {
"KMSANReasoning": "The patch modifies the internal memory management freeing paths, specifically extending `kfree_nolock()` to handle objects allocated by `kmalloc()` and `kmem_cache_alloc()`, including large kmallocs and KFENCE objects. It defers freeing to an `irq_work` using a lockless list (`llist`) when necessary. The changes involve overwriting parts of the freed objects (like the freepointer or the first word of a large kmalloc object) to store the `llist_node`. Since these objects are in the process of being freed, any access to them would constitute a use-after-free bug. The patch does not introduce new structures copied to user space, nor does it alter bounds checking or initialization logic of active objects. Any bugs introduced by these changes (such as use-after-free, double free, or memory corruption) will be effectively caught by KASAN and standard SLUB debugging tools. There are no uninitialized memory risks or info-leaks exposed here that would require KMSAN.",
"NeedsKMSAN": false
}Instruction: You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.
CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
a comprehensive suite of debugging tools and sanitizers, including KASAN
(out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
(locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
or page allocations) and kernel-to-user memory info-leaks.
Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.
Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.
Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
attributes, ioctl output arguments, socket options, or BPF buffers) where fields
or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
uninitialized bytes of existing buffers.
Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
dereferences, locking deadlocks, or use-after-free bugs (these are already caught
by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.
Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: For your convenience, here is the diff of the changes:
commit e5b86bdc5984b6b0ba31de079501de6b21619874
Author: syz-cluster <triage@syzkaller.com>
Date: Fri Aug 7 16:53:46 2026 +0000
syz-cluster: applied patch under review
diff --git a/include/linux/kfence.h b/include/linux/kfence.h
index e5822f6e7f279..00721c85258dc 100644
--- a/include/linux/kfence.h
+++ b/include/linux/kfence.h
@@ -188,6 +188,9 @@ static __always_inline __must_check bool kfence_free(void *addr)
return true;
}
+struct llist_node *kfence_obj_to_llnode(void *addr);
+void *kfence_llnode_to_obj(struct llist_node *llnode);
+
/**
* kfence_handle_page_fault() - perform page fault handling for KFENCE pages
* @addr: faulting address
@@ -235,6 +238,8 @@ static inline size_t kfence_ksize(const void *addr) { return 0; }
static inline void *kfence_object_start(const void *addr) { return NULL; }
static inline void __kfence_free(void *addr) { }
static inline bool __must_check kfence_free(void *addr) { return false; }
+static inline struct llist_node *kfence_obj_to_llnode(void *addr) { return NULL; }
+static inline void *kfence_llnode_to_obj(struct llist_node *llnode) { return NULL; }
static inline bool __must_check kfence_handle_page_fault(unsigned long addr, bool is_write,
struct pt_regs *regs)
{
diff --git a/include/linux/kmemleak.h b/include/linux/kmemleak.h
index fbd424b2abb14..52f75f10a9ce6 100644
--- a/include/linux/kmemleak.h
+++ b/include/linux/kmemleak.h
@@ -22,6 +22,7 @@ extern void kmemleak_alloc_percpu(const void __percpu *ptr, size_t size,
extern void kmemleak_vmalloc(const struct vm_struct *area, size_t size,
gfp_t gfp) __ref;
extern void kmemleak_free(const void *ptr) __ref;
+bool kmemleak_may_need_free(const void *ptr) __ref;
extern void kmemleak_free_part(const void *ptr, size_t size) __ref;
extern void kmemleak_free_percpu(const void __percpu *ptr) __ref;
extern void kmemleak_update_trace(const void *ptr) __ref;
@@ -50,6 +51,14 @@ static inline void kmemleak_free_recursive(const void *ptr, slab_flags_t flags)
kmemleak_free(ptr);
}
+static inline bool kmemleak_may_need_free_recursive(const void *ptr, slab_flags_t flags)
+{
+ if (!(flags & SLAB_NOLEAKTRACE))
+ return kmemleak_may_need_free(ptr);
+
+ return false;
+}
+
static inline void kmemleak_erase(void **ptr)
{
*ptr = NULL;
@@ -86,6 +95,14 @@ static inline void kmemleak_free_part(const void *ptr, size_t size)
static inline void kmemleak_free_recursive(const void *ptr, slab_flags_t flags)
{
}
+static inline bool kmemleak_may_need_free(const void *ptr)
+{
+ return false;
+}
+static inline bool kmemleak_may_need_free_recursive(const void *ptr, slab_flags_t flags)
+{
+ return false;
+}
static inline void kmemleak_free_percpu(const void __percpu *ptr)
{
}
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 96226707c2f61..d2929e4e23f13 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -2807,20 +2807,15 @@ void set_cpus_allowed_force(struct task_struct *p, const struct cpumask *new_mas
.user_mask = NULL,
.flags = SCA_USER, /* clear the user requested mask */
};
- union cpumask_rcuhead {
- cpumask_t cpumask;
- struct rcu_head rcu;
- };
scoped_guard (__task_rq_lock, p)
do_set_cpus_allowed(p, &ac);
/*
* Because this is called with p->pi_lock held, it is not possible
- * to use kfree() here (when PREEMPT_RT=y), therefore punt to using
- * kfree_rcu().
+ * to use kfree() here (when PREEMPT_RT=y), thus use kfree_nolock()
*/
- kfree_rcu((union cpumask_rcuhead *)ac.user_mask, rcu);
+ kfree_nolock(ac.user_mask);
}
int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *src,
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 56acf502ba260..6a8d0578e9633 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -2886,12 +2886,7 @@ static inline bool task_allowed_on_cpu(struct task_struct *p, int cpu)
static inline cpumask_t *alloc_user_cpus_ptr(int node)
{
- /*
- * See set_cpus_allowed_force() above for the rcu_head usage.
- */
- int size = max_t(int, cpumask_size(), sizeof(struct rcu_head));
-
- return kmalloc_node(size, GFP_KERNEL, node);
+ return kmalloc_node(cpumask_size(), GFP_KERNEL, node);
}
static inline struct task_struct *get_push_task(struct rq *rq)
diff --git a/mm/kfence/core.c b/mm/kfence/core.c
index 6577bd76954eb..42519d24687ff 100644
--- a/mm/kfence/core.c
+++ b/mm/kfence/core.c
@@ -1271,6 +1271,20 @@ void __kfence_free(void *addr)
}
}
+struct llist_node *kfence_obj_to_llnode(void *addr)
+{
+ struct kfence_metadata *meta = addr_to_metadata((unsigned long)addr);
+
+ return &meta->llnode;
+}
+
+void *kfence_llnode_to_obj(struct llist_node *llnode)
+{
+ struct kfence_metadata *meta = container_of(llnode, struct kfence_metadata, llnode);
+
+ return (void *)meta->addr;
+}
+
bool kfence_handle_page_fault(unsigned long addr, bool is_write, struct pt_regs *regs)
{
const int page_index = (addr - (unsigned long)__kfence_pool) / PAGE_SIZE;
diff --git a/mm/kfence/kfence.h b/mm/kfence/kfence.h
index 1f618f9b0d12d..0fca1dc2c7941 100644
--- a/mm/kfence/kfence.h
+++ b/mm/kfence/kfence.h
@@ -58,7 +58,10 @@ struct kfence_track {
/* KFENCE metadata per guarded allocation. */
struct kfence_metadata {
struct list_head list __guarded_by(&kfence_freelist_lock); /* Freelist node. */
- struct rcu_head rcu_head; /* For delayed freeing. */
+ union {
+ struct rcu_head rcu_head; /* For delayed freeing. */
+ struct llist_node llnode; /* For kfree_nolock(). */
+ };
/*
* Lock protecting below data; to ensure consistency of the below data,
diff --git a/mm/kmemleak.c b/mm/kmemleak.c
index 7c7ba17ce7af0..e3560ce826326 100644
--- a/mm/kmemleak.c
+++ b/mm/kmemleak.c
@@ -1168,6 +1168,48 @@ void __ref kmemleak_free(const void *ptr)
}
EXPORT_SYMBOL_GPL(kmemleak_free);
+/**
+ * kmemleak_may_need_free - check if object is registered
+ * @ptr: pointer to beginning of the object
+ *
+ * This function is called from the kernel allocator when an object should be
+ * freed but the caller context might be unsafe to spin on the internal locks.
+ *
+ * It will therefore only use trylock and thus might return a false positive
+ * if the trylock fails and the status cannot be determined.
+ *
+ * For objects that (might) need free, the allocator has to defer the actual
+ * freeing to a safe context.
+ *
+ * The assumption is that most objects freed from the unsafe context are also
+ * allocated in such context and thus are not registered in kmemleak, so it's
+ * unlikely the defered freeing will be necessary just because kmemleak is
+ * enabled.
+ */
+bool __ref kmemleak_may_need_free(const void *ptr)
+{
+ unsigned long flags;
+ struct kmemleak_object *object;
+
+ pr_debug("%s(0x%px)\n", __func__, ptr);
+
+ if (!kmemleak_free_enabled || !ptr || IS_ERR(ptr))
+ return false;
+
+ /* On UP, raw_spin_trylock() always succeeds even when it is locked */
+ if (!IS_ENABLED(CONFIG_SMP) && in_nmi())
+ return true;
+
+ if (!raw_spin_trylock_irqsave(&kmemleak_lock, flags))
+ return true;
+
+ object = __lookup_object((unsigned long)ptr, 0, 0);
+
+ raw_spin_unlock_irqrestore(&kmemleak_lock, flags);
+
+ return !!object;
+}
+
/**
* kmemleak_free_part - partially unregister a previously registered object
* @ptr: pointer to the beginning or inside the object. This also
diff --git a/mm/slub.c b/mm/slub.c
index b9aeb02a880f1..3be98faa9f0de 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -4050,6 +4050,8 @@ static void flush_all(struct kmem_cache *s)
struct deferred_percpu_work {
struct llist_head objects;
+ struct llist_head objects_kfence;
+ struct llist_head objects_large_kmalloc;
struct llist_head objects_by_rcu;
struct llist_head rcu_sheaves;
struct irq_work work;
@@ -4059,6 +4061,8 @@ static void deferred_percpu_work_fn(struct irq_work *work);
static DEFINE_PER_CPU(struct deferred_percpu_work, deferred_percpu_work) = {
.objects = LLIST_HEAD_INIT(objects),
+ .objects_kfence = LLIST_HEAD_INIT(objects_kfence),
+ .objects_large_kmalloc = LLIST_HEAD_INIT(objects_large_kmalloc),
.objects_by_rcu = LLIST_HEAD_INIT(objects_by_rcu),
.rcu_sheaves = LLIST_HEAD_INIT(rcu_sheaves),
.work = IRQ_WORK_INIT(deferred_percpu_work_fn),
@@ -6363,6 +6367,21 @@ static void free_to_pcs_bulk(struct kmem_cache *s, size_t size, void **p)
}
}
+static inline void
+__free_large_kmalloc_page(struct page *page, unsigned int free_flags)
+{
+ unsigned int order = compound_order(page);
+
+ mod_lruvec_page_state(page, NR_SLAB_UNRECLAIMABLE_B,
+ -(PAGE_SIZE << order));
+ __ClearPageLargeKmalloc(page);
+
+ if (free_flags & SLAB_FREE_NOLOCK)
+ free_frozen_pages_nolock(page, order);
+ else
+ free_frozen_pages(page, order);
+}
+
/*
* In PREEMPT_RT irq_work runs in per-cpu kthread, so it's safe
* to take sleeping spin_locks from __slab_free().
@@ -6371,16 +6390,12 @@ static void free_to_pcs_bulk(struct kmem_cache *s, size_t size, void **p)
static void deferred_percpu_work_fn(struct irq_work *work)
{
struct deferred_percpu_work *dpw;
- struct llist_head *objs, *objs_by_rcu, *rcu_sheaves;
struct llist_node *llnode, *pos, *t;
struct slab_sheaf *sheaf, *next;
dpw = container_of(work, struct deferred_percpu_work, work);
- rcu_sheaves = &dpw->rcu_sheaves;
- objs = &dpw->objects;
- objs_by_rcu = &dpw->objects_by_rcu;
- llnode = llist_del_all(objs);
+ llnode = llist_del_all(&dpw->objects);
llist_for_each_safe(pos, t, llnode) {
struct kmem_cache *s;
struct slab *slab;
@@ -6392,6 +6407,8 @@ static void deferred_percpu_work_fn(struct irq_work *work)
/* Point 'x' back to the beginning of allocated object */
x -= s->offset;
+ kmemleak_free_recursive(x, s->flags);
+
/*
* We used freepointer in 'x' to link 'x' into df->objects.
* Clear it to NULL to avoid false positive detection
@@ -6403,7 +6420,30 @@ static void deferred_percpu_work_fn(struct irq_work *work)
stat(s, FREE_SLOWPATH);
}
- llnode = llist_del_all(objs_by_rcu);
+ llnode = llist_del_all(&dpw->objects_kfence);
+ llist_for_each_safe(pos, t, llnode) {
+ struct kmem_cache *s;
+ struct slab *slab;
+ void *obj = kfence_llnode_to_obj(pos);
+
+ slab = virt_to_slab(obj);
+ s = slab->slab_cache;
+
+ kmemleak_free_recursive(obj, s->flags);
+
+ __kfence_free(obj);
+ }
+
+ llnode = llist_del_all(&dpw->objects_large_kmalloc);
+ llist_for_each_safe(pos, t, llnode) {
+ struct page *page = virt_to_page(pos);
+
+ kmemleak_free(pos);
+
+ __free_large_kmalloc_page(page, SLAB_FREE_DEFAULT);
+ }
+
+ llnode = llist_del_all(&dpw->objects_by_rcu);
llist_for_each_safe(pos, t, llnode) {
void *head = pos;
void *objp = kvmalloc_obj_start_addr(head);
@@ -6411,21 +6451,60 @@ static void deferred_percpu_work_fn(struct irq_work *work)
kvfree_call_rcu(head, objp);
}
- llnode = llist_del_all(rcu_sheaves);
+ llnode = llist_del_all(&dpw->rcu_sheaves);
llist_for_each_entry_safe(sheaf, next, llnode, llnode)
call_rcu(&sheaf->rcu_head, rcu_free_sheaf);
}
-static void defer_free(struct kmem_cache *s, void *head)
+static void defer_free(struct kmem_cache *s, void *obj)
{
struct deferred_percpu_work *dpw;
+ struct llist_node *llnode;
+
+ /*
+ * Place the llist node where the freepointer would be if we freed the
+ * object immediately. That means we can write there safely, only need
+ * to remove kasan tag first.
+ */
+ llnode = kasan_reset_tag(obj) + s->offset;
guard(preempt)();
- head = kasan_reset_tag(head);
+ dpw = this_cpu_ptr(&deferred_percpu_work);
+ if (llist_add(llnode, &dpw->objects))
+ irq_work_queue(&dpw->work);
+}
+
+static void defer_free_kfence(void *obj)
+{
+ struct deferred_percpu_work *dpw;
+ struct llist_node *llnode;
+
+ /* kasan_reset_tag() is not necessary, kfence objects are not tagged */
+ llnode = kfence_obj_to_llnode(obj);
+
+ guard(preempt)();
+
+ dpw = this_cpu_ptr(&deferred_percpu_work);
+ if (llist_add(llnode, &dpw->objects_kfence))
+ irq_work_queue(&dpw->work);
+}
+
+static void defer_free_large_kmalloc(void *obj)
+{
+ struct deferred_percpu_work *dpw;
+ struct llist_node *llnode;
+
+ /*
+ * we can simply use the first word of the large kmalloc object
+ * for the llnode, as there's no ctor or TYPESAFE_BY_RCU
+ */
+ llnode = kasan_reset_tag(obj);
+
+ guard(preempt)();
dpw = this_cpu_ptr(&deferred_percpu_work);
- if (llist_add(head + s->offset, &dpw->objects))
+ if (llist_add(llnode, &dpw->objects_large_kmalloc))
irq_work_queue(&dpw->work);
}
@@ -6677,9 +6756,11 @@ size_t ksize(const void *objp)
}
EXPORT_SYMBOL(ksize);
-static void free_large_kmalloc(struct page *page, void *object)
+static void free_large_kmalloc(struct page *page, void *object,
+ unsigned int free_flags)
{
unsigned int order = compound_order(page);
+ bool nolock = free_flags & SLAB_FREE_NOLOCK;
if (WARN_ON_ONCE(!PageLargeKmalloc(page))) {
dump_page(page, "Not a kmalloc allocation");
@@ -6689,14 +6770,16 @@ static void free_large_kmalloc(struct page *page, void *object)
if (WARN_ON_ONCE(order == 0))
pr_warn_once("object pointer: 0x%p\n", object);
- kmemleak_free(object);
+ if (!nolock)
+ kmemleak_free(object);
+
kasan_kfree_large(object);
kmsan_kfree_large(object);
- mod_lruvec_page_state(page, NR_SLAB_UNRECLAIMABLE_B,
- -(PAGE_SIZE << order));
- __ClearPageLargeKmalloc(page);
- free_frozen_pages(page, order);
+ if (unlikely(nolock && kmemleak_may_need_free(object)))
+ defer_free_large_kmalloc(object);
+ else
+ __free_large_kmalloc_page(page, free_flags);
}
/*
@@ -6718,7 +6801,7 @@ void kvfree_rcu_cb(struct rcu_head *head)
if (slab)
slab_free(slab->slab_cache, slab, obj, _RET_IP_);
else
- free_large_kmalloc(page, obj);
+ free_large_kmalloc(page, obj, SLAB_FREE_DEFAULT);
}
}
@@ -6744,7 +6827,7 @@ void kfree(const void *object)
slab = page_slab(page);
if (!slab) {
/* kmalloc_nolock() doesn't support large kmalloc */
- free_large_kmalloc(page, (void *)object);
+ free_large_kmalloc(page, (void *)object, SLAB_FREE_DEFAULT);
return;
}
@@ -6755,15 +6838,14 @@ EXPORT_SYMBOL(kfree);
/*
* Can be called while holding raw_spinlock_t or from IRQ and NMI,
- * but ONLY for objects allocated by kmalloc_nolock().
- * Debug checks (like kmemleak and kfence) were skipped on allocation,
- * hence
- * obj = kmalloc(); kfree_nolock(obj);
- * will miss kmemleak/kfence book keeping and will cause false positives.
- * large_kmalloc is not supported either.
+ * but may defer freeing to irq_work() in some cases.
+ *
+ * Intended mainly for objects allocated from kmalloc_nolock(), but can handle
+ * also kmem_cache_alloc() and kmalloc() objects, including large_kmalloc.
*/
void kfree_nolock(const void *object)
{
+ struct page *page;
struct slab *slab;
struct kmem_cache *s;
void *x = (void *)object;
@@ -6771,9 +6853,10 @@ void kfree_nolock(const void *object)
if (unlikely(ZERO_OR_NULL_PTR(object)))
return;
- slab = virt_to_slab(object);
+ page = virt_to_page(object);
+ slab = page_slab(page);
if (unlikely(!slab)) {
- WARN_ONCE(1, "large_kmalloc is not supported by kfree_nolock()");
+ free_large_kmalloc(page, (void *)object, SLAB_FREE_NOLOCK);
return;
}
@@ -6791,6 +6874,12 @@ void kfree_nolock(const void *object)
* since they take spinlocks or not safe from any context.
*/
kmsan_slab_free(s, x);
+
+ if (is_kfence_address(x)) {
+ defer_free_kfence(x);
+ return;
+ }
+
/*
* If KASAN finds a kernel bug it will do kasan_report_invalid_free()
* which will call raw_spin_lock_irqsave() which is technically
@@ -6809,9 +6898,23 @@ void kfree_nolock(const void *object)
*/
kasan_slab_free(s, x, false, false, /* skip quarantine */true);
+ /*
+ * with kfree() the kmemleak handling happens much sooner, but for
+ * defering we need to write llnode to the object's freepointer so
+ * we should have it in the state when it's no longer treated as
+ * allocated by kasan etc.
+ *
+ * defer_free will also reset the pointer tag, but it's ok to do a
+ * deferred kmemleak_free() using the untagged pointer, because
+ * __lookup_object() resets the tag anyway
+ */
+ if (unlikely(kmemleak_may_need_free_recursive(x, s->flags)))
+ goto defer;
+
if (likely(can_free_to_pcs(slab)) && likely(free_to_pcs(s, x, false)))
return;
+defer:
/*
* __slab_free() can locklessly cmpxchg16 into a slab, but then it might
* need to take spin_lock for further processing.
@@ -7114,7 +7217,7 @@ int build_detached_freelist(struct kmem_cache *s, size_t size,
if (!s) {
/* Handle kalloc'ed objects */
if (!slab) {
- free_large_kmalloc(page, object);
+ free_large_kmalloc(page, object, SLAB_FREE_DEFAULT);
df->slab = NULL;
return size;
}
|
| 25/3 | 2026/08/07 16:55 | llm | kmsan-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 8417 output: 241 thoughts: 1094 Thoughts: **KMSAN Risk Assessment of Memory Freeing Patch**
Okay, let's break this down. My initial concern, as with any memory management change, is whether this introduces uninitialized memory reads or exposes uninitialized memory to user space. The core of this patch centers around modifying `kfree_nolock()` and its related functions to handle object freeing, especially those allocated by `kmalloc()` and `kmem_cache_alloc()`, including large kmallocs and KFENCE objects. The interesting part is the deferral to `irq_work` under specific conditions, like if `kmemleak_may_need_free()` is true, if it's a KFENCE object, or a large kmalloc object triggering kmemleak freeing. It also looks like a change to `set_cpus_allowed_force()` to use `kfree_nolock()` instead of `kfree_rcu()`.
My immediate focus is on potential uninitialized memory risks. Essentially, we're dealing with lockless freeing (`kfree_nolock`), and the patch utilizes the object's memory for an `llist_node` to handle the deferred freeing. For `defer_free()`, it uses `llnode = kasan_reset_tag(obj) + s->offset;`, which seems fine. The `defer_free_kfence()` uses `llnode = kfence_obj_to_llnode(obj);` (using `&meta->llnode`), also seems correct, as the KFENCE metadata is clearly defined. But the `defer_free_large_kmalloc()` uses `llnode = kasan_reset_tag(obj);`, which is the first word of the large kmalloc object.
The concern with `defer_free_large_kmalloc()` is that by overwriting the first 8 bytes of the object, am I inadvertently exposing uninitialized memory? No. This is not the case because the object is *being freed*. The `llnode` is purely for internal linking within the SLUB allocator's deferred free list. Once the `irq_work` runs, the object is removed from the list and actually freed. Thus, any content in the object is irrelevant at this point and there should be no risk of uninitialized memory being read.
There's no introduction of new structures exposed to user space, no modifications to bounds checking or data length calculations, and no allocation of complex data structures with uninitialized fields. This is all internal memory management within the freeing paths. Consequently, the likely bugs introduced would be use-after-free, double free, or memory corruption. These are *precisely* the kinds of issues KASAN and standard SLUB debugging are designed to catch.
Let's double-check that `defer_free_large_kmalloc()` code block:
```c
static void defer_free_large_kmalloc(void *obj)
{
struct deferred_percpu_work *dpw;
struct llist_node *llnode;
/*
* we can simply use the first word of the large kmalloc object
* for the llnode, as there's no ctor or TYPESAFE_BY_RCU
*/
llnode = kasan_reset_tag(obj);
guard(preempt)();
dpw = this_cpu_ptr(&deferred_percpu_work);
if (llist_add(llnode, &dpw->objects_large_kmalloc))
irq_work_queue(&dpw->work);
}
```
Yeah, exactly as I expected. It overwrites the first 8 bytes of the object to be freed. Since the object is already scheduled to be freed, it should not be accessed by user space any longer. If it *is* accessed, that's a use-after-free, which KASAN *will* catch.
Now, let's briefly look at the change in `set_cpus_allowed_force()`:
```c
- kfree_rcu((union cpumask_rcuhead *)ac.user_mask, rcu);
+ kfree_nolock(ac.user_mask);
```
It's just replacing `kfree_rcu` with `kfree_nolock`. The `ac.user_mask` was allocated with `alloc_user_cpus_ptr`.
```c
static inline cpumask_t *alloc_user_cpus_ptr(int node)
{
- /*
- * See set_cpus_allowed_force() above for the rcu_head usage.
- */
- int size = max_t(int, cpumask_size
|
| 26/3 | 2026/08/07 16:55 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch modifies the internal memory management freeing paths, specifically extending `kfree_nolock()` to handle objects allocated by `kmalloc()` and `kmem_cache_alloc()`, including large kmallocs and KFENCE objects. It defers freeing to an `irq_work` using a lockless list (`llist`) when necessary. The changes involve overwriting parts of the freed objects (like the freepointer or the first word of a large kmalloc object) to store the `llist_node`. Since these objects are in the process of being freed, any access to them would constitute a use-after-free bug. The patch does not introduce new structures copied to user space, nor does it alter bounds checking or initialization logic of active objects. Any bugs introduced by these changes (such as use-after-free, double free, or memory corruption) will be effectively caught by KASAN and standard SLUB debugging tools. There are no uninitialized memory risks or info-leaks exposed here that would require KMSAN.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch modifies the internal memory management freeing paths, specifically extending `kfree_nolock()` to handle objects allocated by `kmalloc()` and `kmem_cache_alloc()`, including large kmallocs and KFENCE objects. It defers freeing to an `irq_work` using a lockless list (`llist`) when necessary. The changes involve overwriting parts of the freed objects (like the freepointer or the first word of a large kmalloc object) to store the `llist_node`. Since these objects are in the process of being freed, any access to them would constitute a use-after-free bug. The patch does not introduce new structures copied to user space, nor does it alter bounds checking or initialization logic of active objects. Any bugs introduced by these changes (such as use-after-free, double free, or memory corruption) will be effectively caught by KASAN and standard SLUB debugging tools. There are no uninitialized memory risks or info-leaks exposed here that would require KMSAN.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|