Skip to content

Commit da7e394

Browse files
committed
gpio: convert linehandle_create() to FD_PREPARE()
Link: https://patch.msgid.link/20251123-work-fd-prepare-v4-38-b6efa1706cfd@kernel.org Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent 6ae8da4 commit da7e394

1 file changed

Lines changed: 21 additions & 45 deletions

File tree

drivers/gpio/gpiolib-cdev.c

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,13 @@ static const struct file_operations linehandle_fileops = {
298298
#endif
299299
};
300300

301+
DEFINE_FREE(linehandle_free, struct linehandle_state *, if (!IS_ERR_OR_NULL(_T)) linehandle_free(_T))
302+
301303
static int linehandle_create(struct gpio_device *gdev, void __user *ip)
302304
{
303305
struct gpiohandle_request handlereq;
304-
struct linehandle_state *lh;
305-
struct file *file;
306-
int fd, i, ret;
306+
struct linehandle_state *lh __free(linehandle_free) = NULL;
307+
int i, ret;
307308
u32 lflags;
308309

309310
if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
@@ -327,10 +328,8 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
327328
lh->label = kstrndup(handlereq.consumer_label,
328329
sizeof(handlereq.consumer_label) - 1,
329330
GFP_KERNEL);
330-
if (!lh->label) {
331-
ret = -ENOMEM;
332-
goto out_free_lh;
333-
}
331+
if (!lh->label)
332+
return -ENOMEM;
334333
}
335334

336335
lh->num_descs = handlereq.lines;
@@ -340,20 +339,18 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
340339
u32 offset = handlereq.lineoffsets[i];
341340
struct gpio_desc *desc = gpio_device_get_desc(gdev, offset);
342341

343-
if (IS_ERR(desc)) {
344-
ret = PTR_ERR(desc);
345-
goto out_free_lh;
346-
}
342+
if (IS_ERR(desc))
343+
return PTR_ERR(desc);
347344

348345
ret = gpiod_request_user(desc, lh->label);
349346
if (ret)
350-
goto out_free_lh;
347+
return ret;
351348
lh->descs[i] = desc;
352349
linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags);
353350

354351
ret = gpiod_set_transitory(desc, false);
355352
if (ret < 0)
356-
goto out_free_lh;
353+
return ret;
357354

358355
/*
359356
* Lines have to be requested explicitly for input
@@ -364,11 +361,11 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
364361

365362
ret = gpiod_direction_output_nonotify(desc, val);
366363
if (ret)
367-
goto out_free_lh;
364+
return ret;
368365
} else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
369366
ret = gpiod_direction_input_nonotify(desc);
370367
if (ret)
371-
goto out_free_lh;
368+
return ret;
372369
}
373370

374371
gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED);
@@ -377,44 +374,23 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
377374
offset);
378375
}
379376

380-
fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
381-
if (fd < 0) {
382-
ret = fd;
383-
goto out_free_lh;
384-
}
377+
FD_PREPARE(fdf, O_RDONLY | O_CLOEXEC,
378+
anon_inode_getfile("gpio-linehandle", &linehandle_fileops,
379+
lh, O_RDONLY | O_CLOEXEC));
380+
if (fdf.err)
381+
return fdf.err;
382+
retain_and_null_ptr(lh);
385383

386-
file = anon_inode_getfile("gpio-linehandle",
387-
&linehandle_fileops,
388-
lh,
389-
O_RDONLY | O_CLOEXEC);
390-
if (IS_ERR(file)) {
391-
ret = PTR_ERR(file);
392-
goto out_put_unused_fd;
393-
}
394-
395-
handlereq.fd = fd;
396-
if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
397-
/*
398-
* fput() will trigger the release() callback, so do not go onto
399-
* the regular error cleanup path here.
400-
*/
401-
fput(file);
402-
put_unused_fd(fd);
384+
handlereq.fd = fd_prepare_fd(fdf);
385+
if (copy_to_user(ip, &handlereq, sizeof(handlereq)))
403386
return -EFAULT;
404-
}
405387

406-
fd_install(fd, file);
388+
fd_publish(fdf);
407389

408390
dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
409391
lh->num_descs);
410392

411393
return 0;
412-
413-
out_put_unused_fd:
414-
put_unused_fd(fd);
415-
out_free_lh:
416-
linehandle_free(lh);
417-
return ret;
418394
}
419395
#endif /* CONFIG_GPIO_CDEV_V1 */
420396

0 commit comments

Comments
 (0)