From patchwork Thu Oct 27 02:58:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: lizetao X-Patchwork-Id: 13021498 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 739F3C38A2D for ; Thu, 27 Oct 2022 01:55:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DB5B38E0002; Wed, 26 Oct 2022 21:55:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D65CD8E0001; Wed, 26 Oct 2022 21:55:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C550E8E0002; Wed, 26 Oct 2022 21:55:32 -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 B70258E0001 for ; Wed, 26 Oct 2022 21:55:32 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 85E35120329 for ; Thu, 27 Oct 2022 01:55:32 +0000 (UTC) X-FDA: 80065062504.21.99A442A Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf26.hostedemail.com (Postfix) with ESMTP id E49F3140005 for ; Thu, 27 Oct 2022 01:55:29 +0000 (UTC) Received: from kwepemi500012.china.huawei.com (unknown [172.30.72.53]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4MyTJw0lVNzHvMP; Thu, 27 Oct 2022 09:55:12 +0800 (CST) Received: from huawei.com (10.175.101.6) by kwepemi500012.china.huawei.com (7.221.188.12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Thu, 27 Oct 2022 09:55:24 +0800 From: Li Zetao To: , , , CC: , , , , , Li Zetao Subject: [PATCH] mm/mmap: Fix memory leak in mmap_region() Date: Thu, 27 Oct 2022 10:58:37 +0800 Message-ID: <20221027025837.136492-1-lizetao1@huawei.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Originating-IP: [10.175.101.6] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To kwepemi500012.china.huawei.com (7.221.188.12) X-CFilter-Loop: Reflected ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1666835732; 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=K5kxLSvsaRAKBCCdhwDuoW1PdJLxVODfQ1k4sVAD1FQ=; b=XryqNTrLtWFndJPPzbn895WB/hCnAkYNR8gHvctU1gwsqFn2kOpB5sVFTbATyAb84IVwzC gdck5E2yxSa2o/vZ/9sc4wjyu/wT9vUK5LB6AiIETikw7+NIfiwjahbNlsJiCGMRd8OPow o85tlGp1hwOjn5YiNmjNsDQ7ZApCLgA= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf26.hostedemail.com: domain of lizetao1@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=lizetao1@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1666835732; a=rsa-sha256; cv=none; b=VRPvGj9SHqc69pFp1sgzXLKtitoM9eiopjXkK80TDNy9gwViTHJUbjftuj9G1Tcxdb6sIS EBo+pO8kYbc0tCCEtUz/2R3jFeFmixsBq5BQ0k+NhTsflOXbVYR5zy3NsL11tqxGmx668x O1FQMwL2cpnRh5koAxpLle0CLFbun7I= X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: E49F3140005 X-Rspam-User: Authentication-Results: imf26.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf26.hostedemail.com: domain of lizetao1@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=lizetao1@huawei.com X-Stat-Signature: ukyyg6mtghdhqm3eg1br4h7teri634im X-HE-Tag: 1666835729-745177 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: There is a memory leak reported by kmemleak: unreferenced object 0xffff88817231ce40 (size 224): comm "mount.cifs", pid 19308, jiffies 4295917571 (age 405.880s) hex dump (first 32 bytes): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 60 c0 b2 00 81 88 ff ff 98 83 01 42 81 88 ff ff `..........B.... backtrace: [] __alloc_file+0x21/0x250 [] alloc_empty_file+0x41/0xf0 [] alloc_file+0x59/0x710 [] alloc_file_pseudo+0x154/0x210 [] __shmem_file_setup+0xff/0x2a0 [] shmem_zero_setup+0x8d/0x160 [] mmap_region+0x1075/0x19d0 [] do_mmap+0x727/0x1110 [] vm_mmap_pgoff+0x112/0x1e0 [] do_syscall_64+0x35/0x80 [] entry_SYSCALL_64_after_hwframe+0x46/0xb0 The root cause was traced to an error handing path in mmap_region() when arch_validate_flags() or mas_preallocate() fails. In the shared anonymous mapping sence, vma will be setuped and mapped with a new shared anonymous file via shmem_zero_setup(). So in this case, the file resource needs to be released. Fix it by calling fput(vma->vm_file) when arch_validate_flags() or mas_preallocate() returns an error. And for the beauty of the code, put fput() under mapping_unmap_writable(). Fixes: d4af56c5c7c6 ("mm: start tracking VMAs with maple tree") Fixes: c462ac288f2c ("mm: Introduce arch_validate_flags()") Signed-off-by: Li Zetao --- mm/mmap.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index e270057ed04e..8530195b3ec5 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2674,6 +2674,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr, error = -EINVAL; if (file) goto close_and_free_vma; + else if (vm_flags & VM_SHARED) + goto put_vma_file; else goto free_vma; } @@ -2682,6 +2684,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr, error = -ENOMEM; if (file) goto close_and_free_vma; + else if (vm_flags & VM_SHARED) + goto put_vma_file; else goto free_vma; } @@ -2746,13 +2750,13 @@ unsigned long mmap_region(struct file *file, unsigned long addr, if (vma->vm_ops && vma->vm_ops->close) vma->vm_ops->close(vma); unmap_and_free_vma: - fput(vma->vm_file); - vma->vm_file = NULL; - /* Undo any partial mapping done by a device driver. */ unmap_region(mm, mas.tree, vma, prev, next, vma->vm_start, vma->vm_end); if (vm_flags & VM_SHARED) mapping_unmap_writable(file->f_mapping); +put_vma_file: + fput(vma->vm_file); + vma->vm_file = NULL; free_vma: vm_area_free(vma); unacct_error: