Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752593AbdHJN0B (ORCPT ); Thu, 10 Aug 2017 09:26:01 -0400 Received: from mx07-00252a01.pphosted.com ([62.209.51.214]:40726 "EHLO mx07-00252a01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752358AbdHJNZ7 (ORCPT ); Thu, 10 Aug 2017 09:25:59 -0400 Subject: Re: [PATCH] staging: bcm2835-audio: Fix memory corruption To: Dan Carpenter Cc: Eric Anholt , Stefan Wahren , Greg Kroah-Hartman , Florian Fainelli , Aishwarya Pant , linux-rpi-kernel@lists.infradead.org, devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org References: <1502193902-87160-1-git-send-email-phil@raspberrypi.org> <20170810102155.6j373u3yktnlwsqw@mwanda> <20170810112451.nkvj7p6r7qgqqnh3@mwanda> From: Phil Elwell Message-ID: <1d59de68-22c0-6f28-ef9b-3393e457e23f@raspberrypi.org> Date: Thu, 10 Aug 2017 14:25:57 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 MIME-Version: 1.0 In-Reply-To: <20170810112451.nkvj7p6r7qgqqnh3@mwanda> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2017-08-10_03:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_spam_notspam policy=outbound_spam score=0 priorityscore=1501 malwarescore=0 suspectscore=2 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1706020000 definitions=main-1708100220 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3539 Lines: 85 On 10/08/2017 12:24, Dan Carpenter wrote: > On Thu, Aug 10, 2017 at 11:52:42AM +0100, Phil Elwell wrote: >> On 10/08/2017 11:21, Dan Carpenter wrote: >>> The original patch did not go through the normal review process... >>> >>> On Tue, Aug 08, 2017 at 01:05:02PM +0100, Phil Elwell wrote: >>>> I'm all for fixing memory leaks, but freeing a block while it is still >>>> being used is a recipe for hard-to-debug kernel exceptions. >>>> >>> >>> This bug completely breaks the driver doesn't it? It's not very subtle >>> so it should be easy to diagnose with git bisect. >> >> It's more subtle than that - the failure isn't consistent, sometimes crashing >> and sometimes not depending on how and when memory is reused. >> >>>> 1) There is already a vchi method for freeing the instance, so use it. >>>> 2) Only call it on error, and then only before initted is false. >>>> >>>> Signed-off-by: Phil Elwell >>>> Fixes: 0adbfd4694c2 ("staging: bcm2835-audio: fix memory leak in bcm2835_audio_open_connection()") >>>> --- >>>> drivers/staging/vc04_services/bcm2835-audio/bcm2835-vchiq.c | 2 +- >>>> 1 file changed, 1 insertion(+), 1 deletion(-) >>>> >>>> diff --git a/drivers/staging/vc04_services/bcm2835-audio/bcm2835-vchiq.c b/drivers/staging/vc04_services/bcm2835-audio/bcm2835-vchiq.c >>>> index 5f3d8f2..89f96f3 100644 >>>> --- a/drivers/staging/vc04_services/bcm2835-audio/bcm2835-vchiq.c >>>> +++ b/drivers/staging/vc04_services/bcm2835-audio/bcm2835-vchiq.c >>>> @@ -409,6 +409,7 @@ static int bcm2835_audio_open_connection(struct bcm2835_alsa_stream *alsa_stream >>>> LOG_ERR("%s: failed to connect VCHI instance (ret=%d)\n", >>>> __func__, ret); >>>> >>>> + vchi_disconnect(vchi_instance); >>> >>> This is ugly because why are we calling disconnect() if connect() fails? >>> These functions should be symetric so disconnect only disconnects and >>> we call a different function to undo vchi_initialise(). >> >> Agreed - I'm not going to change the API, but I can add a comment. >> > > Nah... Please don't do that. Just create a function: > > void vchiq_free(VCHIQ_INSTANCE_T *vchi_instance) > { > kfree(vchi_instance); > } > > Really vchi_initialise() is badly named and it's just a wrapper around > vchiq_initialise(). It should be deleted and vchiq_initialise() should > be renamed to allocate instead of initialize... But that's for a > separate patch. > > And also we should move the kfree() out of disconnect() like I said > and instead call vchiq_free(). But again, that's for a separate patch. > > Change the goto to "return -EIO." Leave the last error path as-is. > Eventually we will want to break this into two functions, one that > allocates the first vchi_instance and one that calls vc_vchi_audio_init(). > But again, there are many patches needed to fix this code. [ static removed ] Shouldn't that be: void vchi_uninitialise(VCHI_INSTANCE_T instance_handle) { vchiq_shutdown((VCHIQ_INSTANCE_T)instance_handle); } EXPORT_SYMBOL(vchi_uninitialise); Yes, in this case the instance is just a block of memory with no users, but if this is a general API call then you want it to work in all cases, and calling vchiq_shutdown is the right way to free an instance. Calling a VCHIQ method when all the other calls are to the VCHI API is grubby, so make it a VCHI method instead. If instead this is just another hack - a kfree with a comment in code form - then lets keep it static within the audio driver (which is what I thought you were initially suggesting). Phil