From patchwork Thu Nov 15 10:22:48 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vasilis Liaskovitis X-Patchwork-Id: 1748321 Return-Path: X-Original-To: patchwork-linux-acpi@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork2.kernel.org (Postfix) with ESMTP id 2B08ADF230 for ; Thu, 15 Nov 2012 10:24:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2993110Ab2KOKXD (ORCPT ); Thu, 15 Nov 2012 05:23:03 -0500 Received: from mail-bk0-f46.google.com ([209.85.214.46]:52807 "EHLO mail-bk0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2992493Ab2KOKXA (ORCPT ); Thu, 15 Nov 2012 05:23:00 -0500 Received: by mail-bk0-f46.google.com with SMTP id q16so614387bkw.19 for ; Thu, 15 Nov 2012 02:22:59 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references :x-gm-message-state; bh=2Ny7ha8KvhOj4k5NGSYiBdOshDMEUAGrFwTMYuxeS6o=; b=oJJkiDirL4ljeUpCPLTVk55C5uwzRoogSpfEi0Fy25pB918iqkb5rYCa5RCOTyDTBn COF9kR3t6dt9ShxUQ4qNhfns/N+5ZrWn97yedDf084XIZWidw2yLqW8o5wHkv55HoEsP Q2qRui3lAoDOLs1bxH82Dw5cLDMq+fuI2GsuDw8HDfA37wGMLjuQ92t3FcunZ2NJCvEU iLq2MrVpGU2/CPOq3iqL20JD3pWb5pNXVyks/3bqaey6Fl6F6f1Ux2hpUYTzlbfteLQp LY5ft0+IOBookJtjEJbZqr2tvu9r1R0t7xmIypEg/SVJRA4vgsB2wyaG1Y0c5RMyV707 99Lw== Received: by 10.204.130.73 with SMTP id r9mr162228bks.131.1352974978934; Thu, 15 Nov 2012 02:22:58 -0800 (PST) Received: from dhcp-192-168-178-175.ri.profitbricks.localdomain ([62.217.45.26]) by mx.google.com with ESMTPS id k3sm2859450bku.13.2012.11.15.02.22.57 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 15 Nov 2012 02:22:57 -0800 (PST) From: Vasilis Liaskovitis To: linux-acpi@vger.kernel.org, isimatu.yasuaki@jp.fujitsu.com, wency@cn.fujitsu.com Cc: rjw@sisk.pl, lenb@kernel.org, toshi.kani@hp.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Vasilis Liaskovitis Subject: [RFC PATCH v2 1/3] driver core: Introduce prepare_remove in bus_type Date: Thu, 15 Nov 2012 11:22:48 +0100 Message-Id: <1352974970-6643-2-git-send-email-vasilis.liaskovitis@profitbricks.com> X-Mailer: git-send-email 1.7.9 In-Reply-To: <1352974970-6643-1-git-send-email-vasilis.liaskovitis@profitbricks.com> References: <1352974970-6643-1-git-send-email-vasilis.liaskovitis@profitbricks.com> X-Gm-Message-State: ALoCoQn5oM/eHtIS6uL1qTmL/Qf5Aeqdc/PyDl+HnmsTPfNXZ9kT7uEGsRwS4ggI05Eyg+/4M9+U Sender: linux-acpi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org This function will call a bus-specific prepare_remove callback. If this call is not successful, the device cannot be safely removed, or the driver cannot be safely unbound. This operation is needed to safely execute OSPM-induced unbind or rebind of ACPI memory devices e.g. echo "PNP0C80:00" > /sys/bus/acpi/drivers/acpi_memhotplug/unbind driver_unbind and device_reprobe will use the new callback before calling device_release_driver() PROBLEM: bus_remove_device and bus_remove_driver also call device_release_driver but these functions always succeed under the core device-driver model i.e. there is no possibility of failure. These functions do not call the prepare_remove callback currently. This creates an unwanted assymetry between device/driver removal and driver unbinding. Suggestions to fix welcome. Signed-off-by: Vasilis Liaskovitis --- drivers/base/bus.c | 36 ++++++++++++++++++++++++++++++++++++ include/linux/device.h | 2 ++ 2 files changed, 38 insertions(+), 0 deletions(-) diff --git a/drivers/base/bus.c b/drivers/base/bus.c index 181ed26..c5dad55 100644 --- a/drivers/base/bus.c +++ b/drivers/base/bus.c @@ -34,6 +34,7 @@ static struct kset *system_kset; static int __must_check bus_rescan_devices_helper(struct device *dev, void *data); +static int bus_prepare_remove_device(struct device *dev); static struct bus_type *bus_get(struct bus_type *bus) { @@ -178,11 +179,18 @@ static ssize_t driver_unbind(struct device_driver *drv, if (dev && dev->driver == drv) { if (dev->parent) /* Needed for USB */ device_lock(dev->parent); + err = bus_prepare_remove_device(dev); + if (err) { + if (dev->parent) + device_unlock(dev->parent); + goto out; + } device_release_driver(dev); if (dev->parent) device_unlock(dev->parent); err = count; } +out: put_device(dev); bus_put(bus); return err; @@ -587,6 +595,26 @@ void bus_remove_device(struct device *dev) bus_put(dev->bus); } +/** + * device_prepare_release_driver - call driver specific operations to prepare + * for manually detaching device from driver. + * @dev: device. + * + * Prepare for detaching device from driver. + * When called for a USB interface, @dev->parent lock must be held. + * This function returns 0 if preparation is successful, non-zero error value + * otherwise. + */ +static int bus_prepare_remove_device(struct device *dev) +{ + int ret = 0; + device_lock(dev); + if (dev->bus) + ret = dev->bus->prepare_remove(dev); + device_unlock(dev); + return ret; +} + static int driver_add_attrs(struct bus_type *bus, struct device_driver *drv) { int error = 0; @@ -820,9 +848,17 @@ EXPORT_SYMBOL_GPL(bus_rescan_devices); */ int device_reprobe(struct device *dev) { + int ret; + if (dev->driver) { if (dev->parent) /* Needed for USB */ device_lock(dev->parent); + ret = bus_prepare_remove_device(dev); + if (ret) { + if (dev->parent) + device_unlock(dev->parent); + return ret; + } device_release_driver(dev); if (dev->parent) device_unlock(dev->parent); diff --git a/include/linux/device.h b/include/linux/device.h index cc3aee5..8e7055b 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -104,6 +104,7 @@ struct bus_type { int (*suspend)(struct device *dev, pm_message_t state); int (*resume)(struct device *dev); + int (*prepare_remove) (struct device *dev); const struct dev_pm_ops *pm; @@ -853,6 +854,7 @@ extern void device_release_driver(struct device *dev); extern int __must_check device_attach(struct device *dev); extern int __must_check driver_attach(struct device_driver *drv); extern int __must_check device_reprobe(struct device *dev); +extern int device_prepare_release_driver(struct device *dev); /* * Easy functions for dynamically creating devices on the fly