Re: [PATCH v4] Drivers: hv: vmbus: Expose monitor data only when monitor pages are used

From: Kimberly Brown
Date: Sun Mar 03 2019 - 16:41:05 EST


On Sat, Mar 02, 2019 at 06:39:30PM +0000, Michael Kelley wrote:
> From: Kimberly Brown <kimbrownkd@xxxxxxxxx> Sent: Friday, March 1, 2019 11:18 AM
> >
> > +/*
> > + * Channel-level attribute_group callback function. Returns the permission for
> > + * each attribute, and returns 0 if an attribute is not visible.
> > + */
> > +static umode_t vmbus_chan_attr_is_visible(struct kobject *kobj,
> > + struct attribute *attr, int idx)
> > +{
> > + const struct vmbus_channel *channel =
> > + container_of(kobj, struct vmbus_channel, kobj);
> > +
> > + /* Hide the monitor attributes if the monitor mechanism is not used. */
> > + if (!channel->offermsg.monitor_allocated &&
> > + (attr == &chan_attr_pending.attr ||
> > + attr == &chan_attr_latency.attr ||
> > + attr == &chan_attr_monitor_id.attr))
> > + return 0;
> > +
> > + return attr->mode;
> > +}
> > +
> > +static struct attribute_group vmbus_chan_group = {
> > + .attrs = vmbus_chan_attrs,
> > + .is_visible = vmbus_chan_attr_is_visible
> > +};
> > +
> > static struct kobj_type vmbus_chan_ktype = {
> > .sysfs_ops = &vmbus_chan_sysfs_ops,
> > .release = vmbus_chan_release,
> > - .default_attrs = vmbus_chan_attrs,
>
> Just to double-check my understanding, removing the default_attrs
> here means that in vmbus_add_channel_kobj(), the call to
> kobject_init_and_add() will only create the sub-directory that is the
> channel number. The sub-directory will be empty. Then the new
> call to sysfs_create_group() that you added below will populate
> the subdirectory, as filtered by vmbus_chan_attr_is_visible().

Yes, that's my understanding as well.

>
> > };
> >
> > /*
> > @@ -1571,6 +1624,12 @@ int vmbus_add_channel_kobj(struct hv_device *dev, struct
> > vmbus_channel *channel)
> > if (ret)
> > return ret;
> >
> > + ret = sysfs_create_group(kobj, &vmbus_chan_group);
> > + if (ret) {
> > + pr_err("Unable to set up channel sysfs files\n");
> > + return ret;
>
> In this error case, the previously created sub-directory that is the
> channel number needs to be deleted/cleaned up.

I was going to let the kobject and sysfs systems take care of that. If
vmbus_add_channel_kobj() returns an error to the calling functions, they
call kobject_put(), which eventually removes the directory.

There are two functions that call vmbus_add_channel_kobj():
vmbus_add_channel_work() and vmbus_device_register(). If
vmbus_add_channel_kobj() returns an error to vmbus_add_channel_work(),
the function calls are:

free_channel() => kobject_put() (channel kobj ref. count should now be
0) => kobject_release() => kobject_cleanup() => kobject_del() =>
sysfs_remove_dir()

If vmbus_add_channel_kobject() returns an error to
vmbus_device_register(), the device's sub-directory is removed:

device_unregister() => put_device() => kobject_put() (device kobj ref.
count should now be 0) => kobject_release() => ...


So, I don't think its necessary to cleanup the subdirectory created by
kobject_init_and_add() here.

Thanks,
Kim


>
> > + }
> > +
>
> Michael