FAILED: patch "[PATCH] uio_hv_generic: Fix sysfs creation path for ring buffer" failed to apply to 6.12-stable tree
Subsystems (3)
[email protected]
maintainerThe patch below does not apply to the 6.12-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.12.y
git checkout FETCH_HEAD
git cherry-pick -x f31fe8165d365379d858c53bef43254c7d6d1cfd
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2025051208-hardly-dole-1422@gregkh' --subject-prefix 'PATCH 6.12.y' HEAD^..
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From f31fe8165d365379d858c53bef43254c7d6d1cfd Mon Sep 17 00:00:00 2001
From: Naman Jain <[email protected]>
Date: Fri, 2 May 2025 13:18:10 +0530
Subject: [PATCH] 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 hv_uio_probe() works fine. However, when the device is removed and brought back, the channel gets rescinded and the 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's kobject gets initialized completely.
Fix this by moving the core logic of sysfs creation of ring buffer, from uio_hv_generic to HyperV's VMBus driver, where the 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.
Problematic path:vmbus_process_offer (A new offer comes for the VMBus device)
vmbus_add_channel_work
vmbus_device_register
|-> device_register
| |...
| |-> hv_uio_probe
| |...
| |-> sysfs_create_bin_file (leads to a warning as
| the primary channel's kobject, which is used to
| create the sysfs file, is not yet initialized)
|-> kset_create_and_add
|-> vmbus_add_channel_kobj (initialization of the primary
channel's kobject happens later)Above code flow is sequential and the warning is always reproducible in this path.
drivers/hv/hyperv_vmbus.h +6 -0
@@ -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 +99 -1
126 lines not on this page · raw message
drivers/uio/uio_hv_generic.c +17 -22
@@ -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,18 @@ 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 expected as the 'ring' will be created later in +* vmbus_device_register() -> vmbus_add_channel_kobj(). Thus, no need to check the return +* value and print warning. +* +* Creating/exposing sysfs in driver probe is not encouraged as it can lead to race +* conditions with userspace. For backward compatibility, "ring" sysfs could not be removed +* or decoupled from uio_hv_generic probe. Userspace programs can make use of inotify +* APIs to make sure that ring is created. +*/ +hv_create_ring_sysfs (channel, hv_uio_ring_mmap);hv_set_drvdata (dev, pdata);
@@ -375,7 +370,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
@@ -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) \
PATCH 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 hv_uio_probe() works fine. However, when the device is removed and brought back, the channel gets rescinded and the 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's kobject gets initialized completely.
Fix this by moving the core logic of sysfs creation of ring buffer, from uio_hv_generic to HyperV's VMBus driver, where the 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.
Problematic path:vmbus_process_offer (A new offer comes for the VMBus device)
vmbus_add_channel_work
vmbus_device_register
|-> device_register
| |...
| |-> hv_uio_probe
| |...
| |-> sysfs_create_bin_file (leads to a warning as
| the primary channel's kobject, which is used to
| create the sysfs file, is not yet initialized)
|-> kset_create_and_add
|-> vmbus_add_channel_kobj (initialization of the primary
channel's kobject happens later)Above code flow is sequential and the warning is always reproducible in this path.
--- drivers/hv/hyperv_vmbus.h | 6 +++ drivers/hv/vmbus_drv.c | 100 ++++++++++++++++++++++++++++++++++- drivers/uio/uio_hv_generic.c | 39 ++++++-------- include/linux/hyperv.h | 6 +++ 4 files changed, 128 insertions(+), 23 deletions(-)
drivers/hv/hyperv_vmbus.h +6 -0
@@ -485,4 +485,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 +99 -1
126 lines not on this page · raw message
drivers/uio/uio_hv_generic.c +17 -22
@@ -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, -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,18 @@ 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 expected as the 'ring' will be created later in +* vmbus_device_register() -> vmbus_add_channel_kobj(). Thus, no need to check the return +* value and print warning. +* +* Creating/exposing sysfs in driver probe is not encouraged as it can lead to race +* conditions with userspace. For backward compatibility, "ring" sysfs could not be removed +* or decoupled from uio_hv_generic probe. Userspace programs can make use of inotify +* APIs to make sure that ring is created. +*/ +hv_create_ring_sysfs (channel, hv_uio_ring_mmap);hv_set_drvdata (dev, pdata);
@@ -375,7 +370,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
@@ -1067,6 +1067,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) \
Signature · 1 line
2.34.1
Sasha Levin
maintainerRe: [PATCH 6.12.y] uio_hv_generic: Fix sysfs creation path for ring buffer
[ Sasha's backport helper bot ]
Hi,
Summary of potential issues:
⚠️ Found matching upstream commit but patch is missing proper reference to it
Found matching upstream commit: f31fe8165d365379d858c53bef43254c7d6d1cfd
Status in newer kernel trees:
6.14.y | Present (different SHA1: 419663d2d8f5)
Note: The patch differs from the upstream commit:
---
1: f31fe8165d365 ! 1: 0f7ab12a823b8 uio_hv_generic: Fix sysfs creation path for ring buffer
@@ Commit message
+ (cherry picked from commit f31fe8165d365379d858c53bef43254c7d6d1cfd)
## drivers/hv/hyperv_vmbus.h ##
@@ drivers/hv/hyperv_vmbus.h: static inline int hv_debug_add_dev_dir(struct hv_device *dev)
@@ drivers/hv/vmbus_drv.c: static ssize_t subchannel_id_show(struct vmbus_channel *
static VMBUS_CHAN_ATTR_RO(subchannel_id);
+static int hv_mmap_ring_buffer_wrapper(struct file *filp, struct kobject *kobj,
-+ const struct bin_attribute *attr,
++ struct bin_attribute *attr,
+ struct vm_area_struct *vma)
+{
+ struct vmbus_channel *channel = container_of(kobj, struct vmbus_channel, kobj);
@@ drivers/hv/vmbus_drv.c: static umode_t vmbus_chan_attr_is_visible(struct kobject
}
+static umode_t vmbus_chan_bin_attr_is_visible(struct kobject *kobj,
-+ const struct bin_attribute *attr, int idx)
++ struct bin_attribute *attr, int idx)
+{
+ const struct vmbus_channel *channel =
+ container_of(kobj, struct vmbus_channel, kobj);
@@ drivers/uio/uio_hv_generic.c: static void hv_uio_rescind(struct vmbus_channel *c
* 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 bin_attribute *attr,
- struct vm_area_struct *vma)
+static int
+hv_uio_ring_mmap(struct vmbus_channel *channel, struct vm_area_struct *vma)
---
Results of testing on various branches:
| Branch | Patch Apply | Build Test | |---------------------------|-------------|------------| | stable/linux-6.12.y | Success | Success |