From: yusuhao <2025461029@korea.edu> The OTP buffer is allocated on the first NVMEM read and retained as a cache. It is allocated with kzalloc(), while the containing nvm_data structure is managed by devres, so the buffer is not released when the sensor is removed. Register a devres action before registering the NVMEM device. Devres releases actions in reverse order, so the NVMEM device is unregistered before the cached buffer is freed. An AI assistant found this leak and drafted the fix. Fixes: 798f1a6b0efc ("media: ov2740: only do OTP data read on demand from user") Cc: stable@vger.kernel.org Signed-off-by: yusuhao <2025461029@korea.edu> --- drivers/media/i2c/ov2740.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/drivers/media/i2c/ov2740.c b/drivers/media/i2c/ov2740.c index 31513d1524a3..f0e984cfd68a 100644 --- a/drivers/media/i2c/ov2740.c +++ b/drivers/media/i2c/ov2740.c @@ -936,6 +936,13 @@ static int ov2740_load_otp_data(struct nvm_data *nvm) return ret; } +static void ov2740_free_otp_buffer(void *data) +{ + struct nvm_data *nvm = data; + + kfree(nvm->nvm_buffer); +} + static int ov2740_start_streaming(struct ov2740 *ov2740) { const struct ov2740_reg_list *reg_list; @@ -1254,6 +1261,7 @@ static int ov2740_register_nvmem(struct i2c_client *client, struct nvm_data *nvm; struct regmap *regmap; struct device *dev = ov2740->dev; + int ret; struct nvmem_config nvmem_config = { .name = dev_name(dev), .dev = dev, @@ -1279,6 +1287,10 @@ static int ov2740_register_nvmem(struct i2c_client *client, nvm->regmap = regmap; nvmem_config.priv = nvm; + ret = devm_add_action_or_reset(dev, ov2740_free_otp_buffer, nvm); + if (ret) + return ret; + nvm->nvmem = devm_nvmem_register(dev, &nvmem_config); if (IS_ERR(nvm->nvmem)) return PTR_ERR(nvm->nvmem);