Skip to contentSkip to conversation

mm/vmalloc: Add a safer version of find_vm_area() for debug

PATCH Superseded Joel Fernandes (Google) 2 participants 8 messages Aug 31, 2023 – Sep 4, 2023

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 mm/vmalloc: Add a safer version of find_vm_area() for debug

It is unsafe to dump vmalloc area information when trying to do so from some contexts. Add a safer trylock version of the same function to do a best-effort VMA finding and use it from vmalloc_dump_obj().

[apply test robot feedback on unused function fix.]

---
v1->v2: Apply review tags and test robot feedback.

 mm/vmalloc.c | 39 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)
mm/vmalloc.c +38 -1
index 93cf99aba335..f09e882ae3b8 100644
@@ -1865,6 +1865,20 @@ struct vmap_area *find_vmap_area(unsigned long addr)
 	return va;
 }
 
+#ifdef CONFIG_PRINTK
+static struct vmap_area *find_vmap_area_trylock(unsigned long addr)
+{
+	struct vmap_area *va;
+
+	if (!spin_trylock(&vmap_area_lock))
+		return NULL;
+	va = __find_vmap_area(addr, &vmap_area_root);
+	spin_unlock(&vmap_area_lock);
+
+	return va;
+}
+#endif
+
 static struct vmap_area *find_unlink_vmap_area(unsigned long addr)
 {
 	struct vmap_area *va;
@@ -2671,6 +2685,29 @@ struct vm_struct *find_vm_area(const void *addr)
 	return va->vm;
 }
 
+/**
+ * try_to_find_vm_area - find a continuous kernel virtual area
+ * @addr:	  base address
+ *
+ * This function is the same as find_vm_area() except that it is
+ * safe to call if vmap_area_lock is already held and returns NULL
+ * if it is. See comments in find_vmap_area() for other details.
+ *
+ * Return: the area descriptor on success or %NULL on failure.
+ */
+#ifdef CONFIG_PRINTK
+static struct vm_struct *try_to_find_vm_area(const void *addr)
+{
+	struct vmap_area *va;
+
+	va = find_vmap_area_trylock((unsigned long)addr);
+	if (!va)
+		return NULL;
+
+	return va->vm;
+}
+#endif
+
 /**
  * remove_vm_area - find and remove a continuous kernel virtual area
  * @addr:	    base address
@@ -4277,7 +4314,7 @@ bool vmalloc_dump_obj(void *object)
 	struct vm_struct *vm;
 	void *objp = (void *)PAGE_ALIGN((unsigned long)object);
 
-	vm = find_vm_area(objp);
+	vm = try_to_find_vm_area(objp);
 	if (!vm)
 		return false;
 	pr_cont(" %u-page vmalloc region starting at %#lx allocated at %pS\n",
Signature · 1 line
2.42.0.rc2.253.gd59a3bf2b4-goog
#1 lore raw

PATCH v2 2/2 rcu: Dump vmalloc memory info safely

From: Zqiang <[email protected]>

Currently, for double invoke call_rcu(), will dump rcu_head objects memory info, if the objects is not allocated from the slab allocator, the vmalloc_dump_obj() will be invoke and the vmap_area_lock spinlock need to be held, since the call_rcu() can be invoked in interrupt context, therefore, there is a possibility of spinlock deadlock scenarios.

And in Preempt-RT kernel, the rcutorture test also trigger the following lockdep warning:

BUG: sleeping function called from invalid context at kernel/locking/spinlock_rt.c:48

in_atomic(): 1, irqs_disabled(): 1, non_block: 0, pid: 1, name: swapper/0
preempt_count: 1, expected: 0
RCU nest depth: 1, expected: 1
3 locks held by swapper/0/1:
#0: ffffffffb534ee80 (fullstop_mutex){+.+.}-{4:4}, at: torture_init_begin+0x24/0xa0 #1: ffffffffb5307940 (rcu_read_lock){....}-{1:3}, at: rcu_torture_init+0x1ec7/0x2370 #2: ffffffffb536af40 (vmap_area_lock){+.+.}-{3:3}, at: find_vmap_area+0x1f/0x70

irq event stamp: 565512
hardirqs last  enabled at (565511): [<ffffffffb379b138>] __call_rcu_common+0x218/0x940
hardirqs last disabled at (565512): [<ffffffffb5804262>] rcu_torture_init+0x20b2/0x2370
softirqs last  enabled at (399112): [<ffffffffb36b2586>] __local_bh_enable_ip+0x126/0x170
softirqs last disabled at (399106): [<ffffffffb43fef59>] inet_register_protosw+0x9/0x1d0
Preemption disabled at:
[<ffffffffb58040c3>] rcu_torture_init+0x1f13/0x2370
CPU: 0 PID: 1 Comm: swapper/0 Tainted: G        W          6.5.0-rc4-rt2-yocto-preempt-rt+ #15
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS rel-1.16.2-0-gea1b7a073390-prebuilt.qemu.org 04/01/2014
Call Trace:
 <TASK>
 dump_stack_lvl+0x68/0xb0
 dump_stack+0x14/0x20
 __might_resched+0x1aa/0x280
 ? __pfx_rcu_torture_err_cb+0x10/0x10
 rt_spin_lock+0x53/0x130
 ? find_vmap_area+0x1f/0x70
 find_vmap_area+0x1f/0x70
 vmalloc_dump_obj+0x20/0x60
 mem_dump_obj+0x22/0x90
 __call_rcu_common+0x5bf/0x940
 ? debug_smp_processor_id+0x1b/0x30
 call_rcu_hurry+0x14/0x20
 rcu_torture_init+0x1f82/0x2370
 ? __pfx_rcu_torture_leak_cb+0x10/0x10
 ? __pfx_rcu_torture_leak_cb+0x10/0x10
 ? __pfx_rcu_torture_init+0x10/0x10
 do_one_initcall+0x6c/0x300
 ? debug_smp_processor_id+0x1b/0x30
 kernel_init_freeable+0x2b9/0x540
 ? __pfx_kernel_init+0x10/0x10
 kernel_init+0x1f/0x150
 ret_from_fork+0x40/0x50
 ? __pfx_kernel_init+0x10/0x10
 ret_from_fork_asm+0x1b/0x30
 </TASK>

The previous patch fixes this by using the deadlock-safe best-effort version of find_vm_area. However, in case of failure print the fact that the pointer was a vmalloc pointer so that we print at least something.

---
 mm/util.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
mm/util.c +3 -1
index dd12b9531ac4..406634f26918 100644
@@ -1071,7 +1071,9 @@ void mem_dump_obj(void *object)
 	if (vmalloc_dump_obj(object))
 		return;
 
-	if (virt_addr_valid(object))
+	if (is_vmalloc_addr(object))
+		type = "vmalloc memory";
+	else if (virt_addr_valid(object))
 		type = "non-slab/vmalloc memory";
 	else if (object == NULL)
 		type = "NULL pointer";
Signature · 1 line
2.42.0.rc2.253.gd59a3bf2b4-goog
maintainer
#2 lore raw
In reply to Joel Fernandes (Google)
Quoted 75 lines · Joel Fernandes (Google)

It is unsafe to dump vmalloc area information when trying to do so from some contexts. Add a safer trylock version of the same function to do a best-effort VMA finding and use it from vmalloc_dump_obj().

[apply test robot feedback on unused function fix.]

---
v1->v2: Apply review tags and test robot feedback.

 mm/vmalloc.c | 39 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)
mm/vmalloc.c +37 -0
index 93cf99aba335..f09e882ae3b8 100644
@@ -1865,6 +1865,20 @@ struct vmap_area *find_vmap_area(unsigned long addr)
 	return va;
 }
 
+#ifdef CONFIG_PRINTK
+static struct vmap_area *find_vmap_area_trylock(unsigned long addr)
+{
+	struct vmap_area *va;
+
+	if (!spin_trylock(&vmap_area_lock))
+		return NULL;
+	va = __find_vmap_area(addr, &vmap_area_root);
+	spin_unlock(&vmap_area_lock);
+
+	return va;
+}
+#endif
+
 static struct vmap_area *find_unlink_vmap_area(unsigned long addr)
 {
 	struct vmap_area *va;
@@ -2671,6 +2685,29 @@ struct vm_struct *find_vm_area(const void *addr)
 	return va->vm;
 }
 
+/**
+ * try_to_find_vm_area - find a continuous kernel virtual area
+ * @addr:	  base address
+ *
+ * This function is the same as find_vm_area() except that it is
+ * safe to call if vmap_area_lock is already held and returns NULL
+ * if it is. See comments in find_vmap_area() for other details.
+ *
+ * Return: the area descriptor on success or %NULL on failure.
+ */
+#ifdef CONFIG_PRINTK
+static struct vm_struct *try_to_find_vm_area(const void *addr)
+{
+	struct vmap_area *va;
+
+	va = find_vmap_area_trylock((unsigned long)addr);
+	if (!va)
+		return NULL;
+
+	return va->vm;
+}
+#endif
+
 /**
  * remove_vm_area - find and remove a continuous kernel virtual area
  * @addr:	    base address

mm/vmalloc.c bool vmalloc_dump_obj(void *object) · line 4320

Quoted 8 lines
mm/vmalloc.c +1 -1
@@ -4277,7 +4314,7 @@ bool vmalloc_dump_obj(void *object)
 	struct vm_struct *vm;
 	void *objp = (void *)PAGE_ALIGN((unsigned long)object);
 
-	vm = find_vm_area(objp);
+	vm = try_to_find_vm_area(objp);
 	if (!vm)
 		return false;
 	pr_cont(" %u-page vmalloc region starting at %#lx allocated at %pS\n",

I am not sure if this patch makes a lot of sense. I agree, this is a problem and it mitigates it. But it is broken in terms of once you drop the lock, the VA should not be accessed.

Is that a real issue or it gets triggered due to some syntetic test case?

If i were you, i would go with open-coded version of trylock. Because there is only one user so far.

--
Uladzislau Rezki

#3 lore raw
Quoted 78 lines · Uladzislau Rezki
Quoted 77 lines · Joel Fernandes (Google)

It is unsafe to dump vmalloc area information when trying to do so from some contexts. Add a safer trylock version of the same function to do a best-effort VMA finding and use it from vmalloc_dump_obj().

[apply test robot feedback on unused function fix.]

---
v1->v2: Apply review tags and test robot feedback.

 mm/vmalloc.c | 39 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)
mm/vmalloc.c +37 -0
index 93cf99aba335..f09e882ae3b8 100644
@@ -1865,6 +1865,20 @@ struct vmap_area *find_vmap_area(unsigned long addr)
 	return va;
 }
 
+#ifdef CONFIG_PRINTK
+static struct vmap_area *find_vmap_area_trylock(unsigned long addr)
+{
+	struct vmap_area *va;
+
+	if (!spin_trylock(&vmap_area_lock))
+		return NULL;
+	va = __find_vmap_area(addr, &vmap_area_root);
+	spin_unlock(&vmap_area_lock);
+
+	return va;
+}
+#endif
+
 static struct vmap_area *find_unlink_vmap_area(unsigned long addr)
 {
 	struct vmap_area *va;
@@ -2671,6 +2685,29 @@ struct vm_struct *find_vm_area(const void *addr)
 	return va->vm;
 }
 
+/**
+ * try_to_find_vm_area - find a continuous kernel virtual area
+ * @addr:	  base address
+ *
+ * This function is the same as find_vm_area() except that it is
+ * safe to call if vmap_area_lock is already held and returns NULL
+ * if it is. See comments in find_vmap_area() for other details.
+ *
+ * Return: the area descriptor on success or %NULL on failure.
+ */
+#ifdef CONFIG_PRINTK
+static struct vm_struct *try_to_find_vm_area(const void *addr)
+{
+	struct vmap_area *va;
+
+	va = find_vmap_area_trylock((unsigned long)addr);
+	if (!va)
+		return NULL;
+
+	return va->vm;
+}
+#endif
+
 /**
  * remove_vm_area - find and remove a continuous kernel virtual area
  * @addr:	    base address
@@ -4277,7 +4314,7 @@ bool vmalloc_dump_obj(void *object)
 	struct vm_struct *vm;
 	void *objp = (void *)PAGE_ALIGN((unsigned long)object);
 
Quoted 5 lines

mm/vmalloc.c bool vmalloc_dump_obj(void *object) · line 4320

Quoted 5 lines
mm/vmalloc.c +1 -1
-	vm = find_vm_area(objp);
+	vm = try_to_find_vm_area(objp);
 	if (!vm)
 		return false;
 	pr_cont(" %u-page vmalloc region starting at %#lx allocated at %pS\n",

Hi Vlad,
Thanks for taking a look.

Quoted 3 lines

I am not sure if this patch makes a lot of sense. I agree, this is a problem and it mitigates it. But it is broken in terms of once you drop the lock, the VA should not be accessed.

Just to note the lockless-access issue you are referring to is not introduced by this patch but is rather in the existing code. Also just to note this is debug code.

Quoted 1 line

Is that a real issue or it gets triggered due to some syntetic test case?

It is a real issue. See 2/2.

Quoted 2 lines

If i were you, i would go with open-coded version of trylock. Because there is only one user so far.

Taking your open coding and locking suggestions, I came up with the below which actually results in a smaller patch. Does it look good to you?

mm/vmalloc.c +21 -4
index 93cf99aba335..aaf6bad997a7 100644
@@ -4274,14 +4274,31 @@ void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
 #ifdef CONFIG_PRINTK
 bool vmalloc_dump_obj(void *object)
 {
+	void *caller, *objp = (void *)PAGE_ALIGN((unsigned long)object);
 	struct vm_struct *vm;
-	void *objp = (void *)PAGE_ALIGN((unsigned long)object);
+	struct vmap_area *va;
+	unsigned long addr;
+	unsigned int nr_pages;
 
-	vm = find_vm_area(objp);
-	if (!vm)
+	if (!spin_trylock(&vmap_area_lock))
+		return false;
+	va = __find_vmap_area((unsigned long)addr, &vmap_area_root);
+	if (!va) {
+		spin_unlock(&vmap_area_lock);
 		return false;
+	}
+
+	vm = va->vm;
+	if (!vm) {
+		spin_unlock(&vmap_area_lock);
+		return false;
+	}
+	addr = vm->addr;
+	caller = vm->caller;
+	nr_pages = vm->nr_pages;
+	spin_unlock(&vmap_area_lock);
 	pr_cont(" %u-page vmalloc region starting at %#lx allocated at %pS\n",
-		vm->nr_pages, (unsigned long)vm->addr, vm->caller);
+		nr_pages, addr, caller);
 	return true;
 }
 #endif
#4 lore raw

mm/vmalloc.c

Quoted 108 lines · Joel Fernandes
Quoted 78 lines · Uladzislau Rezki
Quoted 77 lines · Joel Fernandes (Google)

It is unsafe to dump vmalloc area information when trying to do so from some contexts. Add a safer trylock version of the same function to do a best-effort VMA finding and use it from vmalloc_dump_obj().

[apply test robot feedback on unused function fix.]

---
v1->v2: Apply review tags and test robot feedback.

 mm/vmalloc.c | 39 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)
mm/vmalloc.c +37 -0
index 93cf99aba335..f09e882ae3b8 100644
@@ -1865,6 +1865,20 @@ struct vmap_area *find_vmap_area(unsigned long addr)
 	return va;
 }
 
+#ifdef CONFIG_PRINTK
+static struct vmap_area *find_vmap_area_trylock(unsigned long addr)
+{
+	struct vmap_area *va;
+
+	if (!spin_trylock(&vmap_area_lock))
+		return NULL;
+	va = __find_vmap_area(addr, &vmap_area_root);
+	spin_unlock(&vmap_area_lock);
+
+	return va;
+}
+#endif
+
 static struct vmap_area *find_unlink_vmap_area(unsigned long addr)
 {
 	struct vmap_area *va;
@@ -2671,6 +2685,29 @@ struct vm_struct *find_vm_area(const void *addr)
 	return va->vm;
 }
 
+/**
+ * try_to_find_vm_area - find a continuous kernel virtual area
+ * @addr:	  base address
+ *
+ * This function is the same as find_vm_area() except that it is
+ * safe to call if vmap_area_lock is already held and returns NULL
+ * if it is. See comments in find_vmap_area() for other details.
+ *
+ * Return: the area descriptor on success or %NULL on failure.
+ */
+#ifdef CONFIG_PRINTK
+static struct vm_struct *try_to_find_vm_area(const void *addr)
+{
+	struct vmap_area *va;
+
+	va = find_vmap_area_trylock((unsigned long)addr);
+	if (!va)
+		return NULL;
+
+	return va->vm;
+}
+#endif
+
 /**
  * remove_vm_area - find and remove a continuous kernel virtual area
  * @addr:	    base address
@@ -4277,7 +4314,7 @@ bool vmalloc_dump_obj(void *object)
 	struct vm_struct *vm;
 	void *objp = (void *)PAGE_ALIGN((unsigned long)object);
 
Quoted 5 lines

mm/vmalloc.c bool vmalloc_dump_obj(void *object) · line 4320

Quoted 5 lines
mm/vmalloc.c +1 -1
-	vm = find_vm_area(objp);
+	vm = try_to_find_vm_area(objp);
 	if (!vm)
 		return false;
 	pr_cont(" %u-page vmalloc region starting at %#lx allocated at %pS\n",

Hi Vlad,
Thanks for taking a look.

Quoted 3 lines

I am not sure if this patch makes a lot of sense. I agree, this is a problem and it mitigates it. But it is broken in terms of once you drop the lock, the VA should not be accessed.

Just to note the lockless-access issue you are referring to is not introduced by this patch but is rather in the existing code. Also just to note this is debug code.

Quoted 1 line

Is that a real issue or it gets triggered due to some syntetic test case?

It is a real issue. See 2/2.

Quoted 2 lines

If i were you, i would go with open-coded version of trylock. Because there is only one user so far.

Taking your open coding and locking suggestions, I came up with the below which actually results in a smaller patch. Does it look good to you?

mm/vmalloc.c +0 -0
index 93cf99aba335..aaf6bad997a7 100644

And with some trivial compiler errors fixed (sorry should have build tested but wanted to just share the idea earlier):

mm/vmalloc.c +22 -4
index 93cf99aba335..2c6a0e2ff404 100644
@@ -4274,14 +4274,32 @@ void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
 #ifdef CONFIG_PRINTK
 bool vmalloc_dump_obj(void *object)
 {
-	struct vm_struct *vm;
 	void *objp = (void *)PAGE_ALIGN((unsigned long)object);
+	const void *caller;
+	struct vm_struct *vm;
+	struct vmap_area *va;
+	unsigned long addr;
+	unsigned int nr_pages;
 
-	vm = find_vm_area(objp);
-	if (!vm)
+	if (!spin_trylock(&vmap_area_lock))
+		return false;
+	va = __find_vmap_area((unsigned long)objp, &vmap_area_root);
+	if (!va) {
+		spin_unlock(&vmap_area_lock);
 		return false;
+	}
+
+	vm = va->vm;
+	if (!vm) {
+		spin_unlock(&vmap_area_lock);
+		return false;
+	}
+	addr = (unsigned long)vm->addr;
+	caller = vm->caller;
+	nr_pages = vm->nr_pages;
+	spin_unlock(&vmap_area_lock);
 	pr_cont(" %u-page vmalloc region starting at %#lx allocated at %pS\n",
-		vm->nr_pages, (unsigned long)vm->addr, vm->caller);
+		nr_pages, addr, caller);
 	return true;
 }
 #endif
maintainer
#5 lore raw
Quoted 145 lines · Joel Fernandes

mm/vmalloc.c

Quoted 108 lines · Joel Fernandes
Quoted 78 lines · Uladzislau Rezki
Quoted 77 lines · Joel Fernandes (Google)

It is unsafe to dump vmalloc area information when trying to do so from some contexts. Add a safer trylock version of the same function to do a best-effort VMA finding and use it from vmalloc_dump_obj().

[apply test robot feedback on unused function fix.]

---
v1->v2: Apply review tags and test robot feedback.

 mm/vmalloc.c | 39 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)
mm/vmalloc.c +37 -0
index 93cf99aba335..f09e882ae3b8 100644
@@ -1865,6 +1865,20 @@ struct vmap_area *find_vmap_area(unsigned long addr)
 	return va;
 }
 
+#ifdef CONFIG_PRINTK
+static struct vmap_area *find_vmap_area_trylock(unsigned long addr)
+{
+	struct vmap_area *va;
+
+	if (!spin_trylock(&vmap_area_lock))
+		return NULL;
+	va = __find_vmap_area(addr, &vmap_area_root);
+	spin_unlock(&vmap_area_lock);
+
+	return va;
+}
+#endif
+
 static struct vmap_area *find_unlink_vmap_area(unsigned long addr)
 {
 	struct vmap_area *va;
@@ -2671,6 +2685,29 @@ struct vm_struct *find_vm_area(const void *addr)
 	return va->vm;
 }
 
+/**
+ * try_to_find_vm_area - find a continuous kernel virtual area
+ * @addr:	  base address
+ *
+ * This function is the same as find_vm_area() except that it is
+ * safe to call if vmap_area_lock is already held and returns NULL
+ * if it is. See comments in find_vmap_area() for other details.
+ *
+ * Return: the area descriptor on success or %NULL on failure.
+ */
+#ifdef CONFIG_PRINTK
+static struct vm_struct *try_to_find_vm_area(const void *addr)
+{
+	struct vmap_area *va;
+
+	va = find_vmap_area_trylock((unsigned long)addr);
+	if (!va)
+		return NULL;
+
+	return va->vm;
+}
+#endif
+
 /**
  * remove_vm_area - find and remove a continuous kernel virtual area
  * @addr:	    base address
@@ -4277,7 +4314,7 @@ bool vmalloc_dump_obj(void *object)
 	struct vm_struct *vm;
 	void *objp = (void *)PAGE_ALIGN((unsigned long)object);
 
Quoted 5 lines

mm/vmalloc.c void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms) · line 4301

Quoted 5 lines
mm/vmalloc.c +1 -1
-	vm = find_vm_area(objp);
+	vm = try_to_find_vm_area(objp);
 	if (!vm)
 		return false;
 	pr_cont(" %u-page vmalloc region starting at %#lx allocated at %pS\n",

Hi Vlad,
Thanks for taking a look.

Quoted 3 lines

I am not sure if this patch makes a lot of sense. I agree, this is a problem and it mitigates it. But it is broken in terms of once you drop the lock, the VA should not be accessed.

Just to note the lockless-access issue you are referring to is not introduced by this patch but is rather in the existing code. Also just to note this is debug code.

Quoted 1 line

Is that a real issue or it gets triggered due to some syntetic test case?

It is a real issue. See 2/2.

Quoted 2 lines

If i were you, i would go with open-coded version of trylock. Because there is only one user so far.

Taking your open coding and locking suggestions, I came up with the below which actually results in a smaller patch. Does it look good to you?

mm/vmalloc.c +0 -0
index 93cf99aba335..aaf6bad997a7 100644

And with some trivial compiler errors fixed (sorry should have build tested but wanted to just share the idea earlier):

mm/vmalloc.c +17 -3
index 93cf99aba335..2c6a0e2ff404 100644
@@ -4274,14 +4274,32 @@ void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
 #ifdef CONFIG_PRINTK
 bool vmalloc_dump_obj(void *object)
 {
-	struct vm_struct *vm;
 	void *objp = (void *)PAGE_ALIGN((unsigned long)object);
+	const void *caller;
+	struct vm_struct *vm;
+	struct vmap_area *va;
+	unsigned long addr;
+	unsigned int nr_pages;
 
-	vm = find_vm_area(objp);
-	if (!vm)
+	if (!spin_trylock(&vmap_area_lock))
+		return false;
+	va = __find_vmap_area((unsigned long)objp, &vmap_area_root);
+	if (!va) {
+		spin_unlock(&vmap_area_lock);
 		return false;
+	}
+
+	vm = va->vm;
+	if (!vm) {
+		spin_unlock(&vmap_area_lock);
+		return false;
+	}

mm/vmalloc.c void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms) · line 4305

Quoted 10 lines
mm/vmalloc.c +5 -1
+	addr = (unsigned long)vm->addr;
+	caller = vm->caller;
+	nr_pages = vm->nr_pages;
+	spin_unlock(&vmap_area_lock);
 	pr_cont(" %u-page vmalloc region starting at %#lx allocated at %pS\n",
-		vm->nr_pages, (unsigned long)vm->addr, vm->caller);
+		nr_pages, addr, caller);
 	return true;
 }
 #endif

Looks good to me and thank you for fixing a locking issue :) I think you will re-spin and resend it one more time?

--
Uladzislau Rezki

#6 lore raw
Quoted 154 lines · Uladzislau Rezki
Quoted 149 lines · Joel Fernandes

mm/vmalloc.c

Quoted 109 lines · Joel Fernandes
Quoted 78 lines · Uladzislau Rezki
Quoted 77 lines · Joel Fernandes (Google)

It is unsafe to dump vmalloc area information when trying to do so from some contexts. Add a safer trylock version of the same function to do a best-effort VMA finding and use it from vmalloc_dump_obj().

[apply test robot feedback on unused function fix.]

---
v1->v2: Apply review tags and test robot feedback.

mm/vmalloc.c | 39 ++++++++++++++++++++++++++++++++++++++-

1 file changed, 38 insertions(+), 1 deletion(-)
mm/vmalloc.c +37 -0
index 93cf99aba335..f09e882ae3b8 100644
@@ -1865,6 +1865,20 @@ struct vmap_area *find_vmap_area(unsigned long addr)
   return va;
 }
 
+#ifdef CONFIG_PRINTK
+static struct vmap_area *find_vmap_area_trylock(unsigned long addr)
+{
+    struct vmap_area *va;
+
+    if (!spin_trylock(&vmap_area_lock))
+        return NULL;
+    va = __find_vmap_area(addr, &vmap_area_root);
+    spin_unlock(&vmap_area_lock);
+
+    return va;
+}
+#endif
+
 static struct vmap_area *find_unlink_vmap_area(unsigned long addr)
 {
   struct vmap_area *va;
@@ -2671,6 +2685,29 @@ struct vm_struct *find_vm_area(const void *addr)
   return va->vm;
 }
 
+/**
+ * try_to_find_vm_area - find a continuous kernel virtual area
+ * @addr:      base address
+ *
+ * This function is the same as find_vm_area() except that it is
+ * safe to call if vmap_area_lock is already held and returns NULL
+ * if it is. See comments in find_vmap_area() for other details.
+ *
+ * Return: the area descriptor on success or %NULL on failure.
+ */
+#ifdef CONFIG_PRINTK
+static struct vm_struct *try_to_find_vm_area(const void *addr)
+{
+    struct vmap_area *va;
+
+    va = find_vmap_area_trylock((unsigned long)addr);
+    if (!va)
+        return NULL;
+
+    return va->vm;
+}
+#endif
+
 /**
 * remove_vm_area - find and remove a continuous kernel virtual area
 * @addr:        base address
@@ -4277,7 +4314,7 @@ bool vmalloc_dump_obj(void *object)
   struct vm_struct *vm;
   void *objp = (void *)PAGE_ALIGN((unsigned long)object);
 
Quoted 5 lines

mm/vmalloc.c void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms) · line 4301

Quoted 5 lines
mm/vmalloc.c +1 -1
-    vm = find_vm_area(objp);
+    vm = try_to_find_vm_area(objp);
   if (!vm)
       return false;
   pr_cont(" %u-page vmalloc region starting at %#lx allocated at %pS\n",

Hi Vlad,
Thanks for taking a look.

Quoted 3 lines

I am not sure if this patch makes a lot of sense. I agree, this is a problem and it mitigates it. But it is broken in terms of once you drop the lock, the VA should not be accessed.

Just to note the lockless-access issue you are referring to is not introduced by this patch but is rather in the existing code. Also just to note this is debug code.

Quoted 1 line

Is that a real issue or it gets triggered due to some syntetic test case?

It is a real issue. See 2/2.

Quoted 2 lines

If i were you, i would go with open-coded version of trylock. Because there is only one user so far.

Taking your open coding and locking suggestions, I came up with the below which actually results in a smaller patch. Does it look good to you?

mm/vmalloc.c +0 -0
index 93cf99aba335..aaf6bad997a7 100644

And with some trivial compiler errors fixed (sorry should have build tested but wanted to just share the idea earlier):

mm/vmalloc.c +21 -3
index 93cf99aba335..2c6a0e2ff404 100644
@@ -4274,14 +4274,32 @@ void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
 #ifdef CONFIG_PRINTK
 bool vmalloc_dump_obj(void *object)
 {
-    struct vm_struct *vm;
   void *objp = (void *)PAGE_ALIGN((unsigned long)object);
+    const void *caller;
+    struct vm_struct *vm;
+    struct vmap_area *va;
+    unsigned long addr;
+    unsigned int nr_pages;
 
-    vm = find_vm_area(objp);
-    if (!vm)
+    if (!spin_trylock(&vmap_area_lock))
+        return false;
+    va = __find_vmap_area((unsigned long)objp, &vmap_area_root);
+    if (!va) {
+        spin_unlock(&vmap_area_lock);
       return false;
+    }
+
+    vm = va->vm;
+    if (!vm) {
+        spin_unlock(&vmap_area_lock);
+        return false;
+    }
+    addr = (unsigned long)vm->addr;
+    caller = vm->caller;
+    nr_pages = vm->nr_pages;
+    spin_unlock(&vmap_area_lock);
   pr_cont(" %u-page vmalloc region starting at %#lx allocated at %pS\n",
Quoted 8 lines

mm/vmalloc.c void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms) · line 4305

Quoted 6 lines
mm/vmalloc.c +1 -1
-        vm->nr_pages, (unsigned long)vm->addr, vm->caller);
+        nr_pages, addr, caller);
   return true;
 }
 #endif

Looks good to me and thank you for fixing a locking issue :) I think you will re-spin and resend it one more time?

Yes. May I add your Reviewed-by tag to both patches after re-spinning as mentioned above?

thanks!

- Joel

Quoted 3 lines

--
Uladzislau Rezki

maintainer Reviewed-by
#7 lore raw
Quoted 163 lines · Joel Fernandes
Quoted 154 lines · Uladzislau Rezki
Quoted 149 lines · Joel Fernandes

mm/vmalloc.c

Quoted 109 lines · Joel Fernandes
Quoted 78 lines · Uladzislau Rezki
Quoted 77 lines · Joel Fernandes (Google)

It is unsafe to dump vmalloc area information when trying to do so from some contexts. Add a safer trylock version of the same function to do a best-effort VMA finding and use it from vmalloc_dump_obj().

[apply test robot feedback on unused function fix.]

---
v1->v2: Apply review tags and test robot feedback.

mm/vmalloc.c | 39 ++++++++++++++++++++++++++++++++++++++-

1 file changed, 38 insertions(+), 1 deletion(-)
mm/vmalloc.c +37 -0
index 93cf99aba335..f09e882ae3b8 100644
@@ -1865,6 +1865,20 @@ struct vmap_area *find_vmap_area(unsigned long addr)
   return va;
 }
 
+#ifdef CONFIG_PRINTK
+static struct vmap_area *find_vmap_area_trylock(unsigned long addr)
+{
+    struct vmap_area *va;
+
+    if (!spin_trylock(&vmap_area_lock))
+        return NULL;
+    va = __find_vmap_area(addr, &vmap_area_root);
+    spin_unlock(&vmap_area_lock);
+
+    return va;
+}
+#endif
+
 static struct vmap_area *find_unlink_vmap_area(unsigned long addr)
 {
   struct vmap_area *va;
@@ -2671,6 +2685,29 @@ struct vm_struct *find_vm_area(const void *addr)
   return va->vm;
 }
 
+/**
+ * try_to_find_vm_area - find a continuous kernel virtual area
+ * @addr:      base address
+ *
+ * This function is the same as find_vm_area() except that it is
+ * safe to call if vmap_area_lock is already held and returns NULL
+ * if it is. See comments in find_vmap_area() for other details.
+ *
+ * Return: the area descriptor on success or %NULL on failure.
+ */
+#ifdef CONFIG_PRINTK
+static struct vm_struct *try_to_find_vm_area(const void *addr)
+{
+    struct vmap_area *va;
+
+    va = find_vmap_area_trylock((unsigned long)addr);
+    if (!va)
+        return NULL;
+
+    return va->vm;
+}
+#endif
+
 /**
 * remove_vm_area - find and remove a continuous kernel virtual area
 * @addr:        base address
@@ -4277,7 +4314,7 @@ bool vmalloc_dump_obj(void *object)
   struct vm_struct *vm;
   void *objp = (void *)PAGE_ALIGN((unsigned long)object);
 
Quoted 5 lines

mm/vmalloc.c void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms) · line 4301

Quoted 5 lines
mm/vmalloc.c +1 -1
-    vm = find_vm_area(objp);
+    vm = try_to_find_vm_area(objp);
   if (!vm)
       return false;
   pr_cont(" %u-page vmalloc region starting at %#lx allocated at %pS\n",

Hi Vlad,
Thanks for taking a look.

Quoted 3 lines

I am not sure if this patch makes a lot of sense. I agree, this is a problem and it mitigates it. But it is broken in terms of once you drop the lock, the VA should not be accessed.

Just to note the lockless-access issue you are referring to is not introduced by this patch but is rather in the existing code. Also just to note this is debug code.

Quoted 1 line

Is that a real issue or it gets triggered due to some syntetic test case?

It is a real issue. See 2/2.

Quoted 2 lines

If i were you, i would go with open-coded version of trylock. Because there is only one user so far.

Taking your open coding and locking suggestions, I came up with the below which actually results in a smaller patch. Does it look good to you?

mm/vmalloc.c +0 -0
index 93cf99aba335..aaf6bad997a7 100644

And with some trivial compiler errors fixed (sorry should have build tested but wanted to just share the idea earlier):

mm/vmalloc.c +21 -3
index 93cf99aba335..2c6a0e2ff404 100644
@@ -4274,14 +4274,32 @@ void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
 #ifdef CONFIG_PRINTK
 bool vmalloc_dump_obj(void *object)
 {
-    struct vm_struct *vm;
   void *objp = (void *)PAGE_ALIGN((unsigned long)object);
+    const void *caller;
+    struct vm_struct *vm;
+    struct vmap_area *va;
+    unsigned long addr;
+    unsigned int nr_pages;
 
-    vm = find_vm_area(objp);
-    if (!vm)
+    if (!spin_trylock(&vmap_area_lock))
+        return false;
+    va = __find_vmap_area((unsigned long)objp, &vmap_area_root);
+    if (!va) {
+        spin_unlock(&vmap_area_lock);
       return false;
+    }
+
+    vm = va->vm;
+    if (!vm) {
+        spin_unlock(&vmap_area_lock);
+        return false;
+    }
+    addr = (unsigned long)vm->addr;
+    caller = vm->caller;
+    nr_pages = vm->nr_pages;
+    spin_unlock(&vmap_area_lock);
   pr_cont(" %u-page vmalloc region starting at %#lx allocated at %pS\n",
Quoted 8 lines

mm/vmalloc.c void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms) · line 4305

Quoted 6 lines
mm/vmalloc.c +1 -1
-        vm->nr_pages, (unsigned long)vm->addr, vm->caller);
+        nr_pages, addr, caller);
   return true;
 }
 #endif

Looks good to me and thank you for fixing a locking issue :) I think you will re-spin and resend it one more time?

Quoted 2 lines

Yes. May I add your Reviewed-by tag to both patches after re-spinning as mentioned above?

--
Uladzislau Rezki