Skip to content

Commit 39aaa33

Browse files
Dan Carpentervinodkoul
authored andcommitted
dmaengine: idxd: Fix double free in idxd_setup_wqs()
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 <[email protected]> Closes: https://lore.kernel.org/all/[email protected]/ Signed-off-by: Dan Carpenter <[email protected]> Reviewed-by: Dave Jiang <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Vinod Koul <[email protected]>
1 parent b7cb9a0 commit 39aaa33

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

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

202202
idxd_dev_set_type(&wq->idxd_dev, IDXD_DEV_WQ);
203203
conf_dev = wq_confdev(wq);
204204
wq->id = i;
205205
wq->idxd = idxd;
206-
device_initialize(wq_confdev(wq));
206+
device_initialize(conf_dev);
207207
conf_dev->parent = idxd_confdev(idxd);
208208
conf_dev->bus = &dsa_bus_type;
209209
conf_dev->type = &idxd_wq_device_type;
210210
rc = dev_set_name(conf_dev, "wq%d.%d", idxd->id, wq->id);
211-
if (rc < 0)
212-
goto err;
211+
if (rc < 0) {
212+
put_device(conf_dev);
213+
kfree(wq);
214+
goto err_unwind;
215+
}
213216

214217
mutex_init(&wq->wq_lock);
215218
init_waitqueue_head(&wq->err_queue);
@@ -220,15 +223,20 @@ static int idxd_setup_wqs(struct idxd_device *idxd)
220223
wq->enqcmds_retries = IDXD_ENQCMDS_RETRIES;
221224
wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev));
222225
if (!wq->wqcfg) {
226+
put_device(conf_dev);
227+
kfree(wq);
223228
rc = -ENOMEM;
224-
goto err;
229+
goto err_unwind;
225230
}
226231

227232
if (idxd->hw.wq_cap.op_config) {
228233
wq->opcap_bmap = bitmap_zalloc(IDXD_MAX_OPCAP_BITS, GFP_KERNEL);
229234
if (!wq->opcap_bmap) {
235+
kfree(wq->wqcfg);
236+
put_device(conf_dev);
237+
kfree(wq);
230238
rc = -ENOMEM;
231-
goto err_opcap_bmap;
239+
goto err_unwind;
232240
}
233241
bitmap_copy(wq->opcap_bmap, idxd->opcap_bmap, IDXD_MAX_OPCAP_BITS);
234242
}
@@ -239,13 +247,7 @@ static int idxd_setup_wqs(struct idxd_device *idxd)
239247

240248
return 0;
241249

242-
err_opcap_bmap:
243-
kfree(wq->wqcfg);
244-
245-
err:
246-
put_device(conf_dev);
247-
kfree(wq);
248-
250+
err_unwind:
249251
while (--i >= 0) {
250252
wq = idxd->wqs[i];
251253
if (idxd->hw.wq_cap.op_config)
@@ -254,11 +256,10 @@ static int idxd_setup_wqs(struct idxd_device *idxd)
254256
conf_dev = wq_confdev(wq);
255257
put_device(conf_dev);
256258
kfree(wq);
257-
258259
}
259260
bitmap_free(idxd->wq_enable_map);
260261

261-
err_bitmap:
262+
err_free_wqs:
262263
kfree(idxd->wqs);
263264

264265
return rc;

0 commit comments

Comments
 (0)