Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Chuanhua Han" <hanchuanhua802@gmail.com>,
	"Suren Baghdasaryan" <surenb@google.com>,
	"Lorenzo Stoakes" <ljs@kernel.org>,
	"Liam R. Howlett" <liam@infradead.org>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Yonghong Song" <yonghong.song@linux.dev>,
	"Alexei Starovoitov" <ast@kernel.org>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"Andrii Nakryiko" <andrii@kernel.org>,
	"Eduard Zingerman" <eddyz87@gmail.com>,
	"Kumar Kartikeya Dwivedi" <memxor@gmail.com>
Cc: "Chuanhua Han" <hanchuanhua@xiaomi.com>,
	"Vlastimil Babka" <vbabka@kernel.org>,
	"Shakeel Butt" <shakeel.butt@linux.dev>,
	"Pedro Falcato" <pfalcato@suse.de>,
	"David Hildenbrand" <david@kernel.org>,
	"Jann Horn" <jannh@google.com>,
	"Martin KaFai Lau" <martin.lau@linux.dev>,
	"Song Liu" <song@kernel.org>, "Jiri Olsa" <jolsa@kernel.org>,
	"Emil Tsalapatis" <emil@etsalapatis.com>,
	"Ihor Solodrai" <ihor.solodrai@linux.dev>,
	"Barry Song" <baohua@kernel.org>,
	linux-mm@kvack.org, bpf@vger.kernel.org
Subject: Re: [RFC PATCH 2/2] bpf: use try_lock_next_vma() in the task_vma iterator
Date: Sat, 05 Dec 2026 04:11:19 +0000	[thread overview]
Message-ID: <sashiko-outbox-251056@kernel.org> (raw)
In-Reply-To: <20261205033951.1811578-3-hanchuanhua802@gmail.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] bpf: try_lock_next_vma() runs synchronous __mmdrop() inside RCU
  read lock
- [Low] bpf: 32-bit truncation of next_addr in
  bpf_iter_task_vma_find_next()
--

commit 775b83d9ac0b9c4a11b6a270f1f445469d3c8aa0
Author: Chuanhua Han <hanchuanhua@xiaomi.com>

bpf: use try_lock_next_vma() in the task_vma iterator

This commit replaces the open-coded RCU maple tree walk and
lock_vma_under_rcu() retry loop in bpf_iter_task_vma_find_next() with a
call to try_lock_next_vma().

> diff --git a/kernel/bpf/task_iter.c b/kernel/bpf/task_iter.c
> index 2ee7b3b3c10be..a12c125eef9d1 100644
> --- a/kernel/bpf/task_iter.c
> +++ b/kernel/bpf/task_iter.c
> @@ -941,53 +941,17 @@ __bpf_kfunc int bpf_iter_task_vma_new(struct bpf_iter_task_vma *it,

[ ... ]

> +/* Find and lock the next VMA at or after data->next_addr. */
>  static struct vm_area_struct *
>  bpf_iter_task_vma_find_next(struct bpf_iter_task_vma_kern_data *data)
>  {
> +	VMA_ITERATOR(vmi, data->mm, data->next_addr);
>  	struct vm_area_struct *vma;
> -	struct vma_iterator vmi;
> -	unsigned long start, end;
>  
> -retry:
>  	rcu_read_lock();
> -	vma_iter_init(&vmi, data->mm, data->next_addr);
> -	vma = vma_next(&vmi);
> -	if (!vma) {
> -		rcu_read_unlock();
> -		return NULL;
> -	}
> -	start = vma->vm_start;
> -	end = vma->vm_end;
> +	vma = try_lock_next_vma(data->mm, &vmi, data->next_addr, ULONG_MAX);
>  	rcu_read_unlock();

[Severity: High]
Can calling try_lock_next_vma() under rcu_read_lock() run synchronous
__mmdrop() inside an RCU read-side critical section?

Commit "mm/mmap_lock: add try_lock_next_vma()" takes a nested
rcu_read_lock() around vma_start_read(), assuming commit 44926897b6c2
("mm/vma: drop mm asynchronously in vma_start_read()") is present.  In
this tree, vma_start_read() still calls synchronous mmdrop():

mm/mmap_lock.c:vma_start_read() {
	...
	/* __mmdrop() is a heavy operation, do it after dropping RCU lock. */
	rcu_read_unlock();
	mmgrab(other_mm);
	vma_refcount_put(vma);
	mmdrop(other_mm);
	...
}

When a SLAB_TYPESAFE_BY_RCU VMA is reused by another mm and that mm drops
its last mm_count reference here, vma_start_read()'s single
rcu_read_unlock() leaves the outer rcu_read_lock() held across __mmdrop().
Because bpf_iter_task_vma_new() only rejects irqs_disabled(), a softirq
BPF program on x86 can deadlock on pgd_lock:

pgd_alloc()
  spin_lock(&pgd_lock)
  <softirq>
    bpf_iter_task_vma_find_next()
      rcu_read_lock()
      try_lock_next_vma() -> vma_start_read()
        mmdrop() -> __mmdrop() -> pgd_dtor()
          spin_lock(&pgd_lock)  <-- deadlock

Under CONFIG_MMU_LAZY_TLB_SHOOTDOWN, __mmdrop() -> cleanup_lazy_tlbs() ->
on_each_cpu_mask(..., wait=1) also hits WARN_ON_ONCE(!in_task()) in
smp_call_function_many_cond().  Applying commit 44926897b6c2 ("mm/vma:
drop mm asynchronously in vma_start_read()") as a prerequisite would keep
__mmdrop() out of this path.

[ ... ]

> -	if (unlikely(vma->vm_end <= data->next_addr)) {
> -		data->next_addr += PAGE_SIZE;
> -		vma_end_read(vma);
> -		goto retry;
> -	}
> -
>  	return vma;
>  }

[Severity: Low]
Does removing this check introduce a 32-bit wrap-around regression when
data->next_addr exceeds ULONG_MAX?

bpf_iter_task_vma_new() stores the caller-supplied u64 addr directly into
data->next_addr, which is implicitly truncated to unsigned long in
VMA_ITERATOR() and try_lock_next_vma().  On 32-bit kernels, passing an
addr >= (1ULL << 32) now wraps to a low address and returns VMAs below
addr instead of NULL, whereas the removed 64-bit vma->vm_end <=
data->next_addr check previously prevented returning them.

Guarding data->next_addr >= ULONG_MAX (or rejecting out-of-range addr in
bpf_iter_task_vma_new()) would avoid the truncation.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205033951.1811578-1-hanchuanhua802@gmail.com?part=2


      reply	other threads:[~2026-12-05  4:11 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05  3:39 [RFC PATCH 0/2] mm: add try_lock_next_vma() and use it in the BPF task_vma iterator Chuanhua Han
2026-12-05  3:39 ` [RFC PATCH 1/2] mm/mmap_lock: add try_lock_next_vma() Chuanhua Han
2026-12-05  3:56   ` sashiko-bot
2026-12-05  3:39 ` [RFC PATCH 2/2] bpf: use try_lock_next_vma() in the task_vma iterator Chuanhua Han
2026-12-05  4:11   ` sashiko-bot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-251056@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=baohua@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=david@kernel.org \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=hanchuanhua802@gmail.com \
    --cc=hanchuanhua@xiaomi.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=jannh@google.com \
    --cc=jolsa@kernel.org \
    --cc=liam@infradead.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=pfalcato@suse.de \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=shakeel.butt@linux.dev \
    --cc=song@kernel.org \
    --cc=surenb@google.com \
    --cc=vbabka@kernel.org \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox