From patchwork Thu Jan 5 10:18:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13089652 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 88B5AC3DA7D for ; Thu, 5 Jan 2023 10:19:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B655B94000F; Thu, 5 Jan 2023 05:19:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B16CB940008; Thu, 5 Jan 2023 05:19:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9B70694000F; Thu, 5 Jan 2023 05:19:24 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 8A74E940008 for ; Thu, 5 Jan 2023 05:19:24 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 6CE1F1A0786 for ; Thu, 5 Jan 2023 10:19:24 +0000 (UTC) X-FDA: 80320348248.29.ADC1C8B Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf27.hostedemail.com (Postfix) with ESMTP id D81C64000B for ; Thu, 5 Jan 2023 10:19:22 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=gcqO4fum; spf=pass (imf27.hostedemail.com: domain of 3KqS2YwoKCHEYiWdjVWidcVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--jthoughton.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3KqS2YwoKCHEYiWdjVWidcVddVaT.RdbaXcjm-bbZkPRZ.dgV@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=1672913962; 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=lp3NkrzzsDy9ah3gYP7Di6ebFhCh/hI0r9EhbhKs3qA=; b=p1qK7ykZm+OOi5OC5ymuy8D4MeYiOIftSle6byDtyKhbdSoW3yaQDwfWcZ+LOGUgMN2R94 +lYIh1+mlXlbhw9Dztb1rpsM8Bph/Vgh4h7x5DhZOVlzM3wMbtbktdMi9ieERuzOsBor5V cxbv87HaAJ3fn5XyUxNfEbFV7J4tH+M= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=gcqO4fum; spf=pass (imf27.hostedemail.com: domain of 3KqS2YwoKCHEYiWdjVWidcVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--jthoughton.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3KqS2YwoKCHEYiWdjVWidcVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--jthoughton.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1672913962; a=rsa-sha256; cv=none; b=AkmQ4xNYW+at+SO/LesSab1uLd8oSJz4dU7X1oqeMrayMbahOZUN/vaZxT0rbSsdqnvUQ3 jN0yhNzlafdd3PIO3v1X3Cxl8kuNwG4nnOlEg9pz665FZ1/Zw2FcKc9pQQ0D0R59Dk4S4W OTeFPazgjmukM9x0BZyPeaHEyF6NJXU= Received: by mail-yb1-f201.google.com with SMTP id t13-20020a056902018d00b0074747131938so36503692ybh.12 for ; Thu, 05 Jan 2023 02:19:22 -0800 (PST) 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=lp3NkrzzsDy9ah3gYP7Di6ebFhCh/hI0r9EhbhKs3qA=; b=gcqO4fumRx0zgQZPFIbbvhj2RDn3uzOiEjenn5HSn/KoUqOlS6cvCBPlQZqdnokXAv yuBgbgVdZ68dWkcgWO302Mw9PxijLJ23xw6JxoY2ObqFFf3QxAWs1BdbmgNwKsp6kjEu 8GFqq0LZlvusyfldGmkc6FfTZZ/RbptJV8SUrxL5hGv7ZFxVYZY2rxA32ezUN4HQKndZ n4NkO1TfQAKi7YjlKvn942sD0+vyg8DNgNqttkJLun2/ieyUyjjedHWOdKf/Toc3RDXd NYkM9a5g7CgZdcmSy/hZ0BvTwMl6CAeI0KwuHwV/HKzTEVBfjxeq4ELE0Z8GmCd1b9Ya 6emg== 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=lp3NkrzzsDy9ah3gYP7Di6ebFhCh/hI0r9EhbhKs3qA=; b=YvPHqia7EMVOCNsEKk8Uo7VfvIyn8GZiB7yIJExDwcaK2KnHlnteQY0w7KwOc1rFQT xnXYKpa5IpT2YgRn55EgMdwBr/c7RVQGske7nb/55eKyuBlJQYbMl1ag0Te0VE6GEtEd xHv5kGBbWWbBBlOAoP0T/4PltLyZlO9Jte8mqoj+XYL5MRyHkinUHNhm+l4jSCwX6wRO JcItiZisS509s5ctbTe9g7muIuH3xuG6tYZZ1TaTFzi4vF9fhfQ3N9a2W30e6jR9PREf R9IBgFFsjpZ29JX2HN34kCHgFIQt3K+PbhIFW9VUIF6h6ThTvosN53hTCzmGYxGv1/xj o9SA== X-Gm-Message-State: AFqh2kogBm0YZvDqOHQeZTEHVW8voLAUMoJcqQTzm0M866RnqmIrOGpf TY4uilBuGZei7v4i5jPwJjrCdXV1LEWoE8Bq X-Google-Smtp-Source: AMrXdXtIJM/w1a8XxNEazXxZ6FOVAPFAuY6o16gkUOQyGBaWZ7iAlnjKjopmkNnTtIe8ZRhqObJ/lwIhr3EKSgQ+ X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a25:13c2:0:b0:716:10cb:dc2e with SMTP id 185-20020a2513c2000000b0071610cbdc2emr6990557ybt.530.1672913962037; Thu, 05 Jan 2023 02:19:22 -0800 (PST) Date: Thu, 5 Jan 2023 10:18:16 +0000 In-Reply-To: <20230105101844.1893104-1-jthoughton@google.com> Mime-Version: 1.0 References: <20230105101844.1893104-1-jthoughton@google.com> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog Message-ID: <20230105101844.1893104-19-jthoughton@google.com> Subject: [PATCH 18/46] hugetlb: add HGM support for hugetlb_change_protection 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 X-Rspamd-Queue-Id: D81C64000B X-Stat-Signature: 7p9q5qjjs8ciearrnw9qaq95t7zmy3ef X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1672913962-783655 X-HE-Meta: U2FsdGVkX18SKco89E1qIXY8j36Xqm3Q843g/2ZLLUrEeJ4dUyeE7KrJtYVooBfGg+uuFjPJSqaiBzZYgd4CqOxZ7cSWmT052qxLeXMry9zBF8da3H+4RcNmZBCxkH9nZnEmdddX9u7dQZBpc+XGOQBPb7liNaF5p0WxSLAOAxLDn/G3joBwoAGNiYjzsEhbmjzmrUIIIi6INUa9RkYuF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2o9NONGwVUbww5Rd7fZQFd1ASa3Y4VkaWqJdfachVcWHPyPlws0F16NE15HWjow7HB9+TWSwYWwZ0eebwd1XIjen7oQO3l6rgf5NmgTJBlqQS2dtAShoILSY1YwSWOcSyewAsXd/FOYCZBYxoq0Rjcyn2No 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: The main change here is to do a high-granularity walk and pulling the shift from the walk (not from the hstate). Signed-off-by: James Houghton --- mm/hugetlb.c | 59 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index dfd6c1491ac3..73672d806172 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6798,15 +6798,15 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma, { struct mm_struct *mm = vma->vm_mm; unsigned long start = address; - pte_t *ptep; pte_t pte; struct hstate *h = hstate_vma(vma); - unsigned long pages = 0, psize = huge_page_size(h); + unsigned long base_pages = 0, psize = huge_page_size(h); bool shared_pmd = false; struct mmu_notifier_range range; unsigned long last_addr_mask; bool uffd_wp = cp_flags & MM_CP_UFFD_WP; bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE; + struct hugetlb_pte hpte; /* * In the case of shared PMDs, the area to flush could be beyond @@ -6824,28 +6824,30 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma, hugetlb_vma_lock_write(vma); i_mmap_lock_write(vma->vm_file->f_mapping); last_addr_mask = hugetlb_mask_last_page(h); - for (; address < end; address += psize) { + while (address < end) { spinlock_t *ptl; - ptep = hugetlb_walk(vma, address, psize); - if (!ptep) { - address |= last_addr_mask; + + if (hugetlb_full_walk(&hpte, vma, address)) { + address = (address | last_addr_mask) + psize; continue; } - ptl = huge_pte_lock(h, mm, ptep); - if (huge_pmd_unshare(mm, vma, address, ptep)) { + + ptl = hugetlb_pte_lock(&hpte); + if (hugetlb_pte_size(&hpte) == psize && + huge_pmd_unshare(mm, vma, address, hpte.ptep)) { /* * When uffd-wp is enabled on the vma, unshare * shouldn't happen at all. Warn about it if it * happened due to some reason. */ WARN_ON_ONCE(uffd_wp || uffd_wp_resolve); - pages++; + base_pages += psize / PAGE_SIZE; spin_unlock(ptl); shared_pmd = true; - address |= last_addr_mask; + address = (address | last_addr_mask) + psize; continue; } - pte = huge_ptep_get(ptep); + pte = huge_ptep_get(hpte.ptep); if (unlikely(is_hugetlb_entry_hwpoisoned(pte))) { /* Nothing to do. */ } else if (unlikely(is_hugetlb_entry_migration(pte))) { @@ -6861,7 +6863,7 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma, entry = make_readable_migration_entry( swp_offset(entry)); newpte = swp_entry_to_pte(entry); - pages++; + base_pages += hugetlb_pte_size(&hpte) / PAGE_SIZE; } if (uffd_wp) @@ -6869,34 +6871,49 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma, else if (uffd_wp_resolve) newpte = pte_swp_clear_uffd_wp(newpte); if (!pte_same(pte, newpte)) - set_huge_pte_at(mm, address, ptep, newpte); + set_huge_pte_at(mm, address, hpte.ptep, newpte); } else if (unlikely(is_pte_marker(pte))) { /* No other markers apply for now. */ WARN_ON_ONCE(!pte_marker_uffd_wp(pte)); if (uffd_wp_resolve) /* Safe to modify directly (non-present->none). */ - huge_pte_clear(mm, address, ptep, psize); + huge_pte_clear(mm, address, hpte.ptep, + hugetlb_pte_size(&hpte)); } else if (!huge_pte_none(pte)) { pte_t old_pte; - unsigned int shift = huge_page_shift(hstate_vma(vma)); + unsigned int shift = hpte.shift; - old_pte = huge_ptep_modify_prot_start(vma, address, ptep); + if (unlikely(!hugetlb_pte_present_leaf(&hpte, pte))) { + /* + * Someone split the PTE from under us, so retry + * the walk, + */ + spin_unlock(ptl); + continue; + } + + old_pte = huge_ptep_modify_prot_start( + vma, address, hpte.ptep); pte = huge_pte_modify(old_pte, newprot); - pte = arch_make_huge_pte(pte, shift, vma->vm_flags); + pte = arch_make_huge_pte( + pte, shift, vma->vm_flags); if (uffd_wp) pte = huge_pte_mkuffd_wp(pte); else if (uffd_wp_resolve) pte = huge_pte_clear_uffd_wp(pte); - huge_ptep_modify_prot_commit(vma, address, ptep, old_pte, pte); - pages++; + huge_ptep_modify_prot_commit( + vma, address, hpte.ptep, + old_pte, pte); + base_pages += hugetlb_pte_size(&hpte) / PAGE_SIZE; } else { /* None pte */ if (unlikely(uffd_wp)) /* Safe to modify directly (none->non-present). */ - set_huge_pte_at(mm, address, ptep, + set_huge_pte_at(mm, address, hpte.ptep, make_pte_marker(PTE_MARKER_UFFD_WP)); } spin_unlock(ptl); + address += hugetlb_pte_size(&hpte); } /* * Must flush TLB before releasing i_mmap_rwsem: x86's huge_pmd_unshare @@ -6919,7 +6936,7 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma, hugetlb_vma_unlock_write(vma); mmu_notifier_invalidate_range_end(&range); - return pages << h->order; + return base_pages; } /* Return true if reservation was successful, false otherwise. */