All of lore.kernel.org
 help / color / mirror / Atom feed
From: Dheeraj Reddy Jonnalagadda <dheeraj.linuxdev@gmail.com>
To: edumazet@google.com, jasowang@redhat.com
Cc: akpm@linux-foundation.org, surenb@google.com, jack@suse.cz,
	linux-kernel@vger.kernel.org
Subject: [PATCH RFC] possible atomicity issue in ptr_ring_resize_multiple_bh_noprof
Date: Sat, 21 Dec 2024 11:45:54 +0530	[thread overview]
Message-ID: <20241221061555.1071516-1-dheeraj.linuxdev@gmail.com> (raw)

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

             reply	other threads:[~2024-12-21  6:15 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-21  6:15 Dheeraj Reddy Jonnalagadda [this message]
2024-12-21  6:15 ` [PATCH include] ptr_ring: fix potential race in ptr_ring_resize_multiple_bh_noprof Dheeraj Reddy Jonnalagadda
2024-12-21  9:04   ` Eric Dumazet

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241221061555.1071516-1-dheeraj.linuxdev@gmail.com \
    --to=dheeraj.linuxdev@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=edumazet@google.com \
    --cc=jack@suse.cz \
    --cc=jasowang@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=surenb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.