From patchwork Wed Oct 18 16:52:49 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 10015055 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 08564600CC for ; Wed, 18 Oct 2017 16:53:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EE988291E5 for ; Wed, 18 Oct 2017 16:53:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E3A45291EF; Wed, 18 Oct 2017 16:53:34 +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.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID 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 9AD4F291E5 for ; Wed, 18 Oct 2017 16:53:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751739AbdJRQxd (ORCPT ); Wed, 18 Oct 2017 12:53:33 -0400 Received: from bombadil.infradead.org ([65.50.211.133]:55613 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751411AbdJRQxd (ORCPT ); Wed, 18 Oct 2017 12:53:33 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=References:In-Reply-To:Message-Id: Date:Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=xYSFlGfyDDwmKx9D6b7/AXPqiOa7Gxc9PJi6VmBJb1Y=; b=MqI/EWo2DD0ETrWwtVUj/sLfO Xp6/IHKXFKiiwXBBMBRxr8+N8OxmSPM/+SSCH9nbVVcZLpIAdTg7f8Zjdc7z8i/gdOD3pcSdQA/W7 UeVEJsYC7VX1gWUe+o1JnAI1+nn3vG4ty5JONwMG+6g6xaZBFMkKyEZz2JXh0rKsnwIPTPhJI75Ob mkKPF6gI7E31oCouqWj7wZ/2F0YD+Y7qK7byqU7i2bxcQPKzk4rWsguqtPjiaAH24/3wiSpUJZllw 8T7J+ASG6JCY24sTMvm4dTGOQtPqhppOCU9vc59j0xYKUHmis84jKiazDlSxAkJJ9h68Om3ToviS+ FDDIoBNFQ==; Received: from clnet-p099-196.ikbnet.co.at ([83.175.99.196] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.87 #1 (Red Hat Linux)) id 1e4raw-0004Ac-Ak; Wed, 18 Oct 2017 16:53:26 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Keith Busch , Sagi Grimberg , Hannes Reinecke , Johannes Thumshirn , linux-nvme@lists.infradead.org, linux-block@vger.kernel.org Subject: [PATCH 08/17] nvme: use kref_get_unless_zero in nvme_find_get_ns Date: Wed, 18 Oct 2017 18:52:49 +0200 Message-Id: <20171018165258.23212-9-hch@lst.de> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20171018165258.23212-1-hch@lst.de> References: <20171018165258.23212-1-hch@lst.de> X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP For kref_get_unless_zero to protect against lookup vs free races we need to use it in all places where we aren't guaranteed to already hold a reference. There is no such guarantee in nvme_find_get_ns, so switch to kref_get_unless_zero in this function. Signed-off-by: Christoph Hellwig Reviewed-by: Johannes Thumshirn Reviewed-by: Sagi Grimberg --- drivers/nvme/host/core.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c index bdae1e4fcd0b..3f51bd0326b5 100644 --- a/drivers/nvme/host/core.c +++ b/drivers/nvme/host/core.c @@ -2290,7 +2290,8 @@ static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid) mutex_lock(&ctrl->namespaces_mutex); list_for_each_entry(ns, &ctrl->namespaces, list) { if (ns->ns_id == nsid) { - kref_get(&ns->kref); + if (!kref_get_unless_zero(&ns->kref)) + continue; ret = ns; break; }