From patchwork Fri Jan 8 07:15:45 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nicholas A. Bellinger" X-Patchwork-Id: 7982991 Return-Path: X-Original-To: patchwork-linux-scsi@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 02019BEEE5 for ; Fri, 8 Jan 2016 07:19:52 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 1C38020138 for ; Fri, 8 Jan 2016 07:19:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 16B54200ED for ; Fri, 8 Jan 2016 07:19:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754050AbcAHHTi (ORCPT ); Fri, 8 Jan 2016 02:19:38 -0500 Received: from mail-oi0-f47.google.com ([209.85.218.47]:35497 "EHLO mail-oi0-f47.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751690AbcAHHRx (ORCPT ); Fri, 8 Jan 2016 02:17:53 -0500 Received: by mail-oi0-f47.google.com with SMTP id l9so301842884oia.2 for ; Thu, 07 Jan 2016 23:17:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daterainc-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=wkyUIUd8Qr/TnLGEVek7gU1KfGTJDxnhMYaOO6DVWcs=; b=sRWxXKjRi7Go+OZJG3NzoA7OaDAXomwwesfXp7iTlt1EX01G2YaRamPcPRauud6d8d QerqRhy29cAfYSk0wnlEpESMDv4zzmVitkK96QuEOrZGzwE4dSfotXjuiCkQjCtnbad9 UYNKpTz3SIOZjnWSSk+BPYMPVBQJHQJpDRvyebnimmP2Hch306h4alHUd4zMyS0Jr4PX a/PPPdjorphFdsdboTbgBNu7/ZXGySTZwjNKXqbzzeDFVK8oCtbRVJ7VQaW4XJKcN6GA +U9rYq512KXkST4F0j14MAX2+bSOlgg9O2lc+zWcYRyhbVdrN8U3KO+tJGs+rwWh+XmG AFeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=wkyUIUd8Qr/TnLGEVek7gU1KfGTJDxnhMYaOO6DVWcs=; b=fgmBqsIlRodoC9urCDrkaAs+9JNjLDDGbaN5HYpl113rx1W1/xRnUUA0lP5OCmkwn6 WwDlFomMI2/H/gItNRhkifjJOMHA+k/iuZu9ImK3Zf/BZCjLGSOf9HLPQ1j1VhgA7die 9wL7DsvufL/7wi9zEbuCXMZK0LZlm6ugEOpCfdcam7eYARiYntoYnVMrX71j/GIGubyd yFL4nbEP+j9g1+703ZQJ8rRGDviSErBV1fMxQCWq6SyyHLywt0MQYhcQGyqWNlsmKjx7 6qdRK+IQ5Y/kx1iszmfWakeRkTP/TDUM/LdW0IZcDNdC++rwETcE7XbOTd+qdAhyNNOZ vDjA== X-Gm-Message-State: ALoCoQkm/63McmuJh6OTeMBNFJV8+GVKh/9Bw+80gtQt5fygwxeGMlUBjytTDmngcTzrRnrcLkxs/ZRKV2KScucU1Clh3zLY4g== X-Received: by 10.202.79.139 with SMTP id d133mr75251184oib.20.1452237472909; Thu, 07 Jan 2016 23:17:52 -0800 (PST) Received: from localhost.localdomain (mail.linux-iscsi.org. [67.23.28.174]) by smtp.gmail.com with ESMTPSA id l19sm5788137oig.27.2016.01.07.23.17.52 (version=TLS1 cipher=AES128-SHA bits=128/128); Thu, 07 Jan 2016 23:17:52 -0800 (PST) From: "Nicholas A. Bellinger" To: target-devel Cc: linux-scsi , lkml , Sagi Grimberg , Christoph Hellwig , Hannes Reinecke , Andy Grover , Vasu Dev , Vu Pham , Nicholas Bellinger Subject: [PATCH 1/4] target: Obtain se_node_acl->acl_kref during get_initiator_node_acl Date: Fri, 8 Jan 2016 07:15:45 +0000 Message-Id: <1452237348-2277-2-git-send-email-nab@daterainc.com> X-Mailer: git-send-email 1.7.2.5 In-Reply-To: <1452237348-2277-1-git-send-email-nab@daterainc.com> References: <1452237348-2277-1-git-send-email-nab@daterainc.com> Sender: linux-scsi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,RP_MATCHES_RCVD,T_DKIM_INVALID,UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Nicholas Bellinger This patch addresses a long standing race where obtaining se_node_acl->acl_kref in __transport_register_session() happens a bit too late, and leaves open the potential for core_tpg_del_initiator_node_acl() to hit a NULL pointer dereference. Instead, get ->acl_kref in core_tpg_get_initiator_node_acl() while se_portal_group->acl_node_mutex is held, and move the final target_put_nacl() from transport_deregister_session() into transport_free_session() so that fabric driver login failure handling using the modern method to still work as expected. Note the existing wait_for_completion(&acl->acl_free_comp) in core_tpg_del_initiator_node_acl() does not change. Cc: Sagi Grimberg Cc: Christoph Hellwig Cc: Hannes Reinecke Cc: Andy Grover Signed-off-by: Nicholas Bellinger --- drivers/target/target_core_tpg.c | 6 ++++++ drivers/target/target_core_transport.c | 18 ++++++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/drivers/target/target_core_tpg.c b/drivers/target/target_core_tpg.c index 62103a8..fb77fe1 100644 --- a/drivers/target/target_core_tpg.c +++ b/drivers/target/target_core_tpg.c @@ -78,6 +78,12 @@ struct se_node_acl *core_tpg_get_initiator_node_acl( mutex_lock(&tpg->acl_node_mutex); acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname); + /* + * Obtain the acl_kref now, which will be dropped upon the + * release of se_sess memory within transport_free_session(). + */ + if (acl) + kref_get(&acl->acl_kref); mutex_unlock(&tpg->acl_node_mutex); return acl; diff --git a/drivers/target/target_core_transport.c b/drivers/target/target_core_transport.c index eb7aaf0..81cc699 100644 --- a/drivers/target/target_core_transport.c +++ b/drivers/target/target_core_transport.c @@ -341,7 +341,6 @@ void __transport_register_session( &buf[0], PR_REG_ISID_LEN); se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]); } - kref_get(&se_nacl->acl_kref); spin_lock_irq(&se_nacl->nacl_sess_lock); /* @@ -464,6 +463,15 @@ EXPORT_SYMBOL(transport_deregister_session_configfs); void transport_free_session(struct se_session *se_sess) { + struct se_node_acl *se_nacl = se_sess->se_node_acl; + /* + * Drop the se_node_acl->nacl_kref obtained from within + * core_tpg_get_initiator_node_acl(). + */ + if (se_nacl) { + se_sess->se_node_acl = NULL; + target_put_nacl(se_nacl); + } if (se_sess->sess_cmd_map) { percpu_ida_destroy(&se_sess->sess_tag_pool); kvfree(se_sess->sess_cmd_map); @@ -478,7 +486,7 @@ void transport_deregister_session(struct se_session *se_sess) const struct target_core_fabric_ops *se_tfo; struct se_node_acl *se_nacl; unsigned long flags; - bool comp_nacl = true, drop_nacl = false; + bool drop_nacl = false; if (!se_tpg) { transport_free_session(se_sess); @@ -510,18 +518,16 @@ void transport_deregister_session(struct se_session *se_sess) if (drop_nacl) { core_tpg_wait_for_nacl_pr_ref(se_nacl); core_free_device_list_for_node(se_nacl, se_tpg); + se_sess->se_node_acl = NULL; kfree(se_nacl); - comp_nacl = false; } pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n", se_tpg->se_tpg_tfo->get_fabric_name()); /* * If last kref is dropping now for an explicit NodeACL, awake sleeping * ->acl_free_comp caller to wakeup configfs se_node_acl->acl_group - * removal context. + * removal context from within transport_free_session() code. */ - if (se_nacl && comp_nacl) - target_put_nacl(se_nacl); transport_free_session(se_sess); }