Skip to content

Commit ec5430d

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 ce81905 commit ec5430d

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
@@ -187,27 +187,30 @@ static int idxd_setup_wqs(struct idxd_device *idxd)
187187
idxd->wq_enable_map = bitmap_zalloc_node(idxd->max_wqs, GFP_KERNEL, dev_to_node(dev));
188188
if (!idxd->wq_enable_map) {
189189
rc = -ENOMEM;
190-
goto err_bitmap;
190+
goto err_free_wqs;
191191
}
192192

193193
for (i = 0; i < idxd->max_wqs; i++) {
194194
wq = kzalloc_node(sizeof(*wq), GFP_KERNEL, dev_to_node(dev));
195195
if (!wq) {
196196
rc = -ENOMEM;
197-
goto err;
197+
goto err_unwind;
198198
}
199199

200200
idxd_dev_set_type(&wq->idxd_dev, IDXD_DEV_WQ);
201201
conf_dev = wq_confdev(wq);
202202
wq->id = i;
203203
wq->idxd = idxd;
204-
device_initialize(wq_confdev(wq));
204+
device_initialize(conf_dev);
205205
conf_dev->parent = idxd_confdev(idxd);
206206
conf_dev->bus = &dsa_bus_type;
207207
conf_dev->type = &idxd_wq_device_type;
208208
rc = dev_set_name(conf_dev, "wq%d.%d", idxd->id, wq->id);
209-
if (rc < 0)
210-
goto err;
209+
if (rc < 0) {
210+
put_device(conf_dev);
211+
kfree(wq);
212+
goto err_unwind;
213+
}
211214

212215
mutex_init(&wq->wq_lock);
213216
init_waitqueue_head(&wq->err_queue);
@@ -218,15 +221,20 @@ static int idxd_setup_wqs(struct idxd_device *idxd)
218221
wq->enqcmds_retries = IDXD_ENQCMDS_RETRIES;
219222
wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev));
220223
if (!wq->wqcfg) {
224+
put_device(conf_dev);
225+
kfree(wq);
221226
rc = -ENOMEM;
222-
goto err;
227+
goto err_unwind;
223228
}
224229

225230
if (idxd->hw.wq_cap.op_config) {
226231
wq->opcap_bmap = bitmap_zalloc(IDXD_MAX_OPCAP_BITS, GFP_KERNEL);
227232
if (!wq->opcap_bmap) {
233+
kfree(wq->wqcfg);
234+
put_device(conf_dev);
235+
kfree(wq);
228236
rc = -ENOMEM;
229-
goto err_opcap_bmap;
237+
goto err_unwind;
230238
}
231239
bitmap_copy(wq->opcap_bmap, idxd->opcap_bmap, IDXD_MAX_OPCAP_BITS);
232240
}
@@ -237,13 +245,7 @@ static int idxd_setup_wqs(struct idxd_device *idxd)
237245

238246
return 0;
239247

240-
err_opcap_bmap:
241-
kfree(wq->wqcfg);
242-
243-
err:
244-
put_device(conf_dev);
245-
kfree(wq);
246-
248+
err_unwind:
247249
while (--i >= 0) {
248250
wq = idxd->wqs[i];
249251
if (idxd->hw.wq_cap.op_config)
@@ -252,11 +254,10 @@ static int idxd_setup_wqs(struct idxd_device *idxd)
252254
conf_dev = wq_confdev(wq);
253255
put_device(conf_dev);
254256
kfree(wq);
255-
256257
}
257258
bitmap_free(idxd->wq_enable_map);
258259

259-
err_bitmap:
260+
err_free_wqs:
260261
kfree(idxd->wqs);
261262

262263
return rc;

0 commit comments

Comments
 (0)