From patchwork Mon Jun 6 03:45:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen Wandun X-Patchwork-Id: 12869907 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 A242DC43334 for ; Mon, 6 Jun 2022 03:25:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E38548D0003; Sun, 5 Jun 2022 23:25:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DC31A8D0001; Sun, 5 Jun 2022 23:25:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C5EFA8D0003; Sun, 5 Jun 2022 23:25:03 -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 B61EF8D0001 for ; Sun, 5 Jun 2022 23:25:03 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 8EA502031F for ; Mon, 6 Jun 2022 03:25:03 +0000 (UTC) X-FDA: 79546369686.11.F6520DF Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by imf14.hostedemail.com (Postfix) with ESMTP id 34F1E10004B for ; Mon, 6 Jun 2022 03:25:02 +0000 (UTC) Received: from dggpemm500021.china.huawei.com (unknown [172.30.72.53]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4LGf2K3qn5z1K9CB; Mon, 6 Jun 2022 11:23:05 +0800 (CST) Received: from dggpemm500002.china.huawei.com (7.185.36.229) by dggpemm500021.china.huawei.com (7.185.36.109) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 6 Jun 2022 11:24:51 +0800 Received: from localhost.localdomain (10.175.112.125) by dggpemm500002.china.huawei.com (7.185.36.229) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 6 Jun 2022 11:24:50 +0800 From: Chen Wandun To: , , , , , , CC: Subject: [PATCH v3 1/3] mm/shmem: check return value of shmem_init_inodecache Date: Mon, 6 Jun 2022 11:45:28 +0800 Message-ID: <20220606034530.153505-2-chenwandun@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220606034530.153505-1-chenwandun@huawei.com> References: <20220606034530.153505-1-chenwandun@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 dggpemm500002.china.huawei.com (7.185.36.229) X-CFilter-Loop: Reflected X-Stat-Signature: wb8qwb4qmys5m5upbgyp9whag9cpqebt Authentication-Results: imf14.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf14.hostedemail.com: domain of chenwandun@huawei.com designates 45.249.212.255 as permitted sender) smtp.mailfrom=chenwandun@huawei.com X-Rspam-User: X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 34F1E10004B X-HE-Tag: 1654485902-152508 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: It will result in null pointer access if shmem_init_inodecache fail, so check return value of shmem_init_inodecache Signed-off-by: Chen Wandun --- mm/shmem.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 12d45a03f7fc..7419ab219b97 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -3775,11 +3775,16 @@ static void shmem_init_inode(void *foo) inode_init_once(&info->vfs_inode); } -static void shmem_init_inodecache(void) +static int shmem_init_inodecache(void) { shmem_inode_cachep = kmem_cache_create("shmem_inode_cache", sizeof(struct shmem_inode_info), 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode); + + if (!shmem_inode_cachep) + return -ENOMEM; + + return 0; } static void shmem_destroy_inodecache(void) @@ -3923,7 +3928,9 @@ void __init shmem_init(void) { int error; - shmem_init_inodecache(); + error = shmem_init_inodecache(); + if (error) + goto out2; error = register_filesystem(&shmem_fs_type); if (error) { From patchwork Mon Jun 6 03:45:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen Wandun X-Patchwork-Id: 12869908 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 1B9A8C433EF for ; Mon, 6 Jun 2022 03:25:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 31B898D0001; Sun, 5 Jun 2022 23:25:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2A2E08D0005; Sun, 5 Jun 2022 23:25:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 193DC8D0001; Sun, 5 Jun 2022 23:25:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 083EB8D0005 for ; Sun, 5 Jun 2022 23:25:04 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id CF2FD205FF for ; Mon, 6 Jun 2022 03:25:03 +0000 (UTC) X-FDA: 79546369686.09.7F44C42 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by imf18.hostedemail.com (Postfix) with ESMTP id 310411C0013 for ; Mon, 6 Jun 2022 03:24:41 +0000 (UTC) Received: from dggpemm500020.china.huawei.com (unknown [172.30.72.54]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4LGf2N6NZ1z1K9GX; Mon, 6 Jun 2022 11:23:08 +0800 (CST) Received: from dggpemm500002.china.huawei.com (7.185.36.229) by dggpemm500020.china.huawei.com (7.185.36.49) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 6 Jun 2022 11:24:54 +0800 Received: from localhost.localdomain (10.175.112.125) by dggpemm500002.china.huawei.com (7.185.36.229) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 6 Jun 2022 11:24:54 +0800 From: Chen Wandun To: , , , , , , CC: Subject: [PATCH v3 2/3] mm/shmem: return error code directly for invalid addr Date: Mon, 6 Jun 2022 11:45:29 +0800 Message-ID: <20220606034530.153505-3-chenwandun@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220606034530.153505-1-chenwandun@huawei.com> References: <20220606034530.153505-1-chenwandun@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 dggpemm500002.china.huawei.com (7.185.36.229) X-CFilter-Loop: Reflected X-Rspam-User: Authentication-Results: imf18.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf18.hostedemail.com: domain of chenwandun@huawei.com designates 45.249.212.255 as permitted sender) smtp.mailfrom=chenwandun@huawei.com X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 310411C0013 X-Stat-Signature: se4e8cqtqg59pniqx8s6endkwb6fq6od X-HE-Tag: 1654485881-636681 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: Return error code directly for addr is not PAGE_SIZE aligned or beyond TASK_SIZE, no need to check these cases in caller. Signed-off-by: Chen Wandun --- mm/shmem.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 7419ab219b97..48b7172f81d6 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2142,10 +2142,10 @@ unsigned long shmem_get_unmapped_area(struct file *file, return addr; if (IS_ERR_VALUE(addr)) return addr; - if (addr & ~PAGE_MASK) - return addr; + if (offset_in_page(addr)) + return -EINVAL; if (addr > TASK_SIZE - len) - return addr; + return -ENOMEM; if (shmem_huge == SHMEM_HUGE_DENY) return addr; @@ -2196,7 +2196,7 @@ unsigned long shmem_get_unmapped_area(struct file *file, inflated_addr = get_area(NULL, uaddr, inflated_len, 0, flags); if (IS_ERR_VALUE(inflated_addr)) return addr; - if (inflated_addr & ~PAGE_MASK) + if (offset_in_page(inflated_addr)) return addr; inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1); From patchwork Mon Jun 6 03:45:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen Wandun X-Patchwork-Id: 12869909 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 EB4F1C43334 for ; Mon, 6 Jun 2022 03:25:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7D8F28D0006; Sun, 5 Jun 2022 23:25:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 78A708D0005; Sun, 5 Jun 2022 23:25:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 69E5E8D0006; Sun, 5 Jun 2022 23:25:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 56BAE8D0005 for ; Sun, 5 Jun 2022 23:25:10 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id 301A6803CE for ; Mon, 6 Jun 2022 03:25:10 +0000 (UTC) X-FDA: 79546369980.10.D137D0E Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf04.hostedemail.com (Postfix) with ESMTP id 5659B40039 for ; Mon, 6 Jun 2022 03:24:47 +0000 (UTC) Received: from dggpemm500022.china.huawei.com (unknown [172.30.72.57]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4LGf2z0V6bzjXDJ; Mon, 6 Jun 2022 11:23:39 +0800 (CST) Received: from dggpemm500002.china.huawei.com (7.185.36.229) by dggpemm500022.china.huawei.com (7.185.36.162) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 6 Jun 2022 11:24:56 +0800 Received: from localhost.localdomain (10.175.112.125) by dggpemm500002.china.huawei.com (7.185.36.229) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 6 Jun 2022 11:24:55 +0800 From: Chen Wandun To: , , , , , , CC: Subject: [PATCH v3 3/3] mm/shmem: rework calculation of inflated_addr in shmem_get_unmapped_area Date: Mon, 6 Jun 2022 11:45:30 +0800 Message-ID: <20220606034530.153505-4-chenwandun@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220606034530.153505-1-chenwandun@huawei.com> References: <20220606034530.153505-1-chenwandun@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 dggpemm500002.china.huawei.com (7.185.36.229) X-CFilter-Loop: Reflected X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 5659B40039 X-Stat-Signature: oeif71w9qygo6mh3fg3k6z7z7qry8hi3 X-Rspam-User: Authentication-Results: imf04.hostedemail.com; dkim=none; spf=pass (imf04.hostedemail.com: domain of chenwandun@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=chenwandun@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com X-HE-Tag: 1654485887-238655 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: In function shmem_get_unmapped_area, inflated_offset and offset are unsigned long, it will result in underflow when offset below inflated_offset, a little confusing, no functional change. Signed-off-by: Chen Wandun --- mm/shmem.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 48b7172f81d6..ac277b11bdfa 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2200,9 +2200,12 @@ unsigned long shmem_get_unmapped_area(struct file *file, return addr; inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1); - inflated_addr += offset - inflated_offset; - if (inflated_offset > offset) + if (offset > inflated_offset) + inflated_addr += offset - inflated_offset; + else if (offset < inflated_offset) { + inflated_addr -= inflated_offset - offset; inflated_addr += HPAGE_PMD_SIZE; + } if (inflated_addr > TASK_SIZE - len) return addr;