Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp56507iof; Sun, 5 Jun 2022 20:59:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwx8BqUKtC/TskNB0rZuv3XM4Oqx35XQ17e8Y/hDSOmQw0YUK5xHo8e91TpVQpXI+nrnFub X-Received: by 2002:a17:902:a507:b0:167:7a06:b2f5 with SMTP id s7-20020a170902a50700b001677a06b2f5mr4419084plq.128.1654487990928; Sun, 05 Jun 2022 20:59:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654487990; cv=none; d=google.com; s=arc-20160816; b=eoPKgKLmQZBxP3QPT+YOtPYD+rnVyNOmLfWXNPFTxp9Ov0SQJp1D0x42d6f0OVs4nm jKTYaigXZHNn4XmbTG9c/YFEx0ieKf/bssbcDyjNw+saPmbNcbmdQy0yo/3HLGQpFOah 1jar71zbCFszG17Qri7DUio+/qcpmckDYLPgZdJGUhwMM5pXvdWJM6SZJqSuajLVr5/O LEbyCi4tf/oy6e5y6sxCfJY/wlD0t1TiL/FMxtAVxdc4JMbWPMXiuFcQ4FyVmdnue1Q9 sNKs7Fh7rzhePstMB/0tqMsRCG3wJPupjVMjuu3kCCl119wDY3HM/OgYpzMjK3EvAAOc K/1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=6g09y1A9jVwuZlJR1irGeBqDpd31Ui3pSGWrlaz4nb0=; b=phqpu/D9VJH5SyNVnmmYW4ZVXUmePBVCkkIjmMAsc2cA1/+XtayA1bTb2Y5nvhpFgD jKN2dPbcTfv7caYPGpQrm4H6SU2Og1xxyDheqGZWKzD6IKEk1LbRwNuOwf6wq2eW39dh vytK6BQ7EYY3JQ5IDKd9zCfYTSu1CSnDgNVexUajvtQf10ikSG+Or90zqBdaWF6XWAGI YR/dIN8xmNMKY8kqNcin/x/vJ8+m2E1Lh7zxT42hevLs+sbH5aB1E9nvvjztvLOLghTQ TPNyl3PSXLsD8hb6MSnV0pkN18aK4TkQtE4+L0qIIIbE21gwQNx1Wmt+1vAErOXndFHZ 5Z0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=pg4gnoTy; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id k10-20020a170902d58a00b0015eb08a71a6si18002769plh.16.2022.06.05.20.59.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Jun 2022 20:59:50 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=pg4gnoTy; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id F417A5A2D7; Sun, 5 Jun 2022 20:45:57 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351542AbiFETjN (ORCPT + 99 others); Sun, 5 Jun 2022 15:39:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42528 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347115AbiFETjG (ORCPT ); Sun, 5 Jun 2022 15:39:06 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2C3C21181A; Sun, 5 Jun 2022 12:39:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=6g09y1A9jVwuZlJR1irGeBqDpd31Ui3pSGWrlaz4nb0=; b=pg4gnoTyTVQMZkuLsLjufELZOm zdlkyM6Epz9Y5ls6kjBxq58ihNIUrb2VqIKn1nUlONRVxbp4g7dlQzMHo9JPUvh1cyx4cdcigWDI1 slwAgDoI7SkWyyzH+j97LCItJ0qSvavcuPnYPdS5zdoFT4aZpCiGwaGGj7aUofkedaHvtjKzyVrNI 42u9ldindn05Wm1yrBmLckPHwYi8R4I2j0QnGjrpg23633uwpEOoaC3S55ItOiGyvCgkUjK/1UElz ks1lQWAPi22UiL9YIdwqGaBwOialRfaScxUmJwyto5dh3WMxU2Ag123Wygqx+r/Kt4P8V7BgYZyFl 43sDr6cA==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1nxw5Q-009wsP-JZ; Sun, 05 Jun 2022 19:38:56 +0000 From: "Matthew Wilcox (Oracle)" To: linux-fsdevel@vger.kernel.org Cc: "Matthew Wilcox (Oracle)" , linux-kernel@vger.kernel.org, linux-ext4@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, linux-mm@kvack.org, linux-nilfs@vger.kernel.org Subject: [PATCH 01/10] filemap: Add filemap_get_folios() Date: Sun, 5 Jun 2022 20:38:45 +0100 Message-Id: <20220605193854.2371230-2-willy@infradead.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220605193854.2371230-1-willy@infradead.org> References: <20220605193854.2371230-1-willy@infradead.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=0.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,SUSPICIOUS_RECIPS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This is the equivalent of find_get_pages() but fills a folio_batch instead of an array of pages. Signed-off-by: Matthew Wilcox (Oracle) --- include/linux/pagemap.h | 2 ++ mm/filemap.c | 55 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index 5555689ea809..50e57b2d845f 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -718,6 +718,8 @@ static inline struct page *find_subpage(struct page *head, pgoff_t index) return head + (index & (thp_nr_pages(head) - 1)); } +unsigned filemap_get_folios(struct address_space *mapping, pgoff_t *start, + pgoff_t end, struct folio_batch *fbatch); unsigned find_get_pages_range(struct address_space *mapping, pgoff_t *start, pgoff_t end, unsigned int nr_pages, struct page **pages); diff --git a/mm/filemap.c b/mm/filemap.c index 1e66eea98a7e..ea4145b7a84c 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2127,6 +2127,61 @@ unsigned find_lock_entries(struct address_space *mapping, pgoff_t start, return folio_batch_count(fbatch); } +/** + * filemap_get_folios - Get a batch of folios + * @mapping: The address_space to search + * @start: The starting page index + * @end: The final page index (inclusive) + * @fbatch: The batch to fill. + * + * Search for and return a batch of folios in the mapping starting at + * index @start and up to index @end (inclusive). The folios are returned + * in @fbatch with an elevated reference count. + * + * The first folio may start before @start; if it does, it will contain + * @start. The final folio may extend beyond @end; if it does, it will + * contain @end. The folios have ascending indices. There may be gaps + * between the folios if there are indices which have no folio in the + * page cache. If folios are added to or removed from the page cache + * while this is running, they may or may not be found by this call. + * + * Return: The number of folios which were found. + * We also update @start to index the next folio for the traversal. + */ +unsigned filemap_get_folios(struct address_space *mapping, pgoff_t *start, + pgoff_t end, struct folio_batch *fbatch) +{ + XA_STATE(xas, &mapping->i_pages, *start); + struct folio *folio; + + rcu_read_lock(); + while ((folio = find_get_entry(&xas, end, XA_PRESENT)) != NULL) { + /* Skip over shadow, swap and DAX entries */ + if (xa_is_value(folio)) + continue; + if (!folio_batch_add(fbatch, folio)) { + *start = folio->index + folio_nr_pages(folio); + goto out; + } + } + + /* + * We come here when there is no page beyond @end. We take care to not + * overflow the index @start as it confuses some of the callers. This + * breaks the iteration when there is a page at index -1 but that is + * already broken anyway. + */ + if (end == (pgoff_t)-1) + *start = (pgoff_t)-1; + else + *start = end + 1; +out: + rcu_read_unlock(); + + return folio_batch_count(fbatch); +} +EXPORT_SYMBOL(filemap_get_folios); + static inline bool folio_more_pages(struct folio *folio, pgoff_t index, pgoff_t max) { -- 2.35.1