From patchwork Wed Sep 16 14:26:51 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 11780929 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id DC77A92C for ; Wed, 16 Sep 2020 20:53:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C6B1C2087D for ; Wed, 16 Sep 2020 20:53:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728436AbgIPUxT (ORCPT ); Wed, 16 Sep 2020 16:53:19 -0400 Received: from mx2.suse.de ([195.135.220.15]:44740 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726606AbgIPQyL (ORCPT ); Wed, 16 Sep 2020 12:54:11 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 11AF8B53C; Wed, 16 Sep 2020 14:27:12 +0000 (UTC) Message-ID: <1600266411.2424.32.camel@suse.de> Subject: memory allocations dring device removal From: Oliver Neukum To: Alan Stern , gregKH@linuxfoundation.org Cc: linux-usb@vger.kernel.org Date: Wed, 16 Sep 2020 16:26:51 +0200 X-Mailer: Evolution 3.26.6 Mime-Version: 1.0 Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org Hi, I am cleaning up house, electronically speaking. Reading the thread about the keyboard with the storage device reminded me about a potential issue. What happens if you allocate memory during disconnect()? If the storage device is second, the storage driver will still be bound and the SCSI device will still exist. The kernel may use it to launder pages. This will fail, as the device is physically gone. So can we deadlock? Is this patch necessary? Regards Oliver From 97b7e91af588b7489795e3eaf773be032bc91b70 Mon Sep 17 00:00:00 2001 From: Oliver Neukum Date: Tue, 28 May 2019 11:43:02 +0200 Subject: [PATCH] base: force NOIO allocations during unplug There is one overlooked situation under which a driver must not do IO to allocate memory. You cannot do that while disconnecting a device. A device being disconnected is no longer functional in most cases, yet IO may fail only when the handler runs. Signed-off-by: Oliver Neukum --- drivers/base/core.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/base/core.c b/drivers/base/core.c index bb5806a2bd4c..509306a4ea89 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include "base.h" @@ -3062,6 +3063,7 @@ void device_del(struct device *dev) struct device *parent = dev->parent; struct kobject *glue_dir = NULL; struct class_interface *class_intf; + unsigned int noio_flag; device_lock(dev); kill_device(dev); @@ -3073,6 +3075,7 @@ void device_del(struct device *dev) /* Notify clients of device removal. This call must come * before dpm_sysfs_remove(). */ + noio_flag = memalloc_noio_save(); if (dev->bus) blocking_notifier_call_chain(&dev->bus->p->bus_notifier, BUS_NOTIFY_DEL_DEVICE, dev); @@ -3106,6 +3109,7 @@ void device_del(struct device *dev) device_platform_notify(dev, KOBJ_REMOVE); device_remove_properties(dev); device_links_purge(dev); + memalloc_noio_restore(noio_flag); if (dev->bus) blocking_notifier_call_chain(&dev->bus->p->bus_notifier,