From nobody Mon Apr 27 07:26:20 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 410BEC43334 for ; Wed, 15 Jun 2022 17:29:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349861AbiFOR3u (ORCPT ); Wed, 15 Jun 2022 13:29:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40594 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349966AbiFOR3g (ORCPT ); Wed, 15 Jun 2022 13:29:36 -0400 Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EDE8D45ADB for ; Wed, 15 Jun 2022 10:29:34 -0700 (PDT) Received: by mail-pf1-x42a.google.com with SMTP id e11so12049417pfj.5 for ; Wed, 15 Jun 2022 10:29:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=gQUColGb/PRKYuEV4lE8XSFjbzf2io5XZTvMnHxGXf0=; b=P5PSssOdjWY/xPZ7jddHeQPn8UutJ5m9TF+K4vzKHaQtM6cE/Mf+NE4GoAnGSxi3Io Y2LlfpoNzzarjkEPUOBBbK2VNLV6QvrkCU0YgZMxegJsia53STwh6Di/GzL3gsyqzoFP rUAxM+SHTD3Ks6z6Onsi/In0Mrk2HzetpwpKANFN1OqsGo2AHjr6/haWrWg2TS12egTu 1PKdIZyzPNk09Cs6fGAidY2XEJ/VqNbV78o1QLo7kcwS7nDaVKeKv77VuBXpQHOqqTvl KFl4JRX1TJx3TG4Jb+1vv779H0J6hBCcUH2+m3wdmfheRWs69YXkaQK77FyCnII6PT7d qqTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=gQUColGb/PRKYuEV4lE8XSFjbzf2io5XZTvMnHxGXf0=; b=QanER3wHNGcdcSBqsvIEpwmZz/dVF4GWaYln0rnml30fGCMtlymuNtOqtQeVlvUOjy vuATArUKpXSKkSS8EOfXO/WqjoodzlSMPMXvloo35gC9RkUTyVrMPlahd+apLzbqQ3WY Kv7ZzM/1032OCiT3zaqxaS4mozrY/HvlZHaPwKlFPAjGnMnC8oGejFDPQqXaCHLaYABt cDx2LlXOkUfXaPKmIRymgDSirPVJVAns3TWLF95ZYIRYtlFbApnlTPf4gXu4UkHRWh8J WaDk8WHdd96s0tHf4yz4DzLgtOjPAlfavidcdcmJtogOPYcusDWjefi8f+ZTuaBr47fw 4WjA== X-Gm-Message-State: AJIora8WLbYUYCHw0HU7dcJoOVDp+UeEvzMdL5armqpQSdWe9KKOQ8gg BCpNa4effXdBRpipVy27BNKFANZE6FY= X-Google-Smtp-Source: AGRyM1s1Cazz5Rcz0v51mDmvba3KC32sP5s7r0OCDu88q+Oc50uuWQFdSzmqKeUrPH3UKttvAQA9+w== X-Received: by 2002:a63:ec0f:0:b0:3fd:e23d:b9f9 with SMTP id j15-20020a63ec0f000000b003fde23db9f9mr747614pgh.612.1655314174419; Wed, 15 Jun 2022 10:29:34 -0700 (PDT) Received: from localhost.localdomain (c-67-174-241-145.hsd1.ca.comcast.net. [67.174.241.145]) by smtp.gmail.com with ESMTPSA id w14-20020a1709029a8e00b001676f87473fsm9552244plp.302.2022.06.15.10.29.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jun 2022 10:29:33 -0700 (PDT) From: Yang Shi To: vbabka@suse.cz, kirill.shutemov@linux.intel.com, willy@infradead.org, zokeefe@google.com, linmiaohe@huawei.com, akpm@linux-foundation.org Cc: shy828301@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [v4 PATCH 1/7] mm: khugepaged: check THP flag in hugepage_vma_check() Date: Wed, 15 Jun 2022 10:29:20 -0700 Message-Id: <20220615172926.546974-2-shy828301@gmail.com> X-Mailer: git-send-email 2.26.3 In-Reply-To: <20220615172926.546974-1-shy828301@gmail.com> References: <20220615172926.546974-1-shy828301@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Currently the THP flag check in hugepage_vma_check() will fallthrough if the flag is NEVER and VM_HUGEPAGE is set. This is not a problem for now since all the callers have the flag checked before or can't be invoked if the flag is NEVER. However, the following patch will call hugepage_vma_check() in more places, for example, page fault, so this flag must be checked in hugepge_vma_check(). Reviewed-by: Zach O'Keefe Reviewed-by: Miaohe Lin Signed-off-by: Yang Shi --- mm/khugepaged.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 476d79360101..b1dab94c0f1e 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -458,6 +458,9 @@ bool hugepage_vma_check(struct vm_area_struct *vma, if (shmem_file(vma->vm_file)) return shmem_huge_enabled(vma); =20 + if (!khugepaged_enabled()) + return false; + /* THP settings require madvise. */ if (!(vm_flags & VM_HUGEPAGE) && !khugepaged_always()) return false; --=20 2.26.3 From nobody Mon Apr 27 07:26:20 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E574DC433EF for ; Wed, 15 Jun 2022 17:29:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356054AbiFOR3w (ORCPT ); Wed, 15 Jun 2022 13:29:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40620 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350138AbiFOR3i (ORCPT ); Wed, 15 Jun 2022 13:29:38 -0400 Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9ED51393DC for ; Wed, 15 Jun 2022 10:29:36 -0700 (PDT) Received: by mail-pl1-x634.google.com with SMTP id d5so8380073plo.12 for ; Wed, 15 Jun 2022 10:29:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=51sNwNc9iGRZC8aX2+MPWXbJAssH24vr8KC9AQWwCZ4=; b=DA1ymx6Eusxll6nKGGOtuCA1e5yLxoL17ga00PUZEbMSGUwZ9QG6+NBJuPfd7ilrmc vxzciGDA9U1JKH4VjkUhxyoesCyPzaLeNmcLKGq2yivT4DaqFKlc2ONucsFlXYa0GFhn nlwiSSWq4Fu6wWNsgWNPShUy524Ut88IgYHLjMWkb9gyH3pmJkLL99fTKnmqxnJZh/cu q6A8rMdMbGH6pv03ku6Mp75PoUjbWnjkDtLFaQBSegDQOFObnqm0jYznkWl4N7JJycvK U7IW31KkKAzmgrgRgQw3xFuSzOZqlDBEt+MnTO135pasLPl+TY1lV5wINAfU/h9zAtby eaiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=51sNwNc9iGRZC8aX2+MPWXbJAssH24vr8KC9AQWwCZ4=; b=ppBdfp5QYNCXm4DyLczW9eBwr44RwclZYe3c9zwYqdbB9SnBX9QlNBuYhIGuc9Qs0N qpXkORpioEahzjRAwsroWEFU4vHNdfFPs10zG2tflTsPH10DJmBd4jeoaSC1uXZ/Dh5+ zq4KYGuyt0cdpgBB0bzkPz6AnY+ZIxTer6fAehhvZo6eDLX+jmbxIcFG+BqeMpPD/0RF ResT24n+kMH7XEa+kvC2jmXCcTtHPit/dPEPCxDJlIEljiWMkTNgsxfiOOYvFho+P1S4 tFFXgA1gCM/pZU6kF9Oi8o65rCc4l7I7EjPCoLJ6Nqbn3y3kXci7KhTEBc6u9U8gzQ1e giPA== X-Gm-Message-State: AJIora+3RPWJpReVsiG89+Bo9/z57lsxEOjn6LGI/kd1iwmXKxM66RQU 0doReJuR4GpRa6WZ//Ro5Do= X-Google-Smtp-Source: AGRyM1ubIIBH2uc5bj3/MaNUe6wnfsdVL/fOlKAmVLhqdVvcHkAScJirEVp37jaO3ZqktGgU+ZYlcg== X-Received: by 2002:a17:90a:31cf:b0:1c9:f9b8:68c7 with SMTP id j15-20020a17090a31cf00b001c9f9b868c7mr11499592pjf.34.1655314176105; Wed, 15 Jun 2022 10:29:36 -0700 (PDT) Received: from localhost.localdomain (c-67-174-241-145.hsd1.ca.comcast.net. [67.174.241.145]) by smtp.gmail.com with ESMTPSA id w14-20020a1709029a8e00b001676f87473fsm9552244plp.302.2022.06.15.10.29.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jun 2022 10:29:35 -0700 (PDT) From: Yang Shi To: vbabka@suse.cz, kirill.shutemov@linux.intel.com, willy@infradead.org, zokeefe@google.com, linmiaohe@huawei.com, akpm@linux-foundation.org Cc: shy828301@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [v4 PATCH 2/7] mm: thp: consolidate vma size check to transhuge_vma_suitable Date: Wed, 15 Jun 2022 10:29:21 -0700 Message-Id: <20220615172926.546974-3-shy828301@gmail.com> X-Mailer: git-send-email 2.26.3 In-Reply-To: <20220615172926.546974-1-shy828301@gmail.com> References: <20220615172926.546974-1-shy828301@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" There are couple of places that check whether the vma size is ok for THP or whether address fits, they are open coded and duplicate, use transhuge_vma_suitable() to do the job by passing in (vma->end - HPAGE_PMD_SIZE). Move vma size check into hugepage_vma_check(). This will make khugepaged_enter() is as same as khugepaged_enter_vma(). There is just one caller for khugepaged_enter(), replace it to khugepaged_enter_vma() and remove khugepaged_enter(). Signed-off-by: Yang Shi Reviewed-by: Zach O'Keefe --- include/linux/huge_mm.h | 11 +++++++++++ include/linux/khugepaged.h | 14 -------------- mm/huge_memory.c | 2 +- mm/khugepaged.c | 19 ++++++------------- 4 files changed, 18 insertions(+), 28 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index 648cb3ce7099..8a5a8bfce0f5 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -116,6 +116,17 @@ extern struct kobj_attribute shmem_enabled_attr; =20 extern unsigned long transparent_hugepage_flags; =20 +/* + * Do the below checks: + * - For file vma, check if the linear page offset of vma is + * HPAGE_PMD_NR aligned within the file. The hugepage is + * guaranteed to be hugepage-aligned within the file, but we must + * check that the PMD-aligned addresses in the VMA map to + * PMD-aligned offsets within the file, else the hugepage will + * not be PMD-mappable. + * - For all vmas, check if the haddr is in an aligned HPAGE_PMD_SIZE + * area. + */ static inline bool transhuge_vma_suitable(struct vm_area_struct *vma, unsigned long addr) { diff --git a/include/linux/khugepaged.h b/include/linux/khugepaged.h index 392d34c3c59a..31ca8a7f78f4 100644 --- a/include/linux/khugepaged.h +++ b/include/linux/khugepaged.h @@ -51,16 +51,6 @@ static inline void khugepaged_exit(struct mm_struct *mm) if (test_bit(MMF_VM_HUGEPAGE, &mm->flags)) __khugepaged_exit(mm); } - -static inline void khugepaged_enter(struct vm_area_struct *vma, - unsigned long vm_flags) -{ - if (!test_bit(MMF_VM_HUGEPAGE, &vma->vm_mm->flags) && - khugepaged_enabled()) { - if (hugepage_vma_check(vma, vm_flags)) - __khugepaged_enter(vma->vm_mm); - } -} #else /* CONFIG_TRANSPARENT_HUGEPAGE */ static inline void khugepaged_fork(struct mm_struct *mm, struct mm_struct = *oldmm) { @@ -68,10 +58,6 @@ static inline void khugepaged_fork(struct mm_struct *mm,= struct mm_struct *oldmm static inline void khugepaged_exit(struct mm_struct *mm) { } -static inline void khugepaged_enter(struct vm_area_struct *vma, - unsigned long vm_flags) -{ -} static inline void khugepaged_enter_vma(struct vm_area_struct *vma, unsigned long vm_flags) { diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 4f9bbb4eab23..b530462c4493 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -726,7 +726,7 @@ vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *= vmf) return VM_FAULT_FALLBACK; if (unlikely(anon_vma_prepare(vma))) return VM_FAULT_OOM; - khugepaged_enter(vma, vma->vm_flags); + khugepaged_enter_vma(vma, vma->vm_flags); =20 if (!(vmf->flags & FAULT_FLAG_WRITE) && !mm_forbids_zeropage(vma->vm_mm) && diff --git a/mm/khugepaged.c b/mm/khugepaged.c index b1dab94c0f1e..db0b334a7d1f 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -450,8 +450,8 @@ bool hugepage_vma_check(struct vm_area_struct *vma, if (vma_is_dax(vma)) return false; =20 - if (vma->vm_file && !IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - - vma->vm_pgoff, HPAGE_PMD_NR)) + /* Check alignment for file vma and size for both file and anon vma */ + if (!transhuge_vma_suitable(vma, (vma->vm_end - HPAGE_PMD_SIZE))) return false; =20 /* Enabled via shmem mount options or sysfs settings. */ @@ -512,9 +512,7 @@ void khugepaged_enter_vma(struct vm_area_struct *vma, unsigned long vm_flags) { if (!test_bit(MMF_VM_HUGEPAGE, &vma->vm_mm->flags) && - khugepaged_enabled() && - (((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) < - (vma->vm_end & HPAGE_PMD_MASK))) { + khugepaged_enabled()) { if (hugepage_vma_check(vma, vm_flags)) __khugepaged_enter(vma->vm_mm); } @@ -950,7 +948,6 @@ static int hugepage_vma_revalidate(struct mm_struct *mm= , unsigned long address, struct vm_area_struct **vmap) { struct vm_area_struct *vma; - unsigned long hstart, hend; =20 if (unlikely(khugepaged_test_exit(mm))) return SCAN_ANY_PROCESS; @@ -959,9 +956,7 @@ static int hugepage_vma_revalidate(struct mm_struct *mm= , unsigned long address, if (!vma) return SCAN_VMA_NULL; =20 - hstart =3D (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK; - hend =3D vma->vm_end & HPAGE_PMD_MASK; - if (address < hstart || address + HPAGE_PMD_SIZE > hend) + if (!transhuge_vma_suitable(vma, address)) return SCAN_ADDRESS_RANGE; if (!hugepage_vma_check(vma, vma->vm_flags)) return SCAN_VMA_CHECK; @@ -2147,10 +2142,8 @@ static unsigned int khugepaged_scan_mm_slot(unsigned= int pages, progress++; continue; } - hstart =3D (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK; - hend =3D vma->vm_end & HPAGE_PMD_MASK; - if (hstart >=3D hend) - goto skip; + hstart =3D round_up(vma->vm_start, HPAGE_PMD_SIZE); + hend =3D round_down(vma->vm_end, HPAGE_PMD_SIZE); if (khugepaged_scan.address > hend) goto skip; if (khugepaged_scan.address < hstart) --=20 2.26.3 From nobody Mon Apr 27 07:26:20 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D2D10C43334 for ; Wed, 15 Jun 2022 17:30:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1357241AbiFORaA (ORCPT ); Wed, 15 Jun 2022 13:30:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40536 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242396AbiFOR3i (ORCPT ); Wed, 15 Jun 2022 13:29:38 -0400 Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 27A8E34BB3 for ; Wed, 15 Jun 2022 10:29:38 -0700 (PDT) Received: by mail-pj1-x1029.google.com with SMTP id k5-20020a17090a404500b001e8875e6242so2670938pjg.5 for ; Wed, 15 Jun 2022 10:29:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=WrF836BhqYoAAQYNFMJen23eOl+F6zz51fEhzRe3ck4=; b=jNiMa1EClCZmNoxHFwK9zkQt71WNj8eQXx1AtJzkARohOhJSxbNe9g9wKpdtMdMiFM JUylBaxwrEsHZe9FDtW/ty8pZHqkoiXrCVnIf4XeVJdT7glhN+11Co4GX3RF1sUTcydf V3HfSNZro9f+AcdFfuHK+L4B6g8H28LVBK/7P7o1JiWQR01uO+wBiMM/9HiCPM6SAMQx d2a2Sz0T524tVIKdzfkdECmzkPJH8aXQXrXRWCyPKVJHg489kISeCmxjam/lwp9uY3TL eupQy8+iv/QTL3N+W5Rklt4NRIJ/YA5TCnZRDnOfKJw5G4UK28mGTOs/faVfewTqkG2c Oztw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=WrF836BhqYoAAQYNFMJen23eOl+F6zz51fEhzRe3ck4=; b=6GYxj8c4efxyDZuscsY6CuorXXB84ETMOEBLdVV1kEfn5THX4LIApMSmxSldSNHzCL jcDl5nRr7TtdklXetfpymE9/Q8hTBz3/Wj6/FoBI2asWQCRpgVQyJc8Naa6jbnfyQ6q6 S6VYnJkRkciARry/XywJCwX+ZA0YPSGguv6XRPwm/24zKwUwbzP9AR+oSbT+tpZMr7lo YwNJXVZbhqxqxPKhCjOttyEScMvqJnFJ99z5bHduf147yh7JcGZrwuYZnWoXh7V4vRic 09o71ybsIq4PwuYHyt6RVDOgkCZWAR3DQMZsbt/fZpiyYepFes8+Nek3fZ7dwhrrDLWX udCg== X-Gm-Message-State: AJIora8EZIxG179JGHE+y54cQzOYOGZZU7sdFmx8xeRM5pqQho6YwGRW ItLWJHvCGzUjaaXeq9E99Z8= X-Google-Smtp-Source: AGRyM1vRDq5qAc7St9qdLuZS57haehWUHZEdEiVLV8l2rTbRMydreLZ77f4O354sxk37OOZMcG9tJw== X-Received: by 2002:a17:90b:4c4c:b0:1e8:6f9a:b642 with SMTP id np12-20020a17090b4c4c00b001e86f9ab642mr11434299pjb.21.1655314177666; Wed, 15 Jun 2022 10:29:37 -0700 (PDT) Received: from localhost.localdomain (c-67-174-241-145.hsd1.ca.comcast.net. [67.174.241.145]) by smtp.gmail.com with ESMTPSA id w14-20020a1709029a8e00b001676f87473fsm9552244plp.302.2022.06.15.10.29.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jun 2022 10:29:37 -0700 (PDT) From: Yang Shi To: vbabka@suse.cz, kirill.shutemov@linux.intel.com, willy@infradead.org, zokeefe@google.com, linmiaohe@huawei.com, akpm@linux-foundation.org Cc: shy828301@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [v4 PATCH 3/7] mm: khugepaged: better comments for anon vma check in hugepage_vma_revalidate Date: Wed, 15 Jun 2022 10:29:22 -0700 Message-Id: <20220615172926.546974-4-shy828301@gmail.com> X-Mailer: git-send-email 2.26.3 In-Reply-To: <20220615172926.546974-1-shy828301@gmail.com> References: <20220615172926.546974-1-shy828301@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The hugepage_vma_revalidate() needs to check if the vma is still anonymous vma or not since the address may be unmapped then remapped to file before khugepaged reaquired the mmap_lock. The old comment is not quite helpful, elaborate this with better comment. Signed-off-by: Yang Shi Reviewed-by: Zach O'Keefe --- mm/khugepaged.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index db0b334a7d1f..5baa394e34c8 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -960,7 +960,13 @@ static int hugepage_vma_revalidate(struct mm_struct *m= m, unsigned long address, return SCAN_ADDRESS_RANGE; if (!hugepage_vma_check(vma, vma->vm_flags)) return SCAN_VMA_CHECK; - /* Anon VMA expected */ + /* + * Anon VMA expected, the address may be unmapped then + * remapped to file after khugepaged reaquired the mmap_lock. + * + * hugepage_vma_check may return true for qualified file + * vmas. + */ if (!vma->anon_vma || !vma_is_anonymous(vma)) return SCAN_VMA_CHECK; return 0; --=20 2.26.3 From nobody Mon Apr 27 07:26:20 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 85BBBC433EF for ; Wed, 15 Jun 2022 17:30:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356772AbiFORaF (ORCPT ); Wed, 15 Jun 2022 13:30:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40672 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349688AbiFOR3k (ORCPT ); Wed, 15 Jun 2022 13:29:40 -0400 Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DB7DA3FBFC for ; Wed, 15 Jun 2022 10:29:39 -0700 (PDT) Received: by mail-pj1-x1029.google.com with SMTP id k5-20020a17090a404500b001e8875e6242so2670938pjg.5 for ; Wed, 15 Jun 2022 10:29:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=0N0t9howdLfNntzOrT/sFPKbK6rgBL9c7HpeSr6Z4As=; b=plkDnRo/ngAq2p1xXo+l6xL9fBjXBke5So1zG5HFSb5vJX4ot9uGcb0KUAHR+Ph+Ml x9+sINiMQ0/2ZvfachaHAvl/wBQtjQzst7n7Vt7qQqMPyg5Ee6u3sIzzCasOKvESnXT/ yTC9rYAiCSHRW7KU07yE8dExF1o7McjKNNaRrAzG+Q69Jmj0VAlO1g8uYNrZR46TLcKI TvEO/Z/yoWxuwYqMLnkn/ak0Ki9A7ZX4XqFuqOcLKWBLmVSiySfVp4lwB2GAUXZEpq0r GLeKiHv1Ss/ZouDvybXXN6J2W+EFzaOMEa6Zo4xe7J+R2VeFBAILAsZ8oBsF9mSScg4H P+lQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=0N0t9howdLfNntzOrT/sFPKbK6rgBL9c7HpeSr6Z4As=; b=A421PQUk+H119AbQZ/SphmghQqo4YZ9LUkqn63AKi6aI3+/1m94PFJoASk1opdFsaa ZXJjg0Yt1Q1T6J9OiublJ9JPpi806pH9RmNuiwQoOT6/zsnm1ZidI39o53Q62NnD3qzJ xhmnPTBlPbUVGlI7RYjv9OTwdtdvGjWI5O+noojn67kwIe0XNYb0OuokVie0ih7MA7hi lQjDtI7ddhHpXaW+FivjWFyh+BH8fPNYmp4382Kn/f47xE5Wt/+18M1ZhE7AONmLohu8 VB2IUewJczuLuJ4G1P5WOSAKSeeT0zgiSblx2uvQvHJLxPYbpFx1Hu4NhOVW3HNnPRdd M+Yw== X-Gm-Message-State: AJIora+1eGwN/4vavtadjFyVdYsuF8Y47yRJqREJH0hY0g+rAS4Z3YeD hZWBohU6kagbo4BqducX+mg= X-Google-Smtp-Source: AGRyM1uHc6Ah3zHBOP8Sn5+jQVN7TbPYQZ5uzgq4ONWj/a98/ypZEEKfD2jSWquQYwKo4HLyFsAIDA== X-Received: by 2002:a17:90b:304:b0:1e3:4489:193c with SMTP id ay4-20020a17090b030400b001e34489193cmr11441651pjb.78.1655314179550; Wed, 15 Jun 2022 10:29:39 -0700 (PDT) Received: from localhost.localdomain (c-67-174-241-145.hsd1.ca.comcast.net. [67.174.241.145]) by smtp.gmail.com with ESMTPSA id w14-20020a1709029a8e00b001676f87473fsm9552244plp.302.2022.06.15.10.29.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jun 2022 10:29:38 -0700 (PDT) From: Yang Shi To: vbabka@suse.cz, kirill.shutemov@linux.intel.com, willy@infradead.org, zokeefe@google.com, linmiaohe@huawei.com, akpm@linux-foundation.org Cc: shy828301@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [v4 PATCH 4/7] mm: thp: kill transparent_hugepage_active() Date: Wed, 15 Jun 2022 10:29:23 -0700 Message-Id: <20220615172926.546974-5-shy828301@gmail.com> X-Mailer: git-send-email 2.26.3 In-Reply-To: <20220615172926.546974-1-shy828301@gmail.com> References: <20220615172926.546974-1-shy828301@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The transparent_hugepage_active() was introduced to show THP eligibility bit in smaps in proc, smaps is the only user. But it actually does the similar check as hugepage_vma_check() which is used by khugepaged. We definitely don't have to maintain two similar checks, so kill transparent_hugepage_active(). This patch also fixed the wrong behavior for VM_NO_KHUGEPAGED vmas. Also move hugepage_vma_check() to huge_memory.c and huge_mm.h since it is not only for khugepaged anymore. Signed-off-by: Yang Shi Reviewed-by: Zach O'Keefe --- fs/proc/task_mmu.c | 2 +- include/linux/huge_mm.h | 11 ++++++++- include/linux/khugepaged.h | 2 -- mm/huge_memory.c | 50 +++++++++++++++++++++++++++++++------- mm/khugepaged.c | 48 +++--------------------------------- 5 files changed, 56 insertions(+), 57 deletions(-) diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 37ccb5c9f4f8..39a40ec181e7 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -863,7 +863,7 @@ static int show_smap(struct seq_file *m, void *v) __show_smap(m, &mss, false); =20 seq_printf(m, "THPeligible: %d\n", - transparent_hugepage_active(vma)); + hugepage_vma_check(vma, vma->vm_flags, true)); =20 if (arch_pkeys_enabled()) seq_printf(m, "ProtectionKey: %8u\n", vma_pkey(vma)); diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index 8a5a8bfce0f5..aeb13119ee28 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -202,7 +202,9 @@ static inline bool file_thp_enabled(struct vm_area_stru= ct *vma) !inode_is_open_for_write(inode) && S_ISREG(inode->i_mode); } =20 -bool transparent_hugepage_active(struct vm_area_struct *vma); +bool hugepage_vma_check(struct vm_area_struct *vma, + unsigned long vm_flags, + bool smaps); =20 #define transparent_hugepage_use_zero_page() \ (transparent_hugepage_flags & \ @@ -368,6 +370,13 @@ static inline bool transhuge_vma_enabled(struct vm_are= a_struct *vma, return false; } =20 +static inline bool hugepage_vma_check(struct vm_area_struct *vma, + unsigned long vm_flags, + bool smaps) +{ + return false; +} + static inline void prep_transhuge_page(struct page *page) {} =20 #define transparent_hugepage_flags 0UL diff --git a/include/linux/khugepaged.h b/include/linux/khugepaged.h index 31ca8a7f78f4..ea5fd4c398f7 100644 --- a/include/linux/khugepaged.h +++ b/include/linux/khugepaged.h @@ -10,8 +10,6 @@ extern struct attribute_group khugepaged_attr_group; extern int khugepaged_init(void); extern void khugepaged_destroy(void); extern int start_stop_khugepaged(void); -extern bool hugepage_vma_check(struct vm_area_struct *vma, - unsigned long vm_flags); extern void __khugepaged_enter(struct mm_struct *mm); extern void __khugepaged_exit(struct mm_struct *mm); extern void khugepaged_enter_vma(struct vm_area_struct *vma, diff --git a/mm/huge_memory.c b/mm/huge_memory.c index b530462c4493..a28c6100b491 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -69,21 +69,53 @@ static atomic_t huge_zero_refcount; struct page *huge_zero_page __read_mostly; unsigned long huge_zero_pfn __read_mostly =3D ~0UL; =20 -bool transparent_hugepage_active(struct vm_area_struct *vma) +bool hugepage_vma_check(struct vm_area_struct *vma, + unsigned long vm_flags, + bool smaps) { - /* The addr is used to check if the vma size fits */ - unsigned long addr =3D (vma->vm_end & HPAGE_PMD_MASK) - HPAGE_PMD_SIZE; + if (!transhuge_vma_enabled(vma, vm_flags)) + return false; + + if (vm_flags & VM_NO_KHUGEPAGED) + return false; + + /* Don't run khugepaged against DAX vma */ + if (vma_is_dax(vma)) + return false; =20 - if (!transhuge_vma_suitable(vma, addr)) + /* Check alignment for file vma and size for both file and anon vma */ + if (!transhuge_vma_suitable(vma, (vma->vm_end - HPAGE_PMD_SIZE))) return false; - if (vma_is_anonymous(vma)) - return __transparent_hugepage_enabled(vma); - if (vma_is_shmem(vma)) + + /* Enabled via shmem mount options or sysfs settings. */ + if (shmem_file(vma->vm_file)) return shmem_huge_enabled(vma); - if (transhuge_vma_enabled(vma, vma->vm_flags) && file_thp_enabled(vma)) + + if (!khugepaged_enabled()) + return false; + + /* THP settings require madvise. */ + if (!(vm_flags & VM_HUGEPAGE) && !khugepaged_always()) + return false; + + /* Only regular file is valid */ + if (file_thp_enabled(vma)) return true; =20 - return false; + if (!vma_is_anonymous(vma)) + return false; + + if (vma_is_temporary_stack(vma)) + return false; + + /* + * THPeligible bit of smaps should show 1 for proper VMAs even + * though anon_vma is not initialized yet. + */ + if (!vma->anon_vma) + return smaps; + + return true; } =20 static bool get_huge_zero_page(void) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 5baa394e34c8..3afd87f8c0b1 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -437,46 +437,6 @@ static inline int khugepaged_test_exit(struct mm_struc= t *mm) return atomic_read(&mm->mm_users) =3D=3D 0; } =20 -bool hugepage_vma_check(struct vm_area_struct *vma, - unsigned long vm_flags) -{ - if (!transhuge_vma_enabled(vma, vm_flags)) - return false; - - if (vm_flags & VM_NO_KHUGEPAGED) - return false; - - /* Don't run khugepaged against DAX vma */ - if (vma_is_dax(vma)) - return false; - - /* Check alignment for file vma and size for both file and anon vma */ - if (!transhuge_vma_suitable(vma, (vma->vm_end - HPAGE_PMD_SIZE))) - return false; - - /* Enabled via shmem mount options or sysfs settings. */ - if (shmem_file(vma->vm_file)) - return shmem_huge_enabled(vma); - - if (!khugepaged_enabled()) - return false; - - /* THP settings require madvise. */ - if (!(vm_flags & VM_HUGEPAGE) && !khugepaged_always()) - return false; - - /* Only regular file is valid */ - if (file_thp_enabled(vma)) - return true; - - if (!vma->anon_vma || !vma_is_anonymous(vma)) - return false; - if (vma_is_temporary_stack(vma)) - return false; - - return true; -} - void __khugepaged_enter(struct mm_struct *mm) { struct mm_slot *mm_slot; @@ -513,7 +473,7 @@ void khugepaged_enter_vma(struct vm_area_struct *vma, { if (!test_bit(MMF_VM_HUGEPAGE, &vma->vm_mm->flags) && khugepaged_enabled()) { - if (hugepage_vma_check(vma, vm_flags)) + if (hugepage_vma_check(vma, vm_flags, false)) __khugepaged_enter(vma->vm_mm); } } @@ -958,7 +918,7 @@ static int hugepage_vma_revalidate(struct mm_struct *mm= , unsigned long address, =20 if (!transhuge_vma_suitable(vma, address)) return SCAN_ADDRESS_RANGE; - if (!hugepage_vma_check(vma, vma->vm_flags)) + if (!hugepage_vma_check(vma, vma->vm_flags, false)) return SCAN_VMA_CHECK; /* * Anon VMA expected, the address may be unmapped then @@ -1448,7 +1408,7 @@ void collapse_pte_mapped_thp(struct mm_struct *mm, un= signed long addr) * the valid THP. Add extra VM_HUGEPAGE so hugepage_vma_check() * will not fail the vma for missing VM_HUGEPAGE */ - if (!hugepage_vma_check(vma, vma->vm_flags | VM_HUGEPAGE)) + if (!hugepage_vma_check(vma, vma->vm_flags | VM_HUGEPAGE, false)) return; =20 /* Keep pmd pgtable for uffd-wp; see comment in retract_page_tables() */ @@ -2143,7 +2103,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned = int pages, progress++; break; } - if (!hugepage_vma_check(vma, vma->vm_flags)) { + if (!hugepage_vma_check(vma, vma->vm_flags, false)) { skip: progress++; continue; --=20 2.26.3 From nobody Mon Apr 27 07:26:20 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DBA3BC433EF for ; Wed, 15 Jun 2022 17:30:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354874AbiFORaH (ORCPT ); Wed, 15 Jun 2022 13:30:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40706 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355146AbiFOR3m (ORCPT ); Wed, 15 Jun 2022 13:29:42 -0400 Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4F43E42A0D for ; Wed, 15 Jun 2022 10:29:41 -0700 (PDT) Received: by mail-pl1-x62f.google.com with SMTP id u18so11001080plb.3 for ; Wed, 15 Jun 2022 10:29:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=h2pLxupVhk89AMwfS8APe/ZQV3YQbagepd9ct77VG2Q=; b=puz5OfQ8vLFujYwqvU8w7iV6yjTTZbZpPMN7QBZX+grfhQ8IiGAqV4Se0C4Hdpiqvk OrkRmY9ak/Ct1UNJeweplSPK8Z22nychYkNPpUVMCs9fmfsI3cnnL8uZ/6xu4GUj2Sgo J+QN9JT58X0TXJxeRnLaSw4M2w8qUirxCowi3hvZafGwpujCLqfR0bulYpJSAvqzrrYu YppV1M+A/XdzlYXyuxa3QGsniodVzcqYP0rcAPPNL/a8PvX1e0ebmRJ7lpkOlWAjeclP X/QwDhTqBnIvVXPKKCdnKarZSJCUOPQsgCB0HFh57WgD3UatQhyLHfOQyskhk9kIJUoH Ouaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=h2pLxupVhk89AMwfS8APe/ZQV3YQbagepd9ct77VG2Q=; b=w0jTJmTDnrDamJ5rs7gJVni3uSnDqaISRy3DhBjmCJfFDuvMWDR1ikQJvL5T4hsa4Y J9eXRHgQyBk0Z7iUtz0eOlFGjl92Y2jyn80WEPXXEwnxb/Mk4q6EbNw+rmC0solXqyTA A3ev7vItXOj752Bugqt/WBd47DJr8mQ89oDMllqE4zqdakhBpGl96ZTc/M7W62/NE9MG ya57ft+teL7OyfWIZHTFBUv4vqJy7tJSkhpql61xKlgENzCLvDQ3Gh1emXczKp8bkfKn rN4Zj/hR6IggvmcJQYYoLYinGAtQ59PgxdJHOEaz0XghArmU0E094OoRqm9ZYpuADkPx Nf1A== X-Gm-Message-State: AJIora/WfKbCkJgM6k/1P7G9MDmjathAvUb92raqNb4gWB5Z/1dqnkSU g6k1DrwEr0hdDSyKsY2qJBc= X-Google-Smtp-Source: AGRyM1uWyukWlQxI0iqWLK7ytf/4APIc7YOybkeB4SN9ZpOj92E5RGABSQGWJMfAfoOSrKD/V4E+iw== X-Received: by 2002:a17:90a:94cb:b0:1e3:4295:9c18 with SMTP id j11-20020a17090a94cb00b001e342959c18mr519315pjw.53.1655314180969; Wed, 15 Jun 2022 10:29:40 -0700 (PDT) Received: from localhost.localdomain (c-67-174-241-145.hsd1.ca.comcast.net. [67.174.241.145]) by smtp.gmail.com with ESMTPSA id w14-20020a1709029a8e00b001676f87473fsm9552244plp.302.2022.06.15.10.29.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jun 2022 10:29:40 -0700 (PDT) From: Yang Shi To: vbabka@suse.cz, kirill.shutemov@linux.intel.com, willy@infradead.org, zokeefe@google.com, linmiaohe@huawei.com, akpm@linux-foundation.org Cc: shy828301@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [v4 PATCH 5/7] mm: thp: kill __transhuge_page_enabled() Date: Wed, 15 Jun 2022 10:29:24 -0700 Message-Id: <20220615172926.546974-6-shy828301@gmail.com> X-Mailer: git-send-email 2.26.3 In-Reply-To: <20220615172926.546974-1-shy828301@gmail.com> References: <20220615172926.546974-1-shy828301@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The page fault path checks THP eligibility with __transhuge_page_enabled() which does the similar thing as hugepage_vma_check(), so use hugepage_vma_check() instead. However page fault allows DAX and !anon_vma cases, so added a new flag, in_pf, to hugepage_vma_check() to make page fault work correctly. The in_pf flag is also used to skip shmem and file THP for page fault since shmem handles THP in its own shmem_fault() and file THP allocation on fault is not supported yet. Also remove hugepage_vma_enabled() since hugepage_vma_check() is the only caller now, it is not necessary to have a helper function. Signed-off-by: Yang Shi Reviewed-by: Zach O'Keefe --- fs/proc/task_mmu.c | 2 +- include/linux/huge_mm.h | 57 ++--------------------------------------- mm/huge_memory.c | 51 ++++++++++++++++++++++++++++-------- mm/khugepaged.c | 8 +++--- mm/memory.c | 7 +++-- 5 files changed, 52 insertions(+), 73 deletions(-) diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 39a40ec181e7..cef72e49acc5 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -863,7 +863,7 @@ static int show_smap(struct seq_file *m, void *v) __show_smap(m, &mss, false); =20 seq_printf(m, "THPeligible: %d\n", - hugepage_vma_check(vma, vma->vm_flags, true)); + hugepage_vma_check(vma, vma->vm_flags, true, false)); =20 if (arch_pkeys_enabled()) seq_printf(m, "ProtectionKey: %8u\n", vma_pkey(vma)); diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index aeb13119ee28..9d97d7ee6234 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -146,48 +146,6 @@ static inline bool transhuge_vma_suitable(struct vm_ar= ea_struct *vma, return true; } =20 -static inline bool transhuge_vma_enabled(struct vm_area_struct *vma, - unsigned long vm_flags) -{ - /* Explicitly disabled through madvise. */ - if ((vm_flags & VM_NOHUGEPAGE) || - test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags)) - return false; - return true; -} - -/* - * to be used on vmas which are known to support THP. - * Use transparent_hugepage_active otherwise - */ -static inline bool __transparent_hugepage_enabled(struct vm_area_struct *v= ma) -{ - - /* - * If the hardware/firmware marked hugepage support disabled. - */ - if (transparent_hugepage_flags & (1 << TRANSPARENT_HUGEPAGE_NEVER_DAX)) - return false; - - if (!transhuge_vma_enabled(vma, vma->vm_flags)) - return false; - - if (vma_is_temporary_stack(vma)) - return false; - - if (transparent_hugepage_flags & (1 << TRANSPARENT_HUGEPAGE_FLAG)) - return true; - - if (vma_is_dax(vma)) - return true; - - if (transparent_hugepage_flags & - (1 << TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)) - return !!(vma->vm_flags & VM_HUGEPAGE); - - return false; -} - static inline bool file_thp_enabled(struct vm_area_struct *vma) { struct inode *inode; @@ -204,7 +162,7 @@ static inline bool file_thp_enabled(struct vm_area_stru= ct *vma) =20 bool hugepage_vma_check(struct vm_area_struct *vma, unsigned long vm_flags, - bool smaps); + bool smaps, bool in_pf); =20 #define transparent_hugepage_use_zero_page() \ (transparent_hugepage_flags & \ @@ -348,11 +306,6 @@ static inline bool folio_test_pmd_mappable(struct foli= o *folio) return false; } =20 -static inline bool __transparent_hugepage_enabled(struct vm_area_struct *v= ma) -{ - return false; -} - static inline bool transparent_hugepage_active(struct vm_area_struct *vma) { return false; @@ -364,15 +317,9 @@ static inline bool transhuge_vma_suitable(struct vm_ar= ea_struct *vma, return false; } =20 -static inline bool transhuge_vma_enabled(struct vm_area_struct *vma, - unsigned long vm_flags) -{ - return false; -} - static inline bool hugepage_vma_check(struct vm_area_struct *vma, unsigned long vm_flags, - bool smaps) + bool smaps, bool in_pf) { return false; } diff --git a/mm/huge_memory.c b/mm/huge_memory.c index a28c6100b491..d0c37d99917b 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -71,24 +71,50 @@ unsigned long huge_zero_pfn __read_mostly =3D ~0UL; =20 bool hugepage_vma_check(struct vm_area_struct *vma, unsigned long vm_flags, - bool smaps) + bool smaps, bool in_pf) { - if (!transhuge_vma_enabled(vma, vm_flags)) + /* + * Explicitly disabled through madvise or prctl, or some + * architectures may disable THP for some mappings, for + * example, s390 kvm. + * */ + if ((vm_flags & VM_NOHUGEPAGE) || + test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags)) return false; - - if (vm_flags & VM_NO_KHUGEPAGED) + /* + * If the hardware/firmware marked hugepage support disabled. + */ + if (transparent_hugepage_flags & (1 << TRANSPARENT_HUGEPAGE_NEVER_DAX)) return false; =20 - /* Don't run khugepaged against DAX vma */ + /* khugepaged doesn't collapse DAX vma, but page fault is fine. */ if (vma_is_dax(vma)) + return in_pf; + + /* + * Special VMA and hugetlb VMA. + * Must be checked after dax since some dax mappings may have + * VM_MIXEDMAP set. + */ + if (vm_flags & VM_NO_KHUGEPAGED) return false; =20 - /* Check alignment for file vma and size for both file and anon vma */ - if (!transhuge_vma_suitable(vma, (vma->vm_end - HPAGE_PMD_SIZE))) + /* + * Check alignment for file vma and size for both file and anon vma. + * + * Skip the check for page fault. Huge fault does the check in fault + * handlers. And this check is not suitable for huge PUD fault. + */ + if (!in_pf && + !transhuge_vma_suitable(vma, (vma->vm_end - HPAGE_PMD_SIZE))) return false; =20 - /* Enabled via shmem mount options or sysfs settings. */ - if (shmem_file(vma->vm_file)) + /* + * Enabled via shmem mount options or sysfs settings. + * Must be done before hugepage flags check since shmem has its + * own flags. + */ + if (!in_pf && shmem_file(vma->vm_file)) return shmem_huge_enabled(vma); =20 if (!khugepaged_enabled()) @@ -99,7 +125,7 @@ bool hugepage_vma_check(struct vm_area_struct *vma, return false; =20 /* Only regular file is valid */ - if (file_thp_enabled(vma)) + if (!in_pf && file_thp_enabled(vma)) return true; =20 if (!vma_is_anonymous(vma)) @@ -111,9 +137,12 @@ bool hugepage_vma_check(struct vm_area_struct *vma, /* * THPeligible bit of smaps should show 1 for proper VMAs even * though anon_vma is not initialized yet. + * + * Allow page fault since anon_vma may be not initialized until + * the first page fault. */ if (!vma->anon_vma) - return smaps; + return (smaps || in_pf); =20 return true; } diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 3afd87f8c0b1..2a676f37c921 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -473,7 +473,7 @@ void khugepaged_enter_vma(struct vm_area_struct *vma, { if (!test_bit(MMF_VM_HUGEPAGE, &vma->vm_mm->flags) && khugepaged_enabled()) { - if (hugepage_vma_check(vma, vm_flags, false)) + if (hugepage_vma_check(vma, vm_flags, false, false)) __khugepaged_enter(vma->vm_mm); } } @@ -918,7 +918,7 @@ static int hugepage_vma_revalidate(struct mm_struct *mm= , unsigned long address, =20 if (!transhuge_vma_suitable(vma, address)) return SCAN_ADDRESS_RANGE; - if (!hugepage_vma_check(vma, vma->vm_flags, false)) + if (!hugepage_vma_check(vma, vma->vm_flags, false, false)) return SCAN_VMA_CHECK; /* * Anon VMA expected, the address may be unmapped then @@ -1408,7 +1408,7 @@ void collapse_pte_mapped_thp(struct mm_struct *mm, un= signed long addr) * the valid THP. Add extra VM_HUGEPAGE so hugepage_vma_check() * will not fail the vma for missing VM_HUGEPAGE */ - if (!hugepage_vma_check(vma, vma->vm_flags | VM_HUGEPAGE, false)) + if (!hugepage_vma_check(vma, vma->vm_flags | VM_HUGEPAGE, false, false)) return; =20 /* Keep pmd pgtable for uffd-wp; see comment in retract_page_tables() */ @@ -2103,7 +2103,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned = int pages, progress++; break; } - if (!hugepage_vma_check(vma, vma->vm_flags, false)) { + if (!hugepage_vma_check(vma, vma->vm_flags, false, false)) { skip: progress++; continue; diff --git a/mm/memory.c b/mm/memory.c index be724238a9d3..fee2884481f2 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4985,6 +4985,7 @@ static vm_fault_t __handle_mm_fault(struct vm_area_st= ruct *vma, .gfp_mask =3D __get_fault_gfp_mask(vma), }; struct mm_struct *mm =3D vma->vm_mm; + unsigned long vm_flags =3D vma->vm_flags; pgd_t *pgd; p4d_t *p4d; vm_fault_t ret; @@ -4998,7 +4999,8 @@ static vm_fault_t __handle_mm_fault(struct vm_area_st= ruct *vma, if (!vmf.pud) return VM_FAULT_OOM; retry_pud: - if (pud_none(*vmf.pud) && __transparent_hugepage_enabled(vma)) { + if (pud_none(*vmf.pud) && + hugepage_vma_check(vma, vm_flags, false, true)) { ret =3D create_huge_pud(&vmf); if (!(ret & VM_FAULT_FALLBACK)) return ret; @@ -5031,7 +5033,8 @@ static vm_fault_t __handle_mm_fault(struct vm_area_st= ruct *vma, if (pud_trans_unstable(vmf.pud)) goto retry_pud; =20 - if (pmd_none(*vmf.pmd) && __transparent_hugepage_enabled(vma)) { + if (pmd_none(*vmf.pmd) && + hugepage_vma_check(vma, vm_flags, false, true)) { ret =3D create_huge_pmd(&vmf); if (!(ret & VM_FAULT_FALLBACK)) return ret; --=20 2.26.3 From nobody Mon Apr 27 07:26:20 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EBD52C433EF for ; Wed, 15 Jun 2022 17:30:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1357517AbiFORaN (ORCPT ); Wed, 15 Jun 2022 13:30:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356183AbiFOR35 (ORCPT ); Wed, 15 Jun 2022 13:29:57 -0400 Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D05EB49F34 for ; Wed, 15 Jun 2022 10:29:42 -0700 (PDT) Received: by mail-pj1-x102a.google.com with SMTP id t3-20020a17090a510300b001ea87ef9a3dso2671389pjh.4 for ; Wed, 15 Jun 2022 10:29:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=BxodM/enWfpl7ZA6N5naFXFtK7edc51Vh1QDre/mU/Q=; b=mkhCdkIJNqb0v6VU4lByixweHMSmazQOcfv8y2ewFiVeKtD8M+w7to2fu1BHM9jFdW FnkzXfY6+FrSC06EzCmia1Hfqwj1UVUHQBir/i6VaPcvgTdyxFi/D0bk0ChP45wRaEP7 i0x+6MMSNGr6PWLhRBI/MFVrSihpDL13tYcEA5WFGBI73Ue+rR1ygElAlQtRw2JOk8mv lrCeuu2Rhb9PYkgS4A0u3VW2Sok5anSb27z8+p8NuL0bW/OEUtFQkKXGTRPY9nAjsuWR BU1bLwPC0owvKLfTqg4S7y1AcndfGkSb6Q6aDEtdQYxtLn1MEzxy/KNL8S+tIYR50lXG VKcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=BxodM/enWfpl7ZA6N5naFXFtK7edc51Vh1QDre/mU/Q=; b=V+AJ5tad1VOlxso26twWxFKz4l2NMR1sOQtqbtinu2c9oMdDlAebhaDEsrup5WfCk9 sNgukMxw5VDl9UWubLgSusOqD/V+mdqnBYMDCtQaN04v/qk1LLMyKkL9wzVJNy1UsRUv yfe/C9D1maOxZEcGoUCqdUbsncFkVofaSQzBaREYMAAHWDkmwt7MuVfJeV0qvlK2bkrl 0B0NKkjznl42UUNoGeVf+WVFguohoZ/ebASKJNdvYXw4yPT+4glKq2as7ZaF1ww+voky p64w/3ndRs6+ZkjqphEqH4MJUNnV1TT93HDBMwc5pClOcAVgLig86e70jkOe/+MbCNWF cw1A== X-Gm-Message-State: AJIora+teTOnQSs/B7n49D+ahipkL3riWaIfBeBxABrfylfsCRDhsk9b 8k8jkT0fxPmcOwFC89Fgfcw= X-Google-Smtp-Source: AGRyM1txU2kmgua+NX3rp4duO3aRrjlN4ZOjkSUAr8RCRc2/lLC+ogmCrebuBsavl/ElraFx4uynEA== X-Received: by 2002:a17:903:186:b0:167:6d8c:68c4 with SMTP id z6-20020a170903018600b001676d8c68c4mr789311plg.91.1655314182347; Wed, 15 Jun 2022 10:29:42 -0700 (PDT) Received: from localhost.localdomain (c-67-174-241-145.hsd1.ca.comcast.net. [67.174.241.145]) by smtp.gmail.com with ESMTPSA id w14-20020a1709029a8e00b001676f87473fsm9552244plp.302.2022.06.15.10.29.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jun 2022 10:29:41 -0700 (PDT) From: Yang Shi To: vbabka@suse.cz, kirill.shutemov@linux.intel.com, willy@infradead.org, zokeefe@google.com, linmiaohe@huawei.com, akpm@linux-foundation.org Cc: shy828301@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [v4 PATCH 6/7] mm: khugepaged: reorg some khugepaged helpers Date: Wed, 15 Jun 2022 10:29:25 -0700 Message-Id: <20220615172926.546974-7-shy828301@gmail.com> X-Mailer: git-send-email 2.26.3 In-Reply-To: <20220615172926.546974-1-shy828301@gmail.com> References: <20220615172926.546974-1-shy828301@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The khugepaged_{enabled|always|req_madv} are not khugepaged only anymore, move them to huge_mm.h and rename to hugepage_flags_xxx, and remove khugepaged_req_madv due to no users. Also move khugepaged_defrag to khugepaged.c since its only caller is in that file, it doesn't have to be in a header file. Signed-off-by: Yang Shi Reviewed-by: Zach O'Keefe --- include/linux/huge_mm.h | 8 ++++++++ include/linux/khugepaged.h | 14 -------------- mm/huge_memory.c | 4 ++-- mm/khugepaged.c | 18 +++++++++++------- 4 files changed, 21 insertions(+), 23 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index 9d97d7ee6234..4cf546af7d97 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -116,6 +116,14 @@ extern struct kobj_attribute shmem_enabled_attr; =20 extern unsigned long transparent_hugepage_flags; =20 +#define hugepage_flags_enabled() \ + (transparent_hugepage_flags & \ + ((1<flags)) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index d0c37d99917b..0f2cce2d7041 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -117,11 +117,11 @@ bool hugepage_vma_check(struct vm_area_struct *vma, if (!in_pf && shmem_file(vma->vm_file)) return shmem_huge_enabled(vma); =20 - if (!khugepaged_enabled()) + if (!hugepage_flags_enabled()) return false; =20 /* THP settings require madvise. */ - if (!(vm_flags & VM_HUGEPAGE) && !khugepaged_always()) + if (!(vm_flags & VM_HUGEPAGE) && !hugepage_flags_always()) return false; =20 /* Only regular file is valid */ diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 2a676f37c921..d8ebb60aae36 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -472,7 +472,7 @@ void khugepaged_enter_vma(struct vm_area_struct *vma, unsigned long vm_flags) { if (!test_bit(MMF_VM_HUGEPAGE, &vma->vm_mm->flags) && - khugepaged_enabled()) { + hugepage_flags_enabled()) { if (hugepage_vma_check(vma, vm_flags, false, false)) __khugepaged_enter(vma->vm_mm); } @@ -763,6 +763,10 @@ static bool khugepaged_scan_abort(int nid) return false; } =20 +#define khugepaged_defrag() \ + (transparent_hugepage_flags & \ + (1< X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 19E65C43334 for ; Wed, 15 Jun 2022 17:30:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1357575AbiFORaQ (ORCPT ); Wed, 15 Jun 2022 13:30:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40536 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356282AbiFOR35 (ORCPT ); Wed, 15 Jun 2022 13:29:57 -0400 Received: from mail-pf1-x430.google.com (mail-pf1-x430.google.com [IPv6:2607:f8b0:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 582D64C418 for ; Wed, 15 Jun 2022 10:29:44 -0700 (PDT) Received: by mail-pf1-x430.google.com with SMTP id u37so3633090pfg.3 for ; Wed, 15 Jun 2022 10:29:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=YtollJahbUnQdMIR/aYwe0ysfbXt4BsX5JTH7cAUXak=; b=N4NuWQSKWQiM9FdTh0JRYtj0u28EsEtECIADCbJ92IumR+tzfD/GaDHyaEZCwEPZ45 ndkfZiW/jfRsteeB9yP9cN9gJTN6ydS9wdWzU+oLNT1WC4vOvwoBdzmDrBH+qpNvAxag JpSIsvOqGHCmhbOYwzkelddRSkQVvjIZMqs7jmrSBxqN0FpowIgVL6ZXoXHpGmHD3PZ+ S2uzDoLcfArRPZ8Mne+aFujCMcOt0U6HXoPEWgUn+o315U9ROrtTdkAbIiHOPZYkP5fK idIjGjTzUH0kLYHJQkeMhS/nKieRKIcCdF/6ipPVUxwBIgIBjV91PFInRIenewHra95S GHdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=YtollJahbUnQdMIR/aYwe0ysfbXt4BsX5JTH7cAUXak=; b=5rE3HVPnhTXGafy/OZbA2UDaX3QB7XQB+23K/fjjk4DrlO1W0oN7ffvWoLIPXahdoP Bv9Ho5c1sOugPThS5DU1knWbA5gBdFPzIlyZmSsUJgRZqCb0extR94F7m19LPQz7jUuJ IA/z0GsP+iYojsl6VElkNd4Jp146ABOo6+qeLlnPvB7NQlIITwjWlpxYP3jbo0aBs5Ib aESLsJnhjtOKYDk1yiVz6+Vx133injfApfBC8m7ufBQyqHsoPaSiYxePFTCaYi0HRNC7 xcuG2+UwCDznlFryYEIlfnnHHT2e07ShKajJV/tclWWWF1nARf5skXXDswOZJ/kKcHfs SKMw== X-Gm-Message-State: AJIora/8wbqTpm2pypA+Cf1Gmn34+S8pZneN/CYh8ffGCXS5OTmCHMeB B+6hhZx/w/wCa3M+Tv8FXGc= X-Google-Smtp-Source: AGRyM1s7KETEepArG5JL8meMY5y+t8s8chQ/4O2VLPIxoO5+mpzvdwZBLrR6Ib9aQtPfZIU27L5KBg== X-Received: by 2002:a63:234f:0:b0:405:3981:be7 with SMTP id u15-20020a63234f000000b0040539810be7mr742484pgm.15.1655314183940; Wed, 15 Jun 2022 10:29:43 -0700 (PDT) Received: from localhost.localdomain (c-67-174-241-145.hsd1.ca.comcast.net. [67.174.241.145]) by smtp.gmail.com with ESMTPSA id w14-20020a1709029a8e00b001676f87473fsm9552244plp.302.2022.06.15.10.29.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jun 2022 10:29:43 -0700 (PDT) From: Yang Shi To: vbabka@suse.cz, kirill.shutemov@linux.intel.com, willy@infradead.org, zokeefe@google.com, linmiaohe@huawei.com, akpm@linux-foundation.org Cc: shy828301@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [v4 PATCH 7/7] doc: proc: fix the description to THPeligible Date: Wed, 15 Jun 2022 10:29:26 -0700 Message-Id: <20220615172926.546974-8-shy828301@gmail.com> X-Mailer: git-send-email 2.26.3 In-Reply-To: <20220615172926.546974-1-shy828301@gmail.com> References: <20220615172926.546974-1-shy828301@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The THPeligible bit shows 1 if and only if the VMA is eligible for allocating THP and the THP is also PMD mappable. Some misaligned file VMAs may be eligible for allocating THP but the THP can't be mapped by PMD. Make this more explictly to avoid ambiguity. Signed-off-by: Yang Shi Reviewed-by: Zach O'Keefe --- Documentation/filesystems/proc.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Documentation/filesystems/proc.rst b/Documentation/filesystems= /proc.rst index 1bc91fb8c321..a5e41e636a1a 100644 --- a/Documentation/filesystems/proc.rst +++ b/Documentation/filesystems/proc.rst @@ -514,8 +514,10 @@ replaced by copy-on-write) part of the underlying shme= m object out on swap. "SwapPss" shows proportional swap share of this mapping. Unlike "Swap", th= is does not take into account swapped out page of underlying shmem objects. "Locked" indicates whether the mapping is locked in memory or not. + "THPeligible" indicates whether the mapping is eligible for allocating THP -pages - 1 if true, 0 otherwise. It just shows the current status. +pages as well as the THP is PMD mappable or not - 1 if true, 0 otherwise. +It just shows the current status. =20 "VmFlags" field deserves a separate description. This member represents the kernel flags associated with the particular virtual memory area in two let= ter --=20 2.26.3