* [PATCH v3] vfio/mlx5: Read the resume buffer pointers under migf->lock
@ 2026-11-24 11:25 Qican Ma
2026-11-24 12:17 ` Yishai Hadas
2026-11-26 5:49 ` Alex Williamson
0 siblings, 2 replies; 3+ messages in thread
From: Qican Ma @ 2026-11-24 11:25 UTC (permalink / raw)
To: yishaih, alex; +Cc: jgg, skolothumtho, kevin.tian, kvm
mlx5vf_resume_write() reads migf->buf[0] and migf->buf_header[0]
before taking migf->lock. The migration file is opened with
stream_open(), so concurrent writes are not serialized by f_pos_lock.
A racing writer may free and reallocate these buffers in PREP_IMAGE or
PREP_HEADER_DATA, leaving the other writer with a freed pointer.
Read the buffer pointers under migf->lock, after the
MLX5_MIGF_STATE_ERROR check.
While at it, move the f_pos check under the lock as well, as f_pos is
protected by it. This is not a functional change, as f_pos is never
negative and can't get close to an overflow.
Found with an LLM-assisted code review tool and verified by inspection on
v7.4-rc4 (b6a9fa34ce4). Build-tested only due to lack of mlx5 VF
live-migration hardware.
Fixes: 34e2f27143d1 ("vfio/mlx5: Introduce multiple loads")
Cc: stable@vger.kernel.org
Signed-off-by: Qican Ma <goonsomeway@gmail.com>
---
v3: correct Fixes: tag to 34e2f27143d1 and rework the commit message per
Yishai's review; no code changes.
v2: resend via direct SMTP without line-wrapping corruption in the diff.
drivers/vfio/pci/mlx5/main.c | 17 +++++++++++------
1 file changed, 11 insertions(+), 6 deletions(-)
diff --git a/drivers/vfio/pci/mlx5/main.c b/drivers/vfio/pci/mlx5/main.c
index 18a6d224b1f..ebcc22bcb75 100644
--- a/drivers/vfio/pci/mlx5/main.c
+++ b/drivers/vfio/pci/mlx5/main.c
@@ -884,8 +884,8 @@ static ssize_t mlx5vf_resume_write(struct file *filp, const char __user *buf,
size_t len, loff_t *pos)
{
struct mlx5_vf_migration_file *migf = filp->private_data;
- struct mlx5_vhca_data_buffer *vhca_buf = migf->buf[0];
- struct mlx5_vhca_data_buffer *vhca_buf_header = migf->buf_header[0];
+ struct mlx5_vhca_data_buffer *vhca_buf;
+ struct mlx5_vhca_data_buffer *vhca_buf_header;
loff_t requested_length;
bool has_work = false;
ssize_t done = 0;
@@ -895,10 +895,6 @@ static ssize_t mlx5vf_resume_write(struct file *filp, const char __user *buf,
return -ESPIPE;
pos = &filp->f_pos;
- if (*pos < 0 ||
- check_add_overflow((loff_t)len, *pos, &requested_length))
- return -EINVAL;
-
mutex_lock(&migf->mvdev->state_mutex);
mutex_lock(&migf->lock);
if (migf->state == MLX5_MIGF_STATE_ERROR) {
@@ -906,6 +902,15 @@ static ssize_t mlx5vf_resume_write(struct file *filp, const char __user *buf,
goto out_unlock;
}
+ if (*pos < 0 ||
+ check_add_overflow((loff_t)len, *pos, &requested_length)) {
+ done = -EINVAL;
+ goto out_unlock;
+ }
+
+ vhca_buf = migf->buf[0];
+ vhca_buf_header = migf->buf_header[0];
+
while (len || has_work) {
has_work = false;
switch (migf->load_state) {
--
2.50.1 (Apple Git-155)
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v3] vfio/mlx5: Read the resume buffer pointers under migf->lock
2026-11-24 11:25 [PATCH v3] vfio/mlx5: Read the resume buffer pointers under migf->lock Qican Ma
@ 2026-11-24 12:17 ` Yishai Hadas
2026-11-26 5:49 ` Alex Williamson
1 sibling, 0 replies; 3+ messages in thread
From: Yishai Hadas @ 2026-11-24 12:17 UTC (permalink / raw)
To: Qican Ma, alex; +Cc: jgg, skolothumtho, kevin.tian, kvm
On 24/11/2026 13:25, Qican Ma wrote:
> mlx5vf_resume_write() reads migf->buf[0] and migf->buf_header[0]
> before taking migf->lock. The migration file is opened with
> stream_open(), so concurrent writes are not serialized by f_pos_lock.
> A racing writer may free and reallocate these buffers in PREP_IMAGE or
> PREP_HEADER_DATA, leaving the other writer with a freed pointer.
>
> Read the buffer pointers under migf->lock, after the
> MLX5_MIGF_STATE_ERROR check.
>
> While at it, move the f_pos check under the lock as well, as f_pos is
> protected by it. This is not a functional change, as f_pos is never
> negative and can't get close to an overflow.
>
> Found with an LLM-assisted code review tool and verified by inspection on
> v7.4-rc4 (b6a9fa34ce4). Build-tested only due to lack of mlx5 VF
> live-migration hardware.
>
> Fixes: 34e2f27143d1 ("vfio/mlx5: Introduce multiple loads")
> Cc: stable@vger.kernel.org
> Signed-off-by: Qican Ma <goonsomeway@gmail.com>
Reviewed-by: Yishai Hadas <yishaih@nvidia.com>
Yishai
> ---
> v3: correct Fixes: tag to 34e2f27143d1 and rework the commit message per
> Yishai's review; no code changes.
> v2: resend via direct SMTP without line-wrapping corruption in the diff.
>
> drivers/vfio/pci/mlx5/main.c | 17 +++++++++++------
> 1 file changed, 11 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/vfio/pci/mlx5/main.c b/drivers/vfio/pci/mlx5/main.c
> index 18a6d224b1f..ebcc22bcb75 100644
> --- a/drivers/vfio/pci/mlx5/main.c
> +++ b/drivers/vfio/pci/mlx5/main.c
> @@ -884,8 +884,8 @@ static ssize_t mlx5vf_resume_write(struct file *filp, const char __user *buf,
> size_t len, loff_t *pos)
> {
> struct mlx5_vf_migration_file *migf = filp->private_data;
> - struct mlx5_vhca_data_buffer *vhca_buf = migf->buf[0];
> - struct mlx5_vhca_data_buffer *vhca_buf_header = migf->buf_header[0];
> + struct mlx5_vhca_data_buffer *vhca_buf;
> + struct mlx5_vhca_data_buffer *vhca_buf_header;
> loff_t requested_length;
> bool has_work = false;
> ssize_t done = 0;
> @@ -895,10 +895,6 @@ static ssize_t mlx5vf_resume_write(struct file *filp, const char __user *buf,
> return -ESPIPE;
> pos = &filp->f_pos;
>
> - if (*pos < 0 ||
> - check_add_overflow((loff_t)len, *pos, &requested_length))
> - return -EINVAL;
> -
> mutex_lock(&migf->mvdev->state_mutex);
> mutex_lock(&migf->lock);
> if (migf->state == MLX5_MIGF_STATE_ERROR) {
> @@ -906,6 +902,15 @@ static ssize_t mlx5vf_resume_write(struct file *filp, const char __user *buf,
> goto out_unlock;
> }
>
> + if (*pos < 0 ||
> + check_add_overflow((loff_t)len, *pos, &requested_length)) {
> + done = -EINVAL;
> + goto out_unlock;
> + }
> +
> + vhca_buf = migf->buf[0];
> + vhca_buf_header = migf->buf_header[0];
> +
> while (len || has_work) {
> has_work = false;
> switch (migf->load_state) {
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v3] vfio/mlx5: Read the resume buffer pointers under migf->lock
2026-11-24 11:25 [PATCH v3] vfio/mlx5: Read the resume buffer pointers under migf->lock Qican Ma
2026-11-24 12:17 ` Yishai Hadas
@ 2026-11-26 5:49 ` Alex Williamson
1 sibling, 0 replies; 3+ messages in thread
From: Alex Williamson @ 2026-11-26 5:49 UTC (permalink / raw)
To: Qican Ma; +Cc: yishaih, jgg, skolothumtho, kevin.tian, kvm, alex
On Tue, 24 Nov 2026 19:25:59 +0800
Qican Ma <goonsomeway@gmail.com> wrote:
> mlx5vf_resume_write() reads migf->buf[0] and migf->buf_header[0]
> before taking migf->lock. The migration file is opened with
> stream_open(), so concurrent writes are not serialized by f_pos_lock.
> A racing writer may free and reallocate these buffers in PREP_IMAGE or
> PREP_HEADER_DATA, leaving the other writer with a freed pointer.
>
> Read the buffer pointers under migf->lock, after the
> MLX5_MIGF_STATE_ERROR check.
>
> While at it, move the f_pos check under the lock as well, as f_pos is
> protected by it. This is not a functional change, as f_pos is never
> negative and can't get close to an overflow.
>
> Found with an LLM-assisted code review tool and verified by inspection on
> v7.4-rc4 (b6a9fa34ce4). Build-tested only due to lack of mlx5 VF
> live-migration hardware.
>
> Fixes: 34e2f27143d1 ("vfio/mlx5: Introduce multiple loads")
> Cc: stable@vger.kernel.org
> Signed-off-by: Qican Ma <goonsomeway@gmail.com>
> ---
> v3: correct Fixes: tag to 34e2f27143d1 and rework the commit message per
> Yishai's review; no code changes.
> v2: resend via direct SMTP without line-wrapping corruption in the diff.
>
> drivers/vfio/pci/mlx5/main.c | 17 +++++++++++------
> 1 file changed, 11 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/vfio/pci/mlx5/main.c b/drivers/vfio/pci/mlx5/main.c
> index 18a6d224b1f..ebcc22bcb75 100644
> --- a/drivers/vfio/pci/mlx5/main.c
> +++ b/drivers/vfio/pci/mlx5/main.c
> @@ -884,8 +884,8 @@ static ssize_t mlx5vf_resume_write(struct file *filp, const char __user *buf,
> size_t len, loff_t *pos)
> {
> struct mlx5_vf_migration_file *migf = filp->private_data;
> - struct mlx5_vhca_data_buffer *vhca_buf = migf->buf[0];
> - struct mlx5_vhca_data_buffer *vhca_buf_header = migf->buf_header[0];
> + struct mlx5_vhca_data_buffer *vhca_buf;
> + struct mlx5_vhca_data_buffer *vhca_buf_header;
> loff_t requested_length;
> bool has_work = false;
> ssize_t done = 0;
> @@ -895,10 +895,6 @@ static ssize_t mlx5vf_resume_write(struct file *filp, const char __user *buf,
> return -ESPIPE;
> pos = &filp->f_pos;
>
> - if (*pos < 0 ||
> - check_add_overflow((loff_t)len, *pos, &requested_length))
> - return -EINVAL;
> -
> mutex_lock(&migf->mvdev->state_mutex);
> mutex_lock(&migf->lock);
> if (migf->state == MLX5_MIGF_STATE_ERROR) {
> @@ -906,6 +902,15 @@ static ssize_t mlx5vf_resume_write(struct file *filp, const char __user *buf,
> goto out_unlock;
> }
>
> + if (*pos < 0 ||
> + check_add_overflow((loff_t)len, *pos, &requested_length)) {
> + done = -EINVAL;
> + goto out_unlock;
> + }
> +
> + vhca_buf = migf->buf[0];
> + vhca_buf_header = migf->buf_header[0];
> +
> while (len || has_work) {
> has_work = false;
> switch (migf->load_state) {
Applied to vfio next branch for v7.5. Thanks,
Alex
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-26 5:49 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 11:25 [PATCH v3] vfio/mlx5: Read the resume buffer pointers under migf->lock Qican Ma
2026-11-24 12:17 ` Yishai Hadas
2026-11-26 5:49 ` Alex Williamson
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.