Received: by 2002:a05:6500:1b8f:b0:1fa:5c73:8e2d with SMTP id df15csp1015599lqb; Wed, 29 May 2024 19:05:38 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWBk97b3UbKDlsqR+Y4JK8589KddtXdGATmwiHZIUFDcjNjfI7tVvr0i5rB7X6oUS5ZLwRG/bfaOAEERRzTZnSSVeJBSXQFGDy3BwkS0A== X-Google-Smtp-Source: AGHT+IEOPMW7KvxdMmP8PaCIIPniix+sK2vll4owrrwbhpXaDk8tOnxstvijThM0lguiK1g+xq9n X-Received: by 2002:a17:903:1cd:b0:1f6:1a86:37e6 with SMTP id d9443c01a7336-1f61a863b77mr7210195ad.23.1717034737898; Wed, 29 May 2024 19:05:37 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717034737; cv=pass; d=google.com; s=arc-20160816; b=z8PN/LtBRbPX8yk/FnK7sEZfSElm0UkiHfK05zsRvsOM9Zu4Hv5oyHQB2VwqomLPHJ ssU9RvyIV0qyd0DraG63C+m5Ylsjx+ZxX2ZWYcgVY+E/EJf8qXIuxUyyCBpaI+dA7XMB kOhJWwP39pSCMIbT4O+aCReXhDhiFm9Mri+ya/wtEn3O88tKBsuG6xjqy3uUMpeJFaxb 38M5G2GjiYy600Xuf43+l+2IeREV1h5pGdCDSAXXROvFHC9TLviGAgOpSnBxZ2/P+tds XQyozf0Ie539ex9BgtdsK9q9dgnbPuwobLyVOTUyx1tErfCw3dYwkbQlVh8h5GiyMnKS yQHw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=OpKbZmXRrURMX+/QsaD7+TkYK79RF0tdTg/q8PuO7h4=; fh=d9B6n7k7iFbk3BUsTidzz8lQOS2iyyRQbZ5BNlMEHw4=; b=ynHoDN1/F3ArPh9mrRSnJ20cDvC7tcVPj4rE2aMqBWcnVVtuvJoYOLQroWS7rf36bD cpKThsgFpUmGZX9+vjfU4ooIRABeaVmGRNpjZsUjdRiN3iY4dQNg8piSPTCuDEnYh2RT Hm9X9F6ZK7MBD+naHev0L+3iJjrNHgP/CXU1uySe3pbwB+iAoRU/C+FWPknsVYAfHIx+ mfSRpzTUy1PXaI8uZmHSaUi+YUnd2ggSS22I9sYjRgMnzFSEI+FXx/6KMiEegc0WeWjB pPOTzm1FUzmB1dqnt8vGDrYBD+dN5vF5i9O8B5Da6hGwQBH60NlLKoIysqF0f0gX5COq WYwQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.alibaba.com header.s=default header.b="XypK4/JW"; arc=pass (i=1 spf=pass spfdomain=linux.alibaba.com dkim=pass dkdomain=linux.alibaba.com dmarc=pass fromdomain=linux.alibaba.com); spf=pass (google.com: domain of linux-kernel+bounces-194873-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-194873-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.alibaba.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id d9443c01a7336-1f44c971942si4268545ad.322.2024.05.29.19.05.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 May 2024 19:05:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-194873-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.alibaba.com header.s=default header.b="XypK4/JW"; arc=pass (i=1 spf=pass spfdomain=linux.alibaba.com dkim=pass dkdomain=linux.alibaba.com dmarc=pass fromdomain=linux.alibaba.com); spf=pass (google.com: domain of linux-kernel+bounces-194873-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-194873-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.alibaba.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 84D0128306F for ; Thu, 30 May 2024 02:05:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C1A3D2C181; Thu, 30 May 2024 02:04:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="XypK4/JW" Received: from out30-100.freemail.mail.aliyun.com (out30-100.freemail.mail.aliyun.com [115.124.30.100]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D4A9A22EED for ; Thu, 30 May 2024 02:04:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.100 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717034691; cv=none; b=oX6ZHva4eAhG0yFGqF6HmyLekqwxUhIWiUwvdWqOqVK6rXJ8rqGj7qQO/dIMPApV7p1wZATnwJn71Td2UE5zfOcqU76efTF6fBND6A7pvnFDM1GI0IaX2f8D5wve7WI35D6BG6OIpKUMzzLXkJQ26cusvoymW+KbwFkvhP5/Qac= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717034691; c=relaxed/simple; bh=DCLpbiumVdaZe+rSZntSUm/RXQvdQ4mbHP4ZLPPiJzY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XhiJcyuwlPs7AlTqdBTTb78217duxwKy1zkFEFxqgNrAlDXQjVwhOBuO+Z790phYy7BsQpMM3v0YZOnmdjC6whCOvf6xJzyNrraf2xWsAwtxP2KPhUi31QXMwq9UhryaSfEo2x3YxyJC93olxPeMf3jX8fgo2VSd2kC0mKufV+E= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=XypK4/JW; arc=none smtp.client-ip=115.124.30.100 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1717034681; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=OpKbZmXRrURMX+/QsaD7+TkYK79RF0tdTg/q8PuO7h4=; b=XypK4/JWee0Alk9Dp1YxzwUyqWQRv1drfxnEnzpZy50yvbcyg5h+GsSs7+GS6MgxEnFsZkE/Z3ETrncSO+eU8tmO5STBbjQoi0ntP8FL+tNwUG09AVAeVN5On1/2LxgE9YWz6Nq9nI67f+JNzSQyVOwRkkwb/FKHjrH/sZsGoaE= X-Alimail-AntiSpam:AC=PASS;BC=-1|-1;BR=01201311R111e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037067113;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=16;SR=0;TI=SMTPD_---0W7V1J68_1717034679; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W7V1J68_1717034679) by smtp.aliyun-inc.com; Thu, 30 May 2024 10:04:39 +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 v3 5/6] mm: shmem: add mTHP size alignment in shmem_get_unmapped_area Date: Thu, 30 May 2024 10:04:16 +0800 Message-Id: <4d0b226f2a14f93d156a01855db32ff562547bcc.1717033868.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 493873d7246c..efc093ee7d4d 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,34 @@ 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_anon_shmem_orders_always); + hpage_orders |= READ_ONCE(huge_anon_shmem_orders_within_size); + hpage_orders |= READ_ONCE(huge_anon_shmem_orders_madvise); + hpage_orders |= READ_ONCE(huge_anon_shmem_orders_inherit); + 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 +2481,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; -- 2.39.3