From patchwork Mon Oct 28 14:56:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kefeng Wang X-Patchwork-Id: 13853639 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 6E8FFD33992 for ; Mon, 28 Oct 2024 14:57:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F1E446B0089; Mon, 28 Oct 2024 10:57:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ECDC26B008C; Mon, 28 Oct 2024 10:57:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DBCFE6B0092; Mon, 28 Oct 2024 10:57:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id C49716B0089 for ; Mon, 28 Oct 2024 10:57:43 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 419441C55D0 for ; Mon, 28 Oct 2024 14:57:43 +0000 (UTC) X-FDA: 82723314156.19.7B7B18B Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf01.hostedemail.com (Postfix) with ESMTP id 743B640004 for ; Mon, 28 Oct 2024 14:57:20 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=none; spf=pass (imf01.hostedemail.com: domain of wangkefeng.wang@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=wangkefeng.wang@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730127302; 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-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references; bh=ui5cpwb6Tw8NRzcPOhjmqQaxQOVZGqWX1AuklEuYaPs=; b=usNucMGHtmZlfHUc4VjKe6FRDBpSD7OuHGG8mWVtvDnLqvUkLSjMXHN/1TbhuKxrynG22L edh1nZKu2c7oUHy5sixtfSpNWqBqYbSwyOzGFmTXunKrIBB30XUvfILe+kf6jzOUPD7Nm9 ji2I+cSC0p7DkUYgoq9xt1jD7dJymXw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730127302; a=rsa-sha256; cv=none; b=0TMyrThLNPnD2kpgvAd8A8WhnVOE+3ZoMYlBnDefuIIUQppAyOwHgqsXUBWKjUul4NLgy2 9087BzcW9i29H1GdSQcJheg+1p4BGFGKLd5eknsbbE8XnDw9uH6Hrv7ki7jjiOKtdeihD+ fwwWOT2d9wb73wwPoyrnUtnyjIeASqQ= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=none; spf=pass (imf01.hostedemail.com: domain of wangkefeng.wang@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=wangkefeng.wang@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4XcbzJ68bDz10P6k; Mon, 28 Oct 2024 22:55:24 +0800 (CST) Received: from dggpemf100008.china.huawei.com (unknown [7.185.36.138]) by mail.maildlp.com (Postfix) with ESMTPS id 940D4140135; Mon, 28 Oct 2024 22:57:33 +0800 (CST) Received: from localhost.localdomain (10.175.112.125) by dggpemf100008.china.huawei.com (7.185.36.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Mon, 28 Oct 2024 22:57:33 +0800 From: Kefeng Wang To: Andrew Morton CC: David Hildenbrand , Matthew Wilcox , Muchun Song , "Huang, Ying" , , Kefeng Wang Subject: [PATCH v3 1/2] mm: use aligned address in clear_gigantic_page() Date: Mon, 28 Oct 2024 22:56:55 +0800 Message-ID: <20241028145656.932941-1-wangkefeng.wang@huawei.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Originating-IP: [10.175.112.125] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpemf100008.china.huawei.com (7.185.36.138) X-Stat-Signature: ox3reicprq7k9p96rnfth5owbfbqgmx9 X-Rspamd-Queue-Id: 743B640004 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1730127440-513549 X-HE-Meta: U2FsdGVkX19vIWzisyDUsp6aH0ZXeLM/ek1n1L1GRIR18ukFiqDYcV5Qx39Uxn6N/1qqrFw6AUbGZ3RWjRGomF7wVy/kZargioaj2xhRCKxRNMcpduqhSM3vszs3GWzEWZWismcDjv4h9qMcuEAnq9y9X6typ7FeBa2RvT8Bv1I7QwKicZUExXhYW6YPWPEtyHsFDg9qq/YG4ViA3xckLVP9v78LifhYZKgX2BTN1Jg6YuLXQBwmN9IZU7q11dz92LZeh5fQnsWRpMYl5whTTGHdU8TsyXj1dUSSPQ4jR2dgCfPWfbcfmL8MwEQO3EGDC5M1Wo2vRZg3sj06fARdUP6aRNQ/0c8rYsRaGpxXvIN8aMZb8PoPdW7PeP6m7D1KKQXicIxcGfS1pkEYnHoISmXo/ZAZywwCL2fTsAFbARGLjIEw9dowGpYTcthSM/mlVT6/2zHGXujfPnb8b3mSN19hcj6TWaEKYPafzXGm4pvl7SbQfzGRiDbSG2qAg0hR3IsKpMs3/jzrtI+dXrrvF7i2xmaoXLLSKkE4EdIobexDIC9OYC8gFb0qHLOiG6kY/fC7iyYBlFkl0YdVWZqeQ/3l/UgeSPfjFFNwyB+E0tMmJpSXugNOx9kIken2ZbnhYEhZVPIIH3XE2yGHCuE6x4gJVZzkrMXfo+0jrUBi9ba3mJSss5VMzfJkk0kmaWy4h3TeVHdlOwT9TH3WLKMVPXe7R/Hyd9FA8o6gkUQwaA0WqZX5LnqpCQLxY/J/FURVEW+szPYUuk9YWiX/FKDeicIbL+vAoaVhMqZvWXpWfaKAERjqGHOUSxAfAyiWW2H+o5Us0MZdZ3nT2desOoqlFJpYYD8EcBWIdailsW02u+C094c0Iv1cNcsDdD5PlOeg+fQ6YiNLE23vlVGUN6e+MtZNLqZ/llMt5J3ZBzk4jUHIMXpg2PiSukf+Vjvr8lL1xPjsGISyjUUUXmeEBnK 931kNa7/ RbMZfxI92Lv+hnlBYmMfBY87wGZYCSHQr7TNvi6dVs59UecY487zj5ybXq2WTOvV5BMQLjkkRz1jsXZxd32ystvrLBLUdHQwIGfZc10cOcQhbB8zFJhMB4ob4b0oZj/enc2euHnk6eJnLGyY4iJacBsz+pYa2pvfI/pK26HdWgbvEvx/3v9mfX2ihAIUYq3zJwCbI 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: List-Subscribe: List-Unsubscribe: In current kernel, hugetlb_no_page() calls folio_zero_user() with the fault address. Where the fault address may be not aligned with the huge page size. Then, folio_zero_user() may call clear_gigantic_page() with the address, while clear_gigantic_page() requires the address to be huge page size aligned. So, this may cause memory corruption or information leak, addtional, use more obvious naming 'addr_hint' instead of 'addr' for clear_gigantic_page(). Fixes: 78fefd04c123 ("mm: memory: convert clear_huge_page() to folio_zero_user()") Signed-off-by: Kefeng Wang Reviewed-by: "Huang, Ying" Reviewed-by: David Hildenbrand --- v3: - revise patch description, suggested by Huang Ying - use addr_hint for clear_gigantic_page(), suggested by David v2: - update changelog to clarify the impact, per Andrew fs/hugetlbfs/inode.c | 2 +- mm/memory.c | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index a4441fb77f7c..a5ea006f403e 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -825,7 +825,7 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset, error = PTR_ERR(folio); goto out; } - folio_zero_user(folio, ALIGN_DOWN(addr, hpage_size)); + folio_zero_user(folio, addr); __folio_mark_uptodate(folio); error = hugetlb_add_to_page_cache(folio, mapping, index); if (unlikely(error)) { diff --git a/mm/memory.c b/mm/memory.c index 75c2dfd04f72..84864387f965 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -6815,9 +6815,10 @@ static inline int process_huge_page( return 0; } -static void clear_gigantic_page(struct folio *folio, unsigned long addr, +static void clear_gigantic_page(struct folio *folio, unsigned long addr_hint, unsigned int nr_pages) { + unsigned long addr = ALIGN_DOWN(addr_hint, folio_size(folio)); int i; might_sleep(); From patchwork Mon Oct 28 14:56:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kefeng Wang X-Patchwork-Id: 13853640 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 7E762D33990 for ; Mon, 28 Oct 2024 14:57:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F1ED86B008C; Mon, 28 Oct 2024 10:57:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ED0186B0092; Mon, 28 Oct 2024 10:57:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D96896B0093; Mon, 28 Oct 2024 10:57:44 -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 BE8F36B008C for ; Mon, 28 Oct 2024 10:57:44 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 6D613A02FA for ; Mon, 28 Oct 2024 14:57:44 +0000 (UTC) X-FDA: 82723315038.14.77A35D0 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by imf02.hostedemail.com (Postfix) with ESMTP id 9077780009 for ; Mon, 28 Oct 2024 14:56:56 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=none; spf=pass (imf02.hostedemail.com: domain of wangkefeng.wang@huawei.com designates 45.249.212.189 as permitted sender) smtp.mailfrom=wangkefeng.wang@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730127252; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1mPTJyGQcEm5j63O67VUTQq3hLo8NkV5QGizMH85nQ0=; b=WSbd3C8fnjFZ95NMOKS8aTAUMBckKdq/syMJ8azh/sWTxowjAdZxsYGKPTY6O41rRl0xCy 3iN2z3vM80nwVsvxnJEHS+uCeBiLH83LhkQRcSBjkjaC9b6myjiLm+1TGCcFeMrlXm9R8S A0QC6Y8liz13ovdM3zyNaEtplLdqroQ= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=none; spf=pass (imf02.hostedemail.com: domain of wangkefeng.wang@huawei.com designates 45.249.212.189 as permitted sender) smtp.mailfrom=wangkefeng.wang@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730127252; a=rsa-sha256; cv=none; b=Cis/RuHfaBAoinsVcytduPFM+buxVplQHPeYFYT9LRQW4q3BZs8tNPOYKpKhgPJ8AmJEDc ajjw7xJaI0RAo0LMgLXa7isDiK312eywjPxboJcM633Rey7tvMfQFD4oY7kE/YmnrHQWht c28qka1v3VKkATVcDiTycTKm5tKHsSM= Received: from mail.maildlp.com (unknown [172.19.162.254]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4Xcc0h3CLwzQsNG; Mon, 28 Oct 2024 22:56:36 +0800 (CST) Received: from dggpemf100008.china.huawei.com (unknown [7.185.36.138]) by mail.maildlp.com (Postfix) with ESMTPS id 0C0A01800DE; Mon, 28 Oct 2024 22:57:34 +0800 (CST) Received: from localhost.localdomain (10.175.112.125) by dggpemf100008.china.huawei.com (7.185.36.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Mon, 28 Oct 2024 22:57:33 +0800 From: Kefeng Wang To: Andrew Morton CC: David Hildenbrand , Matthew Wilcox , Muchun Song , "Huang, Ying" , , Kefeng Wang Subject: [PATCH v3 2/2] mm: use aligned address in copy_user_gigantic_page() Date: Mon, 28 Oct 2024 22:56:56 +0800 Message-ID: <20241028145656.932941-2-wangkefeng.wang@huawei.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20241028145656.932941-1-wangkefeng.wang@huawei.com> References: <20241028145656.932941-1-wangkefeng.wang@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.112.125] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpemf100008.china.huawei.com (7.185.36.138) X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 9077780009 X-Stat-Signature: 3oc9wsgbsa5tpt83gtqzdir5gemo7erz X-Rspam-User: X-HE-Tag: 1730127416-698699 X-HE-Meta: U2FsdGVkX1/Ly26bdOB5ggN0rDVHpmZqI4MC3ec4JEtvSpehPNm9FbF/gtBjurbsOAygIw2PnEVRN9QsWYoEZXavcgl9eDAOem5PpGXMbXxEjS0PdzOueyrrIZtPaPy63p8+K5g/Eh/y1dGgVDsk5E4ndQgiKnRb2iIDPpLBiUW8hU0TerQnUmb5oZty/S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ner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: In current kernel, hugetlb_wp() calls copy_user_large_folio() with the fault address. Where the fault address may be not aligned with the huge page size. Then, copy_user_large_folio() may call copy_user_gigantic_page() with the address, while copy_user_gigantic_page() requires the address to be huge page size aligned. So, this may cause memory corruption or information leak, addtional, use more obvious naming 'addr_hint' instead of 'addr' for copy_user_gigantic_page(). Fixes: 530dd9926dc1 ("mm: memory: improve copy_user_large_folio()") Signed-off-by: Kefeng Wang Reviewed-by: David Hildenbrand --- v3: - revise patch description, suggested by Huang Ying - use addr_hint for copy_user_gigantic_page(), suggested by David v2: - update changelog to clarify the impact, per Andrew mm/hugetlb.c | 5 ++--- mm/memory.c | 5 +++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 2c8c5da0f5d3..15b5d46d49d2 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5338,7 +5338,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, break; } ret = copy_user_large_folio(new_folio, pte_folio, - ALIGN_DOWN(addr, sz), dst_vma); + addr, dst_vma); folio_put(pte_folio); if (ret) { folio_put(new_folio); @@ -6641,8 +6641,7 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte, *foliop = NULL; goto out; } - ret = copy_user_large_folio(folio, *foliop, - ALIGN_DOWN(dst_addr, size), dst_vma); + ret = copy_user_large_folio(folio, *foliop, dst_addr, dst_vma); folio_put(*foliop); *foliop = NULL; if (ret) { diff --git a/mm/memory.c b/mm/memory.c index 84864387f965..209885a4134f 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -6852,13 +6852,14 @@ void folio_zero_user(struct folio *folio, unsigned long addr_hint) } static int copy_user_gigantic_page(struct folio *dst, struct folio *src, - unsigned long addr, + unsigned long addr_hint, struct vm_area_struct *vma, unsigned int nr_pages) { - int i; + unsigned long addr = ALIGN_DOWN(addr_hint, folio_size(dst)); struct page *dst_page; struct page *src_page; + int i; for (i = 0; i < nr_pages; i++) { dst_page = folio_page(dst, i);