[PATCH RFC] possible atomicity issue in ptr_ring_resize_multiple_bh_noprof
From: Dheeraj Reddy Jonnalagadda
Date: Sat Dec 21 2024 - 01:16:09 EST
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