Skip to content

Commit f6f9db5

Browse files
Dan Carpenterbhe4
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: 3fd2f4bc010c ("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 28d3110 commit f6f9db5

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
@@ -181,27 +181,30 @@ static int idxd_setup_wqs(struct idxd_device *idxd)
181181
idxd->wq_enable_map = bitmap_zalloc_node(idxd->max_wqs, GFP_KERNEL, dev_to_node(dev));
182182
if (!idxd->wq_enable_map) {
183183
rc = -ENOMEM;
184-
goto err_bitmap;
184+
goto err_free_wqs;
185185
}
186186

187187
for (i = 0; i < idxd->max_wqs; i++) {
188188
wq = kzalloc_node(sizeof(*wq), GFP_KERNEL, dev_to_node(dev));
189189
if (!wq) {
190190
rc = -ENOMEM;
191-
goto err;
191+
goto err_unwind;
192192
}
193193

194194
idxd_dev_set_type(&wq->idxd_dev, IDXD_DEV_WQ);
195195
conf_dev = wq_confdev(wq);
196196
wq->id = i;
197197
wq->idxd = idxd;
198-
device_initialize(wq_confdev(wq));
198+
device_initialize(conf_dev);
199199
conf_dev->parent = idxd_confdev(idxd);
200200
conf_dev->bus = &dsa_bus_type;
201201
conf_dev->type = &idxd_wq_device_type;
202202
rc = dev_set_name(conf_dev, "wq%d.%d", idxd->id, wq->id);
203-
if (rc < 0)
204-
goto err;
203+
if (rc < 0) {
204+
put_device(conf_dev);
205+
kfree(wq);
206+
goto err_unwind;
207+
}
205208

206209
mutex_init(&wq->wq_lock);
207210
init_waitqueue_head(&wq->err_queue);
@@ -212,15 +215,20 @@ static int idxd_setup_wqs(struct idxd_device *idxd)
212215
wq->enqcmds_retries = IDXD_ENQCMDS_RETRIES;
213216
wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev));
214217
if (!wq->wqcfg) {
218+
put_device(conf_dev);
219+
kfree(wq);
215220
rc = -ENOMEM;
216-
goto err;
221+
goto err_unwind;
217222
}
218223

219224
if (idxd->hw.wq_cap.op_config) {
220225
wq->opcap_bmap = bitmap_zalloc(IDXD_MAX_OPCAP_BITS, GFP_KERNEL);
221226
if (!wq->opcap_bmap) {
227+
kfree(wq->wqcfg);
228+
put_device(conf_dev);
229+
kfree(wq);
222230
rc = -ENOMEM;
223-
goto err_opcap_bmap;
231+
goto err_unwind;
224232
}
225233
bitmap_copy(wq->opcap_bmap, idxd->opcap_bmap, IDXD_MAX_OPCAP_BITS);
226234
}
@@ -231,13 +239,7 @@ static int idxd_setup_wqs(struct idxd_device *idxd)
231239

232240
return 0;
233241

234-
err_opcap_bmap:
235-
kfree(wq->wqcfg);
236-
237-
err:
238-
put_device(conf_dev);
239-
kfree(wq);
240-
242+
err_unwind:
241243
while (--i >= 0) {
242244
wq = idxd->wqs[i];
243245
if (idxd->hw.wq_cap.op_config)
@@ -246,11 +248,10 @@ static int idxd_setup_wqs(struct idxd_device *idxd)
246248
conf_dev = wq_confdev(wq);
247249
put_device(conf_dev);
248250
kfree(wq);
249-
250251
}
251252
bitmap_free(idxd->wq_enable_map);
252253

253-
err_bitmap:
254+
err_free_wqs:
254255
kfree(idxd->wqs);
255256

256257
return rc;

0 commit comments

Comments
 (0)