public inbox for [email protected]
 help / color / mirror / Atom feed
From: Jens Axboe <[email protected]>
To: [email protected]
Cc: [email protected], [email protected],
	[email protected]
Subject: [PATCH v2 03/11] mm: add support for async page locking
Date: Fri, 22 May 2020 14:57:20 -0600	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>

On 5/22/20 2:23 PM, Jens Axboe wrote:
> Normally waiting for a page to become unlocked, or locking the page,
> requires waiting for IO to complete. Add support for lock_page_async()
> and wait_on_page_locked_async(), which are callback based instead. This
> allows a caller to get notified when a page becomes unlocked, rather
> than wait for it.
> 
> We use the iocb->private field to pass in this necessary data for this
> to happen. struct wait_page_key is made public, and we define struct
> wait_page_async as the interface between the caller and the core.

I did some reshuffling of this patch before sending it out, and
I ended up sending a previous version. Please look at this one instead.

commit d8f0a0bfc4a0742cb461287561b956bc56e90976
Author: Jens Axboe <[email protected]>
Date:   Fri May 22 09:12:09 2020 -0600

    mm: add support for async page locking
    
    Normally waiting for a page to become unlocked, or locking the page,
    requires waiting for IO to complete. Add support for lock_page_async()
    and wait_on_page_locked_async(), which are callback based instead. This
    allows a caller to get notified when a page becomes unlocked, rather
    than wait for it.
    
    We use the iocb->private field to pass in this necessary data for this
    to happen. struct wait_page_key is made public, and we define struct
    wait_page_async as the interface between the caller and the core.
    
    Signed-off-by: Jens Axboe <[email protected]>

diff --git a/include/linux/fs.h b/include/linux/fs.h
index 7e84d823c6a8..82b989695ab9 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -314,6 +314,8 @@ enum rw_hint {
 #define IOCB_SYNC		(1 << 5)
 #define IOCB_WRITE		(1 << 6)
 #define IOCB_NOWAIT		(1 << 7)
+/* iocb->private holds wait_page_async struct */
+#define IOCB_WAITQ		(1 << 8)
 
 struct kiocb {
 	struct file		*ki_filp;
diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
index a8f7bd8ea1c6..e260bcd071e4 100644
--- a/include/linux/pagemap.h
+++ b/include/linux/pagemap.h
@@ -456,8 +456,21 @@ static inline pgoff_t linear_page_index(struct vm_area_struct *vma,
 	return pgoff;
 }
 
+/* This has the same layout as wait_bit_key - see fs/cachefiles/rdwr.c */
+struct wait_page_key {
+	struct page *page;
+	int bit_nr;
+	int page_match;
+};
+
+struct wait_page_async {
+	struct wait_queue_entry wait;
+	struct wait_page_key key;
+};
+
 extern void __lock_page(struct page *page);
 extern int __lock_page_killable(struct page *page);
+extern int __lock_page_async(struct page *page, struct wait_page_async *wait);
 extern int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
 				unsigned int flags);
 extern void unlock_page(struct page *page);
@@ -494,6 +507,14 @@ static inline int lock_page_killable(struct page *page)
 	return 0;
 }
 
+static inline int lock_page_async(struct page *page,
+				  struct wait_page_async *wait)
+{
+	if (!trylock_page(page))
+		return __lock_page_async(page, wait);
+	return 0;
+}
+
 /*
  * lock_page_or_retry - Lock the page, unless this would block and the
  * caller indicated that it can handle a retry.
diff --git a/mm/filemap.c b/mm/filemap.c
index 80747f1377d5..ebee7350ea3b 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -990,13 +990,6 @@ void __init pagecache_init(void)
 	page_writeback_init();
 }
 
-/* This has the same layout as wait_bit_key - see fs/cachefiles/rdwr.c */
-struct wait_page_key {
-	struct page *page;
-	int bit_nr;
-	int page_match;
-};
-
 struct wait_page_queue {
 	struct page *page;
 	int bit_nr;
@@ -1210,6 +1203,33 @@ int wait_on_page_bit_killable(struct page *page, int bit_nr)
 }
 EXPORT_SYMBOL(wait_on_page_bit_killable);
 
+static int __wait_on_page_locked_async(struct page *page,
+				       struct wait_page_async *wait)
+{
+	struct wait_queue_head *q = page_waitqueue(page);
+	int ret = 0;
+
+	wait->key.page = page;
+	wait->key.bit_nr = PG_locked;
+
+	spin_lock_irq(&q->lock);
+	if (PageLocked(page)) {
+		__add_wait_queue_entry_tail(q, &wait->wait);
+		SetPageWaiters(page);
+		ret = -EIOCBQUEUED;
+	}
+	spin_unlock_irq(&q->lock);
+	return ret;
+}
+
+static int wait_on_page_locked_async(struct page *page,
+				     struct wait_page_async *wait)
+{
+	if (!PageLocked(page))
+		return 0;
+	return __wait_on_page_locked_async(compound_head(page), wait);
+}
+
 /**
  * put_and_wait_on_page_locked - Drop a reference and wait for it to be unlocked
  * @page: The page to wait for.
@@ -1372,6 +1392,11 @@ int __lock_page_killable(struct page *__page)
 }
 EXPORT_SYMBOL_GPL(__lock_page_killable);
 
+int __lock_page_async(struct page *page, struct wait_page_async *wait)
+{
+	return wait_on_page_locked_async(page, wait);
+}
+
 /*
  * Return values:
  * 1 - page is locked; mmap_sem is still held.

-- 
Jens Axboe


  reply	other threads:[~2020-05-22 20:57 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-22 20:23 [PATCHSET RFC 0/11] Add support for async buffered reads Jens Axboe
2020-05-22 20:23 ` [PATCH 01/11] block: read-ahead submission should imply no-wait as well Jens Axboe
2020-05-22 20:23 ` [PATCH 02/11] mm: allow read-ahead with IOCB_NOWAIT set Jens Axboe
2020-05-22 20:23 ` [PATCH 03/11] mm: add support for async page locking Jens Axboe
2020-05-22 20:57   ` Jens Axboe [this message]
2020-05-22 20:23 ` [PATCH 04/11] mm: support async buffered reads in generic_file_buffered_read() Jens Axboe
2020-05-22 20:23 ` [PATCH 05/11] fs: add FMODE_BUF_RASYNC Jens Axboe
2020-05-22 20:23 ` [PATCH 06/11] ext4: flag as supporting buffered async reads Jens Axboe
2020-05-22 20:23 ` [PATCH 07/11] block: flag block devices as supporting IOCB_WAITQ Jens Axboe
2020-05-22 20:23 ` [PATCH 08/11] xfs: flag files as supporting buffered async reads Jens Axboe
2020-05-22 20:23 ` [PATCH 09/11] btrfs: " Jens Axboe
2020-05-22 20:23 ` [PATCH 10/11] mm: add kiocb_wait_page_async_init() helper Jens Axboe
2020-05-22 20:23 ` [PATCH 11/11] io_uring: support true async buffered reads, if file provides it Jens Axboe

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 \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /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