Skip to contentSkip to conversation

possible atomicity issue in ptr_ring_resize_multiple_bh_noprof

RFC PATCH New Dheeraj Reddy Jonnalagadda 2 participants 3 messages Dec 21, 2024

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 possible atomicity issue in ptr_ring_resize_multiple_bh_noprof

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

#1 lore raw

PATCH 1/1 ptr_ring: fix potential race in ptr_ring_resize_multiple_bh_noprof

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")
---
 include/linux/ptr_ring.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
include/linux/ptr_ring.h +1 -3
index 551329220e4f..cb06c74fc791 100644
@@ -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;
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH include] ptr_ring: fix potential race in ptr_ring_resize_multiple_bh_noprof

Quoted 23 lines · 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")
---
 include/linux/ptr_ring.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
include/linux/ptr_ring.h +0 -0
index 551329220e4f..cb06c74fc791 100644
@@ -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],

include/linux/ptr_ring.h static inline int ptr_ring_resize_multiple_bh_noprof(struct ptr_ring **rings, · line 649

Quoted 10 lines
include/linux/ptr_ring.h +1 -3
                                                  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.