Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp129140imm; Thu, 14 Jun 2018 16:48:24 -0700 (PDT) X-Google-Smtp-Source: ADUXVKKrGdJ3LohjlUCaW4XJhVcyuKPw1bygFcWD3qT9wrHR6OAhoA5+kSAONeQEa6XXcOqXuG1U X-Received: by 2002:a17:902:7c16:: with SMTP id x22-v6mr5136826pll.77.1529020104526; Thu, 14 Jun 2018 16:48:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529020104; cv=none; d=google.com; s=arc-20160816; b=wp3UlGhyaC/MRbgXYr+1DRNoNw+BIQJqbdwmr6DgDK+AD7xtOT5pabN+0j0EW7vyNH PhbViWtGz8b0lpxHWJSEeSLxlA3k4IX9V9u8U+BlvTOSuuVpO1+BMiBwbyoNuYHo5OiF 5GBZzVO13Mb1BU7GLxqsf6Ex6KjhNL/R9Nt9gh4cBHXLgTTwZPhMiO+fYhG5KchrqWs/ dEfnkuxKyYiV54Kyyu0G2LecVQlRmuziBkzUy2cOW+MRB0yfuvSDX3XcQ5Mdw6p2hZE5 zne8eeAw8/AIVaYulh/k91KqRdOM2PfiEZTKQXu8n98cYsYNHUT1UIrA68U/DHFqbZF9 X2LA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:arc-authentication-results; bh=MzxlJPCgLWvSKXPajxKaJnmmaMkzzFbi7bj7RF7h3Dc=; b=Xbae3hKmd4pQu8k4kCi7wI0LoCngRcvioHqQptEdy3sYLAwjPFiiszH2zuLyDFj5b4 DG0TtyJ44j2uKTGoYe8P1MsCxbS1KkUmBj2n4ONpWJGZzpECTURxNamyZqfSqEmq8ceQ 565MI+HLg4YYrbhhaA1X5teMnBbgJbl9QqoEsp14kZrlPfKCuiz74eOkb8kx48MfwIZu 4UGtrI031b4odCykc1OKVUhyYv+6EXgV5++MqhptJzfW6SyKD1ZM0L6GOckEc0RrhcOS aMLOwIpzPHDP1l3X4sLW1Bgd65ufRdPRl6xUpKFjGXQwfOY38WEz3l2eqL5KBcWmSSFa HARg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=jkZoNBem; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t195-v6si5288300pgb.323.2018.06.14.16.48.09; Thu, 14 Jun 2018 16:48:24 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=jkZoNBem; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965143AbeFNXrk (ORCPT + 99 others); Thu, 14 Jun 2018 19:47:40 -0400 Received: from mail-pg0-f65.google.com ([74.125.83.65]:33774 "EHLO mail-pg0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S965001AbeFNXri (ORCPT ); Thu, 14 Jun 2018 19:47:38 -0400 Received: by mail-pg0-f65.google.com with SMTP id e11-v6so3619559pgq.0; Thu, 14 Jun 2018 16:47:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=MzxlJPCgLWvSKXPajxKaJnmmaMkzzFbi7bj7RF7h3Dc=; b=jkZoNBemhGFvJOnjKDdIKCjsO4rCYD6i/4zpFkBvE+MBx++EfWghjgsopuJIo0lMt/ txjjQZfRKrRiSBXajhIhkwhDpQxfqk0Wai1sm1Q3Kp54aLS2QCDEZInj703r/6fekUm2 xSXeD8/lKuBuerUZAxwMqT/oy/BkGl8ZixBYFC825K+ewdeAXgpvDAoV12sqz7RuNPzH Vb8cSeTpUf+mcyXJrRGj5ZZlvitVYk9vlZRJjmgUEfVd5D6oKIFHs5tuUydGh7EQ6nfv Z/Uf4GWhAfDkZ0JXoVUImcN64RPB28hvUC+npf4u7sFV8Eiv4yE+/p5Zid2MCq9XzqPy Zspg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=MzxlJPCgLWvSKXPajxKaJnmmaMkzzFbi7bj7RF7h3Dc=; b=H4TJEOja2+23ErlLTenX9GQhFdjOUBz652QqNpAYOtYNfHAIFu8KLl9sPQsS1Mo3ab 50/i/9INuYq/8BAb4L4yDoZq/8tXPeqwZ8gFa79GcuRYRARwInS28LhbGdPx4P+kTDKP 61X4P1duvY1QvKmW13tmKliAkYQmDDbqhJXvmx/lVYHE9gEk0V79Fey5dNQ0/VdJ0L0i K89jcWKrJ9nB1+Z0HRvFJU3vOkMfLLmDBSx2hyP/1pSB4NeIT6kJu5RLyTVCUkfzvXVr TRCsrLxoIZ+4+xtoykfQT6txnDNyo+dmuiIfflbmFbTusZNw7pgGVp4Ovtkmj7zAQxZG TgSA== X-Gm-Message-State: APt69E0h1VmAb+Yz8iaWB9bAaCHPb59FbV6EQpkxgoMFdzfZHogNexVb /rBnIQPpXXFR8fOhN25xsHQ= X-Received: by 2002:a63:7741:: with SMTP id s62-v6mr4113059pgc.103.1529020057573; Thu, 14 Jun 2018 16:47:37 -0700 (PDT) Received: from xldev-tmpl.dev.purestorage.com ([192.30.188.252]) by smtp.gmail.com with ESMTPSA id b15-v6sm8075193pgu.54.2018.06.14.16.47.36 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Thu, 14 Jun 2018 16:47:36 -0700 (PDT) Date: Thu, 14 Jun 2018 17:47:34 -0600 From: Anatoliy Glagolev To: Christoph Hellwig Cc: linux-block@vger.kernel.org, "James E.J. Bottomley" , FUJITA Tomonori , Jens Axboe , linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCHv2] block: fix bsg_unregister and bsg_open race Message-ID: <20180614234733.GA38460@xldev-tmpl.dev.purestorage.com> References: <20180613221417.GA22778@xldev-tmpl.dev.purestorage.com> <20180614083806.GA18966@lst.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180614083806.GA18966@lst.de> User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Thanks, Christoph. Good catch releasing the mutex on failure path. V2 patch with your suggestions applied. From 7a8dc4b10f344d915658549581158f3da2cc1f91 Mon Sep 17 00:00:00 2001 From: Anatoliy Glagolev Date: Wed, 13 Jun 2018 15:38:51 -0600 Subject: [PATCHv2] Fix race of bsg_open and bsg_unregister Signed-Off-By: Anatoliy Glagolev --- block/bsg.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/block/bsg.c b/block/bsg.c index 132e657..66602c4 100644 --- a/block/bsg.c +++ b/block/bsg.c @@ -693,6 +693,8 @@ static struct bsg_device *bsg_add_device(struct inode *inode, struct bsg_device *bd; unsigned char buf[32]; + lockdep_assert_held(&bsg_mutex); + if (!blk_get_queue(rq)) return ERR_PTR(-ENXIO); @@ -707,14 +709,12 @@ static struct bsg_device *bsg_add_device(struct inode *inode, bsg_set_block(bd, file); atomic_set(&bd->ref_count, 1); - mutex_lock(&bsg_mutex); hlist_add_head(&bd->dev_list, bsg_dev_idx_hash(iminor(inode))); strncpy(bd->name, dev_name(rq->bsg_dev.class_dev), sizeof(bd->name) - 1); bsg_dbg(bd, "bound to <%s>, max queue %d\n", format_dev_t(buf, inode->i_rdev), bd->max_queue); - mutex_unlock(&bsg_mutex); return bd; } @@ -722,7 +722,7 @@ static struct bsg_device *__bsg_get_device(int minor, struct request_queue *q) { struct bsg_device *bd; - mutex_lock(&bsg_mutex); + lockdep_assert_held(&bsg_mutex); hlist_for_each_entry(bd, bsg_dev_idx_hash(minor), dev_list) { if (bd->queue == q) { @@ -732,7 +732,6 @@ static struct bsg_device *__bsg_get_device(int minor, struct request_queue *q) } bd = NULL; found: - mutex_unlock(&bsg_mutex); return bd; } @@ -746,17 +745,18 @@ static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file) */ mutex_lock(&bsg_mutex); bcd = idr_find(&bsg_minor_idr, iminor(inode)); - mutex_unlock(&bsg_mutex); - if (!bcd) - return ERR_PTR(-ENODEV); + if (!bcd) { + bd = ERR_PTR(-ENODEV); + goto out_unlock; + } bd = __bsg_get_device(iminor(inode), bcd->queue); - if (bd) - return bd; - - bd = bsg_add_device(inode, bcd->queue, file); + if (!bd) + bd = bsg_add_device(inode, bcd->queue, file); +out_unlock: + mutex_unlock(&bsg_mutex); return bd; } -- 1.9.1