[PATCH 375/437] drivers/vfio: convert to read/write iterators

Jens Axboe
2024-04-11T15:42:12Z

.../vfio/pci/hisilicon/hisi_acc_vfio_pci.c | 31 ++++++++++--------
drivers/vfio/pci/mlx5/main.c | 6 ++--
drivers/vfio/pci/pds/lm.c | 32 +++++++++----------
drivers/vfio/vfio_main.c | 6 ++--
4 files changed, 41 insertions(+), 34 deletions(-)

diff --git a/drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c b/drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c
index 9a3e97108ace..8ea4c46a3334 100644
--- a/drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c
+++ b/drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c
@@ -676,18 +676,20 @@ static int hisi_acc_vf_release_file(struct inode *inode, struct file *filp)
return 0;
}

-static ssize_t hisi_acc_vf_resume_write(struct file *filp, const char __user *buf,
- size_t len, loff_t *pos)
+static ssize_t hisi_acc_vf_resume_write(struct kiocb *iocb,
+ struct iov_iter *from)
{
- struct hisi_acc_vf_migration_file *migf = filp->private_data;
+ struct hisi_acc_vf_migration_file *migf = iocb->ki_filp->private_data;
u8 *vf_data = (u8 *)&migf->vf_data;
+ size_t len = iov_iter_count(from);
loff_t requested_length;
ssize_t done = 0;
+ loff_t *pos;
int ret;

- if (pos)
+ if (iocb->ki_pos)
return -ESPIPE;
- pos = &filp->f_pos;
+ pos = &iocb->ki_filp->f_pos;

if (*pos < 0 ||
check_add_overflow((loff_t)len, *pos, &requested_length))
@@ -702,7 +704,7 @@ static ssize_t hisi_acc_vf_resume_write(struct file *filp, const char __user *bu
goto out_unlock;
}

- ret = copy_from_user(vf_data + *pos, buf, len);
+ ret = !copy_from_iter_full(vf_data + *pos, len, from);
if (ret) {
done = -EFAULT;
goto out_unlock;
@@ -721,7 +723,7 @@ static ssize_t hisi_acc_vf_resume_write(struct file *filp, const char __user *bu

static const struct file_operations hisi_acc_vf_resume_fops = {
.owner = THIS_MODULE,
- .write = hisi_acc_vf_resume_write,
+ .write_iter = hisi_acc_vf_resume_write,
.release = hisi_acc_vf_release_file,
.llseek = no_llseek,
};
@@ -800,16 +802,17 @@ static long hisi_acc_vf_precopy_ioctl(struct file *filp,
return ret;
}

-static ssize_t hisi_acc_vf_save_read(struct file *filp, char __user *buf, size_t len,
- loff_t *pos)
+static ssize_t hisi_acc_vf_save_read(struct kiocb *iocb, struct iov_iter *to)
{
- struct hisi_acc_vf_migration_file *migf = filp->private_data;
+ struct hisi_acc_vf_migration_file *migf = iocb->ki_filp->private_data;
+ size_t len = iov_iter_count(to);
ssize_t done = 0;
+ loff_t *pos;
int ret;

- if (pos)
+ if (iocb->ki_pos)
return -ESPIPE;
- pos = &filp->f_pos;
+ pos = &iocb->ki_filp->f_pos;

mutex_lock(&migf->lock);
if (*pos > migf->total_length) {
@@ -826,7 +829,7 @@ static ssize_t hisi_acc_vf_save_read(struct file *filp, char __user *buf, size_t
if (len) {
u8 *vf_data = (u8 *)&migf->vf_data;

- ret = copy_to_user(buf, vf_data + *pos, len);
+ ret = !copy_to_iter_full(vf_data + *pos, len, to);
if (ret) {
done = -EFAULT;
goto out_unlock;
@@ -841,7 +844,7 @@ static ssize_t hisi_acc_vf_save_read(struct file *filp, char __user *buf, size_t

static const struct file_operations hisi_acc_vf_save_fops = {
.owner = THIS_MODULE,
- .read = hisi_acc_vf_save_read,
+ .read_iter = hisi_acc_vf_save_read,
.unlocked_ioctl = hisi_acc_vf_precopy_ioctl,
.compat_ioctl = compat_ptr_ioctl,
.release = hisi_acc_vf_release_file,
diff --git a/drivers/vfio/pci/mlx5/main.c b/drivers/vfio/pci/mlx5/main.c
index 61d9b0f9146d..05552bcb7a31 100644
--- a/drivers/vfio/pci/mlx5/main.c
+++ b/drivers/vfio/pci/mlx5/main.c
@@ -263,6 +263,7 @@ static ssize_t mlx5vf_save_read(struct file *filp, char __user *buf, size_t len,
mutex_unlock(&migf->lock);
return done;
}
+FOPS_READ_ITER_HELPER(mlx5vf_save_read);

static __poll_t mlx5vf_save_poll(struct file *filp,
struct poll_table_struct *wait)
@@ -582,7 +583,7 @@ static long mlx5vf_precopy_ioctl(struct file *filp, unsigned int cmd,

static const struct file_operations mlx5vf_save_fops = {
.owner = THIS_MODULE,
- .read = mlx5vf_save_read,
+ .read_iter = mlx5vf_save_read_iter,
.poll = mlx5vf_save_poll,
.unlocked_ioctl = mlx5vf_precopy_ioctl,
.compat_ioctl = compat_ptr_ioctl,
@@ -995,10 +996,11 @@ static ssize_t mlx5vf_resume_write(struct file *filp, const char __user *buf,
mlx5vf_state_mutex_unlock(migf->mvdev);
return ret ? ret : done;
}
+FOPS_WRITE_ITER_HELPER(mlx5vf_resume_write);

static const struct file_operations mlx5vf_resume_fops = {
.owner = THIS_MODULE,
- .write = mlx5vf_resume_write,
+ .write_iter = mlx5vf_resume_write_iter,
.release = mlx5vf_release_file,
.llseek = no_llseek,
};
diff --git a/drivers/vfio/pci/pds/lm.c b/drivers/vfio/pci/pds/lm.c
index 6b94cc0bf45b..8e86b3c39a28 100644
--- a/drivers/vfio/pci/pds/lm.c
+++ b/drivers/vfio/pci/pds/lm.c
@@ -174,15 +174,16 @@ static int pds_vfio_release_file(struct inode *inode, struct file *filp)
return 0;
}

-static ssize_t pds_vfio_save_read(struct file *filp, char __user *buf,
- size_t len, loff_t *pos)
+static ssize_t pds_vfio_save_read(struct kiocb *iocb, struct iov_iter *to)
{
- struct pds_vfio_lm_file *lm_file = filp->private_data;
+ struct pds_vfio_lm_file *lm_file = iocb->ki_filp->private_data;
+ size_t len = iov_iter_count(to);
ssize_t done = 0;
+ loff_t *pos;

- if (pos)
+ if (iocb->ki_pos)
return -ESPIPE;
- pos = &filp->f_pos;
+ pos = &iocb->ki_filp->f_pos;

mutex_lock(&lm_file->lock);

@@ -214,7 +215,7 @@ static ssize_t pds_vfio_save_read(struct file *filp, char __user *buf,

page_len = min_t(size_t, len, PAGE_SIZE - page_offset);
from_buff = kmap_local_page(page);
- err = copy_to_user(buf, from_buff + page_offset, page_len);
+ err = !copy_to_iter_full(from_buff + page_offset, page_len, to);
kunmap_local(from_buff);
if (err) {
done = -EFAULT;
@@ -223,7 +224,6 @@ static ssize_t pds_vfio_save_read(struct file *filp, char __user *buf,
*pos += page_len;
len -= page_len;
done += page_len;
- buf += page_len;
}

out_unlock:
@@ -233,7 +233,7 @@ static ssize_t pds_vfio_save_read(struct file *filp, char __user *buf,

static const struct file_operations pds_vfio_save_fops = {
.owner = THIS_MODULE,
- .read = pds_vfio_save_read,
+ .read_iter = pds_vfio_save_read,
.release = pds_vfio_release_file,
.llseek = no_llseek,
};
@@ -273,17 +273,18 @@ static int pds_vfio_get_save_file(struct pds_vfio_pci_device *pds_vfio)
return 0;
}

-static ssize_t pds_vfio_restore_write(struct file *filp, const char __user *buf,
- size_t len, loff_t *pos)
+static ssize_t pds_vfio_restore_write(struct kiocb *iocb, struct iov_iter *from)
{
- struct pds_vfio_lm_file *lm_file = filp->private_data;
+ struct pds_vfio_lm_file *lm_file = iocb->ki_filp->private_data;
+ size_t len = iov_iter_count(from);
loff_t requested_length;
ssize_t done = 0;
+ loff_t *pos;

- if (pos)
+ if (iocb->ki_pos)
return -ESPIPE;

- pos = &filp->f_pos;
+ pos = &iocb->ki_filp->f_pos;

if (*pos < 0 ||
check_add_overflow((loff_t)len, *pos, &requested_length))
@@ -313,7 +314,7 @@ static ssize_t pds_vfio_restore_write(struct file *filp, const char __user *buf,

page_len = min_t(size_t, len, PAGE_SIZE - page_offset);
to_buff = kmap_local_page(page);
- err = copy_from_user(to_buff + page_offset, buf, page_len);
+ err = !copy_from_iter_full(to_buff + page_offset, page_len, from);
kunmap_local(to_buff);
if (err) {
done = -EFAULT;
@@ -322,7 +323,6 @@ static ssize_t pds_vfio_restore_write(struct file *filp, const char __user *buf,
*pos += page_len;
len -= page_len;
done += page_len;
- buf += page_len;
lm_file->size += page_len;
}
out_unlock:
@@ -332,7 +332,7 @@ static ssize_t pds_vfio_restore_write(struct file *filp, const char __user *buf,

static const struct file_operations pds_vfio_restore_fops = {
.owner = THIS_MODULE,
- .write = pds_vfio_restore_write,
+ .write_iter = pds_vfio_restore_write,
.release = pds_vfio_release_file,
.llseek = no_llseek,
};
diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
index e97d796a54fb..dd96d1756110 100644
--- a/drivers/vfio/vfio_main.c
+++ b/drivers/vfio/vfio_main.c
@@ -1277,6 +1277,7 @@ static ssize_t vfio_device_fops_read(struct file *filep, char __user *buf,

return device->ops->read(device, buf, count, ppos);
}
+FOPS_READ_ITER_HELPER(vfio_device_fops_read);

static ssize_t vfio_device_fops_write(struct file *filep,
const char __user *buf,
@@ -1294,6 +1295,7 @@ static ssize_t vfio_device_fops_write(struct file *filep,

return device->ops->write(device, buf, count, ppos);
}
+FOPS_WRITE_ITER_HELPER(vfio_device_fops_write);

static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma)
{
@@ -1314,8 +1316,8 @@ const struct file_operations vfio_device_fops = {
.owner = THIS_MODULE,
.open = vfio_device_fops_cdev_open,
.release = vfio_device_fops_release,
- .read = vfio_device_fops_read,
- .write = vfio_device_fops_write,
+ .read_iter = vfio_device_fops_read_iter,
+ .write_iter = vfio_device_fops_write_iter,
.unlocked_ioctl = vfio_device_fops_unl_ioctl,
.compat_ioctl = compat_ptr_ioctl,
.mmap = vfio_device_fops_mmap,
--
2.43.0