[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20160224071958.GB2151@nanopsycho.orion>
Date: Wed, 24 Feb 2016 08:19:58 +0100
From: Jiri Pirko <jiri@...nulli.us>
To: Jarod Wilson <jarod@...hat.com>
Cc: netdev@...r.kernel.org, davem@...emloft.net, idosch@...lanox.com,
eladr@...lanox.com, yotamg@...lanox.com, ogerlitz@...lanox.com,
yishaih@...lanox.com, dledford@...hat.com, sean.hefty@...el.com,
hal.rosenstock@...il.com, eugenia@...lanox.com,
roopa@...ulusnetworks.com, nikolay@...ulusnetworks.com,
hadarh@...lanox.com, jhs@...atatu.com, john.fastabend@...il.com,
jeffrey.t.kirsher@...el.com, brouer@...hat.com, ivecera@...hat.com,
rami.rosen@...el.com, hannes@...essinduktion.org,
gospo@...ulusnetworks.com
Subject: Re: [patch net-next v2 1/9] Introduce devlink infrastructure
Tue, Feb 23, 2016 at 10:19:48PM CET, jarod@...hat.com wrote:
>On Tue, Feb 23, 2016 at 04:51:26PM +0100, Jiri Pirko wrote:
>> From: Jiri Pirko <jiri@...lanox.com>
>>
>> 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.
Powered by blists - more mailing lists