* [PATCH RFC] possible atomicity issue in ptr_ring_resize_multiple_bh_noprof
@ 2024-12-21 6:15 Dheeraj Reddy Jonnalagadda
2024-12-21 6:15 ` [PATCH include] ptr_ring: fix potential race " Dheeraj Reddy Jonnalagadda
0 siblings, 1 reply; 3+ messages in thread
From: Dheeraj Reddy Jonnalagadda @ 2024-12-21 6:15 UTC (permalink / raw)
To: edumazet, jasowang; +Cc: akpm, surenb, jack, linux-kernel
Hi Maintainers,
While reviewing the ptr_ring_resize_multiple_bh_noprof function, I noticed
a potential atomicity issue. The function appears to be callable from multiple
threads, based on the locking patterns and _bh suffix.
The current code frees queues[i] after releasing locks:
for (i = 0; i < nrings; ++i) {
spin_lock_bh(&(rings[i])->consumer_lock);
spin_lock(&(rings[i])->producer_lock);
queues[i] = __ptr_ring_swap_queue(rings[i], queues[i],
size, gfp, destroy);
spin_unlock(&(rings[i])->producer_lock);
spin_unlock_bh(&(rings[i])->consumer_lock);
}
/* Free after releasing locks */
for (i = 0; i < nrings; ++i)
kvfree(queues[i]);
It seems that there could be a race condition where another thread modifies
queues[i] between the unlock and the kvfree. Would it be safer to do the
kvfree while still holding the locks and removing the kvfree loop later
as shown below?
for (i = 0; i < nrings; ++i) {
spin_lock_bh(&(rings[i])->consumer_lock);
spin_lock(&(rings[i])->producer_lock);
queues[i] = __ptr_ring_swap_queue(rings[i], queues[i],
size, gfp, destroy);
kvfree(queues[i]);
spin_unlock(&(rings[i])->producer_lock);
spin_unlock_bh(&(rings[i])->consumer_lock);
}
kfree(queues);
return 0;
I've attached a potential fix, but would appreciate confirmation on whether
this is actually an issue that needs addressing.
-Dheeraj
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH include] ptr_ring: fix potential race in ptr_ring_resize_multiple_bh_noprof
2024-12-21 6:15 [PATCH RFC] possible atomicity issue in ptr_ring_resize_multiple_bh_noprof Dheeraj Reddy Jonnalagadda
@ 2024-12-21 6:15 ` Dheeraj Reddy Jonnalagadda
2024-12-21 9:04 ` Eric Dumazet
0 siblings, 1 reply; 3+ messages in thread
From: Dheeraj Reddy Jonnalagadda @ 2024-12-21 6:15 UTC (permalink / raw)
To: edumazet, jasowang
Cc: akpm, surenb, jack, linux-kernel, Dheeraj Reddy Jonnalagadda
The ptr_ring_resize_multiple_bh_noprof function may have a race condition
where queues[i] is freed after releasing the locks. Since this function
can be called from multiple threads, another thread could potentially modify
queues[i] between the unlock and kvfree operations.
Move the kvfree inside the critical section to ensure atomicity of the
queue swap and cleanup operations.
Fixes: 59e6ae53248a("ptr_ring: support resizing multiple queues")
Closes: https://scan7.scan.coverity.com/#/project-view/52337/11354?selectedIssue=1602644
Signed-off-by: Dheeraj Reddy Jonnalagadda <dheeraj.linuxdev@gmail.com>
---
include/linux/ptr_ring.h | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/include/linux/ptr_ring.h b/include/linux/ptr_ring.h
index 551329220e4f..cb06c74fc791 100644
--- a/include/linux/ptr_ring.h
+++ b/include/linux/ptr_ring.h
@@ -641,13 +641,11 @@ static inline int ptr_ring_resize_multiple_bh_noprof(struct ptr_ring **rings,
spin_lock(&(rings[i])->producer_lock);
queues[i] = __ptr_ring_swap_queue(rings[i], queues[i],
size, gfp, destroy);
+ kvfree(queues[i]);
spin_unlock(&(rings[i])->producer_lock);
spin_unlock_bh(&(rings[i])->consumer_lock);
}
- for (i = 0; i < nrings; ++i)
- kvfree(queues[i]);
-
kfree(queues);
return 0;
--
2.34.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH include] ptr_ring: fix potential race in ptr_ring_resize_multiple_bh_noprof
2024-12-21 6:15 ` [PATCH include] ptr_ring: fix potential race " Dheeraj Reddy Jonnalagadda
@ 2024-12-21 9:04 ` Eric Dumazet
0 siblings, 0 replies; 3+ messages in thread
From: Eric Dumazet @ 2024-12-21 9:04 UTC (permalink / raw)
To: Dheeraj Reddy Jonnalagadda; +Cc: jasowang, akpm, surenb, jack, linux-kernel
On Sat, Dec 21, 2024 at 7:16 AM Dheeraj Reddy Jonnalagadda
<dheeraj.linuxdev@gmail.com> wrote:
>
> The ptr_ring_resize_multiple_bh_noprof function may have a race condition
> where queues[i] is freed after releasing the locks. Since this function
> can be called from multiple threads, another thread could potentially modify
> queues[i] between the unlock and kvfree operations.
>
> Move the kvfree inside the critical section to ensure atomicity of the
> queue swap and cleanup operations.
>
> Fixes: 59e6ae53248a("ptr_ring: support resizing multiple queues")
> Closes: https://scan7.scan.coverity.com/#/project-view/52337/11354?selectedIssue=1602644
> Signed-off-by: Dheeraj Reddy Jonnalagadda <dheeraj.linuxdev@gmail.com>
> ---
> include/linux/ptr_ring.h | 4 +---
> 1 file changed, 1 insertion(+), 3 deletions(-)
>
> diff --git a/include/linux/ptr_ring.h b/include/linux/ptr_ring.h
> index 551329220e4f..cb06c74fc791 100644
> --- a/include/linux/ptr_ring.h
> +++ b/include/linux/ptr_ring.h
> @@ -641,13 +641,11 @@ static inline int ptr_ring_resize_multiple_bh_noprof(struct ptr_ring **rings,
> spin_lock(&(rings[i])->producer_lock);
> queues[i] = __ptr_ring_swap_queue(rings[i], queues[i],
> size, gfp, destroy);
> + kvfree(queues[i]);
> spin_unlock(&(rings[i])->producer_lock);
> spin_unlock_bh(&(rings[i])->consumer_lock);
> }
>
> - for (i = 0; i < nrings; ++i)
> - kvfree(queues[i]);
> -
> kfree(queues);
I do not think this patch makes sense.
queues[] is private to this thread, there is no possible race.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-12-21 9:04 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-21 6:15 [PATCH RFC] possible atomicity issue in ptr_ring_resize_multiple_bh_noprof Dheeraj Reddy Jonnalagadda
2024-12-21 6:15 ` [PATCH include] ptr_ring: fix potential race " Dheeraj Reddy Jonnalagadda
2024-12-21 9:04 ` Eric Dumazet
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.