uprobes: Use flexible array for xol_area bitmap
Subsystems (3)
PATCH 1/1 uprobes: Use flexible array for xol_area bitmap
The XOL slot bitmap has the same lifetime as struct xol_area, but it is currently allocated separately. That adds another allocation failure path and a matching cleanup branch without buying any extra flexibility.
Store the bitmap as a flexible array member and allocate it together with the xol_area using kzalloc_flex(). The bitmap remains zero-initialized, while the allocation and error handling become simpler.
--- kernel/events/uprobes.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-)
kernel/events/uprobes.c +3 -10
@@ -108,7 +108,6 @@ static LIST_HEAD(delayed_uprobe_list);*/ struct xol_area {wait_queue_head_t wq;/* if all slots are busy */ -unsigned long *bitmap;/* 0 = free slot */ struct page *page;/*
@@ -117,6 +116,7 @@ struct xol_area {* the vma go away, and we must handle that reasonably gracefully. */ unsigned long vaddr;/* Page(s) of instruction slots */ +unsigned long bitmap[];/* 0 = free slot */ };static void uprobe_warn (struct task_struct *t,const char *msg)
@@ -1755,18 +1755,13 @@ static struct xol_area *__create_xol_area(unsigned long vaddr)struct xol_area *area;void *insns; - area =kzalloc_obj (*area); + area =kzalloc_flex (*area, bitmap,BITS_TO_LONGS (UINSNS_PER_PAGE ));if (unlikely (!area)) goto out; - area->bitmap =kcalloc (BITS_TO_LONGS (UINSNS_PER_PAGE ),sizeof (long ), -GFP_KERNEL ); -if (!area->bitmap) - goto free_area; - area->page =alloc_page (GFP_HIGHUSER | __GFP_ZERO);if (!area->page) - goto free_bitmap; + goto free_area; area->vaddr = vaddr;init_waitqueue_head (&area->wq);
@@ -1779,8 +1774,6 @@ static struct xol_area *__create_xol_area(unsigned long vaddr)return area;__free_page (area->page); - free_bitmap: -kfree (area->bitmap); free_area:kfree (area); out:
Signature · 1 line
2.54.0
Masami Hiramatsu (Google)
maintainerQuoted 10 lines · On Sun, 10 May 2026 14:41:18 -0700 Rosen Penev <[email protected]> wrote:
The XOL slot bitmap has the same lifetime as struct
xol_area, but it is currently allocated separately. That adds another allocation failure path and a matching cleanup branch without buying any extra flexibility.Store the bitmap as a flexible array member and allocate it together with the
xol_areausingkzalloc_flex(). The bitmap remains zero-initialized, while the allocation and error handling become simpler.
You also have to update uprobe_clear_state(), because area->bitmap is no longer allocated separately.
Thank you,
Quoted 59 lines
Assisted-by Codex:GPT-5.5Signed-off-by Rosen Penev <[email protected]>--- kernel/events/uprobes.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-)kernel/events/uprobes.c +3
-10@@ -108,7 +108,6 @@ static LIST_HEAD(delayed_uprobe_list);*/ struct xol_area {wait_queue_head_t wq;/* if all slots are busy */ -unsigned long *bitmap;/* 0 = free slot */ struct page *page;/* @@ -117,6 +116,7 @@ struct xol_area {* the vma go away, and we must handle that reasonably gracefully. */ unsigned long vaddr;/* Page(s) of instruction slots */ +unsigned long bitmap[];/* 0 = free slot */ };static void uprobe_warn (struct task_struct *t,const char *msg)@@ -1755,18 +1755,13 @@ static struct xol_area *__create_xol_area(unsigned long vaddr)struct xol_area *area;void *insns; - area =kzalloc_obj (*area); + area =kzalloc_flex (*area, bitmap,BITS_TO_LONGS (UINSNS_PER_PAGE ));if (unlikely (!area)) goto out; - area->bitmap =kcalloc (BITS_TO_LONGS (UINSNS_PER_PAGE ),sizeof (long ), -GFP_KERNEL ); -if (!area->bitmap) - goto free_area; - area->page =alloc_page (GFP_HIGHUSER | __GFP_ZERO);if (!area->page) - goto free_bitmap; + goto free_area; area->vaddr = vaddr;init_waitqueue_head (&area->wq);@@ -1779,8 +1774,6 @@ static struct xol_area *__create_xol_area(unsigned long vaddr)return area;__free_page (area->page); - free_bitmap: -kfree (area->bitmap); free_area:kfree (area); out:Signature · 1 line
2.54.0
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] Calling kfree() on a flexible array member triggers an invalid free, leading to kernel panic…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] Calling kfree() on a flexible array member triggers an invalid free, leading to kernel panic or slab corruption. --
commit 2653ad1e88bfeb53b361b9c646fc5e888019afa9
Author: Rosen Penev <[email protected]>
uprobes: Use flexible array for xol_area bitmap
This commit converts the xol_area bitmap from a separately allocated pointer to a flexible array member. It also updates the allocation to use kzalloc_flex() and removes the associated error handling in __create_xol_area().
Quoted 13 lines
kernel/events/uprobes.c +0
-1@@ -108,7 +108,6 @@ static LIST_HEAD(delayed_uprobe_list);*/ struct xol_area {wait_queue_head_t wq;/* if all slots are busy */ -unsigned long *bitmap;/* 0 = free slot */ struct page *page;/*
kernel/events/uprobes.c struct xol_area { · line 120
Quoted 5 lines
kernel/events/uprobes.c +1
-0@@ -117,6 +116,7 @@ struct xol_area {* the vma go away, and we must handle that reasonably gracefully. */ unsigned long vaddr;/* Page(s) of instruction slots */ +unsigned long bitmap[];/* 0 = free slot */ };
Does this conversion leave a stale kfree() in uprobe_clear_state()?
Since bitmap is now a flexible array member, area->bitmap evaluates to an interior address within the xol_area slab object.
Looking at uprobe_clear_state(), the kfree() is still present:
kernel/events/uprobes.c:uprobe_clear_state() {
...
if (!area) return; put_page(area->page); kfree(area->bitmap); kfree(area); }
Could this lead to slab corruption or a kernel panic when a process with a registered uprobe is terminated, since we are passing an interior pointer to kfree()?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1