# Re: \[PATCH 2/2\] nvmem: expose NVMEM cells in sysfs

- From: Rafał Miłecki \<zajec5@gmail.com\>
- Date: 2021-12-21 06:39:30 
- List: linux-doc
- Page: https://ratatoskr.run/linux-doc/2021/12/1615135
- Thread: https://ratatoskr.run/linux-doc/2021/12/1613624/t.md

```text
On 21.12.2021 07:33, Greg Kroah-Hartman wrote:
> On Mon, Dec 20, 2021 at 09:39:43PM +0100, Rafał Miłecki wrote:
>> Hi Greg,
>>
>> On 20.12.2021 09:00, Greg Kroah-Hartman wrote:
>>> On Mon, Dec 20, 2021 at 07:47:30AM +0100, Rafał Miłecki wrote:
>>>>    static void nvmem_cell_entry_add(struct nvmem_cell_entry *cell)
>>>>    {
>>>> +	struct device *dev = &cell->nvmem->dev;
>>>> +	int err;
>>>> +
>>>>    	mutex_lock(&nvmem_mutex);
>>>>    	list_add_tail(&cell->node, &cell->nvmem->cells);
>>>>    	mutex_unlock(&nvmem_mutex);
>>>> +
>>>> +	sysfs_attr_init(&cell->battr.attr);
>>>> +	cell->battr.attr.name = cell->name;
>>>> +	cell->battr.attr.mode = 0400;
>>>> +	cell->battr.read = nvmem_cell_attr_read;
>>>> +	err = sysfs_add_bin_file_to_group(&dev->kobj, &cell->battr,
>>>> +					  nvmem_cells_group.name);
>>>
>>> Why not just use the is_bin_visible attribute instead to determine if
>>> the attribute should be shown or not instead of having to add it
>>> after-the-fact which will race with userspace and loose?
>>
>> I'm sorry I really don't see how you suggest to get it done.
>>
>> I can use .is_bin_visible() callback indeed to respect nvmem->root_only.
>
> Great.
>
>> I don't understand addig-after-the-fact part. How is .is_bin_visible()
>> related to adding attributes for newly created cells?
>
> You are adding a sysfs attribute to a device that is already registered
> in the driver core, and so the creation of that attribute is never seen
> by userspace.  The attribute needs to be attached to the device _BEFORE_
> it is registered.
>
> Also, huge hint, if a driver has to call as sysfs_*() call, something is
> wrong.
>
>> Do you mean I can
>> avoid calling sysfs_add_bin_file_to_group()?
>
> Yes.
>
>> Do you recall any existing example of such solution?
>
> Loads.
>
> Just add this attribute group to your driver as a default attribute
> group and the driver core will create it for you if needed.
>
> Or if you always need it, no need to mess sith is_bin_visible() at all,
> I can't really understand what you are trying to do here at all.

Thanks a lot! In nvmem_register() first there is a call to the
device_register() and only later cells get added. I suppose I just have
to rework nvmem_register() order so that:
1. Cells are collected earlier. For each cell I allocate group attribute
2. device_register() gets called

Thank you for explaining that with patience.
```
