From: Jianchao Wang <jianchao.w.wang@oracle.com>
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 [thread overview]
Message-ID: <1529391637-1704-1-git-send-email-jianchao.w.wang@oracle.com> (raw)
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 <jianchao.w.wang@oracle.com>
---
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
next reply other threads:[~2018-06-19 6:59 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-06-19 7:00 Jianchao Wang [this message]
2018-06-19 15:20 ` [PATCH] blk-mq: use mutex_trylock to avoid lock inversion Bart Van Assche
2018-06-20 2:09 ` jianchao.wang
2018-06-20 16:18 ` Bart Van Assche
2018-06-21 8:23 ` jianchao.wang
2018-06-26 3:46 ` jianchao.wang
2018-06-26 15:36 ` Bart Van Assche
2018-06-28 1:11 ` jianchao.wang
2018-06-28 1:15 ` jianchao.wang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1529391637-1704-1-git-send-email-jianchao.w.wang@oracle.com \
--to=jianchao.w.wang@oracle.com \
--cc=axboe@kernel.dk \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).