On Wed, Feb 24, 2016 at 08:19:58AM +0100, Jiri Pirko wrote:
> Tue, Feb 23, 2016 at 10:19:48PM CET, [email protected] wrote:
> >On Tue, Feb 23, 2016 at 04:51:26PM +0100, Jiri Pirko wrote:
> >> From: Jiri Pirko <[email protected]>
> >> 
> >> Introduce devlink infrastructure for drivers to register and expose to
> >> userspace via generic Netlink interface.
> >> 
> >> There are two basic objects defined:
> >> devlink - one instance for every "parent device", for example switch ASIC
> >> devlink port - one instance for every physical port of the device.
> >> 
> >> This initial portion implements basic get/dump of objects to userspace.
> >> Also, port splitter and port type setting is implemented.
> >...
> >> +/**
> >> + *        devlink_alloc - Allocate new devlink instance resources
> >> + *
> >> + *        @ops: ops
> >> + *        @priv_size: size of user private data
> >> + *
> >> + *        Allocate new devlink instance resources, including devlink index
> >> + *        and name.
> >> + */
> >> +struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t 
> >> priv_size)
> >> +{
> >> +  static atomic_t dev_counter = ATOMIC_INIT(0);
> >> +  struct devlink *devlink;
> >> +
> >> +  devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
> >> +  if (!devlink)
> >> +          return NULL;
> >> +  devlink->ops = ops;
> >> +  devlink_net_set(devlink, &init_net);
> >> +
> >> +different_name:
> >> +  devlink->index = atomic_inc_return(&dev_counter);
> >> +  if (devlink->index < 0) {
> >> +          /* wrapped */
> >> +          atomic_dec(&dev_counter);
> >> +          kfree(devlink);
> >> +          return NULL;
> >> +  }
> >> +  /* atomic_inc_return makes it start at 1, make it start at 0 */
> >> +  devlink->index--;
> >> +
> >> +  dev_set_name(&devlink->dev, DEVLINK_GENL_NAME "%d", devlink->index);
> >> +  if (devlink_name_exists(devlink_net(devlink), devlink_name(devlink)))
> >> +          goto different_name;
> >> +
> >> +  INIT_LIST_HEAD(&devlink->port_list);
> >> +  device_initialize(&devlink->dev);
> >> +  devlink->dev.class = &devlink_class;
> >> +  devlink->dev.platform_data = devlink;
> >> +  return devlink;
> >> +}
> >> +EXPORT_SYMBOL_GPL(devlink_alloc);
> >
> >The increment then decrement bit feels... Clunky and a little confusing.
> >Why not just atomic_read(&dev_counter) before different_name:, then an
> >atomic_inc_return(&dev_counter) under the if clause before the goto, and
> >eliminate the devlink->index-- bit?
> 
> If I split it it would not be atomic op anymore. But the
> devlink->index-- is just to adjust the start, nothing else.
> This is copied from nl80211 code.

Argh, I'm a moron. You need to increment dev_counter, can't just read it's
current value. Still fugly, but okay, does actually serve a purpose this
way.

-- 
Jarod Wilson
[email protected]

Reply via email to