pps: add epoll support
Hi,
I fixed-up s/pps->fetched_ev/pps->last_fetched_ev/ and tested it.
With one process it works well.
``` # cat /sys/class/pps/pps1/assert; PpsPollTest /dev/pps1; cat /sys/class/pps/pps1/assert 1520599383.268749168#29 assert: 29 time: 1520599383.268749168 assert: 30 time: 1520599384.292728352 assert: 31 time: 1520599385.316788416 1520599385.316788416#31 ```
If I start multiple user space programs data races are visible.
``` # for i in 0 1 2 3 4 5 6; do PpsPollTest /dev/pps1 > log$i & done # sleep 6 # tail log* ==> log0 <== timeout assert: 196 time: 1520599554.276752928 assert: 197 time: 1520599555.300692704 ==> log1 <== assert: 193 time: 1520599551.204723248 timeout assert: 196 time: 1520599554.276752928 ==> log2 <== assert: 195 time: 1520599553.252784688 timeout assert: 198 time: 1520599556.324909152 ==> log3 <== assert: 195 time: 1520599553.252784688 timeout assert: 198 time: 1520599556.324909152 ==> log4 <== assert: 194 time: 1520599552.228723584 assert: 195 time: 1520599553.252784688 timeout ==> log5 <== assert: 194 time: 1520599552.228723584 assert: 195 time: 1520599553.252784688 timeout ==> log6 <== assert: 194 time: 1520599552.228723584 assert: 195 time: 1520599553.252784688 ```
From my point of view it would be great to fix this bug without such an limitation.
Regards, Denis
-----Original Message-----
From: Denis OSTERLAND-HEIM
Sent: Tuesday, February 25, 2025 1:47 PM
To: 'Rodolfo Giometti' <[email protected]>
Cc: [email protected]
Subject: RE: Re: [PATCH] pps: add epoll support
Quoted 17 lines
Hi,
-----Original Message-----
From: Rodolfo Giometti <[email protected]>
Sent: Monday, February 24, 2025 6:39 PM
To: Denis OSTERLAND-HEIM <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] pps: add epoll supportQuoted 8 lines
The idea is to use poll to wait for the next data become available.
The should poll work likewait_event_interruptible(pps->queue, ev != pps->last_ev), wherepoll_wait(file, &pps->queue, wait)already does the first part, but the conditionev != pps->last_evis missing.Poll shall return 0 if ev ==
ps->last_ev
and shall return EPOLLIN if ev !=ps->last_ev; EPOLLRDNORM is equivalent[^1] so better return both
Quoted 7 lines
Quoted 5 lines
In case of
ioctl(PPS_FETCH)ev is stored on the stack.
If two applications access one pps device, they both get the right result, because thewait_eventuses the ev from their stack. To do so withpoll()ev needs to be available via file, because every applications opens a sepate file and the file is passed to poll. That is what my patch does.
It adds a per file storage so that poll has the ev value of the last fetch to compare.I agree, but are you sure that your solution will save you in case of
fork()?
Well, I have not tested it.
I would expect that regular files behave the same and a read in the forked process influences the f_pos in the origin process as well. So this would be not supprising that the fork may "steals" the poll condition.
Quoted 1 line
So, why don't simply add a new variable as below?
If more than one process opens the same pps, they refer all to the same pps_device structure.
They have all their own file struct, but that refer to the same pps_device.
I guess this leads to the situation that only one process sees the last_fetched_ev != last_ev condition, but I will test.
I will give your patch a try and report.
drivers/pps/pps.c static __poll_t pps_cdev_poll(struct file *file, poll_table · line 46
Quoted 13 lines
drivers/pps/pps.c +3
-0@@ -40,8 +40,11 @@ static __poll_t pps_cdev_poll(struct file *file, poll_table *wait)struct pps_device *pps = file->private_data;poll_wait (file, &pps->queue, wait); +if (pps->info.mode &PPS_CANWAIT ) +if (pps->fetched_ev != pps->last_ev) +return EPOLLIN |EPOLLRDNORM ;
maybe leave poll direct with error condition, to signal that this is not support instead of normal timeout behavior
+ if ((pps->info.mode & PPS_CANWAIT) == 0)
+ return EPOLLERR;
+ if (pps->fetched_ev != pps->last_ev)
+ return EPOLLIN | EPOLLRDNORM;
Quoted 53 lines
drivers/pps/pps.c +7
-3-return EPOLLIN |EPOLLRDNORM ; +return 0 ; }static int pps_cdev_fasync (int fd,struct file *file,int on)@@ -186,9 +195,11 @@ static long pps_cdev_ioctl(struct file *file,if (err)return err; -/* Return the fetched timestamp */ +/* Return the fetched timestamp and save last fetched event */ spin_lock_irq (&pps->lock); + pps->last_fetched_ev = pps->last_ev; + fdata.info.assert_sequence = pps->assert_sequence; fdata.info.clear_sequence = pps->clear_sequence; fdata.info.assert_tu = pps->assert_tu;@@ -272,9 +283,11 @@ static long pps_cdev_compat_ioctl(struct file *file,if (err)return err; -/* Return the fetched timestamp */ +/* Return the fetched timestamp and save last fetched event */ spin_lock_irq (&pps->lock); + pps->last_fetched_ev = pps->last_ev; + compat.info.assert_sequence = pps->assert_sequence; compat.info.clear_sequence = pps->clear_sequence; compat.info.current_mode = pps->current_mode;include/linux/pps_kernel.h +1
-0@@ -52,6 +52,7 @@ struct pps_device {int current_mode;/* PPS mode at event time */ unsigned int last_ev;/* last PPS event id */ +unsigned int last_fetched_ev;/* last fetched PPS event id */ wait_queue_head_t queue;/* PPS event queue */ unsigned int id;/* PPS source unique ID */ Quoted 8 lines
If you want to avoid this extra alloc and derefers, we may use file position (file.f_pos) to store last fetched ev value. The pps does not provide read/write, so
f_posis unused anyway.I am a little bit puzzeled about your second diff.
Every pps client that usespps_event()supports WAITEVENT, because this is in the generic part. To me not usingpps_event()but reimplement parts ofpps_event()seems to be a bad idea. That’s why I thing that this diff is not needed.
Quoted 5 lines
All clients specify their PPS information within the struct
pps_source_info, and if for some reason one source doesn't add thePPS_CANWAITflag, we should properly support this setting.However, this is related to the
poll()support, and we can defer it for the moment.
I see. Maybe it is easier to reason about the details when a real need araises.
Quoted 2 lines
Thank you so much for your suggestions and tests! :)
You´re welcome.
Regards, Denis
Quoted 11 lines
Ciao,
Rodolfo
Signature · 4 lines
GNU/Linux Solutions e-mail: [email protected] Linux Device Driver [email protected] Embedded Systems phone: +39 349 2432127 UNIX programming
Quoted 6 lines · Denis OSTERLAND-HEIM
Hi,
I fixed-up s/pps->fetched_ev/pps->last_fetched_ev/ and tested it.
With one process it works well.
Quoted 11 lines
``` # cat /sys/class/pps/pps1/assert; PpsPollTest /dev/pps1; cat /sys/class/pps/pps1/assert 1520599383.268749168#29 assert: 29 time: 1520599383.268749168 assert: 30 time: 1520599384.292728352 assert: 31 time: 1520599385.316788416 1520599385.316788416#31 ```
OK.
Quoted 12 lines
If I start multiple user space programs data races are visible.
```
# for i in 0 1 2 3 4 5 6; do PpsPollTest /dev/pps1 > log$i & done # sleep 6 # tail log* ==> log0 <== timeout assert: 196 time: 1520599554.276752928 assert: 197 time: 1520599555.300692704
This is the same behavior we have when working with a serial port: if more than one process gets access to it, data is stolen.
Quoted 1 line
Quoted 1 line
From my point of view it would be great to fix this bug without such an limitation.
I disagree, it is not a limitation! It is like a normal char device work.
What we have to test now is if your initial goal has been addressed! That is, in an application that has more to do than just dealing with one PPS device, we can use poll()/select() in order to avoid using threads.
Ciao,
Rodolfo
Signature · 4 lines
GNU/Linux Solutions e-mail: [email protected] Linux Device Driver [email protected] Embedded Systems phone: +39 349 2432127 UNIX programming