Skip to content

Commit 25e6146

Browse files
Dan Carpentergregkh
authored andcommitted
dmaengine: idxd: Fix double free in idxd_setup_wqs()
[ Upstream commit 39aaa33 ] The clean up in idxd_setup_wqs() has had a couple bugs because the error handling is a bit subtle. It's simpler to just re-write it in a cleaner way. The issues here are: 1) If "idxd->max_wqs" is <= 0 then we call put_device(conf_dev) when "conf_dev" hasn't been initialized. 2) If kzalloc_node() fails then again "conf_dev" is invalid. It's either uninitialized or it points to the "conf_dev" from the previous iteration so it leads to a double free. It's better to free partial loop iterations within the loop and then the unwinding at the end can handle whole loop iterations. I also renamed the labels to describe what the goto does and not where the goto was located. Fixes: 3fd2f4b ("dmaengine: idxd: fix memory leak in error handling path of idxd_setup_wqs") Reported-by: Colin Ian King <colin.i.king@gmail.com> Closes: https://lore.kernel.org/all/20250811095836.1642093-1-colin.i.king@gmail.com/ Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org> Reviewed-by: Dave Jiang <dave.jiang@intel.com> Link: https://lore.kernel.org/r/aJnJW3iYTDDCj9sk@stanley.mountain Signed-off-by: Vinod Koul <vkoul@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 1d9a628 commit 25e6146

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

drivers/dma/idxd/init.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -175,27 +175,30 @@ static int idxd_setup_wqs(struct idxd_device *idxd)
175175
idxd->wq_enable_map = bitmap_zalloc_node(idxd->max_wqs, GFP_KERNEL, dev_to_node(dev));
176176
if (!idxd->wq_enable_map) {
177177
rc = -ENOMEM;
178-
goto err_bitmap;
178+
goto err_free_wqs;
179179
}
180180

181181
for (i = 0; i < idxd->max_wqs; i++) {
182182
wq = kzalloc_node(sizeof(*wq), GFP_KERNEL, dev_to_node(dev));
183183
if (!wq) {
184184
rc = -ENOMEM;
185-
goto err;
185+
goto err_unwind;
186186
}
187187

188188
idxd_dev_set_type(&wq->idxd_dev, IDXD_DEV_WQ);
189189
conf_dev = wq_confdev(wq);
190190
wq->id = i;
191191
wq->idxd = idxd;
192-
device_initialize(wq_confdev(wq));
192+
device_initialize(conf_dev);
193193
conf_dev->parent = idxd_confdev(idxd);
194194
conf_dev->bus = &dsa_bus_type;
195195
conf_dev->type = &idxd_wq_device_type;
196196
rc = dev_set_name(conf_dev, "wq%d.%d", idxd->id, wq->id);
197-
if (rc < 0)
198-
goto err;
197+
if (rc < 0) {
198+
put_device(conf_dev);
199+
kfree(wq);
200+
goto err_unwind;
201+
}
199202

200203
mutex_init(&wq->wq_lock);
201204
init_waitqueue_head(&wq->err_queue);
@@ -206,15 +209,20 @@ static int idxd_setup_wqs(struct idxd_device *idxd)
206209
wq->enqcmds_retries = IDXD_ENQCMDS_RETRIES;
207210
wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev));
208211
if (!wq->wqcfg) {
212+
put_device(conf_dev);
213+
kfree(wq);
209214
rc = -ENOMEM;
210-
goto err;
215+
goto err_unwind;
211216
}
212217

213218
if (idxd->hw.wq_cap.op_config) {
214219
wq->opcap_bmap = bitmap_zalloc(IDXD_MAX_OPCAP_BITS, GFP_KERNEL);
215220
if (!wq->opcap_bmap) {
221+
kfree(wq->wqcfg);
222+
put_device(conf_dev);
223+
kfree(wq);
216224
rc = -ENOMEM;
217-
goto err_opcap_bmap;
225+
goto err_unwind;
218226
}
219227
bitmap_copy(wq->opcap_bmap, idxd->opcap_bmap, IDXD_MAX_OPCAP_BITS);
220228
}
@@ -225,13 +233,7 @@ static int idxd_setup_wqs(struct idxd_device *idxd)
225233

226234
return 0;
227235

228-
err_opcap_bmap:
229-
kfree(wq->wqcfg);
230-
231-
err:
232-
put_device(conf_dev);
233-
kfree(wq);
234-
236+
err_unwind:
235237
while (--i >= 0) {
236238
wq = idxd->wqs[i];
237239
if (idxd->hw.wq_cap.op_config)
@@ -240,11 +242,10 @@ static int idxd_setup_wqs(struct idxd_device *idxd)
240242
conf_dev = wq_confdev(wq);
241243
put_device(conf_dev);
242244
kfree(wq);
243-
244245
}
245246
bitmap_free(idxd->wq_enable_map);
246247

247-
err_bitmap:
248+
err_free_wqs:
248249
kfree(idxd->wqs);
249250

250251
return rc;

0 commit comments

Comments
 (0)