Skip to contentSkip to conversation

sched/wait: introduce endmark in __wake_up_common

PATCH In review Bin Lai 3 participants 6 messages Oct 10, 2023

Subsystems (4)
lore
Share & tools
lore

Bin Lai

author
#0 lore raw

PATCH 1/1 sched/wait: introduce endmark in __wake_up_common

Without this patch applied, it can cause the waker to fall into an infinite loop in some cases. The commit 2554db916586 ("sched/wait: Break up long wake list walk") introduces WQ_FLAG_BOOKMARK to break up long wake list walk. When the number of walked entries reach 64, the waker will record scan position and release the queue lock, which reduces interrupts and rescheduling latency.

Let's take an example, ltp-aiodio case of runltp-ng create 100 processes for io testing. These processes write the same test file and wait for page writing complete. Because these processes are all writing to the same file, they may all be waiting for the writeback bit of the same page to be cleared. When the page writeback is completed, the end_page_writeback will clear the writeback bit of the page and wake up all processes on the wake list for the page. At the same time, another process could submit the page and set the writeback bit again. When the awakened processes find that the writeback bit has not been cleared, thery will try to add themselves to the wake list immediately. Because of the WQ_FLAG_BOOKMARK feature, the awakened processes will be added to the tail of wake list again after the waker releases the queue lock. It causes the waker to fall into an infinite loop.

Therefore, we introduce the endmark to indicate the end of bookmark state. When we get the endmark entry, stop placing the bookmark flag and hold the lock until all remaining entries have been walked.

include/linux/wait.h +2 -1
index 5ec7739400f4..3413babd2db4 100644
@@ -213,7 +213,8 @@ int __wake_up(struct wait_queue_head *wq_head, unsigned int mode, int nr, void *
 void __wake_up_on_current_cpu(struct wait_queue_head *wq_head, unsigned int mode, void *key);
 void __wake_up_locked_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
 void __wake_up_locked_key_bookmark(struct wait_queue_head *wq_head,
-		unsigned int mode, void *key, wait_queue_entry_t *bookmark);
+		unsigned int mode, void *key, wait_queue_entry_t *bookmark,
+		wait_queue_entry_t *endmark);
 void __wake_up_sync_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
 void __wake_up_locked_sync_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
 void __wake_up_locked(struct wait_queue_head *wq_head, unsigned int mode, int nr);
kernel/sched/wait.c +34 -12

114 lines not on this page · raw message

mm/filemap.c +8 -3
index 4ea4387053e8..49dc8620271d 100644
@@ -1135,7 +1135,7 @@ static void folio_wake_bit(struct folio *folio, int bit_nr)
 	wait_queue_head_t *q = folio_waitqueue(folio);
 	struct wait_page_key key;
 	unsigned long flags;
-	wait_queue_entry_t bookmark;
+	wait_queue_entry_t bookmark, endmark;
 
 	key.folio = folio;
 	key.bit_nr = bit_nr;
@@ -1146,8 +1146,13 @@ static void folio_wake_bit(struct folio *folio, int bit_nr)
 	bookmark.func = NULL;
 	INIT_LIST_HEAD(&bookmark.entry);
 
+	endmark.flags = 0;
+	endmark.private = NULL;
+	endmark.func = NULL;
+	INIT_LIST_HEAD(&endmark.entry);
+
 	spin_lock_irqsave(&q->lock, flags);
-	__wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
+	__wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark, &endmark);
 
 	while (bookmark.flags & WQ_FLAG_BOOKMARK) {
 		/*
@@ -1159,7 +1164,7 @@ static void folio_wake_bit(struct folio *folio, int bit_nr)
 		spin_unlock_irqrestore(&q->lock, flags);
 		cpu_relax();
 		spin_lock_irqsave(&q->lock, flags);
-		__wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
+		__wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark, &endmark);
 	}
 
 	/*
Signature · 1 line
2.39.3
maintainer
#1 lore raw
Quoted 6 lines · Bin Lai

Without this patch applied, it can cause the waker to fall into an infinite loop in some cases. The commit 2554db916586 ("sched/wait: Break up long wake list walk") introduces WQ_FLAG_BOOKMARK to break up long wake list walk. When the number of walked entries reach 64, the waker will record scan position and release the queue lock, which reduces interrupts and rescheduling latency.

Maybe we could try removing bookmarks instead? There was a thread a while ago where we agreed they weren't actually useful any more.

Patches to follow ...

author
#2 lore raw

Yes, you are right. Now that bookmarks weren't actually useful any more, removing bookmarks is a better way to address the livelocks.

Quoted 13 lines · Matthew Wilcox <[email protected]> 于2023年10月10日周二 11:58写道:
Quoted 6 lines · Bin Lai

Without this patch applied, it can cause the waker to fall into an infinite loop in some cases. The commit 2554db916586 ("sched/wait: Break up long wake list walk") introduces WQ_FLAG_BOOKMARK to break up long wake list walk. When the number of walked entries reach 64, the waker will record scan position and release the queue lock, which reduces interrupts and rescheduling latency.

Maybe we could try removing bookmarks instead? There was a thread a while ago where we agreed they weren't actually useful any more.

Patches to follow ...

#3 lore raw

PATCH 1/2 filemap: Remove use of wait bookmarks

In reply to Bin Lai

The original problem of the overly long list of waiters on a locked page was solved properly by commit 9a1ea439b16b ("mm:
put_and_wait_on_page_locked() while page is migrated"). In the meantime, using bookmarks for the writeback bit can cause livelocks, so we need to stop using them.

---
 mm/filemap.c | 21 +--------------------
 1 file changed, 1 insertion(+), 20 deletions(-)
mm/filemap.c +1 -20
index f0a15ce1bd1b..1f9adad2d781 100644
@@ -1135,32 +1135,13 @@ static void folio_wake_bit(struct folio *folio, int bit_nr)
 	wait_queue_head_t *q = folio_waitqueue(folio);
 	struct wait_page_key key;
 	unsigned long flags;
-	wait_queue_entry_t bookmark;
 
 	key.folio = folio;
 	key.bit_nr = bit_nr;
 	key.page_match = 0;
 
-	bookmark.flags = 0;
-	bookmark.private = NULL;
-	bookmark.func = NULL;
-	INIT_LIST_HEAD(&bookmark.entry);
-
 	spin_lock_irqsave(&q->lock, flags);
-	__wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
-
-	while (bookmark.flags & WQ_FLAG_BOOKMARK) {
-		/*
-		 * Take a breather from holding the lock,
-		 * allow pages that finish wake up asynchronously
-		 * to acquire the lock and remove themselves
-		 * from wait queue
-		 */
-		spin_unlock_irqrestore(&q->lock, flags);
-		cpu_relax();
-		spin_lock_irqsave(&q->lock, flags);
-		__wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
-	}
+	__wake_up_locked_key(q, TASK_NORMAL, &key);
 
 	/*
 	 * It's possible to miss clearing waiters here, when we woke our page
Signature · 1 line
2.40.1
#4 lore raw

PATCH 2/2 sched: Remove wait bookmarks

There are no users of wait bookmarks left, so simplify the wait code by removing them.

---
 include/linux/wait.h |  9 +++----
 kernel/sched/wait.c  | 60 ++++++++------------------------------------
 2 files changed, 13 insertions(+), 56 deletions(-)
include/linux/wait.h +3 -6
index 5ec7739400f4..3473b663176f 100644
@@ -19,10 +19,9 @@ int default_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int
 /* wait_queue_entry::flags */
 #define WQ_FLAG_EXCLUSIVE	0x01
 #define WQ_FLAG_WOKEN		0x02
-#define WQ_FLAG_BOOKMARK	0x04
-#define WQ_FLAG_CUSTOM		0x08
-#define WQ_FLAG_DONE		0x10
-#define WQ_FLAG_PRIORITY	0x20
+#define WQ_FLAG_CUSTOM		0x04
+#define WQ_FLAG_DONE		0x08
+#define WQ_FLAG_PRIORITY	0x10
 
 /*
  * A single wait-queue entry structure:
@@ -212,8 +211,6 @@ __remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq
 int __wake_up(struct wait_queue_head *wq_head, unsigned int mode, int nr, void *key);
 void __wake_up_on_current_cpu(struct wait_queue_head *wq_head, unsigned int mode, void *key);
 void __wake_up_locked_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
-void __wake_up_locked_key_bookmark(struct wait_queue_head *wq_head,
-		unsigned int mode, void *key, wait_queue_entry_t *bookmark);
 void __wake_up_sync_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
 void __wake_up_locked_sync_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
 void __wake_up_locked(struct wait_queue_head *wq_head, unsigned int mode, int nr);
kernel/sched/wait.c +10 -50

115 lines not on this page · raw message

Signature · 1 line
2.40.1
Acked-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] sched: Remove wait bookmarks

Quoted 8 lines · Matthew Wilcox (Oracle)

There are no users of wait bookmarks left, so simplify the wait code by removing them.

 include/linux/wait.h |  9 +++----
 kernel/sched/wait.c  | 60 ++++++++------------------------------------
 2 files changed, 13 insertions(+), 56 deletions(-)

Thanks,

Ingo