From: Dongli Zhang <dongli.zhang@oracle.com>
To: virtualization@lists.linux.dev
Cc: mst@redhat.com, jasowangio@gmail.com,
michael.christie@oracle.com, pbonzini@redhat.com,
stefanha@redhat.com, eperezma@redhat.com, kvm@vger.kernel.org,
joe.jin@oracle.com
Subject: [PATCH v2 2/2] vhost-scsi: clamp max_io_vqs module parameter
Date: Sun, 2 Aug 2026 10:24:56 -0700 [thread overview]
Message-ID: <20260802172534.260047-3-dongli.zhang@oracle.com> (raw)
In-Reply-To: <20260802172534.260047-1-dongli.zhang@oracle.com>
max_io_vqs is currently validated only when a vhost-scsi device is opened.
This allows sysfs to show values larger than the driver will actually use,
e.g. writing 2048 succeeds even though vhost_scsi_open() later clamps it to
VHOST_SCSI_MAX_IO_VQ. This makes the sysfs value differ from the value that
will actually be used.
hv# echo 2048 > /sys/module/vhost_scsi/parameters/max_io_vqs
hv# cat /sys/module/vhost_scsi/parameters/max_io_vqs
2048
[ 315.630495] Invalid max_io_vqs of 2048. Using 1024.
Keep accepting out-of-range values for compatibility, but clamp them in the
module parameter setter and store the effective value. This preserves the
existing behavior that invalid values do not make module loading or sysfs
writes fail. It also makes reads report the value that will actually be
used.
With the parameter value kept in range, remove the duplicate validation
from vhost_scsi_open().
Signed-off-by: Dongli Zhang <dongli.zhang@oracle.com>
---
v1->v2:
- Access vhost_scsi_max_io_vqs with READ_ONCE().
- Clamp out-of-range max_io_vqs values instead of rejecting them.
drivers/vhost/scsi.c | 43 +++++++++++++++++++++++++++++++++----------
1 file changed, 33 insertions(+), 10 deletions(-)
diff --git a/drivers/vhost/scsi.c b/drivers/vhost/scsi.c
index 0c0634eea144..fb262cf9a34d 100644
--- a/drivers/vhost/scsi.c
+++ b/drivers/vhost/scsi.c
@@ -210,7 +210,37 @@ static const int vhost_scsi_bits[] = {
#define VHOST_SCSI_MAX_EVENT 128
static unsigned vhost_scsi_max_io_vqs = 128;
-module_param_named(max_io_vqs, vhost_scsi_max_io_vqs, uint, 0644);
+
+static int vhost_scsi_set_max_io_vqs(const char *val,
+ const struct kernel_param *kp)
+{
+ unsigned int max_io_vqs;
+ int ret;
+
+ ret = kstrtouint(val, 0, &max_io_vqs);
+ if (ret)
+ return ret;
+
+ if (max_io_vqs > VHOST_SCSI_MAX_IO_VQ) {
+ pr_err("Invalid max_io_vqs of %u. Using %u.\n",
+ max_io_vqs, VHOST_SCSI_MAX_IO_VQ);
+ max_io_vqs = VHOST_SCSI_MAX_IO_VQ;
+ } else if (!max_io_vqs) {
+ pr_err("Invalid max_io_vqs of 0. Using 1.\n");
+ max_io_vqs = 1;
+ }
+
+ WRITE_ONCE(vhost_scsi_max_io_vqs, max_io_vqs);
+ return 0;
+}
+
+static const struct kernel_param_ops vhost_scsi_max_io_vqs_op = {
+ .set = vhost_scsi_set_max_io_vqs,
+ .get = param_get_uint,
+};
+
+module_param_cb(max_io_vqs, &vhost_scsi_max_io_vqs_op,
+ &vhost_scsi_max_io_vqs, 0644);
MODULE_PARM_DESC(max_io_vqs, "Set the max number of IO virtqueues a vhost scsi device can support. The default is 128. The max is 1024.");
struct vhost_scsi_virtqueue {
@@ -2273,21 +2303,14 @@ static int vhost_scsi_open(struct inode *inode, struct file *f)
struct vhost_scsi_virtqueue *svq;
struct vhost_scsi *vs;
struct vhost_virtqueue **vqs;
- int r = -ENOMEM, i, nvqs = vhost_scsi_max_io_vqs;
+ int r = -ENOMEM, i, nvqs;
vs = kvzalloc_obj(*vs);
if (!vs)
goto err_vs;
vs->inline_sg_cnt = vhost_scsi_inline_sg_cnt;
- if (nvqs > VHOST_SCSI_MAX_IO_VQ) {
- pr_err("Invalid max_io_vqs of %d. Using %d.\n", nvqs,
- VHOST_SCSI_MAX_IO_VQ);
- nvqs = VHOST_SCSI_MAX_IO_VQ;
- } else if (nvqs == 0) {
- pr_err("Invalid max_io_vqs of %d. Using 1.\n", nvqs);
- nvqs = 1;
- }
+ nvqs = READ_ONCE(vhost_scsi_max_io_vqs);
nvqs += VHOST_SCSI_VQ_IO;
vs->old_inflight = kmalloc_objs(*vs->old_inflight, nvqs,
--
2.43.5
next prev parent reply other threads:[~2026-08-02 17:28 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-02 17:24 [PATCH v2 0/2] vhost-scsi: fix max_io_vqs handling Dongli Zhang
2026-08-02 17:24 ` [PATCH v2 1/2] vhost-scsi: use kvzalloc for vq array allocation Dongli Zhang
2026-08-06 18:30 ` Mike Christie
2026-08-02 17:24 ` Dongli Zhang [this message]
2026-08-06 19:30 ` [PATCH v2 2/2] vhost-scsi: clamp max_io_vqs module parameter Mike Christie
2026-08-11 13:51 ` [PATCH v2 0/2] vhost-scsi: fix max_io_vqs handling Stefan Hajnoczi
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=20260802172534.260047-3-dongli.zhang@oracle.com \
--to=dongli.zhang@oracle.com \
--cc=eperezma@redhat.com \
--cc=jasowangio@gmail.com \
--cc=joe.jin@oracle.com \
--cc=kvm@vger.kernel.org \
--cc=michael.christie@oracle.com \
--cc=mst@redhat.com \
--cc=pbonzini@redhat.com \
--cc=stefanha@redhat.com \
--cc=virtualization@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox