From patchwork Fri May 12 13:15:08 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Wilck X-Patchwork-Id: 9724115 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 1186A600CB for ; Fri, 12 May 2017 13:15:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E959E2881E for ; Fri, 12 May 2017 13:15:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DD76F28822; Fri, 12 May 2017 13:15:50 +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=-6.9 required=2.0 tests=BAYES_00,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 81CE72881E for ; Fri, 12 May 2017 13:15:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756435AbdELNPt (ORCPT ); Fri, 12 May 2017 09:15:49 -0400 Received: from prv3-mh.provo.novell.com ([137.65.250.26]:35796 "EHLO prv3-mh.provo.novell.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755736AbdELNPr (ORCPT ); Fri, 12 May 2017 09:15:47 -0400 Received: from apollon.suse.de.de (prv-ext-foundry1int.gns.novell.com [137.65.251.240]) by prv3-mh.provo.novell.com with ESMTP (TLS encrypted); Fri, 12 May 2017 07:15:34 -0600 From: Martin Wilck To: "Martin K. Petersen" , Hannes Reinecke Cc: Bart Van Assche , Mauricio Faria de Oliveira , linux-scsi@vger.kernel.org Subject: [PATCH v2 4/4] scsi_dh_alua: take sdev reference in alua_bus_attach Date: Fri, 12 May 2017 15:15:08 +0200 Message-Id: <20170512131508.3231-5-mwilck@suse.com> X-Mailer: git-send-email 2.12.2 In-Reply-To: <20170512131508.3231-1-mwilck@suse.com> References: <20170512131508.3231-1-mwilck@suse.com> 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 Modification of the access_state field in struct scsi_device in alua_rtpg() may race with alua_bus_detach(). Avoid the scsi_device struct to be freed while it's being processed in the alua code by taking a reference. Signed-off-by: Martin Wilck --- drivers/scsi/device_handler/scsi_dh_alua.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/drivers/scsi/device_handler/scsi_dh_alua.c b/drivers/scsi/device_handler/scsi_dh_alua.c index a59783020c66..4b33e076d4f7 100644 --- a/drivers/scsi/device_handler/scsi_dh_alua.c +++ b/drivers/scsi/device_handler/scsi_dh_alua.c @@ -1108,6 +1108,10 @@ static int alua_bus_attach(struct scsi_device *sdev) h = kzalloc(sizeof(*h) , GFP_KERNEL); if (!h) return -ENOMEM; + if (scsi_device_get(sdev)) { + ret = -ENXIO; + goto failed; + } spin_lock_init(&h->pg_lock); rcu_assign_pointer(h->pg, NULL); h->init_error = SCSI_DH_OK; @@ -1119,10 +1123,13 @@ static int alua_bus_attach(struct scsi_device *sdev) if (err == SCSI_DH_NOMEM) ret = -ENOMEM; if (err != SCSI_DH_OK && err != SCSI_DH_DEV_OFFLINED) - goto failed; + goto failed_put; sdev->handler_data = h; return 0; + +failed_put: + scsi_device_put(sdev); failed: kfree(h); return ret; @@ -1140,6 +1147,7 @@ static void alua_bus_detach(struct scsi_device *sdev) spin_lock(&h->pg_lock); pg = rcu_dereference_protected(h->pg, lockdep_is_held(&h->pg_lock)); rcu_assign_pointer(h->pg, NULL); + scsi_device_put(h->sdev); h->sdev = NULL; spin_unlock(&h->pg_lock); if (pg) {