From patchwork Mon Feb 8 22:18:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 12076607 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.6 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 0F22BC433DB for ; Mon, 8 Feb 2021 22:18:40 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 87EA664E9A for ; Mon, 8 Feb 2021 22:18:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 87EA664E9A 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 086A46B006C; Mon, 8 Feb 2021 17:18:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 00DF06B006E; Mon, 8 Feb 2021 17:18:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E16806B0070; Mon, 8 Feb 2021 17:18:38 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0120.hostedemail.com [216.40.44.120]) by kanga.kvack.org (Postfix) with ESMTP id CC5B66B006C for ; Mon, 8 Feb 2021 17:18:38 -0500 (EST) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 98673B9E8 for ; Mon, 8 Feb 2021 22:18:38 +0000 (UTC) X-FDA: 77796515916.22.wash71_400500b27602 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin22.hostedemail.com (Postfix) with ESMTP id 74EB0181D179E for ; Mon, 8 Feb 2021 22:18:38 +0000 (UTC) X-HE-Tag: wash71_400500b27602 X-Filterd-Recvd-Size: 6296 Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) by imf47.hostedemail.com (Postfix) with ESMTP for ; Mon, 8 Feb 2021 22:18:37 +0000 (UTC) Received: by mail-pg1-f175.google.com with SMTP id c132so11173478pga.3 for ; Mon, 08 Feb 2021 14:18:37 -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=/11CO3rYWuI/f/YHyMz9MjwWlbtuyR7jns8drX5Z6GM=; b=mb2oFeIEVgDEViUTWxQhJRn8gyfghyhp/ElayDIuXqgeaBC3HYBqXz/VyGWRwyfxyX xNOVUOTlqZOiwoPZvGkawIKvPSdaiRywSD983pANIibOlTA45T3D/u9hBzhvPIaon/tU WBt2X4roLHiAAxx56l/gFiUa/HQcYyqIS4SHHtGj51uM/gYRzdJCOrOubTCc7icgPz/e TBPf1d33DE2Lp+z5ek0LEGD8l0kYthWmJBknCrZPLXoi744YaRuxo52NTOvbjN+Hl6oI shdmBniDyS8xzu99iHg+R7lPS9r9fejFakjX/txkrDqm21gvQbo2V0UeFU7OjW3KBDZA ushw== 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=/11CO3rYWuI/f/YHyMz9MjwWlbtuyR7jns8drX5Z6GM=; b=LFZqQJImsP5YLVitImWMvN7lqaN91NhkxbrYdQI3ChgaZcuiGTrxZ1VQhnit9dt7oA FFdoNLAOtUJ7In/6qbIAAcYv/43hVSKDVJVwgvqj0mFDsmaeTyhPGSK26b9KbZoSlFgm PRq17OEH591YT+Yof99e28N6fuBJfFoPR/F1PXjR+W+NwovqAt8yOTt+d+MefIVdLadM oBjY+rYxwSVbVGIcxF49kqxXJitlQtM4zAUp0vO9XmxQJgor6rQb1OEsGmPteQSExqoB XknqTJ+I9ZlRimr71GUs+0BKy43W0/pcLvptpRvwVE00yWLH85cejiqn+/w7ykGZTyWc y72g== X-Gm-Message-State: AOAM533O7t/wf5sSW6h2kl9xdbsInxdiBJVzjaeSC0DRA2j/U36eY5As /fe3Oee5qwTyxaM8+6cIOzEbHQ== X-Google-Smtp-Source: ABdhPJzusnHUSOjH31Tu9aj2qgCr38CSB6bRtXmJ7daXvBLWnpK3G1cjMgphD7zjBJsZ8VTZxUZ39g== X-Received: by 2002:a63:416:: with SMTP id 22mr18850742pge.286.1612822716935; Mon, 08 Feb 2021 14:18:36 -0800 (PST) Received: from localhost.localdomain ([2600:380:4a36:d38a:f60:a5d4:5474:9bbc]) by smtp.gmail.com with ESMTPSA id o10sm19324472pfp.87.2021.02.08.14.18.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Feb 2021 14:18:36 -0800 (PST) From: Jens Axboe To: linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Cc: hch@infradead.org, akpm@linux-foundation.org, Jens Axboe Subject: [PATCH 1/3] mm: provide filemap_range_needs_writeback() helper Date: Mon, 8 Feb 2021 15:18:27 -0700 Message-Id: <20210208221829.17247-2-axboe@kernel.dk> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210208221829.17247-1-axboe@kernel.dk> References: <20210208221829.17247-1-axboe@kernel.dk> MIME-Version: 1.0 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 | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/include/linux/fs.h b/include/linux/fs.h index fd47deea7c17..def89222dfe9 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2633,6 +2633,8 @@ static inline int filemap_fdatawait(struct address_space *mapping) 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 aa0e0fb04670..1ed7acac8a1b 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -633,6 +633,52 @@ static bool mapping_needs_writeback(struct address_space *mapping) return mapping->nrpages; } +/** + * filemap_range_needs_writeback - check if range potentially needs writeback + * @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 if + * direct writing in this range will trigger a writeback. Used by O_DIRECT + * 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) +{ + struct page *page = NULL, *head; + XA_STATE(xas, &mapping->i_pages, start_byte >> PAGE_SHIFT); + pgoff_t max = end_byte >> PAGE_SHIFT; + + 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, head, max) { + if (xas_retry(&xas, head)) + continue; + if (xa_is_value(head)) + continue; + page = find_subpage(head, xas.xa_index); + if (PageDirty(page) || PageLocked(page) || PageWriteback(page)) + break; + page = NULL; + } + rcu_read_unlock(); + + return page != 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