From patchwork Wed Aug 24 04:24:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yingliang X-Patchwork-Id: 12952898 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 A6CC6C3F6B0 for ; Wed, 24 Aug 2022 04:16:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 15DCB94000A; Wed, 24 Aug 2022 00:16:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0E5EE940008; Wed, 24 Aug 2022 00:16:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EC98A94000A; Wed, 24 Aug 2022 00:16:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id D5FAB940008 for ; Wed, 24 Aug 2022 00:16:35 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id A54A28022B for ; Wed, 24 Aug 2022 04:16:35 +0000 (UTC) X-FDA: 79833174750.18.067F75B Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf08.hostedemail.com (Postfix) with ESMTP id 74F2D160022 for ; Wed, 24 Aug 2022 04:16:34 +0000 (UTC) Received: from dggpemm500022.china.huawei.com (unknown [172.30.72.53]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4MCCPQ5npLzkWRQ; Wed, 24 Aug 2022 12:12:58 +0800 (CST) Received: from dggpemm500007.china.huawei.com (7.185.36.183) 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; Wed, 24 Aug 2022 12:16:28 +0800 Received: from huawei.com (10.175.103.91) by dggpemm500007.china.huawei.com (7.185.36.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Wed, 24 Aug 2022 12:16:28 +0800 From: Yang Yingliang To: , CC: , , , Subject: [PATCH -next] mm/nommu: fix error handling in split_vma() Date: Wed, 24 Aug 2022 12:24:24 +0800 Message-ID: <20220824042424.2031508-1-yangyingliang@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Originating-IP: [10.175.103.91] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpemm500007.china.huawei.com (7.185.36.183) X-CFilter-Loop: Reflected ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661314595; 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=TVFuR52t+EYlj9eU+65raBOLx4Mti6n4iGJB5Lp9k4g=; b=1Gbi4b30yiF5TxegQfPw8Hv/6uEiaZ81vbwILxdkbe7AxAOCPmlmuQBdHjPBDf6/yCV2az DJS2QHXOL6vFVpCbiEDXSXrbJg7d/WsCxfszxzlkH2t5Ks7XLICJwtiMVC0lLWc4eIiiLh vpdX8IcqZS8BFrzwCpzLD6sKBk0mybg= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=none; spf=pass (imf08.hostedemail.com: domain of yangyingliang@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=yangyingliang@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661314595; a=rsa-sha256; cv=none; b=fVr4lyMCfwAWbOArmAhaXokQAglbtedfqYyIo+TkbG+thH/je6+tWahEzC1ZetPZNzdLRg tnvUhzYIpyI4E1cTmP2L8esjV+R4Fa0CTs3NqUI/dXfUDas76xR1E/2oKkuginxDG0qREY HavpP3FkEWYyb4b5DEXsV5DgFTDm+4o= X-Stat-Signature: 66i5k4x6yydyjyqd7emhp1cr76fby59d X-Rspamd-Queue-Id: 74F2D160022 X-Rspam-User: Authentication-Results: imf08.hostedemail.com; dkim=none; spf=pass (imf08.hostedemail.com: domain of yangyingliang@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=yangyingliang@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com X-Rspamd-Server: rspam01 X-HE-Tag: 1661314594-159181 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: The memory allocated before calling mas_preallocate() is leaked if it fails. 'mas' won't be modify until calling mas_preallocate(), so move it up and add error label for free the memory. Fixes: 8aff7dbeaeb1 ("nommu: remove uses of VMA linked list") Signed-off-by: Yang Yingliang Reviewed-by: Liam R. Howlett --- mm/nommu.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/mm/nommu.c b/mm/nommu.c index 6c611a689ec0..214c70e1d059 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -1355,9 +1355,13 @@ int split_vma(struct mm_struct *mm, struct vm_area_struct *vma, return -ENOMEM; new = vm_area_dup(vma); - if (!new) { - kmem_cache_free(vm_region_jar, region); - return -ENOMEM; + if (!new) + goto err_vma_dup; + + if (mas_preallocate(&mas, vma, GFP_KERNEL)) { + pr_warn("Allocation of vma tree for process %d failed\n", + current->pid); + goto err_mas_preallocate; } /* most fields are the same, copy all, and then fixup */ @@ -1388,11 +1392,6 @@ int split_vma(struct mm_struct *mm, struct vm_area_struct *vma, add_nommu_region(vma->vm_region); add_nommu_region(new->vm_region); up_write(&nommu_region_sem); - if (mas_preallocate(&mas, vma, GFP_KERNEL)) { - pr_warn("Allocation of vma tree for process %d failed\n", - current->pid); - return -ENOMEM; - } setup_vma_to_mm(vma, mm); setup_vma_to_mm(new, mm); @@ -1400,6 +1399,12 @@ int split_vma(struct mm_struct *mm, struct vm_area_struct *vma, mas_store(&mas, vma); vma_mas_store(new, &mas); return 0; + +err_mas_preallocate: + vm_area_free(new); +err_vma_dup: + kmem_cache_free(vm_region_jar, region); + return -ENOMEM; } /*