From patchwork Tue Jun 11 10:11:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 13693378 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 B5C46C25B76 for ; Tue, 11 Jun 2024 10:11:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 776C16B00A8; Tue, 11 Jun 2024 06:11:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 701806B00A9; Tue, 11 Jun 2024 06:11:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 506316B00AA; Tue, 11 Jun 2024 06:11:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 286D76B00A8 for ; Tue, 11 Jun 2024 06:11:29 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id D76EB12130C for ; Tue, 11 Jun 2024 10:11:28 +0000 (UTC) X-FDA: 82218190656.03.7F4A81F Received: from out30-99.freemail.mail.aliyun.com (out30-99.freemail.mail.aliyun.com [115.124.30.99]) by imf27.hostedemail.com (Postfix) with ESMTP id 849B440029 for ; Tue, 11 Jun 2024 10:11:26 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=Jptw3rzn; spf=pass (imf27.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.99 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com; dmarc=pass (policy=none) header.from=linux.alibaba.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1718100687; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ihHol0Lo4aQL92vXhUE0q0HG3Xsyoz8HlIbQSl3GlRA=; b=ZbMOdVhKglfelgyHmVDv5/yr3xhmLwUc/vIcoqBB6ZMyaR+CwuWpM5O/epWAAOHYsebBcK IqxonnVFIdNXqOrozIbLz5NC09X7tKZKMS2VIKUWzcv8sG4gEvbELEehCX/qfD2XMQRI7T 77l19mpMUOQUjbCSJBkevs8qW8KWcW0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718100687; a=rsa-sha256; cv=none; b=3/HtKyOBbluGFWwN/5IvU+S7m8OvwUPUG+mLC9sTnybZ1EpObYD4bzrM0Wt7c1mfU+GYmn 4Be90xuMWx6XfXFDw3J8NPqdAA1kbQs7j2JRWzZwYgJi2jxZKKfxWbKzaOEDHN4mIyicT+ tsJYdfrCh34xu0eh8NUUV3DhMruVD4o= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=Jptw3rzn; spf=pass (imf27.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.99 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com; dmarc=pass (policy=none) header.from=linux.alibaba.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1718100682; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=ihHol0Lo4aQL92vXhUE0q0HG3Xsyoz8HlIbQSl3GlRA=; b=Jptw3rznsqoHQxQkCYMKVYim7AsECX9znu/4R94NJp8vLd4WcIzqq+a0yaHYvsN5Bwt54eTffUbUeq1WyXmROKAbiVoPr2290bOnpEePq7Cx23kngnfjzAdFNZycFGJFwpb2enBjzGuxIGdknmyd9dla+hIkBGFUeBLOFw+eu1I= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R161e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033045075189;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=16;SR=0;TI=SMTPD_---0W8G1Jrq_1718100680; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W8G1Jrq_1718100680) by smtp.aliyun-inc.com; Tue, 11 Jun 2024 18:11:21 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com Cc: willy@infradead.org, david@redhat.com, wangkefeng.wang@huawei.com, ying.huang@intel.com, 21cnbao@gmail.com, ryan.roberts@arm.com, shy828301@gmail.com, ziy@nvidia.com, ioworker0@gmail.com, da.gomez@samsung.com, p.raghav@samsung.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v5 5/6] mm: shmem: add mTHP size alignment in shmem_get_unmapped_area Date: Tue, 11 Jun 2024 18:11:09 +0800 Message-Id: <0c549b57cf7db07503af692d8546ecfad0fcce52.1718090413.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: References: MIME-Version: 1.0 X-Stat-Signature: ot71ssk4nursrbgx679177se4gtxx37t X-Rspamd-Queue-Id: 849B440029 X-Rspam-User: X-Rspamd-Server: rspam05 X-HE-Tag: 1718100686-848679 X-HE-Meta: U2FsdGVkX186+qZ+MVDNc/GVXRV9ybdFoyJLrfCg5LnvJnJOL3P2V+q+bcUC9zNAZy8vpAi+w7z9WCh1kZM64yrQmCk9SHqDXWOy4/qnUS6DMO4+8ummOrc5GaD2LGUI/dlH1G1Pl8NWOrsHvnMtBKtNzeRzcFhYSGpFPzAi9ruHIUV2g3UDXrBddSDyQC+8wBA/GVJlNhM15Fls/vnodtNKm6/pEge85F9DPOUy3RUH3vV9MIXpcFjbuQN8SRT1LWUEC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, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Although the top-level hugepage allocation can be turned off, anonymous shmem can still use mTHP by configuring the sysfs interface located at '/sys/kernel/mm/transparent_hugepage/hugepage-XXkb/shmem_enabled'. Therefore, add alignment for mTHP size to provide a suitable alignment address in shmem_get_unmapped_area(). Signed-off-by: Baolin Wang Tested-by: Lance Yang --- mm/shmem.c | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index e849c88452b2..f5469c357be6 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2394,6 +2394,7 @@ unsigned long shmem_get_unmapped_area(struct file *file, unsigned long inflated_len; unsigned long inflated_addr; unsigned long inflated_offset; + unsigned long hpage_size; if (len > TASK_SIZE) return -ENOMEM; @@ -2412,8 +2413,6 @@ unsigned long shmem_get_unmapped_area(struct file *file, if (shmem_huge == SHMEM_HUGE_DENY) return addr; - if (len < HPAGE_PMD_SIZE) - return addr; if (flags & MAP_FIXED) return addr; /* @@ -2425,8 +2424,11 @@ unsigned long shmem_get_unmapped_area(struct file *file, if (uaddr == addr) return addr; + hpage_size = HPAGE_PMD_SIZE; if (shmem_huge != SHMEM_HUGE_FORCE) { struct super_block *sb; + unsigned long __maybe_unused hpage_orders; + int order = 0; if (file) { VM_BUG_ON(file->f_op != &shmem_file_operations); @@ -2439,18 +2441,38 @@ unsigned long shmem_get_unmapped_area(struct file *file, if (IS_ERR(shm_mnt)) return addr; sb = shm_mnt->mnt_sb; + + /* + * Find the highest mTHP order used for anonymous shmem to + * provide a suitable alignment address. + */ +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + hpage_orders = READ_ONCE(huge_shmem_orders_always); + hpage_orders |= READ_ONCE(huge_shmem_orders_within_size); + hpage_orders |= READ_ONCE(huge_shmem_orders_madvise); + if (SHMEM_SB(sb)->huge != SHMEM_HUGE_NEVER) + hpage_orders |= READ_ONCE(huge_shmem_orders_inherit); + + if (hpage_orders > 0) { + order = highest_order(hpage_orders); + hpage_size = PAGE_SIZE << order; + } +#endif } - if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER) + if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER && !order) return addr; } - offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1); - if (offset && offset + len < 2 * HPAGE_PMD_SIZE) + if (len < hpage_size) + return addr; + + offset = (pgoff << PAGE_SHIFT) & (hpage_size - 1); + if (offset && offset + len < 2 * hpage_size) return addr; - if ((addr & (HPAGE_PMD_SIZE-1)) == offset) + if ((addr & (hpage_size - 1)) == offset) return addr; - inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE; + inflated_len = len + hpage_size - PAGE_SIZE; if (inflated_len > TASK_SIZE) return addr; if (inflated_len < len) @@ -2463,10 +2485,10 @@ unsigned long shmem_get_unmapped_area(struct file *file, if (inflated_addr & ~PAGE_MASK) return addr; - inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1); + inflated_offset = inflated_addr & (hpage_size - 1); inflated_addr += offset - inflated_offset; if (inflated_offset > offset) - inflated_addr += HPAGE_PMD_SIZE; + inflated_addr += hpage_size; if (inflated_addr > TASK_SIZE - len) return addr;