From patchwork Sun Jun 5 03:55:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen Wandun X-Patchwork-Id: 12869696 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 6544FC43334 for ; Sun, 5 Jun 2022 03:47:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BCB7F6B0074; Sat, 4 Jun 2022 23:46:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B7A5F6B0075; Sat, 4 Jun 2022 23:46:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 957D76B007B; Sat, 4 Jun 2022 23:46:59 -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 761B56B0075 for ; Sat, 4 Jun 2022 23:46:59 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 4F40260112 for ; Sun, 5 Jun 2022 03:46:59 +0000 (UTC) X-FDA: 79542796158.18.ADBC854 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf01.hostedemail.com (Postfix) with ESMTP id 9E11440005 for ; Sun, 5 Jun 2022 03:46:49 +0000 (UTC) Received: from dggpemm500020.china.huawei.com (unknown [172.30.72.56]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4LG2bC3phCzjXGn; Sun, 5 Jun 2022 11:45:59 +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; Sun, 5 Jun 2022 11:46:53 +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; Sun, 5 Jun 2022 11:46:53 +0800 From: Chen Wandun To: , , , , , , Subject: [PATCH v2 1/3] mm/shmem: check return value of shmem_init_inodecache Date: Sun, 5 Jun 2022 11:55:55 +0800 Message-ID: <20220605035557.3957759-2-chenwandun@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220605035557.3957759-1-chenwandun@huawei.com> References: <20220605035557.3957759-1-chenwandun@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.112.125] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemm500002.china.huawei.com (7.185.36.229) X-CFilter-Loop: Reflected X-Rspamd-Queue-Id: 9E11440005 X-Stat-Signature: p1387zin3rmstj7z1m4811cksfra74os X-Rspam-User: Authentication-Results: imf01.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf01.hostedemail.com: domain of chenwandun@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=chenwandun@huawei.com X-Rspamd-Server: rspam08 X-HE-Tag: 1654400809-624156 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 | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 12d45a03f7fc..17f8297ece29 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -3775,11 +3775,13 @@ static void shmem_init_inode(void *foo) inode_init_once(&info->vfs_inode); } -static void shmem_init_inodecache(void) +static struct kmem_cache *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); + + return shmem_inode_cachep; } static void shmem_destroy_inodecache(void) @@ -3923,7 +3925,10 @@ void __init shmem_init(void) { int error; - shmem_init_inodecache(); + if (!shmem_init_inodecache()) { + error = -ENOMEM; + goto out2; + } error = register_filesystem(&shmem_fs_type); if (error) { From patchwork Sun Jun 5 03:55:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen Wandun X-Patchwork-Id: 12869695 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 3B727C433EF for ; Sun, 5 Jun 2022 03:47:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 752BF6B0073; Sat, 4 Jun 2022 23:46:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 72DF36B0074; Sat, 4 Jun 2022 23:46:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6169E6B0075; Sat, 4 Jun 2022 23:46:59 -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 56AA86B0073 for ; Sat, 4 Jun 2022 23:46:59 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 2ADE227A for ; Sun, 5 Jun 2022 03:46:59 +0000 (UTC) X-FDA: 79542796158.13.678DEA1 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf28.hostedemail.com (Postfix) with ESMTP id 608A7C0037 for ; Sun, 5 Jun 2022 03:46:16 +0000 (UTC) Received: from dggpemm500022.china.huawei.com (unknown [172.30.72.57]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4LG2ZG1vWCzgYYt; Sun, 5 Jun 2022 11:45:10 +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; Sun, 5 Jun 2022 11:46:55 +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; Sun, 5 Jun 2022 11:46:54 +0800 From: Chen Wandun To: , , , , , , Subject: [PATCH v2 2/3] mm/shmem: return error code directly for invalid addr Date: Sun, 5 Jun 2022 11:55:56 +0800 Message-ID: <20220605035557.3957759-3-chenwandun@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220605035557.3957759-1-chenwandun@huawei.com> References: <20220605035557.3957759-1-chenwandun@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.112.125] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemm500002.china.huawei.com (7.185.36.229) X-CFilter-Loop: Reflected X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 608A7C0037 X-Stat-Signature: rp7ym4tw8iy5c9kmimc5msoa3u13n156 Authentication-Results: imf28.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf28.hostedemail.com: domain of chenwandun@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=chenwandun@huawei.com X-HE-Tag: 1654400776-795863 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 17f8297ece29..5e61692f2d8e 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 Sun Jun 5 03:55:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen Wandun X-Patchwork-Id: 12869697 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 080FEC433EF for ; Sun, 5 Jun 2022 03:47:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 65C5B6B0075; Sat, 4 Jun 2022 23:47:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5E5366B0078; Sat, 4 Jun 2022 23:47:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 45C476B007B; Sat, 4 Jun 2022 23:47:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 2907C6B0075 for ; Sat, 4 Jun 2022 23:47:00 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id ED12D20390 for ; Sun, 5 Jun 2022 03:46:59 +0000 (UTC) X-FDA: 79542796158.25.9AE47D0 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by imf29.hostedemail.com (Postfix) with ESMTP id ECA4F120004 for ; Sun, 5 Jun 2022 03:46:44 +0000 (UTC) Received: from dggpemm500023.china.huawei.com (unknown [172.30.72.56]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4LG2c10vMZzKmK8; Sun, 5 Jun 2022 11:46:41 +0800 (CST) Received: from dggpemm500002.china.huawei.com (7.185.36.229) by dggpemm500023.china.huawei.com (7.185.36.83) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Sun, 5 Jun 2022 11:46: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; Sun, 5 Jun 2022 11:46:55 +0800 From: Chen Wandun To: , , , , , , Subject: [PATCH v2 3/3] mm/shmem: rework calculation of inflated_addr in shmem_get_unmapped_area Date: Sun, 5 Jun 2022 11:55:57 +0800 Message-ID: <20220605035557.3957759-4-chenwandun@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220605035557.3957759-1-chenwandun@huawei.com> References: <20220605035557.3957759-1-chenwandun@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.112.125] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemm500002.china.huawei.com (7.185.36.229) X-CFilter-Loop: Reflected X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: ECA4F120004 X-Stat-Signature: yfx74f8uffqopgegtq1qjto7he3ai5rs Authentication-Results: imf29.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf29.hostedemail.com: domain of chenwandun@huawei.com designates 45.249.212.189 as permitted sender) smtp.mailfrom=chenwandun@huawei.com X-HE-Tag: 1654400804-382426 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 5e61692f2d8e..0b6c61a7cbb3 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;