[PATCH] power: reset: nvmem-reboot-mode: respect cell size for nvmem_cell_write

Alexander Koskovich posted 1 patch 1 month, 3 weeks ago
There is a newer version of this series
drivers/power/reset/nvmem-reboot-mode.c | 14 ++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)
[PATCH] power: reset: nvmem-reboot-mode: respect cell size for nvmem_cell_write
Posted by Alexander Koskovich 1 month, 3 weeks ago
Some platforms expose reboot mode cells that are smaller than an
unsigned int, in which cases lead to write failures. Read the cell
first to determine actual size and only write the number of bytes the
cell can hold.

Fixes: 7a78a7f7695b ("power: reset: nvmem-reboot-mode: use NVMEM as reboot mode write interface")
Signed-off-by: Alexander Koskovich <akoskovich@pm.me>
---
 drivers/power/reset/nvmem-reboot-mode.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/drivers/power/reset/nvmem-reboot-mode.c b/drivers/power/reset/nvmem-reboot-mode.c
index 41530b70cfc4..9627c78e0e86 100644
--- a/drivers/power/reset/nvmem-reboot-mode.c
+++ b/drivers/power/reset/nvmem-reboot-mode.c
@@ -19,12 +19,22 @@ struct nvmem_reboot_mode {
 static int nvmem_reboot_mode_write(struct reboot_mode_driver *reboot,
 				    unsigned int magic)
 {
-	int ret;
 	struct nvmem_reboot_mode *nvmem_rbm;
+	size_t buf_len;
+	void *buf;
+	int ret;
 
 	nvmem_rbm = container_of(reboot, struct nvmem_reboot_mode, reboot);
 
-	ret = nvmem_cell_write(nvmem_rbm->cell, &magic, sizeof(magic));
+	buf = nvmem_cell_read(nvmem_rbm->cell, &buf_len);
+	if (IS_ERR(buf))
+		return PTR_ERR(buf);
+	kfree(buf);
+
+	if (buf_len > sizeof(magic))
+		return -EINVAL;
+
+	ret = nvmem_cell_write(nvmem_rbm->cell, &magic, buf_len);
 	if (ret < 0)
 		dev_err(reboot->dev, "update reboot mode bits failed\n");
 
-- 
2.47.3
Re: [PATCH] power: reset: nvmem-reboot-mode: respect cell size for nvmem_cell_write
Posted by kernel test robot 1 month, 3 weeks ago
Hi Alexander,

kernel test robot noticed the following build errors:

[auto build test ERROR on sre-power-supply/for-next]
[also build test ERROR on amd-pstate/linux-next amd-pstate/bleeding-edge linus/master v6.19-rc1 next-20251212]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Alexander-Koskovich/power-reset-nvmem-reboot-mode-respect-cell-size-for-nvmem_cell_write/20251214-090110
base:   https://git.kernel.org/pub/scm/linux/kernel/git/sre/linux-power-supply.git for-next
patch link:    https://lore.kernel.org/r/20251214005903.2802724-1-akoskovich%40pm.me
patch subject: [PATCH] power: reset: nvmem-reboot-mode: respect cell size for nvmem_cell_write
config: i386-randconfig-004-20251214 (https://download.01.org/0day-ci/archive/20251215/202512150225.njZvGeNB-lkp@intel.com/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251215/202512150225.njZvGeNB-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>
| Closes: https://lore.kernel.org/oe-kbuild-all/202512150225.njZvGeNB-lkp@intel.com/

All errors (new ones prefixed by >>):

>> drivers/power/reset/nvmem-reboot-mode.c:32:2: error: call to undeclared function 'kfree'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
      32 |         kfree(buf);
         |         ^
   1 error generated.


vim +/kfree +32 drivers/power/reset/nvmem-reboot-mode.c

    18	
    19	static int nvmem_reboot_mode_write(struct reboot_mode_driver *reboot,
    20					    unsigned int magic)
    21	{
    22		struct nvmem_reboot_mode *nvmem_rbm;
    23		size_t buf_len;
    24		void *buf;
    25		int ret;
    26	
    27		nvmem_rbm = container_of(reboot, struct nvmem_reboot_mode, reboot);
    28	
    29		buf = nvmem_cell_read(nvmem_rbm->cell, &buf_len);
    30		if (IS_ERR(buf))
    31			return PTR_ERR(buf);
  > 32		kfree(buf);
    33	
    34		if (buf_len > sizeof(magic))
    35			return -EINVAL;
    36	
    37		ret = nvmem_cell_write(nvmem_rbm->cell, &magic, buf_len);
    38		if (ret < 0)
    39			dev_err(reboot->dev, "update reboot mode bits failed\n");
    40	
    41		return ret;
    42	}
    43	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki