Skip to content

Commit df82c79

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 f0e4609 commit df82c79

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

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

192192
idxd_dev_set_type(&wq->idxd_dev, IDXD_DEV_WQ);
193193
conf_dev = wq_confdev(wq);
194194
wq->id = i;
195195
wq->idxd = idxd;
196-
device_initialize(wq_confdev(wq));
196+
device_initialize(conf_dev);
197197
conf_dev->parent = idxd_confdev(idxd);
198198
conf_dev->bus = &dsa_bus_type;
199199
conf_dev->type = &idxd_wq_device_type;
200200
rc = dev_set_name(conf_dev, "wq%d.%d", idxd->id, wq->id);
201-
if (rc < 0)
202-
goto err;
201+
if (rc < 0) {
202+
put_device(conf_dev);
203+
kfree(wq);
204+
goto err_unwind;
205+
}
203206

204207
mutex_init(&wq->wq_lock);
205208
init_waitqueue_head(&wq->err_queue);
@@ -210,15 +213,20 @@ static int idxd_setup_wqs(struct idxd_device *idxd)
210213
wq->enqcmds_retries = IDXD_ENQCMDS_RETRIES;
211214
wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev));
212215
if (!wq->wqcfg) {
216+
put_device(conf_dev);
217+
kfree(wq);
213218
rc = -ENOMEM;
214-
goto err;
219+
goto err_unwind;
215220
}
216221

217222
if (idxd->hw.wq_cap.op_config) {
218223
wq->opcap_bmap = bitmap_zalloc(IDXD_MAX_OPCAP_BITS, GFP_KERNEL);
219224
if (!wq->opcap_bmap) {
225+
kfree(wq->wqcfg);
226+
put_device(conf_dev);
227+
kfree(wq);
220228
rc = -ENOMEM;
221-
goto err_opcap_bmap;
229+
goto err_unwind;
222230
}
223231
bitmap_copy(wq->opcap_bmap, idxd->opcap_bmap, IDXD_MAX_OPCAP_BITS);
224232
}
@@ -229,13 +237,7 @@ static int idxd_setup_wqs(struct idxd_device *idxd)
229237

230238
return 0;
231239

232-
err_opcap_bmap:
233-
kfree(wq->wqcfg);
234-
235-
err:
236-
put_device(conf_dev);
237-
kfree(wq);
238-
240+
err_unwind:
239241
while (--i >= 0) {
240242
wq = idxd->wqs[i];
241243
if (idxd->hw.wq_cap.op_config)
@@ -244,11 +246,10 @@ static int idxd_setup_wqs(struct idxd_device *idxd)
244246
conf_dev = wq_confdev(wq);
245247
put_device(conf_dev);
246248
kfree(wq);
247-
248249
}
249250
bitmap_free(idxd->wq_enable_map);
250251

251-
err_bitmap:
252+
err_free_wqs:
252253
kfree(idxd->wqs);
253254

254255
return rc;

0 commit comments

Comments
 (0)