From patchwork Fri Oct 21 23:57:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wenchao Hao X-Patchwork-Id: 6635 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4242:0:0:0:0:0 with SMTP id s2csp622868wrr; Fri, 21 Oct 2022 03:44:21 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7ks0250mXEtkweB+mhKPJP6r8tOcX8G4xxlqiO07GFTfoBntdGHN1gXOlLsOjFbLRJulRu X-Received: by 2002:a17:906:fd84:b0:730:acee:d067 with SMTP id xa4-20020a170906fd8400b00730aceed067mr15195071ejb.206.1666349061513; Fri, 21 Oct 2022 03:44:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666349061; cv=none; d=google.com; s=arc-20160816; b=cpaG6hDqyZaJRBpw9Id931skVufZhouGEwM98KR/OAM5bj6jUvP8UI74LJpGtFcsgZ RQacbEHataaOwsdWBGSYjuLsV58sKImjMhH/vMr6fRjUUnQDA6tBH3hBtvLh/yvXfU1A jiSPG6AyWrc2VqoChZHS52C6td/vnpeUFZ44l5zBwN7SuA67qNiRcWiNbZLXNKpCI2H+ B2BWBDOCdJBtSEcYBkktdhoxQoVxhOI80AVZI9f5oPkFi6Neuj5UnJqDBgK5wPf06Lx/ mM+pMgI7lLCekaAXgRcN41OlF86ITyaU+yK3MXzQtrZMI4ifRki84TtIlkBTwWsk5FIc VVVA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=6Pz3lnA/ym8V4MsTjJtj8HExCzj6d4leZaHybOixoag=; b=LUDG4IBGgygGvMkw5++IWofkVHdQ8gtPmZWsZY93J/3frSPfgXZq37NDq6PiDQ2IA5 xHy7h47cRD2SnQDmTFSA2hcq+V5yfwrMqfoCbOZTI051nV9pcHlcMvfOoIeND2UGmDUn hE/TiGtOAciu4y1JaPk1LBzUOv1STxgWUoFjBWVtyeXTc4Uisu4MqiwvH4F84aXZ1ses zB0YHR6alcw9crwVR/ywY8I79mi8fzOQNm/x3RrDbh/SqdQucWZAGuZNpIUuF7DHR7Ec ABs68jvLvdihF1bSzmfvsB27HhP4VFs/uWrKjp0fvpXgnjqwXaF6z96shERhKntKO004 QtIg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id pk21-20020a170906d7b500b0078db6f488c2si17677108ejb.56.2022.10.21.03.43.56; Fri, 21 Oct 2022 03:44:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230024AbiJUKlq (ORCPT + 99 others); Fri, 21 Oct 2022 06:41:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59370 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229835AbiJUKle (ORCPT ); Fri, 21 Oct 2022 06:41:34 -0400 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 19D1725E0AD; Fri, 21 Oct 2022 03:41:31 -0700 (PDT) Received: from dggpemm500024.china.huawei.com (unknown [172.30.72.54]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4Mv19R17fKz15LxN; Fri, 21 Oct 2022 18:36:43 +0800 (CST) Received: from dggpemm500017.china.huawei.com (7.185.36.178) by dggpemm500024.china.huawei.com (7.185.36.203) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 21 Oct 2022 18:41:29 +0800 Received: from build.huawei.com (10.175.101.6) by dggpemm500017.china.huawei.com (7.185.36.178) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 21 Oct 2022 18:41:28 +0800 From: Wenchao Hao To: Lee Duncan , Chris Leech , "Mike Christie" , "James E . J . Bottomley" , "Martin K . Petersen" , , CC: Steffen Maier , , , Wenchao Hao Subject: [PATCH v4] scsi:iscsi: Fix multiple iscsi session unbind event sent to userspace Date: Fri, 21 Oct 2022 19:57:54 -0400 Message-ID: <20221021235754.1968981-1-haowenchao@huawei.com> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 X-Originating-IP: [10.175.101.6] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpemm500017.china.huawei.com (7.185.36.178) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.0 required=5.0 tests=BAYES_00,DATE_IN_FUTURE_12_24, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747293633268592450?= X-GMAIL-MSGID: =?utf-8?q?1747293633268592450?= I found an issue that kernel would send ISCSI_KEVENT_UNBIND_SESSION for multiple times which should be fixed. This patch introduce target_state in iscsi_cls_session to make sure session would send only one ISCSI_KEVENT_UNBIND_SESSION. But this would break issue fixed in commit 13e60d3ba287 ("scsi: iscsi: Report unbind session event when the target has been removed"). The issue is iscsid died for any reason after it send unbind session to kernel, once iscsid restart again, it loss kernel's ISCSI_KEVENT_UNBIND_SESSION event. Now kernel think iscsi_cls_session has already sent an ISCSI_KEVENT_UNBIND_SESSION event and would not send it any more. Which would cause userspace unable to logout. Actually the session is in invalid state(it's target_id is INVALID), iscsid should not sync this session in it's restart. So we need to check session's target state during iscsid restart, if session is in unbound state, do not sync this session and perform session teardown. It's reasonable because once a session is unbound, we can not recover it any more(mainly because it's target id is INVALID) V4: - Move debug print out of spinlock critical section V3: - Make target bind state to a state kind rather than a bool. V2: - Using target_unbound rather than state to indicate session has been unbound Signed-off-by: Wenchao Hao --- drivers/scsi/scsi_transport_iscsi.c | 49 +++++++++++++++++++++++++++++ include/scsi/scsi_transport_iscsi.h | 7 +++++ 2 files changed, 56 insertions(+) diff --git a/drivers/scsi/scsi_transport_iscsi.c b/drivers/scsi/scsi_transport_iscsi.c index cd3db9684e52..1ed279831a78 100644 --- a/drivers/scsi/scsi_transport_iscsi.c +++ b/drivers/scsi/scsi_transport_iscsi.c @@ -1676,6 +1676,29 @@ static const char *iscsi_session_state_name(int state) return name; } +static struct { + int value; + char *name; +} iscsi_session_target_state_names[] = { + { ISCSI_SESSION_TARGET_UNBOUND, "UNBOUND" }, + { ISCSI_SESSION_TARGET_BOUND, "BOUND" }, + { ISCSI_SESSION_TARGET_UNBINDING, "UNBINDING" }, +}; + +static const char *iscsi_session_target_state_name(int state) +{ + int i; + char *name = NULL; + + for (i = 0; i < ARRAY_SIZE(iscsi_session_target_state_names); i++) { + if (iscsi_session_target_state_names[i].value == state) { + name = iscsi_session_target_state_names[i].name; + break; + } + } + return name; +} + int iscsi_session_chkready(struct iscsi_cls_session *session) { int err; @@ -1899,6 +1922,7 @@ static void __iscsi_unblock_session(struct work_struct *work) cancel_delayed_work_sync(&session->recovery_work); spin_lock_irqsave(&session->lock, flags); session->state = ISCSI_SESSION_LOGGED_IN; + session->target_state = ISCSI_SESSION_TARGET_BOUND; spin_unlock_irqrestore(&session->lock, flags); /* start IO */ scsi_target_unblock(&session->dev, SDEV_RUNNING); @@ -1961,6 +1985,15 @@ static void __iscsi_unbind_session(struct work_struct *work) unsigned long flags; unsigned int target_id; + spin_lock_irqsave(&session->lock, flags); + if (session->target_state != ISCSI_SESSION_TARGET_BOUND) { + spin_unlock_irqrestore(&session->lock, flags); + ISCSI_DBG_TRANS_SESSION(session, "Abort unbind sesison\n"); + return; + } + session->target_state = ISCSI_SESSION_TARGET_UNBINDING; + spin_unlock_irqrestore(&session->lock, flags); + ISCSI_DBG_TRANS_SESSION(session, "Unbinding session\n"); /* Prevent new scans and make sure scanning is not in progress */ @@ -1984,6 +2017,9 @@ static void __iscsi_unbind_session(struct work_struct *work) unbind_session_exit: iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION); + spin_lock_irqsave(&session->lock, flags); + session->target_state = ISCSI_SESSION_TARGET_UNBOUND; + spin_unlock_irqrestore(&session->lock, flags); ISCSI_DBG_TRANS_SESSION(session, "Completed target removal\n"); } @@ -4368,6 +4404,16 @@ iscsi_session_attr(def_taskmgmt_tmo, ISCSI_PARAM_DEF_TASKMGMT_TMO, 0); iscsi_session_attr(discovery_parent_idx, ISCSI_PARAM_DISCOVERY_PARENT_IDX, 0); iscsi_session_attr(discovery_parent_type, ISCSI_PARAM_DISCOVERY_PARENT_TYPE, 0); +static ssize_t +show_priv_session_target_state(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent); + return sysfs_emit(buf, "%s\n", + iscsi_session_target_state_name(session->target_state)); +} +static ISCSI_CLASS_ATTR(priv_sess, target_state, S_IRUGO, + show_priv_session_target_state, NULL); static ssize_t show_priv_session_state(struct device *dev, struct device_attribute *attr, char *buf) @@ -4470,6 +4516,7 @@ static struct attribute *iscsi_session_attrs[] = { &dev_attr_sess_boot_target.attr, &dev_attr_priv_sess_recovery_tmo.attr, &dev_attr_priv_sess_state.attr, + &dev_attr_priv_sess_target_state.attr, &dev_attr_priv_sess_creator.attr, &dev_attr_sess_chap_out_idx.attr, &dev_attr_sess_chap_in_idx.attr, @@ -4583,6 +4630,8 @@ static umode_t iscsi_session_attr_is_visible(struct kobject *kobj, return S_IRUGO | S_IWUSR; else if (attr == &dev_attr_priv_sess_state.attr) return S_IRUGO; + else if (attr == &dev_attr_priv_sess_target_state.attr) + return S_IRUGO; else if (attr == &dev_attr_priv_sess_creator.attr) return S_IRUGO; else if (attr == &dev_attr_priv_sess_target_id.attr) diff --git a/include/scsi/scsi_transport_iscsi.h b/include/scsi/scsi_transport_iscsi.h index cab52b0f11d0..d59e31ebbfdf 100644 --- a/include/scsi/scsi_transport_iscsi.h +++ b/include/scsi/scsi_transport_iscsi.h @@ -236,6 +236,12 @@ enum { ISCSI_SESSION_FREE, }; +enum { + ISCSI_SESSION_TARGET_UNBOUND, + ISCSI_SESSION_TARGET_BOUND, + ISCSI_SESSION_TARGET_UNBINDING, +}; + #define ISCSI_MAX_TARGET -1 struct iscsi_cls_session { @@ -264,6 +270,7 @@ struct iscsi_cls_session { */ pid_t creator; int state; + int target_state; /* session target bind state */ int sid; /* session id */ void *dd_data; /* LLD private data */ struct device dev; /* sysfs transport/container device */