From patchwork Thu Apr 23 12:37:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 11505587 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 93B1F81 for ; Thu, 23 Apr 2020 12:37:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7AC6320857 for ; Thu, 23 Apr 2020 12:37:31 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="KGETPAGZ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728336AbgDWMh3 (ORCPT ); Thu, 23 Apr 2020 08:37:29 -0400 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:60628 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726840AbgDWMh3 (ORCPT ); Thu, 23 Apr 2020 08:37:29 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1587645447; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=+XlBFNq2TljiIz7UMQVRidAnnXkCvE8Tn9pzuRMdxJE=; b=KGETPAGZxzZWjKfc9ds35wzFNfCghTI9gsRYfEmHKVPhA8jEOfZz7uHGjxusjElANNaswf 3c3QNiFUvHjai6xTFBzDYL1btpbFd5sN+V57r+j+rRo8+udZHW7We48yKnC9xgEWjhnOm4 QiS0ZzdwwcshliVDScCzf1Er38c56+0= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-159-RqunHNMFNlqYJD41EtgEXw-1; Thu, 23 Apr 2020 08:37:26 -0400 X-MC-Unique: RqunHNMFNlqYJD41EtgEXw-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 1F0F61800D51; Thu, 23 Apr 2020 12:37:25 +0000 (UTC) Received: from localhost (ovpn-114-230.ams2.redhat.com [10.36.114.230]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2FA8B5D70A; Thu, 23 Apr 2020 12:37:18 +0000 (UTC) From: Stefan Hajnoczi To: virtualization@lists.linux-foundation.org Cc: Jens Axboe , linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Paolo Bonzini , Jason Wang , "Michael S. Tsirkin" , Stefan Hajnoczi , Lance Digby Subject: [PATCH] virtio-blk: handle block_device_operations callbacks after hot unplug Date: Thu, 23 Apr 2020 13:37:17 +0100 Message-Id: <20200423123717.139141-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org A virtio_blk block device can still be referenced after hot unplug by userspace processes that hold the file descriptor. In this case virtblk_getgeo() can be invoked after virtblk_remove() was called. For example, a program that has /dev/vdb open can call ioctl(HDIO_GETGEO) after hot unplug. Fix this by clearing vblk->disk->private_data and checking that the virtio_blk driver instance is still around in virtblk_getgeo(). Note that the virtblk_getgeo() function itself is guaranteed to remain in memory after hot unplug because the virtio_blk module refcount is still held while a block device reference exists. Originally-by: Lance Digby Signed-off-by: Stefan Hajnoczi --- drivers/block/virtio_blk.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c index 93468b7c6701..b50cdf37a6f7 100644 --- a/drivers/block/virtio_blk.c +++ b/drivers/block/virtio_blk.c @@ -300,6 +300,10 @@ static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo) { struct virtio_blk *vblk = bd->bd_disk->private_data; + /* Driver instance has been removed */ + if (!vblk) + return -ENOTTY; + /* see if the host passed in geometry config */ if (virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_GEOMETRY)) { virtio_cread(vblk->vdev, struct virtio_blk_config, @@ -835,6 +839,7 @@ static void virtblk_remove(struct virtio_device *vdev) vdev->config->reset(vdev); refc = kref_read(&disk_to_dev(vblk->disk)->kobj.kref); + vblk->disk->private_data = NULL; put_disk(vblk->disk); vdev->config->del_vqs(vdev); kfree(vblk->vqs);