Drivers: hv: Make the sysfs node size for the ring buffer dynamic
authorNaman Jain <namjain@linux.microsoft.com>
Fri, 2 May 2025 07:48:11 +0000 (13:18 +0530)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 2 May 2025 11:59:02 +0000 (13:59 +0200)
The ring buffer size varies across VMBus channels. The size of sysfs
node for the ring buffer is currently hardcoded to 4 MB. Userspace
clients either use fstat() or hardcode this size for doing mmap().
To address this, make the sysfs node size dynamic to reflect the
actual ring buffer size for each channel. This will ensure that
fstat() on ring sysfs node always returns the correct size of
ring buffer.

Reviewed-by: Michael Kelley <mhklinux@outlook.com>
Tested-by: Michael Kelley <mhklinux@outlook.com>
Reviewed-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Naman Jain <namjain@linux.microsoft.com>
Link: https://lore.kernel.org/r/20250502074811.2022-3-namjain@linux.microsoft.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/hv/vmbus_drv.c

index 0f16a83cc2d6bf81eac907e730432819926b5638..e3d51a3163163c84f5198cdef48fa34dbb648eae 100644 (file)
@@ -1820,7 +1820,6 @@ static struct bin_attribute chan_attr_ring_buffer = {
                .name = "ring",
                .mode = 0600,
        },
-       .size = 2 * SZ_2M,
        .mmap = hv_mmap_ring_buffer_wrapper,
 };
 static struct attribute *vmbus_chan_attrs[] = {
@@ -1880,11 +1879,21 @@ static umode_t vmbus_chan_bin_attr_is_visible(struct kobject *kobj,
        return attr->attr.mode;
 }
 
+static size_t vmbus_chan_bin_size(struct kobject *kobj,
+                                 const struct bin_attribute *bin_attr, int a)
+{
+       const struct vmbus_channel *channel =
+               container_of(kobj, struct vmbus_channel, kobj);
+
+       return channel->ringbuffer_pagecount << PAGE_SHIFT;
+}
+
 static const struct attribute_group vmbus_chan_group = {
        .attrs = vmbus_chan_attrs,
        .bin_attrs = vmbus_chan_bin_attrs,
        .is_visible = vmbus_chan_attr_is_visible,
        .is_bin_visible = vmbus_chan_bin_attr_is_visible,
+       .bin_size = vmbus_chan_bin_size,
 };
 
 static const struct kobj_type vmbus_chan_ktype = {