All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "John Garry" <john.g.garry@oracle.com>
Cc: linux-scsi@vger.kernel.org
Subject: Re: [PATCH v4 06/28] libmultipath: Add delayed removal support
Date: Thu, 23 Jul 2026 09:57:12 +0000	[thread overview]
Message-ID: <20260723095713.5BD361F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260723093627.2327456-7-john.g.garry@oracle.com>

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Concurrent path addition and delayed removal execution causes a use-after-free by destroying an active multipath head.
- [High] Missing synchronous cancellation of `remove_work` during `mpath_head` teardown leaks the delayed work.
- [Medium] Module reference count leak in `mpath_can_remove_head()` when called multiple times.
- [Medium] Passing a signed `int *` to `kstrtouint()` causes sign-extension and massively inflated timeouts.
--

--- Patch [6]: [PATCH v4 06/28] libmultipath: Add delayed removal support ---
commit e89d624119c3a1b6148c925347d05a0886a18e1d
Author: John Garry <john.garry@linux.dev>

libmultipath: Add delayed removal support

Add support for delayed removal, same as exists for NVMe. The purpose
of this feature is to keep the multipath disk and cdev present for
intermittent periods of no available path. A removal work callback is
supplied by the driver.

> diff --git a/include/linux/multipath.h b/include/linux/multipath.h
> index 6915c2a979642..44e26c16b3dc4 100644
> --- a/include/linux/multipath.h
> +++ b/include/linux/multipath.h

[ ... ]

> diff --git a/lib/multipath.c b/lib/multipath.c
> index 20c633ad7e1df..1fa4bd6677051 100644
> --- a/lib/multipath.c
> +++ b/lib/multipath.c
> @@ -65,6 +65,9 @@ int mpath_add_device(struct mpath_device *mpath_device,
>  	list_add_tail_rcu(&mpath_device->siblings, &mpath_head->dev_list);
>  	mutex_unlock(&mpath_head->lock);
>  
> +	if (cancel_delayed_work(&mpath_head->remove_work))
> +		module_put(mpath_head->drv_module);
> +

[Severity: High]
Could this lead to a use-after-free if the delayed work is already executing?

When mpath_add_device() calls cancel_delayed_work():

lib/multipath.c:mpath_add_device() {
    ...
    if (cancel_delayed_work(&mpath_head->remove_work))
        module_put(mpath_head->drv_module);
    return 0;
}

If the timer has already expired and mpath_remove_head_work() is actively
running, cancel_delayed_work() returns false and this function proceeds to
add the device and return success. 

Concurrently, mpath_remove_head_work() executes mpdt->remove_head(mpath_head)
without taking mpath_head->lock, which destroys the multipath head while we
are adding a new path to it.

>  	return 0;
>  }

[ ... ]

> @@ -480,6 +493,39 @@ static void mpath_requeue_work(struct work_struct *work)
>  	}
>  }
>  
> +bool mpath_can_remove_head(struct mpath_head *mpath_head)
> +{
> +	bool remove = false;
> +
> +	mutex_lock(&mpath_head->lock);
> +	/*
> +	 * Ensure that no one could remove this module while the head
> +	 * remove work is pending.
> +	 */
> +	if (mpath_head_queue_if_no_path(mpath_head) &&
> +		try_module_get(mpath_head->drv_module)) {
> +
> +		mod_delayed_work(mpath_wq, &mpath_head->remove_work,
> +				mpath_head->delayed_removal_secs * HZ);

[Severity: Medium]
Is there a module reference leak if this is called multiple times while the
timer is already pending?

If mpath_can_remove_head() is called multiple times in succession,
try_module_get() acquires a new reference each time. However, if the work
is already queued, mod_delayed_work() returns true but does not queue a
duplicate work item.

When the timer expires, mpath_remove_head_work() executes only once and calls
module_put() only once, leaking the additional references acquired during
the subsequent calls.

> +	} else {
> +		remove = true;
> +	}

[ ... ]

> @@ -524,6 +570,9 @@ int mpath_alloc_head_disk(struct mpath_head *mpath_head,
>  	mpath_head->disk->private_data = mpath_head;
>  	mpath_head->disk->fops = &mpath_ops;
>  
> +	INIT_DELAYED_WORK(&mpath_head->remove_work, mpath_remove_head_work);
> +	mpath_head->delayed_removal_secs = 0;
> +

[Severity: High]
Should there be a matching cancellation of remove_work during the teardown
path?

The delayed work is initialized here, but mpath_put_disk() only flushes
requeue_work and partition_scan_work before putting the disk. If the
multipath device is torn down while the delayed removal timeout is still
pending, does this leak the active timer and result in a use-after-free
when mpath_remove_head_work() eventually executes on the freed memory?

>  	set_bit(GD_SUPPRESS_PART_SCAN, &mpath_head->disk->state);
>  
>  	return 0;

[ ... ]

> @@ -567,6 +616,47 @@ void mpath_device_set_live(struct mpath_device *mpath_device)
>  }
>  EXPORT_SYMBOL_GPL(mpath_device_set_live);

[ ... ]

> +ssize_t mpath_delayed_removal_secs_store(struct mpath_head *mpath_head,
> +			const char *buf, size_t count)
> +{
> +	ssize_t ret;
> +	int sec;
> +
> +	ret = kstrtouint(buf, 0, &sec);

[Severity: Medium]
Does passing a signed integer to kstrtouint() cause sign-extension issues
for large values?

The variable sec is declared as a signed int, but kstrtouint() expects an
unsigned int pointer. If a user writes a value greater than or equal to
2147483648, kstrtouint() will parse it correctly, but it will be stored as
a negative value in the signed int.

When sec is later assigned to mpath_head->delayed_removal_secs (which is
an unsigned long), it will undergo sign extension on 64-bit platforms,
resulting in a massive positive timeout value.

> +	if (ret < 0)
> +		return ret;
> +
> +	mutex_lock(&mpath_head->lock);
> +	mpath_head->delayed_removal_secs = sec;

[ ... ]

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260723093627.2327456-1-john.g.garry@oracle.com?part=6

  reply	other threads:[~2026-07-23  9:57 UTC|newest]

Thread overview: 75+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-23  9:35 [PATCH v4 00/28] Native SCSI Multipath support John Garry
2026-07-23  9:35 ` [PATCH v4 01/28] libmultipath: Add initial framework John Garry
2026-07-23  9:50   ` sashiko-bot
2026-07-23 10:19     ` John Garry
2026-07-23  9:36 ` [PATCH v4 02/28] libmultipath: Add basic gendisk support John Garry
2026-07-23  9:57   ` sashiko-bot
2026-07-23 10:34     ` John Garry
2026-07-23  9:36 ` [PATCH v4 03/28] libmultipath: Add path selection support John Garry
2026-07-23  9:55   ` sashiko-bot
2026-07-23 10:28     ` John Garry
2026-07-23  9:36 ` [PATCH v4 04/28] libmultipath: Add bio handling John Garry
2026-07-23  9:36 ` [PATCH v4 05/28] libmultipath: Add support for mpath_device management John Garry
2026-07-23  9:58   ` sashiko-bot
2026-07-23 10:36     ` John Garry
2026-07-23  9:36 ` [PATCH v4 06/28] libmultipath: Add delayed removal support John Garry
2026-07-23  9:57   ` sashiko-bot [this message]
2026-07-23 10:33     ` John Garry
2026-07-23  9:36 ` [PATCH v4 07/28] libmultipath: Add sysfs helpers John Garry
2026-07-23 10:05   ` sashiko-bot
2026-07-23 10:37     ` John Garry
2026-07-23  9:36 ` [PATCH v4 08/28] libmultipath: Add mpath_bdev_report_zones() John Garry
2026-07-23 10:15   ` sashiko-bot
2026-07-23 10:39     ` John Garry
2026-07-23  9:36 ` [PATCH v4 09/28] libmultipath: Add support for block device IOCTL John Garry
2026-07-23 10:09   ` sashiko-bot
2026-07-23 10:38     ` John Garry
2026-07-23  9:36 ` [PATCH v4 10/28] libmultipath: Add mpath_bdev_getgeo() John Garry
2026-07-23  9:36 ` [PATCH v4 11/28] libmultipath: Add mpath_bdev_get_unique_id() John Garry
2026-07-23  9:36 ` [PATCH v4 12/28] scsi-multipath: introduce basic SCSI device support John Garry
2026-07-23 10:14   ` sashiko-bot
2026-07-23  9:36 ` [PATCH v4 13/28] scsi-multipath: introduce scsi_device head structure John Garry
2026-07-23 10:16   ` sashiko-bot
2026-07-23 10:47     ` John Garry
2026-07-23  9:36 ` [PATCH v4 14/28] scsi-multipath: provide sysfs link from to scsi_device John Garry
2026-07-23  9:36 ` [PATCH v4 15/28] scsi-multipath: support iopolicy John Garry
2026-07-23 10:20   ` sashiko-bot
2026-07-23 10:51     ` John Garry
2026-07-23  9:36 ` [PATCH v4 16/28] scsi-multipath: clone each bio John Garry
2026-07-23 10:27   ` sashiko-bot
2026-07-23 10:55     ` John Garry
2026-07-23  9:36 ` [PATCH v4 17/28] scsi-multipath: clear path when device is blocked John Garry
2026-07-23 10:33   ` sashiko-bot
2026-07-23 11:01     ` John Garry
2026-07-23  9:36 ` [PATCH v4 18/28] scsi-multipath: revalidate paths upon device unblock John Garry
2026-07-23 10:39   ` sashiko-bot
2026-07-23 11:15     ` John Garry
2026-07-23  9:36 ` [PATCH v4 19/28] scsi-multipath: failover handling John Garry
2026-07-23 10:36   ` sashiko-bot
2026-07-23 11:03     ` John Garry
2026-07-23  9:36 ` [PATCH v4 20/28] scsi-multipath: provide callbacks for path state John Garry
2026-07-23 10:36   ` sashiko-bot
2026-07-23 11:05     ` John Garry
2026-07-23  9:36 ` [PATCH v4 21/28] scsi-multipath: add scsi_mpath_{start,end}_request() John Garry
2026-07-23 10:32   ` sashiko-bot
2026-07-23 10:57     ` John Garry
2026-07-23  9:36 ` [PATCH v4 22/28] scsi-multipath: add delayed disk removal support John Garry
2026-07-23 10:39   ` sashiko-bot
2026-07-23 11:21     ` John Garry
2026-07-23  9:36 ` [PATCH v4 23/28] scsi: sd: add multipath disk class John Garry
2026-07-23 10:39   ` sashiko-bot
2026-07-23 11:21     ` John Garry
2026-07-23  9:36 ` [PATCH v4 24/28] scsi: sd: add multipath disk attr groups John Garry
2026-07-23 10:47   ` sashiko-bot
2026-07-23 11:22     ` John Garry
2026-07-23  9:36 ` [PATCH v4 25/28] scsi: sd: support multipath disk John Garry
2026-07-23 10:47   ` sashiko-bot
2026-07-23 11:27     ` John Garry
2026-07-23 16:52       ` John Garry
2026-07-23  9:36 ` [PATCH v4 26/28] scsi: sd: add mpath_dev file John Garry
2026-07-23 11:07   ` sashiko-bot
2026-07-23 11:30     ` John Garry
2026-07-23  9:36 ` [PATCH v4 27/28] scsi: sd: add mpath_numa_nodes dev attribute John Garry
2026-07-23 10:52   ` sashiko-bot
2026-07-23 11:30     ` John Garry
2026-07-23  9:36 ` [PATCH v4 28/28] scsi: sd: add mpath_queue_depth " John Garry

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=20260723095713.5BD361F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=john.g.garry@oracle.com \
    --cc=linux-scsi@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.