public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Jakob Koschel <jkl820.git@gmail.com>
To: Tejun Heo <tj@kernel.org>, Lai Jiangshan <jiangshanlai@gmail.com>
Cc: linux-kernel@vger.kernel.org,
	Pietro Borrello <borrello@diag.uniroma1.it>,
	Cristiano Giuffrida <c.giuffrida@vu.nl>,
	"Bos, H.J." <h.j.bos@vu.nl>, Jakob Koschel <jkl820.git@gmail.com>
Subject: [PATCH] workqueue: avoid usage of list iterator after loop in __flush_workqueue()
Date: Thu, 02 Mar 2023 00:23:19 +0100	[thread overview]
Message-ID: <20230302-workqueue-avoid-iter-after-loop-v1-1-d83324743b99@gmail.com> (raw)

If the list_for_each_entry_safe() iteration never breaks, 'next' would
contain an invalid pointer past the iterator loop. To ensure 'next' is
always valid, we only set it if the correct element was found. That
allows adding a WARN_ON_ONCE in case the code works incorrectly,
exposing currently undetectable potential bugs.

Additionally, Linus proposed to avoid any use of the list iterator
variable after the loop, in the attempt to move the list iterator
variable declaration into the macro to avoid any potential misuse after
the loop [1].

Link: https://lore.kernel.org/all/CAHk-=wgRr_D8CB-D9Kg-c=EHreAsk5SqXPwr9Y7k9sA6cWXJ6w@mail.gmail.com/ [1]
Signed-off-by: Jakob Koschel <jkl820.git@gmail.com>
---
 kernel/workqueue.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index b8b541caed48..73aff7b42903 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -2982,14 +2982,16 @@ void __flush_workqueue(struct workqueue_struct *wq)
 	WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
 
 	while (true) {
-		struct wq_flusher *next, *tmp;
+		struct wq_flusher *next = NULL, *iter, *tmp;
 
 		/* complete all the flushers sharing the current flush color */
-		list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
-			if (next->flush_color != wq->flush_color)
+		list_for_each_entry_safe(iter, tmp, &wq->flusher_queue, list) {
+			if (iter->flush_color != wq->flush_color) {
+				next = iter;
 				break;
-			list_del_init(&next->list);
-			complete(&next->done);
+			}
+			list_del_init(&iter->list);
+			complete(&iter->done);
 		}
 
 		WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
@@ -3006,8 +3008,8 @@ void __flush_workqueue(struct workqueue_struct *wq)
 			 * flusher_queue.  This is the start-to-wait
 			 * phase for these overflowed flushers.
 			 */
-			list_for_each_entry(tmp, &wq->flusher_overflow, list)
-				tmp->flush_color = wq->work_color;
+			list_for_each_entry(iter, &wq->flusher_overflow, list)
+				iter->flush_color = wq->work_color;
 
 			wq->work_color = work_next_color(wq->work_color);
 
@@ -3025,6 +3027,7 @@ void __flush_workqueue(struct workqueue_struct *wq)
 		 * Need to flush more colors.  Make the next flusher
 		 * the new first flusher and arm pwqs.
 		 */
+		WARN_ON_ONCE(!next);
 		WARN_ON_ONCE(wq->flush_color == wq->work_color);
 		WARN_ON_ONCE(wq->flush_color != next->flush_color);
 

---
base-commit: c0927a7a5391f7d8e593e5e50ead7505a23cadf9
change-id: 20230302-workqueue-avoid-iter-after-loop-9d26db48654c

Best regards,
-- 
Jakob Koschel <jkl820.git@gmail.com>


             reply	other threads:[~2023-03-01 23:23 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-01 23:23 Jakob Koschel [this message]
2023-03-02  3:08 ` [PATCH] workqueue: avoid usage of list iterator after loop in __flush_workqueue() Lai Jiangshan

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=20230302-workqueue-avoid-iter-after-loop-v1-1-d83324743b99@gmail.com \
    --to=jkl820.git@gmail.com \
    --cc=borrello@diag.uniroma1.it \
    --cc=c.giuffrida@vu.nl \
    --cc=h.j.bos@vu.nl \
    --cc=jiangshanlai@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tj@kernel.org \
    /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