Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The new helpers virtio_admin_obj_create() and
virtio_admin_obj_destroy() in drivers/virtio/virtio_admin_commands.c…
- [Low] virtio_admin_obj_create()/destroy() accept any obj_type and
obj_id.
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the new
helpers add two more exposed callers.
--
virtio: Expose object create and destroy API
This commit adds virtio_admin_obj_create() and virtio_admin_obj_destroy()
to drivers/virtio/virtio_admin_commands.c. They build the
RESOURCE_OBJ_CREATE and RESOURCE_OBJ_DESTROY admin commands from an object
type, id and group fields passed in by the caller. This lets upper layer
drivers use them outside the existing dev parts code.
> diff --git a/drivers/virtio/virtio_admin_commands.c b/drivers/virtio/virtio_admin_commands.c
> index d4bf8c48e1d91..c53c26de91975 100644
> --- a/drivers/virtio/virtio_admin_commands.c
> +++ b/drivers/virtio/virtio_admin_commands.c
> @@ -96,5 +96,82 @@ int virtio_admin_cap_set(struct virtio_device *vdev,
> }
> EXPORT_SYMBOL_GPL(virtio_admin_cap_set);
>
> +int virtio_admin_obj_create(struct virtio_device *vdev,
> + u16 obj_type,
> + u32 obj_id,
> + u16 group_type,
> + u64 group_member_id,
> + const void *obj_specific_data,
> + size_t obj_specific_data_size)
> +{
[ ... ]
> + obj_create_data = data;
> + obj_create_data->hdr.type = cpu_to_le16(obj_type);
> + obj_create_data->hdr.id = cpu_to_le32(obj_id);
[Severity: Low]
Should these helpers reject VIRTIO_RESOURCE_OBJ_DEV_PARTS, or at least
document that it must not be passed?
virtio_pci_admin_obj_create() and virtio_pci_admin_obj_destroy() in
drivers/virtio/virtio_pci_modern.c track ids for that object type in
admin_vq.dev_parts_ida:
virtio_pci_admin_obj_create() {
...
id = ida_alloc_range(&avq->dev_parts_ida, 0,
avq->max_dev_parts_objects - 1, GFP_KERNEL);
...
}
virtio_pci_admin_obj_destroy() {
...
if (!ret) {
vp_dev = to_vp_device(virtio_dev);
ida_free(&vp_dev->admin_vq.dev_parts_ida, id);
}
...
}
The new generic helpers accept any obj_type and obj_id. If a caller used
them with VIRTIO_RESOURCE_OBJ_DEV_PARTS, dev_parts_ida could fall out of
sync. A duplicate id could be handed out later, or ida_free() could warn
about an id it never allocated.
No caller in this series does this. The virtio_net users pass the
VIRTIO_NET_RESOURCE_OBJ_FF_* types with VIRTIO_ADMIN_GROUP_TYPE_SELF, so
this only affects the API contract.
> + memcpy(obj_create_data->resource_obj_specific_data, obj_specific_data,
> + obj_specific_data_size);
> + sg_init_one(&data_sg, data, data_size);
> +
> + cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_RESOURCE_OBJ_CREATE);
> + cmd.group_type = cpu_to_le16(group_type);
> + cmd.group_member_id = cpu_to_le64(group_member_id);
> + cmd.data_sg = &data_sg;
[Severity: Low]
This isn't a bug, but this builds the same RESOURCE_OBJ_CREATE payload
that virtio_pci_admin_obj_create() in drivers/virtio/virtio_pci_modern.c
already builds:
virtio_pci_admin_obj_create() {
...
cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_RESOURCE_OBJ_CREATE);
cmd.group_type = cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SRIOV);
cmd.group_member_id = cpu_to_le64(vf_id + 1);
...
}
virtio_pci_admin_obj_destroy() overlaps with virtio_admin_obj_destroy() in
the same way. The only extra work it does is the ida_free().
Could the PCI dev parts helpers call the new generic ones, passing
VIRTIO_ADMIN_GROUP_TYPE_SRIOV and vf_id + 1? At the end of the series,
both copies of the command-building code are still there.
> +
> + err = vdev->config->admin_cmd_exec(vdev, &cmd);
> + kfree(data);
[Severity: High]
This is a pre-existing issue, but these helpers add two more callers that
can hit it. What happens to cmd and data if virtqueue_kick() fails after
virtqueue_add_sgs() has succeeded?
drivers/virtio/virtio_pci_modern.c:virtqueue_exec_admin_cmd() {
...
if (!virtqueue_kick(vq))
goto unlock_err;
...
unlock_err:
mutex_unlock(&admin_vq->lock);
return -EIO;
}
On this path the descriptors stay published in the admin vq. They still
point at data, at va_hdr and va_status (which vp_modern_admin_cmd_exec()
frees), and at the token cmd, which lives on this function's stack.
virtqueue_kick() returns false when vq->broken is set. virtio_break_device()
sets that flag without taking admin_vq->lock. One example is
virtio_pci_remove() on surprise removal, so the flag can be set between
the add and the kick.
After the -EIO return, this function frees data and returns, so the stack
frame holding cmd is gone. Later, on the remove path:
virtio_pci_remove()
unregister_virtio_device()
driver remove -> del_vqs()
vp_modern_del_vqs()
vp_modern_avq_cleanup()
vp_modern_avq_cleanup() {
...
while ((cmd = virtqueue_detach_unused_buf(vq))) {
cmd->ret = -EIO;
complete(&cmd->completion);
}
...
}
Doesn't this write to a stack frame that has already returned, and take
the completion spinlock inside it?
If the device is still alive after the break, could it also read or write
descriptors that now point at freed memory?
virtio_admin_obj_destroy() follows the same pattern. Should the kick
failure path in virtqueue_exec_admin_cmd() detach the buffer it just added
before returning -EIO?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/2026…%40nvidia.com