Received: by 2002:a25:ef43:0:0:0:0:0 with SMTP id w3csp275325ybm; Tue, 26 May 2020 16:43:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyg2sHdVuHxwIsBBQK6VLM275a2qW9RAwteRaeWDTHW9cJ/GdzlHdrZpI1Am9afuJeM9DhM X-Received: by 2002:a05:6402:7ca:: with SMTP id u10mr21312444edy.322.1590536598504; Tue, 26 May 2020 16:43:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590536598; cv=none; d=google.com; s=arc-20160816; b=PaNpBvX+RLaAUJf4yef1Nd/CELG05Ru0P0yrByV4ElSyKiJwmX2JQMbIqN3lw/O548 urZyis9jabT4BR5FrQSyxtXd7UD8/YohTdJOwmDJekNgXhgui51qRKZBjK5hlIK91d8h AsJo0XMLlWTAHUK8ybkBIATvXRtlzCsct8O0FPwVpqLY+mP487X8dmry30qLmzzNA09X 7EhB1qUvsZyplUZ1Bh3K+fntYydP4zgOLqo6vxg0Hej/TDXVovf95uQ7jhpQfOwV0nxz yTynPtGhWlXKVBa7OPCNWrk9SpKp3WVvhE2EuuhooY/IkAus4igdUNzCBjOkTdx/Xovr e3DQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=91LPx4VFG9btmYMQhwNjB8l3CSYHzquXTBOwjvdo+tY=; b=AxTT0h/yfmEKHCmKnYvbDz37EdVejBwerz0xryNnurNkGZ89253DJuQL0dDV4z39p/ 4SXiC4lG4O3FVrzAwdsWW7TliGyrrjqdU53701L4USrfkObELu2GERsFDsfeEZywA/4S 6hfAmBQfXgAddpmhfwgDYw5VaxuOWF7Q3Hvu72RB3Maf5nOs7/ZCumGnpjp5nUtPzV8/ D6pyzvzlAoRlLHL5E2C8p2blpby6lZf1BikFV64y5CGWlGFVs1lICw4iOHCiXnsRQnbJ Rj+tvkJDQ+gjS/4qCREs2pn7KnIp/mCyz9jtRibYYNHolZwb2M16JfRBzc0ozcpZ2liT CcuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel-dk.20150623.gappssmtp.com header.s=20150623 header.b="iE3qXf/O"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w19si779960eji.578.2020.05.26.16.42.55; Tue, 26 May 2020 16:43:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel-dk.20150623.gappssmtp.com header.s=20150623 header.b="iE3qXf/O"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390460AbgEZTwW (ORCPT + 99 others); Tue, 26 May 2020 15:52:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40494 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390285AbgEZTvd (ORCPT ); Tue, 26 May 2020 15:51:33 -0400 Received: from mail-pj1-x1044.google.com (mail-pj1-x1044.google.com [IPv6:2607:f8b0:4864:20::1044]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 25A04C03E96E for ; Tue, 26 May 2020 12:51:32 -0700 (PDT) Received: by mail-pj1-x1044.google.com with SMTP id t8so260896pju.3 for ; Tue, 26 May 2020 12:51:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=91LPx4VFG9btmYMQhwNjB8l3CSYHzquXTBOwjvdo+tY=; b=iE3qXf/OK07yvdsW8Vb51ZnFfsRrhY/I8AyPsPZBuYDCG+TC8jz9R0D0M+GRoONqmI gim5npZm8cxTM+QSweOQXfZsODg5VS6+7bN+CKZ4dMY7gY+QpgkMzMrv59Fphw7RYayS JBGmAVIX3BEE46Smv2GYnsm+ImSrUvQiaACUJ58syDnlB+HNG5sK2LsdT4XG1F3dqp/4 oVhakRyiZ0nYbu4FXaNA5/FJOFFKKsGpp/Hce7YkE5gJePG3prwbUWapZwHq23NDXkHD /0mpwLPbH1DZDuHuFhnGHadUwVq75hkc8cYWz+Lkln7BqwK/1OvfBTdLVjGY87S3aO6Z QMtg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=91LPx4VFG9btmYMQhwNjB8l3CSYHzquXTBOwjvdo+tY=; b=SkE0NlmOQ6RFm6SR/KVfKutAk9b4FfmfMJWonSdGUvivrwGmdCu3Qqf4LQ3jDAiUJw dZFaE3ISHGoFBS3H9NQFobvhpSyHA1CWVocp4cfCnQVdPBCuXe5KFdfbCJfruVp2/ytR eopMolBet1WDm3OBgwcrnZ4GQBPXTAuOPdm4hck6v63bE1zNEjJwbzYHP8qMzoC/ymrV ZNs3cdHSwkF45YkXV/krgW4e+Dfi5OfIFEjlkl0NTrgp1QAs/3LFJpA6Emd6Q4ReE0wg oMD+OkBEz8hTlUmjEVZmZpFAsk8iufR/Wdlmb04e3LZ2xFXN0a7baYEK9ut+XYpjl0kR binQ== X-Gm-Message-State: AOAM531jjuIEOhnsHhLO3MqmjbEtMIzA8ToLcu2MjlFAHr9HgPFzXD/X lMdQjhUuswMaMhJyyQY7Mft5HQ== X-Received: by 2002:a17:90a:2a0d:: with SMTP id i13mr909880pjd.94.1590522691575; Tue, 26 May 2020 12:51:31 -0700 (PDT) Received: from x1.lan ([2605:e000:100e:8c61:94bb:59d2:caf6:70e1]) by smtp.gmail.com with ESMTPSA id c184sm313943pfc.57.2020.05.26.12.51.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 May 2020 12:51:31 -0700 (PDT) From: Jens Axboe To: io-uring@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org, Jens Axboe Subject: [PATCH 04/12] mm: add support for async page locking Date: Tue, 26 May 2020 13:51:15 -0600 Message-Id: <20200526195123.29053-5-axboe@kernel.dk> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200526195123.29053-1-axboe@kernel.dk> References: <20200526195123.29053-1-axboe@kernel.dk> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 add a new iocb field, ki_waitq, to pass in the necessary data for this to happen. We can unionize this with ki_cookie, since that is only used for polled IO. Polled IO can never co-exist with async callbacks, as it is (by definition) polled completions. 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 --- include/linux/fs.h | 7 ++++++- include/linux/pagemap.h | 9 +++++++++ mm/filemap.c | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/include/linux/fs.h b/include/linux/fs.h index d3ebb49189df..ba1fff0e7bca 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->ki_waitq is valid */ +#define IOCB_WAITQ (1 << 8) struct kiocb { struct file *ki_filp; @@ -327,7 +329,10 @@ struct kiocb { int ki_flags; u16 ki_hint; u16 ki_ioprio; /* See linux/ioprio.h */ - unsigned int ki_cookie; /* for ->iopoll */ + union { + unsigned int ki_cookie; /* for ->iopoll */ + struct wait_page_queue *ki_waitq; /* for async buffered IO */ + }; randomized_struct_fields_end }; diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index 53d980f2208d..d3e63c9c61ae 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -495,6 +495,7 @@ static inline int wake_page_match(struct wait_page_queue *wait_page, 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_queue *wait); extern int __lock_page_or_retry(struct page *page, struct mm_struct *mm, unsigned int flags); extern void unlock_page(struct page *page); @@ -531,6 +532,14 @@ static inline int lock_page_killable(struct page *page) return 0; } +static inline int lock_page_async(struct page *page, + struct wait_page_queue *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 e891b5bee8fd..c746541b1d49 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -1183,6 +1183,42 @@ 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_queue *wait, bool set) +{ + struct wait_queue_head *q = page_waitqueue(page); + int ret = 0; + + wait->page = page; + wait->bit_nr = PG_locked; + + spin_lock_irq(&q->lock); + if (set) + ret = !trylock_page(page); + else + ret = PageLocked(page); + if (ret) { + __add_wait_queue_entry_tail(q, &wait->wait); + SetPageWaiters(page); + if (set) + ret = !trylock_page(page); + else + ret = PageLocked(page); + /* + * If we were succesful now, we know we're still on the + * waitqueue as we're still under the lock. This means it's + * safe to remove and return success, we know the callback + * isn't going to trigger. + */ + if (!ret) + __remove_wait_queue(q, &wait->wait); + else + ret = -EIOCBQUEUED; + } + spin_unlock_irq(&q->lock); + return ret; +} + /** * put_and_wait_on_page_locked - Drop a reference and wait for it to be unlocked * @page: The page to wait for. @@ -1345,6 +1381,11 @@ int __lock_page_killable(struct page *__page) } EXPORT_SYMBOL_GPL(__lock_page_killable); +int __lock_page_async(struct page *page, struct wait_page_queue *wait) +{ + return __wait_on_page_locked_async(page, wait, true); +} + /* * Return values: * 1 - page is locked; mmap_sem is still held. -- 2.26.2