From patchwork Mon Mar 25 17:01:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 10869643 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9DC6314DE for ; Mon, 25 Mar 2019 17:01:56 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 85FBF294DF for ; Mon, 25 Mar 2019 17:01:56 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 79F9F294C3; Mon, 25 Mar 2019 17:01:56 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 04E18294A7 for ; Mon, 25 Mar 2019 17:01:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729106AbfCYRBz (ORCPT ); Mon, 25 Mar 2019 13:01:55 -0400 Received: from mail-pf1-f196.google.com ([209.85.210.196]:34454 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726024AbfCYRBy (ORCPT ); Mon, 25 Mar 2019 13:01:54 -0400 Received: by mail-pf1-f196.google.com with SMTP id b3so5357648pfd.1; Mon, 25 Mar 2019 10:01:54 -0700 (PDT) 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=A5Klv8cdxhlQhyH1kxt75F3SBeNvWJKnxr7kVXJGc7I=; b=HHhzMgeF7Syi3mi4I8c/jGnlx8Ocp2f6yvHfZzuRGx3804C2BfxnlpB8KYtzBWbvj9 FVN7rBaSiYbSUWXOEnFlHN3J7RqfUIE7aWyqfa701Vz06x0MOtBx0HOfUH/qQ+KLWF8J PIQtgvnlk6vfC8y+EbcWp9sxuiBEeBj+vfDxI2+Dtr2e8mp3kxbd3FD3sqo0MJLUG16j kEU2DJTY7QDO6WMaHu42NT4cmTJw/pNJr1G9jpu5kWKOf4ShdK60HWKVyQd1Bl6c5UNG XAirWVniGNo+Y97Hdmsqly7TO+MKep9xT+Vr1dARadVLijV9dRtJCa2K6UwAncVBSVN7 7+bQ== X-Gm-Message-State: APjAAAX/CghO0krch8Kx0bEhUrYPuHom7rVXvqFpttbGhy2BR/t6ZeDn p7kpw9WYjiLudYr/slD+JNI= X-Google-Smtp-Source: APXvYqy5IJmIy0WFf6k2OSVt6Cd8gjQtNRY37cHsddeaHsdUtnOm3HDDIKkfGRI18WAbrM8AEz3SfA== X-Received: by 2002:a63:3f8b:: with SMTP id m133mr23929921pga.91.1553533313816; Mon, 25 Mar 2019 10:01:53 -0700 (PDT) Received: from desktop-bart.svl.corp.google.com ([2620:15c:2cd:203:5cdc:422c:7b28:ebb5]) by smtp.gmail.com with ESMTPSA id a2sm20033598pff.58.2019.03.25.10.01.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Mar 2019 10:01:53 -0700 (PDT) From: Bart Van Assche To: "Martin K . Petersen" , "James E . J . Bottomley" Cc: linux-scsi@vger.kernel.org, Christoph Hellwig , Bart Van Assche , Ming Lei , Hannes Reinecke , Johannes Thumshirn , Jason Yan , stable@vger.kernel.org Subject: [PATCH] sd: Fix a race between closing an sd device and sd I/O Date: Mon, 25 Mar 2019 10:01:46 -0700 Message-Id: <20190325170146.184414-1-bvanassche@acm.org> X-Mailer: git-send-email 2.20.GIT MIME-Version: 1.0 Sender: linux-scsi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The scsi_end_request() function calls scsi_cmd_to_driver() indirectly and hence needs the disk->private_data pointer. Avoid that that pointer is cleared before all affected I/O requests have finished. This patch avoids that the following crash occurs: Unable to handle kernel NULL pointer dereference at virtual address 0000000000000000 Call trace: scsi_mq_uninit_cmd+0x1c/0x30 scsi_end_request+0x7c/0x1b8 scsi_io_completion+0x464/0x668 scsi_finish_command+0xbc/0x160 scsi_eh_flush_done_q+0x10c/0x170 sas_scsi_recover_host+0x84c/0xa98 [libsas] scsi_error_handler+0x140/0x5b0 kthread+0x100/0x12c ret_from_fork+0x10/0x18 Cc: Christoph Hellwig Cc: Ming Lei Cc: Hannes Reinecke Cc: Johannes Thumshirn Cc: Jason Yan Cc: Reported-by: Jason Yan Signed-off-by: Bart Van Assche Reviewed-by: Christoph Hellwig --- drivers/scsi/sd.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c index ed34bfbc3844..0077880c0cc8 100644 --- a/drivers/scsi/sd.c +++ b/drivers/scsi/sd.c @@ -1416,11 +1416,6 @@ static void sd_release(struct gendisk *disk, fmode_t mode) scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW); } - /* - * XXX and what if there are packets in flight and this close() - * XXX is followed by a "rmmod sd_mod"? - */ - scsi_disk_put(sdkp); } @@ -3483,9 +3478,21 @@ static void scsi_disk_release(struct device *dev) { struct scsi_disk *sdkp = to_scsi_disk(dev); struct gendisk *disk = sdkp->disk; - + struct request_queue *q = disk->queue; + ida_free(&sd_index_ida, sdkp->index); + /* + * Wait until all requests that are in progress have completed. + * This is necessary to avoid that e.g. scsi_end_request() crashes + * due to clearing the disk->private_data pointer. Wait from inside + * scsi_disk_release() instead of from sd_release() to avoid that + * freezing and unfreezing the request queue affects user space I/O + * in case multiple processes open a /dev/sd... node concurrently. + */ + blk_mq_freeze_queue(q); + blk_mq_unfreeze_queue(q); + disk->private_data = NULL; put_disk(disk); put_device(&sdkp->device->sdev_gendev);