Skip to contentSkip to conversation

bpf: arena: fix mmap_lock deadlock on arena lock failure

PATCH In review Jiayuan Chen 4 participants 8 messages Jul 28 – Aug 3 BPF

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: arena: fix mmap_lock deadlock on arena lock failure

Reported by the Sashiko AI review.

arena_vm_fault() returns VM_FAULT_RETRY when it can't take arena->spinlock, but it never took mmap_lock. The fault path assumes a VM_FAULT_RETRY handler already dropped mmap_lock and re-takes it on the retry, so mmap_lock gets taken twice and can deadlock:

	do_user_addr_fault()
	{
		fault = handle_mm_fault(...);   // calls arena_vm_fault()
		if (fault & VM_FAULT_RETRY)
			goto retry;   // re-locks mmap_lock
		mmap_read_unlock(mm);
	}

Return VM_FAULT_SIGBUS instead, for two reasons:

1. We could keep VM_FAULT_RETRY, but then we'd have to drop the fault lock first and cap the retry ourselves, the way __folio_lock_or_retry() does.

2. A failed raw_res_spin_lock_irqsave() already means a possible deadlock was detected, so retrying just hits the same lock again.

So returning VM_FAULT_RETRY here is overkill.

Fixes b8467290edab ("bpf: arena: make arena kfuncs any context safe")

---
target to bpf-next since I think it is moderate.

---
 kernel/bpf/arena.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
kernel/bpf/arena.c +6 -2
index 34f023a537fe..555ee2531ef9 100644
@@ -490,8 +490,12 @@ static vm_fault_t arena_vm_fault(struct vm_fault *vmf)
 	kaddr = kbase + (u32)(vmf->address);
 
 	if (raw_res_spin_lock_irqsave(&arena->spinlock, flags))
-		/* Make a reasonable effort to address impossible case */
-		return VM_FAULT_RETRY;
+		/*
+		 * A failed lock means a possible deadlock was detected. Don't
+		 * return VM_FAULT_RETRY: this handler never took mmap_lock, but
+		 * the fault path would re-take it on retry and deadlock. Fail.
+		 */
+		return VM_FAULT_SIGBUS;
 
 	page = vmalloc_to_page((void *)kaddr);
 	if (page) {
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] BPF arena page faults can spuriously crash the user process with SIGSEGV due to transient lockless allocation failures. --

--- Patch [1]: [PATCH bpf-next] bpf: arena: fix mmap_lock deadlock on arena lock failure --- commit c57623f4077e743e8c901306fcec7c67d2aafc17
Author: Jiayuan Chen <[email protected]>

bpf: arena: fix mmap_lock deadlock on arena lock failure

This commit fixes a potential deadlock in the arena page fault handler by returning VM_FAULT_SIGBUS instead of VM_FAULT_RETRY when the arena spinlock cannot be acquired. Returning retry would cause the fault path to attempt retaking the mmap_lock, which was never dropped.

Quoted 5 lines
kernel/bpf/arena.c +0 -0
@@ -490,8 +490,12 @@ static vm_fault_t arena_vm_fault(struct vm_fault *vmf)
 	kaddr = kbase + (u32)(vmf->address);

kernel/bpf/arena.c static vm_fault_t arena_vm_fault(struct vm_fault *vmf) · line 498

Quoted 10 lines
kernel/bpf/arena.c +6 -2
 
 	if (raw_res_spin_lock_irqsave(&arena->spinlock, flags))
-		/* Make a reasonable effort to address impossible case */
-		return VM_FAULT_RETRY;
+		/*
+		 * A failed lock means a possible deadlock was detected. Don't
+		 * return VM_FAULT_RETRY: this handler never took mmap_lock, but
+		 * the fault path would re-take it on retry and deadlock. Fail.
+		 */
+		return VM_FAULT_SIGBUS;

[Severity: High]
This is a pre-existing issue, but looking further down in arena_vm_fault(), does a transient allocation failure unexpectedly cause a SIGSEGV?

Since the raw spinlock is held, disabling IRQs, bpf_map_alloc_pages() falls back to an opportunistic lockless allocation which can fail under memory pressure:

	ret = bpf_map_alloc_pages(map, NUMA_NO_NODE, 1, &page);
	if (ret) {
		range_tree_set(&arena->rt, vmf->pgoff, 1);
		goto out_sigsegv_memcg;
	}

When this lockless allocation fails, the error path jumps to out_sigsegv_memcg and returns VM_FAULT_SIGSEGV instead of VM_FAULT_OOM or safely dropping the lock to retry.

Could this cause user processes mapping a BPF arena to experience spurious crashes during memory contention?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
Quoted 46 lines · On 7/28/26 2:23 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] BPF arena page faults can spuriously crash the user process with SIGSEGV due to transient lockless allocation failures. --

--- Patch [1]: [PATCH bpf-next] bpf: arena: fix mmap_lock deadlock on arena lock failure --- commit c57623f4077e743e8c901306fcec7c67d2aafc17
Author: Jiayuan Chen <[email protected]>

bpf: arena: fix mmap_lock deadlock on arena lock failure

This commit fixes a potential deadlock in the arena page fault handler by returning VM_FAULT_SIGBUS instead of VM_FAULT_RETRY when the arena spinlock cannot be acquired. Returning retry would cause the fault path to attempt retaking the mmap_lock, which was never dropped.

Quoted 5 lines
kernel/bpf/arena.c +0 -0
@@ -490,8 +490,12 @@ static vm_fault_t arena_vm_fault(struct vm_fault *vmf)
  	kaddr = kbase + (u32)(vmf->address);

kernel/bpf/arena.c static vm_fault_t arena_vm_fault(struct vm_fault *vmf) · line 498

Quoted 10 lines
kernel/bpf/arena.c +6 -2
 
  	if (raw_res_spin_lock_irqsave(&arena->spinlock, flags))
-		/* Make a reasonable effort to address impossible case */
-		return VM_FAULT_RETRY;
+		/*
+		 * A failed lock means a possible deadlock was detected. Don't
+		 * return VM_FAULT_RETRY: this handler never took mmap_lock, but
+		 * the fault path would re-take it on retry and deadlock. Fail.
+		 */
+		return VM_FAULT_SIGBUS;

[Severity: High]
This is a pre-existing issue, but looking further down in arena_vm_fault(), does a transient allocation failure unexpectedly cause a SIGSEGV?

Since the raw spinlock is held, disabling IRQs, bpf_map_alloc_pages() falls back to an opportunistic lockless allocation which can fail under memory pressure:

	ret = bpf_map_alloc_pages(map, NUMA_NO_NODE, 1, &page);
	if (ret) {
		range_tree_set(&arena->rt, vmf->pgoff, 1);
		goto out_sigsegv_memcg;
	}
Quoted 6 lines

When this lockless allocation fails, the error path jumps to out_sigsegv_memcg and returns VM_FAULT_SIGSEGV instead of VM_FAULT_OOM or safely dropping the lock to retry.

Could this cause user processes mapping a BPF arena to experience spurious crashes during memory contention?

Yes I'm on it https://lore.kernel.org/bpf/[email protected]/

And current patch is from your review result of that patch...

reviewer Reviewed-by on v1 1/1
#3 lore raw
In reply to Jiayuan Chen
Quoted 21 lines · Jiayuan Chen

Reported by the Sashiko AI review.

arena_vm_fault() returns VM_FAULT_RETRY when it can't take arena->spinlock, but it never took mmap_lock. The fault path assumes a VM_FAULT_RETRY handler already dropped mmap_lock and re-takes it on the retry, so mmap_lock gets taken twice and can deadlock:

	do_user_addr_fault()
	{
		fault = handle_mm_fault(...);   // calls arena_vm_fault()
		if (fault & VM_FAULT_RETRY)
			goto retry;   // re-locks mmap_lock
		mmap_read_unlock(mm);
	}

Return VM_FAULT_SIGBUS instead, for two reasons:

1. We could keep VM_FAULT_RETRY, but then we'd have to drop the fault lock first and cap the retry ourselves, the way __folio_lock_or_retry() does.

Quoted 7 lines

2. A failed raw_res_spin_lock_irqsave() already means a possible deadlock was detected, so retrying just hits the same lock again.

So returning VM_FAULT_RETRY here is overkill.

Fixes b8467290edab ("bpf: arena: make arena kfuncs any context safe")
Quoted 3 lines

---
target to bpf-next since I think it is moderate.

Agreed, I think either branch is fine.

Quoted 23 lines
---
 kernel/bpf/arena.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
kernel/bpf/arena.c +6 -2
index 34f023a537fe..555ee2531ef9 100644
@@ -490,8 +490,12 @@ static vm_fault_t arena_vm_fault(struct vm_fault *vmf)
 	kaddr = kbase + (u32)(vmf->address);
 
 	if (raw_res_spin_lock_irqsave(&arena->spinlock, flags))
-		/* Make a reasonable effort to address impossible case */
-		return VM_FAULT_RETRY;
+		/*
+		 * A failed lock means a possible deadlock was detected. Don't
+		 * return VM_FAULT_RETRY: this handler never took mmap_lock, but
+		 * the fault path would re-take it on retry and deadlock. Fail.
+		 */
+		return VM_FAULT_SIGBUS;
 
 	page = vmalloc_to_page((void *)kaddr);
 	if (page) {
reviewer Reviewed-by on v1 1/1
#4 lore raw
In reply to Jiayuan Chen
Quoted 21 lines · Jiayuan Chen

Reported by the Sashiko AI review.

arena_vm_fault() returns VM_FAULT_RETRY when it can't take arena->spinlock, but it never took mmap_lock. The fault path assumes a VM_FAULT_RETRY handler already dropped mmap_lock and re-takes it on the retry, so mmap_lock gets taken twice and can deadlock:

	do_user_addr_fault()
	{
		fault = handle_mm_fault(...);   // calls arena_vm_fault()
		if (fault & VM_FAULT_RETRY)
			goto retry;   // re-locks mmap_lock
		mmap_read_unlock(mm);
	}

Return VM_FAULT_SIGBUS instead, for two reasons:

1. We could keep VM_FAULT_RETRY, but then we'd have to drop the fault lock first and cap the retry ourselves, the way __folio_lock_or_retry() does.

Quoted 7 lines

2. A failed raw_res_spin_lock_irqsave() already means a possible deadlock was detected, so retrying just hits the same lock again.

So returning VM_FAULT_RETRY here is overkill.

Fixes b8467290edab ("bpf: arena: make arena kfuncs any context safe")
Quoted 3 lines

---
target to bpf-next since I think it is moderate.

Agreed, I think either branch is fine.

Quoted 23 lines
---
 kernel/bpf/arena.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
kernel/bpf/arena.c +6 -2
index 34f023a537fe..555ee2531ef9 100644
@@ -490,8 +490,12 @@ static vm_fault_t arena_vm_fault(struct vm_fault *vmf)
 	kaddr = kbase + (u32)(vmf->address);
 
 	if (raw_res_spin_lock_irqsave(&arena->spinlock, flags))
-		/* Make a reasonable effort to address impossible case */
-		return VM_FAULT_RETRY;
+		/*
+		 * A failed lock means a possible deadlock was detected. Don't
+		 * return VM_FAULT_RETRY: this handler never took mmap_lock, but
+		 * the fault path would re-take it on retry and deadlock. Fail.
+		 */
+		return VM_FAULT_SIGBUS;
 
 	page = vmalloc_to_page((void *)kaddr);
 	if (page) {
#5 lore raw
In reply to Jiayuan Chen
Quoted 24 lines · Jiayuan Chen

Reported by the Sashiko AI review.

arena_vm_fault() returns VM_FAULT_RETRY when it can't take arena->spinlock, but it never took mmap_lock. The fault path assumes a VM_FAULT_RETRY handler already dropped mmap_lock and re-takes it on the retry, so mmap_lock gets taken twice and can deadlock:

	do_user_addr_fault()
	{
		fault = handle_mm_fault(...);   // calls arena_vm_fault()
		if (fault & VM_FAULT_RETRY)
			goto retry;   // re-locks mmap_lock
		mmap_read_unlock(mm);
	}

Return VM_FAULT_SIGBUS instead, for two reasons:

1. We could keep VM_FAULT_RETRY, but then we'd have to drop the fault lock first and cap the retry ourselves, the way __folio_lock_or_retry() does.

2. A failed raw_res_spin_lock_irqsave() already means a possible deadlock was detected, so retrying just hits the same lock again.

Quoted 8 lines

So returning VM_FAULT_RETRY here is overkill.

Fixes b8467290edab ("bpf: arena: make arena kfuncs any context safe")

---
target to bpf-next since I think it is moderate.
---

This looks ok to me, but after staring at this function for longer, I think all kinds of non-recoverable errors should be using VM_FAULT_SIGBUS vs SIGSEGV.

The only case with SIGSEGV should be the flag based request to disable allocation on lazy faults. That should include the scratch page case, since it represents a hole.

SIGSEGV: BPF_F_SEGV_ON_FAULT, scratch page.
SIGBUS: lock acquisition failure, range-tree failures, page allocation failure, kernel PTE install failure.

All of these SIGBUS are almost improbable, but I think it would be cleaner to keep semantics clear.

Could you follow up with this change? I applied this one for now.

Quoted 22 lines
 kernel/bpf/arena.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
kernel/bpf/arena.c +6 -2
index 34f023a537fe..555ee2531ef9 100644
@@ -490,8 +490,12 @@ static vm_fault_t arena_vm_fault(struct vm_fault *vmf)
 	kaddr = kbase + (u32)(vmf->address);
 
 	if (raw_res_spin_lock_irqsave(&arena->spinlock, flags))
-		/* Make a reasonable effort to address impossible case */
-		return VM_FAULT_RETRY;
+		/*
+		 * A failed lock means a possible deadlock was detected. Don't
+		 * return VM_FAULT_RETRY: this handler never took mmap_lock, but
+		 * the fault path would re-take it on retry and deadlock. Fail.
+		 */
+		return VM_FAULT_SIGBUS;
 
 	page = vmalloc_to_page((void *)kaddr);
 	if (page) {
#6 lore raw
Quoted 41 lines · Kumar Kartikeya Dwivedi
Quoted 24 lines · Jiayuan Chen

Reported by the Sashiko AI review.

arena_vm_fault() returns VM_FAULT_RETRY when it can't take arena->spinlock, but it never took mmap_lock. The fault path assumes a VM_FAULT_RETRY handler already dropped mmap_lock and re-takes it on the retry, so mmap_lock gets taken twice and can deadlock:

	do_user_addr_fault()
	{
		fault = handle_mm_fault(...);   // calls arena_vm_fault()
		if (fault & VM_FAULT_RETRY)
			goto retry;   // re-locks mmap_lock
		mmap_read_unlock(mm);
	}

Return VM_FAULT_SIGBUS instead, for two reasons:

1. We could keep VM_FAULT_RETRY, but then we'd have to drop the fault lock first and cap the retry ourselves, the way __folio_lock_or_retry() does.

2. A failed raw_res_spin_lock_irqsave() already means a possible deadlock was detected, so retrying just hits the same lock again.

Quoted 8 lines

So returning VM_FAULT_RETRY here is overkill.

Fixes b8467290edab ("bpf: arena: make arena kfuncs any context safe")

---
target to bpf-next since I think it is moderate.
---

This looks ok to me, but after staring at this function for longer, I think all kinds of non-recoverable errors should be using VM_FAULT_SIGBUS vs SIGSEGV.

The only case with SIGSEGV should be the flag based request to disable allocation on lazy faults. That should include the scratch page case, since it represents a hole.

Quoted 9 lines

SIGSEGV: BPF_F_SEGV_ON_FAULT, scratch page.
SIGBUS: lock acquisition failure, range-tree failures, page allocation failure, kernel PTE install failure.

All of these SIGBUS are almost improbable, but I think it would be cleaner to keep semantics clear.

Could you follow up with this change? I applied this one for now.

For the scratch page case, I think we do SIGSEGV when the BPF_F_SEGV_ON_FAULT flag is set, but SIGBUS otherwise there too, since we could but choose not to lazy allocate a page due to the program's error / bug.

#7 lore raw
In reply to Kumar Kartikeya Dwivedi
Quoted 40 lines · Kumar Kartikeya Dwivedi
Quoted 24 lines · Jiayuan Chen

Reported by the Sashiko AI review.

arena_vm_fault() returns VM_FAULT_RETRY when it can't take arena->spinlock, but it never took mmap_lock. The fault path assumes a VM_FAULT_RETRY handler already dropped mmap_lock and re-takes it on the retry, so mmap_lock gets taken twice and can deadlock:

	do_user_addr_fault()
	{
		fault = handle_mm_fault(...);   // calls arena_vm_fault()
		if (fault & VM_FAULT_RETRY)
			goto retry;   // re-locks mmap_lock
		mmap_read_unlock(mm);
	}

Return VM_FAULT_SIGBUS instead, for two reasons:

1. We could keep VM_FAULT_RETRY, but then we'd have to drop the fault lock first and cap the retry ourselves, the way __folio_lock_or_retry() does.

2. A failed raw_res_spin_lock_irqsave() already means a possible deadlock was detected, so retrying just hits the same lock again.

Quoted 8 lines

So returning VM_FAULT_RETRY here is overkill.

Fixes b8467290edab ("bpf: arena: make arena kfuncs any context safe")

---
target to bpf-next since I think it is moderate.
---

This looks ok to me, but after staring at this function for longer, I think all kinds of non-recoverable errors should be using VM_FAULT_SIGBUS vs SIGSEGV.

The only case with SIGSEGV should be the flag based request to disable allocation on lazy faults. That should include the scratch page case, since it represents a hole.

Quoted 8 lines

SIGSEGV: BPF_F_SEGV_ON_FAULT, scratch page.
SIGBUS: lock acquisition failure, range-tree failures, page allocation failure, kernel PTE install failure.

All of these SIGBUS are almost improbable, but I think it would be cleaner to keep semantics clear.

Could you follow up with this change? I applied this one for now.

Sure. I'm happy to do it.

Quoted 22 lines
Quoted 22 lines
  kernel/bpf/arena.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
kernel/bpf/arena.c +6 -2
index 34f023a537fe..555ee2531ef9 100644
@@ -490,8 +490,12 @@ static vm_fault_t arena_vm_fault(struct vm_fault *vmf)
  	kaddr = kbase + (u32)(vmf->address);
 
  	if (raw_res_spin_lock_irqsave(&arena->spinlock, flags))
-		/* Make a reasonable effort to address impossible case */
-		return VM_FAULT_RETRY;
+		/*
+		 * A failed lock means a possible deadlock was detected. Don't
+		 * return VM_FAULT_RETRY: this handler never took mmap_lock, but
+		 * the fault path would re-take it on retry and deadlock. Fail.
+		 */
+		return VM_FAULT_SIGBUS;
 
  	page = vmalloc_to_page((void *)kaddr);
  	if (page) {