All of lore.kernel.org
 help / color / mirror / Atom feed
* [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

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.