Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp6091678yba; Thu, 11 Apr 2019 11:53:32 -0700 (PDT) X-Google-Smtp-Source: APXvYqxkFc19czwuO+EU5pxJ7b1ITol6gzO4cd8WxMi9VsJzKJuZE/3ud12McnEpo8n75y1aW5Ui X-Received: by 2002:a65:6107:: with SMTP id z7mr48714719pgu.313.1555008812271; Thu, 11 Apr 2019 11:53:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555008812; cv=none; d=google.com; s=arc-20160816; b=S+2tQo19uTsT9N0zDjrCuFb1noRDB0znNiQeFgHKYBcHMACFOj6OgQKBLveHL5pYqg Er11BNfYpCwu4y++tJVJ4TSIzjY2+ja1AWPIKcndk37ys6o8zMGq4W+eNFWeTzncqFxe s0jNc/H1OMiJBbDgxHx3LvheOd57xY6Wn6mxs2mCf3DyERYx5lB8jBsTHafcNFyDay9S ovmpdtgQbOtuxzz+GGC6XZmRB+w6naiSKrdgTSbghmCWzphAHkJC56kMStwI4/prCJB5 9kugGOVNOqe26RCNKDZrXvHSTcC6CSnuIM/jWI3c2T7+hn1FjDzbrQZINPPd4DHho0mS CzBA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=Q9LCv6hvKvVV7ugfmYyfukiH7BzQUtUryPXyxFdbIQ4=; b=MgmoDp5sqlgUyM0r2q8HHKTR30W/K/aVyFErzDXQHtwphjlloyjUnjHL9kyIuP5n0T FDNY/1Pi1pxgYB0XHb96tLSfLLMNKKnGnkou6Tj1bdHbThccutJGi4y7AV2+qMmUgxiL MWMVcqEn+KW+R23/GXNVkI/+EXIUUFNwWv6ACT6MOEu/nWGAySkkyBI6D42CTaJLcByu Btl5/9+gjqkwoc4MTOTwTGVusm+s9P6X1MV6k3AuCvLtJSmqSx9Lvg1bZ2FaVjavJQmH dGQ1z7HTtOoAbBBkPAwDAToIxBiS7wFKRxKS3XhSyhTZD7DNgm20werQu84JREoEjUmL 6FBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=mH9Z66Di; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v25si35147024pfe.22.2019.04.11.11.53.15; Thu, 11 Apr 2019 11:53:32 -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; dkim=pass header.i=@chromium.org header.s=google header.b=mH9Z66Di; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726757AbfDKSwi (ORCPT + 99 others); Thu, 11 Apr 2019 14:52:38 -0400 Received: from mail-io1-f66.google.com ([209.85.166.66]:35925 "EHLO mail-io1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726633AbfDKSwh (ORCPT ); Thu, 11 Apr 2019 14:52:37 -0400 Received: by mail-io1-f66.google.com with SMTP id f6so6283464iop.3 for ; Thu, 11 Apr 2019 11:52:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Q9LCv6hvKvVV7ugfmYyfukiH7BzQUtUryPXyxFdbIQ4=; b=mH9Z66DiPJdu4zjz7xq5WPrJBbeNAxbp7LYDENdORPWcjHBZbkWHfiUtffqPDob7I3 4ztFOCPXbAf0W6WKRb3cZ5DX6h+TXLIcHwuXDUs+Jn1+mbKQfKfPZbeKfLQulaXHTlp4 CE8YsMCqZdjH++Ugf5oXRQYhnXthmrLgOAIZk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Q9LCv6hvKvVV7ugfmYyfukiH7BzQUtUryPXyxFdbIQ4=; b=TxHQLbYv6GkRHPDlkm9eYZVYWlERemgtPM4fdRAgYDiNuChkS2KkZapmOHT/botiEM Ize9n2wFnOEs3p7Zprrww78ik7pxqq/CN8pxix3ElhG58hxZQrcE/SrAEt8RJSf/ZPAs EHx+c8HwGJ1i/JBQboSbrSjYsv3CMLsTWyucg4J9yBoQmJNTNhxgWtfn6/YNWeEc+TbV kgNwST5+EiettyJj7jyN/vcX0wzFmPLytGldFpPtsndUFqNApnOTah7ASo+rZqvsHelj O5SF5UZmxFA2giS7JU+mZHzsAREvhp6s1IkwLwLAFMOhWVmUvSWUiICXZ2yMYXyj/zVQ CsdQ== X-Gm-Message-State: APjAAAVz15hLdah5FumZ49jlkWZdaL1JGp/PJLLGQDsgLNSjabuWOntp Fd0wgHjjcZ22KyL8XFuTnTaOaw== X-Received: by 2002:a6b:c909:: with SMTP id z9mr32966003iof.244.1555008756639; Thu, 11 Apr 2019 11:52:36 -0700 (PDT) Received: from localhost ([2620:15c:183:0:20b8:dee7:5447:d05]) by smtp.gmail.com with ESMTPSA id t78sm2891373itb.39.2019.04.11.11.52.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Apr 2019 11:52:35 -0700 (PDT) From: Raul E Rangel To: linux-usb@vger.kernel.org Cc: groeck@chromium.org, oneukum@suse.com, djkurtz@chromium.org, zwisler@chromium.org, Raul E Rangel , Sebastian Andrzej Siewior , Martin Blumenstingl , Alan Stern , Dmitry Torokhov , Suwan Kim , linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" , Miquel Raynal , Johan Hovold , Greg Kroah-Hartman , Mathias Nyman Subject: [PATCH v2] usb/hcd: Send a uevent signaling that the host controller has died Date: Thu, 11 Apr 2019 12:52:11 -0600 Message-Id: <20190411185211.235940-1-rrangel@chromium.org> X-Mailer: git-send-email 2.21.0.392.gf8f6787159e-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This change will send a CHANGE event to udev with the DEAD environment variable set when the HC dies. I chose this instead of any of the other udev events because it's representing a state change in the host controller. The only other event that might have fit was OFFLINE, but that seems to be used for hot-removal and it implies the device could come ONLINE again. By notifying user space the appropriate policies can be applied. i.e., * Collect error logs. * Notify the user that USB is no longer functional. * Perform a graceful reboot. Signed-off-by: Raul E Rangel --- I wasn't able to find any good examples of other drivers sending a dead notification. Use an EVENT= format https://github.com/torvalds/linux/blob/master/drivers/acpi/dock.c#L302 https://github.com/torvalds/linux/blob/master/drivers/net/wireless/ath/wil6210/interrupt.c#L497 Uses SDEV_MEDIA_CHANGE= https://github.com/torvalds/linux/blob/master/drivers/scsi/scsi_lib.c#L2318 Uses ERROR=1. https://chromium.googlesource.com/chromiumos/third_party/kernel/+/7f6d8aec5803aac44192f03dce5637b66cda7abf/drivers/input/touchscreen/atmel_mxt_ts.c#1581 I'm not a fan because it doesn't signal what the error was. We could change the DEAD=1 event to maybe ERROR=1? Also where would be a good place to document this? Also thanks for the review. This is my first kernel patch so forgive me if I get the workflow wrong. Changes in v2: - Check that the root hub still exists before sending the uevent. - Ensure died_work has completed before deallocating. drivers/usb/core/hcd.c | 32 ++++++++++++++++++++++++++++++++ include/linux/usb/hcd.h | 1 + 2 files changed, 33 insertions(+) diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c index 975d7c1288e3..7835f1a3647d 100644 --- a/drivers/usb/core/hcd.c +++ b/drivers/usb/core/hcd.c @@ -2343,6 +2343,27 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg) return status; } + +/** + * hcd_died_work - Workqueue routine for root-hub has died. + * @hcd: primary host controller for this root hub. + * + * Do not call with the shared_hcd. + * */ +static void hcd_died_work(struct work_struct *work) +{ + struct usb_hcd *hcd = container_of(work, struct usb_hcd, died_work); + + mutex_lock(&usb_bus_idr_lock); + + if (hcd->self.root_hub) + /* Notify user space that the host controller has died */ + kobject_uevent_env(&hcd->self.root_hub->dev.kobj, KOBJ_CHANGE, + (char *[]){ "DEAD=1", NULL }); + + mutex_unlock(&usb_bus_idr_lock); +} + /* Workqueue routine for root-hub remote wakeup */ static void hcd_resume_work(struct work_struct *work) { @@ -2488,6 +2509,13 @@ void usb_hc_died (struct usb_hcd *hcd) usb_kick_hub_wq(hcd->self.root_hub); } } + + /* Handle the case where this function gets called with a shared HCD */ + if (usb_hcd_is_primary_hcd(hcd)) + schedule_work(&hcd->died_work); + else + schedule_work(&hcd->primary_hcd->died_work); + spin_unlock_irqrestore (&hcd_root_hub_lock, flags); /* Make sure that the other roothub is also deallocated. */ } @@ -2555,6 +2583,8 @@ struct usb_hcd *__usb_create_hcd(const struct hc_driver *driver, INIT_WORK(&hcd->wakeup_work, hcd_resume_work); #endif + INIT_WORK(&hcd->died_work, hcd_died_work); + hcd->driver = driver; hcd->speed = driver->flags & HCD_MASK; hcd->product_desc = (driver->product_desc) ? driver->product_desc : @@ -2908,6 +2938,7 @@ int usb_add_hcd(struct usb_hcd *hcd, #ifdef CONFIG_PM cancel_work_sync(&hcd->wakeup_work); #endif + cancel_work_sync(&hcd->died_work); mutex_lock(&usb_bus_idr_lock); usb_disconnect(&rhdev); /* Sets rhdev to NULL */ mutex_unlock(&usb_bus_idr_lock); @@ -2968,6 +2999,7 @@ void usb_remove_hcd(struct usb_hcd *hcd) #ifdef CONFIG_PM cancel_work_sync(&hcd->wakeup_work); #endif + cancel_work_sync(&hcd->died_work); mutex_lock(&usb_bus_idr_lock); usb_disconnect(&rhdev); /* Sets rhdev to NULL */ diff --git a/include/linux/usb/hcd.h b/include/linux/usb/hcd.h index 695931b03684..ae51d5bd1dfc 100644 --- a/include/linux/usb/hcd.h +++ b/include/linux/usb/hcd.h @@ -98,6 +98,7 @@ struct usb_hcd { #ifdef CONFIG_PM struct work_struct wakeup_work; /* for remote wakeup */ #endif + struct work_struct died_work; /* for dying */ /* * hardware info/state -- 2.21.0.392.gf8f6787159e-goog