Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp416871pxv; Thu, 1 Jul 2021 00:45:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwr6lFjHrYPpUWPc5ObXIkLktS9OrbTk3UKNHxa/PF8MbfptKqeXftQgSkN9sYc5sF4wOoQ X-Received: by 2002:a17:906:6817:: with SMTP id k23mr40335895ejr.300.1625125525928; Thu, 01 Jul 2021 00:45:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625125525; cv=none; d=google.com; s=arc-20160816; b=GwQ0kxfX6vv2It0+Emm/GREV4YWL9KkpMP0tSow3ZcYYci/N/zZY2QPTEuV5XN9msP KP8NIjaK1T94p2KckbMCN8HzkiaR8kc5K4b6aYFeV3fqIRN+zr1aEFBzUIms7urqc8lU zwjgJKl/UDP9KSK7Ak9mwtJS2izce5NEMXRl0b+mwLZrDz6KFdwkOPEF8e8UKT1Nfgee K2I+SLl8qoBdLXOPrRHUfQPuujDnkku7bFevHR/7PigouujgxdgAbgF76ywdx3ELOdD6 ksaNb5HDHCYNYGAqlQuThup+oA65RdoQk5Qr8xqk6tlFNC8LChIe3/SnjytfMTt1VGd1 C7BQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=Gq2ljfs2CXXb3YhD80yiiPsmHKF8xMR8mqGstdWnGlQ=; b=dXRFt/Vn3uQjzyxGlwV0onZEwSZvaN+2NU9xQoJBbBlotiJZcjCROoDwJzeTEWTsNS //orMh+lCyB5wA99FgfKUd+61t1RjnC/pnJKoivKK66ti6TF9K20TV0F2RxXZsyEQ1Bx kXvRERTOLpCraaIB2oU7cJ+S/kEyhAIeTPx5pKFTz/7qGfR9vJnecZ5d5FxhojJI59TG q/IbFrU58NU3C0dIjx4C+piuzNRqjuaSKQKQvLu7JcWREkUY/lcKgUi0t0AtN80wQIDX ROEIz9+CdfYVC4WJsD7PkmOKl1/aDPO0M651QOh5nXRq3HvB979SOWmocIMolMZZcIEo clIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=wIM71vPu; 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 e5si22337268edr.499.2021.07.01.00.45.00; Thu, 01 Jul 2021 00:45:25 -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; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=wIM71vPu; 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 S234905AbhGAHpD (ORCPT + 99 others); Thu, 1 Jul 2021 03:45:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55578 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229906AbhGAHpC (ORCPT ); Thu, 1 Jul 2021 03:45:02 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F0F15C061756; Thu, 1 Jul 2021 00:42:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=Gq2ljfs2CXXb3YhD80yiiPsmHKF8xMR8mqGstdWnGlQ=; b=wIM71vPuXgowFO5VOyaPi1mS0G +uM8DKlAu4CoK9uVAVNWLE2u4HIXcNdfMZMI91DdNZIFJB3z1AUcOwIT8No38azcqUYl5ivsc97x5 e756gbK84Z7gZY1z1DxBLGTJJLU9Nsr1MPq1dfCZCAHVlwnB2cURPDPJtpD+27qHiD/BGytjNIBHO oYMA5ryciMg6EgNRB1PUQcuMPIc0R9bTiwDHvH7TlgsvzvQtfu3quNdjaZmeIikZ5Zlu6mu66SFYL PYVleWdAKfRdkUde4HpkwdDBsxuoq11Y3j513yH4Uh4TgcpcUUVVO8D19IqgW1CaQgp4JAi4a/J/E KlkVsR1g==; Received: from hch by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1lyrKm-006J4o-W0; Thu, 01 Jul 2021 07:42:12 +0000 Date: Thu, 1 Jul 2021 08:42:04 +0100 From: Christoph Hellwig To: Christian L?hle Cc: "linux-kernel@vger.kernel.org" , "linux-block@vger.kernel.org" , "axboe@kernel.dk" Subject: Re: [PATCH] block: prevent sending events from removed device Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jun 30, 2021 at 08:09:39AM +0000, Christian L?hle wrote: > Skip kobject_uevent_env in case the associated kobject > no longer exists, as calling kobject_uevent_env with > NULL is not safe. > I don't see how this is going to work. If the device is being deleted the reference count will be zero and refcount_inc as called from kobject_get will WARN. We'll need to check the disk is marked up, and we need to do that under a lock. Something like this completely untested patch: diff --git a/block/disk-events.c b/block/disk-events.c index a75931ff5da4..27b845c51f2a 100644 --- a/block/disk-events.c +++ b/block/disk-events.c @@ -190,6 +190,9 @@ static void disk_check_events(struct disk_events *ev, spin_unlock_irq(&ev->lock); + if (!(disk->flags & GENHD_FL_UP)) + return; + /* * Tell userland about new events. Only the events listed in * @disk->events are reported, and only if DISK_EVENT_FLAG_UEVENT @@ -268,6 +271,8 @@ bool bdev_check_media_change(struct block_device *bdev) { unsigned int events; + lockdep_assert_held(&bdev->bd_disk->open_mutex); + events = disk_clear_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE | DISK_EVENT_EJECT_REQUEST); if (!(events & DISK_EVENT_MEDIA_CHANGE)) @@ -290,7 +295,10 @@ static void disk_events_workfn(struct work_struct *work) struct delayed_work *dwork = to_delayed_work(work); struct disk_events *ev = container_of(dwork, struct disk_events, dwork); - disk_check_events(ev, &ev->clearing); + mutex_lock(&ev->disk->open_mutex); + if (ev->disk->flags & GENHD_FL_UP) + disk_check_events(ev, &ev->clearing); + mutex_unlock(&ev->disk->open_mutex); } /* diff --git a/drivers/block/ataflop.c b/drivers/block/ataflop.c index a093644ac39f..b8e77da44235 100644 --- a/drivers/block/ataflop.c +++ b/drivers/block/ataflop.c @@ -1735,8 +1735,10 @@ static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, /* invalidate the buffer track to force a reread */ BufferDrive = -1; set_bit(drive, &fake_change); + mutex_lock(&bdev->bd_disk->open_mutex); if (bdev_check_media_change(bdev)) floppy_revalidate(bdev->bd_disk); + mutex_unlock(&bdev->bd_disk->open_mutex); return 0; default: return -EINVAL; diff --git a/drivers/block/floppy.c b/drivers/block/floppy.c index 87460e0e5c72..2a97f22cfa0b 100644 --- a/drivers/block/floppy.c +++ b/drivers/block/floppy.c @@ -3185,8 +3185,10 @@ static int invalidate_drive(struct block_device *bdev) /* invalidate the buffer track to force a reread */ set_bit((long)bdev->bd_disk->private_data, &fake_change); process_fd_request(); + mutex_lock(&bdev->bd_disk->open_mutex); if (bdev_check_media_change(bdev)) floppy_revalidate(bdev->bd_disk); + mutex_unlock(&bdev->bd_disk->open_mutex); return 0; }