Skip to contentSkip to conversation

mm/maps: move kmalloc() call location in do_procmap_query() out of RCU critical section

PATCH In review Jeongjun Park 3 participants 4 messages Jul 2, 2025 – Jul 4, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/maps: move kmalloc() call location in do_procmap_query() out of RCU critical section

In do_procmap_query(), we are allocating name_buf as much as name_buf_sz with kmalloc().

However, due to the previous commit eff061546ca5 ("mm/maps: execute PROCMAP_QUERY ioctl under per-vma locks"), the location of kmalloc() is located inside the RCU critical section.

This causes might_sleep_if() to be called inside the RCU critical section, so we need to move the call location of kmalloc() outside the RCU critical section to prevent this.

Reported-by [email protected]
Fixes eff061546ca5 ("mm/maps: execute PROCMAP_QUERY ioctl under per-vma locks")
---
 fs/proc/task_mmu.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
fs/proc/task_mmu.c +11 -11
index f3659046efb7..42b0224c6ac9 100644
@@ -595,6 +595,7 @@ static int do_procmap_query(struct proc_maps_private *priv, void __user *uarg)
 	char build_id_buf[BUILD_ID_SIZE_MAX], *name_buf = NULL;
 	__u64 usize;
 	int err;
+	size_t name_buf_sz;
 
 	if (copy_from_user(&usize, (void __user *)uarg, sizeof(usize)))
 		return -EFAULT;
@@ -621,12 +622,18 @@ static int do_procmap_query(struct proc_maps_private *priv, void __user *uarg)
 	if (!mm || !mmget_not_zero(mm))
 		return -ESRCH;
 
-	err = query_vma_setup(priv);
-	if (err) {
+	name_buf_sz = min_t(size_t, PATH_MAX, karg.vma_name_size);
+
+	name_buf = kmalloc(name_buf_sz, GFP_KERNEL);
+	if (!name_buf) {
 		mmput(mm);
-		return err;
+		return -ENOMEM;
 	}
 
+	err = query_vma_setup(priv);
+	if (err)
+		goto fail_vma_setup;
+
 	vma = query_matching_vma(priv, karg.query_addr, karg.query_flags);
 	if (IS_ERR(vma)) {
 		err = PTR_ERR(vma);
@@ -679,20 +686,12 @@ static int do_procmap_query(struct proc_maps_private *priv, void __user *uarg)
 	}
 
 	if (karg.vma_name_size) {
-		size_t name_buf_sz = min_t(size_t, PATH_MAX, karg.vma_name_size);
 		const struct path *path;
 		const char *name_fmt;
 		size_t name_sz = 0;
 
 		get_vma_name(vma, &path, &name, &name_fmt);
 
-		if (path || name_fmt || name) {
-			name_buf = kmalloc(name_buf_sz, GFP_KERNEL);
-			if (!name_buf) {
-				err = -ENOMEM;
-				goto out;
-			}
-		}
 		if (path) {
 			name = d_path(path, name_buf, name_buf_sz);
 			if (IS_ERR(name)) {
@@ -733,6 +732,7 @@ static int do_procmap_query(struct proc_maps_private *priv, void __user *uarg)
 
 out:
 	query_vma_teardown(priv);
+fail_vma_setup:
 	mmput(mm);
 	kfree(name_buf);
 	return err;

--

#1 lore raw
Quoted 7 lines · Jeongjun Park

In do_procmap_query(), we are allocating name_buf as much as name_buf_sz with kmalloc().

However, due to the previous commit eff061546ca5 ("mm/maps: execute PROCMAP_QUERY ioctl under per-vma locks"), the location of kmalloc() is located inside the RCU critical section.

Quoted 7 lines

This causes might_sleep_if() to be called inside the RCU critical section, so we need to move the call location of kmalloc() outside the RCU critical section to prevent this.

Reported-by [email protected]
Fixes eff061546ca5 ("mm/maps: execute PROCMAP_QUERY ioctl under per-vma locks")

That commit is not upstream yet (and the commit id is not stable), so it should be squashed into the problematic commit.

As a side note: the patch subject of this and the original patch should start with "fs/proc/task_mmu", not "mm/maps".

Signature · 3 lines
Cheers,

David / dhildenb
#2 lore raw
Quoted 17 lines · David Hildenbrand
Quoted 7 lines · Jeongjun Park

In do_procmap_query(), we are allocating name_buf as much as name_buf_sz with kmalloc().

However, due to the previous commit eff061546ca5 ("mm/maps: execute PROCMAP_QUERY ioctl under per-vma locks"), the location of kmalloc() is located inside the RCU critical section.

Quoted 7 lines

This causes might_sleep_if() to be called inside the RCU critical section, so we need to move the call location of kmalloc() outside the RCU critical section to prevent this.

Reported-by [email protected]
Fixes eff061546ca5 ("mm/maps: execute PROCMAP_QUERY ioctl under per-vma locks")
Quoted 5 lines

That commit is not upstream yet (and the commit id is not stable), so it should be squashed into the problematic commit.

As a side note: the patch subject of this and the original patch should start with "fs/proc/task_mmu", not "mm/maps".

Thanks for the fix Jeongjun and thanks for the note David. I'm preparing the next version of my patchset and there is a much simpler fix for this issue which I'll implement there. Planning to post it tomorrow.
Thanks,
Suren.

Quoted 6 lines

--
Cheers,

David / dhildenb

#3 lore raw
Quoted 22 lines · Suren Baghdasaryan
Quoted 20 lines · David Hildenbrand
Quoted 7 lines · Jeongjun Park

In do_procmap_query(), we are allocating name_buf as much as name_buf_sz with kmalloc().

However, due to the previous commit eff061546ca5 ("mm/maps: execute PROCMAP_QUERY ioctl under per-vma locks"), the location of kmalloc() is located inside the RCU critical section.

Quoted 7 lines

This causes might_sleep_if() to be called inside the RCU critical section, so we need to move the call location of kmalloc() outside the RCU critical section to prevent this.

Reported-by [email protected]
Fixes eff061546ca5 ("mm/maps: execute PROCMAP_QUERY ioctl under per-vma locks")

That commit is not upstream yet (and the commit id is not stable), so it should be squashed into the problematic commit.

Quoted 7 lines
Quoted 2 lines

As a side note: the patch subject of this and the original patch should start with "fs/proc/task_mmu", not "mm/maps".

Thanks for the fix Jeongjun and thanks for the note David. I'm preparing the next version of my patchset and there is a much simpler fix for this issue which I'll implement there. Planning to post it tomorrow.

v6 of my patchset is posted at
https://lore.kernel.org/all/[email protected]/
I reworked the last patch to address this issue by narrowing down the rcu read section to query_vma_find_by_addr() only. That should fix the original issue.

Quoted 9 lines

Thanks,
Suren.

Quoted 6 lines

--
Cheers,

David / dhildenb