From patchwork Sun Aug 21 22:04:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 599079 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BEEDAC00140 for ; Sun, 21 Aug 2022 22:05:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231509AbiHUWFV (ORCPT ); Sun, 21 Aug 2022 18:05:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48448 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231465AbiHUWFQ (ORCPT ); Sun, 21 Aug 2022 18:05:16 -0400 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5CB0213F6D for ; Sun, 21 Aug 2022 15:05:15 -0700 (PDT) Received: by mail-pf1-f182.google.com with SMTP id x15so7807178pfp.4 for ; Sun, 21 Aug 2022 15:05:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=0VwZUnoAKsTU3QZyo0Wn07IK3n+pQzKKsf+5q3gT3x4=; b=ltHuBzoqYI7bZrrie8ZnqzaaRtT4EnvxjRE+8gOk6jRgLxyDetHuRh89bbElfKP1lf 4VhJEI8pI7hmixCd6ebrhXE76Hx1sa5tQlPuUojm44xUVNdwkl45JDuyBMVyfiRwThVN dSa7PSbTXee4fnp6mhMpR5qoJ4vnv9zPL9y7XTxQWCxUiCTenOnd5SkAw+TToUh+Iqnp ZwV7ecCOxI/QLCyb4xBroUVDPAhJsyELNhBKnN14QnOzBrcKqpSdOEkbHjsSEdxyouqZ i9i91w3kF6Ffk9dd2jkgcnhNhWj1hFKkA/LVsRw76do6mgERjmIBfFKE8tsFGunIKdl8 ThaA== X-Gm-Message-State: ACgBeo0kQ2/e0PFKOk38+O1qna0DV/Rhs+Ac0xuQBIBBlr1EV2fteXEA cj6XQmzO1IjE4jpA8GeSHHQ= X-Google-Smtp-Source: AA6agR463FihpAVZOfZnxkdMkhGjpILCw7UsUOVuPvLmrW3yJ/vud1HNiqoF1Jq0/YH3SSZo79zFLg== X-Received: by 2002:a65:6bca:0:b0:420:712f:ab98 with SMTP id e10-20020a656bca000000b00420712fab98mr14624976pgw.350.1661119514740; Sun, 21 Aug 2022 15:05:14 -0700 (PDT) Received: from asus.hsd1.ca.comcast.net ([98.51.102.78]) by smtp.gmail.com with ESMTPSA id f17-20020a170902ce9100b0016c09a0ef87sm3110994plg.255.2022.08.21.15.05.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Aug 2022 15:05:13 -0700 (PDT) From: Bart Van Assche To: "Martin K . Petersen" Cc: linux-scsi@vger.kernel.org, Bart Van Assche , syzbot+bafeb834708b1bb750bc@syzkaller.appspotmail.com Subject: [PATCH 1/4] scsi: core: Revert "Call blk_mq_free_tag_set() earlier" Date: Sun, 21 Aug 2022 15:04:59 -0700 Message-Id: <20220821220502.13685-2-bvanassche@acm.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220821220502.13685-1-bvanassche@acm.org> References: <20220821220502.13685-1-bvanassche@acm.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org Revert the patch series "Call blk_mq_free_tag_set() earlier" because it introduces a deadlock if the scsi_remove_host() caller holds a reference on a device, target or host. Reported-by: syzbot+bafeb834708b1bb750bc@syzkaller.appspotmail.com Fixes: f323896fe6fa ("scsi: core: Call blk_mq_free_tag_set() earlier") Signed-off-by: Bart Van Assche --- drivers/scsi/hosts.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/drivers/scsi/hosts.c b/drivers/scsi/hosts.c index 0738238ed6cc..20c1f5420ba6 100644 --- a/drivers/scsi/hosts.c +++ b/drivers/scsi/hosts.c @@ -197,8 +197,6 @@ void scsi_remove_host(struct Scsi_Host *shost) * the dependent SCSI targets and devices are gone before returning. */ wait_event(shost->targets_wq, atomic_read(&shost->target_count) == 0); - - scsi_mq_destroy_tags(shost); } EXPORT_SYMBOL(scsi_remove_host); @@ -309,8 +307,8 @@ int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev, return error; /* - * Any resources associated with the SCSI host in this function except - * the tag set will be freed by scsi_host_dev_release(). + * Any host allocation in this function will be freed in + * scsi_host_dev_release(). */ out_del_dev: device_del(&shost->shost_dev); @@ -326,7 +324,6 @@ int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev, pm_runtime_disable(&shost->shost_gendev); pm_runtime_set_suspended(&shost->shost_gendev); pm_runtime_put_noidle(&shost->shost_gendev); - scsi_mq_destroy_tags(shost); fail: return error; } @@ -360,6 +357,9 @@ static void scsi_host_dev_release(struct device *dev) kfree(dev_name(&shost->shost_dev)); } + if (shost->tag_set.tags) + scsi_mq_destroy_tags(shost); + kfree(shost->shost_data); ida_free(&host_index_ida, shost->host_no); From patchwork Sun Aug 21 22:05:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 600554 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 39879C32789 for ; Sun, 21 Aug 2022 22:05:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231783AbiHUWFW (ORCPT ); Sun, 21 Aug 2022 18:05:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48472 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231328AbiHUWFU (ORCPT ); Sun, 21 Aug 2022 18:05:20 -0400 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 05CA813F87 for ; Sun, 21 Aug 2022 15:05:18 -0700 (PDT) Received: by mail-pf1-f173.google.com with SMTP id f17so2553787pfk.11 for ; Sun, 21 Aug 2022 15:05:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=7BkBiicCaEgyMOno+tfM9H2orpg5mKuuiwhxuVw8KTU=; b=orVoxvq5XAtFG71oeu8lID2OHikykh8eZX3nBiRVTn0xM93KQWZG10V8RTbBVGFZfV ogt/SU0GSbj4nwXvoJ+GMoF3/Sl844AipQa68BuYsReqFStP7nuy3n6yywm/xmX8vtUT pkjtzUSTY2bSJAw0vPLLbeBukBEUIvZJc9xjXn2N/Px+mePOtPfpA0c8OJef7Nl+Ws3/ TASCrVQGhdEO6uc6L9qUJV8PnFY/qsvHpXcAyD6+Wedwu5qrkEgclZchBrRENYoQhnN+ 6HhX4ra/gchPmiWDVIgL/d30XTQFKwSnG6eZ0tJB0OyMnl+ibm+vx40XxUA8Vej5YKRy sPjQ== X-Gm-Message-State: ACgBeo1utF0D+ESpcvPyr+LDSh/BkzvAKI2SmjNIFNZ4ynjsXxOrA+TT u7u9WDmt7IA3Nzc3b/lnCBg= X-Google-Smtp-Source: AA6agR7oMNs8P1upIocwLJ9yycKTsMOp4iw9KZLYx8sVYCJkpv3XTlaKtUopLghMspb4zOzzfWvWpQ== X-Received: by 2002:a63:5b4f:0:b0:426:9c52:a1f with SMTP id l15-20020a635b4f000000b004269c520a1fmr14667779pgm.511.1661119517371; Sun, 21 Aug 2022 15:05:17 -0700 (PDT) Received: from asus.hsd1.ca.comcast.net ([98.51.102.78]) by smtp.gmail.com with ESMTPSA id f17-20020a170902ce9100b0016c09a0ef87sm3110994plg.255.2022.08.21.15.05.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Aug 2022 15:05:15 -0700 (PDT) From: Bart Van Assche To: "Martin K . Petersen" Cc: linux-scsi@vger.kernel.org, Bart Van Assche , syzbot+bafeb834708b1bb750bc@syzkaller.appspotmail.com Subject: [PATCH 2/4] scsi: core: Revert "Simplify LLD module reference counting" Date: Sun, 21 Aug 2022 15:05:00 -0700 Message-Id: <20220821220502.13685-3-bvanassche@acm.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220821220502.13685-1-bvanassche@acm.org> References: <20220821220502.13685-1-bvanassche@acm.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org Revert the patch series "Call blk_mq_free_tag_set() earlier" because it introduces a deadlock if the scsi_remove_host() caller holds a reference on a device, target or host. Reported-by: syzbot+bafeb834708b1bb750bc@syzkaller.appspotmail.com Fixes: 1a9283782df2 ("scsi: core: Simplify LLD module reference counting") Signed-off-by: Bart Van Assche --- drivers/scsi/scsi.c | 9 +++------ drivers/scsi/scsi_sysfs.c | 9 +++++++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/drivers/scsi/scsi.c b/drivers/scsi/scsi.c index 086ec5b5862d..c59eac7a32f2 100644 --- a/drivers/scsi/scsi.c +++ b/drivers/scsi/scsi.c @@ -586,13 +586,10 @@ EXPORT_SYMBOL(scsi_device_get); */ void scsi_device_put(struct scsi_device *sdev) { - /* - * Decreasing the module reference count before the device reference - * count is safe since scsi_remove_host() only returns after all - * devices have been removed. - */ - module_put(sdev->host->hostt->module); + struct module *mod = sdev->host->hostt->module; + put_device(&sdev->sdev_gendev); + module_put(mod); } EXPORT_SYMBOL(scsi_device_put); diff --git a/drivers/scsi/scsi_sysfs.c b/drivers/scsi/scsi_sysfs.c index 9dad2fd5297f..282b32781e8c 100644 --- a/drivers/scsi/scsi_sysfs.c +++ b/drivers/scsi/scsi_sysfs.c @@ -452,6 +452,9 @@ static void scsi_device_dev_release_usercontext(struct work_struct *work) struct scsi_vpd *vpd_pg0 = NULL, *vpd_pg89 = NULL; struct scsi_vpd *vpd_pgb0 = NULL, *vpd_pgb1 = NULL, *vpd_pgb2 = NULL; unsigned long flags; + struct module *mod; + + mod = sdev->host->hostt->module; scsi_dh_release_device(sdev); @@ -518,11 +521,17 @@ static void scsi_device_dev_release_usercontext(struct work_struct *work) if (parent) put_device(parent); + module_put(mod); } static void scsi_device_dev_release(struct device *dev) { struct scsi_device *sdp = to_scsi_device(dev); + + /* Set module pointer as NULL in case of module unloading */ + if (!try_module_get(sdp->host->hostt->module)) + sdp->host->hostt->module = NULL; + execute_in_process_context(scsi_device_dev_release_usercontext, &sdp->ew); } From patchwork Sun Aug 21 22:05:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 599078 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9E9ABC00140 for ; Sun, 21 Aug 2022 22:05:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231786AbiHUWFX (ORCPT ); Sun, 21 Aug 2022 18:05:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48474 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231737AbiHUWFU (ORCPT ); Sun, 21 Aug 2022 18:05:20 -0400 Received: from mail-pj1-f48.google.com (mail-pj1-f48.google.com [209.85.216.48]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 98AFA13F7A for ; Sun, 21 Aug 2022 15:05:19 -0700 (PDT) Received: by mail-pj1-f48.google.com with SMTP id m15so1185511pjj.3 for ; Sun, 21 Aug 2022 15:05:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=BNKAEzSk3SNjegg5rWXH/+ZNTw8DhVQ+LI3LGlP9G4g=; b=N6o2Pn7nEF1MbXt5QC+3EiKA40Pn2AEnieJeZMFUTVxu5OvLvvv5qfEfXGBflF1R28 2NIgtHNVoA9rNCiBVdPzowSY/B3ypBmOR375WvKti0vSvGpFapk3BpyW0GY4HwsoJrlU bedE7yfdMSNKXExOQn5AzrmpARw0N/09npRdIFVTWJfK1f4ZJB5yxXGz2zSlx59FMI5T WtacnYOS8NvUiCauVQu/aKV2hWF5qhIYTUC03LgHvHYQgI2Cke0qKEi/BLvvtT2ScB6C O41v4c7qnUe9TIhQAPHq6/a/tqas15Sy0jboL9vybZsb4tlu7U5fXbhN12NMzL51CmSW NJug== X-Gm-Message-State: ACgBeo0s/PlvUlZPlzEKWQe9m66hK5o0hpbQ6G5MToe+WRz8ksv3vHGO eIbIi4R+CO6+Ff1zSvHTxAw= X-Google-Smtp-Source: AA6agR4s4XZk6eZeKGNm6siq5mN5zueKUVxhH650wzytsRttUGJ99zEUtDhavLc8T6SV2HNlD8Izxg== X-Received: by 2002:a17:903:489:b0:172:abe8:fed1 with SMTP id jj9-20020a170903048900b00172abe8fed1mr17591316plb.71.1661119519265; Sun, 21 Aug 2022 15:05:19 -0700 (PDT) Received: from asus.hsd1.ca.comcast.net ([98.51.102.78]) by smtp.gmail.com with ESMTPSA id f17-20020a170902ce9100b0016c09a0ef87sm3110994plg.255.2022.08.21.15.05.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Aug 2022 15:05:18 -0700 (PDT) From: Bart Van Assche To: "Martin K . Petersen" Cc: linux-scsi@vger.kernel.org, Bart Van Assche , syzbot+bafeb834708b1bb750bc@syzkaller.appspotmail.com Subject: [PATCH 3/4] scsi: core: Revert "Make sure that hosts outlive targets" Date: Sun, 21 Aug 2022 15:05:01 -0700 Message-Id: <20220821220502.13685-4-bvanassche@acm.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220821220502.13685-1-bvanassche@acm.org> References: <20220821220502.13685-1-bvanassche@acm.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org Revert the patch series "Call blk_mq_free_tag_set() earlier" because it introduces a deadlock if the scsi_remove_host() caller holds a reference on a device, target or host. Reported-by: syzbot+bafeb834708b1bb750bc@syzkaller.appspotmail.com Fixes: 16728aaba62e ("scsi: core: Make sure that hosts outlive targets") Signed-off-by: Bart Van Assche --- drivers/scsi/hosts.c | 8 -------- drivers/scsi/scsi_scan.c | 7 ------- include/scsi/scsi_host.h | 3 --- 3 files changed, 18 deletions(-) diff --git a/drivers/scsi/hosts.c b/drivers/scsi/hosts.c index 20c1f5420ba6..26bf3b153595 100644 --- a/drivers/scsi/hosts.c +++ b/drivers/scsi/hosts.c @@ -190,13 +190,6 @@ void scsi_remove_host(struct Scsi_Host *shost) transport_unregister_device(&shost->shost_gendev); device_unregister(&shost->shost_dev); device_del(&shost->shost_gendev); - - /* - * After scsi_remove_host() has returned the scsi LLD module can be - * unloaded and/or the host resources can be released. Hence wait until - * the dependent SCSI targets and devices are gone before returning. - */ - wait_event(shost->targets_wq, atomic_read(&shost->target_count) == 0); } EXPORT_SYMBOL(scsi_remove_host); @@ -406,7 +399,6 @@ struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize) INIT_LIST_HEAD(&shost->starved_list); init_waitqueue_head(&shost->host_wait); mutex_init(&shost->scan_mutex); - init_waitqueue_head(&shost->targets_wq); index = ida_alloc(&host_index_ida, GFP_KERNEL); if (index < 0) { diff --git a/drivers/scsi/scsi_scan.c b/drivers/scsi/scsi_scan.c index ac6059702d13..4c1efd6a3b0c 100644 --- a/drivers/scsi/scsi_scan.c +++ b/drivers/scsi/scsi_scan.c @@ -406,14 +406,9 @@ static void scsi_target_destroy(struct scsi_target *starget) static void scsi_target_dev_release(struct device *dev) { struct device *parent = dev->parent; - struct Scsi_Host *shost = dev_to_shost(parent); struct scsi_target *starget = to_scsi_target(dev); kfree(starget); - - if (atomic_dec_return(&shost->target_count) == 0) - wake_up(&shost->targets_wq); - put_device(parent); } @@ -528,8 +523,6 @@ static struct scsi_target *scsi_alloc_target(struct device *parent, starget->max_target_blocked = SCSI_DEFAULT_TARGET_BLOCKED; init_waitqueue_head(&starget->sdev_wq); - atomic_inc(&shost->target_count); - retry: spin_lock_irqsave(shost->host_lock, flags); diff --git a/include/scsi/scsi_host.h b/include/scsi/scsi_host.h index aa7b7496c93a..b6e41ee3d566 100644 --- a/include/scsi/scsi_host.h +++ b/include/scsi/scsi_host.h @@ -690,9 +690,6 @@ struct Scsi_Host { /* ldm bits */ struct device shost_gendev, shost_dev; - atomic_t target_count; - wait_queue_head_t targets_wq; - /* * Points to the transport data (if any) which is allocated * separately From patchwork Sun Aug 21 22:05:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 600553 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2DCC9C00140 for ; Sun, 21 Aug 2022 22:05:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231329AbiHUWF3 (ORCPT ); Sun, 21 Aug 2022 18:05:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48490 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231429AbiHUWFX (ORCPT ); Sun, 21 Aug 2022 18:05:23 -0400 Received: from mail-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D22B013F7A for ; Sun, 21 Aug 2022 15:05:21 -0700 (PDT) Received: by mail-pj1-f45.google.com with SMTP id s31-20020a17090a2f2200b001faaf9d92easo12189158pjd.3 for ; Sun, 21 Aug 2022 15:05:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=tMvkLpSPx0ef1ALnoiT9QUhcN0pT/j+rXogH1XEEfV8=; b=xS5xVXCaR3hUWQSaReY6eetDJ9MZ8s73XvdOUZGr86a3/uiY2++TPjYywsBOF6BG2X 6Yk6hUIMyGAdTsTdUqOETTkWVIEV1CSlB+QFJgJD6VAjWwDkTGroM2VwFcuBJoZpvGfD 6XK/rDWug9I/8BQt3L7lXzS4w07OMSZHQAp88TEkjB2foWARW2gXTz1xFGCPjhJzjfLo Y+qTZq+b2R9MVd210MGtbHHVtAzS5Ns+vgt2oe98t5PrIcvRvy5cluuiDbK4T+tl7140 UdsG4v/eEZh5VvZc5/tqOTD8R3TH9GLUNRvUOtJCb0y+pMMvNvqZHJu2cGQNSLi301zL C2UA== X-Gm-Message-State: ACgBeo0M9LbVx6cMCg4Gk/WmExrvl4dJokI4+MNsHgax8p4W0DMVViYT Li6Ri99c6g/HpwtIkedeoaE= X-Google-Smtp-Source: AA6agR5QNHduz5BMQIgHRvAW9vmWCrlt/yX4VcTWMKrMPGpT9zPr4FAfS73FOqfK3hZ/3vQmpxMn3Q== X-Received: by 2002:a17:90a:c702:b0:1fb:2beb:9606 with SMTP id o2-20020a17090ac70200b001fb2beb9606mr2727700pjt.158.1661119521245; Sun, 21 Aug 2022 15:05:21 -0700 (PDT) Received: from asus.hsd1.ca.comcast.net ([98.51.102.78]) by smtp.gmail.com with ESMTPSA id f17-20020a170902ce9100b0016c09a0ef87sm3110994plg.255.2022.08.21.15.05.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Aug 2022 15:05:20 -0700 (PDT) From: Bart Van Assche To: "Martin K . Petersen" Cc: linux-scsi@vger.kernel.org, Bart Van Assche , syzbot+bafeb834708b1bb750bc@syzkaller.appspotmail.com Subject: [PATCH 4/4] scsi: core: Revert "Make sure that targets outlive devices" Date: Sun, 21 Aug 2022 15:05:02 -0700 Message-Id: <20220821220502.13685-5-bvanassche@acm.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220821220502.13685-1-bvanassche@acm.org> References: <20220821220502.13685-1-bvanassche@acm.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org Revert the patch series "Call blk_mq_free_tag_set() earlier" because it introduces a deadlock if the scsi_remove_host() caller holds a reference on a device, target or host. Reported-by: syzbot+bafeb834708b1bb750bc@syzkaller.appspotmail.com Fixes: fe442604199e ("scsi: core: Make sure that targets outlive devices") Signed-off-by: Bart Van Assche --- drivers/scsi/scsi_scan.c | 2 -- drivers/scsi/scsi_sysfs.c | 20 +++----------------- include/scsi/scsi_device.h | 2 -- 3 files changed, 3 insertions(+), 21 deletions(-) diff --git a/drivers/scsi/scsi_scan.c b/drivers/scsi/scsi_scan.c index 4c1efd6a3b0c..91ac901a6682 100644 --- a/drivers/scsi/scsi_scan.c +++ b/drivers/scsi/scsi_scan.c @@ -521,8 +521,6 @@ static struct scsi_target *scsi_alloc_target(struct device *parent, starget->state = STARGET_CREATED; starget->scsi_level = SCSI_2; starget->max_target_blocked = SCSI_DEFAULT_TARGET_BLOCKED; - init_waitqueue_head(&starget->sdev_wq); - retry: spin_lock_irqsave(shost->host_lock, flags); diff --git a/drivers/scsi/scsi_sysfs.c b/drivers/scsi/scsi_sysfs.c index 282b32781e8c..aa70d9282161 100644 --- a/drivers/scsi/scsi_sysfs.c +++ b/drivers/scsi/scsi_sysfs.c @@ -443,9 +443,7 @@ static void scsi_device_cls_release(struct device *class_dev) static void scsi_device_dev_release_usercontext(struct work_struct *work) { - struct scsi_device *sdev = container_of(work, struct scsi_device, - ew.work); - struct scsi_target *starget = sdev->sdev_target; + struct scsi_device *sdev; struct device *parent; struct list_head *this, *tmp; struct scsi_vpd *vpd_pg80 = NULL, *vpd_pg83 = NULL; @@ -454,6 +452,8 @@ static void scsi_device_dev_release_usercontext(struct work_struct *work) unsigned long flags; struct module *mod; + sdev = container_of(work, struct scsi_device, ew.work); + mod = sdev->host->hostt->module; scsi_dh_release_device(sdev); @@ -516,9 +516,6 @@ static void scsi_device_dev_release_usercontext(struct work_struct *work) kfree(sdev->inquiry); kfree(sdev); - if (starget && atomic_dec_return(&starget->sdev_count) == 0) - wake_up(&starget->sdev_wq); - if (parent) put_device(parent); module_put(mod); @@ -1538,14 +1535,6 @@ static void __scsi_remove_target(struct scsi_target *starget) goto restart; } spin_unlock_irqrestore(shost->host_lock, flags); - - /* - * After scsi_remove_target() returns its caller can remove resources - * associated with @starget, e.g. an rport or session. Wait until all - * devices associated with @starget have been removed to prevent that - * a SCSI error handling callback function triggers a use-after-free. - */ - wait_event(starget->sdev_wq, atomic_read(&starget->sdev_count) == 0); } /** @@ -1656,9 +1645,6 @@ void scsi_sysfs_device_initialize(struct scsi_device *sdev) list_add_tail(&sdev->same_target_siblings, &starget->devices); list_add_tail(&sdev->siblings, &shost->__devices); spin_unlock_irqrestore(shost->host_lock, flags); - - atomic_inc(&starget->sdev_count); - /* * device can now only be removed via __scsi_remove_device() so hold * the target. Target will be held in CREATED state until something diff --git a/include/scsi/scsi_device.h b/include/scsi/scsi_device.h index 3113471ca375..2493bd65351a 100644 --- a/include/scsi/scsi_device.h +++ b/include/scsi/scsi_device.h @@ -309,8 +309,6 @@ struct scsi_target { struct list_head devices; struct device dev; struct kref reap_ref; /* last put renders target invisible */ - atomic_t sdev_count; - wait_queue_head_t sdev_wq; unsigned int channel; unsigned int id; /* target id ... replace * scsi_device.id eventually */