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 X-Spam-Level: X-Spam-Status: No, score=-5.3 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_SANE_1 autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0D149C43460 for ; Tue, 4 May 2021 09:18:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C7DF761078 for ; Tue, 4 May 2021 09:18:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230186AbhEDJTK (ORCPT ); Tue, 4 May 2021 05:19:10 -0400 Received: from bmailout2.hostsharing.net ([83.223.78.240]:51487 "EHLO bmailout2.hostsharing.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230102AbhEDJTJ (ORCPT ); Tue, 4 May 2021 05:19:09 -0400 Received: from h08.hostsharing.net (h08.hostsharing.net [IPv6:2a01:37:1000::53df:5f1c:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "*.hostsharing.net", Issuer "RapidSSL TLS DV RSA Mixed SHA256 2020 CA-1" (verified OK)) by bmailout2.hostsharing.net (Postfix) with ESMTPS id 4A2472800BD97; Tue, 4 May 2021 11:17:54 +0200 (CEST) Received: by h08.hostsharing.net (Postfix, from userid 100393) id 397F82CCEE; Tue, 4 May 2021 11:17:54 +0200 (CEST) Date: Tue, 4 May 2021 11:17:54 +0200 From: Lukas Wunner To: Saravana Kannan Cc: Mark Brown , Andy Shevchenko , Greg Kroah-Hartman , "Rafael J. Wysocki" , Guenter Roeck , Marek Szyprowski , Android Kernel Team , linux-spi , LKML Subject: Re: [PATCH] spi: Fix spi device unregister flow Message-ID: <20210504091754.GA22045@wunner.de> References: <20210426235638.1285530-1-saravanak@google.com> <20210503100733.GA8114@wunner.de> <20210503175600.GA3864@wunner.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, May 03, 2021 at 11:15:50AM -0700, Saravana Kannan wrote: > On Mon, May 3, 2021 at 10:56 AM Lukas Wunner wrote: > > Without your patch: > > > > spi_unregister_device() > > device_unregister() > > device_del() > > bus_remove_device() > > device_release_driver() # access to physical SPI device in ->remove() > > put_device() > > kobject_put() > > kref_put() > > kobject_release() > > kobject_cleanup() > > device_release() > > spidev_release() > > spi->controller->cleanup() # controller_state freed > > > > With your patch: > > > > spi_unregister_device() > > spi_cleanup() > > spi->controller->cleanup() # controller_state freed > > device_unregister() > > device_del() > > bus_remove_device() > > device_release_driver() # access to physical SPI device in ->remove() [...] > So, it looks like the fix is simple. We just need to move > spi_cleanup() to the bottom of spi_unregister_device(). I'll send a > patch for that rather than reverting this and bringing back the other > bugs. That would result in a use-after-free if the call to device_unregister() indeed releases the last ref to the spi_device (which I'd expect is usually the case). However, something like this might work (in spi_unregister_device()): device_del(&spi->dev); spi_cleanup(spi); put_device(&spi->dev); Thanks, Lukas