Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp667377pxj; Fri, 14 May 2021 12:34:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzf2X5ETP2iIa7wOikSc993MDgxRoJVHwsdr8HbaFFVfiW5H+50BaVwopsGGy3lS2b+B89x X-Received: by 2002:a05:6402:22c3:: with SMTP id dm3mr59757587edb.307.1621020892118; Fri, 14 May 2021 12:34:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621020892; cv=none; d=google.com; s=arc-20160816; b=AZWnORlaP52MXxp9YiRqhRDXupd1Fzb2Mbdz+EzXNqap3f13z5DEv0D67Ndg1Wj15N HU0Zd2rxTtyE9Yykii0accZ7MZ0k4OYSZCTtFJYQ1/CHe9HRwARfwO0I9JoQpHsmRlAO 9lxR1LO/JHR7lI1dhOSJVeuo6n/n1Vbhsx8wIQjcG2TXYFqTyMPPpkOS6m1k4NKSad64 nyVmFMkwMaGE8g9YCSPVh9FQonY0mm0aDhJvtpv7MD13tsD2E1BkO5/jZ/IL7h9fFBz4 hGKo5e7Glo/rQJPyCjNI9ovoYw1bvA/GyIYyX3TLZ4gbc1QcJOtmFACzkCQsHejrVVPW cOww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=qEPEUGMPLAiJKoIvgThM06a9G411AMNSksurikzkqDY=; b=do2Z0zAIV8gPxfxUdyDUif2nQ64bJ61M0sDi2DGce0etAtcuMdvRd4F8Gr/xJVWdCe lpDyaDuU/aw5NaMpnOuB5cwxGFBNp1ffjNHauP7aLE+S554r0f97jjabsZtbVmukRLM2 WQcXxEtp0KjHbTbItBmcKB+LKk9UE39b+oUvtWL2EwboxirdOjKvu7BVJpEFWY9s2vjz wZzbTXZfWIWxBLy/sg+k4MM/MGvUQWiY2aKa+LuLK3oO258mBzv17xHrLtCOOKmdo5Gz WDNiUE8klQWQBkBAghCRq5SgzCJCKZECHGDWo99XvozjhnbvR3LDhoF0LiR+d0W9C7uF xIzw== ARC-Authentication-Results: i=1; mx.google.com; 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 t12si6676461eji.166.2021.05.14.12.34.28; Fri, 14 May 2021 12:34:52 -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; 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 S234936AbhENP5A (ORCPT + 99 others); Fri, 14 May 2021 11:57:00 -0400 Received: from mx2.suse.de ([195.135.220.15]:58768 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231503AbhENP47 (ORCPT ); Fri, 14 May 2021 11:56:59 -0400 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 >