Received: by 10.223.185.116 with SMTP id b49csp817415wrg; Sat, 10 Feb 2018 21:03:30 -0800 (PST) X-Google-Smtp-Source: AH8x226JW0fpQhGsOQBxisGDtFWAMq0+macO2LP9h86Szm1XoDJL/ekW0/DnhBJU/JsTLIBU/2Kg X-Received: by 2002:a17:902:2845:: with SMTP id e63-v6mr7111891plb.438.1518325410785; Sat, 10 Feb 2018 21:03:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518325410; cv=none; d=google.com; s=arc-20160816; b=MQRCtSvsziQGtvDuC+Cewz+/LVUl9awfL52mpfDjBczuQ5dyVrJygIKwkbDxfHidZQ AgWqm0Bx72NXEwJLjBu0JyUZvIirRq5jyr4HoPt6swnSXO5VpPXp1zuvQff/dCWbyDN6 uZHAidW+5/RP2kyE9z4ZNic/khy1lOweQtYH4PVCsKD5BYSBGFv4dbnm7sMWPFLE8lcA FqSG0di8ujS2Jtr2sH6BqAGXE06fV1A1dF74n6ewvBJ068ourwNXuM12uv765t3pvmQD 7rbq12SLxPadoltxKiD8pGk5D3ot5jV1xLaIdgm5ev80GToxCGv7/5UJft3VP3f0Xynm 1nJw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition :arc-authentication-results; bh=1GdlTN34VTm5RlQyxmRHgSNfP+1eIYXtOaIS1ilGyV0=; b=UzZhXOpyVeTDE7PKLmAsO9bTOhOqCfmeZSAzxhsKxVBq9aAltbb6JpQx5pe7WLfmuX mqD3PzgVoZ9ZZpiFBVaQVsXeFcXvy1Ebk3B4u/n+vJoLP+isM2Nd5gTGmjIGOj+W6GGA cqnW1jv6aL3bOo6OwmVauxwGjbBK3ixidtSzHMK/D5yHdP+kGpVUrnwtne22YHR4s2Is 46Jm1gEFnR2Mzp1DmpQSX+tGc3ZjnNv0TNKIUyQ7pc2nr5bJa3+0BG85CFPQeZG5232Q 3STGZTu4cxw/a3yErgZUDi6okZh5xBgIIh19ffAsh8a7JY0L6ifs1NSfPV8k/lQezATj jKng== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f4si689264pgn.334.2018.02.10.21.03.17; Sat, 10 Feb 2018 21:03:30 -0800 (PST) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753661AbeBKFCO (ORCPT + 99 others); Sun, 11 Feb 2018 00:02:14 -0500 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:41613 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752751AbeBKEdq (ORCPT ); Sat, 10 Feb 2018 23:33:46 -0500 Received: from [2a02:8011:400e:2:6f00:88c8:c921:d332] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ekjKe-0002hC-MS; Sun, 11 Feb 2018 04:33:40 +0000 Received: from ben by deadeye with local (Exim 4.90) (envelope-from ) id 1ekjKX-0004UF-UW; Sun, 11 Feb 2018 04:33:33 +0000 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Christoph Hellwig" , "Waiman Long" , "Steven Rostedt (VMware)" , "Jens Axboe" Date: Sun, 11 Feb 2018 04:20:06 +0000 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) Subject: [PATCH 3.2 35/79] blktrace: Fix potential deadlock between delete & sysfs ops In-Reply-To: X-SA-Exim-Connect-IP: 2a02:8011:400e:2:6f00:88c8:c921:d332 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.2.99-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Waiman Long commit 5acb3cc2c2e9d3020a4fee43763c6463767f1572 upstream. The lockdep code had reported the following unsafe locking scenario: CPU0 CPU1 ---- ---- lock(s_active#228); lock(&bdev->bd_mutex/1); lock(s_active#228); lock(&bdev->bd_mutex); *** DEADLOCK *** The deadlock may happen when one task (CPU1) is trying to delete a partition in a block device and another task (CPU0) is accessing tracing sysfs file (e.g. /sys/block/dm-1/trace/act_mask) in that partition. The s_active isn't an actual lock. It is a reference count (kn->count) on the sysfs (kernfs) file. Removal of a sysfs file, however, require a wait until all the references are gone. The reference count is treated like a rwsem using lockdep instrumentation code. The fact that a thread is in the sysfs callback method or in the ioctl call means there is a reference to the opended sysfs or device file. That should prevent the underlying block structure from being removed. Instead of using bd_mutex in the block_device structure, a new blk_trace_mutex is now added to the request_queue structure to protect access to the blk_trace structure. Suggested-by: Christoph Hellwig Signed-off-by: Waiman Long Acked-by: Steven Rostedt (VMware) Fix typo in patch subject line, and prune a comment detailing how the code used to work. Signed-off-by: Jens Axboe Signed-off-by: Ben Hutchings --- block/blk-core.c | 3 +++ include/linux/blkdev.h | 1 + kernel/trace/blktrace.c | 18 ++++++++++++------ 3 files changed, 16 insertions(+), 6 deletions(-) --- a/block/blk-core.c +++ b/block/blk-core.c @@ -499,6 +499,9 @@ struct request_queue *blk_alloc_queue_no kobject_init(&q->kobj, &blk_queue_ktype); +#ifdef CONFIG_BLK_DEV_IO_TRACE + mutex_init(&q->blk_trace_mutex); +#endif mutex_init(&q->sysfs_lock); spin_lock_init(&q->__queue_lock); --- a/include/linux/blkdev.h +++ b/include/linux/blkdev.h @@ -361,6 +361,7 @@ struct request_queue { int node; #ifdef CONFIG_BLK_DEV_IO_TRACE struct blk_trace *blk_trace; + struct mutex blk_trace_mutex; #endif /* * for flush operations --- a/kernel/trace/blktrace.c +++ b/kernel/trace/blktrace.c @@ -631,6 +631,12 @@ int blk_trace_startstop(struct request_q } EXPORT_SYMBOL_GPL(blk_trace_startstop); +/* + * When reading or writing the blktrace sysfs files, the references to the + * opened sysfs or device files should prevent the underlying block device + * from being removed. So no further delete protection is really needed. + */ + /** * blk_trace_ioctl: - handle the ioctls associated with tracing * @bdev: the block device @@ -648,7 +654,7 @@ int blk_trace_ioctl(struct block_device if (!q) return -ENXIO; - mutex_lock(&bdev->bd_mutex); + mutex_lock(&q->blk_trace_mutex); switch (cmd) { case BLKTRACESETUP: @@ -674,7 +680,7 @@ int blk_trace_ioctl(struct block_device break; } - mutex_unlock(&bdev->bd_mutex); + mutex_unlock(&q->blk_trace_mutex); return ret; } @@ -1660,7 +1666,7 @@ static ssize_t sysfs_blk_trace_attr_show if (q == NULL) goto out_bdput; - mutex_lock(&bdev->bd_mutex); + mutex_lock(&q->blk_trace_mutex); if (attr == &dev_attr_enable) { ret = sprintf(buf, "%u\n", !!q->blk_trace); @@ -1679,7 +1685,7 @@ static ssize_t sysfs_blk_trace_attr_show ret = sprintf(buf, "%llu\n", q->blk_trace->end_lba); out_unlock_bdev: - mutex_unlock(&bdev->bd_mutex); + mutex_unlock(&q->blk_trace_mutex); out_bdput: bdput(bdev); out: @@ -1721,7 +1727,7 @@ static ssize_t sysfs_blk_trace_attr_stor if (q == NULL) goto out_bdput; - mutex_lock(&bdev->bd_mutex); + mutex_lock(&q->blk_trace_mutex); if (attr == &dev_attr_enable) { if (value) @@ -1747,7 +1753,7 @@ static ssize_t sysfs_blk_trace_attr_stor } out_unlock_bdev: - mutex_unlock(&bdev->bd_mutex); + mutex_unlock(&q->blk_trace_mutex); out_bdput: bdput(bdev); out: