Skip to content

Commit 48145b6

Browse files
minwooimChristoph Hellwig
authored andcommitted
nvme: fix controller ioctl through ns_head
In multipath case, we should consider namespace attachment with controllers in a subsystem when we find out the live controller for the namespace. This patch manually reverted the commit 3557a44 ("nvme: don't bother to look up a namespace for controller ioctls") with few more updates to nvme_ns_head_chr_ioctl which has been newly updated. Fixes: 3557a44 ("nvme: don't bother to look up a namespace for controller ioctls") Cc: Christoph Hellwig <hch@lst.de> Signed-off-by: Minwoo Im <minwoo.im.dev@gmail.com> Signed-off-by: Christoph Hellwig <hch@lst.de>
1 parent cd2c754 commit 48145b6

3 files changed

Lines changed: 41 additions & 47 deletions

File tree

drivers/nvme/host/core.c

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,28 +1999,6 @@ static const struct block_device_operations nvme_bdev_ops = {
19991999
.pr_ops = &nvme_pr_ops,
20002000
};
20012001

2002-
#ifdef CONFIG_NVME_MULTIPATH
2003-
struct nvme_ctrl *nvme_find_get_live_ctrl(struct nvme_subsystem *subsys)
2004-
{
2005-
struct nvme_ctrl *ctrl;
2006-
int ret;
2007-
2008-
ret = mutex_lock_killable(&nvme_subsystems_lock);
2009-
if (ret)
2010-
return ERR_PTR(ret);
2011-
list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
2012-
if (ctrl->state == NVME_CTRL_LIVE)
2013-
goto found;
2014-
}
2015-
mutex_unlock(&nvme_subsystems_lock);
2016-
return ERR_PTR(-EWOULDBLOCK);
2017-
found:
2018-
nvme_get_ctrl(ctrl);
2019-
mutex_unlock(&nvme_subsystems_lock);
2020-
return ctrl;
2021-
}
2022-
#endif /* CONFIG_NVME_MULTIPATH */
2023-
20242002
static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
20252003
{
20262004
unsigned long timeout =

drivers/nvme/host/ioctl.c

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -370,41 +370,45 @@ long nvme_ns_chr_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
370370
}
371371

372372
#ifdef CONFIG_NVME_MULTIPATH
373-
static int nvme_ns_head_ctrl_ioctl(struct nvme_ns_head *head,
374-
unsigned int cmd, void __user *argp)
373+
static int nvme_ns_head_ctrl_ioctl(struct nvme_ns *ns, unsigned int cmd,
374+
void __user *argp, struct nvme_ns_head *head, int srcu_idx)
375375
{
376-
struct nvme_ctrl *ctrl = nvme_find_get_live_ctrl(head->subsys);
376+
struct nvme_ctrl *ctrl = ns->ctrl;
377377
int ret;
378378

379-
if (IS_ERR(ctrl))
380-
return PTR_ERR(ctrl);
381-
ret = nvme_ctrl_ioctl(ctrl, cmd, argp);
382-
nvme_put_ctrl(ctrl);
383-
return ret;
384-
}
379+
nvme_get_ctrl(ns->ctrl);
380+
nvme_put_ns_from_disk(head, srcu_idx);
381+
ret = nvme_ctrl_ioctl(ns->ctrl, cmd, argp);
385382

386-
static int nvme_ns_head_ns_ioctl(struct nvme_ns_head *head,
387-
unsigned int cmd, void __user *argp)
388-
{
389-
int srcu_idx = srcu_read_lock(&head->srcu);
390-
struct nvme_ns *ns = nvme_find_path(head);
391-
int ret = -EWOULDBLOCK;
392-
393-
if (ns)
394-
ret = nvme_ns_ioctl(ns, cmd, argp);
395-
srcu_read_unlock(&head->srcu, srcu_idx);
383+
nvme_put_ctrl(ctrl);
396384
return ret;
397385
}
398386

399387
int nvme_ns_head_ioctl(struct block_device *bdev, fmode_t mode,
400388
unsigned int cmd, unsigned long arg)
401389
{
402-
struct nvme_ns_head *head = bdev->bd_disk->private_data;
390+
struct nvme_ns_head *head = NULL;
403391
void __user *argp = (void __user *)arg;
392+
struct nvme_ns *ns;
393+
int srcu_idx, ret;
394+
395+
ns = nvme_get_ns_from_disk(bdev->bd_disk, &head, &srcu_idx);
396+
if (unlikely(!ns))
397+
return -EWOULDBLOCK;
404398

399+
/*
400+
* Handle ioctls that apply to the controller instead of the namespace
401+
* seperately and drop the ns SRCU reference early. This avoids a
402+
* deadlock when deleting namespaces using the passthrough interface.
403+
*/
405404
if (is_ctrl_ioctl(cmd))
406-
return nvme_ns_head_ctrl_ioctl(head, cmd, argp);
407-
return nvme_ns_head_ns_ioctl(head, cmd, argp);
405+
ret = nvme_ns_head_ctrl_ioctl(ns, cmd, argp, head, srcu_idx);
406+
else {
407+
ret = nvme_ns_ioctl(ns, cmd, argp);
408+
nvme_put_ns_from_disk(head, srcu_idx);
409+
}
410+
411+
return ret;
408412
}
409413

410414
long nvme_ns_head_chr_ioctl(struct file *file, unsigned int cmd,
@@ -414,10 +418,23 @@ long nvme_ns_head_chr_ioctl(struct file *file, unsigned int cmd,
414418
struct nvme_ns_head *head =
415419
container_of(cdev, struct nvme_ns_head, cdev);
416420
void __user *argp = (void __user *)arg;
421+
struct nvme_ns *ns;
422+
int srcu_idx, ret;
423+
424+
srcu_idx = srcu_read_lock(&head->srcu);
425+
ns = nvme_find_path(head);
426+
if (!ns) {
427+
srcu_read_unlock(&head->srcu, srcu_idx);
428+
return -EWOULDBLOCK;
429+
}
417430

418431
if (is_ctrl_ioctl(cmd))
419-
return nvme_ns_head_ctrl_ioctl(head, cmd, argp);
420-
return nvme_ns_head_ns_ioctl(head, cmd, argp);
432+
return nvme_ns_head_ctrl_ioctl(ns, cmd, argp, head, srcu_idx);
433+
434+
ret = nvme_ns_ioctl(ns, cmd, argp);
435+
nvme_put_ns_from_disk(head, srcu_idx);
436+
437+
return ret;
421438
}
422439
#endif /* CONFIG_NVME_MULTIPATH */
423440

drivers/nvme/host/nvme.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,6 @@ struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk,
664664
void nvme_put_ns_from_disk(struct nvme_ns_head *head, int idx);
665665
bool nvme_tryget_ns_head(struct nvme_ns_head *head);
666666
void nvme_put_ns_head(struct nvme_ns_head *head);
667-
struct nvme_ctrl *nvme_find_get_live_ctrl(struct nvme_subsystem *subsys);
668667
int nvme_cdev_add(struct cdev *cdev, struct device *cdev_device,
669668
const struct file_operations *fops, struct module *owner);
670669
void nvme_cdev_del(struct cdev *cdev, struct device *cdev_device);

0 commit comments

Comments
 (0)