Received: by 2002:a25:2c96:0:0:0:0:0 with SMTP id s144csp518994ybs; Sun, 24 May 2020 12:26:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxeN7MjMZs6VohqHSFaQ6CeYKNeibiii+79B/v/OhJeraeKeuzKb0di4CFRjOg4422V2sr5 X-Received: by 2002:a17:906:4353:: with SMTP id z19mr17325011ejm.371.1590348407877; Sun, 24 May 2020 12:26:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590348407; cv=none; d=google.com; s=arc-20160816; b=rZTDe1LI0b6Y9os8RZapdZhOjKWqJzpqxFxQ2+Z9WsD+wD0c7SApWpQ0nqHgu6s1EO GSrroxvBHUesyo7zYU+3+xNoBRSYuFBA9ch+Qb+AGl+vrCMTolQaOqA6HBrYVlrxFRSP J5T9iFiSCOFjZZWKr2Vzpe4CBxi8GYVOUGUxXCCTe+ChC4/zh7mi5xpRwHkiUTZZmZD/ 7J84aEogqVPW8qDJJBNYBXKtrdp0fRDqp2ZfYWsX3yk8YA4o+WdMx30io9TgtZFxRQkQ CWuTnGKfRhz1fZ3w4NNpmMy5SVJB6Y3XJy+y/rsqjaJprERsT2LOqE+b4J6H46pwL9Yy Zi5A== 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=dRxVLULuo23vPsgtEWsGYGFpCYUxsZhIaT6u42Hql2E=; b=sTVKeo8dt0kQ0Fd9rMEck8JD8XjI49qdw/cMA/Pfcj0hECmX3rBhn0W4ZFAkOY1c1y ATXCzobbpi/5RHXGYJm+eFo2sR39oqniLEHHFGO/xuLKgDwH3R3fw1fkh4dnqYvCTiCX gnerOdn0MD9qGvYXAUGun/hWF5dNVgGu79Dh4Z7np3eORlA9GEMVk1Uu4BkA7VId932q KU6XZKNLek3IaHP5LrlO6Ob4i9LSwdxEojLJYvvE1shXiNNz6QtBZMmcVrLLF++yQGpz DUSuaz0vY3XvSAhr6HkUuPTrH5HhgB8fVHJDJz8MRzY0kOPatj78uXLgW3LC5UN6v59p xO2Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel-dk.20150623.gappssmtp.com header.s=20150623 header.b=V4mIXVQF; 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 i5si7526344edq.575.2020.05.24.12.26.24; Sun, 24 May 2020 12:26:47 -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=V4mIXVQF; 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 S2388226AbgEXTWZ (ORCPT + 99 others); Sun, 24 May 2020 15:22:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36946 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388119AbgEXTWS (ORCPT ); Sun, 24 May 2020 15:22:18 -0400 Received: from mail-pg1-x543.google.com (mail-pg1-x543.google.com [IPv6:2607:f8b0:4864:20::543]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9DCEBC05BD43 for ; Sun, 24 May 2020 12:22:18 -0700 (PDT) Received: by mail-pg1-x543.google.com with SMTP id m1so1528733pgk.1 for ; Sun, 24 May 2020 12:22:18 -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=dRxVLULuo23vPsgtEWsGYGFpCYUxsZhIaT6u42Hql2E=; b=V4mIXVQFyA2xlU324aezgz7hoH5/kwrcukrZDA1MEw3qMNYi6vXFPLbiXDJ35lEn1M cz+I3E0aSOYnWOWKnM05PwTsEls9yrhFa1tZ/Oyxfddc2RtB03mcUSpferTg5lavJIqJ Dj5UTA49dxBRYaCq/zzgZHSotdJgYJMe4YTpXOUfPwjxszbv2+Rj3Diq6hu6rCdGQgeR jCdV/0Q2QAhgKJmNRqUY3HFNQUHsB+4Yt2ZVj5amRvJ7o3JkwGLTixFLXi1j/pnfGltP 8shg/8s9ZL/+mu2rqdj8zLMzh+R2KH1VtFdrw+ufEj28wz2kyOkdQqQeGPprP02KtqDR DvoA== 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=dRxVLULuo23vPsgtEWsGYGFpCYUxsZhIaT6u42Hql2E=; b=T4l1RnhWWHDqPW5Lz+r+sb5IPnOazu230fYqWmK0snCgQ8aqyWFKTzRhk1dYcqJzzo HKpVHrgU1QCEyei/r6Ok6wyM55q8XM8/cceTKSMksLh8YDB4YH4jy/7MYct2DKTxMsxU xyu7G/ZuQCJpfORrqGESsXCKRWUuWCZj8EaLjyRDRfKFc2DKO1k4h7bq6oSGGdkO2hts ZZqy582cOK5AlitKmO/pLN+PRI4j/Z560XldbFPAxQMMti4d7gUiW11K6XIgvMF6rfCm oTBZCczBTNctkye/AfEx7zBE4e5yYogBUBOVCm4mvE4wOzVWg9l4jguGFJk8cleKFAxk MUiw== X-Gm-Message-State: AOAM532zlSv0jg1rggZto6tquE4IBu5mhS7WuqSTOE55Fkk4DDhaogrD wVwZnC7xTJ7O06defHEsv7mZsQ== X-Received: by 2002:a63:712:: with SMTP id 18mr22778908pgh.96.1590348138120; Sun, 24 May 2020 12:22:18 -0700 (PDT) Received: from x1.lan ([2605:e000:100e:8c61:c871:e701:52fa:2107]) by smtp.gmail.com with ESMTPSA id t21sm10312426pgu.39.2020.05.24.12.22.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 May 2020 12:22:17 -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, Jens Axboe Subject: [PATCH 04/12] mm: add support for async page locking Date: Sun, 24 May 2020 13:21:58 -0600 Message-Id: <20200524192206.4093-5-axboe@kernel.dk> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200524192206.4093-1-axboe@kernel.dk> References: <20200524192206.4093-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 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 --- 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 7e84d823c6a8..5a5434ff7543 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