Message ID | 20230218002819.1486479-13-jthoughton@google.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp142437wrn; Fri, 17 Feb 2023 16:30:40 -0800 (PST) X-Google-Smtp-Source: AK7set8uxK3r2kqrpRSBiGnpHCAY9FXrjQXoqWI7qm53Y4FfxO8FPjA4fWp9TO2bAU7MGIVT2fNX X-Received: by 2002:a17:906:9688:b0:8b1:32b0:2a24 with SMTP id w8-20020a170906968800b008b132b02a24mr1426267ejx.47.1676680240610; Fri, 17 Feb 2023 16:30:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676680240; cv=none; d=google.com; s=arc-20160816; b=O7hjP5/fjgqf9CdQMn4CXqx/cT+n3297VdyG+mcMLbQMsaR+FahuMQSSI31MxKflpg DuXaS7uZdjU7o45JDurcqG4TCezy2TuFBH7cqT4EhOmrugEFOzVGa6OLZ+aQFLpUJ0La +rbCYE/2J7N79LO/A8+Z3TMfjQOzrGkveR2Bb8IxmTjk+TcnguIcEkSGQNaFfZeqOWvv qndjNelWrLpfqh5AN7e1beg8EVso+DRJzOQ1Jfhg8OgZGhcgFQEDuhyN2ZDcDw3u32cw 5sdmx9pMFM9n84Fq/a5ZXokn/+KIeJgp3KYEMa6KQRD0QLOizQH6c90ZrANqUbq2WQOa /Oxg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=Coc3VejVpx2o70gw2F1FMLmVPB7+pJmCD4QBd1TWaEQ=; b=Y+/mEwyMQBd2/z8eHmQMgW8H6Lj/2bDEJPqwiUoAl3Pg19oNeEeMnXcc3Udo/+zFbl GbPAMZpWZESJF3+00yiW5PxqN1NpjP3tIqK6Dug3yqqV05fzuvuCw+PLgZq8/+4M8Ud3 uxQPh8vxMl4EroFWqKTA+4GcLN1LsfdPJl+N+Eow3X0yeymc5xPrIEVl9jR/4/N3AzPG CQ5xoZ6KXMsA831KYr4cv8h1SJNVTZDzKskcUGQnmpeuPbx8nn7d8Y3YkVlbIAyQRQYX GcoDJtmTDLxFQxm+juAD7rApnhpWMpJJWglDIAyZjF4gfyJKzuPOZqbCIP2Q7wxwdyfD NYTQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=b9QeURwu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bp11-20020a170907918b00b008787e4ac0bfsi5548464ejb.719.2023.02.17.16.30.18; Fri, 17 Feb 2023 16:30:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=b9QeURwu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230064AbjBRA3c (ORCPT <rfc822;daweilics@gmail.com> + 99 others); Fri, 17 Feb 2023 19:29:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229694AbjBRA3R (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 17 Feb 2023 19:29:17 -0500 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 118F16ABF3 for <linux-kernel@vger.kernel.org>; Fri, 17 Feb 2023 16:28:56 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5365a2b9e4fso19053897b3.15 for <linux-kernel@vger.kernel.org>; Fri, 17 Feb 2023 16:28:56 -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=Coc3VejVpx2o70gw2F1FMLmVPB7+pJmCD4QBd1TWaEQ=; b=b9QeURwuMOHoR6oMuvaNhmrHn5fZGAuH6iVnLRa9UH1kSasBCwgdba6QJPd+K6jzHb xz6s/6q+eVA72+3bOi37v4IpLi+gt+dDSU4ZiqKrbXd+tRUGRvRJ1mGjZ02I2XMyurj4 SsSE1g1trZVfhuYlrhfBM4w2eQv5qoEpUPlr5CJMDFyaM52fzsGSeRGzxi0rFH4dVLC6 IFd4sxykmk7qNUAT62W1oRvfIgHfE5E1BizMsR8G5eNcCZgGLkwZhgNod0TNXPX+Z62j 6Y1IxEaN7fiBpSKCe960CuuSJGub6psv+J5ih1KSgDonLBFQ4O9QsMNnykmF5WR/o4UG 3CMg== 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=Coc3VejVpx2o70gw2F1FMLmVPB7+pJmCD4QBd1TWaEQ=; b=klwZ9okWf7daMKyeDQ0qFM8L7FDsN5pOwsL7CRHRgZ3SM5JhPzz8QOzOydPwjoACUj 7VXFCi3kyGhJ/jRrOZWSw6ogAjQdaktugZf/lpSroB8MctgELZQvGyiL7jNbYp2emez5 10KCnYsM+OEL+oVtyRjRjZnDc1YxPuHnkjQUgOhsZ13E+ppVw97ZCuFFqrtO9YltPny/ rtxU+99k/BnlmDBAaHpvY4wf7hmkeSRQ3GvPIl6fQWXFsSowC2dJH6x3p2An5L9yALLe N7RuUwpGn3UDSLYPU3UPdaivWXw/GeGnesY12IhtIN17lrJrQhTJf+6fSBr+80UmIlf3 ziPQ== X-Gm-Message-State: AO0yUKXOH+ifzjGYDFbF9mVdSLaY0CypMaP58GQQo72c/iJJTcrXjhom h4kxniod5CGCa2R7DnPUSoThV53CdGmIAxcB X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a81:b705:0:b0:534:d71f:14e6 with SMTP id v5-20020a81b705000000b00534d71f14e6mr53501ywh.9.1676680135521; Fri, 17 Feb 2023 16:28:55 -0800 (PST) Date: Sat, 18 Feb 2023 00:27:45 +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-13-jthoughton@google.com> Subject: [PATCH v2 12/46] hugetlb: add hugetlb_alloc_pmd and hugetlb_alloc_pte From: James Houghton <jthoughton@google.com> To: Mike Kravetz <mike.kravetz@oracle.com>, Muchun Song <songmuchun@bytedance.com>, Peter Xu <peterx@redhat.com>, Andrew Morton <akpm@linux-foundation.org> Cc: David Hildenbrand <david@redhat.com>, David Rientjes <rientjes@google.com>, Axel Rasmussen <axelrasmussen@google.com>, Mina Almasry <almasrymina@google.com>, "Zach O'Keefe" <zokeefe@google.com>, Manish Mishra <manish.mishra@nutanix.com>, Naoya Horiguchi <naoya.horiguchi@nec.com>, "Dr . David Alan Gilbert" <dgilbert@redhat.com>, "Matthew Wilcox (Oracle)" <willy@infradead.org>, Vlastimil Babka <vbabka@suse.cz>, Baolin Wang <baolin.wang@linux.alibaba.com>, Miaohe Lin <linmiaohe@huawei.com>, Yang Shi <shy828301@gmail.com>, Frank van der Linden <fvdl@google.com>, Jiaqi Yan <jiaqiyan@google.com>, linux-mm@kvack.org, linux-kernel@vger.kernel.org, James Houghton <jthoughton@google.com> Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758126659898156037?= X-GMAIL-MSGID: =?utf-8?q?1758126659898156037?= |
Series |
hugetlb: introduce HugeTLB high-granularity mapping
|
|
Commit Message
James Houghton
Feb. 18, 2023, 12:27 a.m. UTC
These functions are used to allocate new PTEs below the hstate PTE. This
will be used by hugetlb_walk_step, which implements stepping forwards in
a HugeTLB high-granularity page table walk.
The reasons that we don't use the standard pmd_alloc/pte_alloc*
functions are:
1) This prevents us from accidentally overwriting swap entries or
attempting to use swap entries as present non-leaf PTEs (see
pmd_alloc(); we assume that !pte_none means pte_present and
non-leaf).
2) Locking hugetlb PTEs can different than regular PTEs. (Although, as
implemented right now, locking is the same.)
3) We can maintain compatibility with CONFIG_HIGHPTE. That is, HugeTLB
HGM won't use HIGHPTE, but the kernel can still be built with it,
and other mm code will use it.
When GENERAL_HUGETLB supports P4D-based hugepages, we will need to
implement hugetlb_pud_alloc to implement hugetlb_walk_step.
Signed-off-by: James Houghton <jthoughton@google.com>
Comments
Hi James, Thank you for the patch! Yet something to improve: [auto build test ERROR on next-20230217] [cannot apply to kvm/queue shuah-kselftest/next shuah-kselftest/fixes arnd-asm-generic/master linus/master kvm/linux-next v6.2-rc8 v6.2-rc7 v6.2-rc6 v6.2-rc8] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch#_base_tree_information] url: https://github.com/intel-lab-lkp/linux/commits/James-Houghton/hugetlb-don-t-set-PageUptodate-for-UFFDIO_CONTINUE/20230218-083216 patch link: https://lore.kernel.org/r/20230218002819.1486479-13-jthoughton%40google.com patch subject: [PATCH v2 12/46] hugetlb: add hugetlb_alloc_pmd and hugetlb_alloc_pte config: powerpc-randconfig-r001-20230217 (https://download.01.org/0day-ci/archive/20230219/202302190142.59JVVPVm-lkp@intel.com/config) compiler: clang version 17.0.0 (https://github.com/llvm/llvm-project db89896bbbd2251fff457699635acbbedeead27f) reproduce (this is a W=1 build): wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # install powerpc cross compiling tool for clang build # apt-get install binutils-powerpc-linux-gnu # https://github.com/intel-lab-lkp/linux/commit/af805b45df0b60a0ef2231f41413b8265e1e8d93 git remote add linux-review https://github.com/intel-lab-lkp/linux git fetch --no-tags linux-review James-Houghton/hugetlb-don-t-set-PageUptodate-for-UFFDIO_CONTINUE/20230218-083216 git checkout af805b45df0b60a0ef2231f41413b8265e1e8d93 # save the config file mkdir build_dir && cp config build_dir/.config COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=powerpc olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=powerpc SHELL=/bin/bash If you fix the issue, kindly add following tag where applicable | Reported-by: kernel test robot <lkp@intel.com> | Link: https://lore.kernel.org/oe-kbuild-all/202302190142.59JVVPVm-lkp@intel.com/ All errors (new ones prefixed by >>): __do_insb ^ arch/powerpc/include/asm/io.h:577:56: note: expanded from macro '__do_insb' #define __do_insb(p, b, n) readsb((PCI_IO_ADDR)_IO_BASE+(p), (b), (n)) ~~~~~~~~~~~~~~~~~~~~~^ In file included from mm/hugetlb.c:11: In file included from include/linux/highmem.h:12: In file included from include/linux/hardirq.h:11: In file included from arch/powerpc/include/asm/hardirq.h:6: In file included from include/linux/irq.h:20: In file included from include/linux/io.h:13: In file included from arch/powerpc/include/asm/io.h:640: arch/powerpc/include/asm/io-defs.h:45:1: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic] DEF_PCI_AC_NORET(insw, (unsigned long p, void *b, unsigned long c), ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ arch/powerpc/include/asm/io.h:637:3: note: expanded from macro 'DEF_PCI_AC_NORET' __do_##name al; \ ^~~~~~~~~~~~~~ <scratch space>:102:1: note: expanded from here __do_insw ^ arch/powerpc/include/asm/io.h:578:56: note: expanded from macro '__do_insw' #define __do_insw(p, b, n) readsw((PCI_IO_ADDR)_IO_BASE+(p), (b), (n)) ~~~~~~~~~~~~~~~~~~~~~^ In file included from mm/hugetlb.c:11: In file included from include/linux/highmem.h:12: In file included from include/linux/hardirq.h:11: In file included from arch/powerpc/include/asm/hardirq.h:6: In file included from include/linux/irq.h:20: In file included from include/linux/io.h:13: In file included from arch/powerpc/include/asm/io.h:640: arch/powerpc/include/asm/io-defs.h:47:1: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic] DEF_PCI_AC_NORET(insl, (unsigned long p, void *b, unsigned long c), ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ arch/powerpc/include/asm/io.h:637:3: note: expanded from macro 'DEF_PCI_AC_NORET' __do_##name al; \ ^~~~~~~~~~~~~~ <scratch space>:104:1: note: expanded from here __do_insl ^ arch/powerpc/include/asm/io.h:579:56: note: expanded from macro '__do_insl' #define __do_insl(p, b, n) readsl((PCI_IO_ADDR)_IO_BASE+(p), (b), (n)) ~~~~~~~~~~~~~~~~~~~~~^ In file included from mm/hugetlb.c:11: In file included from include/linux/highmem.h:12: In file included from include/linux/hardirq.h:11: In file included from arch/powerpc/include/asm/hardirq.h:6: In file included from include/linux/irq.h:20: In file included from include/linux/io.h:13: In file included from arch/powerpc/include/asm/io.h:640: arch/powerpc/include/asm/io-defs.h:49:1: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic] DEF_PCI_AC_NORET(outsb, (unsigned long p, const void *b, unsigned long c), ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ arch/powerpc/include/asm/io.h:637:3: note: expanded from macro 'DEF_PCI_AC_NORET' __do_##name al; \ ^~~~~~~~~~~~~~ <scratch space>:106:1: note: expanded from here __do_outsb ^ arch/powerpc/include/asm/io.h:580:58: note: expanded from macro '__do_outsb' #define __do_outsb(p, b, n) writesb((PCI_IO_ADDR)_IO_BASE+(p),(b),(n)) ~~~~~~~~~~~~~~~~~~~~~^ In file included from mm/hugetlb.c:11: In file included from include/linux/highmem.h:12: In file included from include/linux/hardirq.h:11: In file included from arch/powerpc/include/asm/hardirq.h:6: In file included from include/linux/irq.h:20: In file included from include/linux/io.h:13: In file included from arch/powerpc/include/asm/io.h:640: arch/powerpc/include/asm/io-defs.h:51:1: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic] DEF_PCI_AC_NORET(outsw, (unsigned long p, const void *b, unsigned long c), ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ arch/powerpc/include/asm/io.h:637:3: note: expanded from macro 'DEF_PCI_AC_NORET' __do_##name al; \ ^~~~~~~~~~~~~~ <scratch space>:108:1: note: expanded from here __do_outsw ^ arch/powerpc/include/asm/io.h:581:58: note: expanded from macro '__do_outsw' #define __do_outsw(p, b, n) writesw((PCI_IO_ADDR)_IO_BASE+(p),(b),(n)) ~~~~~~~~~~~~~~~~~~~~~^ In file included from mm/hugetlb.c:11: In file included from include/linux/highmem.h:12: In file included from include/linux/hardirq.h:11: In file included from arch/powerpc/include/asm/hardirq.h:6: In file included from include/linux/irq.h:20: In file included from include/linux/io.h:13: In file included from arch/powerpc/include/asm/io.h:640: arch/powerpc/include/asm/io-defs.h:53:1: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic] DEF_PCI_AC_NORET(outsl, (unsigned long p, const void *b, unsigned long c), ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ arch/powerpc/include/asm/io.h:637:3: note: expanded from macro 'DEF_PCI_AC_NORET' __do_##name al; \ ^~~~~~~~~~~~~~ <scratch space>:110:1: note: expanded from here __do_outsl ^ arch/powerpc/include/asm/io.h:582:58: note: expanded from macro '__do_outsl' #define __do_outsl(p, b, n) writesl((PCI_IO_ADDR)_IO_BASE+(p),(b),(n)) ~~~~~~~~~~~~~~~~~~~~~^ >> mm/hugetlb.c:581:8: error: call to undeclared function '__pte_alloc_one'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] new = __pte_alloc_one(mm, GFP_PGTABLE_USER); ^ mm/hugetlb.c:581:8: note: did you mean 'pte_alloc_one'? arch/powerpc/include/asm/pgalloc.h:30:25: note: 'pte_alloc_one' declared here static inline pgtable_t pte_alloc_one(struct mm_struct *mm) ^ >> mm/hugetlb.c:581:28: error: use of undeclared identifier 'GFP_PGTABLE_USER' new = __pte_alloc_one(mm, GFP_PGTABLE_USER); ^ >> mm/hugetlb.c:588:25: error: incompatible pointer types passing 'pgtable_t' (aka 'unsigned long *') to parameter of type 'struct page *' [-Werror,-Wincompatible-pointer-types] pgtable_pte_page_dtor(new); ^~~ include/linux/mm.h:2661:55: note: passing argument to parameter 'page' here static inline void pgtable_pte_page_dtor(struct page *page) ^ mm/hugetlb.c:589:3: error: incompatible pointer types passing 'pgtable_t' (aka 'unsigned long *') to parameter of type 'struct page *' [-Werror,-Wincompatible-pointer-types] __free_page(new); ^~~~~~~~~~~~~~~~ include/linux/gfp.h:319:40: note: expanded from macro '__free_page' #define __free_page(page) __free_pages((page), 0) ^~~~~~ include/linux/gfp.h:302:39: note: passing argument to parameter 'page' here extern void __free_pages(struct page *page, unsigned int order); ^ 6 warnings and 4 errors generated. vim +/__pte_alloc_one +581 mm/hugetlb.c 543 544 /* 545 * hugetlb_alloc_pte -- Allocate a PTE beneath a pmd_none PMD-level hpte. 546 * 547 * See the comment above hugetlb_alloc_pmd. 548 */ 549 pte_t *hugetlb_alloc_pte(struct mm_struct *mm, struct hugetlb_pte *hpte, 550 unsigned long addr) 551 { 552 spinlock_t *ptl = hugetlb_pte_lockptr(hpte); 553 pgtable_t new; 554 pmd_t *pmdp; 555 pmd_t pmd; 556 557 if (hpte->level != HUGETLB_LEVEL_PMD) 558 return ERR_PTR(-EINVAL); 559 560 pmdp = (pmd_t *)hpte->ptep; 561 retry: 562 pmd = READ_ONCE(*pmdp); 563 if (likely(pmd_present(pmd))) 564 return unlikely(pmd_leaf(pmd)) 565 ? ERR_PTR(-EEXIST) 566 : pte_offset_kernel(pmdp, addr); 567 else if (!pmd_none(pmd)) 568 /* 569 * Not present and not none means that a swap entry lives here, 570 * and we can't get rid of it. 571 */ 572 return ERR_PTR(-EEXIST); 573 574 /* 575 * With CONFIG_HIGHPTE, calling `pte_alloc_one` directly may result 576 * in page tables being allocated in high memory, needing a kmap to 577 * access. Instead, we call __pte_alloc_one directly with 578 * GFP_PGTABLE_USER to prevent these PTEs being allocated in high 579 * memory. 580 */ > 581 new = __pte_alloc_one(mm, GFP_PGTABLE_USER); 582 if (!new) 583 return ERR_PTR(-ENOMEM); 584 585 spin_lock(ptl); 586 if (!pmd_same(pmd, *pmdp)) { 587 spin_unlock(ptl); > 588 pgtable_pte_page_dtor(new); 589 __free_page(new); 590 goto retry; 591 } 592 593 mm_inc_nr_ptes(mm); 594 smp_wmb(); /* See comment in pmd_install() */ 595 pmd_populate(mm, pmdp, new); 596 spin_unlock(ptl); 597 return pte_offset_kernel(pmdp, addr); 598 } 599
On 02/18/23 00:27, James Houghton wrote: > These functions are used to allocate new PTEs below the hstate PTE. This > will be used by hugetlb_walk_step, which implements stepping forwards in > a HugeTLB high-granularity page table walk. > > The reasons that we don't use the standard pmd_alloc/pte_alloc* > functions are: > 1) This prevents us from accidentally overwriting swap entries or > attempting to use swap entries as present non-leaf PTEs (see > pmd_alloc(); we assume that !pte_none means pte_present and > non-leaf). > 2) Locking hugetlb PTEs can different than regular PTEs. (Although, as > implemented right now, locking is the same.) > 3) We can maintain compatibility with CONFIG_HIGHPTE. That is, HugeTLB > HGM won't use HIGHPTE, but the kernel can still be built with it, > and other mm code will use it. > > When GENERAL_HUGETLB supports P4D-based hugepages, we will need to > implement hugetlb_pud_alloc to implement hugetlb_walk_step. > > Signed-off-by: James Houghton <jthoughton@google.com> > > diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h > index eeacadf3272b..9d839519c875 100644 > --- a/include/linux/hugetlb.h > +++ b/include/linux/hugetlb.h > @@ -72,6 +72,11 @@ unsigned long hugetlb_pte_mask(const struct hugetlb_pte *hpte) > > bool hugetlb_pte_present_leaf(const struct hugetlb_pte *hpte, pte_t pte); > > +pmd_t *hugetlb_alloc_pmd(struct mm_struct *mm, struct hugetlb_pte *hpte, > + unsigned long addr); > +pte_t *hugetlb_alloc_pte(struct mm_struct *mm, struct hugetlb_pte *hpte, > + unsigned long addr); > + > struct hugepage_subpool { > spinlock_t lock; > long count; > diff --git a/mm/hugetlb.c b/mm/hugetlb.c > index 6c74adff43b6..bb424cdf79e4 100644 > --- a/mm/hugetlb.c > +++ b/mm/hugetlb.c > @@ -483,6 +483,120 @@ static bool has_same_uncharge_info(struct file_region *rg, > #endif > } > > +/* > + * hugetlb_alloc_pmd -- Allocate or find a PMD beneath a PUD-level hpte. > + * > + * This is meant to be used to implement hugetlb_walk_step when one must go to > + * step down to a PMD. Different architectures may implement hugetlb_walk_step > + * differently, but hugetlb_alloc_pmd and hugetlb_alloc_pte are architecture- > + * independent. > + * > + * Returns: > + * On success: the pointer to the PMD. This should be placed into a > + * hugetlb_pte. @hpte is not changed. > + * ERR_PTR(-EINVAL): hpte is not PUD-level > + * ERR_PTR(-EEXIST): there is a non-leaf and non-empty PUD in @hpte I often get this confused, should this really be 'non-leaf'? Because, ... > + * ERR_PTR(-ENOMEM): could not allocate the new PMD > + */ > +pmd_t *hugetlb_alloc_pmd(struct mm_struct *mm, struct hugetlb_pte *hpte, > + unsigned long addr) > +{ > + spinlock_t *ptl = hugetlb_pte_lockptr(hpte); > + pmd_t *new; > + pud_t *pudp; > + pud_t pud; > + > + if (hpte->level != HUGETLB_LEVEL_PUD) > + return ERR_PTR(-EINVAL); > + > + pudp = (pud_t *)hpte->ptep; > +retry: > + pud = READ_ONCE(*pudp); > + if (likely(pud_present(pud))) > + return unlikely(pud_leaf(pud)) > + ? ERR_PTR(-EEXIST) > + : pmd_offset(pudp, addr); ... it seems we return -EEXIST in the pud_leaf case.
" On Mon, Feb 27, 2023 at 11:17 AM Mike Kravetz <mike.kravetz@oracle.com> wrote: > > On 02/18/23 00:27, James Houghton wrote: > > These functions are used to allocate new PTEs below the hstate PTE. This > > will be used by hugetlb_walk_step, which implements stepping forwards in > > a HugeTLB high-granularity page table walk. > > > > The reasons that we don't use the standard pmd_alloc/pte_alloc* > > functions are: > > 1) This prevents us from accidentally overwriting swap entries or > > attempting to use swap entries as present non-leaf PTEs (see > > pmd_alloc(); we assume that !pte_none means pte_present and > > non-leaf). > > 2) Locking hugetlb PTEs can different than regular PTEs. (Although, as > > implemented right now, locking is the same.) > > 3) We can maintain compatibility with CONFIG_HIGHPTE. That is, HugeTLB > > HGM won't use HIGHPTE, but the kernel can still be built with it, > > and other mm code will use it. > > > > When GENERAL_HUGETLB supports P4D-based hugepages, we will need to > > implement hugetlb_pud_alloc to implement hugetlb_walk_step. > > > > Signed-off-by: James Houghton <jthoughton@google.com> > > > > diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h > > index eeacadf3272b..9d839519c875 100644 > > --- a/include/linux/hugetlb.h > > +++ b/include/linux/hugetlb.h > > @@ -72,6 +72,11 @@ unsigned long hugetlb_pte_mask(const struct hugetlb_pte *hpte) > > > > bool hugetlb_pte_present_leaf(const struct hugetlb_pte *hpte, pte_t pte); > > > > +pmd_t *hugetlb_alloc_pmd(struct mm_struct *mm, struct hugetlb_pte *hpte, > > + unsigned long addr); > > +pte_t *hugetlb_alloc_pte(struct mm_struct *mm, struct hugetlb_pte *hpte, > > + unsigned long addr); > > + > > struct hugepage_subpool { > > spinlock_t lock; > > long count; > > diff --git a/mm/hugetlb.c b/mm/hugetlb.c > > index 6c74adff43b6..bb424cdf79e4 100644 > > --- a/mm/hugetlb.c > > +++ b/mm/hugetlb.c > > @@ -483,6 +483,120 @@ static bool has_same_uncharge_info(struct file_region *rg, > > #endif > > } > > > > +/* > > + * hugetlb_alloc_pmd -- Allocate or find a PMD beneath a PUD-level hpte. > > + * > > + * This is meant to be used to implement hugetlb_walk_step when one must go to > > + * step down to a PMD. Different architectures may implement hugetlb_walk_step > > + * differently, but hugetlb_alloc_pmd and hugetlb_alloc_pte are architecture- > > + * independent. > > + * > > + * Returns: > > + * On success: the pointer to the PMD. This should be placed into a > > + * hugetlb_pte. @hpte is not changed. > > + * ERR_PTR(-EINVAL): hpte is not PUD-level > > + * ERR_PTR(-EEXIST): there is a non-leaf and non-empty PUD in @hpte > > I often get this confused, should this really be 'non-leaf'? Because, ... This comment is wrong, it should be "non-empty PUD that is not pointing to page tables". Maybe it would be better to say "-EEXIST unless @hpte is pud_none() or already points to page tables". In this commit, PTEs containing PTE markers are treated as non-empty here (and not pointing to page tables), but after the commit "hugetlb: split PTE markers when doing HGM walks", they are treated as empty. I'll update the comment in that commit as well. > > > + * ERR_PTR(-ENOMEM): could not allocate the new PMD > > + */ > > +pmd_t *hugetlb_alloc_pmd(struct mm_struct *mm, struct hugetlb_pte *hpte, > > + unsigned long addr) > > +{ > > + spinlock_t *ptl = hugetlb_pte_lockptr(hpte); > > + pmd_t *new; > > + pud_t *pudp; > > + pud_t pud; > > + > > + if (hpte->level != HUGETLB_LEVEL_PUD) > > + return ERR_PTR(-EINVAL); > > + > > + pudp = (pud_t *)hpte->ptep; > > +retry: > > + pud = READ_ONCE(*pudp); > > + if (likely(pud_present(pud))) > > + return unlikely(pud_leaf(pud)) > > + ? ERR_PTR(-EEXIST) > > + : pmd_offset(pudp, addr); > > ... it seems we return -EEXIST in the pud_leaf case. This code is correct. :) We don't want to overwrite a leaf. Sorry for the confusion!
diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index eeacadf3272b..9d839519c875 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -72,6 +72,11 @@ unsigned long hugetlb_pte_mask(const struct hugetlb_pte *hpte) bool hugetlb_pte_present_leaf(const struct hugetlb_pte *hpte, pte_t pte); +pmd_t *hugetlb_alloc_pmd(struct mm_struct *mm, struct hugetlb_pte *hpte, + unsigned long addr); +pte_t *hugetlb_alloc_pte(struct mm_struct *mm, struct hugetlb_pte *hpte, + unsigned long addr); + struct hugepage_subpool { spinlock_t lock; long count; diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 6c74adff43b6..bb424cdf79e4 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -483,6 +483,120 @@ static bool has_same_uncharge_info(struct file_region *rg, #endif } +/* + * hugetlb_alloc_pmd -- Allocate or find a PMD beneath a PUD-level hpte. + * + * This is meant to be used to implement hugetlb_walk_step when one must go to + * step down to a PMD. Different architectures may implement hugetlb_walk_step + * differently, but hugetlb_alloc_pmd and hugetlb_alloc_pte are architecture- + * independent. + * + * Returns: + * On success: the pointer to the PMD. This should be placed into a + * hugetlb_pte. @hpte is not changed. + * ERR_PTR(-EINVAL): hpte is not PUD-level + * ERR_PTR(-EEXIST): there is a non-leaf and non-empty PUD in @hpte + * ERR_PTR(-ENOMEM): could not allocate the new PMD + */ +pmd_t *hugetlb_alloc_pmd(struct mm_struct *mm, struct hugetlb_pte *hpte, + unsigned long addr) +{ + spinlock_t *ptl = hugetlb_pte_lockptr(hpte); + pmd_t *new; + pud_t *pudp; + pud_t pud; + + if (hpte->level != HUGETLB_LEVEL_PUD) + return ERR_PTR(-EINVAL); + + pudp = (pud_t *)hpte->ptep; +retry: + pud = READ_ONCE(*pudp); + if (likely(pud_present(pud))) + return unlikely(pud_leaf(pud)) + ? ERR_PTR(-EEXIST) + : pmd_offset(pudp, addr); + else if (!pud_none(pud)) + /* + * Not present and not none means that a swap entry lives here, + * and we can't get rid of it. + */ + return ERR_PTR(-EEXIST); + + new = pmd_alloc_one(mm, addr); + if (!new) + return ERR_PTR(-ENOMEM); + + spin_lock(ptl); + if (!pud_same(pud, *pudp)) { + spin_unlock(ptl); + pmd_free(mm, new); + goto retry; + } + + mm_inc_nr_pmds(mm); + smp_wmb(); /* See comment in pmd_install() */ + pud_populate(mm, pudp, new); + spin_unlock(ptl); + return pmd_offset(pudp, addr); +} + +/* + * hugetlb_alloc_pte -- Allocate a PTE beneath a pmd_none PMD-level hpte. + * + * See the comment above hugetlb_alloc_pmd. + */ +pte_t *hugetlb_alloc_pte(struct mm_struct *mm, struct hugetlb_pte *hpte, + unsigned long addr) +{ + spinlock_t *ptl = hugetlb_pte_lockptr(hpte); + pgtable_t new; + pmd_t *pmdp; + pmd_t pmd; + + if (hpte->level != HUGETLB_LEVEL_PMD) + return ERR_PTR(-EINVAL); + + pmdp = (pmd_t *)hpte->ptep; +retry: + pmd = READ_ONCE(*pmdp); + if (likely(pmd_present(pmd))) + return unlikely(pmd_leaf(pmd)) + ? ERR_PTR(-EEXIST) + : pte_offset_kernel(pmdp, addr); + else if (!pmd_none(pmd)) + /* + * Not present and not none means that a swap entry lives here, + * and we can't get rid of it. + */ + return ERR_PTR(-EEXIST); + + /* + * With CONFIG_HIGHPTE, calling `pte_alloc_one` directly may result + * in page tables being allocated in high memory, needing a kmap to + * access. Instead, we call __pte_alloc_one directly with + * GFP_PGTABLE_USER to prevent these PTEs being allocated in high + * memory. + */ + new = __pte_alloc_one(mm, GFP_PGTABLE_USER); + if (!new) + return ERR_PTR(-ENOMEM); + + spin_lock(ptl); + if (!pmd_same(pmd, *pmdp)) { + spin_unlock(ptl); + pgtable_pte_page_dtor(new); + __free_page(new); + goto retry; + } + + mm_inc_nr_ptes(mm); + smp_wmb(); /* See comment in pmd_install() */ + pmd_populate(mm, pmdp, new); + spin_unlock(ptl); + return pte_offset_kernel(pmdp, addr); +} + static void coalesce_file_region(struct resv_map *resv, struct file_region *rg) { struct file_region *nrg, *prg;