Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp8156941ybn; Tue, 1 Oct 2019 04:11:27 -0700 (PDT) X-Google-Smtp-Source: APXvYqyPzuWdjp2ft66BhRzNBjskWSsrVHY/ZcSzl7HhbYIjwHQ9GmyNrUGntIV5M3HXbf1edSB9 X-Received: by 2002:a17:906:4b15:: with SMTP id y21mr16715855eju.112.1569928287204; Tue, 01 Oct 2019 04:11:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1569928287; cv=none; d=google.com; s=arc-20160816; b=Du7LMsWZqI2+sFvIS6TZXHlcNDvVCF0xMQ7FbURrPqfwHLArAIPW/CvSpQLhOw69vY PXQ2LY3lk8+tGbf+XvasX1Pe7XkNUwYH62m/grfg6V9m4UxegtzKPX5hdIF/4u9DcxJv DM13hHCAkCLOlr9H36LMfXXgr9rMAOs5ifDJlobWFz7dvuZsfi+vqexqxF4Jp40mT0ls 47mRWSF8MXZ4OE6jetZ1Ig/jaFXedNIvZWgMcSqjvA+EZQe+LhBsLMace6rF5/7oo5gW hcRW8TQvbueCMgA+Mr4mHqNTR0zHYv55n+ebnzp8jUDpebLUkQog/XwEsb8QGype0r7T 6sQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:organization:references :in-reply-to:message-id:subject:cc:to:from:date; bh=Zo1nYcJH1l/QJmrnAfEfSCu0aNNdYnCblPNkTFWhQ68=; b=0qAI6kwPmdzfODjKnGKJYXW5xUrgHZQq1vq9Fee2/hqFROVzZGgWC35IJChD5QQxlo JNkNLQg4ms9+qPaac5pTk4fW+wehQFYR7boKe5RCSLIpiLMiFlsIv1pfDAe7OUaTU/Un RKl6EiHQI4OY/+mqyEUEjKQ+lu0YGd+LT4MEIw/citAz7ONDuJ1LXbfsaPhkfFQAJCQN 7Pe6nqEDLGG01eu6XLGzFBs1Z7TImHN6rtvCTLUDKqY+YVpujuxmFvjNM7LTc/YT73dF D7yldLC0hoGmosAY+yfjFzhDZQavlvPPfu0dF9XpR9580YZUvPTGutDGgxJDeVvH9z6X rHOw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b22si9355870eda.194.2019.10.01.04.11.02; Tue, 01 Oct 2019 04:11:27 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731208AbfJALH0 (ORCPT + 99 others); Tue, 1 Oct 2019 07:07:26 -0400 Received: from mail-out.m-online.net ([212.18.0.10]:44334 "EHLO mail-out.m-online.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730506AbfJALH0 (ORCPT ); Tue, 1 Oct 2019 07:07:26 -0400 Received: from frontend01.mail.m-online.net (unknown [192.168.8.182]) by mail-out.m-online.net (Postfix) with ESMTP id 46jGgs6lMMz1rhsg; Tue, 1 Oct 2019 13:07:21 +0200 (CEST) Received: from localhost (dynscan1.mnet-online.de [192.168.6.70]) by mail.m-online.net (Postfix) with ESMTP id 46jGgs5ZDDz1qql3; Tue, 1 Oct 2019 13:07:21 +0200 (CEST) X-Virus-Scanned: amavisd-new at mnet-online.de Received: from mail.mnet-online.de ([192.168.8.182]) by localhost (dynscan1.mail.m-online.net [192.168.6.70]) (amavisd-new, port 10024) with ESMTP id FEVJJvJNUS56; Tue, 1 Oct 2019 13:07:19 +0200 (CEST) X-Auth-Info: 93MR/orNrEe2jhKJjzkEZxd48FBIEA3YGLYZuH1w+pY= Received: from jawa (85-222-111-42.dynamic.chello.pl [85.222.111.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.mnet-online.de (Postfix) with ESMTPSA; Tue, 1 Oct 2019 13:07:19 +0200 (CEST) Date: Tue, 1 Oct 2019 13:07:13 +0200 From: Lukasz Majewski To: Geert Uytterhoeven Cc: Mark Brown , Colin Ian King , linux-spi , Krzysztof Kozlowski , Linux Kernel Mailing List , kbuild test robot , Julia Lawall , Dan Carpenter Subject: Re: [PATCH] spi: Avoid calling spi_slave_abort() with kfreed spidev Message-ID: <20191001130713.6eafb728@jawa> In-Reply-To: References: <20191001090657.25721-1-lukma@denx.de> <20191001113420.032dbfef@jawa> Organization: denx.de X-Mailer: Claws Mail 3.17.4 (GTK+ 2.24.32; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; boundary="Sig_/BmcV7SX+cE.Huey1lpa1dZL"; protocol="application/pgp-signature" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --Sig_/BmcV7SX+cE.Huey1lpa1dZL Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Hi Geert, > Hi Lukasz, >=20 > On Tue, Oct 1, 2019 at 11:34 AM Lukasz Majewski wrote: > > > On Tue, Oct 1, 2019 at 11:07 AM Lukasz Majewski > > > wrote: =20 > > > > Call spi_slave_abort() only when the spidev->spi is !NULL and > > > > the structure hasn't already been kfreed. > > > > > > > > Reported-by: kbuild test robot > > > > Reported-by: Julia Lawall > > > > Reported-by: Dan Carpenter > > > > Signed-off-by: Lukasz Majewski =20 >=20 > > > > --- a/drivers/spi/spidev.c > > > > +++ b/drivers/spi/spidev.c > > > > @@ -600,15 +600,16 @@ static int spidev_open(struct inode > > > > *inode, struct file *filp) static int spidev_release(struct > > > > inode *inode, struct file *filp) { > > > > struct spidev_data *spidev; > > > > + int dofree; > > > > > > > > mutex_lock(&device_list_lock); > > > > spidev =3D filp->private_data; > > > > filp->private_data =3D NULL; > > > > + dofree =3D 0; > > > > > > > > /* last close? */ > > > > spidev->users--; > > > > if (!spidev->users) { > > > > - int dofree; > > > > > > > > kfree(spidev->tx_buffer); > > > > spidev->tx_buffer =3D NULL; > > > > @@ -628,7 +629,8 @@ static int spidev_release(struct inode > > > > *inode, struct file *filp) kfree(spidev); > > > > } > > > > #ifdef CONFIG_SPI_SLAVE > > > > - spi_slave_abort(spidev->spi); > > > > + if (!dofree) > > > > + spi_slave_abort(spidev->spi); =20 > > > > > > Can spidev->spi be NULL, if spidev->users !=3D 0? =20 > > > > No, it shouldn't be. > > > > The "dofree" is only set to true (the spidev->spi =3D=3D NULL condition > > is checked) if there are no references (spidev->users =3D=3D 0). > > > > The if (!dofree) prevents from calling spi_slave_abort() when > > spidev->spi =3D=3D NULL and spidev is kfree'd. =20 >=20 > If spidev->users !=3D 0, the block checking spidev->spi =3D=3D NULL is ne= ver > executed, and spi_slave_abort() will be called. Yes, this is correct. My other patch [1] clears the FIFOs in SPI IP block and ends (if there are any stalled) DMA transactions. >=20 > I'm wondering if spidev->spi can be NULL if spidev->users is still > positive. I think that it cannot. =46rom my tests [2] - when I do enter spi_slave_abort() function the state of spidev->users: 0 dofree: 0 spidev->spi: 0x51337072 So it is possible to call the spidev_release without previously setting spidev->spi to NULL (which is done in spidev_remove() function). IMHO the above behavior also seems to be correct, as during distortion the slave losts synchronization from master. The spidev_remove() callback is part of spi_device struct and is called when the device is removed (rmmod spi_fsl_dspi). =46rom my tests the spidev_release() is NOT called after spidev_remove(), so the code in former seems to be a dead one. Or maybe there is an use case which causes calling spidev_release() after spidev_remove()? >=20 > Gr{oetje,eeting}s, >=20 > Geert >=20 Note: [1] - https://lkml.org/lkml/2019/9/24/245 [2] - https://github.com/lmajewski/tests-spi/blob/master/tests/spi/spi_tests.sh HW setup: HW loopback with two /dev/spidevX.Y devices used Best regards, Lukasz Majewski -- DENX Software Engineering GmbH, Managing Director: Wolfgang Denk HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email: lukma@denx.de --Sig_/BmcV7SX+cE.Huey1lpa1dZL Content-Type: application/pgp-signature Content-Description: OpenPGP digital signature -----BEGIN PGP SIGNATURE----- iQEzBAEBCgAdFiEEgAyFJ+N6uu6+XupJAR8vZIA0zr0FAl2TM2EACgkQAR8vZIA0 zr22NQf/R5mfFd/b6MTcfGxaoSiilulAObqBQ/0HBVTAQFbBK0EFU2/b3Jgoy0Nu NaGoOOT/hHUAmVf9oZR1h5FsdV0rzSG/jbSkOMA+uN+7QlhlwqaBg4BWanrPcSEi xpP7gsbTPj+yiDDtdRoUDxz5P+TMvaf/DrLPn6fkau+YpPAzGTy1AYkdXbDp19SY EoJjxBet0qDfK4/r7hssANNvT2iAeF4jInswxhtjFR8+WN8tT69kOP1n+skXN7eH hbNn7ghgjNsos+iT+kWLOvdr6vdjR6eh1fCEdZwYhKbyyoMHgnZAicC1tA5TDWqR 70fH/6TXos2MgYjiLYpq2GgzAQUgSg== =JhoH -----END PGP SIGNATURE----- --Sig_/BmcV7SX+cE.Huey1lpa1dZL--