The Linux Kernel Mailing List
 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,
	Dheeraj Reddy Jonnalagadda <dheeraj.linuxdev@gmail.com>
Subject: [PATCH include] ptr_ring: fix potential race in ptr_ring_resize_multiple_bh_noprof
Date: Sat, 21 Dec 2024 11:45:55 +0530	[thread overview]
Message-ID: <20241221061555.1071516-2-dheeraj.linuxdev@gmail.com> (raw)
In-Reply-To: <20241221061555.1071516-1-dheeraj.linuxdev@gmail.com>

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


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

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2024-12-21  9:04   ` [PATCH include] ptr_ring: fix potential race " 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-2-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox