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=-15.3 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=unavailable 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 4B634C433B4 for ; Fri, 14 May 2021 15:55:51 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id A206561444 for ; Fri, 14 May 2021 15:55:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A206561444 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id D37DC6B006C; Fri, 14 May 2021 11:55:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D0FB06B006E; Fri, 14 May 2021 11:55:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B8A4F6B0070; Fri, 14 May 2021 11:55:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0042.hostedemail.com [216.40.44.42]) by kanga.kvack.org (Postfix) with ESMTP id 8686A6B006C for ; Fri, 14 May 2021 11:55:49 -0400 (EDT) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 29D2B8249980 for ; Fri, 14 May 2021 15:55:49 +0000 (UTC) X-FDA: 78140287218.14.6A62D54 Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by imf03.hostedemail.com (Postfix) with ESMTP id E7811C0007E0 for ; Fri, 14 May 2021 15:55:47 +0000 (UTC) X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 66346B05C; Fri, 14 May 2021 15:55:47 +0000 (UTC) Subject: Re: [PATCH v10 12/33] mm/filemap: Add folio_index, folio_file_page and folio_contains To: "Matthew Wilcox (Oracle)" , akpm@linux-foundation.org Cc: linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Christoph Hellwig , Jeff Layton References: <20210511214735.1836149-1-willy@infradead.org> <20210511214735.1836149-13-willy@infradead.org> From: Vlastimil Babka Message-ID: <77357d4f-5f56-6c12-7602-697773c2f125@suse.cz> Date: Fri, 14 May 2021 17:55:46 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.10.1 MIME-Version: 1.0 In-Reply-To: <20210511214735.1836149-13-willy@infradead.org> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Authentication-Results: imf03.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf03.hostedemail.com: domain of vbabka@suse.cz designates 195.135.220.15 as permitted sender) smtp.mailfrom=vbabka@suse.cz X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: E7811C0007E0 X-Stat-Signature: 4eekd9urcycbn34kdganajut9dnpcntq X-HE-Tag: 1621007747-494532 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: On 5/11/21 11:47 PM, Matthew Wilcox (Oracle) wrote: > folio_index() is the equivalent of page_index() for folios. > folio_file_page() is the equivalent of find_subpage(). find_subpage() special cases hugetlbfs, folio_file_page() doesn't. > folio_contains() is the equivalent of thp_contains(). Yet here, both thp_contains() and folio_contains() does. This patch doesn't add users so maybe it becomes obvious later, but perhaps worth explaining in the changelog or comment? > Signed-off-by: Matthew Wilcox (Oracle) > Reviewed-by: Christoph Hellwig > Acked-by: Jeff Layton > --- > include/linux/pagemap.h | 53 +++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 53 insertions(+) > > diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h > index bc5fa3d7204e..8eaeffccfd38 100644 > --- a/include/linux/pagemap.h > +++ b/include/linux/pagemap.h > @@ -386,6 +386,59 @@ static inline bool thp_contains(struct page *head, pgoff_t index) > return page_index(head) == (index & ~(thp_nr_pages(head) - 1UL)); > } > > +#define swapcache_index(folio) __page_file_index(&(folio)->page) > + > +/** > + * folio_index - File index of a folio. > + * @folio: The folio. > + * > + * For a folio which is either in the page cache or the swap cache, > + * return its index within the address_space it belongs to. If you know > + * the page is definitely in the page cache, you can look at the folio's > + * index directly. > + * > + * Return: The index (offset in units of pages) of a folio in its file. > + */ > +static inline pgoff_t folio_index(struct folio *folio) > +{ > + if (unlikely(folio_swapcache(folio))) > + return swapcache_index(folio); > + return folio->index; > +} > + > +/** > + * folio_file_page - The page for a particular index. > + * @folio: The folio which contains this index. > + * @index: The index we want to look up. > + * > + * Sometimes after looking up a folio in the page cache, we need to > + * obtain the specific page for an index (eg a page fault). > + * > + * Return: The page containing the file data for this index. > + */ > +static inline struct page *folio_file_page(struct folio *folio, pgoff_t index) > +{ > + return folio_page(folio, index & (folio_nr_pages(folio) - 1)); > +} > + > +/** > + * folio_contains - Does this folio contain this index? > + * @folio: The folio. > + * @index: The page index within the file. > + * > + * Context: The caller should have the page locked in order to prevent > + * (eg) shmem from moving the page between the page cache and swap cache > + * and changing its index in the middle of the operation. > + * Return: true or false. > + */ > +static inline bool folio_contains(struct folio *folio, pgoff_t index) > +{ > + /* HugeTLBfs indexes the page cache in units of hpage_size */ > + if (folio_hugetlb(folio)) > + return folio->index == index; > + return index - folio_index(folio) < folio_nr_pages(folio); > +} > + > /* > * Given the page we found in the page cache, return the page corresponding > * to this index in the file >