[v7,5/7] nvmem: core: Rework layouts to become platform devices

Message ID 20230801182132.1058707-6-miquel.raynal@bootlin.com
State New
Headers
Series NVMEM cells in sysfs |

Commit Message

Miquel Raynal Aug. 1, 2023, 6:21 p.m. UTC
  Current layout support was initially written without modules support in
mind. When the requirement for module support rose, the existing base
was improved to adopt modularization support, but kind of a design flaw
was introduced. With the existing implementation, when a storage device
registers into NVMEM, the core tries to hook a layout (if any) and
populates its cells immediately. This means, if the hardware description
expects a layout to be hooked up, but no driver was provided for that,
the storage medium will fail to probe and try later from
scratch. Technically, the layouts are more like a "plus" and, even we
consider that the hardware description shall be correct, we could still
probe the storage device (especially if it contains the rootfs).

One way to overcome this situation is to consider the layouts as
devices, and leverage the existing notifier mechanism. When a new NVMEM
device is registered, we can:
- populate its nvmem-layout child, if any
- try to modprobe the relevant driver, if relevant
- try to hook the NVMEM device with a layout in the notifier
And when a new layout is registered:
- try to hook all the existing NVMEM devices which are not yet hooked to
  a layout with the new layout
This way, there is no strong order to enforce, any NVMEM device creation
or NVMEM layout driver insertion will be observed as a new event which
may lead to the creation of additional cells, without disturbing the
probes with costly (and sometimes endless) deferrals.

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
---
 drivers/nvmem/core.c             | 132 ++++++++++++++++++++++++-------
 drivers/nvmem/layouts/onie-tlv.c |  39 +++++++--
 drivers/nvmem/layouts/sl28vpd.c  |  39 +++++++--
 include/linux/nvmem-provider.h   |  11 +--
 4 files changed, 172 insertions(+), 49 deletions(-)
  

Comments

Dan Carpenter Aug. 3, 2023, 10:13 a.m. UTC | #1
Hi Miquel,

kernel test robot noticed the following build warnings:

https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Miquel-Raynal/nvmem-core-Create-all-cells-before-adding-the-nvmem-device/20230802-022331
base:   char-misc/char-misc-testing
patch link:    https://lore.kernel.org/r/20230801182132.1058707-6-miquel.raynal%40bootlin.com
patch subject: [PATCH v7 5/7] nvmem: core: Rework layouts to become platform devices
config: x86_64-randconfig-m001-20230730 (https://download.01.org/0day-ci/archive/20230803/202308030002.DnSFOrMB-lkp@intel.com/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce: (https://download.01.org/0day-ci/archive/20230803/202308030002.DnSFOrMB-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
| Closes: https://lore.kernel.org/r/202308030002.DnSFOrMB-lkp@intel.com/

New smatch warnings:
drivers/nvmem/core.c:1003 nvmem_register() warn: 'layout_np' is an error pointer or valid
drivers/nvmem/core.c:2130 nvmem_try_loading_layout_driver() warn: 'layout_np' is an error pointer or valid

Old smatch warnings:
drivers/nvmem/core.c:761 nvmem_add_cells_from_fixed_layout() warn: 'layout_np' is an error pointer or valid
drivers/nvmem/core.c:802 nvmem_layout_get() warn: 'layout_np' is an error pointer or valid

vim +/layout_np +1003 drivers/nvmem/core.c

266570f496b90d Michael Walle         2023-04-04  1000  
00d059fd6702f0 Miquel Raynal         2023-08-01  1001  	/* Populate layouts as devices */
00d059fd6702f0 Miquel Raynal         2023-08-01  1002  	layout_np = of_nvmem_layout_get_container(nvmem);
00d059fd6702f0 Miquel Raynal         2023-08-01 @1003  	if (layout_np) {

So, ugh, of_nvmem_layout_get_container() return NULL on error or error
pointer if either CONFIG_NVMEM or CONFIG_OF is turned off.  I feel like
that's a mistake.  Normally when a function returns both error pointers
and NULL then the NULL means the feature is disabled and the error
pointers mean there was an error.  Here it is the opposite.

I have written a blog about this:
https://staticthinking.wordpress.com/2022/08/01/mixing-error-pointers-and-null/

At first I thought that this was to do with CONFIG_COMPILE_TEST but
actually that is disabled.  The issue here is that CONFIG_OF is turned
off.  So this is a genuine bug, we're compiling a module which will
always crash.

So I guess the fix is easy that this should return NULL if either
CONFIG_NVMEM or CONFIG_OF is turned off.  That was a long explanation
which is no longer required now that it's not a COMPILE_TEST issue.  :P

00d059fd6702f0 Miquel Raynal         2023-08-01  1004  		rval = of_platform_populate(nvmem->dev.of_node, NULL, NULL, NULL);
00d059fd6702f0 Miquel Raynal         2023-08-01  1005  		if (rval)
00d059fd6702f0 Miquel Raynal         2023-08-01  1006  			goto err_remove_cells;
00d059fd6702f0 Miquel Raynal         2023-08-01  1007  		of_node_put(layout_np);
00d059fd6702f0 Miquel Raynal         2023-08-01  1008  	}
00d059fd6702f0 Miquel Raynal         2023-08-01  1009  
25c9b5d3aa24a0 Miquel Raynal         2023-08-01  1010  	mutex_lock(&nvmem_devices_mutex);
25c9b5d3aa24a0 Miquel Raynal         2023-08-01  1011  	list_add_tail(&nvmem->node, &nvmem_devices_list);
25c9b5d3aa24a0 Miquel Raynal         2023-08-01  1012  	mutex_unlock(&nvmem_devices_mutex);
25c9b5d3aa24a0 Miquel Raynal         2023-08-01  1013  
f4853e1c321edb Bartosz Golaszewski   2019-02-15  1014  	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
bee1138bea15a6 Bartosz Golaszewski   2018-09-21  1015  
eace75cfdcf7d9 Srinivas Kandagatla   2015-07-27  1016  	return nvmem;
3360acdf839170 Johan Hovold          2017-06-09  1017  
b985f4cba6dbb3 Bartosz Golaszewski   2018-09-21  1018  err_remove_cells:
b985f4cba6dbb3 Bartosz Golaszewski   2018-09-21  1019  	nvmem_device_remove_all_cells(nvmem);
fa72d847d68d78 Bartosz Golaszewski   2018-09-21  1020  	if (config->compat)
ae0c2d725512f3 Srinivas Kandagatla   2019-04-16  1021  		nvmem_sysfs_remove_compat(nvmem, config);
3360acdf839170 Johan Hovold          2017-06-09  1022  err_put_device:
3360acdf839170 Johan Hovold          2017-06-09  1023  	put_device(&nvmem->dev);
3360acdf839170 Johan Hovold          2017-06-09  1024  
b6c217ab9be689 Andrew Lunn           2016-02-26  1025  	return ERR_PTR(rval);
eace75cfdcf7d9 Srinivas Kandagatla   2015-07-27  1026  }
  
Miquel Raynal Aug. 4, 2023, 3:39 p.m. UTC | #2
Hi Dan,

dan.carpenter@linaro.org wrote on Thu, 3 Aug 2023 13:13:04 +0300:

> Hi Miquel,
> 
> kernel test robot noticed the following build warnings:
> 
> https://git-scm.com/docs/git-format-patch#_base_tree_information]
> 
> url:    https://github.com/intel-lab-lkp/linux/commits/Miquel-Raynal/nvmem-core-Create-all-cells-before-adding-the-nvmem-device/20230802-022331
> base:   char-misc/char-misc-testing
> patch link:    https://lore.kernel.org/r/20230801182132.1058707-6-miquel.raynal%40bootlin.com
> patch subject: [PATCH v7 5/7] nvmem: core: Rework layouts to become platform devices
> config: x86_64-randconfig-m001-20230730 (https://download.01.org/0day-ci/archive/20230803/202308030002.DnSFOrMB-lkp@intel.com/config)
> compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
> reproduce: (https://download.01.org/0day-ci/archive/20230803/202308030002.DnSFOrMB-lkp@intel.com/reproduce)
> 
> If you fix the issue in a separate patch/commit
> (i.e. not just a new version of the same patch/commit),

(Nice addition, a lot of newcomers would always add these tags
otherwise.)

> kindly add following tags
> | Reported-by: kernel test robot <lkp@intel.com>
> | Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
> | Closes: https://lore.kernel.org/r/202308030002.DnSFOrMB-lkp@intel.com/
> 
> New smatch warnings:
> drivers/nvmem/core.c:1003 nvmem_register() warn: 'layout_np' is an error pointer or valid
> drivers/nvmem/core.c:2130 nvmem_try_loading_layout_driver() warn: 'layout_np' is an error pointer or valid
> 
> Old smatch warnings:
> drivers/nvmem/core.c:761 nvmem_add_cells_from_fixed_layout() warn: 'layout_np' is an error pointer or valid
> drivers/nvmem/core.c:802 nvmem_layout_get() warn: 'layout_np' is an error pointer or valid
> 
> vim +/layout_np +1003 drivers/nvmem/core.c
> 
> 266570f496b90d Michael Walle         2023-04-04  1000  
> 00d059fd6702f0 Miquel Raynal         2023-08-01  1001  	/* Populate layouts as devices */
> 00d059fd6702f0 Miquel Raynal         2023-08-01  1002  	layout_np = of_nvmem_layout_get_container(nvmem);
> 00d059fd6702f0 Miquel Raynal         2023-08-01 @1003  	if (layout_np) {
> 
> So, ugh, of_nvmem_layout_get_container() return NULL on error or error
> pointer if either CONFIG_NVMEM or CONFIG_OF is turned off.  I feel like
> that's a mistake.  Normally when a function returns both error pointers
> and NULL then the NULL means the feature is disabled and the error
> pointers mean there was an error.  Here it is the opposite.
> 
> I have written a blog about this:
> https://staticthinking.wordpress.com/2022/08/01/mixing-error-pointers-and-null/

Nice (besides the huge spider which stared at me unexpectedly :-) )

> At first I thought that this was to do with CONFIG_COMPILE_TEST but
> actually that is disabled.  The issue here is that CONFIG_OF is turned
> off.  So this is a genuine bug, we're compiling a module which will
> always crash.
> 
> So I guess the fix is easy that this should return NULL if either
> CONFIG_NVMEM or CONFIG_OF is turned off.  That was a long explanation
> which is no longer required now that it's not a COMPILE_TEST issue.  :P

I wanted to disable CONFIG_OF to make the test, I totally forget, I'll
handle this case and return NULL when this happens.

However I don't understand why you mention CONFIG_NVMEM, because if it
is not defined, this file will not compile at all?

> 00d059fd6702f0 Miquel Raynal         2023-08-01  1004  		rval = of_platform_populate(nvmem->dev.of_node, NULL, NULL, NULL);
> 00d059fd6702f0 Miquel Raynal         2023-08-01  1005  		if (rval)
> 00d059fd6702f0 Miquel Raynal         2023-08-01  1006  			goto err_remove_cells;
> 00d059fd6702f0 Miquel Raynal         2023-08-01  1007  		of_node_put(layout_np);
> 00d059fd6702f0 Miquel Raynal         2023-08-01  1008  	}
> 00d059fd6702f0 Miquel Raynal         2023-08-01  1009  
> 25c9b5d3aa24a0 Miquel Raynal         2023-08-01  1010  	mutex_lock(&nvmem_devices_mutex);
> 25c9b5d3aa24a0 Miquel Raynal         2023-08-01  1011  	list_add_tail(&nvmem->node, &nvmem_devices_list);
> 25c9b5d3aa24a0 Miquel Raynal         2023-08-01  1012  	mutex_unlock(&nvmem_devices_mutex);
> 25c9b5d3aa24a0 Miquel Raynal         2023-08-01  1013  
> f4853e1c321edb Bartosz Golaszewski   2019-02-15  1014  	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
> bee1138bea15a6 Bartosz Golaszewski   2018-09-21  1015  
> eace75cfdcf7d9 Srinivas Kandagatla   2015-07-27  1016  	return nvmem;
> 3360acdf839170 Johan Hovold          2017-06-09  1017  
> b985f4cba6dbb3 Bartosz Golaszewski   2018-09-21  1018  err_remove_cells:
> b985f4cba6dbb3 Bartosz Golaszewski   2018-09-21  1019  	nvmem_device_remove_all_cells(nvmem);
> fa72d847d68d78 Bartosz Golaszewski   2018-09-21  1020  	if (config->compat)
> ae0c2d725512f3 Srinivas Kandagatla   2019-04-16  1021  		nvmem_sysfs_remove_compat(nvmem, config);
> 3360acdf839170 Johan Hovold          2017-06-09  1022  err_put_device:
> 3360acdf839170 Johan Hovold          2017-06-09  1023  	put_device(&nvmem->dev);
> 3360acdf839170 Johan Hovold          2017-06-09  1024  
> b6c217ab9be689 Andrew Lunn           2016-02-26  1025  	return ERR_PTR(rval);
> eace75cfdcf7d9 Srinivas Kandagatla   2015-07-27  1026  }
> 


Thanks,
Miquèl
  
Miquel Raynal Aug. 4, 2023, 3:46 p.m. UTC | #3
Hi again Dan,

miquel.raynal@bootlin.com wrote on Fri, 4 Aug 2023 17:39:03 +0200:

> Hi Dan,
> 
> dan.carpenter@linaro.org wrote on Thu, 3 Aug 2023 13:13:04 +0300:
> 
> > Hi Miquel,
> > 
> > kernel test robot noticed the following build warnings:
> > 
> > https://git-scm.com/docs/git-format-patch#_base_tree_information]
> > 
> > url:    https://github.com/intel-lab-lkp/linux/commits/Miquel-Raynal/nvmem-core-Create-all-cells-before-adding-the-nvmem-device/20230802-022331
> > base:   char-misc/char-misc-testing
> > patch link:    https://lore.kernel.org/r/20230801182132.1058707-6-miquel.raynal%40bootlin.com
> > patch subject: [PATCH v7 5/7] nvmem: core: Rework layouts to become platform devices
> > config: x86_64-randconfig-m001-20230730 (https://download.01.org/0day-ci/archive/20230803/202308030002.DnSFOrMB-lkp@intel.com/config)
> > compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
> > reproduce: (https://download.01.org/0day-ci/archive/20230803/202308030002.DnSFOrMB-lkp@intel.com/reproduce)
> > 
> > If you fix the issue in a separate patch/commit
> > (i.e. not just a new version of the same patch/commit),
> 
> (Nice addition, a lot of newcomers would always add these tags
> otherwise.)
> 
> > kindly add following tags
> > | Reported-by: kernel test robot <lkp@intel.com>
> > | Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
> > | Closes: https://lore.kernel.org/r/202308030002.DnSFOrMB-lkp@intel.com/
> > 
> > New smatch warnings:
> > drivers/nvmem/core.c:1003 nvmem_register() warn: 'layout_np' is an error pointer or valid
> > drivers/nvmem/core.c:2130 nvmem_try_loading_layout_driver() warn: 'layout_np' is an error pointer or valid
> > 
> > Old smatch warnings:
> > drivers/nvmem/core.c:761 nvmem_add_cells_from_fixed_layout() warn: 'layout_np' is an error pointer or valid
> > drivers/nvmem/core.c:802 nvmem_layout_get() warn: 'layout_np' is an error pointer or valid
> > 
> > vim +/layout_np +1003 drivers/nvmem/core.c
> > 
> > 266570f496b90d Michael Walle         2023-04-04  1000  
> > 00d059fd6702f0 Miquel Raynal         2023-08-01  1001  	/* Populate layouts as devices */
> > 00d059fd6702f0 Miquel Raynal         2023-08-01  1002  	layout_np = of_nvmem_layout_get_container(nvmem);
> > 00d059fd6702f0 Miquel Raynal         2023-08-01 @1003  	if (layout_np) {
> > 
> > So, ugh, of_nvmem_layout_get_container() return NULL on error or error
> > pointer if either CONFIG_NVMEM or CONFIG_OF is turned off.  I feel like
> > that's a mistake.  Normally when a function returns both error pointers
> > and NULL then the NULL means the feature is disabled and the error
> > pointers mean there was an error.  Here it is the opposite.
> > 
> > I have written a blog about this:
> > https://staticthinking.wordpress.com/2022/08/01/mixing-error-pointers-and-null/
> 
> Nice (besides the huge spider which stared at me unexpectedly :-) )
> 
> > At first I thought that this was to do with CONFIG_COMPILE_TEST but
> > actually that is disabled.  The issue here is that CONFIG_OF is turned
> > off.  So this is a genuine bug, we're compiling a module which will
> > always crash.
> > 
> > So I guess the fix is easy that this should return NULL if either
> > CONFIG_NVMEM or CONFIG_OF is turned off.  That was a long explanation
> > which is no longer required now that it's not a COMPILE_TEST issue.  :P
> 
> I wanted to disable CONFIG_OF to make the test, I totally forget, I'll
> handle this case and return NULL when this happens.

Actually of_nvmem_layout_get_container() already returns NULL if
CONFIG_OF is not defined. This helper returns either a valid pointer of
NULL. Where can it return an error pointer?

> However I don't understand why you mention CONFIG_NVMEM, because if it
> is not defined, this file will not compile at all?
> 
> > 00d059fd6702f0 Miquel Raynal         2023-08-01  1004  		rval = of_platform_populate(nvmem->dev.of_node, NULL, NULL, NULL);
> > 00d059fd6702f0 Miquel Raynal         2023-08-01  1005  		if (rval)
> > 00d059fd6702f0 Miquel Raynal         2023-08-01  1006  			goto err_remove_cells;
> > 00d059fd6702f0 Miquel Raynal         2023-08-01  1007  		of_node_put(layout_np);
> > 00d059fd6702f0 Miquel Raynal         2023-08-01  1008  	}
> > 00d059fd6702f0 Miquel Raynal         2023-08-01  1009  
> > 25c9b5d3aa24a0 Miquel Raynal         2023-08-01  1010  	mutex_lock(&nvmem_devices_mutex);
> > 25c9b5d3aa24a0 Miquel Raynal         2023-08-01  1011  	list_add_tail(&nvmem->node, &nvmem_devices_list);
> > 25c9b5d3aa24a0 Miquel Raynal         2023-08-01  1012  	mutex_unlock(&nvmem_devices_mutex);
> > 25c9b5d3aa24a0 Miquel Raynal         2023-08-01  1013  
> > f4853e1c321edb Bartosz Golaszewski   2019-02-15  1014  	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
> > bee1138bea15a6 Bartosz Golaszewski   2018-09-21  1015  
> > eace75cfdcf7d9 Srinivas Kandagatla   2015-07-27  1016  	return nvmem;
> > 3360acdf839170 Johan Hovold          2017-06-09  1017  
> > b985f4cba6dbb3 Bartosz Golaszewski   2018-09-21  1018  err_remove_cells:
> > b985f4cba6dbb3 Bartosz Golaszewski   2018-09-21  1019  	nvmem_device_remove_all_cells(nvmem);
> > fa72d847d68d78 Bartosz Golaszewski   2018-09-21  1020  	if (config->compat)
> > ae0c2d725512f3 Srinivas Kandagatla   2019-04-16  1021  		nvmem_sysfs_remove_compat(nvmem, config);
> > 3360acdf839170 Johan Hovold          2017-06-09  1022  err_put_device:
> > 3360acdf839170 Johan Hovold          2017-06-09  1023  	put_device(&nvmem->dev);
> > 3360acdf839170 Johan Hovold          2017-06-09  1024  
> > b6c217ab9be689 Andrew Lunn           2016-02-26  1025  	return ERR_PTR(rval);
> > eace75cfdcf7d9 Srinivas Kandagatla   2015-07-27  1026  }
> > 
> 
> 
> Thanks,
> Miquèl


Thanks,
Miquèl
  
Dan Carpenter Aug. 4, 2023, 4:02 p.m. UTC | #4
On Fri, Aug 04, 2023 at 05:39:03PM +0200, Miquel Raynal wrote:
> Hi Dan,
> 
> dan.carpenter@linaro.org wrote on Thu, 3 Aug 2023 13:13:04 +0300:
> 
> > Hi Miquel,
> > 
> > kernel test robot noticed the following build warnings:
> > 
> > https://git-scm.com/docs/git-format-patch#_base_tree_information]
> > 
> > url:    https://github.com/intel-lab-lkp/linux/commits/Miquel-Raynal/nvmem-core-Create-all-cells-before-adding-the-nvmem-device/20230802-022331
> > base:   char-misc/char-misc-testing
> > patch link:    https://lore.kernel.org/r/20230801182132.1058707-6-miquel.raynal%40bootlin.com
> > patch subject: [PATCH v7 5/7] nvmem: core: Rework layouts to become platform devices
> > config: x86_64-randconfig-m001-20230730 (https://download.01.org/0day-ci/archive/20230803/202308030002.DnSFOrMB-lkp@intel.com/config)
> > compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
> > reproduce: (https://download.01.org/0day-ci/archive/20230803/202308030002.DnSFOrMB-lkp@intel.com/reproduce)
> > 
> > If you fix the issue in a separate patch/commit
> > (i.e. not just a new version of the same patch/commit),
> 
> (Nice addition, a lot of newcomers would always add these tags
> otherwise.)

The Intel kbuild devs add this stuff, I just look it over and hit
forward.

> 
> > kindly add following tags
> > | Reported-by: kernel test robot <lkp@intel.com>
> > | Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
> > | Closes: https://lore.kernel.org/r/202308030002.DnSFOrMB-lkp@intel.com/
> > 
> > New smatch warnings:
> > drivers/nvmem/core.c:1003 nvmem_register() warn: 'layout_np' is an error pointer or valid
> > drivers/nvmem/core.c:2130 nvmem_try_loading_layout_driver() warn: 'layout_np' is an error pointer or valid
> > 
> > Old smatch warnings:
> > drivers/nvmem/core.c:761 nvmem_add_cells_from_fixed_layout() warn: 'layout_np' is an error pointer or valid
> > drivers/nvmem/core.c:802 nvmem_layout_get() warn: 'layout_np' is an error pointer or valid
> > 
> > vim +/layout_np +1003 drivers/nvmem/core.c
> > 
> > 266570f496b90d Michael Walle         2023-04-04  1000  
> > 00d059fd6702f0 Miquel Raynal         2023-08-01  1001  	/* Populate layouts as devices */
> > 00d059fd6702f0 Miquel Raynal         2023-08-01  1002  	layout_np = of_nvmem_layout_get_container(nvmem);
> > 00d059fd6702f0 Miquel Raynal         2023-08-01 @1003  	if (layout_np) {
> > 
> > So, ugh, of_nvmem_layout_get_container() return NULL on error or error
> > pointer if either CONFIG_NVMEM or CONFIG_OF is turned off.  I feel like
> > that's a mistake.  Normally when a function returns both error pointers
> > and NULL then the NULL means the feature is disabled and the error
> > pointers mean there was an error.  Here it is the opposite.
> > 
> > I have written a blog about this:
> > https://staticthinking.wordpress.com/2022/08/01/mixing-error-pointers-and-null/
> 
> Nice (besides the huge spider which stared at me unexpectedly :-) )

Those are a species of jumping spiders.  They are shiny and golden in
real life, but it never shows up properly in photos.  :)

> 
> > At first I thought that this was to do with CONFIG_COMPILE_TEST but
> > actually that is disabled.  The issue here is that CONFIG_OF is turned
> > off.  So this is a genuine bug, we're compiling a module which will
> > always crash.
> > 
> > So I guess the fix is easy that this should return NULL if either
> > CONFIG_NVMEM or CONFIG_OF is turned off.  That was a long explanation
> > which is no longer required now that it's not a COMPILE_TEST issue.  :P
> 
> I wanted to disable CONFIG_OF to make the test, I totally forget, I'll
> handle this case and return NULL when this happens.
> 
> However I don't understand why you mention CONFIG_NVMEM, because if it
> is not defined, this file will not compile at all?

Yeah.  You're right.  I wrote this email thinking it was a
CONFIG_COMPILE_TEST issue and didn't edit it properly in the end.

regards,
dan carpenter
  
Dan Carpenter Aug. 4, 2023, 4:05 p.m. UTC | #5
On Fri, Aug 04, 2023 at 05:46:25PM +0200, Miquel Raynal wrote:
> Actually of_nvmem_layout_get_container() already returns NULL if
> CONFIG_OF is not defined. This helper returns either a valid pointer of
> NULL. Where can it return an error pointer?

This is from linux-next.

include/linux/nvmem-consumer.h
   256  static inline struct device_node *
   257  of_nvmem_layout_get_container(struct nvmem_device *nvmem)
   258  {
   259          return ERR_PTR(-EOPNOTSUPP);
   260  }
   261  #endif /* CONFIG_NVMEM && CONFIG_OF */

regards,
dan carpenter
  
Miquel Raynal Aug. 4, 2023, 4:47 p.m. UTC | #6
Hi Dan,

dan.carpenter@linaro.org wrote on Fri, 4 Aug 2023 19:05:20 +0300:

> On Fri, Aug 04, 2023 at 05:46:25PM +0200, Miquel Raynal wrote:
> > Actually of_nvmem_layout_get_container() already returns NULL if
> > CONFIG_OF is not defined. This helper returns either a valid pointer of
> > NULL. Where can it return an error pointer?  
> 
> This is from linux-next.

Ah! Ok I missed that one, we don't need this double implementation, as
of_nvmem_layout_get_container just calls a function that already has a
static inline counterpart in the of.h header, which returns NULL. I'll
make the change, thanks for the pointer.

> 
> include/linux/nvmem-consumer.h
>    256  static inline struct device_node *
>    257  of_nvmem_layout_get_container(struct nvmem_device *nvmem)
>    258  {
>    259          return ERR_PTR(-EOPNOTSUPP);
>    260  }
>    261  #endif /* CONFIG_NVMEM && CONFIG_OF */
> 
> regards,
> dan carpenter


Thanks,
Miquèl
  

Patch

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 0406fbc7b750..868a6f8b3227 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -80,6 +80,7 @@  static LIST_HEAD(nvmem_lookup_list);
 static DEFINE_MUTEX(nvmem_devices_mutex);
 static LIST_HEAD(nvmem_devices_list);
 
+struct notifier_block nvmem_nb;
 static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
 
 static DEFINE_SPINLOCK(nvmem_layout_lock);
@@ -795,23 +796,16 @@  EXPORT_SYMBOL_GPL(nvmem_layout_unregister);
 static struct nvmem_layout *nvmem_layout_get(struct nvmem_device *nvmem)
 {
 	struct device_node *layout_np;
-	struct nvmem_layout *l, *layout = ERR_PTR(-EPROBE_DEFER);
+	struct nvmem_layout *l, *layout = NULL;
 
 	layout_np = of_nvmem_layout_get_container(nvmem);
 	if (!layout_np)
 		return NULL;
 
-	/*
-	 * In case the nvmem device was built-in while the layout was built as a
-	 * module, we shall manually request the layout driver loading otherwise
-	 * we'll never have any match.
-	 */
-	of_request_module(layout_np);
-
 	spin_lock(&nvmem_layout_lock);
 
 	list_for_each_entry(l, &nvmem_layouts, node) {
-		if (of_match_node(l->of_match_table, layout_np)) {
+		if (of_match_node(l->dev->driver->of_match_table, layout_np)) {
 			if (try_module_get(l->owner))
 				layout = l;
 
@@ -868,7 +862,7 @@  const void *nvmem_layout_get_match_data(struct nvmem_device *nvmem,
 	const struct of_device_id *match;
 
 	layout_np = of_nvmem_layout_get_container(nvmem);
-	match = of_match_node(layout->of_match_table, layout_np);
+	match = of_match_node(layout->dev->driver->of_match_table, layout_np);
 
 	return match ? match->data : NULL;
 }
@@ -887,6 +881,7 @@  EXPORT_SYMBOL_GPL(nvmem_layout_get_match_data);
 struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 {
 	struct nvmem_device *nvmem;
+	struct device_node *layout_np;
 	int rval;
 
 	if (!config->dev)
@@ -979,19 +974,6 @@  struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 			goto err_put_device;
 	}
 
-	/*
-	 * If the driver supplied a layout by config->layout, the module
-	 * pointer will be NULL and nvmem_layout_put() will be a noop.
-	 */
-	nvmem->layout = config->layout ?: nvmem_layout_get(nvmem);
-	if (IS_ERR(nvmem->layout)) {
-		rval = PTR_ERR(nvmem->layout);
-		nvmem->layout = NULL;
-
-		if (rval == -EPROBE_DEFER)
-			goto err_teardown_compat;
-	}
-
 	if (config->cells) {
 		rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
 		if (rval)
@@ -1010,16 +992,21 @@  struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 	if (rval)
 		goto err_remove_cells;
 
-	rval = nvmem_add_cells_from_layout(nvmem);
-	if (rval)
-		goto err_remove_cells;
-
 	dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
 
 	rval = device_add(&nvmem->dev);
 	if (rval)
 		goto err_remove_cells;
 
+	/* Populate layouts as devices */
+	layout_np = of_nvmem_layout_get_container(nvmem);
+	if (layout_np) {
+		rval = of_platform_populate(nvmem->dev.of_node, NULL, NULL, NULL);
+		if (rval)
+			goto err_remove_cells;
+		of_node_put(layout_np);
+	}
+
 	mutex_lock(&nvmem_devices_mutex);
 	list_add_tail(&nvmem->node, &nvmem_devices_list);
 	mutex_unlock(&nvmem_devices_mutex);
@@ -1030,8 +1017,6 @@  struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 
 err_remove_cells:
 	nvmem_device_remove_all_cells(nvmem);
-	nvmem_layout_put(nvmem->layout);
-err_teardown_compat:
 	if (config->compat)
 		nvmem_sysfs_remove_compat(nvmem, config);
 err_put_device:
@@ -2137,13 +2122,100 @@  const char *nvmem_dev_name(struct nvmem_device *nvmem)
 }
 EXPORT_SYMBOL_GPL(nvmem_dev_name);
 
+static void nvmem_try_loading_layout_driver(struct nvmem_device *nvmem)
+{
+	struct device_node *layout_np;
+
+	layout_np = of_nvmem_layout_get_container(nvmem);
+	if (layout_np) {
+		of_request_module(layout_np);
+		of_node_put(layout_np);
+	}
+}
+
+static int nvmem_try_matching_available_layout(struct nvmem_device *nvmem)
+{
+	int ret;
+
+	if (nvmem->layout)
+		return 0;
+
+	nvmem->layout = nvmem_layout_get(nvmem);
+	if (!nvmem->layout)
+		return 0;
+
+	ret = nvmem_add_cells_from_layout(nvmem);
+	if (ret)
+		goto put_layout;
+
+	return 0;
+
+put_layout:
+	nvmem_layout_put(nvmem->layout);
+	return ret;
+}
+
+/*
+ * When an NVMEM device is registered, try to match against a layout and
+ * populate the cells. When an NVMEM layout is probed, ensure all NVMEM devices
+ * which could use it properly expose their cells.
+ */
+static int nvmem_notifier_call(struct notifier_block *notifier,
+			       unsigned long event_flags, void *context)
+{
+	struct nvmem_device *nvmem = NULL, *tmp;
+	int ret;
+
+	switch (event_flags) {
+	case NVMEM_ADD:
+		nvmem = context;
+		break;
+	case NVMEM_LAYOUT_ADD:
+		break;
+	default:
+		return NOTIFY_DONE;
+	}
+
+	if (nvmem) {
+		/*
+		 * In case the nvmem device was built-in while the layout was
+		 * built as a module, manually request loading the layout driver.
+		 */
+		nvmem_try_loading_layout_driver(nvmem);
+
+		/* Populate the cells of the new nvmem device from its layout, if any */
+		ret = nvmem_try_matching_available_layout(nvmem);
+		if (ret)
+			return notifier_from_errno(ret);
+
+		return NOTIFY_OK;
+	}
+
+	/* NVMEM devices might be "waiting" for this layout */
+	list_for_each_entry_safe(nvmem, tmp, &nvmem_devices_list, node) {
+		ret = nvmem_try_matching_available_layout(nvmem);
+		if (ret)
+			return notifier_from_errno(ret);
+	}
+
+	return NOTIFY_OK;
+}
+
 static int __init nvmem_init(void)
 {
-	return bus_register(&nvmem_bus_type);
+	int ret;
+
+	ret = bus_register(&nvmem_bus_type);
+	if (ret)
+		return ret;
+
+	nvmem_nb.notifier_call = &nvmem_notifier_call;
+	return nvmem_register_notifier(&nvmem_nb);
 }
 
 static void __exit nvmem_exit(void)
 {
+	nvmem_unregister_notifier(&nvmem_nb);
 	bus_unregister(&nvmem_bus_type);
 }
 
diff --git a/drivers/nvmem/layouts/onie-tlv.c b/drivers/nvmem/layouts/onie-tlv.c
index 59fc87ccfcff..3d54d3be2c93 100644
--- a/drivers/nvmem/layouts/onie-tlv.c
+++ b/drivers/nvmem/layouts/onie-tlv.c
@@ -13,6 +13,7 @@ 
 #include <linux/nvmem-consumer.h>
 #include <linux/nvmem-provider.h>
 #include <linux/of.h>
+#include <linux/platform_device.h>
 
 #define ONIE_TLV_MAX_LEN 2048
 #define ONIE_TLV_CRC_FIELD_SZ 6
@@ -226,18 +227,46 @@  static int onie_tlv_parse_table(struct device *dev, struct nvmem_device *nvmem,
 	return 0;
 }
 
+static int onie_tlv_probe(struct platform_device *pdev)
+{
+	struct nvmem_layout *layout;
+
+	layout = devm_kzalloc(&pdev->dev, sizeof(*layout), GFP_KERNEL);
+	if (!layout)
+		return -ENOMEM;
+
+	layout->add_cells = onie_tlv_parse_table;
+	layout->dev = &pdev->dev;
+
+	platform_set_drvdata(pdev, layout);
+
+	return nvmem_layout_register(layout);
+}
+
+static int onie_tlv_remove(struct platform_device *pdev)
+{
+	struct nvmem_layout *layout = platform_get_drvdata(pdev);
+
+	nvmem_layout_unregister(layout);
+
+	return 0;
+}
+
 static const struct of_device_id onie_tlv_of_match_table[] = {
 	{ .compatible = "onie,tlv-layout", },
 	{},
 };
 MODULE_DEVICE_TABLE(of, onie_tlv_of_match_table);
 
-static struct nvmem_layout onie_tlv_layout = {
-	.name = "ONIE tlv layout",
-	.of_match_table = onie_tlv_of_match_table,
-	.add_cells = onie_tlv_parse_table,
+static struct platform_driver onie_tlv_layout = {
+	.driver = {
+		.name = "onie-tlv-layout",
+		.of_match_table = onie_tlv_of_match_table,
+	},
+	.probe = onie_tlv_probe,
+	.remove = onie_tlv_remove,
 };
-module_nvmem_layout_driver(onie_tlv_layout);
+module_platform_driver(onie_tlv_layout);
 
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Miquel Raynal <miquel.raynal@bootlin.com>");
diff --git a/drivers/nvmem/layouts/sl28vpd.c b/drivers/nvmem/layouts/sl28vpd.c
index 05671371f631..ad0c39fc7943 100644
--- a/drivers/nvmem/layouts/sl28vpd.c
+++ b/drivers/nvmem/layouts/sl28vpd.c
@@ -5,6 +5,7 @@ 
 #include <linux/nvmem-consumer.h>
 #include <linux/nvmem-provider.h>
 #include <linux/of.h>
+#include <linux/platform_device.h>
 #include <uapi/linux/if_ether.h>
 
 #define SL28VPD_MAGIC 'V'
@@ -135,18 +136,46 @@  static int sl28vpd_add_cells(struct device *dev, struct nvmem_device *nvmem,
 	return 0;
 }
 
+static int sl28vpd_probe(struct platform_device *pdev)
+{
+	struct nvmem_layout *layout;
+
+	layout = devm_kzalloc(&pdev->dev, sizeof(*layout), GFP_KERNEL);
+	if (!layout)
+		return -ENOMEM;
+
+	layout->add_cells = sl28vpd_add_cells;
+	layout->dev = &pdev->dev;
+
+	platform_set_drvdata(pdev, layout);
+
+	return nvmem_layout_register(layout);
+}
+
+static int sl28vpd_remove(struct platform_device *pdev)
+{
+	struct nvmem_layout *layout = platform_get_drvdata(pdev);
+
+	nvmem_layout_unregister(layout);
+
+	return 0;
+}
+
 static const struct of_device_id sl28vpd_of_match_table[] = {
 	{ .compatible = "kontron,sl28-vpd" },
 	{},
 };
 MODULE_DEVICE_TABLE(of, sl28vpd_of_match_table);
 
-static struct nvmem_layout sl28vpd_layout = {
-	.name = "sl28-vpd",
-	.of_match_table = sl28vpd_of_match_table,
-	.add_cells = sl28vpd_add_cells,
+static struct platform_driver sl28vpd_layout = {
+	.driver = {
+		.name = "kontron-sl28vpd-layout",
+		.of_match_table = sl28vpd_of_match_table,
+	},
+	.probe = sl28vpd_probe,
+	.remove = sl28vpd_remove,
 };
-module_nvmem_layout_driver(sl28vpd_layout);
+module_platform_driver(sl28vpd_layout);
 
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Michael Walle <michael@walle.cc>");
diff --git a/include/linux/nvmem-provider.h b/include/linux/nvmem-provider.h
index dae26295e6be..c72064780b50 100644
--- a/include/linux/nvmem-provider.h
+++ b/include/linux/nvmem-provider.h
@@ -154,8 +154,7 @@  struct nvmem_cell_table {
 /**
  * struct nvmem_layout - NVMEM layout definitions
  *
- * @name:		Layout name.
- * @of_match_table:	Open firmware match table.
+ * @dev:		Device-model layout device.
  * @add_cells:		Will be called if a nvmem device is found which
  *			has this layout. The function will add layout
  *			specific cells with nvmem_add_one_cell().
@@ -170,8 +169,7 @@  struct nvmem_cell_table {
  * cells.
  */
 struct nvmem_layout {
-	const char *name;
-	const struct of_device_id *of_match_table;
+	struct device *dev;
 	int (*add_cells)(struct device *dev, struct nvmem_device *nvmem,
 			 struct nvmem_layout *layout);
 	void (*fixup_cell_info)(struct nvmem_device *nvmem,
@@ -243,9 +241,4 @@  nvmem_layout_get_match_data(struct nvmem_device *nvmem,
 }
 
 #endif /* CONFIG_NVMEM */
-
-#define module_nvmem_layout_driver(__layout_driver)		\
-	module_driver(__layout_driver, nvmem_layout_register,	\
-		      nvmem_layout_unregister)
-
 #endif  /* ifndef _LINUX_NVMEM_PROVIDER_H */