Skip to contentSkip to conversation

uprobes: Use flexible array for xol_area bitmap

PATCH Superseded Rosen Penev 4 participants 4 messages Nov 11

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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.

---
v2: add Acked-by. remove Assisted-by

 kernel/events/uprobes.c | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)
kernel/events/uprobes.c +3 -11
index 853fc952331c..876c3cfe8031 100644
@@ -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)
@@ -1760,18 +1760,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);
@@ -1784,8 +1779,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:
@@ -1826,7 +1819,6 @@ void uprobe_clear_state(struct mm_struct *mm)
 		return;
 
 	put_page(area->page);
-	kfree(area->bitmap);
 	kfree(area);
 }
 
Signature · 1 line
2.56.0
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 11 lines

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.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Rosen Penev
Quoted 38 lines · Rosen Penev

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.

---
v2: add Acked-by. remove Assisted-by

 kernel/events/uprobes.c | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)
kernel/events/uprobes.c +1 -1
index 853fc952331c..876c3cfe8031 100644
@@ -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)

kernel/events/uprobes.c static struct xol_area *__create_xol_area(unsigned long vaddr) · line 1763

Quoted 5 lines
kernel/events/uprobes.c +1 -1
@@ -1760,18 +1760,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));

I am confused; this is a compile time constant. Why are we using flex arrays?

maintainer
#3 lore raw
In reply to Rosen Penev
Quoted 12 lines · Rosen Penev
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 119

Quoted 4 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 */

I agree with Peter, you can just use

	unsigned long			bitmap[BITS_TO_LONGS(UINSNS_PER_PAGE)];

Oleg.