public inbox for linux-nvme@lists.infradead.org
 help / color / mirror / Atom feed
* [PATCH] nvme: Add error path for xa_store in nvme_init_effects
@ 2024-12-02 15:39 Keisuke Nishimura
  2024-12-12  6:28 ` Christoph Hellwig
  0 siblings, 1 reply; 3+ messages in thread
From: Keisuke Nishimura @ 2024-12-02 15:39 UTC (permalink / raw)
  To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg
  Cc: linux-nvme, Keisuke Nishimura

The xa_store() may fail due to memory allocation failure because there
is no guarantee that the index NVME_CSI_NVM is already used. This fix
adds an error handling path to check the return value.

Fixes: cc115cbe12d9 ("nvme: always initialize known command effects")
Signed-off-by: Keisuke Nishimura <keisuke.nishimura@inria.fr>
---
 drivers/nvme/host/core.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
index 758de89b47b4..38eb42f1110e 100644
--- a/drivers/nvme/host/core.c
+++ b/drivers/nvme/host/core.c
@@ -3219,10 +3219,18 @@ static int nvme_init_effects(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
 	}
 
 	if (!ctrl->effects) {
-		ctrl->effects = kzalloc(sizeof(*ctrl->effects), GFP_KERNEL);
-		if (!ctrl->effects)
+		struct nvme_effects_log *effects, *old;
+
+		effects = kzalloc(sizeof(*effects), GFP_KERNEL);
+		if (effects)
 			return -ENOMEM;
-		xa_store(&ctrl->cels, NVME_CSI_NVM, ctrl->effects, GFP_KERNEL);
+
+		old = xa_store(&ctrl->cels, NVME_CSI_NVM, effects, GFP_KERNEL);
+		if (xa_is_err(old)) {
+			kfree(effects);
+			return xa_err(old);
+		}
+		ctrl->effects = effects;
 	}
 
 	nvme_init_known_nvm_effects(ctrl);
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] nvme: Add error path for xa_store in nvme_init_effects
  2024-12-02 15:39 [PATCH] nvme: Add error path for xa_store in nvme_init_effects Keisuke Nishimura
@ 2024-12-12  6:28 ` Christoph Hellwig
  2024-12-16 15:32   ` Keisuke Nishimura
  0 siblings, 1 reply; 3+ messages in thread
From: Christoph Hellwig @ 2024-12-12  6:28 UTC (permalink / raw)
  To: Keisuke Nishimura
  Cc: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
	linux-nvme

On Mon, Dec 02, 2024 at 04:39:22PM +0100, Keisuke Nishimura wrote:
>  	if (!ctrl->effects) {
> +		struct nvme_effects_log *effects, *old;
> +
> +		effects = kzalloc(sizeof(*effects), GFP_KERNEL);
> +		if (effects)
>  			return -ENOMEM;
> +
> +		old = xa_store(&ctrl->cels, NVME_CSI_NVM, effects, GFP_KERNEL);
> +		if (xa_is_err(old)) {
> +			kfree(effects);
> +			return xa_err(old);
> +		}
> +		ctrl->effects = effects;

Please split this into a helper now that is becoming rather complex.

Otherwise this looks fine to me.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] nvme: Add error path for xa_store in nvme_init_effects
  2024-12-12  6:28 ` Christoph Hellwig
@ 2024-12-16 15:32   ` Keisuke Nishimura
  0 siblings, 0 replies; 3+ messages in thread
From: Keisuke Nishimura @ 2024-12-16 15:32 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: Keith Busch, Jens Axboe, Sagi Grimberg, linux-nvme



On 12/12/2024 07:28, Christoph Hellwig wrote:
> On Mon, Dec 02, 2024 at 04:39:22PM +0100, Keisuke Nishimura wrote:
>>   	if (!ctrl->effects) {
>> +		struct nvme_effects_log *effects, *old;
>> +
>> +		effects = kzalloc(sizeof(*effects), GFP_KERNEL);
>> +		if (effects)
>>   			return -ENOMEM;
>> +
>> +		old = xa_store(&ctrl->cels, NVME_CSI_NVM, effects, GFP_KERNEL);
>> +		if (xa_is_err(old)) {
>> +			kfree(effects);
>> +			return xa_err(old);
>> +		}
>> +		ctrl->effects = effects;
> 
> Please split this into a helper now that is becoming rather complex.
> 
> Otherwise this looks fine to me.

Thank you for the review. I will send the new version soon.

Keisuke


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-12-16 15:33 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-02 15:39 [PATCH] nvme: Add error path for xa_store in nvme_init_effects Keisuke Nishimura
2024-12-12  6:28 ` Christoph Hellwig
2024-12-16 15:32   ` Keisuke Nishimura

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox