Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp4825745imm; Tue, 19 Jun 2018 00:02:08 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJfQK9mFl6y7FPk0j2s9utp2yGsMDsLMnGKq4a9AmDcr8UUT5KYdKOsNWco+6rpIBNkrmJK X-Received: by 2002:a62:c00e:: with SMTP id x14-v6mr16879755pff.67.1529391728532; Tue, 19 Jun 2018 00:02:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529391728; cv=none; d=google.com; s=arc-20160816; b=GdM/yu7fBqgSf14PXBFJX9eMD43GeaQ+zoFKqVBw2rJ6lvJN96VjfrKmqX2esA1fJv 09m10h1PNUu5wOWFJHqk+tDtPl11KR1MwBRoO6cuvfjunL6v9g2wI85nIPhBChzAWqwm M2jnTA8ZeKRzSf8ZCAzDOixttHmn//JJYZfcbl6AS6O9tlTsVeGotCQXUaggimaFrQsj aQ974azsDkud1KbrU+HcGEOUwRiMmKqfAkoUJcI3b/4aCetgpKGAo9TmEpdB9m2BeZIp 77EE0yTO/Bs7+7fBH++B3TCbGyfspGjr8oPvWL+WX64gDBuuDyeuimt3ywE8WKwzWGZS BXsw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=PfFA2rBNJvD28yGuA5Ef0100Jii92KvUUKnoSUrwqYY=; b=ZNLxcSxKLSiPeb3EInDbK2ZTFA0Eo8ooQs8l5s4/0n0Cr6KKzHzgu8QnI7RqVsyDtG +/t0OiD3Vp4iUj4DLY1lPknYEcB4k1N1y8JCezvUQywxH9GcV9GVKTIODXLocE4iU+fH 1akBgMpS2ylpoKgNXeeMR9l9Jc10YJSK3LjPGe56aYtCPGrnYOOG4FJIgkiEbsKrCBoN ICGg0pp4ho3YdJ1GjtF/+oxSNzzl5AdF1u85k9M1q7YYvgLrrNkS+qIICxnlUMFS1mbp NNnZnw96Zk5bEkWnwC5oY9+f0plvvXjeTSY51kO9tcN0dQ5CTLYJT9HDeAyqnSayz4In v7vw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2017-10-26 header.b=rFZwbdLQ; 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=NONE dis=NONE) header.from=oracle.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e7-v6si13622737pgp.386.2018.06.19.00.01.54; Tue, 19 Jun 2018 00:02:08 -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=@oracle.com header.s=corp-2017-10-26 header.b=rFZwbdLQ; 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=NONE dis=NONE) header.from=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S937327AbeFSG7a (ORCPT + 99 others); Tue, 19 Jun 2018 02:59:30 -0400 Received: from userp2130.oracle.com ([156.151.31.86]:48650 "EHLO userp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935908AbeFSG73 (ORCPT ); Tue, 19 Jun 2018 02:59:29 -0400 Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w5J6wvrk188768; Tue, 19 Jun 2018 06:59:27 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id; s=corp-2017-10-26; bh=PfFA2rBNJvD28yGuA5Ef0100Jii92KvUUKnoSUrwqYY=; b=rFZwbdLQzur42jRHL6EB1ZSevMJR2vVHjxirWiJSyEi+fjO4KUpVmJj85qDF5iFSsux4 PaHLsbl801NfmJpdWtNUELBfLEWcksqR4aitLwXEhuGmtkJUJrCcifo6IM13V7d0/fg6 e/ns8lgHzCBLn6KbRBnM9T4j4VYAgdv/UenFBc/FY6WUbauUmAVgficPQYpXib0nONsv EYAklJ2dWf76k64ZcZKj0X11XElhEJVpLEfx3wgQ4KoSNOrtgYkTOBgmTcwdArUndFrR f5o7Q2ii30jgxch09hYu4h24PhZmtdFUBSPT0AVQzWw9/OhKd0+++dnoVyoBX5Cf7JHg rQ== Received: from aserv0021.oracle.com (aserv0021.oracle.com [141.146.126.233]) by userp2130.oracle.com with ESMTP id 2jmt01etye-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 19 Jun 2018 06:59:27 +0000 Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by aserv0021.oracle.com (8.14.4/8.14.4) with ESMTP id w5J6xQoI019407 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 19 Jun 2018 06:59:26 GMT Received: from abhmp0016.oracle.com (abhmp0016.oracle.com [141.146.116.22]) by aserv0121.oracle.com (8.14.4/8.13.8) with ESMTP id w5J6xQm8003770; Tue, 19 Jun 2018 06:59:26 GMT Received: from will-ThinkCentre-M910s.cn.oracle.com (/10.182.70.254) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Mon, 18 Jun 2018 23:59:26 -0700 From: Jianchao Wang To: axboe@kernel.dk Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] blk-mq: use mutex_trylock to avoid lock inversion Date: Tue, 19 Jun 2018 15:00:37 +0800 Message-Id: <1529391637-1704-1-git-send-email-jianchao.w.wang@oracle.com> X-Mailer: git-send-email 2.7.4 X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=8928 signatures=668702 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=1 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=944 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1805220000 definitions=main-1806190081 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Currently, the kobject_del for kobjs of mq, hctx and ctx is invoked under sysfs_lock, lock inversion will come up when other one is acessing the associated sysfs file and trying to acquire the sysfs_lock. To fix it, use mutex_trylock in blk_mq_sysfs_ops and blk_mq_hw_sysfs_ops, if the lock in on contending, return -EAGAIN. Signed-off-by: Jianchao Wang --- block/blk-mq-sysfs.c | 52 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/block/blk-mq-sysfs.c b/block/blk-mq-sysfs.c index aafb442..210804f 100644 --- a/block/blk-mq-sysfs.c +++ b/block/blk-mq-sysfs.c @@ -53,11 +53,14 @@ static ssize_t blk_mq_sysfs_show(struct kobject *kobj, struct attribute *attr, if (!entry->show) return -EIO; - res = -ENOENT; - mutex_lock(&q->sysfs_lock); - if (!blk_queue_dying(q)) - res = entry->show(ctx, page); - mutex_unlock(&q->sysfs_lock); + res = -EAGAIN; + if (mutex_trylock(&q->sysfs_lock)) { + if (!blk_queue_dying(q)) + res = entry->show(ctx, page); + else + res = -ENOENT; + mutex_unlock(&q->sysfs_lock); + } return res; } @@ -76,11 +79,14 @@ static ssize_t blk_mq_sysfs_store(struct kobject *kobj, struct attribute *attr, if (!entry->store) return -EIO; - res = -ENOENT; - mutex_lock(&q->sysfs_lock); - if (!blk_queue_dying(q)) - res = entry->store(ctx, page, length); - mutex_unlock(&q->sysfs_lock); + res = -EAGAIN; + if (mutex_trylock(&q->sysfs_lock)) { + if (!blk_queue_dying(q)) + res = entry->store(ctx, page, length); + else + res = -ENOENT; + mutex_unlock(&q->sysfs_lock); + } return res; } @@ -99,11 +105,14 @@ static ssize_t blk_mq_hw_sysfs_show(struct kobject *kobj, if (!entry->show) return -EIO; - res = -ENOENT; - mutex_lock(&q->sysfs_lock); - if (!blk_queue_dying(q)) - res = entry->show(hctx, page); - mutex_unlock(&q->sysfs_lock); + res = -EAGAIN; + if (mutex_trylock(&q->sysfs_lock)) { + if (!blk_queue_dying(q)) + res = entry->show(hctx, page); + else + res = -ENOENT; + mutex_unlock(&q->sysfs_lock); + } return res; } @@ -123,11 +132,14 @@ static ssize_t blk_mq_hw_sysfs_store(struct kobject *kobj, if (!entry->store) return -EIO; - res = -ENOENT; - mutex_lock(&q->sysfs_lock); - if (!blk_queue_dying(q)) - res = entry->store(hctx, page, length); - mutex_unlock(&q->sysfs_lock); + res = -EAGAIN; + if (mutex_trylock(&q->sysfs_lock)) { + if (!blk_queue_dying(q)) + res = entry->store(hctx, page, length); + else + res = -ENOENT; + mutex_unlock(&q->sysfs_lock); + } return res; } -- 2.7.4