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=-3.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=no 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 EB81DC433E1 for ; Thu, 27 Aug 2020 19:14:12 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id AF9DB20707 for ; Thu, 27 Aug 2020 19:14:12 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="e2bsFLEa" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org AF9DB20707 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 5C8ED6B0002; Thu, 27 Aug 2020 15:14:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 579D76B0003; Thu, 27 Aug 2020 15:14:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 467D66B0006; Thu, 27 Aug 2020 15:14:12 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0151.hostedemail.com [216.40.44.151]) by kanga.kvack.org (Postfix) with ESMTP id 317326B0002 for ; Thu, 27 Aug 2020 15:14:12 -0400 (EDT) Received: from smtpin10.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id E60032C1E for ; Thu, 27 Aug 2020 19:14:11 +0000 (UTC) X-FDA: 77197299102.10.blow26_540b4482706f Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin10.hostedemail.com (Postfix) with ESMTP id A3B2316A0C3 for ; Thu, 27 Aug 2020 19:14:11 +0000 (UTC) X-HE-Tag: blow26_540b4482706f X-Filterd-Recvd-Size: 2851 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf18.hostedemail.com (Postfix) with ESMTP for ; Thu, 27 Aug 2020 19:14:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:MIME-Version:Message-ID: Subject:To:From:Date:Sender:Reply-To:Cc:Content-Transfer-Encoding:Content-ID: Content-Description:In-Reply-To:References; bh=KbRMDfTQA/loBOHCA4nEVOH3inbrAjkK/cdr1dnfa0g=; b=e2bsFLEauIo5e9D68FGbxPju1n AnNmnpcSds7F/mGqIXAKqAabagFyNlJAKiGrc7ZSeJRc5itRk2n5WsX5DJpGJtmwnfCODqOcHSULG kdukP3LSK1egUrzS6cpj2+1uqVGbi4lf0Six3llD1BvJW1hl3ri5WCoTM+3I9uRZBfz/nSWjhjs7q Bvk8WoBxxreXS9nAo3o4e7vlfh4w7pZc7yJudCock+g2QIN4HZnsLo4i4vdT1vkL/PbOc+GnI1WI/ P0QKTYD0YnXWR7UHGx4F6qiV3sXlVCCAALqpe9wlZEn/YDyrCYrbZ3lDju3zbBxbf/wxy6l7Uo4bB C89hVzPg==; Received: from willy by casper.infradead.org with local (Exim 4.92.3 #3 (Red Hat Linux)) id 1kBNLb-0004Mw-WB for linux-mm@kvack.org; Thu, 27 Aug 2020 19:14:08 +0000 Date: Thu, 27 Aug 2020 20:14:07 +0100 From: Matthew Wilcox To: linux-mm@kvack.org Subject: When is page->index stable? Message-ID: <20200827191407.GM14765@casper.infradead.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Rspamd-Queue-Id: A3B2316A0C3 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam01 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: We have a number of places where we look up a page in the page cache, lock it, then have some kind of assertion that we got back the page we asked for, eg filemap_fault(): page = find_get_page(mapping, offset); ... if (!lock_page_maybe_drop_mmap(vmf, page, &fpin)) goto out_retry; ... VM_BUG_ON_PAGE(page_to_pgoff(page) != offset, page); but today I noticed this in shmem_undo_range(): pvec.nr = find_get_entries(mapping, index, min(end - index, (pgoff_t)PAGEVEC_SIZE), pvec.pages, indices); ... VM_BUG_ON_PAGE(page_to_pgoff(page) != index, page); ... if (!trylock_page(page)) continue; So is page->index stable if we have a refcount on the page, or is a lock on the page required? A refcount on the page prevents it from being split or freed. And there's plenty of comments along the lines of: mm/filemap.c: /* Leave page->index set: truncation lookup relies on it */ which indicates that once a page is removed from the page cache, its index remains reliable (until it's freed). It might be nice to remove all these assertions from the callers and bury them down in find_get_(entry,page,entries,...), but we can't do that if we need the lock to check the index. If we don't need the lock, then it should be safe to check as soon as we've checked that page == xas_reload().