From mboxrd@z Thu Jan 1 00:00:00 1970 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 2138CD20690 for ; Wed, 16 Oct 2024 02:22:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 942526B007B; Tue, 15 Oct 2024 22:22:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8F30A6B0082; Tue, 15 Oct 2024 22:22:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7E1746B0083; Tue, 15 Oct 2024 22:22:25 -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 612B76B007B for ; Tue, 15 Oct 2024 22:22:25 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 0A0321A0D56 for ; Wed, 16 Oct 2024 02:22:07 +0000 (UTC) X-FDA: 82677865956.06.93B4EDE Received: from out-187.mta1.migadu.com (out-187.mta1.migadu.com [95.215.58.187]) by imf28.hostedemail.com (Postfix) with ESMTP id 0890BC0009 for ; Wed, 16 Oct 2024 02:22:14 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=ULTqGZWT; spf=pass (imf28.hostedemail.com: domain of muchun.song@linux.dev designates 95.215.58.187 as permitted sender) smtp.mailfrom=muchun.song@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729045269; a=rsa-sha256; cv=none; b=mHoj4pA0Odl3GiCYUhB2aE3b6p0XnW5NOBx28vBwxb/mEt4CPARZ85TiMUjslkIViY/72H 6ECXNSZriS5AnpqhUUSBhPTN4OCzFoMSJv/1U+Ivk9uhKElYSxmYgHTQ8jGgVJawWb67w6 PMKFtdBVpA+sFXfk2/yPglHKHycfj3Y= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=ULTqGZWT; spf=pass (imf28.hostedemail.com: domain of muchun.song@linux.dev designates 95.215.58.187 as permitted sender) smtp.mailfrom=muchun.song@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729045269; 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:dkim-signature; bh=EqMEPBv9cbTNi1dL6eZXF7Kd8dswrfA1mgY3e9zu5E4=; b=s1apRluVY0rP3SYS4tYBEoLkb7+fn6LoPz4x6SuJK73/YzyLZcIB+wNITwYrIKkXNnQ9XH eu4h14MN5lMCr7M8TlUHfMiePuqRf5aHV77ckPe6DZUerO6L8AS27LIn6Wb2KAX5bMhxhA uPxWwg1heIHJr2EBMxvzGch9t677g6Y= Content-Type: text/plain; charset=us-ascii DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1729045340; h=from:from: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=EqMEPBv9cbTNi1dL6eZXF7Kd8dswrfA1mgY3e9zu5E4=; b=ULTqGZWThzVRjIWzTix80c/Piy17H6duMu8563CHHIeupJaV4GSfC7AxlxVGVX+NBgTEtK z0vpjt0Lx9eVYZQiLhM8Gvm/5mKCLaAUw+PulHntAgIcfglI8I2mDUr/uvPAqFUeveQAe2 aHzFN1dk53JF/pJwKn/LBaMZVfTFIBw= Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3818.100.11.1.3\)) Subject: Re: [PATCH v2] mm: shrinker: avoid memleak in alloc_shrinker_info X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Muchun Song In-Reply-To: <4a18e997-3a94-4248-8923-c3764d12b0d6@huawei.com> Date: Wed, 16 Oct 2024 10:21:30 +0800 Cc: Anshuman Khandual , "Kirill A. Shutemov" , akpm@linux-foundation.org, david@fromorbit.com, zhengqi.arch@bytedance.com, roman.gushchin@linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, wangweiyang2@huawei.com Content-Transfer-Encoding: quoted-printable Message-Id: References: <20241014032336.482088-1-chenridong@huaweicloud.com> <4a18e997-3a94-4248-8923-c3764d12b0d6@huawei.com> To: chenridong X-Migadu-Flow: FLOW_OUT X-Stat-Signature: kb8g5fnudac6u6jaoo1arqo69peuxmsz X-Rspamd-Queue-Id: 0890BC0009 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1729045334-482804 X-HE-Meta: U2FsdGVkX1/+VfShDLKe5muUDzJ+OxCSSG8YEDl+VkHv3Li5rQ5w5EgPqliJMMhtB0lyjV2VCQDTqy2RbO133qmiQTcGi9DgVeTE00yEMGcGOUUErb5PZV49dZuO5KlPYVq2lvSDrJofDESX7RE5yW52nySGjlmAtL2a5OhxrXH/C0FyUQRUEC8NbME+XFXrQxLqwVV2lIMsfHXK2/G9+OXkfrQ7hi5M43IJRQNsT29InBBhatQbhMLjsamjOgFTrkKzTtOU79qII80wvOXvMeuLUXLdra1a+RE1jWOZS5VbjdXwlppGjl6uvPAk8MqOWIwWyR8xsNMsdXapgojYOEMmxpSykLjVuEy0ChoQdgeTPb+X74ZKJ54Xu4NoiMxT7ul9EU8e+yaNpkOEPWv/d3NvkqVXCCGsmXpLS4eIHt9JYyLhiUso/2GL/im1HiBw8e9QkfOL36xDnRecF4MuMe2nfbrhwZVt+UTUaKp2U8e8CWLHYXkk1eM4ZJ1gwdP1mnD18PBynl69woSiVud8eK5ByqjQT21iveyOLlb+JalFLFDVmw08Ra+tPGgdDyNOP/YsGYgTuqu/yL1YGNXreBGI/cKzn9zfLA/tirsabKfV7VsAQFkgVhBH76YXiYVJKVlxT9wKxaI8XcFjPWEq92vX1XBIrvyfB1fbRLCKkJeU4PzvKtTjEbzzQtrpqVx7dqCBFhbbWYdCd0rHaYsB/NhBIVcHCOJN6aKu+tLoa7ArWhbyBuHoQybaO6XBepjSRk/Pyr3m0lyvksNaxX1nsDfPyWhm6JYDpbQnFEvTWE3I3fspc4/Hh+kVxt0aIlyZkBDpBTqfzPoU8uHh7Xa/UWAUVSYdXF1qyez0WRunaVVPDrP+69K3WvcY8TbdKgYe18hvsjsz1BpeF9iZ4q9Mts0dhhjq5DH0TirdtO4WjhiysH90XXpufVdfpGWWX9S88N/e3KmMyzMD7iShg68 dlBuPVXo 6lNThbAjTlqDYaDsmcU51KhTNziebJ65Dr2utd0yxRs4mlAJiRCwyBNaQfW9DRt5PTBOt2KePO0Wz0sI6UENcG1WMuq7u/ZcSPzMAbK7mQkky2g8vw4SEDwwhSpsZJYJVpoZLvLHWT0iofDnDXUbkfOklXq+x8MoX85Tx+Sh80I8ELQwjt4XGjRZRDZRoZgnItmwyk/ancXlCfavdLggZ1k88bfPzbbVY0KDMpPop7Nj62Y8O5e16LQbrkw== 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: > On Oct 16, 2024, at 09:25, chenridong wrote: >=20 >=20 >=20 > On 2024/10/15 14:55, Anshuman Khandual wrote: >> On 10/14/24 16:59, Kirill A. Shutemov wrote: >>> On Mon, Oct 14, 2024 at 03:23:36AM +0000, Chen Ridong wrote: >>>> From: Chen Ridong >>>>=20 >>>> A memleak was found as bellow: >>>>=20 >>>> unreferenced object 0xffff8881010d2a80 (size 32): >>>> comm "mkdir", pid 1559, jiffies 4294932666 >>>> hex dump (first 32 bytes): >>>> 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 = ................ >>>> 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 = @............... >>>> backtrace (crc 2e7ef6fa): >>>> [] __kmalloc_node_noprof+0x394/0x470 >>>> [] alloc_shrinker_info+0x7b/0x1a0 >>>> [] mem_cgroup_css_online+0x11a/0x3b0 >>>> [] online_css+0x29/0xa0 >>>> [] cgroup_apply_control_enable+0x20d/0x360 >>>> [] cgroup_mkdir+0x168/0x5f0 >>>> [] kernfs_iop_mkdir+0x5e/0x90 >>>> [] vfs_mkdir+0x144/0x220 >>>> [] do_mkdirat+0x87/0x130 >>>> [] __x64_sys_mkdir+0x49/0x70 >>>> [] do_syscall_64+0x68/0x140 >>>> [] entry_SYSCALL_64_after_hwframe+0x76/0x7e >>>>=20 >>>> In the alloc_shrinker_info function, when shrinker_unit_alloc = return >>>> err, the info won't be freed. Just fix it. >>>>=20 >>>> Fixes: 307bececcd12 ("mm: shrinker: add a secondary array for = shrinker_info::{map, nr_deferred}") >>>> Signed-off-by: Chen Ridong >>>> --- >>>> mm/shrinker.c | 1 + >>>> 1 file changed, 1 insertion(+) >>>>=20 >>>> diff --git a/mm/shrinker.c b/mm/shrinker.c >>>> index dc5d2a6fcfc4..92270413190d 100644 >>>> --- a/mm/shrinker.c >>>> +++ b/mm/shrinker.c >>>> @@ -97,6 +97,7 @@ int alloc_shrinker_info(struct mem_cgroup *memcg) >>>> err: >>>> mutex_unlock(&shrinker_mutex); >>>> + kvfree(info); >>>> free_shrinker_info(memcg); >>>> return -ENOMEM; >>>> } >>>=20 >>> NAK. If in the future there going to one more error case after >>> rcu_assign_pointer() we will end up with double free. >>>=20 >>> This should be safer: >>>=20 >>> diff --git a/mm/shrinker.c b/mm/shrinker.c >>> index dc5d2a6fcfc4..763fd556bc7d 100644 >>> --- a/mm/shrinker.c >>> +++ b/mm/shrinker.c >>> @@ -87,8 +87,10 @@ int alloc_shrinker_info(struct mem_cgroup *memcg) >>> if (!info) >>> goto err; >>> info->map_nr_max =3D shrinker_nr_max; >>> - if (shrinker_unit_alloc(info, NULL, nid)) >>> + if (shrinker_unit_alloc(info, NULL, nid)) { >>> + kvfree(info); >>> goto err; >>> + } >>> rcu_assign_pointer(memcg->nodeinfo[nid]->shrinker_info, info); >>> } >>> mutex_unlock(&shrinker_mutex); >> Agreed, this is what I mentioned earlier as well. >> ------------------------------------------------------------------ >> I guess kvfree() should be called just after shrinker_unit_alloc() >> fails but before calling into "goto err" >> ------------------------------------------------------------------ >=20 > After discussion, it seems that v1 is acceptable. > Hi, Muchun, do you have any other opinions? I insist on my opinion, not mixing two different approaches to do release resources. Thanks. >=20 > Best regards, > Ridong