Skip to contentSkip to conversation

uio_hv_generic: Fix sysfs creation path for ring buffer

PATCH Superseded Naman Jain 2 participants 5 messages Mar 18, 2025 – Mar 20, 2025

Subsystems (3)

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Naman Jain

author
#0 lore raw

PATCH v2 1/1 uio_hv_generic: Fix sysfs creation path for ring buffer

On regular bootup, devices get registered to vmbus first, so when uio_hv_generic driver for a particular device type is probed, the device is already initialized and added, so sysfs creation in uio_hv_generic probe works fine. However, when device is removed and brought back, the channel rescinds and device again gets registered to vmbus. However this time, the uio_hv_generic driver is already registered to probe for that device and in this case sysfs creation is tried before the device gets initialized completely.

Fix this by moving the core logic of sysfs creation for ring buffer, from uio_hv_generic to HyperV's vmbus driver, where rest of the sysfs attributes for the channels are defined. While doing that, make use of attribute groups and macros, instead of creating sysfs directly, to ensure better error handling and code flow. While at it, configure size of ring sysfs based on ring buffer's actual size and not 2MB default.

Problem path:
vmbus_device_register
device_register
uio_hv_generic probe
sysfs_create_bin_file (fails here)
kset_create_and_add (dependency) vmbus_add_channel_kobj (dependency)

Fixes 9ab877a6ccf8 ("uio_hv_generic: make ring buffer attribute for primary channel")

---
Hi,
PFB change logs:

Changes since v1:
https://lore.kernel.org/all/[email protected]/
* Fixed race condition in setting channel->mmap_ring_buffer by
introducing a new variable for visibility of sysfs (addressed Greg's comments)
* Used binary attribute fields instead of regular ones for initializing attribute_group.
* Make size of ring sysfs dynamic based on actual ring buffer's size.
* Preferred to keep mmap function in uio_hv_generic to give more control over ring's mmap functionality, since this is specific to uio_hv_generic driver.
* Remove spurious warning during sysfs creation in uio_hv_generic probe.
* Added comments in a couple of places.

Changes since RFC patch:
https://lore.kernel.org/all/[email protected]/
* Different approach to solve the problem is proposed (credits to Michael Kelley).
* Core logic for sysfs creation moved out of uio_hv_generic, to VMBus drivers where rest of the sysfs attributes for a VMBus channel are defined. (addressed Greg's comments)
* Used attribute groups instead of sysfs_create* functions, and bundled ring attribute with other attributes for the channel sysfs.

Error logs:

[   35.574120] ------------[ cut here ]------------
[   35.574122] WARNING: CPU: 0 PID: 10 at fs/sysfs/file.c:591 sysfs_create_bin_file+0x81/0x90
[   35.574168] Workqueue: hv_pri_chan vmbus_add_channel_work
[   35.574172] RIP: 0010:sysfs_create_bin_file+0x81/0x90
[   35.574197] Call Trace:
[   35.574199]  <TASK>
[   35.574200]  ? show_regs+0x69/0x80
[   35.574217]  ? __warn+0x8d/0x130
[   35.574220]  ? sysfs_create_bin_file+0x81/0x90
[   35.574222]  ? report_bug+0x182/0x190
[   35.574225]  ? handle_bug+0x5b/0x90
[   35.574244]  ? exc_invalid_op+0x19/0x70
[   35.574247]  ? asm_exc_invalid_op+0x1b/0x20
[   35.574252]  ? sysfs_create_bin_file+0x81/0x90
[   35.574255]  hv_uio_probe+0x1e7/0x410 [uio_hv_generic]
[   35.574271]  vmbus_probe+0x3b/0x90
[   35.574275]  really_probe+0xf4/0x3b0
[   35.574279]  __driver_probe_device+0x8a/0x170
[   35.574282]  driver_probe_device+0x23/0xc0
[   35.574285]  __device_attach_driver+0xb5/0x140
[   35.574288]  ? __pfx___device_attach_driver+0x10/0x10
[   35.574291]  bus_for_each_drv+0x86/0xe0
[   35.574294]  __device_attach+0xc1/0x200
[   35.574297]  device_initial_probe+0x13/0x20
[   35.574315]  bus_probe_device+0x99/0xa0
[   35.574318]  device_add+0x647/0x870
[   35.574320]  ? hrtimer_init+0x28/0x70
[   35.574323]  device_register+0x1b/0x30
[   35.574326]  vmbus_device_register+0x83/0x130
[   35.574328]  vmbus_add_channel_work+0x135/0x1a0
[   35.574331]  process_one_work+0x177/0x340
[   35.574348]  worker_thread+0x2b2/0x3c0
[   35.574350]  kthread+0xe3/0x1f0
[   35.574353]  ? __pfx_worker_thread+0x10/0x10
[   35.574356]  ? __pfx_kthread+0x10/0x10
---
 drivers/hv/hyperv_vmbus.h    |  6 +++
 drivers/hv/vmbus_drv.c       | 82 +++++++++++++++++++++++++++++++++++-
 drivers/uio/uio_hv_generic.c | 33 +++++----------
 include/linux/hyperv.h       |  6 +++
 4 files changed, 104 insertions(+), 23 deletions(-)
drivers/hv/hyperv_vmbus.h +6 -0
index 29780f3a7478..0b450e53161e 100644
@@ -477,4 +477,10 @@ static inline int hv_debug_add_dev_dir(struct hv_device *dev)
 
 #endif /* CONFIG_HYPERV_TESTING */
 
+/* Create and remove sysfs entry for memory mapped ring buffers for a channel */
+int hv_create_ring_sysfs(struct vmbus_channel *channel,
+			 int (*hv_mmap_ring_buffer)(struct vmbus_channel *channel,
+						    struct vm_area_struct *vma));
+int hv_remove_ring_sysfs(struct vmbus_channel *channel);
+
 #endif /* _HYPERV_VMBUS_H */
drivers/hv/vmbus_drv.c +81 -1

108 lines not on this page · raw message

drivers/uio/uio_hv_generic.c +11 -22
index 1b19b5647495..a398e93ed382 100644
@@ -131,15 +131,12 @@ static void hv_uio_rescind(struct vmbus_channel *channel)
 	vmbus_device_unregister(channel->device_obj);
 }
 
-/* Sysfs API to allow mmap of the ring buffers
+/* Function used for mmap of ring buffer sysfs interface.
  * The ring buffer is allocated as contiguous memory by vmbus_open
  */
-static int hv_uio_ring_mmap(struct file *filp, struct kobject *kobj,
-			    const struct bin_attribute *attr,
-			    struct vm_area_struct *vma)
+static int
+hv_uio_ring_mmap(struct vmbus_channel *channel, struct vm_area_struct *vma)
 {
-	struct vmbus_channel *channel
-		= container_of(kobj, struct vmbus_channel, kobj);
 	void *ring_buffer = page_address(channel->ringbuffer_page);
 
 	if (channel->state != CHANNEL_OPENED_STATE)
@@ -149,15 +146,6 @@ static int hv_uio_ring_mmap(struct file *filp, struct kobject *kobj,
 			       channel->ringbuffer_pagecount << PAGE_SHIFT);
 }
 
-static const struct bin_attribute ring_buffer_bin_attr = {
-	.attr = {
-		.name = "ring",
-		.mode = 0600,
-	},
-	.size = 2 * SZ_2M,
-	.mmap = hv_uio_ring_mmap,
-};
-
 /* Callback from VMBUS subsystem when new channel created. */
 static void
 hv_uio_new_channel(struct vmbus_channel *new_sc)
@@ -178,8 +166,7 @@ hv_uio_new_channel(struct vmbus_channel *new_sc)
 	/* Disable interrupts on sub channel */
 	new_sc->inbound.ring_buffer->interrupt_mask = 1;
 	set_channel_read_mode(new_sc, HV_CALL_ISR);
-
-	ret = sysfs_create_bin_file(&new_sc->kobj, &ring_buffer_bin_attr);
+	ret = hv_create_ring_sysfs(new_sc, hv_uio_ring_mmap);
 	if (ret) {
 		dev_err(device, "sysfs create ring bin file failed; %d\n", ret);
 		vmbus_close(new_sc);
@@ -350,10 +337,12 @@ hv_uio_probe(struct hv_device *dev,
 		goto fail_close;
 	}
 
-	ret = sysfs_create_bin_file(&channel->kobj, &ring_buffer_bin_attr);
-	if (ret)
-		dev_notice(&dev->device,
-			   "sysfs create ring bin file failed; %d\n", ret);
+	/*
+	 * This internally calls sysfs_update_group, which returns a non-zero value if it executes
+	 * before sysfs_create_group. This is a false alarm from this use case point of view and
+	 * thus, no need to check the return value and print warning.
+	 */
+	hv_create_ring_sysfs(channel, hv_uio_ring_mmap);
 
 	hv_set_drvdata(dev, pdata);
 
@@ -375,7 +364,7 @@ hv_uio_remove(struct hv_device *dev)
 	if (!pdata)
 		return;
 
-	sysfs_remove_bin_file(&dev->channel->kobj, &ring_buffer_bin_attr);
+	hv_remove_ring_sysfs(dev->channel);
 	uio_unregister_device(&pdata->info);
 	hv_uio_cleanup(dev, pdata);
 
include/linux/hyperv.h +6 -0
index 675959fb97ba..d6ffe01962c2 100644
@@ -1002,6 +1002,12 @@ struct vmbus_channel {
 
 	/* The max size of a packet on this channel */
 	u32 max_pkt_size;
+
+	/* function to mmap ring buffer memory to the channel's sysfs ring attribute */
+	int (*mmap_ring_buffer)(struct vmbus_channel *channel, struct vm_area_struct *vma);
+
+	/* boolean to control visibility of sysfs for ring buffer */
+	bool ring_sysfs_visible;
 };
 
 #define lock_requestor(channel, flags)					\

base-commit: e94bd4ec45ac156616da285a0bf03056cd7430fc

Signature · 1 line
2.34.1
#1 lore raw
Quoted 9 lines · Naman Jain

On regular bootup, devices get registered to vmbus first, so when uio_hv_generic driver for a particular device type is probed, the device is already initialized and added, so sysfs creation in uio_hv_generic probe works fine. However, when device is removed and brought back, the channel rescinds and device again gets registered to vmbus. However this time, the uio_hv_generic driver is already registered to probe for that device and in this case sysfs creation is tried before the device gets initialized completely.

Quoted 6 lines

Fix this by moving the core logic of sysfs creation for ring buffer, from uio_hv_generic to HyperV's vmbus driver, where rest of the sysfs attributes for the channels are defined. While doing that, make use of attribute groups and macros, instead of creating sysfs directly, to ensure better error handling and code flow. While at it, configure size of ring sysfs based on ring buffer's actual size and not 2MB default.

When you say stuff like "while at it..." that's a huge hint that the patch should be broken up into smaller pieces and made a patch series.

Quoted 5 lines

Problem path:
vmbus_device_register
device_register
uio_hv_generic probe
sysfs_create_bin_file (fails here)

Why does it fail?

Quoted 2 lines

kset_create_and_add (dependency)
vmbus_add_channel_kobj (dependency)

I don't understand this "graph", sorry.

Quoted 3 lines

+/*
+ * hv_create_ring_sysfs - create ring sysfs entry corresponding to ring buffers for a channel
+ */

Kerneldoc?

drivers/hv/vmbus_drv.c static const struct kobj_type vmbus_chan_ktype = { · line 1917

Quoted 11 lines
drivers/hv/vmbus_drv.c +11 -0
+int hv_create_ring_sysfs(struct vmbus_channel *channel,
+			 int (*hv_mmap_ring_buffer)(struct vmbus_channel *channel,
+						    struct vm_area_struct *vma))
+{
+	struct kobject *kobj = &channel->kobj;
+
+	channel->mmap_ring_buffer = hv_mmap_ring_buffer;
+	channel->ring_sysfs_visible = true;
+	return sysfs_update_group(kobj, &vmbus_chan_group);
+}
+EXPORT_SYMBOL_GPL(hv_create_ring_sysfs);

You just raced userspace and created a file without telling it that it showed up, right? Something still feels really wrong here.

greg k-h

author
#2 lore raw
Quoted 17 lines · Greg Kroah-Hartman
Quoted 9 lines · Naman Jain

On regular bootup, devices get registered to vmbus first, so when uio_hv_generic driver for a particular device type is probed, the device is already initialized and added, so sysfs creation in uio_hv_generic probe works fine. However, when device is removed and brought back, the channel rescinds and device again gets registered to vmbus. However this time, the uio_hv_generic driver is already registered to probe for that device and in this case sysfs creation is tried before the device gets initialized completely.

Quoted 6 lines

Fix this by moving the core logic of sysfs creation for ring buffer, from uio_hv_generic to HyperV's vmbus driver, where rest of the sysfs attributes for the channels are defined. While doing that, make use of attribute groups and macros, instead of creating sysfs directly, to ensure better error handling and code flow. While at it, configure size of ring sysfs based on ring buffer's actual size and not 2MB default.

Quoted 3 lines

When you say stuff like "while at it..." that's a huge hint that the patch should be broken up into smaller pieces and made a patch series.

I should have done that. I'll take care of it in next version.

Quoted 7 lines
Quoted 5 lines

Problem path:
vmbus_device_register
device_register
uio_hv_generic probe
sysfs_create_bin_file (fails here)

Why does it fail?

It fails because device kobj is not yet initialized. Will add more details.

Quoted 6 lines
Quoted 3 lines

kset_create_and_add (dependency)
vmbus_add_channel_kobj (dependency)

I don't understand this "graph", sorry.

I will revise the commit msg accordingly. Thanks.

Quoted 5 lines
Quoted 3 lines

+/*
+ * hv_create_ring_sysfs - create ring sysfs entry corresponding to ring buffers for a channel
+ */

Kerneldoc?

Documentation of the ring sysfs is present here -
Documentation/ABI/stable/sysfs-bus-vmbus

What:           /sys/bus/vmbus/devices/<UUID>/channels/<N>/ring
Date:           January. 2018
KernelVersion:  4.16
Contact:        Stephen Hemminger <[email protected]>
Description:    Binary file created by uio_hv_generic for ring buffer
Users:          Userspace drivers

I should probably change the description, to reflect that it's visibility is controlled by uio_hv_generic, but it's created by hyperV drivers.

Please correct me if I misunderstood your comment.

Quoted 16 lines

drivers/hv/vmbus_drv.c static const struct kobj_type vmbus_chan_ktype = { · line 1917

Quoted 12 lines
drivers/hv/vmbus_drv.c +11 -0
+int hv_create_ring_sysfs(struct vmbus_channel *channel,
+			 int (*hv_mmap_ring_buffer)(struct vmbus_channel *channel,
+						    struct vm_area_struct *vma))
+{
+	struct kobject *kobj = &channel->kobj;
+
+	channel->mmap_ring_buffer = hv_mmap_ring_buffer;
+	channel->ring_sysfs_visible = true;
+	return sysfs_update_group(kobj, &vmbus_chan_group);
+}
+EXPORT_SYMBOL_GPL(hv_create_ring_sysfs);

You just raced userspace and created a file without telling it that it showed up, right? Something still feels really wrong here.

Quoted 1 line

greg k-h

From use-case POV, we needed to have uio_hv_generic control the visibility of this ring sysfs node, because the same device (HV_NIC) is used by either hv_netvsc or uio_hv_generic at any given point of time. We didn't want to expose ring buffer through sysfs when hv_netvsc is using it. That's the reason why uio_hv_generic was creating sysfs in the first place.

DPDK, which uses this ring sysfs, checks for its presence for primary channel but for secondary channels, it additionally does mmap() of this ring. That's where it becomes more important, not to expose ring buffer when uio_hv_generic is not bind to the device.

DPDK runs after uio_hv_generic probe is complete, so I am not sure if this race can happen, in practice. I'll try to gather more information around it.

Regards,
Naman

#3 lore raw
Quoted 17 lines · Naman Jain
Quoted 5 lines · Greg Kroah-Hartman
Quoted 3 lines

+/*
+ * hv_create_ring_sysfs - create ring sysfs entry corresponding to ring buffers for a channel
+ */

Kerneldoc?

Documentation of the ring sysfs is present here -
Documentation/ABI/stable/sysfs-bus-vmbus

What:           /sys/bus/vmbus/devices/<UUID>/channels/<N>/ring
Date:           January. 2018
KernelVersion:  4.16
Contact:        Stephen Hemminger <[email protected]>
Description:    Binary file created by uio_hv_generic for ring buffer
Users:          Userspace drivers
Quoted 4 lines

I should probably change the description, to reflect that it's visibility is controlled by uio_hv_generic, but it's created by hyperV drivers.

Please correct me if I misunderstood your comment.

I mean you are adding a comment here that is NOT in the correct kerneldoc fomat.

Quoted 23 lines
Quoted 15 lines

drivers/hv/vmbus_drv.c static const struct kobj_type vmbus_chan_ktype = { · line 1917

Quoted 11 lines
drivers/hv/vmbus_drv.c +11 -0
+int hv_create_ring_sysfs(struct vmbus_channel *channel,
+			 int (*hv_mmap_ring_buffer)(struct vmbus_channel *channel,
+						    struct vm_area_struct *vma))
+{
+	struct kobject *kobj = &channel->kobj;
+
+	channel->mmap_ring_buffer = hv_mmap_ring_buffer;
+	channel->ring_sysfs_visible = true;
+	return sysfs_update_group(kobj, &vmbus_chan_group);
+}
+EXPORT_SYMBOL_GPL(hv_create_ring_sysfs);

You just raced userspace and created a file without telling it that it showed up, right? Something still feels really wrong here.

Quoted 1 line

greg k-h

From use-case POV, we needed to have uio_hv_generic control the visibility of this ring sysfs node, because the same device (HV_NIC) is used by either hv_netvsc or uio_hv_generic at any given point of time. We didn't want to expose ring buffer through sysfs when hv_netvsc is using it. That's the reason why uio_hv_generic was creating sysfs in the first place.

Quoted 7 lines

DPDK, which uses this ring sysfs, checks for its presence for primary channel but for secondary channels, it additionally does mmap() of this ring. That's where it becomes more important, not to expose ring buffer when uio_hv_generic is not bind to the device.

DPDK runs after uio_hv_generic probe is complete, so I am not sure if this race can happen, in practice. I'll try to gather more information around it.

Please do, and document the heck out of why you are doing it this way and why there is no such race or issue, and why no one else should ever copy this pattern as an example of what to do for other drivers.

thanks,

greg k-h

author
#4 lore raw
Quoted 18 lines · Greg Kroah-Hartman
Quoted 17 lines · Naman Jain
Quoted 5 lines · Greg Kroah-Hartman
Quoted 3 lines

+/*
+ * hv_create_ring_sysfs - create ring sysfs entry corresponding to ring buffers for a channel
+ */

Kerneldoc?

Documentation of the ring sysfs is present here -
Documentation/ABI/stable/sysfs-bus-vmbus

What:           /sys/bus/vmbus/devices/<UUID>/channels/<N>/ring
Date:           January. 2018
KernelVersion:  4.16
Contact:        Stephen Hemminger <[email protected]>
Description:    Binary file created by uio_hv_generic for ring buffer
Users:          Userspace drivers
Quoted 7 lines
Quoted 4 lines

I should probably change the description, to reflect that it's visibility is controlled by uio_hv_generic, but it's created by hyperV drivers.

Please correct me if I misunderstood your comment.

I mean you are adding a comment here that is NOT in the correct kerneldoc fomat.

Thanks, I get it now. I'll make the changes.

Quoted 32 lines
Quoted 24 lines
Quoted 16 lines

drivers/hv/vmbus_drv.c static const struct kobj_type vmbus_chan_ktype = { · line 1917

Quoted 12 lines
drivers/hv/vmbus_drv.c +11 -0
+int hv_create_ring_sysfs(struct vmbus_channel *channel,
+			 int (*hv_mmap_ring_buffer)(struct vmbus_channel *channel,
+						    struct vm_area_struct *vma))
+{
+	struct kobject *kobj = &channel->kobj;
+
+	channel->mmap_ring_buffer = hv_mmap_ring_buffer;
+	channel->ring_sysfs_visible = true;
+	return sysfs_update_group(kobj, &vmbus_chan_group);
+}
+EXPORT_SYMBOL_GPL(hv_create_ring_sysfs);

You just raced userspace and created a file without telling it that it showed up, right? Something still feels really wrong here.

Quoted 1 line

greg k-h

From use-case POV, we needed to have uio_hv_generic control the visibility
of this ring sysfs node, because the same device (HV_NIC) is used by either hv_netvsc or uio_hv_generic at any given point of time. We didn't want to expose ring buffer through sysfs when hv_netvsc is using it. That's the reason why uio_hv_generic was creating sysfs in the first place.

Quoted 7 lines

DPDK, which uses this ring sysfs, checks for its presence for primary channel but for secondary channels, it additionally does mmap() of this ring. That's where it becomes more important, not to expose ring buffer when uio_hv_generic is not bind to the device.

DPDK runs after uio_hv_generic probe is complete, so I am not sure if this race can happen, in practice. I'll try to gather more information around it.

Quoted 7 lines

Please do, and document the heck out of why you are doing it this way and why there is no such race or issue, and why no one else should ever copy this pattern as an example of what to do for other drivers.

thanks,

greg k-h

I'll add the necessary comments to highlight these problems.

Thanks for reviewing.

Regards,
Naman