Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3911500pxj; Tue, 11 May 2021 14:57:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz3ObfI60XyTdE2VuGmejkpnCTLvo8s1vKWK+z2IeEGb932pkWuTAA1A+dSLdksSWAwrqOv X-Received: by 2002:a54:4395:: with SMTP id u21mr23579880oiv.174.1620770258722; Tue, 11 May 2021 14:57:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620770258; cv=none; d=google.com; s=arc-20160816; b=yO3n/M181uZXLe0cV9xpPs0MAAdFVEaWNIWbXIqWclkk66L2venq/rcfHp3W8rMrix 62H1t4asOz8xJwUCTvPhD0puNOZilymMfhbRPA6HqBVulAWgZo4Hz5/AKD8yTwrUkU3e GdK31GgKholh6S4AQwjpGRUH7BGGJ1tDZvRxVIAe9hRJY1Cgtof8WEtt9pEt2hrjQd2v +0D3mENyU18hlkHD3xM+mwdlihFteC9iad8KOEeXRVZ9j/3dSex2z29Erjj5CjS+u8/y PafahApqIT8PZSg7kYLKPUzoPckNAw951yVIqNeMs3te4q6TnKnmlqrcqfVhYXkw5fYL 0dTA== 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=OtWUjgqcTEsxXDdktE99RbdTTyPFQpVk5HBKDO7qeos=; b=Lxbn7SoREgl0Guv871YB9cRW8YoddANltm4Xwu0+Q+5319yyx0Gsry1WLCSN7U7sB9 LFDTy5GWqcP7Mx4aSNTw+vAPbc6rtL+ZjTUAd+P4cEHDhEq+Kut49hMZ/OH/3PXqC/9B 6xD21IVh4/2WwzmewX49iUItQlLi4UWY9kYiz1VgsnsBIr8aIESEAWYRizwdVkvQgotn FZGgNviyt95Z3Iv2nxWyXyUmIes1QO3rT1gT4K5nV/jC6FQhxnlDswxZYEl6nVskcqMn tQMUsxY4CDyr2gZpZQJDnGit0fpvJzoLhV9LIIlK4JlhbkAYDzLhjPD/aeV1uOdyLPH4 QLJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=QZVpNLJq; 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 c185si18944698oif.214.2021.05.11.14.57.25; Tue, 11 May 2021 14:57:38 -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; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=QZVpNLJq; 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 S229920AbhEKV5f (ORCPT + 99 others); Tue, 11 May 2021 17:57:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37504 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229637AbhEKV5e (ORCPT ); Tue, 11 May 2021 17:57:34 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AA457C061574; Tue, 11 May 2021 14:56:27 -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=OtWUjgqcTEsxXDdktE99RbdTTyPFQpVk5HBKDO7qeos=; b=QZVpNLJqHy+FyZb3dVkRwxshK4 WS7jih8MxKzsqGZwD4nkuwbygX5tX9COID0wYz6RgDQk06fvOjIww6AMWPDGiLuKaTqC7JRLMWQ7M b+phSXAYoosvoE0eY/RZlinlU6CozjiBgF0mlIxAkIWjb/job1RsIdMDB8660Pw31rsboypIuEc1I KRdwkInNJxcUSMnPVOcpZhmk80bnrBcz8uyF+EU6GCHGqd4wgb9qjlcl5w5ILhpx17tWIiwWmQqlG Pqk/BBIh91cZQjdHJsz5lAFmizhnJDkK8+Kv9ETKh9tNbN6JxjyNNCxmXW97th1CVjA2k++DymG7b Wsx9Llog==; Received: from willy by casper.infradead.org with local (Exim 4.94 #2 (Red Hat Linux)) id 1lgaLZ-007i6Y-Hw; Tue, 11 May 2021 21:55:32 +0000 From: "Matthew Wilcox (Oracle)" To: akpm@linux-foundation.org Cc: "Matthew Wilcox (Oracle)" , linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Christoph Hellwig , Jeff Layton Subject: [PATCH v10 12/33] mm/filemap: Add folio_index, folio_file_page and folio_contains Date: Tue, 11 May 2021 22:47:14 +0100 Message-Id: <20210511214735.1836149-13-willy@infradead.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210511214735.1836149-1-willy@infradead.org> References: <20210511214735.1836149-1-willy@infradead.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org folio_index() is the equivalent of page_index() for folios. folio_file_page() is the equivalent of find_subpage(). folio_contains() is the equivalent of thp_contains(). 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 -- 2.30.2