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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1D3DEFA373D for ; Fri, 21 Oct 2022 16:37:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 773108E0012; Fri, 21 Oct 2022 12:37:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6D7ED8E0001; Fri, 21 Oct 2022 12:37:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 48D9F8E0012; Fri, 21 Oct 2022 12:37:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 2B9B18E0001 for ; Fri, 21 Oct 2022 12:37:32 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 0F50281081 for ; Fri, 21 Oct 2022 16:37:32 +0000 (UTC) X-FDA: 80045512344.03.F9FB303 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf06.hostedemail.com (Postfix) with ESMTP id 86BDA18003F for ; Fri, 21 Oct 2022 16:37:31 +0000 (UTC) Received: by mail-yb1-f202.google.com with SMTP id w190-20020a257bc7000000b006c0d1b19526so3747939ybc.12 for ; Fri, 21 Oct 2022 09:37:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=fsyIlVnnMiNXvkl1HsXqgX1TLgLGQ8zT/P/5weg/1gI=; b=XqorezT/2FphyiQnAX1Bd8wgsl7lfI/pP/ncB+cOJZKqD23STzv16ftCYUCPWHqtg5 bpBlu8K4trcZPPP7ksTIPwi+OmxSmuA+bx2JDQ4xMN1Sza2xhzZIri2t3uZC3siOgf1N 6PkInLp3do3ZY7Mr0m9JvaUW9Fio86INZMzBpW/c8ze+LCXgeqrdcTQFLLGy0UJLJ7rx r5nPuIKCiA9//Lvg3JPH/D3IUupmV0+whrn4/c2BWlYGrhrlNQ3nh+aOXJrWK/llyVpr 73/3Br83UOiGnA3b2tybFdHzcf9rRgS7SFsJnN0uyja6ZNBNeDdPNMYCpV/KSWK8w9CV O7AQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=fsyIlVnnMiNXvkl1HsXqgX1TLgLGQ8zT/P/5weg/1gI=; b=YXE9ZA59zn+hF5dZgZJLMNM0yOWzqBisRuQgzegyeOY/fdTSgy5XR+grxTHasDcwC3 yfdFewwYHsPXhFuXykboeSjAv+WJtkZz91jSdT8gNeBVP2xCXuP82jQE2/ZdLP97AkL9 3+bxNfO4IHNu+anTEBMQBYIz4TXBHMpAzmHcFrsWklfn4sl0SuSedkXO2AjJ0GtxBVpA 9wESgjBTDEBm9Ch7Wb4CSzwGgU76Xa8+k9A0NPQrp8iYvxgVqAx71C0mN9l4yEWDKtZJ 9UbNqS7SgO4ese8rzTI9DdU0SgIAn7AtvZ9Y84gUcErj8n914EHcp8xfZ8cGRl0SmiUn pJcQ== X-Gm-Message-State: ACrzQf0DlEsB440BdHetE1aoNEw+ZbaisaPnX+i64McTiP/B6ItDN8RT CxcsePRKeqAuwohAI8D15BnunCSBidX0U7j/ X-Google-Smtp-Source: AMsMyM4BD94+VbGPR4g7N7cSR0mTVCvwlFZLyZIhchhPIXP/hu+fs3VOPw61nlbFmAsQysXWmSvESfUZFYZzAMKD X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a05:6902:70f:b0:6ca:7254:c2ea with SMTP id k15-20020a056902070f00b006ca7254c2eamr3096670ybt.476.1666370250766; Fri, 21 Oct 2022 09:37:30 -0700 (PDT) Date: Fri, 21 Oct 2022 16:36:31 +0000 In-Reply-To: <20221021163703.3218176-1-jthoughton@google.com> Mime-Version: 1.0 References: <20221021163703.3218176-1-jthoughton@google.com> X-Mailer: git-send-email 2.38.0.135.g90850a2211-goog Message-ID: <20221021163703.3218176-16-jthoughton@google.com> Subject: [RFC PATCH v2 15/47] hugetlbfs: for unmapping, treat HGM-mapped pages as potentially mapped From: James Houghton To: Mike Kravetz , Muchun Song , Peter Xu Cc: David Hildenbrand , David Rientjes , Axel Rasmussen , Mina Almasry , "Zach O'Keefe" , Manish Mishra , Naoya Horiguchi , "Dr . David Alan Gilbert" , "Matthew Wilcox (Oracle)" , Vlastimil Babka , Baolin Wang , Miaohe Lin , Yang Shi , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, James Houghton Content-Type: text/plain; charset="UTF-8" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1666370251; a=rsa-sha256; cv=none; b=Zrbuf4d1yXHTla5/QDKQw+moGOWkgO5db7QDtWW6aTzrpx5LbKJ/SH2plwDvYqzaNT7IrC Wpp5c1VvFBHUzTAwvtMH9VR3NY5IMvNEmltrX7g9kNK0e+hbmpkjuj9rLRv7GgfHuCfI8D Xe4COTOA0oKR3KXnA+GPzj2hKN1QO7o= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="XqorezT/"; spf=pass (imf06.hostedemail.com: domain of 3yspSYwoKCMs0Ay5BxyA54x55x2v.t532z4BE-331Crt1.58x@flex--jthoughton.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3yspSYwoKCMs0Ay5BxyA54x55x2v.t532z4BE-331Crt1.58x@flex--jthoughton.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1666370251; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=fsyIlVnnMiNXvkl1HsXqgX1TLgLGQ8zT/P/5weg/1gI=; b=fiHT+0KjAeFartBkgMh+LNAlyX8YGoyLRddozQPnRIDqdA2wJTRnMk77zHvKMC6mf1czXz fQFjTDb6QRaJl8pZ7CmSq3VAay0/A2cD1JNBPFA+tFn+LTksL91JTndDhBeCTvXK5Hh7vq yHwnKe4ZeeVrfrLXt7IaN2XRAV8poVc= X-Stat-Signature: qcyeu54u5rtrjoy8t8gdmbwiwe6813ej X-Rspamd-Queue-Id: 86BDA18003F Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="XqorezT/"; spf=pass (imf06.hostedemail.com: domain of 3yspSYwoKCMs0Ay5BxyA54x55x2v.t532z4BE-331Crt1.58x@flex--jthoughton.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3yspSYwoKCMs0Ay5BxyA54x55x2v.t532z4BE-331Crt1.58x@flex--jthoughton.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1666370251-788362 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: hugetlb_vma_maps_page was mostly used as an optimization: if the VMA isn't mapping a page, then we don't have to attempt to unmap it again. We are still able to call the unmap routine if we need to. For high-granularity mapped pages, we can't easily do a full walk to see if the page is actually mapped or not, so simply return that it might be. Signed-off-by: James Houghton --- fs/hugetlbfs/inode.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index 7f836f8f9db1..a7ab62e39b8c 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -383,21 +383,34 @@ static void hugetlb_delete_from_page_cache(struct folio *folio) * mutex for the page in the mapping. So, we can not race with page being * faulted into the vma. */ -static bool hugetlb_vma_maps_page(struct vm_area_struct *vma, - unsigned long addr, struct page *page) +static bool hugetlb_vma_maybe_maps_page(struct vm_area_struct *vma, + unsigned long addr, struct page *page) { pte_t *ptep, pte; + struct hugetlb_pte hpte; + struct hstate *h = hstate_vma(vma); - ptep = huge_pte_offset(vma->vm_mm, addr, - huge_page_size(hstate_vma(vma))); + ptep = huge_pte_offset(vma->vm_mm, addr, huge_page_size(h)); if (!ptep) return false; + hugetlb_pte_populate(&hpte, ptep, huge_page_shift(h), + hpage_size_to_level(huge_page_size(h))); + pte = huge_ptep_get(ptep); if (huge_pte_none(pte) || !pte_present(pte)) return false; + if (!hugetlb_pte_present_leaf(&hpte, pte)) + /* + * The top-level PTE is not a leaf, so it's possible that a PTE + * under us is mapping the page. We aren't holding the VMA + * lock, so it is unsafe to continue the walk further. Instead, + * return true to indicate that we might be mapping the page. + */ + return true; + if (pte_page(pte) == page) return true; @@ -457,7 +470,8 @@ static void hugetlb_unmap_file_folio(struct hstate *h, v_start = vma_offset_start(vma, start); v_end = vma_offset_end(vma, end); - if (!hugetlb_vma_maps_page(vma, vma->vm_start + v_start, page)) + if (!hugetlb_vma_maybe_maps_page(vma, vma->vm_start + v_start, + page)) continue; if (!hugetlb_vma_trylock_write(vma)) { @@ -507,7 +521,8 @@ static void hugetlb_unmap_file_folio(struct hstate *h, */ v_start = vma_offset_start(vma, start); v_end = vma_offset_end(vma, end); - if (hugetlb_vma_maps_page(vma, vma->vm_start + v_start, page)) + if (hugetlb_vma_maybe_maps_page(vma, vma->vm_start + v_start, + page)) unmap_hugepage_range(vma, vma->vm_start + v_start, v_end, NULL, ZAP_FLAG_DROP_MARKER); -- 2.38.0.135.g90850a2211-goog