From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 16CBDC433DB for ; Wed, 24 Feb 2021 16:45:11 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 1269464F04 for ; Wed, 24 Feb 2021 16:45:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1269464F04 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 4D9626B0006; Wed, 24 Feb 2021 11:45:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 42CAD6B0071; Wed, 24 Feb 2021 11:45:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 340FE6B0070; Wed, 24 Feb 2021 11:45:09 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0056.hostedemail.com [216.40.44.56]) by kanga.kvack.org (Postfix) with ESMTP id 187BE6B0005 for ; Wed, 24 Feb 2021 11:45:09 -0500 (EST) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id C090A80222D9 for ; Wed, 24 Feb 2021 16:45:08 +0000 (UTC) X-FDA: 77853736296.20.C2C36D7 Received: from mail-il1-f179.google.com (mail-il1-f179.google.com [209.85.166.179]) by imf11.hostedemail.com (Postfix) with ESMTP id 2FA9A2000393 for ; Wed, 24 Feb 2021 16:44:51 +0000 (UTC) Received: by mail-il1-f179.google.com with SMTP id q9so2298771ilo.1 for ; Wed, 24 Feb 2021 08:45:00 -0800 (PST) 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=Owukme2spaP1zcM9AHe/6hJeoYkmBs9bd0Xi7g8qADI=; b=GeDAo7Bc1lkwEaS/OFb6vd+jmpQQkTiXXvshs4eXK221ZcL0dZcaVB0lb1WFKvgKym h0uKM1lq1iBvNdNmz7EVIEvEJb11qjJD2oK+PEdz+G1oZ4zhUj+M23frgBZzvgV0Qnw8 troypsZkvPWeHDTHKC6Lha9WQQyh1Ud5MFj/W33jd8LRmE/S3zkeI5irCh2jwDOR75di l3toWqZJZLqiwSUzh9mQL4k45w6vwvBh0jMS0ibTJiQjz2YqgUaF647VT5OM+staaOsZ kTohBAGlscOR7g+W4RQGn8p66TiBRohVf94DEJWLbIY7qmev2STOMA9WutevYPxjtYP9 lWIw== 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=Owukme2spaP1zcM9AHe/6hJeoYkmBs9bd0Xi7g8qADI=; b=GwmokhjtYnCggFfQVpo9ONP0Of0dLlaufMpMToYhX3uQY1MkL3W7sj/DQCexE7n9uO 0w9amEgGMVOdoK14F+6h47ADiF0WWfuWufbgFIIju0yCDxMWhcENux3sY3NHW1R5CUAl w5TFsnib7dbqBCbc/HlPNZFPXYGNd7zx0WQXxb+5DrLVMwU6OBFr7lI60d6YmW2jNP51 2X/t2Q9a0XuUX/kWLKQ/yFdB/0CtMspHfmELQCjGKYwdb06yXWfSRRvL59AWmitsSREJ eOc3WqnohzHw/R6KmPaPJIqlkn9THed/2K4fq90ZkWUD11r0Xu/e9iwQhbRVgVBqq86o 4r6w== X-Gm-Message-State: AOAM5306GpL9WvNaiRv1yXmbUOHGfX9x5h804C5AcMOmT0z5cM0YEduK E6xFc33qcxVEs7dX0EqYv8yHvw== X-Google-Smtp-Source: ABdhPJzpcC+zKFW8OqpZDD687rXHdYoKLtmGUfTu+b4vervy16l9zQ+Zel7Wo9F6htsMLjpgyNRD8A== X-Received: by 2002:a05:6e02:1aa5:: with SMTP id l5mr23894877ilv.278.1614185099819; Wed, 24 Feb 2021 08:44:59 -0800 (PST) Received: from p1.localdomain ([65.144.74.34]) by smtp.gmail.com with ESMTPSA id f1sm2273652iov.3.2021.02.24.08.44.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Feb 2021 08:44:59 -0800 (PST) From: Jens Axboe To: linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Cc: akpm@linux-foundation.org, Jens Axboe Subject: [PATCH 1/3] mm: provide filemap_range_needs_writeback() helper Date: Wed, 24 Feb 2021 09:44:53 -0700 Message-Id: <20210224164455.1096727-2-axboe@kernel.dk> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210224164455.1096727-1-axboe@kernel.dk> References: <20210224164455.1096727-1-axboe@kernel.dk> MIME-Version: 1.0 X-Stat-Signature: cdxybak9za4ux5ss1sk8osjoo5isb7jr X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 2FA9A2000393 Received-SPF: none (kernel.dk>: No applicable sender policy available) receiver=imf11; identity=mailfrom; envelope-from=""; helo=mail-il1-f179.google.com; client-ip=209.85.166.179 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1614185091-638725 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: For O_DIRECT reads/writes, we check if we need to issue a call to filemap_write_and_wait_range() to issue and/or wait for writeback for any page in the given range. The existing mechanism just checks for a page in the range, which is suboptimal for IOCB_NOWAIT as we'll fallback to the slow path (and needing retry) if there's just a clean page cache page in the range. Provide filemap_range_needs_writeback() which tries a little harder to check if we actually need to issue and/or wait for writeback in the range. Signed-off-by: Jens Axboe --- include/linux/fs.h | 2 ++ mm/filemap.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/include/linux/fs.h b/include/linux/fs.h index 6d8b1e7337e4..4925275e6365 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2633,6 +2633,8 @@ static inline int filemap_fdatawait(struct address_= space *mapping) =20 extern bool filemap_range_has_page(struct address_space *, loff_t lstart= , loff_t lend); +extern bool filemap_range_needs_writeback(struct address_space *, + loff_t lstart, loff_t lend); extern int filemap_write_and_wait_range(struct address_space *mapping, loff_t lstart, loff_t lend); extern int __filemap_fdatawrite_range(struct address_space *mapping, diff --git a/mm/filemap.c b/mm/filemap.c index 6ff2a3fb0dc7..13338f877677 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -635,6 +635,49 @@ static bool mapping_needs_writeback(struct address_s= pace *mapping) return mapping->nrpages; } =20 +/** + * filemap_range_needs_writeback - check if range potentially needs writ= eback + * @mapping: address space within which to check + * @start_byte: offset in bytes where the range starts + * @end_byte: offset in bytes where the range ends (inclusive) + * + * Find at least one page in the range supplied, usually used to check i= f + * direct writing in this range will trigger a writeback. Used by O_DIRE= CT + * read/write with IOCB_NOWAIT, to see if the caller needs to do + * filemap_write_and_wait_range() before proceeding. + * + * Return: %true if the caller should do filemap_write_and_wait_range() = before + * doing O_DIRECT to a page in this range, %false otherwise. + */ +bool filemap_range_needs_writeback(struct address_space *mapping, + loff_t start_byte, loff_t end_byte) +{ + XA_STATE(xas, &mapping->i_pages, start_byte >> PAGE_SHIFT); + pgoff_t max =3D end_byte >> PAGE_SHIFT; + struct page *page; + + if (!mapping_needs_writeback(mapping)) + return false; + if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && + !mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK)) + return false; + if (end_byte < start_byte) + return false; + + rcu_read_lock(); + xas_for_each(&xas, page, max) { + if (xas_retry(&xas, page)) + continue; + if (xa_is_value(page)) + continue; + if (PageDirty(page) || PageLocked(page) || PageWriteback(page)) + break; + } + rcu_read_unlock(); + return page !=3D NULL; +} +EXPORT_SYMBOL_GPL(filemap_range_needs_writeback); + /** * filemap_write_and_wait_range - write out & wait on a file range * @mapping: the address_space for the pages --=20 2.30.0