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 alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6ADC6FA373D for ; Thu, 27 Oct 2022 08:07:36 +0000 (UTC) Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id B7A2328F7; Thu, 27 Oct 2022 10:06:43 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz B7A2328F7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1666858053; bh=HyxcGuolgwzu3ItAus7OgJHtNbLCL6rQ15m8cAlAtEg=; h=Date:From:To:Subject:In-Reply-To:References:Cc:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=rToECf3XeiFbcZNrHvoFYe7NYZhHKRjiom657WV/KGOVm2qAXpISNAVL4GqGIzXD7 T3uyl7FnsVm2gDXM4g9WcTe96k2xnDoBHp05JwNHZWp+vC2FHJPMrFRtSsB10SZDvp 4Wqo7QmSABIncP9Yd2iHoC24jR5BDc4qbwbyQvpc= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id 32EBDF8025A; Thu, 27 Oct 2022 10:06:43 +0200 (CEST) Received: by alsa1.perex.cz (Postfix, from userid 50401) id 1D2B8F8025E; Thu, 27 Oct 2022 10:06:42 +0200 (CEST) Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id 2FC04F80100 for ; Thu, 27 Oct 2022 10:06:31 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz 2FC04F80100 Authentication-Results: alsa1.perex.cz; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="eTErvCVD"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="VIdR26Fr" Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 59641228C4; Thu, 27 Oct 2022 08:06:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1666857991; h=from:from:reply-to: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=l1hbvmIy+b7DQo66904Q6hmWtuZVS72VPYew8vdZ4UI=; b=eTErvCVD04XpVXI/Zzh7sU77er5zH+vUE21pir5ZCLSq6iLpfnfgMxxX9EgbnR2iV34+A3 4tubqB2prp3cHB5YQ4JGbU4diAXpAsRsXvT5V4fK7zjT1so07fz/CsqgWQ9qC2PrKMK6+Q z9INplGrSmc4xHnZ0P+cI3f5KtenU3o= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1666857991; h=from:from:reply-to: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=l1hbvmIy+b7DQo66904Q6hmWtuZVS72VPYew8vdZ4UI=; b=VIdR26Fro+ksgTFo3s8v4oINDnt95Nm/xdL2MtChlbWsuJkoZ4iZLNuLYCGMAbdvUICOAi tnZB9r5hArfvaXDQ== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 36FE1134CA; Thu, 27 Oct 2022 08:06:31 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id CQeYDAc8WmOuNgAAMHmgww (envelope-from ); Thu, 27 Oct 2022 08:06:31 +0000 Date: Thu, 27 Oct 2022 10:06:30 +0200 Message-ID: <87ilk5ofll.wl-tiwai@suse.de> From: Takashi Iwai To: Yang Yingliang Subject: Re: [PATCH] ALSA: aoa: i2sbus: fix possible memory leak in i2sbus_add_dev() In-Reply-To: <91e667bb-b6dd-4966-eba6-31de4f2b56a7@huawei.com> References: <20221027013438.991920-1-yangyingliang@huawei.com> <87r0ytojos.wl-tiwai@suse.de> <91e667bb-b6dd-4966-eba6-31de4f2b56a7@huawei.com> User-Agent: Wanderlust/2.15.9 (Almost Unreal) Emacs/27.2 Mule/6.0 MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit Cc: alsa-devel@alsa-project.org, tiwai@suse.com, linuxppc-dev@lists.ozlabs.org, johannes@sipsolutions.net X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: alsa-devel-bounces@alsa-project.org Sender: "Alsa-devel" On Thu, 27 Oct 2022 09:41:03 +0200, Yang Yingliang wrote: > > > On 2022/10/27 14:38, Takashi Iwai wrote: > > On Thu, 27 Oct 2022 03:34:38 +0200, > > Yang Yingliang wrote: > >> dev_set_name() in soundbus_add_one() allocates memory for name, it need be > >> freed when of_device_register() fails, call soundbus_dev_put() to give up > >> the reference that hold in device_initialize(), so that it can be freed in > >> kobject_cleanup() when the refcount hit to 0. And other resources are also > >> freed in i2sbus_release_dev(), so it can return 0 directly. > >> > >> Fixes: f3d9478b2ce4 ("[ALSA] snd-aoa: add snd-aoa") > >> Signed-off-by: Yang Yingliang > > The check of kobj state is awkward, but it seems to be the simplest > > way... Applied now. Thanks! > > Indeed, it's awkward, shall we introduce a helper like this: > > diff --git a/sound/aoa/soundbus/i2sbus/core.c > b/sound/aoa/soundbus/i2sbus/core.c > index f6841daf9e3b..950c37e0297e 100644 > --- a/sound/aoa/soundbus/i2sbus/core.c > +++ b/sound/aoa/soundbus/i2sbus/core.c > @@ -302,7 +302,7 @@ static int i2sbus_add_dev(struct macio_dev *macio, > >         if (soundbus_add_one(&dev->sound)) { >                 printk(KERN_DEBUG "i2sbus: device registration error!\n"); > -               if (dev->sound.ofdev.dev.kobj.state_initialized) { > +               if (soundbus_dev_initialized(&dev->sound)) { >                         soundbus_dev_put(&dev->sound); >                         return 0; >                 } > diff --git a/sound/aoa/soundbus/soundbus.h b/sound/aoa/soundbus/soundbus.h > index 3a99c1f1a3ca..2c9c95cf156a 100644 > --- a/sound/aoa/soundbus/soundbus.h > +++ b/sound/aoa/soundbus/soundbus.h > @@ -174,6 +174,10 @@ struct soundbus_dev { > >  extern int soundbus_add_one(struct soundbus_dev *dev); >  extern void soundbus_remove_one(struct soundbus_dev *dev); > +static inline bool soundbus_dev_initialized(struct soundbus_dev *dev) > +{ > +       return dev->ofdev.dev.kobj.state_initialized; > +} I think it's not worth much as it's used only at a single place. Takashi 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 lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id DC486C67871 for ; Thu, 27 Oct 2022 08:07:40 +0000 (UTC) Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4MydZg0nhMz3cHZ for ; Thu, 27 Oct 2022 19:07:39 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=eTErvCVD; dkim=fail reason="signature verification failed" header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=VIdR26Fr; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=suse.de (client-ip=195.135.220.28; helo=smtp-out1.suse.de; envelope-from=tiwai@suse.de; receiver=) Authentication-Results: lists.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=eTErvCVD; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=VIdR26Fr; dkim-atps=neutral Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4MydYZ53bGz2yy7 for ; Thu, 27 Oct 2022 19:06:42 +1100 (AEDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 59641228C4; Thu, 27 Oct 2022 08:06:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1666857991; h=from:from:reply-to: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=l1hbvmIy+b7DQo66904Q6hmWtuZVS72VPYew8vdZ4UI=; b=eTErvCVD04XpVXI/Zzh7sU77er5zH+vUE21pir5ZCLSq6iLpfnfgMxxX9EgbnR2iV34+A3 4tubqB2prp3cHB5YQ4JGbU4diAXpAsRsXvT5V4fK7zjT1so07fz/CsqgWQ9qC2PrKMK6+Q z9INplGrSmc4xHnZ0P+cI3f5KtenU3o= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1666857991; h=from:from:reply-to: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=l1hbvmIy+b7DQo66904Q6hmWtuZVS72VPYew8vdZ4UI=; b=VIdR26Fro+ksgTFo3s8v4oINDnt95Nm/xdL2MtChlbWsuJkoZ4iZLNuLYCGMAbdvUICOAi tnZB9r5hArfvaXDQ== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 36FE1134CA; Thu, 27 Oct 2022 08:06:31 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id CQeYDAc8WmOuNgAAMHmgww (envelope-from ); Thu, 27 Oct 2022 08:06:31 +0000 Date: Thu, 27 Oct 2022 10:06:30 +0200 Message-ID: <87ilk5ofll.wl-tiwai@suse.de> From: Takashi Iwai To: Yang Yingliang Subject: Re: [PATCH] ALSA: aoa: i2sbus: fix possible memory leak in i2sbus_add_dev() In-Reply-To: <91e667bb-b6dd-4966-eba6-31de4f2b56a7@huawei.com> References: <20221027013438.991920-1-yangyingliang@huawei.com> <87r0ytojos.wl-tiwai@suse.de> <91e667bb-b6dd-4966-eba6-31de4f2b56a7@huawei.com> User-Agent: Wanderlust/2.15.9 (Almost Unreal) Emacs/27.2 Mule/6.0 MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alsa-devel@alsa-project.org, tiwai@suse.com, linuxppc-dev@lists.ozlabs.org, johannes@sipsolutions.net, perex@perex.cz Errors-To: linuxppc-dev-bounces+linuxppc-dev=archiver.kernel.org@lists.ozlabs.org Sender: "Linuxppc-dev" On Thu, 27 Oct 2022 09:41:03 +0200, Yang Yingliang wrote: > > > On 2022/10/27 14:38, Takashi Iwai wrote: > > On Thu, 27 Oct 2022 03:34:38 +0200, > > Yang Yingliang wrote: > >> dev_set_name() in soundbus_add_one() allocates memory for name, it need be > >> freed when of_device_register() fails, call soundbus_dev_put() to give up > >> the reference that hold in device_initialize(), so that it can be freed in > >> kobject_cleanup() when the refcount hit to 0. And other resources are also > >> freed in i2sbus_release_dev(), so it can return 0 directly. > >> > >> Fixes: f3d9478b2ce4 ("[ALSA] snd-aoa: add snd-aoa") > >> Signed-off-by: Yang Yingliang > > The check of kobj state is awkward, but it seems to be the simplest > > way... Applied now. Thanks! > > Indeed, it's awkward, shall we introduce a helper like this: > > diff --git a/sound/aoa/soundbus/i2sbus/core.c > b/sound/aoa/soundbus/i2sbus/core.c > index f6841daf9e3b..950c37e0297e 100644 > --- a/sound/aoa/soundbus/i2sbus/core.c > +++ b/sound/aoa/soundbus/i2sbus/core.c > @@ -302,7 +302,7 @@ static int i2sbus_add_dev(struct macio_dev *macio, > >         if (soundbus_add_one(&dev->sound)) { >                 printk(KERN_DEBUG "i2sbus: device registration error!\n"); > -               if (dev->sound.ofdev.dev.kobj.state_initialized) { > +               if (soundbus_dev_initialized(&dev->sound)) { >                         soundbus_dev_put(&dev->sound); >                         return 0; >                 } > diff --git a/sound/aoa/soundbus/soundbus.h b/sound/aoa/soundbus/soundbus.h > index 3a99c1f1a3ca..2c9c95cf156a 100644 > --- a/sound/aoa/soundbus/soundbus.h > +++ b/sound/aoa/soundbus/soundbus.h > @@ -174,6 +174,10 @@ struct soundbus_dev { > >  extern int soundbus_add_one(struct soundbus_dev *dev); >  extern void soundbus_remove_one(struct soundbus_dev *dev); > +static inline bool soundbus_dev_initialized(struct soundbus_dev *dev) > +{ > +       return dev->ofdev.dev.kobj.state_initialized; > +} I think it's not worth much as it's used only at a single place. Takashi