public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] pps: fix poll support
@ 2025-03-03  8:02 Denis OSTERLAND-HEIM
  2025-03-03 11:53 ` Rodolfo Giometti
  0 siblings, 1 reply; 4+ messages in thread
From: Denis OSTERLAND-HEIM @ 2025-03-03  8:02 UTC (permalink / raw)
  To: Rodolfo Giometti; +Cc: linux-kernel@vger.kernel.org, stable@vger.linux.org

[-- Attachment #1: Type: text/plain, Size: 2949 bytes --]

[BUG]
A user space program that calls select/poll get always an immediate data
ready-to-read response. As a result the intended use to wait until next
data becomes ready does not work.

User space snippet:

    struct pollfd pollfd = {
      .fd = open("/dev/pps0", O_RDONLY),
      .events = POLLIN|POLLERR,
      .revents = 0 };
    while(1) {
      poll(&pollfd, 1, 2000/*ms*/); // returns immediate, but should wait
      if(revents & EPOLLIN) { // always true
        struct pps_fdata fdata;
        memset(&fdata, 0, sizeof(memdata));
        ioctl(PPS_FETCH, &fdata); // currently fetches data at max speed
      }
    }

[CAUSE]
pps_cdev_poll() returns unconditionally EPOLLIN.

[FIX]
Remember the last fetch event counter and compare this value in
pps_cdev_poll() with most recent event counter
and return 0 if they are equal.

Signed-off-by: Denis OSTERLAND-HEIM <denis.osterland@diehl.com>
Co-developed-by: Rodolfo Giometti <giometti@enneenne.com>
Signed-off-by: Rodolfo Giometti <giometti@enneenne.com>
Fixes: eae9d2ba0cfc ("LinuxPPS: core support")
CC: stable@vger.linux.org # 5.4+
---
 drivers/pps/pps.c          | 11 +++++++++--
 include/linux/pps_kernel.h |  1 +
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/drivers/pps/pps.c b/drivers/pps/pps.c
index 6a02245ea35f..9463232af8d2 100644
--- a/drivers/pps/pps.c
+++ b/drivers/pps/pps.c
@@ -41,6 +41,9 @@ static __poll_t pps_cdev_poll(struct file *file, poll_table *wait)
 
 	poll_wait(file, &pps->queue, wait);
 
+	if (pps->last_fetched_ev == pps->last_ev)
+		return 0;
+
 	return EPOLLIN | EPOLLRDNORM;
 }
 
@@ -186,9 +189,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 +277,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;
diff --git a/include/linux/pps_kernel.h b/include/linux/pps_kernel.h
index c7abce28ed29..aab0aebb529e 100644
--- a/include/linux/pps_kernel.h
+++ b/include/linux/pps_kernel.h
@@ -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 */
-- 
2.47.2

[-- Attachment #2: smime.p7s --]
[-- Type: application/x-pkcs7-signature, Size: 6038 bytes --]

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH] pps: fix poll support
@ 2025-03-03  8:05 Denis OSTERLAND-HEIM
  0 siblings, 0 replies; 4+ messages in thread
From: Denis OSTERLAND-HEIM @ 2025-03-03  8:05 UTC (permalink / raw)
  To: Rodolfo Giometti; +Cc: linux-kernel@vger.kernel.org, stable@vger.kernel.org

[BUG]
A user space program that calls select/poll get always an immediate data
ready-to-read response. As a result the intended use to wait until next
data becomes ready does not work.

User space snippet:

    struct pollfd pollfd = {
      .fd = open("/dev/pps0", O_RDONLY),
      .events = POLLIN|POLLERR,
      .revents = 0 };
    while(1) {
      poll(&pollfd, 1, 2000/*ms*/); // returns immediate, but should wait
      if(revents & EPOLLIN) { // always true
        struct pps_fdata fdata;
        memset(&fdata, 0, sizeof(memdata));
        ioctl(PPS_FETCH, &fdata); // currently fetches data at max speed
      }
    }

[CAUSE]
pps_cdev_poll() returns unconditionally EPOLLIN.

[FIX]
Remember the last fetch event counter and compare this value in
pps_cdev_poll() with most recent event counter
and return 0 if they are equal.

Signed-off-by: Denis OSTERLAND-HEIM <denis.osterland@diehl.com>
Co-developed-by: Rodolfo Giometti <giometti@enneenne.com>
Signed-off-by: Rodolfo Giometti <giometti@enneenne.com>
Fixes: eae9d2ba0cfc ("LinuxPPS: core support")
CC: stable@vger.kernel.org # 5.4+
---
 drivers/pps/pps.c          | 11 +++++++++--
 include/linux/pps_kernel.h |  1 +
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/drivers/pps/pps.c b/drivers/pps/pps.c
index 6a02245ea35f..9463232af8d2 100644
--- a/drivers/pps/pps.c
+++ b/drivers/pps/pps.c
@@ -41,6 +41,9 @@ static __poll_t pps_cdev_poll(struct file *file, poll_table *wait)

 poll_wait(file, &pps->queue, wait);

+if (pps->last_fetched_ev == pps->last_ev)
+return 0;
+
 return EPOLLIN | EPOLLRDNORM;
 }

@@ -186,9 +189,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 +277,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;
diff --git a/include/linux/pps_kernel.h b/include/linux/pps_kernel.h
index c7abce28ed29..aab0aebb529e 100644
--- a/include/linux/pps_kernel.h
+++ b/include/linux/pps_kernel.h
@@ -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 */
--
2.47.2
Diehl Metering GmbH, Donaustrasse 120, 90451 Nuernberg
Sitz der Gesellschaft: Ansbach, Registergericht: Ansbach HRB 69
Geschaeftsfuehrer: Dr. Christof Bosbach (Sprecher), Dipl.-Dolm. Annette Geuther, Dipl.-Kfm. Reiner Edel, Jean-Claude Luttringer

Bitte denken Sie an die Umwelt, bevor Sie diese E-Mail drucken. Diese E-Mail kann vertrauliche Informationen enthalten. Sollten die in dieser E-Mail enthaltenen Informationen nicht für Sie bestimmt sein, informieren Sie bitte unverzueglich den Absender per E-Mail und loeschen Sie diese E-Mail in Ihrem System. Jede unberechtigte Form der Reproduktion, Bekanntgabe, Aenderung, Verteilung und/oder Publikation dieser E-Mail ist strengstens untersagt. Informationen zum Datenschutz finden Sie auf unserer Homepage<https://www.diehl.com/metering/de/impressum-und-rechtliche-hinweise/>.

Before printing, think about environmental responsibility.This message may contain confidential information. If you are not authorized to receive this information please advise the sender immediately by reply e-mail and delete this message without making any copies. Any form of unauthorized use, publication, reproduction, copying or disclosure of the e-mail is not permitted. Information about data protection can be found on our homepage<https://www.diehl.com/metering/en/data-protection/>.

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] pps: fix poll support
  2025-03-03  8:02 [PATCH] pps: fix poll support Denis OSTERLAND-HEIM
@ 2025-03-03 11:53 ` Rodolfo Giometti
  0 siblings, 0 replies; 4+ messages in thread
From: Rodolfo Giometti @ 2025-03-03 11:53 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Andrew Morton
  Cc: linux-kernel@vger.kernel.org, Denis OSTERLAND-HEIM, stable

On 03/03/25 09:02, Denis OSTERLAND-HEIM wrote:
> [BUG]
> A user space program that calls select/poll get always an immediate data
> ready-to-read response. As a result the intended use to wait until next
> data becomes ready does not work.
> 
> User space snippet:
> 
>      struct pollfd pollfd = {
>        .fd = open("/dev/pps0", O_RDONLY),
>        .events = POLLIN|POLLERR,
>        .revents = 0 };
>      while(1) {
>        poll(&pollfd, 1, 2000/*ms*/); // returns immediate, but should wait
>        if(revents & EPOLLIN) { // always true
>          struct pps_fdata fdata;
>          memset(&fdata, 0, sizeof(memdata));
>          ioctl(PPS_FETCH, &fdata); // currently fetches data at max speed
>        }
>      }
> 
> [CAUSE]
> pps_cdev_poll() returns unconditionally EPOLLIN.
> 
> [FIX]
> Remember the last fetch event counter and compare this value in
> pps_cdev_poll() with most recent event counter
> and return 0 if they are equal.
> 
> Signed-off-by: Denis OSTERLAND-HEIM <denis.osterland@diehl.com>
> Co-developed-by: Rodolfo Giometti <giometti@enneenne.com>
> Signed-off-by: Rodolfo Giometti <giometti@enneenne.com>

Acked-by: Rodolfo Giometti <giometti@enneenne.com>

If needed. :)

> Fixes: eae9d2ba0cfc ("LinuxPPS: core support")
> CC: stable@vger.linux.org # 5.4+
> ---
>   drivers/pps/pps.c          | 11 +++++++++--
>   include/linux/pps_kernel.h |  1 +
>   2 files changed, 10 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/pps/pps.c b/drivers/pps/pps.c
> index 6a02245ea35f..9463232af8d2 100644
> --- a/drivers/pps/pps.c
> +++ b/drivers/pps/pps.c
> @@ -41,6 +41,9 @@ static __poll_t pps_cdev_poll(struct file *file, poll_table *wait)
>   
>   	poll_wait(file, &pps->queue, wait);
>   
> +	if (pps->last_fetched_ev == pps->last_ev)
> +		return 0;
> +
>   	return EPOLLIN | EPOLLRDNORM;
>   }
>   
> @@ -186,9 +189,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 +277,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;
> diff --git a/include/linux/pps_kernel.h b/include/linux/pps_kernel.h
> index c7abce28ed29..aab0aebb529e 100644
> --- a/include/linux/pps_kernel.h
> +++ b/include/linux/pps_kernel.h
> @@ -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 */

-- 
GNU/Linux Solutions                  e-mail: giometti@enneenne.com
Linux Device Driver                          giometti@linux.it
Embedded Systems                     phone:  +39 349 2432127
UNIX programming


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] pps: fix poll support
  2025-04-03  6:06 Denis OSTERLAND-HEIM
@ 2025-04-03  7:49 ` Rodolfo Giometti
  0 siblings, 0 replies; 4+ messages in thread
From: Rodolfo Giometti @ 2025-04-03  7:49 UTC (permalink / raw)
  To: Denis OSTERLAND-HEIM; +Cc: linux-kernel@vger.kernel.org, stable@vger.kernel.org

On 03/04/25 08:06, Denis OSTERLAND-HEIM wrote:
> Hi Rodolfo,
> 
> Do you think that there is a chance that this patch make it in the current merge window?

Honestly, I don't know, it's up to Greg or Andrew to proceed for inclusion.

Ciao,

Rodolfo

> -----Original Message-----
> From: Rodolfo Giometti <giometti@enneenne.com>
> Sent: Monday, March 3, 2025 12:54 PM
> To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>; Andrew Morton <akpm@linux-foundation.org>
> Cc: linux-kernel@vger.kernel.org; Denis OSTERLAND-HEIM <denis.osterland@diehl.com>; stable@vger.kernel.org
> Subject: [EXT] Re: [PATCH] pps: fix poll support
> 
> [EXTERNAL EMAIL]
>   
> 
> On 03/03/25 09:02, Denis OSTERLAND-HEIM wrote:
>> [BUG]
>> A user space program that calls select/poll get always an immediate data
>> ready-to-read response. As a result the intended use to wait until next
>> data becomes ready does not work.
>>
>> User space snippet:
>>
>>       struct pollfd pollfd = {
>>         .fd = open("/dev/pps0", O_RDONLY),
>>         .events = POLLIN|POLLERR,
>>         .revents = 0 };
>>       while(1) {
>>         poll(&pollfd, 1, 2000/*ms*/); // returns immediate, but should wait
>>         if(revents & EPOLLIN) { // always true
>>           struct pps_fdata fdata;
>>           memset(&fdata, 0, sizeof(memdata));
>>           ioctl(PPS_FETCH, &fdata); // currently fetches data at max speed
>>         }
>>       }
>>
>> [CAUSE]
>> pps_cdev_poll() returns unconditionally EPOLLIN.
>>
>> [FIX]
>> Remember the last fetch event counter and compare this value in
>> pps_cdev_poll() with most recent event counter
>> and return 0 if they are equal.
>>
>> Signed-off-by: Denis OSTERLAND-HEIM <denis.osterland@diehl.com>
>> Co-developed-by: Rodolfo Giometti <giometti@enneenne.com>
>> Signed-off-by: Rodolfo Giometti <giometti@enneenne.com>
> 
> Acked-by: Rodolfo Giometti <giometti@enneenne.com>
> 
> If needed. :)
> 
>> Fixes: eae9d2ba0cfc ("LinuxPPS: core support")
>> CC: stable@vger.linux.org # 5.4+
>> ---
>>    drivers/pps/pps.c          | 11 +++++++++--
>>    include/linux/pps_kernel.h |  1 +
>>    2 files changed, 10 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/pps/pps.c b/drivers/pps/pps.c
>> index 6a02245ea35f..9463232af8d2 100644
>> --- a/drivers/pps/pps.c
>> +++ b/drivers/pps/pps.c
>> @@ -41,6 +41,9 @@ static __poll_t pps_cdev_poll(struct file *file, poll_table *wait)
>>    
>>    	poll_wait(file, &pps->queue, wait);
>>    
>> +	if (pps->last_fetched_ev == pps->last_ev)
>> +		return 0;
>> +
>>    	return EPOLLIN | EPOLLRDNORM;
>>    }
>>    
>> @@ -186,9 +189,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 +277,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;
>> diff --git a/include/linux/pps_kernel.h b/include/linux/pps_kernel.h
>> index c7abce28ed29..aab0aebb529e 100644
>> --- a/include/linux/pps_kernel.h
>> +++ b/include/linux/pps_kernel.h
>> @@ -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 */
> 

-- 
GNU/Linux Solutions                  e-mail: giometti@enneenne.com
Linux Device Driver                          giometti@linux.it
Embedded Systems                     phone:  +39 349 2432127
UNIX programming


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-04-03  7:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-03  8:02 [PATCH] pps: fix poll support Denis OSTERLAND-HEIM
2025-03-03 11:53 ` Rodolfo Giometti
  -- strict thread matches above, loose matches on Subject: below --
2025-03-03  8:05 Denis OSTERLAND-HEIM
2025-04-03  6:06 Denis OSTERLAND-HEIM
2025-04-03  7:49 ` Rodolfo Giometti

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox