From patchwork Sat Feb 18 00:27:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13145385 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 844E2C636D6 for ; Sat, 18 Feb 2023 00:29:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F33C028000F; Fri, 17 Feb 2023 19:29:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E6C24280002; Fri, 17 Feb 2023 19:29:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C956A28000F; Fri, 17 Feb 2023 19:29:04 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id BB0C2280002 for ; Fri, 17 Feb 2023 19:29:04 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 9DC831C5F4C for ; Sat, 18 Feb 2023 00:29:04 +0000 (UTC) X-FDA: 80478527808.20.8276037 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf08.hostedemail.com (Postfix) with ESMTP id DBAB9160009 for ; Sat, 18 Feb 2023 00:29:02 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=tIIvrAMR; spf=pass (imf08.hostedemail.com: domain of 3zRvwYwoKCOoVfTagSTfaZSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--jthoughton.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3zRvwYwoKCOoVfTagSTfaZSaaSXQ.OaYXUZgj-YYWhMOW.adS@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=1676680142; 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=+tSPnCkiv+4IG9wAhN3nJiAPBMdRQ7SRBdMiKhfpNi8=; b=KL5jurEs8El65elTeycif3nyQ3b3kmF2wsUbaQ5SNlif1orzSbqqSZ6cZmJFpr1atsQFx2 1xMQK2BtlS+9KBJ39KW5M6zHWyV2FrlIZhlNLz9pp88HO2qHQsrjFsjRpVz0YwbOEHMVtt TamTIEFdkGglR/dEkPIp6Ede0cn/zyc= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=tIIvrAMR; spf=pass (imf08.hostedemail.com: domain of 3zRvwYwoKCOoVfTagSTfaZSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--jthoughton.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3zRvwYwoKCOoVfTagSTfaZSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--jthoughton.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676680142; a=rsa-sha256; cv=none; b=V0IDt6ZzAvAlGK5WOkw8vFuu6rkIEDzTnU6C2P4g3WnMwj0fpopttlERxex85/XeUAWwWz K6tqquMVgzvqTlbWvQEnU8zzyrAyKzZfSNIJtPDueHyEMz1o2FOSGvt121dMVOVo8VqDgd Uqud+hU0nUij6c5KC8RgLvmzYCHWb54= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5365a2b9e4fso19055837b3.15 for ; Fri, 17 Feb 2023 16:29:02 -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=+tSPnCkiv+4IG9wAhN3nJiAPBMdRQ7SRBdMiKhfpNi8=; b=tIIvrAMRq+3HXFL5rW8Lh7/gCEy2v7mBrYIzZnmVImsr6rHLUrJkHvESEkKIKnP2HA ETKeSXB6gqBCteIJ68LmS1NgcW8NqFCp4N0/v9eSUb5HNjRwRpHF7O8+s4KVElrP4dI4 Zjvu8aQ6Opgdsvu/yAqPR0lJhv574KFnXRf4LH++FsPR7lEug2Wl9pAFrHWFu0GZq42N Vy0SevF4NJaR0vgAKFGoEdFDQKl01vAhAwbwFzmxhSRiOGi2HQEkv51SNB+xC5ZLD4Tm F1M7gD13G/x1ohwgsWJjFr2DhGgQpTZDe/17U0ZGQKm1J8xgGRPTne6Pgw+hiXkqL9v1 Pi9w== 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=+tSPnCkiv+4IG9wAhN3nJiAPBMdRQ7SRBdMiKhfpNi8=; b=LCIh0eIFzs/hGwsZgLVk/wcN1oSZYUCxEOzIFyFS1Z4vWw8kt74tSUEA/w5AmRc6yH 82sX97YnrK2AdB+FQZYGqQBtdQlCRSipeboo/z4mXFPgIOmrIJszbu8ZKvoJB6BCKBy2 7tc7pNmz0PnOq1W2zU2YIVTUi+gCNxpYn/pcpzH1K1n2fYb0Go9DbW+g3wsY6ZASwIYZ l4Oo+g24e/el74FA86CG4Mso3VPEwcfCw8AH9tba+0lRTjuff2+BwkEn7q4f+dYTzayb rXjlnwlAo5CLg39xuKYP+RJxCI3H5zpsqywfU1oOKhaYFjRll/tP/Fiz82m4WZ/jKGx6 om9g== X-Gm-Message-State: AO0yUKUvLPOfWp9bIlEaMz9JimS0M6fvapxWnqo0UF1xGU5KqN/J4qr3 yRaMbcsXVoG8hj5L+YmEykhiLg9t/3tG9HDe X-Google-Smtp-Source: AK7set8J1uc8tp8/hEWZItT5UxOZqGWxRV48NY6+J5XvomWx6cLnGWfxbZYO/6vWe6Thx7C5x8jbEtY4jfNJnPC8 X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a81:46d6:0:b0:52e:e3a8:d0b9 with SMTP id t205-20020a8146d6000000b0052ee3a8d0b9mr1575870ywa.509.1676680141961; Fri, 17 Feb 2023 16:29:01 -0800 (PST) Date: Sat, 18 Feb 2023 00:27:52 +0000 In-Reply-To: <20230218002819.1486479-1-jthoughton@google.com> Mime-Version: 1.0 References: <20230218002819.1486479-1-jthoughton@google.com> X-Mailer: git-send-email 2.39.2.637.g21b0678d19-goog Message-ID: <20230218002819.1486479-20-jthoughton@google.com> Subject: [PATCH v2 19/46] hugetlb: add HGM support to hugetlb_change_protection From: James Houghton To: Mike Kravetz , Muchun Song , Peter Xu , Andrew Morton 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 , Frank van der Linden , Jiaqi Yan , linux-mm@kvack.org, linux-kernel@vger.kernel.org, James Houghton X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: DBAB9160009 X-Stat-Signature: bsqwm95fz4uqjfshj37wamu3g5m197ij X-Rspam-User: X-HE-Tag: 1676680142-651822 X-HE-Meta: U2FsdGVkX18RkxvduM0HRfb5HEsFrE+9jC7d+y2nsPFhUoqJBzacdLaYWQsZyEd8ZCCbvjBi2SmKwi0mRTsmiPcy170NIXnvWExgaD4lAzTG8HJp0o+5J7wmqHRAT98BWefjqhOVdu2Ih5vlcAW7/X2dd7Enc9vdSfeO3O5poahBaenbzC0S38q/2YMR0P1w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uuv8SI9xFGnYlu/upc/lhu1lmP5rmZO1zl7veAcuuWwdPBr6WggK0EcgvDKaXyfqgyxyn2Au/bMu3N9k28UukOOih7GZTCMWhGCGfpMpxmgPtZboEgQjyBhvVbTcxOcs52BnIE+rfq3fbFe728YLbDT3RnJCCLxCKW2kwcGy+/0JV8rGpsoV4sk8VL5mJIPzl9V6IWLUY0UALRE2sJ3ehqVyRCoPw6v/sQdmtZkO+/+n+/u8/EkcKJ9haJ4K0Ybxlby/g9uK6x9oGzpkvh1lRHfWXFGhgfFdDaTcl9+mcteq4YKT9jiddBJzR2lbvLYojmqosUhJQ3aWsr17FldHhMCzRNGYkI+goEHhtaDcHRgNmhI3OoJb3id4kyBdC/rqvcCBLJeqklygXAVtDgZCi2spNLB8= 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 diff --git a/mm/hugetlb.c b/mm/hugetlb.c index ecf1a28dbaaa..7321c6602d6f 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6900,15 +6900,15 @@ 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); - long pages = 0, psize = huge_page_size(h); + 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 @@ -6926,39 +6926,43 @@ 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) { + if (hugetlb_full_walk(&hpte, vma, address)) { if (!uffd_wp) { - address |= last_addr_mask; + address = (address | last_addr_mask) + psize; continue; } /* * Userfaultfd wr-protect requires pgtable * pre-allocations to install pte markers. + * + * Use hugetlb_full_walk_alloc to allocate + * the hstate-level PTE. */ - ptep = huge_pte_alloc(mm, vma, address, psize); - if (!ptep) { - pages = -ENOMEM; + if (hugetlb_full_walk_alloc(&hpte, vma, + address, psize)) { + base_pages = -ENOMEM; break; } } - 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))) { @@ -6974,7 +6978,7 @@ 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) @@ -6982,34 +6986,49 @@ 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; + + 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, ptep); + 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 @@ -7032,7 +7051,7 @@ long hugetlb_change_protection(struct vm_area_struct *vma, hugetlb_vma_unlock_write(vma); mmu_notifier_invalidate_range_end(&range); - return pages > 0 ? (pages << h->order) : pages; + return base_pages; } /* Return true if reservation was successful, false otherwise. */