From patchwork Fri May 20 21:16:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 12857450 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 4ECFCC433F5 for ; Fri, 20 May 2022 21:16:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D6D726B0073; Fri, 20 May 2022 17:16:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D1E666B0074; Fri, 20 May 2022 17:16:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B97376B0075; Fri, 20 May 2022 17:16:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id AB7FE6B0073 for ; Fri, 20 May 2022 17:16:16 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 7D3B4342B8 for ; Fri, 20 May 2022 21:16:16 +0000 (UTC) X-FDA: 79487379552.28.1F11736 Received: from mail-pg1-f171.google.com (mail-pg1-f171.google.com [209.85.215.171]) by imf10.hostedemail.com (Postfix) with ESMTP id C2C3BC0019 for ; Fri, 20 May 2022 21:15:43 +0000 (UTC) Received: by mail-pg1-f171.google.com with SMTP id 31so8724698pgp.8 for ; Fri, 20 May 2022 14:16:15 -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=gg6tNe85KQQ/g3LzRoRENfwwXGulqQh5OTFWqrUU8No=; b=PgTK3glRRfd5JkX+9f6IiYFOmD9Z9dNofGK/OipD+mpgQCHsKjBPp/AQFjZRf0O3qR hKge+wGyWYzGvM+XyongdCHlTqQcHcJKsxqSNcz7crYk+saXUcgUuxTUw2w0bZuQj1N9 Ajipf+rSlrmD0+qBqy5vme1F//nPQIIke6CoQSWdFlkX8NfeJlJqmN9bj5lDVLMt/kZx RJAn3mEOXG65s4VtYcaNT6jb+Kl6w42xRHQhZ2EpOng4nvcb026P/FE8v/RncZcKC9xv zV5b4U2kzG890iKByk0lsfUKoZgZ02xxnRFgP+WURkTaZdFSvAuz1kSFbiv7EZUYt79+ auLA== 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=gg6tNe85KQQ/g3LzRoRENfwwXGulqQh5OTFWqrUU8No=; b=04YBX1M0x4bkxSInDYZvwI8l61XArBIr8tB3FxmvlhsxQCbQuGpTmqY15xqxNSjsxi 8OTmb5HfH3Jw7IgdaoEZGZjQL8vEGZB5xyj4gudeSY7tCxWcqcqyXEwMa6ddF77i/3l9 VPsmHUC5whgDekxQ6e3xotTsKpYCxb31Pi9mHoPryDJbTbC9J11bjg4w71/aS9xh52R9 tQk81p09Y9OyH9NERMtinMmW4EdRGTpYiXGnyceTiKbkbvBNL1AlFiS40GBI8MyWqpPL aMtxNDfMiwVBO/NruhUNtk68mC6W64s/HrfT3ZQWSy49DOoc+XAyfMGLiiYm0aTo+d+9 wXGg== X-Gm-Message-State: AOAM533aob0KD9+eV5OKk5+IovxA5eP/eCH9c0yQ+eZzrKDm8tHPPDPf e7J9E2YODXnem3D55aKhdaw= X-Google-Smtp-Source: ABdhPJzSPHAO+qKZMPiyyvZY1u0P5E9VsnEg+dfk2XO7c6Ka5FUtpo8JOyZCJt+tyv57RY71VKVndw== X-Received: by 2002:a63:2002:0:b0:3c6:ae77:1869 with SMTP id g2-20020a632002000000b003c6ae771869mr9888206pgg.71.1653081375058; Fri, 20 May 2022 14:16:15 -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 h1-20020a170902748100b0015e8d4eb2c2sm167620pll.268.2022.05.20.14.16.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 May 2022 14:16:14 -0700 (PDT) From: Yang Shi To: vbabka@suse.cz, kirill.shutemov@linux.intel.com, akpm@linux-foundation.org Cc: shy828301@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/7] mm: thp: introduce transhuge_vma_size_ok() helper Date: Fri, 20 May 2022 14:16:00 -0700 Message-Id: <20220520211605.51473-3-shy828301@gmail.com> X-Mailer: git-send-email 2.26.3 In-Reply-To: <20220520211605.51473-1-shy828301@gmail.com> References: <20220520211605.51473-1-shy828301@gmail.com> MIME-Version: 1.0 X-Stat-Signature: tyemt1ouh9s9fbne8cagttb4dti16px7 X-Rspam-User: Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=PgTK3glR; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf10.hostedemail.com: domain of shy828301@gmail.com designates 209.85.215.171 as permitted sender) smtp.mailfrom=shy828301@gmail.com X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: C2C3BC0019 X-HE-Tag: 1653081343-111848 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: There are couple of places that check whether the vma size is ok for THP or not, they are open coded and duplicate, introduce transhuge_vma_size_ok() helper to do the job. Signed-off-by: Yang Shi --- include/linux/huge_mm.h | 17 +++++++++++++++++ mm/huge_memory.c | 5 +---- mm/khugepaged.c | 12 ++++++------ 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index 648cb3ce7099..a8f61db47f2a 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -116,6 +116,18 @@ extern struct kobj_attribute shmem_enabled_attr; extern unsigned long transparent_hugepage_flags; +/* + * The vma size has to be large enough to hold an aligned HPAGE_PMD_SIZE area. + */ +static inline bool transhuge_vma_size_ok(struct vm_area_struct *vma) +{ + if (round_up(vma->vm_start, HPAGE_PMD_SIZE) < + (vma->vm_end & HPAGE_PMD_MASK)) + return true; + + return false; +} + static inline bool transhuge_vma_suitable(struct vm_area_struct *vma, unsigned long addr) { @@ -345,6 +357,11 @@ static inline bool transparent_hugepage_active(struct vm_area_struct *vma) return false; } +static inline bool transhuge_vma_size_ok(struct vm_area_struct *vma) +{ + return false; +} + static inline bool transhuge_vma_suitable(struct vm_area_struct *vma, unsigned long addr) { diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 80e8b58b4f39..d633f97452c1 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -71,10 +71,7 @@ unsigned long huge_zero_pfn __read_mostly = ~0UL; bool transparent_hugepage_active(struct vm_area_struct *vma) { - /* The addr is used to check if the vma size fits */ - unsigned long addr = (vma->vm_end & HPAGE_PMD_MASK) - HPAGE_PMD_SIZE; - - if (!transhuge_vma_suitable(vma, addr)) + if (!transhuge_vma_size_ok(vma)) return false; if (vma_is_anonymous(vma)) return __transparent_hugepage_enabled(vma); diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 92e91c08d96a..5bdb30a76f05 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -454,6 +454,9 @@ bool hugepage_vma_check(struct vm_area_struct *vma, vma->vm_pgoff, HPAGE_PMD_NR)) return false; + if (!transhuge_vma_size_ok(vma)) + return false; + /* Enabled via shmem mount options or sysfs settings. */ if (shmem_file(vma->vm_file)) return shmem_huge_enabled(vma); @@ -512,9 +515,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); } @@ -2144,10 +2145,9 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, progress++; continue; } - hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK; + + hstart = round_up(vma->vm_start, HPAGE_PMD_SIZE); hend = vma->vm_end & HPAGE_PMD_MASK; - if (hstart >= hend) - goto skip; if (khugepaged_scan.address > hend) goto skip; if (khugepaged_scan.address < hstart)