Received: by 2002:a05:6a10:a852:0:0:0:0 with SMTP id d18csp2583363pxy; Mon, 3 May 2021 03:35:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwp+c70kA6APT96ujNRkqrWHzg/k7Ljj3XjMebSH0SCdZLU4KwGTj/RvsUc2emzkLnF7OBq X-Received: by 2002:a17:90b:797:: with SMTP id l23mr20588044pjz.160.1620038117283; Mon, 03 May 2021 03:35:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620038117; cv=none; d=google.com; s=arc-20160816; b=duXTbJgNHsgU65M74j9ZwuuLQlI5XhIFk11zIlyz4SzYl9MsGuhsXMvb+rejYDVyt/ v9gbQglfgwwAmLB/7T83/BxoNOoxS4hmrq/PcPAOqZw6Kog2jzh4+S8wEdGBO4GX5Rlz IiXXWKe2ZFj/40eRQeEvi6Q/EmV/6oOsHIYSnavi+XuxSdjaChS9+jQLs6uqWJEb9NyY tv9sXxE1Hy9XxR3t5E9bIeVGSXEWYGFO/ElqlxmzuRQ7hNh6jfiLaN8OjTPUUUaZEqff S4Me3c6fd3EFlvajh2b3cdJfQb75vtnh70lte/oKm4dpJQkVXRJekGDdyifeeNO1A2MV Jkew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=wU3jOWcizTubRpV1GczgjKyqY6koP8nRhRZ88/xwnVc=; b=tSP6nX8vviYSHa6QYkq2Xb6eydfXHg74//YbgLRjPOPsS7gZ1Hxr7hJGBPOZtL3Ih+ WAJBar+mHHHzPJuyPMTfffOst/XzEtDQZopydk6vmwlhDFYPZdmpO94M3scREBzojGlY 9Lxe4CsarbJYxMX/yoIFUnNYUv3OhYXXglgwCmWaVkjGMtOD7ZXmt06/26OESaiRgaVW 2Xf/la5ndlHo1TSPez7nxjg64iW7Va5uvFQ+xCQdpuwQT28ujN5yCeiHAazA1kOw+1Ig o84ELljeLAsshpejdSV3TsgHP8wfVJ+2B0JFhNwZ3kag/Fxs4YDXrH2HEceB9uZrbm0r 2E7w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d8si14016130pgq.348.2021.05.03.03.35.03; Mon, 03 May 2021 03:35:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233249AbhECKPV (ORCPT + 99 others); Mon, 3 May 2021 06:15:21 -0400 Received: from bmailout1.hostsharing.net ([83.223.95.100]:44607 "EHLO bmailout1.hostsharing.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233062AbhECKPV (ORCPT ); Mon, 3 May 2021 06:15:21 -0400 X-Greylist: delayed 411 seconds by postgrey-1.27 at vger.kernel.org; Mon, 03 May 2021 06:15:21 EDT 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 bmailout1.hostsharing.net (Postfix) with ESMTPS id E3FF030014809; Mon, 3 May 2021 12:07:33 +0200 (CEST) Received: by h08.hostsharing.net (Postfix, from userid 100393) id D5F1010C8F; Mon, 3 May 2021 12:07:33 +0200 (CEST) Date: Mon, 3 May 2021 12:07:33 +0200 From: Lukas Wunner To: Saravana Kannan Cc: Mark Brown , Andy Shevchenko , Greg Kroah-Hartman , "Rafael J. Wysocki" , Guenter Roeck , Marek Szyprowski , kernel-team@android.com, linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] spi: Fix spi device unregister flow Message-ID: <20210503100733.GA8114@wunner.de> References: <20210426235638.1285530-1-saravanak@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210426235638.1285530-1-saravanak@google.com> User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Apr 26, 2021 at 04:56:38PM -0700, Saravana Kannan wrote: > When an SPI device is unregistered, the spi->controller->cleanup() is > called in the device's release callback. That's wrong for a couple of > reasons: > > 1. spi_dev_put() can be called before spi_add_device() is called. And > it's spi_add_device() that calls spi_setup(). This will cause clean() > to get called without the spi device ever being setup. Well, yes, but it's not a big problem in practice so far: I've checked all drivers and there are only four which are affected by this: spi-mpc512x-psc.c spi-pic32.c spi-s3c64xx.c spi-st-ssc4.c They all fiddle with the chipselect GPIO in their ->cleanup hook and the GPIO may not have been requested yet because that happens during ->setup. All the other drivers merely invoke kzalloc() on ->setup and kfree() on ->cleanup. The order doesn't matter in this case because kfree(NULL) is a no-op. > 2. There's no guarantee that the controller's driver would be present by > the time the spi device's release function gets called. How so? spi_devices are instantiated on ->probe of the controller via spi_register_controller() and destroyed on ->remove via spi_unregister_controller(). I don't see how the controller driver could ever be unavailable, so this point seems moot. > Fix these issues by simply moving the cleanup from the device release > callback to the actual spi_unregister_device() function. Unfortunately the fix is wrong, it introduces a new problem: > @@ -713,6 +717,8 @@ void spi_unregister_device(struct spi_device *spi) > if (!spi) > return; > > + spi_cleanup(spi); > + > if (spi->dev.of_node) { > of_node_clear_flag(spi->dev.of_node, OF_POPULATED); > of_node_put(spi->dev.of_node); Now you're running ->cleanup before the SPI slave's driver is unbound. That's bad, the driver may need to access the physical device on unbound, e.g. to quiesce interrupts. That may not work now because the slave's controller_state is gone. NAK, this needs to be reverted. Thanks, Lukas