Skip to content

Commit b66d281

Browse files
KAGA-KOKOkuba-moo
authored andcommitted
ptp: Simplify ptp_read()
The mixture of gotos and direct return codes is inconsistent and just makes the code harder to read. Let it consistently return error codes directly and tidy the code flow up accordingly. No functional change intended. Signed-off-by: Thomas Gleixner <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 4838bc9 commit b66d281

File tree

1 file changed

+16
-38
lines changed

1 file changed

+16
-38
lines changed

drivers/ptp/ptp_chardev.c

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin,
106106

107107
int ptp_open(struct posix_clock_context *pccontext, fmode_t fmode)
108108
{
109-
struct ptp_clock *ptp =
110-
container_of(pccontext->clk, struct ptp_clock, clock);
109+
struct ptp_clock *ptp = container_of(pccontext->clk, struct ptp_clock, clock);
111110
struct timestamp_event_queue *queue;
112111
char debugfsname[32];
113112

@@ -536,67 +535,46 @@ __poll_t ptp_poll(struct posix_clock_context *pccontext, struct file *fp,
536535
ssize_t ptp_read(struct posix_clock_context *pccontext, uint rdflags,
537536
char __user *buf, size_t cnt)
538537
{
539-
struct ptp_clock *ptp =
540-
container_of(pccontext->clk, struct ptp_clock, clock);
538+
struct ptp_clock *ptp = container_of(pccontext->clk, struct ptp_clock, clock);
541539
struct timestamp_event_queue *queue;
542540
struct ptp_extts_event *event;
543-
int result;
541+
ssize_t result;
544542

545543
queue = pccontext->private_clkdata;
546-
if (!queue) {
547-
result = -EINVAL;
548-
goto exit;
549-
}
544+
if (!queue)
545+
return -EINVAL;
550546

551-
if (cnt % sizeof(struct ptp_extts_event) != 0) {
552-
result = -EINVAL;
553-
goto exit;
554-
}
547+
if (cnt % sizeof(*event) != 0)
548+
return -EINVAL;
555549

556550
if (cnt > EXTTS_BUFSIZE)
557551
cnt = EXTTS_BUFSIZE;
558552

559-
cnt = cnt / sizeof(struct ptp_extts_event);
560-
561-
if (wait_event_interruptible(ptp->tsev_wq,
562-
ptp->defunct || queue_cnt(queue))) {
553+
if (wait_event_interruptible(ptp->tsev_wq, ptp->defunct || queue_cnt(queue)))
563554
return -ERESTARTSYS;
564-
}
565555

566-
if (ptp->defunct) {
567-
result = -ENODEV;
568-
goto exit;
569-
}
556+
if (ptp->defunct)
557+
return -ENODEV;
570558

571559
event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL);
572-
if (!event) {
573-
result = -ENOMEM;
574-
goto exit;
575-
}
560+
if (!event)
561+
return -ENOMEM;
576562

577563
scoped_guard(spinlock_irq, &queue->lock) {
578-
size_t qcnt = queue_cnt(queue);
579-
580-
if (cnt > qcnt)
581-
cnt = qcnt;
564+
size_t qcnt = min((size_t)queue_cnt(queue), cnt / sizeof(*event));
582565

583-
for (size_t i = 0; i < cnt; i++) {
566+
for (size_t i = 0; i < qcnt; i++) {
584567
event[i] = queue->buf[queue->head];
585568
/* Paired with READ_ONCE() in queue_cnt() */
586569
WRITE_ONCE(queue->head, (queue->head + 1) % PTP_MAX_TIMESTAMPS);
587570
}
571+
cnt = qcnt * sizeof(*event);
588572
}
589573

590-
cnt = cnt * sizeof(struct ptp_extts_event);
591-
592574
result = cnt;
593-
if (copy_to_user(buf, event, cnt)) {
575+
if (copy_to_user(buf, event, cnt))
594576
result = -EFAULT;
595-
goto free_event;
596-
}
597577

598-
free_event:
599578
kfree(event);
600-
exit:
601579
return result;
602580
}

0 commit comments

Comments
 (0)