Skip to contentSkip to conversation

pps: several fixes and improvements

PATCH Superseded Alexander Gordeev 5 participants 50 messages Nov 18, 2010 – Nov 22, 2010

Subsystems (4)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover pps: several fixes and improvements

This patchset contains several changes that improve an overall design/performance of PPS subsystem. I'd like these patches to be merged mainline if no one objects.

Patches 1-3 are bugfixes.
Patches 4-12 are other improvements to PPS subsystem. Patches 13-15 add kernel consumer support. Patch 16 adds parallel port PPS client.
Patch 17 adds parallel port PPS generator.

You can find description for my previous patchset (it describes patches 13-17 in more detailed) here: http://lkml.org/lkml/2010/2/24/189

This patchset is tested against the vanilla 2.6.36 kernel. But we are actually using it on 2.6.33.7-rt29 rt-preempt kernel most of the time. Those who are interested in other versions of the patchset can find them in my git repository:
git://github.com/ago/linux-2.6.git

There is one problem however: kernel consumer works bad (if enabled) when CONFIG_NO_HZ is enabled. The reason for this is commit a092ff0f90cae22b2ac8028ecd2c6f6c1a9e4601. Without it hardpps() is able to sync to 1us precision in about 10 seconds. With CONFIG_NO_HZ it is not syncing at all. This only affects patches 13-15, others are ok.

Changelog
v3 -> v4:
* add patch 12
* fix memory leak when unregistering pps source
* don't allow to pass NULL timestamp to dcd_change() to simplify the code because this possibility is never used
* decrease SEND_DELAY_MAX from 300ms to 100us because spending 300ms with disabled interrupts is inacceptable while 100us can be tolerated in some setups
* integrate Andrew Morton's patch that replaces create_rt_workqueue() with create_workqueue()
* fix issues pointed out by Vitezslav Samel, Rodolfo Giometti, Joe Perches, John Stultz and Andrew Morton:
* fix possible PPS device freeze due to event counter overflow
* add a workaround for a possible race in tty code to the pps-ldisc module; it replaces a previously used workaround in pps subsystem code which was worse because it affected all PPS clients. The problem with the current tty code is that it uses mutexes for locking, but ldisc reference is taken and used in atomic context in uart_handle_dcd_change() so it can't lock mutex and therefore is race-prone. This issue should be discussed I think, but this patch should be ok for now.
* override pr_fmt(fmt) to automagically print module names
* add comments describing struct pps_normtime
* add arch_gettimeoffset() to raw nanoseconds in getnstime_raw_and_real()
* use WARN_ON_ONCE instead of WARN_ON in getnstime_raw_and_real()
* convert SIGNAL_IS_SET macro in pps_parport to inline function
* add documentation for pps_gen_parport

v2 -> v3:
* add patches 1-11
* add clear_wait parameter to pps_parport
* add delay parameter to pps_gen_parport
* fix seqlock unlocking
* fix issues pointed out by Rodolfo Giometti:
* move CONFIG_NTP_PPS to drivers/pps/Kconfig
* swap parport client and generator patches
* style and typo fixes
* move patch that adds unified timestamp gathering to be the beginning

v1 -> v2:
* fix issues pointed out by John Stultz:
* style fixes
* add a about the authorship of the original code
* replace timespec with pps_normtime struct where timespec is used in a wrong way
* fix seqlock usage in hardpps()
* unbind kernel consumer on device removal
* send raw timestamp instead of the last difference to hardpps() which simplifies the code and is less error-prone
* update comments in the kernel consumer code to match the reality
* split the patch that adds MONOTONIC_RAW timestmaps into two
* other small fixes

Alexander Gordeev (17):
pps: trivial fixes
pps: declare variables where they are used in switch
pps: fix race in PPS_FETCH handler
pps: unify timestamp gathering
pps: access pps device by direct pointer
pps: convert printk/pr_* to dev_*
pps: move idr stuff to pps.c
pps: add async PPS event handler
pps: don't disable interrupts when using spin locks
pps: use BUG_ON for kernel API safety checks
pps: simplify conditions a bit
pps: timestamp is always passed to dcd_change()
ntp: add hardpps implementation
pps: capture MONOTONIC_RAW timestamps as well
pps: add kernel consumer support
pps: add parallel port PPS client
pps: add parallel port PPS signal generator

 Documentation/ioctl/ioctl-number.txt     |    2 +-
 Documentation/pps/pps.txt                |   46 ++++
 Documentation/serial/tty.txt             |    2 +-
 drivers/pps/Kconfig                      |   10 +
 drivers/pps/Makefile                     |    2 +-
 drivers/pps/clients/Kconfig              |    7 +
 drivers/pps/clients/Makefile             |    1 +
 drivers/pps/clients/pps-ktimer.c         |   44 ++--
 drivers/pps/clients/pps-ldisc.c          |   70 +++---
 drivers/pps/clients/pps_parport.c        |  247 +++++++++++++++++
 drivers/pps/generators/Kconfig           |   17 ++
 drivers/pps/generators/Makefile          |    9 +
 drivers/pps/generators/pps_gen_parport.c |  275 +++++++++++++++++++
 drivers/pps/kapi.c                       |  334 +++++++++++------------
 drivers/pps/pps.c                        |  203 +++++++++++---
 include/linux/pps.h                      |    7 +
 include/linux/pps_kernel.h               |   64 ++++-
 include/linux/serial_core.h              |    5 +-
 include/linux/time.h                     |    2 +
 include/linux/timex.h                    |    1 +
 include/linux/tty_ldisc.h                |    7 +-
 kernel/time/ntp.c                        |  425 ++++++++++++++++++++++++++++-
 kernel/time/timekeeping.c                |   38 +++
 23 files changed, 1502 insertions(+), 316 deletions(-)
 create mode 100644 drivers/pps/clients/pps_parport.c
 create mode 100644 drivers/pps/generators/Kconfig
 create mode 100644 drivers/pps/generators/Makefile
 create mode 100644 drivers/pps/generators/pps_gen_parport.c
Signature · 1 line
1.7.2.3
#1 lore raw

PATCH v4 1/17 pps: trivial fixes

Here are some very trivial fixes combined:
* add macro definitions to protect header file from including several times
* remove declaration for an unexistent array
* fix typos

---
 drivers/pps/kapi.c         |    2 +-
 include/linux/pps_kernel.h |    7 ++++++-
 2 files changed, 7 insertions(+), 2 deletions(-)
drivers/pps/kapi.c +1 -1
index 1aa02db..55f3961 100644
@@ -324,7 +324,7 @@ void pps_event(int source, struct pps_ktime *ts, int event, void *data)
 		captured = ~0;
 	}
 
-	/* Wake up iif captured somthing */
+	/* Wake up if captured something */
 	if (captured) {
 		pps->go = ~0;
 		wake_up_interruptible(&pps->queue);
include/linux/pps_kernel.h +6 -1
index e0a193f..c930d11 100644
@@ -18,6 +18,9 @@
  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
+#ifndef LINUX_PPS_KERNEL_H
+#define LINUX_PPS_KERNEL_H
+
 #include <linux/pps.h>
 
 #include <linux/cdev.h>
@@ -71,7 +74,6 @@ struct pps_device {
 
 extern spinlock_t pps_idr_lock;
 extern struct idr pps_idr;
-extern struct timespec pps_irq_ts[];
 
 extern struct device_attribute pps_attrs[];
 
@@ -87,3 +89,6 @@ extern void pps_unregister_source(int source);
 extern int pps_register_cdev(struct pps_device *pps);
 extern void pps_unregister_cdev(struct pps_device *pps);
 extern void pps_event(int source, struct pps_ktime *ts, int event, void *data);
+
+#endif /* LINUX_PPS_KERNEL_H */
+
Signature · 1 line
1.7.2.3
#2 lore raw

PATCH v4 2/17 pps: declare variables where they are used in switch

In reply to Alexander Gordeev

Move variable declarations where they are used in pps_cdev_ioctl.

---
 drivers/pps/pps.c |   10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)
drivers/pps/pps.c +6 -4
index ca5183b..c76afb9 100644
@@ -61,8 +61,6 @@ static long pps_cdev_ioctl(struct file *file,
 {
 	struct pps_device *pps = file->private_data;
 	struct pps_kparams params;
-	struct pps_fdata fdata;
-	unsigned long ticks;
 	void __user *uarg = (void __user *) arg;
 	int __user *iuarg = (int __user *) arg;
 	int err;
@@ -136,7 +134,9 @@ static long pps_cdev_ioctl(struct file *file,
 
 		break;
 
-	case PPS_FETCH:
+	case PPS_FETCH: {
+		struct pps_fdata fdata;
+
 		pr_debug("PPS_FETCH: source %d\n", pps->id);
 
 		err = copy_from_user(&fdata, uarg, sizeof(struct pps_fdata));
@@ -149,6 +149,8 @@ static long pps_cdev_ioctl(struct file *file,
 		if (fdata.timeout.flags & PPS_TIME_INVALID)
 			err = wait_event_interruptible(pps->queue, pps->go);
 		else {
+			unsigned long ticks;
+
 			pr_debug("timeout %lld.%09d\n",
 					(long long) fdata.timeout.sec,
 					fdata.timeout.nsec);
@@ -185,7 +187,7 @@ static long pps_cdev_ioctl(struct file *file,
 			return -EFAULT;
 
 		break;
-
+	}
 	default:
 		return -ENOTTY;
 		break;
Signature · 1 line
1.7.2.3
#3 lore raw

PATCH v4 3/17 pps: fix race in PPS_FETCH handler

In reply to Alexander Gordeev

There was a race in PPS_FETCH ioctl handler when several processes want to obtain PPS data simultaneously using sleeping PPS_FETCH. They all sleep most of the time in the system call.
With the old approach when the first process waiting on the pps queue is waken up it makes new system call right away and zeroes pps->go. So other processes continue to sleep. This is a clear race condition because of the global 'go' variable.
With the new approach pps->last_ev holds some value increasing at each PPS event. PPS_FETCH ioctl handler saves current value to the local variable at the very beginning so it can safely check that there is a new event by just comparing both variables.

---
 drivers/pps/kapi.c         |    4 ++--
 drivers/pps/pps.c          |   10 +++++++---
 include/linux/pps_kernel.h |    2 +-
 3 files changed, 10 insertions(+), 6 deletions(-)
drivers/pps/kapi.c +2 -2
index 55f3961..3f89f5eb 100644
@@ -326,8 +326,8 @@ void pps_event(int source, struct pps_ktime *ts, int event, void *data)
 
 	/* Wake up if captured something */
 	if (captured) {
-		pps->go = ~0;
-		wake_up_interruptible(&pps->queue);
+		pps->last_ev++;
+		wake_up_interruptible_all(&pps->queue);
 
 		kill_fasync(&pps->async_queue, SIGIO, POLL_IN);
 	}
drivers/pps/pps.c +7 -3
index c76afb9..dc7e66c 100644
@@ -136,6 +136,7 @@ static long pps_cdev_ioctl(struct file *file,
 
 	case PPS_FETCH: {
 		struct pps_fdata fdata;
+		unsigned int ev;
 
 		pr_debug("PPS_FETCH: source %d\n", pps->id);
 
@@ -143,11 +144,12 @@ static long pps_cdev_ioctl(struct file *file,
 		if (err)
 			return -EFAULT;
 
-		pps->go = 0;
+		ev = pps->last_ev;
 
 		/* Manage the timeout */
 		if (fdata.timeout.flags & PPS_TIME_INVALID)
-			err = wait_event_interruptible(pps->queue, pps->go);
+			err = wait_event_interruptible(pps->queue,
+					ev != pps->last_ev);
 		else {
 			unsigned long ticks;
 
@@ -159,7 +161,9 @@ static long pps_cdev_ioctl(struct file *file,
 
 			if (ticks != 0) {
 				err = wait_event_interruptible_timeout(
-						pps->queue, pps->go, ticks);
+						pps->queue,
+						ev != pps->last_ev,
+						ticks);
 				if (err == 0)
 					return -ETIMEDOUT;
 			}
include/linux/pps_kernel.h +1 -1
index c930d11..c3aed4b 100644
@@ -55,7 +55,7 @@ struct pps_device {
 	struct pps_ktime clear_tu;
 	int current_mode;			/* PPS mode at event time */
 
-	int go;					/* PPS event is arrived? */
+	unsigned int last_ev;			/* last PPS event id */
 	wait_queue_head_t queue;		/* PPS event queue */
 
 	unsigned int id;			/* PPS source unique ID */
Signature · 1 line
1.7.2.3
maintainer Acked-by on v4 3/17
#4 lore raw

Re: [PATCHv4 03/17] pps: fix race in PPS_FETCH handler

Quoted 88 lines · Alexander Gordeev

There was a race in PPS_FETCH ioctl handler when several processes want to obtain PPS data simultaneously using sleeping PPS_FETCH. They all sleep most of the time in the system call.
With the old approach when the first process waiting on the pps queue is waken up it makes new system call right away and zeroes pps->go. So other processes continue to sleep. This is a clear race condition because of the global 'go' variable.
With the new approach pps->last_ev holds some value increasing at each PPS event. PPS_FETCH ioctl handler saves current value to the local variable at the very beginning so it can safely check that there is a new event by just comparing both variables.

---
 drivers/pps/kapi.c         |    4 ++--
 drivers/pps/pps.c          |   10 +++++++---
 include/linux/pps_kernel.h |    2 +-
 3 files changed, 10 insertions(+), 6 deletions(-)
drivers/pps/kapi.c +2 -2
index 55f3961..3f89f5eb 100644
@@ -326,8 +326,8 @@ void pps_event(int source, struct pps_ktime *ts, int event, void *data)
 
 	/* Wake up if captured something */
 	if (captured) {
-		pps->go = ~0;
-		wake_up_interruptible(&pps->queue);
+		pps->last_ev++;
+		wake_up_interruptible_all(&pps->queue);
 
 		kill_fasync(&pps->async_queue, SIGIO, POLL_IN);
 	}
drivers/pps/pps.c +7 -3
index c76afb9..dc7e66c 100644
@@ -136,6 +136,7 @@ static long pps_cdev_ioctl(struct file *file,
 
 	case PPS_FETCH: {
 		struct pps_fdata fdata;
+		unsigned int ev;
 
 		pr_debug("PPS_FETCH: source %d\n", pps->id);
 
@@ -143,11 +144,12 @@ static long pps_cdev_ioctl(struct file *file,
 		if (err)
 			return -EFAULT;
 
-		pps->go = 0;
+		ev = pps->last_ev;
 
 		/* Manage the timeout */
 		if (fdata.timeout.flags & PPS_TIME_INVALID)
-			err = wait_event_interruptible(pps->queue, pps->go);
+			err = wait_event_interruptible(pps->queue,
+					ev != pps->last_ev);
 		else {
 			unsigned long ticks;
 
@@ -159,7 +161,9 @@ static long pps_cdev_ioctl(struct file *file,
 
 			if (ticks != 0) {
 				err = wait_event_interruptible_timeout(
-						pps->queue, pps->go, ticks);
+						pps->queue,
+						ev != pps->last_ev,
+						ticks);
 				if (err == 0)
 					return -ETIMEDOUT;
 			}
include/linux/pps_kernel.h +1 -1
index c930d11..c3aed4b 100644
@@ -55,7 +55,7 @@ struct pps_device {
 	struct pps_ktime clear_tu;
 	int current_mode;			/* PPS mode at event time */
 
-	int go;					/* PPS event is arrived? */
+	unsigned int last_ev;			/* last PPS event id */
 	wait_queue_head_t queue;		/* PPS event queue */
 
 	unsigned int id;			/* PPS source unique ID */
Signature · 1 line
1.7.2.3
Signature · 5 lines
GNU/Linux Solutions                  e-mail: [email protected]
Linux Device Driver                          [email protected]
Embedded Systems                     phone:  +39 349 2432127
UNIX programming                     skype:  rodolfo.giometti
Freelance ICT Italia - Consulente ICT Italia - www.consulenti-ict.it
#5 lore raw

PATCH v4 4/17 pps: unify timestamp gathering

In reply to Alexander Gordeev

Add a helper function to gather timestamps. This way clients don't have to duplicate it.

---
 drivers/pps/clients/pps-ktimer.c |    9 ++-------
 drivers/pps/clients/pps-ldisc.c  |   18 ++++++------------
 drivers/pps/kapi.c               |   19 ++++++++++++-------
 include/linux/pps_kernel.h       |   19 ++++++++++++++++++-
 include/linux/serial_core.h      |    5 +++--
 include/linux/tty_ldisc.h        |    5 +++--
 6 files changed, 44 insertions(+), 31 deletions(-)
drivers/pps/clients/pps-ktimer.c +2 -7
index e7ef5b8..e1bdd8b 100644
@@ -40,18 +40,13 @@ static struct timer_list ktimer;
 
 static void pps_ktimer_event(unsigned long ptr)
 {
-	struct timespec __ts;
-	struct pps_ktime ts;
+	struct pps_event_time ts;
 
 	/* First of all we get the time stamp... */
-	getnstimeofday(&__ts);
+	pps_get_ts(&ts);
 
 	pr_info("PPS event at %lu\n", jiffies);
 
-	/* ... and translate it to PPS time data struct */
-	ts.sec = __ts.tv_sec;
-	ts.nsec = __ts.tv_nsec;
-
 	pps_event(source, &ts, PPS_CAPTUREASSERT, NULL);
 
 	mod_timer(&ktimer, jiffies + HZ);
drivers/pps/clients/pps-ldisc.c +6 -12
index 8e1932d..20fc9f7 100644
@@ -27,26 +27,20 @@
 #define PPS_TTY_MAGIC		0x0001
 
 static void pps_tty_dcd_change(struct tty_struct *tty, unsigned int status,
-				struct timespec *ts)
+				struct pps_event_time *ts)
 {
 	int id = (long)tty->disc_data;
-	struct timespec __ts;
-	struct pps_ktime pps_ts;
+	struct pps_event_time __ts;
 
 	/* First of all we get the time stamp... */
-	getnstimeofday(&__ts);
+	pps_get_ts(&__ts);
 
 	/* Does caller give us a timestamp? */
-	if (ts) {	/* Yes. Let's use it! */
-		pps_ts.sec = ts->tv_sec;
-		pps_ts.nsec = ts->tv_nsec;
-	} else {	/* No. Do it ourself! */
-		pps_ts.sec = __ts.tv_sec;
-		pps_ts.nsec = __ts.tv_nsec;
-	}
+	if (!ts)	/* No. Do it ourself! */
+		ts = &__ts;
 
 	/* Now do the PPS event report */
-	pps_event(id, &pps_ts, status ? PPS_CAPTUREASSERT : PPS_CAPTURECLEAR,
+	pps_event(id, ts, status ? PPS_CAPTUREASSERT : PPS_CAPTURECLEAR,
 			NULL);
 
 	pr_debug("PPS %s at %lu on source #%d\n",
drivers/pps/kapi.c +12 -7
index 3f89f5eb..b431d83 100644
@@ -268,11 +268,12 @@ EXPORT_SYMBOL(pps_unregister_source);
  *	pps->info.echo(source, event, data);
  */
 
-void pps_event(int source, struct pps_ktime *ts, int event, void *data)
+void pps_event(int source, struct pps_event_time *ts, int event, void *data)
 {
 	struct pps_device *pps;
 	unsigned long flags;
 	int captured = 0;
+	struct pps_ktime ts_real;
 
 	if ((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0) {
 		printk(KERN_ERR "pps: unknown event (%x) for source %d\n",
@@ -284,8 +285,10 @@ void pps_event(int source, struct pps_ktime *ts, int event, void *data)
 	if (!pps)
 		return;
 
-	pr_debug("PPS event on source %d at %llu.%06u\n",
-			pps->id, (unsigned long long) ts->sec, ts->nsec);
+	pr_debug("PPS event on source %d at %ld.%09ld\n",
+			pps->id, ts->ts_real.tv_sec, ts->ts_real.tv_nsec);
+
+	timespec_to_pps_ktime(&ts_real, ts->ts_real);
 
 	spin_lock_irqsave(&pps->lock, flags);
 
@@ -299,10 +302,11 @@ void pps_event(int source, struct pps_ktime *ts, int event, void *data)
 			(pps->params.mode & PPS_CAPTUREASSERT)) {
 		/* We have to add an offset? */
 		if (pps->params.mode & PPS_OFFSETASSERT)
-			pps_add_offset(ts, &pps->params.assert_off_tu);
+			pps_add_offset(&ts_real,
+					&pps->params.assert_off_tu);
 
 		/* Save the time stamp */
-		pps->assert_tu = *ts;
+		pps->assert_tu = ts_real;
 		pps->assert_sequence++;
 		pr_debug("capture assert seq #%u for source %d\n",
 			pps->assert_sequence, source);
@@ -313,10 +317,11 @@ void pps_event(int source, struct pps_ktime *ts, int event, void *data)
 			(pps->params.mode & PPS_CAPTURECLEAR)) {
 		/* We have to add an offset? */
 		if (pps->params.mode & PPS_OFFSETCLEAR)
-			pps_add_offset(ts, &pps->params.clear_off_tu);
+			pps_add_offset(&ts_real,
+					&pps->params.clear_off_tu);
 
 		/* Save the time stamp */
-		pps->clear_tu = *ts;
+		pps->clear_tu = ts_real;
 		pps->clear_sequence++;
 		pr_debug("capture clear seq #%u for source %d\n",
 			pps->clear_sequence, source);
include/linux/pps_kernel.h +18 -1
index c3aed4b..32aa676 100644
@@ -43,6 +43,10 @@ struct pps_source_info {
 	struct device *dev;
 };
 
+struct pps_event_time {
+	struct timespec ts_real;
+};
+
 /* The main struct */
 struct pps_device {
 	struct pps_source_info info;		/* PSS source info */
@@ -88,7 +92,20 @@ extern int pps_register_source(struct pps_source_info *info,
 extern void pps_unregister_source(int source);
 extern int pps_register_cdev(struct pps_device *pps);
 extern void pps_unregister_cdev(struct pps_device *pps);
-extern void pps_event(int source, struct pps_ktime *ts, int event, void *data);
+extern void pps_event(int source, struct pps_event_time *ts, int event,
+		void *data);
+
+static inline void timespec_to_pps_ktime(struct pps_ktime *kt,
+		struct timespec ts)
+{
+	kt->sec = ts.tv_sec;
+	kt->nsec = ts.tv_nsec;
+}
+
+static inline void pps_get_ts(struct pps_event_time *ts)
+{
+	getnstimeofday(&ts->ts_real);
+}
 
 #endif /* LINUX_PPS_KERNEL_H */
 
include/linux/serial_core.h +3 -2
index 563e234..55c8192 100644
@@ -206,6 +206,7 @@
 #include <linux/tty.h>
 #include <linux/mutex.h>
 #include <linux/sysrq.h>
+#include <linux/pps_kernel.h>
 
 struct uart_port;
 struct serial_struct;
@@ -510,10 +511,10 @@ uart_handle_dcd_change(struct uart_port *uport, unsigned int status)
 	struct uart_state *state = uport->state;
 	struct tty_port *port = &state->port;
 	struct tty_ldisc *ld = tty_ldisc_ref(port->tty);
-	struct timespec ts;
+	struct pps_event_time ts;
 
 	if (ld && ld->ops->dcd_change)
-		getnstimeofday(&ts);
+		pps_get_ts(&ts);
 
 	uport->icount.dcd++;
 #ifdef CONFIG_HARD_PPS
include/linux/tty_ldisc.h +3 -2
index 526d66f..763e061 100644
@@ -101,7 +101,7 @@
  *	any pending driver I/O is completed.
  *
  * void (*dcd_change)(struct tty_struct *tty, unsigned int status,
- * 			struct timespec *ts)
+ *			struct pps_event_time *ts)
  *
  *	Tells the discipline that the DCD pin has changed its status and
  *	the relative timestamp. Pointer ts can be NULL.
@@ -109,6 +109,7 @@
 
 #include <linux/fs.h>
 #include <linux/wait.h>
+#include <linux/pps_kernel.h>
 
 struct tty_ldisc_ops {
 	int	magic;
@@ -143,7 +144,7 @@ struct tty_ldisc_ops {
 			       char *fp, int count);
 	void	(*write_wakeup)(struct tty_struct *);
 	void	(*dcd_change)(struct tty_struct *, unsigned int,
-				struct timespec *);
+				struct pps_event_time *);
 
 	struct  module *owner;
 	
Signature · 1 line
1.7.2.3
#6 lore raw

PATCH v4 5/17 pps: access pps device by direct pointer

In reply to Alexander Gordeev

Using device index as a pointer needs some unnecessary work to be done every time the pointer is needed (in irq handler for example). Using a direct pointer is much more easy (and safe as well).

---
 drivers/pps/clients/pps-ktimer.c |   30 ++++-----
 drivers/pps/clients/pps-ldisc.c  |   52 ++++++++++------
 drivers/pps/kapi.c               |  124 +++++++++-----------------------------
 drivers/pps/pps.c                |   22 ++-----
 include/linux/pps_kernel.h       |   23 +++----
 5 files changed, 90 insertions(+), 161 deletions(-)
drivers/pps/clients/pps-ktimer.c +13 -17
index e1bdd8b..966ead1 100644
@@ -31,7 +31,7 @@
  * Global variables
  */
 
-static int source;
+static struct pps_device *pps;
 static struct timer_list ktimer;
 
 /*
@@ -47,7 +47,7 @@ static void pps_ktimer_event(unsigned long ptr)
 
 	pr_info("PPS event at %lu\n", jiffies);
 
-	pps_event(source, &ts, PPS_CAPTUREASSERT, NULL);
+	pps_event(pps, &ts, PPS_CAPTUREASSERT, NULL);
 
 	mod_timer(&ktimer, jiffies + HZ);
 }
@@ -56,12 +56,11 @@ static void pps_ktimer_event(unsigned long ptr)
  * The echo function
  */
 
-static void pps_ktimer_echo(int source, int event, void *data)
+static void pps_ktimer_echo(struct pps_device *pps, int event, void *data)
 {
-	pr_info("echo %s %s for source %d\n",
+	dev_info(pps->dev, "echo %s %s\n",
 		event & PPS_CAPTUREASSERT ? "assert" : "",
-		event & PPS_CAPTURECLEAR ? "clear" : "",
-		source);
+		event & PPS_CAPTURECLEAR ? "clear" : "");
 }
 
 /*
@@ -84,30 +83,27 @@ static struct pps_source_info pps_ktimer_info = {
 
 static void __exit pps_ktimer_exit(void)
 {
-	del_timer_sync(&ktimer);
-	pps_unregister_source(source);
+	dev_info(pps->dev, "ktimer PPS source unregistered\n");
 
-	pr_info("ktimer PPS source unregistered\n");
+	del_timer_sync(&ktimer);
+	pps_unregister_source(pps);
 }
 
 static int __init pps_ktimer_init(void)
 {
-	int ret;
-
-	ret = pps_register_source(&pps_ktimer_info,
+	pps = pps_register_source(&pps_ktimer_info,
 				PPS_CAPTUREASSERT | PPS_OFFSETASSERT);
-	if (ret < 0) {
+	if (pps == NULL) {
 		printk(KERN_ERR "cannot register ktimer source\n");
-		return ret;
+		return -ENOMEM;
 	}
-	source = ret;
 
 	setup_timer(&ktimer, pps_ktimer_event, 0);
 	mod_timer(&ktimer, jiffies + HZ);
 
-	pr_info("ktimer PPS source registered at %d\n", source);
+	dev_info(pps->dev, "ktimer PPS source registered\n");
 
-	return  0;
+	return 0;
 }
 
 module_init(pps_ktimer_init);
drivers/pps/clients/pps-ldisc.c +34 -18

99 lines not on this page · raw message

drivers/pps/kapi.c +28 -96

222 lines not on this page · raw message

drivers/pps/pps.c +6 -16
index dc7e66c..1922f27 100644
@@ -204,12 +204,6 @@ static int pps_cdev_open(struct inode *inode, struct file *file)
 {
 	struct pps_device *pps = container_of(inode->i_cdev,
 						struct pps_device, cdev);
-	int found;
-
-	found = pps_get_source(pps->id) != 0;
-	if (!found)
-		return -ENODEV;
-
 	file->private_data = pps;
 
 	return 0;
@@ -217,11 +211,6 @@ static int pps_cdev_open(struct inode *inode, struct file *file)
 
 static int pps_cdev_release(struct inode *inode, struct file *file)
 {
-	struct pps_device *pps = file->private_data;
-
-	/* Free the PPS source and wake up (possible) deregistration */
-	pps_put_source(pps);
-
 	return 0;
 }
 
@@ -242,22 +231,23 @@ static const struct file_operations pps_cdev_fops = {
 int pps_register_cdev(struct pps_device *pps)
 {
 	int err;
+	dev_t devt;
+
+	devt = MKDEV(MAJOR(pps_devt), pps->id);
 
-	pps->devno = MKDEV(MAJOR(pps_devt), pps->id);
 	cdev_init(&pps->cdev, &pps_cdev_fops);
 	pps->cdev.owner = pps->info.owner;
 
-	err = cdev_add(&pps->cdev, pps->devno, 1);
+	err = cdev_add(&pps->cdev, devt, 1);
 	if (err) {
 		printk(KERN_ERR "pps: %s: failed to add char device %d:%d\n",
 				pps->info.name, MAJOR(pps_devt), pps->id);
 		return err;
 	}
-	pps->dev = device_create(pps_class, pps->info.dev, pps->devno, NULL,
+	pps->dev = device_create(pps_class, pps->info.dev, devt, pps,
 							"pps%d", pps->id);
 	if (IS_ERR(pps->dev))
 		goto del_cdev;
-	dev_set_drvdata(pps->dev, pps);
 
 	pr_debug("source %s got cdev (%d:%d)\n", pps->info.name,
 			MAJOR(pps_devt), pps->id);
@@ -272,7 +262,7 @@ del_cdev:
 
 void pps_unregister_cdev(struct pps_device *pps)
 {
-	device_destroy(pps_class, pps->devno);
+	device_destroy(pps_class, pps->dev->devt);
 	cdev_del(&pps->cdev);
 }
 
include/linux/pps_kernel.h +9 -14
index 32aa676..1aedf50 100644
@@ -31,13 +31,16 @@
  * Global defines
  */
 
+struct pps_device;
+
 /* The specific PPS source info */
 struct pps_source_info {
 	char name[PPS_MAX_NAME_LEN];		/* simbolic name */
 	char path[PPS_MAX_NAME_LEN];		/* path of connected device */
 	int mode;				/* PPS's allowed mode */
 
-	void (*echo)(int source, int event, void *data); /* PPS echo function */
+	void (*echo)(struct pps_device *pps,
+			int event, void *data);	/* PPS echo function */
 
 	struct module *owner;
 	struct device *dev;
@@ -65,35 +68,27 @@ struct pps_device {
 	unsigned int id;			/* PPS source unique ID */
 	struct cdev cdev;
 	struct device *dev;
-	int devno;
 	struct fasync_struct *async_queue;	/* fasync method */
 	spinlock_t lock;
-
-	atomic_t usage;				/* usage count */
 };
 
 /*
  * Global variables
  */
 
-extern spinlock_t pps_idr_lock;
-extern struct idr pps_idr;
-
 extern struct device_attribute pps_attrs[];
 
 /*
  * Exported functions
  */
 
-struct pps_device *pps_get_source(int source);
-extern void pps_put_source(struct pps_device *pps);
-extern int pps_register_source(struct pps_source_info *info,
-				int default_params);
-extern void pps_unregister_source(int source);
+extern struct pps_device *pps_register_source(
+		struct pps_source_info *info, int default_params);
+extern void pps_unregister_source(struct pps_device *pps);
 extern int pps_register_cdev(struct pps_device *pps);
 extern void pps_unregister_cdev(struct pps_device *pps);
-extern void pps_event(int source, struct pps_event_time *ts, int event,
-		void *data);
+extern void pps_event(struct pps_device *pps,
+		struct pps_event_time *ts, int event, void *data);
 
 static inline void timespec_to_pps_ktime(struct pps_ktime *kt,
 		struct timespec ts)
Signature · 1 line
1.7.2.3
maintainer
#7 lore raw

Re: [PATCHv4 05/17] pps: access pps device by direct pointer

Quoted 123 lines · Alexander Gordeev

Using device index as a pointer needs some unnecessary work to be done every time the pointer is needed (in irq handler for example). Using a direct pointer is much more easy (and safe as well).

---
 drivers/pps/clients/pps-ktimer.c |   30 ++++-----
 drivers/pps/clients/pps-ldisc.c  |   52 ++++++++++------
 drivers/pps/kapi.c               |  124 +++++++++-----------------------------
 drivers/pps/pps.c                |   22 ++-----
 include/linux/pps_kernel.h       |   23 +++----
 5 files changed, 90 insertions(+), 161 deletions(-)
drivers/pps/clients/pps-ktimer.c +13 -17
index e1bdd8b..966ead1 100644
@@ -31,7 +31,7 @@
  * Global variables
  */
 
-static int source;
+static struct pps_device *pps;
 static struct timer_list ktimer;
 
 /*
@@ -47,7 +47,7 @@ static void pps_ktimer_event(unsigned long ptr)
 
 	pr_info("PPS event at %lu\n", jiffies);
 
-	pps_event(source, &ts, PPS_CAPTUREASSERT, NULL);
+	pps_event(pps, &ts, PPS_CAPTUREASSERT, NULL);
 
 	mod_timer(&ktimer, jiffies + HZ);
 }
@@ -56,12 +56,11 @@ static void pps_ktimer_event(unsigned long ptr)
  * The echo function
  */
 
-static void pps_ktimer_echo(int source, int event, void *data)
+static void pps_ktimer_echo(struct pps_device *pps, int event, void *data)
 {
-	pr_info("echo %s %s for source %d\n",
+	dev_info(pps->dev, "echo %s %s\n",
 		event & PPS_CAPTUREASSERT ? "assert" : "",
-		event & PPS_CAPTURECLEAR ? "clear" : "",
-		source);
+		event & PPS_CAPTURECLEAR ? "clear" : "");
 }
 
 /*
@@ -84,30 +83,27 @@ static struct pps_source_info pps_ktimer_info = {
 
 static void __exit pps_ktimer_exit(void)
 {
-	del_timer_sync(&ktimer);
-	pps_unregister_source(source);
+	dev_info(pps->dev, "ktimer PPS source unregistered\n");
 
-	pr_info("ktimer PPS source unregistered\n");
+	del_timer_sync(&ktimer);
+	pps_unregister_source(pps);
 }
 
 static int __init pps_ktimer_init(void)
 {
-	int ret;
-
-	ret = pps_register_source(&pps_ktimer_info,
+	pps = pps_register_source(&pps_ktimer_info,
 				PPS_CAPTUREASSERT | PPS_OFFSETASSERT);
-	if (ret < 0) {
+	if (pps == NULL) {
 		printk(KERN_ERR "cannot register ktimer source\n");
-		return ret;
+		return -ENOMEM;
 	}
-	source = ret;
 
 	setup_timer(&ktimer, pps_ktimer_event, 0);
 	mod_timer(&ktimer, jiffies + HZ);
 
-	pr_info("ktimer PPS source registered at %d\n", source);
+	dev_info(pps->dev, "ktimer PPS source registered\n");
 
-	return  0;
+	return 0;
 }
 
 module_init(pps_ktimer_init);
drivers/pps/clients/pps-ldisc.c +6 -4
index 20fc9f7..1950b15 100644
@@ -23,14 +23,18 @@
 #include <linux/serial_core.h>
 #include <linux/tty.h>
 #include <linux/pps_kernel.h>
+#include <linux/spinlock.h>
 
 #define PPS_TTY_MAGIC		0x0001
 
+static DEFINE_SPINLOCK(pps_ldisc_lock);
+
 static void pps_tty_dcd_change(struct tty_struct *tty, unsigned int status,
 				struct pps_event_time *ts)
 {
-	int id = (long)tty->disc_data;
+	struct pps_device *pps;
 	struct pps_event_time __ts;
+	unsigned long flags;
 
 	/* First of all we get the time stamp... */
 	pps_get_ts(&__ts);
@@ -39,12 +43,18 @@ static void pps_tty_dcd_change(struct tty_struct *tty, unsigned int status,
 	if (!ts)	/* No. Do it ourself! */
 		ts = &__ts;
 
-	/* Now do the PPS event report */
-	pps_event(id, ts, status ? PPS_CAPTUREASSERT : PPS_CAPTURECLEAR,
-			NULL);
+	spin_lock_irqsave(&pps_ldisc_lock, flags);
 

drivers/pps/clients/pps-ldisc.c static void pps_tty_dcd_change(struct tty_struct *tty, unsigned int status, · line 55

Quoted 10 lines
drivers/pps/clients/pps-ldisc.c +8 -2
-	pr_debug("PPS %s at %lu on source #%d\n",
-			status ? "assert" : "clear", jiffies, id);
+	/* Now do the PPS event report */
+	pps = (struct pps_device *)tty->disc_data;
+	if (pps != NULL) {
+		pps_event(pps, ts, status ? PPS_CAPTUREASSERT :
+				PPS_CAPTURECLEAR, NULL);
+		spin_unlock_irqrestore(&pps_ldisc_lock, flags);
+		dev_dbg(pps->dev, "PPS %s at %lu\n",
+				status ? "assert" : "clear", jiffies);

I think you should swap these two lines because after you release the lock the pps pointer may be not valid...

Quoted 22 lines
drivers/pps/clients/pps-ldisc.c +4 -3
+	} else
+		spin_unlock_irqrestore(&pps_ldisc_lock, flags);
 }
 
 static int (*alias_n_tty_open)(struct tty_struct *tty);
@@ -54,7 +64,7 @@ static int pps_tty_open(struct tty_struct *tty)
 	struct pps_source_info info;
 	struct tty_driver *drv = tty->driver;
 	int index = tty->index + drv->name_base;
-	int ret;
+	struct pps_device *pps;
 
 	info.owner = THIS_MODULE;
 	info.dev = NULL;
@@ -64,20 +74,22 @@ static int pps_tty_open(struct tty_struct *tty)
 			PPS_OFFSETASSERT | PPS_OFFSETCLEAR | \
 			PPS_CANWAIT | PPS_TSFMT_TSPEC;
 
-	ret = pps_register_source(&info, PPS_CAPTUREBOTH | \
+	pps = pps_register_source(&info, PPS_CAPTUREBOTH | \
 				PPS_OFFSETASSERT | PPS_OFFSETCLEAR);
-	if (ret < 0) {

drivers/pps/clients/pps-ldisc.c static int pps_tty_open(struct tty_struct *tty) · line 86

Quoted 10 lines
drivers/pps/clients/pps-ldisc.c +6 -2
+	if (pps == NULL) {
 		pr_err("cannot register PPS source \"%s\"\n", info.path);
-		return ret;
+		return -ENOMEM;
 	}
-	tty->disc_data = (void *)(long)ret;
+
+	spin_lock_irq(&pps_ldisc_lock);
+	tty->disc_data = pps;
+	spin_unlock_irq(&pps_ldisc_lock);

Maybe this lock is useless... however, are we sure that before setting tty->disc_data to pps its value is null? Otherwise the dcd_change may be called with an oops! We cannot control serial port IRQ generation... :-/

drivers/pps/clients/pps-ldisc.c static int pps_tty_open(struct tty_struct *tty) · line 89

Quoted 5 lines
drivers/pps/clients/pps-ldisc.c +1 -3
 	/* Should open N_TTY ldisc too */
-	ret = alias_n_tty_open(tty);
-	if (ret < 0)
-		pps_unregister_source((long)tty->disc_data);
+	if (alias_n_tty_open(tty) < 0)

Are you sure this code style is «canonical»? What does checkpatch say? ;)

drivers/pps/clients/pps-ldisc.c static int pps_tty_open(struct tty_struct *tty) · line 89

Quoted 1 line
drivers/pps/clients/pps-ldisc.c +1 -0
+		pps_unregister_source(pps);

If the above function fails I suppose you should invalidate tty->disc_data, then unregister the pps source and, in the end, return error (I know old code returns 0 anywhere, but I think it can be fixed right now! ;).

Quoted 15 lines
drivers/pps/clients/pps-ldisc.c +2 -3
-	pr_info("PPS source #%d \"%s\" added\n", ret, info.path);
+	dev_info(pps->dev, "source \"%s\" added\n", info.path);
 
 	return 0;
 }
@@ -86,12 +98,16 @@ static void (*alias_n_tty_close)(struct tty_struct *tty);
 
 static void pps_tty_close(struct tty_struct *tty)
 {
-	int id = (long)tty->disc_data;
+	struct pps_device *pps = (struct pps_device *)tty->disc_data;
 
-	pps_unregister_source(id);
 	alias_n_tty_close(tty);
 

drivers/pps/clients/pps-ldisc.c static void (*alias_n_tty_close)(struct tty_struct *tty); · line 113

Quoted 10 lines
drivers/pps/clients/pps-ldisc.c +6 -1
-	pr_info("PPS source #%d removed\n", id);
+	spin_lock_irq(&pps_ldisc_lock);
+	tty->disc_data = NULL;
+	spin_unlock_irq(&pps_ldisc_lock);
+
+	dev_info(pps->dev, "removed\n");
+	pps_unregister_source(pps);
 }
 
 static struct tty_ldisc_ops pps_ldisc_ops;

I suppose you can solve all your problems if you do the lock into pps_tty_init...

Ciao,

Rodolfo

Signature · 5 lines
GNU/Linux Solutions                  e-mail: [email protected]
Linux Device Driver                          [email protected]
Embedded Systems                     phone:  +39 349 2432127
UNIX programming                     skype:  rodolfo.giometti
Freelance ICT Italia - Consulente ICT Italia - www.consulenti-ict.it
#8 lore raw

Re: [PATCHv4 05/17] pps: access pps device by direct pointer

В Sat, 20 Nov 2010 16:44:17 +0100
Rodolfo Giometti <[email protected]> пишет:

Quoted 124 lines · Alexander Gordeev
Quoted 123 lines

Using device index as a pointer needs some unnecessary work to be done every time the pointer is needed (in irq handler for example). Using a direct pointer is much more easy (and safe as well).

---
 drivers/pps/clients/pps-ktimer.c |   30 ++++-----
 drivers/pps/clients/pps-ldisc.c  |   52 ++++++++++------
 drivers/pps/kapi.c               |  124 +++++++++-----------------------------
 drivers/pps/pps.c                |   22 ++-----
 include/linux/pps_kernel.h       |   23 +++----
 5 files changed, 90 insertions(+), 161 deletions(-)
drivers/pps/clients/pps-ktimer.c +13 -17
index e1bdd8b..966ead1 100644
@@ -31,7 +31,7 @@
  * Global variables
  */
 
-static int source;
+static struct pps_device *pps;
 static struct timer_list ktimer;
 
 /*
@@ -47,7 +47,7 @@ static void pps_ktimer_event(unsigned long ptr)
 
 	pr_info("PPS event at %lu\n", jiffies);
 
-	pps_event(source, &ts, PPS_CAPTUREASSERT, NULL);
+	pps_event(pps, &ts, PPS_CAPTUREASSERT, NULL);
 
 	mod_timer(&ktimer, jiffies + HZ);
 }
@@ -56,12 +56,11 @@ static void pps_ktimer_event(unsigned long ptr)
  * The echo function
  */
 
-static void pps_ktimer_echo(int source, int event, void *data)
+static void pps_ktimer_echo(struct pps_device *pps, int event, void *data)
 {
-	pr_info("echo %s %s for source %d\n",
+	dev_info(pps->dev, "echo %s %s\n",
 		event & PPS_CAPTUREASSERT ? "assert" : "",
-		event & PPS_CAPTURECLEAR ? "clear" : "",
-		source);
+		event & PPS_CAPTURECLEAR ? "clear" : "");
 }
 
 /*
@@ -84,30 +83,27 @@ static struct pps_source_info pps_ktimer_info = {
 
 static void __exit pps_ktimer_exit(void)
 {
-	del_timer_sync(&ktimer);
-	pps_unregister_source(source);
+	dev_info(pps->dev, "ktimer PPS source unregistered\n");
 
-	pr_info("ktimer PPS source unregistered\n");
+	del_timer_sync(&ktimer);
+	pps_unregister_source(pps);
 }
 
 static int __init pps_ktimer_init(void)
 {
-	int ret;
-
-	ret = pps_register_source(&pps_ktimer_info,
+	pps = pps_register_source(&pps_ktimer_info,
 				PPS_CAPTUREASSERT | PPS_OFFSETASSERT);
-	if (ret < 0) {
+	if (pps == NULL) {
 		printk(KERN_ERR "cannot register ktimer source\n");
-		return ret;
+		return -ENOMEM;
 	}
-	source = ret;
 
 	setup_timer(&ktimer, pps_ktimer_event, 0);
 	mod_timer(&ktimer, jiffies + HZ);
 
-	pr_info("ktimer PPS source registered at %d\n", source);
+	dev_info(pps->dev, "ktimer PPS source registered\n");
 
-	return  0;
+	return 0;
 }
 
 module_init(pps_ktimer_init);
drivers/pps/clients/pps-ldisc.c +6 -4
index 20fc9f7..1950b15 100644
@@ -23,14 +23,18 @@
 #include <linux/serial_core.h>
 #include <linux/tty.h>
 #include <linux/pps_kernel.h>
+#include <linux/spinlock.h>
 
 #define PPS_TTY_MAGIC		0x0001
 
+static DEFINE_SPINLOCK(pps_ldisc_lock);
+
 static void pps_tty_dcd_change(struct tty_struct *tty, unsigned int status,
 				struct pps_event_time *ts)
 {
-	int id = (long)tty->disc_data;
+	struct pps_device *pps;
 	struct pps_event_time __ts;
+	unsigned long flags;
 
 	/* First of all we get the time stamp... */
 	pps_get_ts(&__ts);
@@ -39,12 +43,18 @@ static void pps_tty_dcd_change(struct tty_struct *tty, unsigned int status,
 	if (!ts)	/* No. Do it ourself! */
 		ts = &__ts;
 
-	/* Now do the PPS event report */
-	pps_event(id, ts, status ? PPS_CAPTUREASSERT : PPS_CAPTURECLEAR,
-			NULL);
+	spin_lock_irqsave(&pps_ldisc_lock, flags);
 
Quoted 13 lines

drivers/pps/clients/pps-ldisc.c static void pps_tty_dcd_change(struct tty_struct *tty, unsigned int status, · line 55

Quoted 10 lines
drivers/pps/clients/pps-ldisc.c +8 -2
-	pr_debug("PPS %s at %lu on source #%d\n",
-			status ? "assert" : "clear", jiffies, id);
+	/* Now do the PPS event report */
+	pps = (struct pps_device *)tty->disc_data;
+	if (pps != NULL) {
+		pps_event(pps, ts, status ? PPS_CAPTUREASSERT :
+				PPS_CAPTURECLEAR, NULL);
+		spin_unlock_irqrestore(&pps_ldisc_lock, flags);
+		dev_dbg(pps->dev, "PPS %s at %lu\n",
+				status ? "assert" : "clear", jiffies);

I think you should swap these two lines because after you release the lock the pps pointer may be not valid...

Ah, yes, good catch, thank you!

Quoted 29 lines
Quoted 29 lines
drivers/pps/clients/pps-ldisc.c +7 -5
+	} else
+		spin_unlock_irqrestore(&pps_ldisc_lock, flags);
 }
 
 static int (*alias_n_tty_open)(struct tty_struct *tty);
@@ -54,7 +64,7 @@ static int pps_tty_open(struct tty_struct *tty)
 	struct pps_source_info info;
 	struct tty_driver *drv = tty->driver;
 	int index = tty->index + drv->name_base;
-	int ret;
+	struct pps_device *pps;
 
 	info.owner = THIS_MODULE;
 	info.dev = NULL;
@@ -64,20 +74,22 @@ static int pps_tty_open(struct tty_struct *tty)
 			PPS_OFFSETASSERT | PPS_OFFSETCLEAR | \
 			PPS_CANWAIT | PPS_TSFMT_TSPEC;
 
-	ret = pps_register_source(&info, PPS_CAPTUREBOTH | \
+	pps = pps_register_source(&info, PPS_CAPTUREBOTH | \
 				PPS_OFFSETASSERT | PPS_OFFSETCLEAR);
-	if (ret < 0) {
+	if (pps == NULL) {
 		pr_err("cannot register PPS source \"%s\"\n", info.path);
-		return ret;
+		return -ENOMEM;
 	}
-	tty->disc_data = (void *)(long)ret;
+
Quoted 8 lines

drivers/pps/clients/pps-ldisc.c static int pps_tty_open(struct tty_struct *tty) · line 86

Quoted 3 lines
drivers/pps/clients/pps-ldisc.c +3 -0
+	spin_lock_irq(&pps_ldisc_lock);
+	tty->disc_data = pps;
+	spin_unlock_irq(&pps_ldisc_lock);

Maybe this lock is useless... however, are we sure that before setting tty->disc_data to pps its value is null? Otherwise the dcd_change may be called with an oops! We cannot control serial port IRQ generation... :-/

No, locking here is necessary.
There is only one problem this spinlock protects us from: current tty code neither disables interrupts nor doesn't ensure there are no references to PPS ldisc from uart_handle_dcd_change() before closing it (and removing PPS source). It relies on flushing workqueue and disabling input. It worked good this way until dcd_change() was added which doesn't use workqueues and is called in atomic context so can't lock on mutex.

Imagine that (on SMP system) uart_handle_dcd_change() could obtain a reference to ldisc and call dcd_change() until actually calling pps_event(); then on another processor all the path from tty_ldisc_halt() until tty_ldisc_stop() is executed. And then pps_event() is called with illegal pps pointer.

I just thought you are right that disc_data can be set not NULL by another ldisc and it's a problem. But actually I just realised how to fix it completely. :)

I just have to add a spinlock to tty_struct, lock all the uart_handle_dcd_change() with it and add a "barrier" between tty_ldisc_halt() and tty_ldisc_close() i.e. just that:

...
spin_lock_irq();
spin_unlock_irq();
...

This "barrier" will ensure that there is no references to ldisc from uart_handle_dcd_change(). It won't be able to obtain a new reference after tty_ldisc_halt() so will become completely sane. Not disabling interrupts won't be a problem because it won't be able to obtain an ldisc reference until tty_ldisc_enable() which is called only after the new ldisc is fully functional. If it's our ldisc than it will have both dcd_change defined and a valid pps pointer. If it's not our ldisc it won't have both so uart_handle_dcd_change() won't call dcd_change() at all.

I think I'll do that as a separate patch.

Quoted 8 lines

drivers/pps/clients/pps-ldisc.c static int pps_tty_open(struct tty_struct *tty) · line 89

Quoted 5 lines
drivers/pps/clients/pps-ldisc.c +1 -3
 	/* Should open N_TTY ldisc too */
-	ret = alias_n_tty_open(tty);
-	if (ret < 0)
-		pps_unregister_source((long)tty->disc_data);
+	if (alias_n_tty_open(tty) < 0)

Are you sure this code style is «canonical»? What does checkpatch say? ;)

checkpatch is completely happy about all patches. :) But indeed it's not good. I'll fix it, thanks!

Quoted 6 lines

drivers/pps/clients/pps-ldisc.c static int pps_tty_open(struct tty_struct *tty) · line 89

Quoted 1 line
drivers/pps/clients/pps-ldisc.c +1 -0
+		pps_unregister_source(pps);

If the above function fails I suppose you should invalidate tty->disc_data, then unregister the pps source and, in the end, return error (I know old code returns 0 anywhere, but I think it can be fixed right now! ;).

OK. :)

Quoted 33 lines
Quoted 15 lines
drivers/pps/clients/pps-ldisc.c +2 -3
-	pr_info("PPS source #%d \"%s\" added\n", ret, info.path);
+	dev_info(pps->dev, "source \"%s\" added\n", info.path);
 
 	return 0;
 }
@@ -86,12 +98,16 @@ static void (*alias_n_tty_close)(struct tty_struct *tty);
 
 static void pps_tty_close(struct tty_struct *tty)
 {
-	int id = (long)tty->disc_data;
+	struct pps_device *pps = (struct pps_device *)tty->disc_data;
 
-	pps_unregister_source(id);
 	alias_n_tty_close(tty);
 

drivers/pps/clients/pps-ldisc.c static void (*alias_n_tty_close)(struct tty_struct *tty); · line 113

Quoted 10 lines
drivers/pps/clients/pps-ldisc.c +6 -1
-	pr_info("PPS source #%d removed\n", id);
+	spin_lock_irq(&pps_ldisc_lock);
+	tty->disc_data = NULL;
+	spin_unlock_irq(&pps_ldisc_lock);
+
+	dev_info(pps->dev, "removed\n");
+	pps_unregister_source(pps);
 }
 
 static struct tty_ldisc_ops pps_ldisc_ops;

I suppose you can solve all your problems if you do the lock into pps_tty_init...

Ciao,

Rodolfo

Signature · 1 line
  Alexander
maintainer
#9 lore raw

Re: [PATCHv4 05/17] pps: access pps device by direct pointer

Quoted 55 lines · Alexander Gordeev
Quoted 8 lines
Quoted 8 lines
drivers/pps/clients/pps-ldisc.c +3 -3
-	if (ret < 0) {
+	if (pps == NULL) {
 		pr_err("cannot register PPS source \"%s\"\n", info.path);
-		return ret;
+		return -ENOMEM;
 	}
-	tty->disc_data = (void *)(long)ret;
+
Quoted 8 lines

drivers/pps/clients/pps-ldisc.c static int pps_tty_open(struct tty_struct *tty) · line 86

Quoted 3 lines
drivers/pps/clients/pps-ldisc.c +3 -0
+	spin_lock_irq(&pps_ldisc_lock);
+	tty->disc_data = pps;
+	spin_unlock_irq(&pps_ldisc_lock);

Maybe this lock is useless... however, are we sure that before setting tty->disc_data to pps its value is null? Otherwise the dcd_change may be called with an oops! We cannot control serial port IRQ generation... :-/

No, locking here is necessary.
There is only one problem this spinlock protects us from: current tty code neither disables interrupts nor doesn't ensure there are no references to PPS ldisc from uart_handle_dcd_change() before closing it (and removing PPS source). It relies on flushing workqueue and disabling input. It worked good this way until dcd_change() was added which doesn't use workqueues and is called in atomic context so can't lock on mutex.

Imagine that (on SMP system) uart_handle_dcd_change() could obtain a reference to ldisc and call dcd_change() until actually calling pps_event(); then on another processor all the path from tty_ldisc_halt() until tty_ldisc_stop() is executed. And then pps_event() is called with illegal pps pointer.

I just thought you are right that disc_data can be set not NULL by another ldisc and it's a problem. But actually I just realised how to fix it completely. :)

I just have to add a spinlock to tty_struct, lock all the uart_handle_dcd_change() with it and add a "barrier" between tty_ldisc_halt() and tty_ldisc_close() i.e. just that:

...
spin_lock_irq();
spin_unlock_irq();
...

This "barrier" will ensure that there is no references to ldisc from uart_handle_dcd_change(). It won't be able to obtain a new reference after tty_ldisc_halt() so will become completely sane. Not disabling interrupts won't be a problem because it won't be able to obtain an ldisc reference until tty_ldisc_enable() which is called only after the new ldisc is fully functional. If it's our ldisc than it will have both dcd_change defined and a valid pps pointer. If it's not our ldisc it won't have both so uart_handle_dcd_change() won't call dcd_change() at all.

Quoted 1 line

I think I'll do that as a separate patch.

Excuse me but IMHO you should solve all your problems if you do the lock into pps_tty_init/cleanup instead of into pps_tty_open/close.

   spin_lock_irq();
           err = tty_register_ldisc(N_PPS, &pps_ldisc_ops);
           if (err)
                   pr_err("can't register PPS line discipline\n");
           else
                   pr_info("PPS line discipline registered\n");
   spin_unlock_irq();

And the same into cleanup.

Ciao,

Rodolfo

Signature · 5 lines
GNU/Linux Solutions                  e-mail: [email protected]
Linux Device Driver                          [email protected]
Embedded Systems                     phone:  +39 349 2432127
UNIX programming                     skype:  rodolfo.giometti
Freelance ICT Italia - Consulente ICT Italia - www.consulenti-ict.it
#10 lore raw

Re: [PATCHv4 05/17] pps: access pps device by direct pointer

В Sun, 21 Nov 2010 09:26:58 +0100
Rodolfo Giometti <[email protected]> пишет:

Quoted 69 lines · Alexander Gordeev
Quoted 55 lines
Quoted 8 lines
Quoted 8 lines
drivers/pps/clients/pps-ldisc.c +3 -3
-	if (ret < 0) {
+	if (pps == NULL) {
 		pr_err("cannot register PPS source \"%s\"\n", info.path);
-		return ret;
+		return -ENOMEM;
 	}
-	tty->disc_data = (void *)(long)ret;
+
Quoted 8 lines

drivers/pps/clients/pps-ldisc.c static int pps_tty_open(struct tty_struct *tty) · line 86

Quoted 3 lines
drivers/pps/clients/pps-ldisc.c +3 -0
+	spin_lock_irq(&pps_ldisc_lock);
+	tty->disc_data = pps;
+	spin_unlock_irq(&pps_ldisc_lock);

Maybe this lock is useless... however, are we sure that before setting tty->disc_data to pps its value is null? Otherwise the dcd_change may be called with an oops! We cannot control serial port IRQ generation... :-/

No, locking here is necessary.
There is only one problem this spinlock protects us from: current tty code neither disables interrupts nor doesn't ensure there are no references to PPS ldisc from uart_handle_dcd_change() before closing it (and removing PPS source). It relies on flushing workqueue and disabling input. It worked good this way until dcd_change() was added which doesn't use workqueues and is called in atomic context so can't lock on mutex.

Imagine that (on SMP system) uart_handle_dcd_change() could obtain a reference to ldisc and call dcd_change() until actually calling pps_event(); then on another processor all the path from tty_ldisc_halt() until tty_ldisc_stop() is executed. And then pps_event() is called with illegal pps pointer.

I just thought you are right that disc_data can be set not NULL by another ldisc and it's a problem. But actually I just realised how to fix it completely. :)

I just have to add a spinlock to tty_struct, lock all the uart_handle_dcd_change() with it and add a "barrier" between tty_ldisc_halt() and tty_ldisc_close() i.e. just that:

...
spin_lock_irq();
spin_unlock_irq();
...

This "barrier" will ensure that there is no references to ldisc from uart_handle_dcd_change(). It won't be able to obtain a new reference after tty_ldisc_halt() so will become completely sane. Not disabling interrupts won't be a problem because it won't be able to obtain an ldisc reference until tty_ldisc_enable() which is called only after the new ldisc is fully functional. If it's our ldisc than it will have both dcd_change defined and a valid pps pointer. If it's not our ldisc it won't have both so uart_handle_dcd_change() won't call dcd_change() at all.

Quoted 1 line

I think I'll do that as a separate patch.

Excuse me but IMHO you should solve all your problems if you do the lock into pps_tty_init/cleanup instead of into pps_tty_open/close.

   spin_lock_irq();
           err = tty_register_ldisc(N_PPS, &pps_ldisc_ops);
           if (err)
                   pr_err("can't register PPS line discipline\n");
           else
                   pr_info("PPS line discipline registered\n");
   spin_unlock_irq();
Quoted 1 line

And the same into cleanup.

Sorry, I don't understand how this is supposed to help. Anyway, a new patch is underway.

Signature · 1 line
  Alexander
#11 lore raw

Re: [PATCHv4 05/17] pps: access pps device by direct pointer

In reply to Rodolfo Giometti
Quoted 5 lines

Maybe this lock is useless... however, are we sure that before setting tty->disc_data to pps its value is null? Otherwise the dcd_change may be called with an oops! We cannot control serial port IRQ generation... :-/

tty->disc_data is LDISC private. It may be any arbitary value on entry to the ldisc open method.

Quoted 3 lines

drivers/pps/clients/pps-ldisc.c static void (*alias_n_tty_close)(struct tty_struct *tty); · line 107

Quoted 3 lines
drivers/pps/clients/pps-ldisc.c +3 -0
+	spin_lock_irq(&pps_ldisc_lock);
+	tty->disc_data = NULL;
+	spin_unlock_irq(&pps_ldisc_lock);

And you don't need to set it to NULL after - some ldiscs do this mostly as a debug aid.

Alan

#12 lore raw

Re: [PATCHv4 05/17] pps: access pps device by direct pointer

В Sun, 21 Nov 2010 14:12:26 +0000
Alan Cox <[email protected]> пишет:

Quoted 10 lines
Quoted 5 lines

Maybe this lock is useless... however, are we sure that before setting tty->disc_data to pps its value is null? Otherwise the dcd_change may be called with an oops! We cannot control serial port IRQ generation... :-/

tty->disc_data is LDISC private. It may be any arbitary value on entry to the ldisc open method.

Quoted 6 lines
Quoted 3 lines

drivers/pps/clients/pps-ldisc.c static void (*alias_n_tty_close)(struct tty_struct *tty); · line 107

Quoted 3 lines
drivers/pps/clients/pps-ldisc.c +3 -0
+	spin_lock_irq(&pps_ldisc_lock);
+	tty->disc_data = NULL;
+	spin_unlock_irq(&pps_ldisc_lock);

And you don't need to set it to NULL after - some ldiscs do this mostly as a debug aid.

It was a part of attempt to do a workaround. But it still is not safe enough because it doesn't protect from a sudden ldisc change while we are at the beginning of pps_tty_dcd_change() i.e. haven't acquired spin lock yet. The real fix is ready, I'll send it soon.

Signature · 1 line
  Alexander
#13 lore raw

PATCH v4 6/17 pps: convert printk/pr_* to dev_*

In reply to Alexander Gordeev

Since we now have direct pointers to struct pps_device everywhere it's easy to use dev_* functions to print messages instead of plain printks. Where dev_* cannot be used printks are converted to pr_*.

---
 drivers/pps/clients/pps-ktimer.c |    5 +++--
 drivers/pps/clients/pps-ldisc.c  |    2 ++
 drivers/pps/kapi.c               |   15 ++++++++-------
 drivers/pps/pps.c                |   25 +++++++++++++------------
 4 files changed, 26 insertions(+), 21 deletions(-)
drivers/pps/clients/pps-ktimer.c +3 -2
index 966ead1..2728469 100644
@@ -19,6 +19,7 @@
  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
 #include <linux/kernel.h>
 #include <linux/module.h>
@@ -45,7 +46,7 @@ static void pps_ktimer_event(unsigned long ptr)
 	/* First of all we get the time stamp... */
 	pps_get_ts(&ts);
 
-	pr_info("PPS event at %lu\n", jiffies);
+	dev_info(pps->dev, "PPS event at %lu\n", jiffies);
 
 	pps_event(pps, &ts, PPS_CAPTUREASSERT, NULL);
 
@@ -94,7 +95,7 @@ static int __init pps_ktimer_init(void)
 	pps = pps_register_source(&pps_ktimer_info,
 				PPS_CAPTUREASSERT | PPS_OFFSETASSERT);
 	if (pps == NULL) {
-		printk(KERN_ERR "cannot register ktimer source\n");
+		pr_err("cannot register PPS source\n");
 		return -ENOMEM;
 	}
 
drivers/pps/clients/pps-ldisc.c +2 -0
index 1950b15..30789fa 100644
@@ -19,6 +19,8 @@
  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
 #include <linux/module.h>
 #include <linux/serial_core.h>
 #include <linux/tty.h>
drivers/pps/kapi.c +8 -7
index 98d4012..fbb89c6 100644
@@ -19,6 +19,7 @@
  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
 #include <linux/kernel.h>
 #include <linux/module.h>
@@ -80,20 +81,20 @@ struct pps_device *pps_register_source(struct pps_source_info *info,
 
 	/* Sanity checks */
 	if ((info->mode & default_params) != default_params) {
-		printk(KERN_ERR "pps: %s: unsupported default parameters\n",
+		pr_err("pps: %s: unsupported default parameters\n",
 					info->name);
 		err = -EINVAL;
 		goto pps_register_source_exit;
 	}
 	if ((info->mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) != 0 &&
 			info->echo == NULL) {
-		printk(KERN_ERR "pps: %s: echo function is not defined\n",
+		pr_err("pps: %s: echo function is not defined\n",
 					info->name);
 		err = -EINVAL;
 		goto pps_register_source_exit;
 	}
 	if ((info->mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
-		printk(KERN_ERR "pps: %s: unspecified time format\n",
+		pr_err("pps: %s: unspecified time format\n",
 					info->name);
 		err = -EINVAL;
 		goto pps_register_source_exit;
@@ -138,7 +139,7 @@ struct pps_device *pps_register_source(struct pps_source_info *info,
 	if (id >= PPS_MAX_SOURCES) {
 		spin_unlock_irq(&pps_idr_lock);
 
-		printk(KERN_ERR "pps: %s: too many PPS sources in the system\n",
+		pr_err("pps: %s: too many PPS sources in the system\n",
 					info->name);
 		err = -EBUSY;
 		goto free_idr;
@@ -150,12 +151,12 @@ struct pps_device *pps_register_source(struct pps_source_info *info,
 	/* Create the char device */
 	err = pps_register_cdev(pps);
 	if (err < 0) {
-		printk(KERN_ERR "pps: %s: unable to create char device\n",
+		pr_err("%s: unable to create char device\n",
 					info->name);
 		goto free_idr;
 	}
 
-	pr_info("new PPS source %s at ID %d\n", info->name, id);
+	dev_info(pps->dev, "new PPS source %s\n", info->name);
 
 	return pps;
 
@@ -168,7 +169,7 @@ kfree_pps:
 	kfree(pps);
 
 pps_register_source_exit:
-	printk(KERN_ERR "pps: %s: unable to register source\n", info->name);
+	pr_err("%s: unable to register source\n", info->name);
 
 	return NULL;
 }
drivers/pps/pps.c +13 -12
index 1922f27..9f7c2e8 100644
@@ -19,6 +19,7 @@
  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
 #include <linux/kernel.h>
 #include <linux/module.h>
@@ -67,7 +68,7 @@ static long pps_cdev_ioctl(struct file *file,
 
 	switch (cmd) {
 	case PPS_GETPARAMS:
-		pr_debug("PPS_GETPARAMS: source %d\n", pps->id);
+		dev_dbg(pps->dev, "PPS_GETPARAMS\n");
 
 		spin_lock_irq(&pps->lock);
 
@@ -83,7 +84,7 @@ static long pps_cdev_ioctl(struct file *file,
 		break;
 
 	case PPS_SETPARAMS:
-		pr_debug("PPS_SETPARAMS: source %d\n", pps->id);
+		dev_dbg(pps->dev, "PPS_SETPARAMS\n");
 
 		/* Check the capabilities */
 		if (!capable(CAP_SYS_TIME))
@@ -93,14 +94,14 @@ static long pps_cdev_ioctl(struct file *file,
 		if (err)
 			return -EFAULT;
 		if (!(params.mode & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR))) {
-			pr_debug("capture mode unspecified (%x)\n",
+			dev_dbg(pps->dev, "capture mode unspecified (%x)\n",
 								params.mode);
 			return -EINVAL;
 		}
 
 		/* Check for supported capabilities */
 		if ((params.mode & ~pps->info.mode) != 0) {
-			pr_debug("unsupported capabilities (%x)\n",
+			dev_dbg(pps->dev, "unsupported capabilities (%x)\n",
 								params.mode);
 			return -EINVAL;
 		}
@@ -113,7 +114,7 @@ static long pps_cdev_ioctl(struct file *file,
 		/* Restore the read only parameters */
 		if ((params.mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
 			/* section 3.3 of RFC 2783 interpreted */
-			pr_debug("time format unspecified (%x)\n",
+			dev_dbg(pps->dev, "time format unspecified (%x)\n",
 								params.mode);
 			pps->params.mode |= PPS_TSFMT_TSPEC;
 		}
@@ -126,7 +127,7 @@ static long pps_cdev_ioctl(struct file *file,
 		break;
 
 	case PPS_GETCAP:
-		pr_debug("PPS_GETCAP: source %d\n", pps->id);
+		dev_dbg(pps->dev, "PPS_GETCAP\n");
 
 		err = put_user(pps->info.mode, iuarg);
 		if (err)
@@ -138,7 +139,7 @@ static long pps_cdev_ioctl(struct file *file,
 		struct pps_fdata fdata;
 		unsigned int ev;
 
-		pr_debug("PPS_FETCH: source %d\n", pps->id);
+		dev_dbg(pps->dev, "PPS_FETCH\n");
 
 		err = copy_from_user(&fdata, uarg, sizeof(struct pps_fdata));
 		if (err)
@@ -153,7 +154,7 @@ static long pps_cdev_ioctl(struct file *file,
 		else {
 			unsigned long ticks;
 
-			pr_debug("timeout %lld.%09d\n",
+			dev_dbg(pps->dev, "timeout %lld.%09d\n",
 					(long long) fdata.timeout.sec,
 					fdata.timeout.nsec);
 			ticks = fdata.timeout.sec * HZ;
@@ -171,7 +172,7 @@ static long pps_cdev_ioctl(struct file *file,
 
 		/* Check for pending signals */
 		if (err == -ERESTARTSYS) {
-			pr_debug("pending signal caught\n");
+			dev_dbg(pps->dev, "pending signal caught\n");
 			return -EINTR;
 		}
 
@@ -240,7 +241,7 @@ int pps_register_cdev(struct pps_device *pps)
 
 	err = cdev_add(&pps->cdev, devt, 1);
 	if (err) {
-		printk(KERN_ERR "pps: %s: failed to add char device %d:%d\n",
+		pr_err("%s: failed to add char device %d:%d\n",
 				pps->info.name, MAJOR(pps_devt), pps->id);
 		return err;
 	}
@@ -282,14 +283,14 @@ static int __init pps_init(void)
 
 	pps_class = class_create(THIS_MODULE, "pps");
 	if (!pps_class) {
-		printk(KERN_ERR "pps: failed to allocate class\n");
+		pr_err("failed to allocate class\n");
 		return -ENOMEM;
 	}
 	pps_class->dev_attrs = pps_attrs;
 
 	err = alloc_chrdev_region(&pps_devt, 0, PPS_MAX_SOURCES, "pps");
 	if (err < 0) {
-		printk(KERN_ERR "pps: failed to allocate char device region\n");
+		pr_err("failed to allocate char device region\n");
 		goto remove_class;
 	}
 
Signature · 1 line
1.7.2.3
maintainer Acked-by on v4 6/17
#14 lore raw

Re: [PATCHv4 06/17] pps: convert printk/pr_* to dev_*

Quoted 17 lines · Alexander Gordeev

Since we now have direct pointers to struct pps_device everywhere it's easy to use dev_* functions to print messages instead of plain printks. Where dev_* cannot be used printks are converted to pr_*.

---
 drivers/pps/clients/pps-ktimer.c |    5 +++--
 drivers/pps/clients/pps-ldisc.c  |    2 ++
 drivers/pps/kapi.c               |   15 ++++++++-------
 drivers/pps/pps.c                |   25 +++++++++++++------------
 4 files changed, 26 insertions(+), 21 deletions(-)
drivers/pps/clients/pps-ktimer.c +0 -0
index 966ead1..2728469 100644

drivers/pps/clients/pps-ktimer.c line 22

Quoted 4 lines
drivers/pps/clients/pps-ktimer.c +1 -0
@@ -19,6 +19,7 @@
  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

Instead of redefining pr_fmt() in each file you may do it once into include file pps_kernel.h...

Quoted 223 lines
drivers/pps/clients/pps-ktimer.c +2 -2
 #include <linux/kernel.h>
 #include <linux/module.h>
@@ -45,7 +46,7 @@ static void pps_ktimer_event(unsigned long ptr)
 	/* First of all we get the time stamp... */
 	pps_get_ts(&ts);
 
-	pr_info("PPS event at %lu\n", jiffies);
+	dev_info(pps->dev, "PPS event at %lu\n", jiffies);
 
 	pps_event(pps, &ts, PPS_CAPTUREASSERT, NULL);
 
@@ -94,7 +95,7 @@ static int __init pps_ktimer_init(void)
 	pps = pps_register_source(&pps_ktimer_info,
 				PPS_CAPTUREASSERT | PPS_OFFSETASSERT);
 	if (pps == NULL) {
-		printk(KERN_ERR "cannot register ktimer source\n");
+		pr_err("cannot register PPS source\n");
 		return -ENOMEM;
 	}
 
drivers/pps/clients/pps-ldisc.c +2 -0
index 1950b15..30789fa 100644
@@ -19,6 +19,8 @@
  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
 #include <linux/module.h>
 #include <linux/serial_core.h>
 #include <linux/tty.h>
drivers/pps/kapi.c +8 -7
index 98d4012..fbb89c6 100644
@@ -19,6 +19,7 @@
  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
 #include <linux/kernel.h>
 #include <linux/module.h>
@@ -80,20 +81,20 @@ struct pps_device *pps_register_source(struct pps_source_info *info,
 
 	/* Sanity checks */
 	if ((info->mode & default_params) != default_params) {
-		printk(KERN_ERR "pps: %s: unsupported default parameters\n",
+		pr_err("pps: %s: unsupported default parameters\n",
 					info->name);
 		err = -EINVAL;
 		goto pps_register_source_exit;
 	}
 	if ((info->mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) != 0 &&
 			info->echo == NULL) {
-		printk(KERN_ERR "pps: %s: echo function is not defined\n",
+		pr_err("pps: %s: echo function is not defined\n",
 					info->name);
 		err = -EINVAL;
 		goto pps_register_source_exit;
 	}
 	if ((info->mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
-		printk(KERN_ERR "pps: %s: unspecified time format\n",
+		pr_err("pps: %s: unspecified time format\n",
 					info->name);
 		err = -EINVAL;
 		goto pps_register_source_exit;
@@ -138,7 +139,7 @@ struct pps_device *pps_register_source(struct pps_source_info *info,
 	if (id >= PPS_MAX_SOURCES) {
 		spin_unlock_irq(&pps_idr_lock);
 
-		printk(KERN_ERR "pps: %s: too many PPS sources in the system\n",
+		pr_err("pps: %s: too many PPS sources in the system\n",
 					info->name);
 		err = -EBUSY;
 		goto free_idr;
@@ -150,12 +151,12 @@ struct pps_device *pps_register_source(struct pps_source_info *info,
 	/* Create the char device */
 	err = pps_register_cdev(pps);
 	if (err < 0) {
-		printk(KERN_ERR "pps: %s: unable to create char device\n",
+		pr_err("%s: unable to create char device\n",
 					info->name);
 		goto free_idr;
 	}
 
-	pr_info("new PPS source %s at ID %d\n", info->name, id);
+	dev_info(pps->dev, "new PPS source %s\n", info->name);
 
 	return pps;
 
@@ -168,7 +169,7 @@ kfree_pps:
 	kfree(pps);
 
 pps_register_source_exit:
-	printk(KERN_ERR "pps: %s: unable to register source\n", info->name);
+	pr_err("%s: unable to register source\n", info->name);
 
 	return NULL;
 }
drivers/pps/pps.c +13 -12
index 1922f27..9f7c2e8 100644
@@ -19,6 +19,7 @@
  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
 #include <linux/kernel.h>
 #include <linux/module.h>
@@ -67,7 +68,7 @@ static long pps_cdev_ioctl(struct file *file,
 
 	switch (cmd) {
 	case PPS_GETPARAMS:
-		pr_debug("PPS_GETPARAMS: source %d\n", pps->id);
+		dev_dbg(pps->dev, "PPS_GETPARAMS\n");
 
 		spin_lock_irq(&pps->lock);
 
@@ -83,7 +84,7 @@ static long pps_cdev_ioctl(struct file *file,
 		break;
 
 	case PPS_SETPARAMS:
-		pr_debug("PPS_SETPARAMS: source %d\n", pps->id);
+		dev_dbg(pps->dev, "PPS_SETPARAMS\n");
 
 		/* Check the capabilities */
 		if (!capable(CAP_SYS_TIME))
@@ -93,14 +94,14 @@ static long pps_cdev_ioctl(struct file *file,
 		if (err)
 			return -EFAULT;
 		if (!(params.mode & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR))) {
-			pr_debug("capture mode unspecified (%x)\n",
+			dev_dbg(pps->dev, "capture mode unspecified (%x)\n",
 								params.mode);
 			return -EINVAL;
 		}
 
 		/* Check for supported capabilities */
 		if ((params.mode & ~pps->info.mode) != 0) {
-			pr_debug("unsupported capabilities (%x)\n",
+			dev_dbg(pps->dev, "unsupported capabilities (%x)\n",
 								params.mode);
 			return -EINVAL;
 		}
@@ -113,7 +114,7 @@ static long pps_cdev_ioctl(struct file *file,
 		/* Restore the read only parameters */
 		if ((params.mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
 			/* section 3.3 of RFC 2783 interpreted */
-			pr_debug("time format unspecified (%x)\n",
+			dev_dbg(pps->dev, "time format unspecified (%x)\n",
 								params.mode);
 			pps->params.mode |= PPS_TSFMT_TSPEC;
 		}
@@ -126,7 +127,7 @@ static long pps_cdev_ioctl(struct file *file,
 		break;
 
 	case PPS_GETCAP:
-		pr_debug("PPS_GETCAP: source %d\n", pps->id);
+		dev_dbg(pps->dev, "PPS_GETCAP\n");
 
 		err = put_user(pps->info.mode, iuarg);
 		if (err)
@@ -138,7 +139,7 @@ static long pps_cdev_ioctl(struct file *file,
 		struct pps_fdata fdata;
 		unsigned int ev;
 
-		pr_debug("PPS_FETCH: source %d\n", pps->id);
+		dev_dbg(pps->dev, "PPS_FETCH\n");
 
 		err = copy_from_user(&fdata, uarg, sizeof(struct pps_fdata));
 		if (err)
@@ -153,7 +154,7 @@ static long pps_cdev_ioctl(struct file *file,
 		else {
 			unsigned long ticks;
 
-			pr_debug("timeout %lld.%09d\n",
+			dev_dbg(pps->dev, "timeout %lld.%09d\n",
 					(long long) fdata.timeout.sec,
 					fdata.timeout.nsec);
 			ticks = fdata.timeout.sec * HZ;
@@ -171,7 +172,7 @@ static long pps_cdev_ioctl(struct file *file,
 
 		/* Check for pending signals */
 		if (err == -ERESTARTSYS) {
-			pr_debug("pending signal caught\n");
+			dev_dbg(pps->dev, "pending signal caught\n");
 			return -EINTR;
 		}
 
@@ -240,7 +241,7 @@ int pps_register_cdev(struct pps_device *pps)
 
 	err = cdev_add(&pps->cdev, devt, 1);
 	if (err) {
-		printk(KERN_ERR "pps: %s: failed to add char device %d:%d\n",
+		pr_err("%s: failed to add char device %d:%d\n",
 				pps->info.name, MAJOR(pps_devt), pps->id);
 		return err;
 	}
@@ -282,14 +283,14 @@ static int __init pps_init(void)
 
 	pps_class = class_create(THIS_MODULE, "pps");
 	if (!pps_class) {
-		printk(KERN_ERR "pps: failed to allocate class\n");
+		pr_err("failed to allocate class\n");
 		return -ENOMEM;
 	}
 	pps_class->dev_attrs = pps_attrs;
 
 	err = alloc_chrdev_region(&pps_devt, 0, PPS_MAX_SOURCES, "pps");
 	if (err < 0) {
-		printk(KERN_ERR "pps: failed to allocate char device region\n");
+		pr_err("failed to allocate char device region\n");
 		goto remove_class;
 	}
 
Signature · 1 line
1.7.2.3
Signature · 5 lines
GNU/Linux Solutions                  e-mail: [email protected]
Linux Device Driver                          [email protected]
Embedded Systems                     phone:  +39 349 2432127
UNIX programming                     skype:  rodolfo.giometti
Freelance ICT Italia - Consulente ICT Italia - www.consulenti-ict.it
#15 lore raw

Re: [PATCHv4 06/17] pps: convert printk/pr_* to dev_*

В Sat, 20 Nov 2010 16:49:40 +0100
Rodolfo Giometti <[email protected]> пишет:

Quoted 21 lines · Alexander Gordeev
Quoted 20 lines

Since we now have direct pointers to struct pps_device everywhere it's easy to use dev_* functions to print messages instead of plain printks. Where dev_* cannot be used printks are converted to pr_*.

---
 drivers/pps/clients/pps-ktimer.c |    5 +++--
 drivers/pps/clients/pps-ldisc.c  |    2 ++
 drivers/pps/kapi.c               |   15 ++++++++-------
 drivers/pps/pps.c                |   25 +++++++++++++------------
 4 files changed, 26 insertions(+), 21 deletions(-)
drivers/pps/clients/pps-ktimer.c +0 -0
index 966ead1..2728469 100644
@@ -19,6 +19,7 @@
  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
Quoted 4 lines

drivers/pps/clients/pps-ktimer.c line 22

Quoted 1 line
drivers/pps/clients/pps-ktimer.c +1 -0
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

Instead of redefining pr_fmt() in each file you may do it once into include file pps_kernel.h...

Yes, but then every .c file will have to include linux/pps_kernel.h before including linux/kernel.h (where pr_* are defined) and only you and me will know the reason for that. Of course, we can add a comment to every .c file to discourage others from moving includes but IMHO the way how it is done in the patch is better. I dislike hidden dependencies between header files very much.

Quoted 226 lines
Quoted 223 lines
drivers/pps/clients/pps-ktimer.c +2 -2
 #include <linux/kernel.h>
 #include <linux/module.h>
@@ -45,7 +46,7 @@ static void pps_ktimer_event(unsigned long ptr)
 	/* First of all we get the time stamp... */
 	pps_get_ts(&ts);
 
-	pr_info("PPS event at %lu\n", jiffies);
+	dev_info(pps->dev, "PPS event at %lu\n", jiffies);
 
 	pps_event(pps, &ts, PPS_CAPTUREASSERT, NULL);
 
@@ -94,7 +95,7 @@ static int __init pps_ktimer_init(void)
 	pps = pps_register_source(&pps_ktimer_info,
 				PPS_CAPTUREASSERT | PPS_OFFSETASSERT);
 	if (pps == NULL) {
-		printk(KERN_ERR "cannot register ktimer source\n");
+		pr_err("cannot register PPS source\n");
 		return -ENOMEM;
 	}
 
drivers/pps/clients/pps-ldisc.c +2 -0
index 1950b15..30789fa 100644
@@ -19,6 +19,8 @@
  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
 #include <linux/module.h>
 #include <linux/serial_core.h>
 #include <linux/tty.h>
drivers/pps/kapi.c +8 -7
index 98d4012..fbb89c6 100644
@@ -19,6 +19,7 @@
  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
 #include <linux/kernel.h>
 #include <linux/module.h>
@@ -80,20 +81,20 @@ struct pps_device *pps_register_source(struct pps_source_info *info,
 
 	/* Sanity checks */
 	if ((info->mode & default_params) != default_params) {
-		printk(KERN_ERR "pps: %s: unsupported default parameters\n",
+		pr_err("pps: %s: unsupported default parameters\n",
 					info->name);
 		err = -EINVAL;
 		goto pps_register_source_exit;
 	}
 	if ((info->mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) != 0 &&
 			info->echo == NULL) {
-		printk(KERN_ERR "pps: %s: echo function is not defined\n",
+		pr_err("pps: %s: echo function is not defined\n",
 					info->name);
 		err = -EINVAL;
 		goto pps_register_source_exit;
 	}
 	if ((info->mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
-		printk(KERN_ERR "pps: %s: unspecified time format\n",
+		pr_err("pps: %s: unspecified time format\n",
 					info->name);
 		err = -EINVAL;
 		goto pps_register_source_exit;
@@ -138,7 +139,7 @@ struct pps_device *pps_register_source(struct pps_source_info *info,
 	if (id >= PPS_MAX_SOURCES) {
 		spin_unlock_irq(&pps_idr_lock);
 
-		printk(KERN_ERR "pps: %s: too many PPS sources in the system\n",
+		pr_err("pps: %s: too many PPS sources in the system\n",
 					info->name);
 		err = -EBUSY;
 		goto free_idr;
@@ -150,12 +151,12 @@ struct pps_device *pps_register_source(struct pps_source_info *info,
 	/* Create the char device */
 	err = pps_register_cdev(pps);
 	if (err < 0) {
-		printk(KERN_ERR "pps: %s: unable to create char device\n",
+		pr_err("%s: unable to create char device\n",
 					info->name);
 		goto free_idr;
 	}
 
-	pr_info("new PPS source %s at ID %d\n", info->name, id);
+	dev_info(pps->dev, "new PPS source %s\n", info->name);
 
 	return pps;
 
@@ -168,7 +169,7 @@ kfree_pps:
 	kfree(pps);
 
 pps_register_source_exit:
-	printk(KERN_ERR "pps: %s: unable to register source\n", info->name);
+	pr_err("%s: unable to register source\n", info->name);
 
 	return NULL;
 }
drivers/pps/pps.c +13 -12
index 1922f27..9f7c2e8 100644
@@ -19,6 +19,7 @@
  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
 #include <linux/kernel.h>
 #include <linux/module.h>
@@ -67,7 +68,7 @@ static long pps_cdev_ioctl(struct file *file,
 
 	switch (cmd) {
 	case PPS_GETPARAMS:
-		pr_debug("PPS_GETPARAMS: source %d\n", pps->id);
+		dev_dbg(pps->dev, "PPS_GETPARAMS\n");
 
 		spin_lock_irq(&pps->lock);
 
@@ -83,7 +84,7 @@ static long pps_cdev_ioctl(struct file *file,
 		break;
 
 	case PPS_SETPARAMS:
-		pr_debug("PPS_SETPARAMS: source %d\n", pps->id);
+		dev_dbg(pps->dev, "PPS_SETPARAMS\n");
 
 		/* Check the capabilities */
 		if (!capable(CAP_SYS_TIME))
@@ -93,14 +94,14 @@ static long pps_cdev_ioctl(struct file *file,
 		if (err)
 			return -EFAULT;
 		if (!(params.mode & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR))) {
-			pr_debug("capture mode unspecified (%x)\n",
+			dev_dbg(pps->dev, "capture mode unspecified (%x)\n",
 								params.mode);
 			return -EINVAL;
 		}
 
 		/* Check for supported capabilities */
 		if ((params.mode & ~pps->info.mode) != 0) {
-			pr_debug("unsupported capabilities (%x)\n",
+			dev_dbg(pps->dev, "unsupported capabilities (%x)\n",
 								params.mode);
 			return -EINVAL;
 		}
@@ -113,7 +114,7 @@ static long pps_cdev_ioctl(struct file *file,
 		/* Restore the read only parameters */
 		if ((params.mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
 			/* section 3.3 of RFC 2783 interpreted */
-			pr_debug("time format unspecified (%x)\n",
+			dev_dbg(pps->dev, "time format unspecified (%x)\n",
 								params.mode);
 			pps->params.mode |= PPS_TSFMT_TSPEC;
 		}
@@ -126,7 +127,7 @@ static long pps_cdev_ioctl(struct file *file,
 		break;
 
 	case PPS_GETCAP:
-		pr_debug("PPS_GETCAP: source %d\n", pps->id);
+		dev_dbg(pps->dev, "PPS_GETCAP\n");
 
 		err = put_user(pps->info.mode, iuarg);
 		if (err)
@@ -138,7 +139,7 @@ static long pps_cdev_ioctl(struct file *file,
 		struct pps_fdata fdata;
 		unsigned int ev;
 
-		pr_debug("PPS_FETCH: source %d\n", pps->id);
+		dev_dbg(pps->dev, "PPS_FETCH\n");
 
 		err = copy_from_user(&fdata, uarg, sizeof(struct pps_fdata));
 		if (err)
@@ -153,7 +154,7 @@ static long pps_cdev_ioctl(struct file *file,
 		else {
 			unsigned long ticks;
 
-			pr_debug("timeout %lld.%09d\n",
+			dev_dbg(pps->dev, "timeout %lld.%09d\n",
 					(long long) fdata.timeout.sec,
 					fdata.timeout.nsec);
 			ticks = fdata.timeout.sec * HZ;
@@ -171,7 +172,7 @@ static long pps_cdev_ioctl(struct file *file,
 
 		/* Check for pending signals */
 		if (err == -ERESTARTSYS) {
-			pr_debug("pending signal caught\n");
+			dev_dbg(pps->dev, "pending signal caught\n");
 			return -EINTR;
 		}
 
@@ -240,7 +241,7 @@ int pps_register_cdev(struct pps_device *pps)
 
 	err = cdev_add(&pps->cdev, devt, 1);
 	if (err) {
-		printk(KERN_ERR "pps: %s: failed to add char device %d:%d\n",
+		pr_err("%s: failed to add char device %d:%d\n",
 				pps->info.name, MAJOR(pps_devt), pps->id);
 		return err;
 	}
@@ -282,14 +283,14 @@ static int __init pps_init(void)
 
 	pps_class = class_create(THIS_MODULE, "pps");
 	if (!pps_class) {
-		printk(KERN_ERR "pps: failed to allocate class\n");
+		pr_err("failed to allocate class\n");
 		return -ENOMEM;
 	}
 	pps_class->dev_attrs = pps_attrs;
 
 	err = alloc_chrdev_region(&pps_devt, 0, PPS_MAX_SOURCES, "pps");
 	if (err < 0) {
-		printk(KERN_ERR "pps: failed to allocate char device region\n");
+		pr_err("failed to allocate char device region\n");
 		goto remove_class;
 	}
 
Signature · 1 line
1.7.2.3
Signature · 1 line
  Alexander
#16 lore raw

Re: [PATCHv4 06/17] pps: convert printk/pr_* to dev_*

Quoted 7 lines · Alexander Gordeev

В Sat, 20 Nov 2010 16:49:40 +0100
Rodolfo Giometti <[email protected]> пишет:

Quoted 5 lines · Alexander Gordeev

drivers/pps/kapi.c

Quoted 4 lines

Since we now have direct pointers to struct pps_device everywhere it's easy to use dev_* functions to print messages instead of plain printks. Where dev_* cannot be used printks are converted to pr_*.

drivers/pps/kapi.c +0 -0

[]

Quoted 7 lines
Quoted 7 lines

drivers/pps/kapi.c struct pps_device *pps_register_source(struct pps_source_info *info, · line 85

Quoted 7 lines
drivers/pps/kapi.c +1 -1
@@ -80,20 +81,20 @@ struct pps_device *pps_register_source(struct pps_source_info *info,
 
 	/* Sanity checks */
 	if ((info->mode & default_params) != default_params) {
-		printk(KERN_ERR "pps: %s: unsupported default parameters\n",
+		pr_err("pps: %s: unsupported default parameters\n",
 					info->name);

Perhaps you want to remove the "pps:" prefixes?

Quoted 2 lines
Quoted 2 lines

drivers/pps/kapi.c struct pps_device *pps_register_source(struct pps_source_info *info, · line 91

Quoted 2 lines
drivers/pps/kapi.c +1 -1
-		printk(KERN_ERR "pps: %s: echo function is not defined\n",
+		pr_err("pps: %s: echo function is not defined\n",

[]

Quoted 2 lines
Quoted 2 lines

drivers/pps/kapi.c struct pps_device *pps_register_source(struct pps_source_info *info, · line 97

Quoted 2 lines
drivers/pps/kapi.c +1 -1
-		printk(KERN_ERR "pps: %s: unspecified time format\n",
+		pr_err("pps: %s: unspecified time format\n",

[]

Quoted 2 lines
Quoted 2 lines

drivers/pps/kapi.c struct pps_device *pps_register_source(struct pps_source_info *info, · line 142

Quoted 2 lines
drivers/pps/kapi.c +1 -1
-		printk(KERN_ERR "pps: %s: too many PPS sources in the system\n",
+		pr_err("pps: %s: too many PPS sources in the system\n",

[]

Quoted 2 lines
Quoted 2 lines

drivers/pps/kapi.c struct pps_device *pps_register_source(struct pps_source_info *info, · line 154

Quoted 2 lines
drivers/pps/kapi.c +1 -1
-		printk(KERN_ERR "pps: %s: unable to create char device\n",
+		pr_err("%s: unable to create char device\n",

It's removed here.

Quoted 2 lines
Quoted 2 lines

drivers/pps/kapi.c kfree_pps: · line 172

Quoted 2 lines
drivers/pps/kapi.c +1 -1
-	printk(KERN_ERR "pps: %s: unable to register source\n", info->name);
+	pr_err("%s: unable to register source\n", info->name);

and here.

#17 lore raw

Re: [PATCHv4 06/17] pps: convert printk/pr_* to dev_*

В Sat, 20 Nov 2010 13:42:34 -0800
Joe Perches <[email protected]> пишет:

Quoted 33 lines · Alexander Gordeev
Quoted 7 lines

В Sat, 20 Nov 2010 16:49:40 +0100
Rodolfo Giometti <[email protected]> пишет:

Quoted 5 lines · Alexander Gordeev

drivers/pps/kapi.c

Quoted 4 lines

Since we now have direct pointers to struct pps_device everywhere it's easy to use dev_* functions to print messages instead of plain printks. Where dev_* cannot be used printks are converted to pr_*.

drivers/pps/kapi.c +0 -0

[]

Quoted 7 lines
Quoted 7 lines

drivers/pps/kapi.c struct pps_device *pps_register_source(struct pps_source_info *info, · line 85

Quoted 7 lines
drivers/pps/kapi.c +1 -1
@@ -80,20 +81,20 @@ struct pps_device *pps_register_source(struct pps_source_info *info,
 
 	/* Sanity checks */
 	if ((info->mode & default_params) != default_params) {
-		printk(KERN_ERR "pps: %s: unsupported default parameters\n",
+		pr_err("pps: %s: unsupported default parameters\n",
 					info->name);

Perhaps you want to remove the "pps:" prefixes?

Quoted 2 lines
Quoted 2 lines

drivers/pps/kapi.c struct pps_device *pps_register_source(struct pps_source_info *info, · line 91

Quoted 2 lines
drivers/pps/kapi.c +1 -1
-		printk(KERN_ERR "pps: %s: echo function is not defined\n",
+		pr_err("pps: %s: echo function is not defined\n",

[]

Quoted 2 lines
Quoted 2 lines

drivers/pps/kapi.c struct pps_device *pps_register_source(struct pps_source_info *info, · line 97

Quoted 2 lines
drivers/pps/kapi.c +1 -1
-		printk(KERN_ERR "pps: %s: unspecified time format\n",
+		pr_err("pps: %s: unspecified time format\n",

[]

Quoted 2 lines
Quoted 2 lines

drivers/pps/kapi.c struct pps_device *pps_register_source(struct pps_source_info *info, · line 142

Quoted 2 lines
drivers/pps/kapi.c +1 -1
-		printk(KERN_ERR "pps: %s: too many PPS sources in the system\n",
+		pr_err("pps: %s: too many PPS sources in the system\n",

[]

Quoted 2 lines
Quoted 2 lines

drivers/pps/kapi.c struct pps_device *pps_register_source(struct pps_source_info *info, · line 154

Quoted 2 lines
drivers/pps/kapi.c +1 -1
-		printk(KERN_ERR "pps: %s: unable to create char device\n",
+		pr_err("%s: unable to create char device\n",

It's removed here.

Quoted 4 lines
Quoted 2 lines
Quoted 2 lines

drivers/pps/kapi.c kfree_pps: · line 172

Quoted 2 lines
drivers/pps/kapi.c +1 -1
-	printk(KERN_ERR "pps: %s: unable to register source\n", info->name);
+	pr_err("%s: unable to register source\n", info->name);

and here.

Thanks! Strange that I overlooked it.

Signature · 1 line
  Alexander
maintainer Acked-by
#18 lore raw

Re: [PATCHv4 06/17] pps: convert printk/pr_* to dev_*

In reply to Alexander Gordeev
Quoted 29 lines · Alexander Gordeev

?? Sat, 20 Nov 2010 16:49:40 +0100
Rodolfo Giometti <[email protected]> ??????????:

Quoted 21 lines · Alexander Gordeev
Quoted 20 lines

Since we now have direct pointers to struct pps_device everywhere it's easy to use dev_* functions to print messages instead of plain printks. Where dev_* cannot be used printks are converted to pr_*.

---
 drivers/pps/clients/pps-ktimer.c |    5 +++--
 drivers/pps/clients/pps-ldisc.c  |    2 ++
 drivers/pps/kapi.c               |   15 ++++++++-------
 drivers/pps/pps.c                |   25 +++++++++++++------------
 4 files changed, 26 insertions(+), 21 deletions(-)
drivers/pps/clients/pps-ktimer.c +0 -0
index 966ead1..2728469 100644
@@ -19,6 +19,7 @@
  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
Quoted 4 lines

drivers/pps/clients/pps-ktimer.c line 22

Quoted 1 line
drivers/pps/clients/pps-ktimer.c +1 -0
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

Instead of redefining pr_fmt() in each file you may do it once into include file pps_kernel.h...

Quoted 6 lines

Yes, but then every .c file will have to include linux/pps_kernel.h before including linux/kernel.h (where pr_* are defined) and only you and me will know the reason for that. Of course, we can add a comment to every .c file to discourage others from moving includes but IMHO the way how it is done in the patch is better. I dislike hidden dependencies between header files very much.

Ok.

Signature · 5 lines
GNU/Linux Solutions                  e-mail: [email protected]
Linux Device Driver                          [email protected]
Embedded Systems                     phone:  +39 349 2432127
UNIX programming                     skype:  rodolfo.giometti
Freelance ICT Italia - Consulente ICT Italia - www.consulenti-ict.it
#19 lore raw

PATCH v4 7/17 pps: move idr stuff to pps.c

In reply to Alexander Gordeev

Since now idr is only used to manage char device id's and not used in kernel API anymore it should be moved to pps.c. This also makes it possible to release id only at actual device freeing so nobody can register a pps device with the same id while our device is not freed yet.

---
 drivers/pps/kapi.c |   56 ++-------------------------------------------------
 drivers/pps/pps.c  |   50 +++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 52 insertions(+), 54 deletions(-)
drivers/pps/kapi.c +3 -53
index fbb89c6..f5b2b78 100644
@@ -27,19 +27,11 @@
 #include <linux/sched.h>
 #include <linux/time.h>
 #include <linux/spinlock.h>
-#include <linux/idr.h>
 #include <linux/fs.h>
 #include <linux/pps_kernel.h>
 #include <linux/slab.h>
 
 /*
- * Local variables
- */
-
-static DEFINE_SPINLOCK(pps_idr_lock);
-static DEFINE_IDR(pps_idr);
-
-/*
  * Local functions
  */
 
@@ -76,7 +68,6 @@ struct pps_device *pps_register_source(struct pps_source_info *info,
 		int default_params)
 {
 	struct pps_device *pps;
-	int id;
 	int err;
 
 	/* Sanity checks */
@@ -117,54 +108,18 @@ struct pps_device *pps_register_source(struct pps_source_info *info,
 	init_waitqueue_head(&pps->queue);
 	spin_lock_init(&pps->lock);
 
-	/* Get new ID for the new PPS source */
-	if (idr_pre_get(&pps_idr, GFP_KERNEL) == 0) {
-		err = -ENOMEM;
-		goto kfree_pps;
-	}
-
-	spin_lock_irq(&pps_idr_lock);
-
-	/* Now really allocate the PPS source.
-	 * After idr_get_new() calling the new source will be freely available
-	 * into the kernel.
-	 */
-	err = idr_get_new(&pps_idr, pps, &id);
-	if (err < 0) {
-		spin_unlock_irq(&pps_idr_lock);
-		goto kfree_pps;
-	}
-
-	id = id & MAX_ID_MASK;
-	if (id >= PPS_MAX_SOURCES) {
-		spin_unlock_irq(&pps_idr_lock);
-
-		pr_err("pps: %s: too many PPS sources in the system\n",
-					info->name);
-		err = -EBUSY;
-		goto free_idr;
-	}
-	pps->id = id;
-
-	spin_unlock_irq(&pps_idr_lock);
-
 	/* Create the char device */
 	err = pps_register_cdev(pps);
 	if (err < 0) {
 		pr_err("%s: unable to create char device\n",
 					info->name);
-		goto free_idr;
+		goto kfree_pps;
 	}
 
 	dev_info(pps->dev, "new PPS source %s\n", info->name);
 
 	return pps;
 
-free_idr:
-	spin_lock_irq(&pps_idr_lock);
-	idr_remove(&pps_idr, id);
-	spin_unlock_irq(&pps_idr_lock);
-
 kfree_pps:
 	kfree(pps);
 
@@ -184,15 +139,10 @@ EXPORT_SYMBOL(pps_register_source);
 
 void pps_unregister_source(struct pps_device *pps)
 {
-	unsigned int id = pps->id;
-
 	pps_unregister_cdev(pps);
 
-	spin_lock_irq(&pps_idr_lock);
-	idr_remove(&pps_idr, pps->id);
-	spin_unlock_irq(&pps_idr_lock);
-
-	kfree(pps);
+	/* don't have to kfree(pps) here because it will be done on
+	 * device destruction */
 }
 EXPORT_SYMBOL(pps_unregister_source);
 
drivers/pps/pps.c +49 -1

91 lines not on this page · raw message

Signature · 1 line
1.7.2.3
maintainer
#20 lore raw

Re: [PATCHv4 07/17] pps: move idr stuff to pps.c

Quoted 5 lines · Alexander Gordeev

Since now idr is only used to manage char device id's and not used in kernel API anymore it should be moved to pps.c. This also makes it possible to release id only at actual device freeing so nobody can register a pps device with the same id while our device is not freed yet.

This patch must be delayed till patch 5 is not fixed.

Rodolfo

Signature · 5 lines
GNU/Linux Solutions                  e-mail: [email protected]
Linux Device Driver                          [email protected]
Embedded Systems                     phone:  +39 349 2432127
UNIX programming                     skype:  rodolfo.giometti
Freelance ICT Italia - Consulente ICT Italia - www.consulenti-ict.it
#21 lore raw

PATCH v4 8/17 pps: add async PPS event handler

In reply to Alexander Gordeev

This handler should be called from an IRQ handler. It uses per-device workqueue internally.

---
 drivers/pps/clients/pps-ktimer.c |    2 +-
 drivers/pps/clients/pps-ldisc.c  |    2 +-
 drivers/pps/kapi.c               |   95 ++++++++++++++++++++++++++++++++++++--
 drivers/pps/pps.c                |   14 +++++-
 include/linux/pps_kernel.h       |   12 +++++
 5 files changed, 117 insertions(+), 8 deletions(-)
drivers/pps/clients/pps-ktimer.c +1 -1
index 2728469..26ed7a2 100644
@@ -48,7 +48,7 @@ static void pps_ktimer_event(unsigned long ptr)
 
 	dev_info(pps->dev, "PPS event at %lu\n", jiffies);
 
-	pps_event(pps, &ts, PPS_CAPTUREASSERT, NULL);
+	pps_event_irq(pps, &ts, PPS_CAPTUREASSERT, NULL);
 
 	mod_timer(&ktimer, jiffies + HZ);
 }
drivers/pps/clients/pps-ldisc.c +1 -1
index 30789fa..7006f85 100644
@@ -50,7 +50,7 @@ static void pps_tty_dcd_change(struct tty_struct *tty, unsigned int status,
 	/* Now do the PPS event report */
 	pps = (struct pps_device *)tty->disc_data;
 	if (pps != NULL) {
-		pps_event(pps, ts, status ? PPS_CAPTUREASSERT :
+		pps_event_irq(pps, ts, status ? PPS_CAPTUREASSERT :
 				PPS_CAPTURECLEAR, NULL);
 		spin_unlock_irqrestore(&pps_ldisc_lock, flags);
 		dev_dbg(pps->dev, "PPS %s at %lu\n",
drivers/pps/kapi.c +91 -4

120 lines not on this page · raw message

drivers/pps/pps.c +12 -2
index 79b4455..f642558 100644
@@ -321,18 +321,26 @@ void pps_unregister_cdev(struct pps_device *pps)
 
 static void __exit pps_exit(void)
 {
-	class_destroy(pps_class);
 	unregister_chrdev_region(pps_devt, PPS_MAX_SOURCES);
+	class_destroy(pps_class);
+	destroy_workqueue(pps_event_workqueue);
 }
 
 static int __init pps_init(void)
 {
 	int err;
 
+	pps_event_workqueue = create_workqueue("pps");
+	if (!pps_event_workqueue) {
+		pr_err("failed to create workqueue\n");
+		return -ENOMEM;
+	}
+
 	pps_class = class_create(THIS_MODULE, "pps");
 	if (!pps_class) {
 		pr_err("failed to allocate class\n");
-		return -ENOMEM;
+		err = -ENOMEM;
+		goto destroy_workqueue;
 	}
 	pps_class->dev_attrs = pps_attrs;
 
@@ -350,6 +358,8 @@ static int __init pps_init(void)
 
 remove_class:
 	class_destroy(pps_class);
+destroy_workqueue:
+	destroy_workqueue(pps_event_workqueue);
 
 	return err;
 }
include/linux/pps_kernel.h +12 -0
index 1aedf50..5af0498 100644
@@ -26,6 +26,7 @@
 #include <linux/cdev.h>
 #include <linux/device.h>
 #include <linux/time.h>
+#include <linux/workqueue.h>
 
 /*
  * Global defines
@@ -70,6 +71,13 @@ struct pps_device {
 	struct device *dev;
 	struct fasync_struct *async_queue;	/* fasync method */
 	spinlock_t lock;
+
+	struct work_struct assert_work;
+	struct work_struct clear_work;
+	struct pps_event_time assert_work_ts;
+	struct pps_event_time clear_work_ts;
+	void *assert_work_data;
+	void *clear_work_data;
 };
 
 /*
@@ -78,6 +86,8 @@ struct pps_device {
 
 extern struct device_attribute pps_attrs[];
 
+extern struct workqueue_struct *pps_event_workqueue;
+
 /*
  * Exported functions
  */
@@ -89,6 +99,8 @@ extern int pps_register_cdev(struct pps_device *pps);
 extern void pps_unregister_cdev(struct pps_device *pps);
 extern void pps_event(struct pps_device *pps,
 		struct pps_event_time *ts, int event, void *data);
+extern void pps_event_irq(struct pps_device *pps,
+		struct pps_event_time *ts, int event, void *data);
 
 static inline void timespec_to_pps_ktime(struct pps_ktime *kt,
 		struct timespec ts)
Signature · 1 line
1.7.2.3
maintainer
#22 lore raw

Re: [PATCHv4 08/17] pps: add async PPS event handler

Quoted 2 lines · Alexander Gordeev

This handler should be called from an IRQ handler. It uses per-device workqueue internally.

Can you please explain to me why do you need this patch? Maybe you can add a verbose patch's description? :)

Quoted 100 lines
---
 drivers/pps/clients/pps-ktimer.c |    2 +-
 drivers/pps/clients/pps-ldisc.c  |    2 +-
 drivers/pps/kapi.c               |   95 ++++++++++++++++++++++++++++++++++++--
 drivers/pps/pps.c                |   14 +++++-
 include/linux/pps_kernel.h       |   12 +++++
 5 files changed, 117 insertions(+), 8 deletions(-)
drivers/pps/clients/pps-ktimer.c +1 -1
index 2728469..26ed7a2 100644
@@ -48,7 +48,7 @@ static void pps_ktimer_event(unsigned long ptr)
 
 	dev_info(pps->dev, "PPS event at %lu\n", jiffies);
 
-	pps_event(pps, &ts, PPS_CAPTUREASSERT, NULL);
+	pps_event_irq(pps, &ts, PPS_CAPTUREASSERT, NULL);
 
 	mod_timer(&ktimer, jiffies + HZ);
 }
drivers/pps/clients/pps-ldisc.c +1 -1
index 30789fa..7006f85 100644
@@ -50,7 +50,7 @@ static void pps_tty_dcd_change(struct tty_struct *tty, unsigned int status,
 	/* Now do the PPS event report */
 	pps = (struct pps_device *)tty->disc_data;
 	if (pps != NULL) {
-		pps_event(pps, ts, status ? PPS_CAPTUREASSERT :
+		pps_event_irq(pps, ts, status ? PPS_CAPTUREASSERT :
 				PPS_CAPTURECLEAR, NULL);
 		spin_unlock_irqrestore(&pps_ldisc_lock, flags);
 		dev_dbg(pps->dev, "PPS %s at %lu\n",
drivers/pps/kapi.c +28 -4
index f5b2b78..ca3b4f8 100644
@@ -32,9 +32,19 @@
 #include <linux/slab.h>
 
 /*
+ * Global variables
+ */
+
+/* PPS event workqueue */
+struct workqueue_struct *pps_event_workqueue;
+
+/*
  * Local functions
  */
 
+static void assert_work_func(struct work_struct *work);
+static void clear_work_func(struct work_struct *work);
+
 static void pps_add_offset(struct pps_ktime *ts, struct pps_ktime *offset)
 {
 	ts->nsec += offset->nsec;
@@ -108,6 +118,9 @@ struct pps_device *pps_register_source(struct pps_source_info *info,
 	init_waitqueue_head(&pps->queue);
 	spin_lock_init(&pps->lock);
 
+	INIT_WORK(&pps->assert_work, assert_work_func);
+	INIT_WORK(&pps->clear_work, clear_work_func);
+
 	/* Create the char device */
 	err = pps_register_cdev(pps);
 	if (err < 0) {
@@ -152,11 +165,12 @@ EXPORT_SYMBOL(pps_unregister_source);
  * @event: the event type
  * @data: userdef pointer
  *
- * This function is used by each PPS client in order to register a new
- * PPS event into the system (it's usually called inside an IRQ handler).
+ * This function is used by PPS clients in order to register a new
+ * PPS event into the system. It should not be called from an IRQ
+ * handler. Use pps_event_irq instead.
  *
- * If an echo function is associated with the PPS device it will be called
- * as:
+ * If an echo function is associated with the PPS device it will be
+ * called as:
  *	pps->info.echo(pps, event, data);
  */
 void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
@@ -226,3 +240,76 @@ void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
 	spin_unlock_irqrestore(&pps->lock, flags);
 }
 EXPORT_SYMBOL(pps_event);
+
+/* Async event handlers */
+
+static void assert_work_func(struct work_struct *work)
+{
+	struct pps_device *pps = container_of(work,
+			struct pps_device, assert_work);
+
+	pps_event(pps, &pps->assert_work_ts, PPS_CAPTUREASSERT,
+			pps->assert_work_data);

drivers/pps/kapi.c void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event, · line 262

Quoted 10 lines
drivers/pps/kapi.c +10 -0
+}
+
+static void clear_work_func(struct work_struct *work)
+{
+	struct pps_device *pps = container_of(work,
+			struct pps_device, clear_work);
+
+	pps_event(pps, &pps->clear_work_ts, PPS_CAPTURECLEAR,
+			pps->clear_work_data);
+}

The RFC-2783 says that (see 3.1 PPS abstraction):

The API optionally supports an "echo" feature, in which events on the
incoming PPS signal may be reflected through software, after the capture of the corresponding timestamp, to an output signal pin. This feature may be used to discover an upper bound on the actual delay between the edges of the PPS signal and the capture of the timestamps; such information may be useful in precise calibration of
the system.

The designation of an output pin for the echo signal, and sense and shape of the output transition, is outside the scope of this specification, but SHOULD be documented for each implementation. The
output pin MAY also undergo transitions at other times besides those
caused by PPS input events.

By applying this patch the echo function is called inside a work queue so it depends to the scheduler. I suppose this is not acceptable, otherwise we should drop the echo function support.

Quoted 6 lines
drivers/pps/kapi.c +6 -0
+/* pps_event_irq - register a PPS event for deffered handling using
+ * workqueue
+ *
+ * @pps: the PPS device
+ * @ts: the event timestamp
+ * @event: the event type

drivers/pps/kapi.c void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event, · line 279

Quoted 10 lines
drivers/pps/kapi.c +10 -0
+ * @data: userdef pointer
+ *
+ * This function is used by PPS clients in order to register a new
+ * PPS event into the system. It should be called from an IRQ handler
+ * only.
+ *
+ * If an echo function is associated with the PPS device it will be
+ * called as:
+ *	pps->info.echo(pps, event, data);
+ */

The above comment talks about the echo function but you removed it from the code below...

Quoted 125 lines
drivers/pps/kapi.c +36 -0
+void pps_event_irq(struct pps_device *pps, struct pps_event_time *ts,
+		int event, void *data)
+{
+	/* check event type */
+	BUG_ON((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0);
+
+	if (event & PPS_CAPTUREASSERT) {
+		if (work_pending(&pps->assert_work)) {
+			dev_err(pps->dev, "deferred assert edge work haven't"
+					" been handled within a second\n");
+			/* FIXME: do something more intelligent
+			 * then just exit */
+		} else {
+			/* now we can copy data safely */
+			pps->assert_work_ts = *ts;
+			pps->assert_work_data = data;
+
+			queue_work(pps_event_workqueue, &pps->assert_work);
+		}
+	}
+	if (event & PPS_CAPTURECLEAR) {
+		if (work_pending(&pps->clear_work)) {
+			dev_err(pps->dev, "deferred clear edge work haven't"
+					" been handled within a second\n");
+			/* FIXME: do something more intelligent
+			 * then just exit */
+		} else {
+			/* now we can copy data safely */
+			pps->clear_work_ts = *ts;
+			pps->clear_work_data = data;
+
+			queue_work(pps_event_workqueue, &pps->clear_work);
+		}
+	}
+}
+EXPORT_SYMBOL(pps_event_irq);
drivers/pps/pps.c +12 -2
index 79b4455..f642558 100644
@@ -321,18 +321,26 @@ void pps_unregister_cdev(struct pps_device *pps)
 
 static void __exit pps_exit(void)
 {
-	class_destroy(pps_class);
 	unregister_chrdev_region(pps_devt, PPS_MAX_SOURCES);
+	class_destroy(pps_class);
+	destroy_workqueue(pps_event_workqueue);
 }
 
 static int __init pps_init(void)
 {
 	int err;
 
+	pps_event_workqueue = create_workqueue("pps");
+	if (!pps_event_workqueue) {
+		pr_err("failed to create workqueue\n");
+		return -ENOMEM;
+	}
+
 	pps_class = class_create(THIS_MODULE, "pps");
 	if (!pps_class) {
 		pr_err("failed to allocate class\n");
-		return -ENOMEM;
+		err = -ENOMEM;
+		goto destroy_workqueue;
 	}
 	pps_class->dev_attrs = pps_attrs;
 
@@ -350,6 +358,8 @@ static int __init pps_init(void)
 
 remove_class:
 	class_destroy(pps_class);
+destroy_workqueue:
+	destroy_workqueue(pps_event_workqueue);
 
 	return err;
 }
include/linux/pps_kernel.h +12 -0
index 1aedf50..5af0498 100644
@@ -26,6 +26,7 @@
 #include <linux/cdev.h>
 #include <linux/device.h>
 #include <linux/time.h>
+#include <linux/workqueue.h>
 
 /*
  * Global defines
@@ -70,6 +71,13 @@ struct pps_device {
 	struct device *dev;
 	struct fasync_struct *async_queue;	/* fasync method */
 	spinlock_t lock;
+
+	struct work_struct assert_work;
+	struct work_struct clear_work;
+	struct pps_event_time assert_work_ts;
+	struct pps_event_time clear_work_ts;
+	void *assert_work_data;
+	void *clear_work_data;
 };
 
 /*
@@ -78,6 +86,8 @@ struct pps_device {
 
 extern struct device_attribute pps_attrs[];
 
+extern struct workqueue_struct *pps_event_workqueue;
+
 /*
  * Exported functions
  */
@@ -89,6 +99,8 @@ extern int pps_register_cdev(struct pps_device *pps);
 extern void pps_unregister_cdev(struct pps_device *pps);
 extern void pps_event(struct pps_device *pps,
 		struct pps_event_time *ts, int event, void *data);
+extern void pps_event_irq(struct pps_device *pps,
+		struct pps_event_time *ts, int event, void *data);
 
 static inline void timespec_to_pps_ktime(struct pps_ktime *kt,
 		struct timespec ts)
Signature · 1 line
1.7.2.3

Ciao,

Rodolfo

Signature · 5 lines
GNU/Linux Solutions                  e-mail: [email protected]
Linux Device Driver                          [email protected]
Embedded Systems                     phone:  +39 349 2432127
UNIX programming                     skype:  rodolfo.giometti
Freelance ICT Italia - Consulente ICT Italia - www.consulenti-ict.it
#23 lore raw

Re: [PATCHv4 08/17] pps: add async PPS event handler

В Sat, 20 Nov 2010 17:08:51 +0100
Rodolfo Giometti <[email protected]> пишет:

Quoted 6 lines · Alexander Gordeev
Quoted 2 lines

This handler should be called from an IRQ handler. It uses per-device workqueue internally.

Can you please explain to me why do you need this patch? Maybe you can add a verbose patch's description? :)

Well, it's all about optimizing latencies on rt-preempt kernel: if everything is done in a process context than we can use mutexes (haven't done that yet) and don't disable interrupts, which is better for hard real time. I measured that pps_event with kernel consumer enabled takes 1-2us on my test machine. Not a big deal, of course...

Sorry, I've completely forgotten about an echo function. However quick look to current clients shows that it is only used in pps-ktimer.c for debug printing... Maybe it's not needed at all? I mean, have you ever got any user request for this feature? If yes, it can be removed IMHO since RFC-2783 says that it's optional. I can handle the removal in the next version of the patchset.

If you don't want it I can have this patch on my rt branch only and don't try to push it into mainline.

Quoted 152 lines
Quoted 100 lines
---
 drivers/pps/clients/pps-ktimer.c |    2 +-
 drivers/pps/clients/pps-ldisc.c  |    2 +-
 drivers/pps/kapi.c               |   95 ++++++++++++++++++++++++++++++++++++--
 drivers/pps/pps.c                |   14 +++++-
 include/linux/pps_kernel.h       |   12 +++++
 5 files changed, 117 insertions(+), 8 deletions(-)
drivers/pps/clients/pps-ktimer.c +1 -1
index 2728469..26ed7a2 100644
@@ -48,7 +48,7 @@ static void pps_ktimer_event(unsigned long ptr)
 
 	dev_info(pps->dev, "PPS event at %lu\n", jiffies);
 
-	pps_event(pps, &ts, PPS_CAPTUREASSERT, NULL);
+	pps_event_irq(pps, &ts, PPS_CAPTUREASSERT, NULL);
 
 	mod_timer(&ktimer, jiffies + HZ);
 }
drivers/pps/clients/pps-ldisc.c +1 -1
index 30789fa..7006f85 100644
@@ -50,7 +50,7 @@ static void pps_tty_dcd_change(struct tty_struct *tty, unsigned int status,
 	/* Now do the PPS event report */
 	pps = (struct pps_device *)tty->disc_data;
 	if (pps != NULL) {
-		pps_event(pps, ts, status ? PPS_CAPTUREASSERT :
+		pps_event_irq(pps, ts, status ? PPS_CAPTUREASSERT :
 				PPS_CAPTURECLEAR, NULL);
 		spin_unlock_irqrestore(&pps_ldisc_lock, flags);
 		dev_dbg(pps->dev, "PPS %s at %lu\n",
drivers/pps/kapi.c +28 -4
index f5b2b78..ca3b4f8 100644
@@ -32,9 +32,19 @@
 #include <linux/slab.h>
 
 /*
+ * Global variables
+ */
+
+/* PPS event workqueue */
+struct workqueue_struct *pps_event_workqueue;
+
+/*
  * Local functions
  */
 
+static void assert_work_func(struct work_struct *work);
+static void clear_work_func(struct work_struct *work);
+
 static void pps_add_offset(struct pps_ktime *ts, struct pps_ktime *offset)
 {
 	ts->nsec += offset->nsec;
@@ -108,6 +118,9 @@ struct pps_device *pps_register_source(struct pps_source_info *info,
 	init_waitqueue_head(&pps->queue);
 	spin_lock_init(&pps->lock);
 
+	INIT_WORK(&pps->assert_work, assert_work_func);
+	INIT_WORK(&pps->clear_work, clear_work_func);
+
 	/* Create the char device */
 	err = pps_register_cdev(pps);
 	if (err < 0) {
@@ -152,11 +165,12 @@ EXPORT_SYMBOL(pps_unregister_source);
  * @event: the event type
  * @data: userdef pointer
  *
- * This function is used by each PPS client in order to register a new
- * PPS event into the system (it's usually called inside an IRQ handler).
+ * This function is used by PPS clients in order to register a new
+ * PPS event into the system. It should not be called from an IRQ
+ * handler. Use pps_event_irq instead.
  *
- * If an echo function is associated with the PPS device it will be called
- * as:
+ * If an echo function is associated with the PPS device it will be
+ * called as:
  *	pps->info.echo(pps, event, data);
  */
 void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
@@ -226,3 +240,76 @@ void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
 	spin_unlock_irqrestore(&pps->lock, flags);
 }
 EXPORT_SYMBOL(pps_event);
+
+/* Async event handlers */
+
+static void assert_work_func(struct work_struct *work)
+{
+	struct pps_device *pps = container_of(work,
+			struct pps_device, assert_work);
+
+	pps_event(pps, &pps->assert_work_ts, PPS_CAPTUREASSERT,
+			pps->assert_work_data);

drivers/pps/kapi.c void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event, · line 262

Quoted 10 lines
drivers/pps/kapi.c +10 -0
+}
+
+static void clear_work_func(struct work_struct *work)
+{
+	struct pps_device *pps = container_of(work,
+			struct pps_device, clear_work);
+
+	pps_event(pps, &pps->clear_work_ts, PPS_CAPTURECLEAR,
+			pps->clear_work_data);
+}

The RFC-2783 says that (see 3.1 PPS abstraction):

The API optionally supports an "echo" feature, in which events on the
incoming PPS signal may be reflected through software, after the capture of the corresponding timestamp, to an output signal pin. This feature may be used to discover an upper bound on the actual delay between the edges of the PPS signal and the capture of the timestamps; such information may be useful in precise calibration of
the system.

The designation of an output pin for the echo signal, and sense and shape of the output transition, is outside the scope of this specification, but SHOULD be documented for each implementation. The
output pin MAY also undergo transitions at other times besides those
caused by PPS input events.

By applying this patch the echo function is called inside a work queue so it depends to the scheduler. I suppose this is not acceptable, otherwise we should drop the echo function support.

Quoted 6 lines
drivers/pps/kapi.c +6 -0
+/* pps_event_irq - register a PPS event for deffered handling using
+ * workqueue
+ *
+ * @pps: the PPS device
+ * @ts: the event timestamp
+ * @event: the event type

drivers/pps/kapi.c void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event, · line 279

Quoted 10 lines
drivers/pps/kapi.c +10 -0
+ * @data: userdef pointer
+ *
+ * This function is used by PPS clients in order to register a new
+ * PPS event into the system. It should be called from an IRQ handler
+ * only.
+ *
+ * If an echo function is associated with the PPS device it will be
+ * called as:
+ *	pps->info.echo(pps, event, data);
+ */
Quoted 2 lines

The above comment talks about the echo function but you removed it from the code below...

Well, I meant that it will be called from pps_event(). :) Actually just copied this comment from pps_event() and thought that it's still right somehow. Do you want me to remove this comment?

Quoted 130 lines
Quoted 125 lines
drivers/pps/kapi.c +36 -0
+void pps_event_irq(struct pps_device *pps, struct pps_event_time *ts,
+		int event, void *data)
+{
+	/* check event type */
+	BUG_ON((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0);
+
+	if (event & PPS_CAPTUREASSERT) {
+		if (work_pending(&pps->assert_work)) {
+			dev_err(pps->dev, "deferred assert edge work haven't"
+					" been handled within a second\n");
+			/* FIXME: do something more intelligent
+			 * then just exit */
+		} else {
+			/* now we can copy data safely */
+			pps->assert_work_ts = *ts;
+			pps->assert_work_data = data;
+
+			queue_work(pps_event_workqueue, &pps->assert_work);
+		}
+	}
+	if (event & PPS_CAPTURECLEAR) {
+		if (work_pending(&pps->clear_work)) {
+			dev_err(pps->dev, "deferred clear edge work haven't"
+					" been handled within a second\n");
+			/* FIXME: do something more intelligent
+			 * then just exit */
+		} else {
+			/* now we can copy data safely */
+			pps->clear_work_ts = *ts;
+			pps->clear_work_data = data;
+
+			queue_work(pps_event_workqueue, &pps->clear_work);
+		}
+	}
+}
+EXPORT_SYMBOL(pps_event_irq);
drivers/pps/pps.c +12 -2
index 79b4455..f642558 100644
@@ -321,18 +321,26 @@ void pps_unregister_cdev(struct pps_device *pps)
 
 static void __exit pps_exit(void)
 {
-	class_destroy(pps_class);
 	unregister_chrdev_region(pps_devt, PPS_MAX_SOURCES);
+	class_destroy(pps_class);
+	destroy_workqueue(pps_event_workqueue);
 }
 
 static int __init pps_init(void)
 {
 	int err;
 
+	pps_event_workqueue = create_workqueue("pps");
+	if (!pps_event_workqueue) {
+		pr_err("failed to create workqueue\n");
+		return -ENOMEM;
+	}
+
 	pps_class = class_create(THIS_MODULE, "pps");
 	if (!pps_class) {
 		pr_err("failed to allocate class\n");
-		return -ENOMEM;
+		err = -ENOMEM;
+		goto destroy_workqueue;
 	}
 	pps_class->dev_attrs = pps_attrs;
 
@@ -350,6 +358,8 @@ static int __init pps_init(void)
 
 remove_class:
 	class_destroy(pps_class);
+destroy_workqueue:
+	destroy_workqueue(pps_event_workqueue);
 
 	return err;
 }
include/linux/pps_kernel.h +12 -0
index 1aedf50..5af0498 100644
@@ -26,6 +26,7 @@
 #include <linux/cdev.h>
 #include <linux/device.h>
 #include <linux/time.h>
+#include <linux/workqueue.h>
 
 /*
  * Global defines
@@ -70,6 +71,13 @@ struct pps_device {
 	struct device *dev;
 	struct fasync_struct *async_queue;	/* fasync method */
 	spinlock_t lock;
+
+	struct work_struct assert_work;
+	struct work_struct clear_work;
+	struct pps_event_time assert_work_ts;
+	struct pps_event_time clear_work_ts;
+	void *assert_work_data;
+	void *clear_work_data;
 };
 
 /*
@@ -78,6 +86,8 @@ struct pps_device {
 
 extern struct device_attribute pps_attrs[];
 
+extern struct workqueue_struct *pps_event_workqueue;
+
 /*
  * Exported functions
  */
@@ -89,6 +99,8 @@ extern int pps_register_cdev(struct pps_device *pps);
 extern void pps_unregister_cdev(struct pps_device *pps);
 extern void pps_event(struct pps_device *pps,
 		struct pps_event_time *ts, int event, void *data);
+extern void pps_event_irq(struct pps_device *pps,
+		struct pps_event_time *ts, int event, void *data);
 
 static inline void timespec_to_pps_ktime(struct pps_ktime *kt,
 		struct timespec ts)
Signature · 1 line
1.7.2.3

Ciao,

Rodolfo

Signature · 1 line
  Alexander
maintainer Acked-by
#24 lore raw

Re: [PATCHv4 08/17] pps: add async PPS event handler

Quoted 23 lines · Alexander Gordeev

?? Sat, 20 Nov 2010 17:08:51 +0100
Rodolfo Giometti <[email protected]> ??????????:

Quoted 6 lines · Alexander Gordeev
Quoted 2 lines

This handler should be called from an IRQ handler. It uses per-device workqueue internally.

Can you please explain to me why do you need this patch? Maybe you can add a verbose patch's description? :)

Well, it's all about optimizing latencies on rt-preempt kernel: if everything is done in a process context than we can use mutexes (haven't done that yet) and don't disable interrupts, which is better for hard real time. I measured that pps_event with kernel consumer enabled takes 1-2us on my test machine. Not a big deal, of course...

Sorry, I've completely forgotten about an echo function. However quick look to current clients shows that it is only used in pps-ktimer.c for debug printing... Maybe it's not needed at all? I mean, have you ever got any user request for this feature? If yes, it can be removed IMHO since RFC-2783 says that it's optional. I can handle the removal in the next version of the patchset.

Quoted 2 lines

If you don't want it I can have this patch on my rt branch only and don't try to push it into mainline.

The echo function is not used as far as I know, so it's ok for me to remove echo support if you get better performance.

In case we can study how to reimplement it! ;)

Signature · 5 lines
GNU/Linux Solutions                  e-mail: [email protected]
Linux Device Driver                          [email protected]
Embedded Systems                     phone:  +39 349 2432127
UNIX programming                     skype:  rodolfo.giometti
Freelance ICT Italia - Consulente ICT Italia - www.consulenti-ict.it
#25 lore raw

PATCH v4 9/17 pps: don't disable interrupts when using spin locks

In reply to Alexander Gordeev

Now all PPS spin locks are never used in interrupt context so we can safely replace spin_lock_irq*/spin_unlock_irq* with plain spin_lock/spin_unlock.

---
 drivers/pps/kapi.c |    5 ++---
 drivers/pps/pps.c  |   24 ++++++++++++------------
 2 files changed, 14 insertions(+), 15 deletions(-)
drivers/pps/kapi.c +2 -3
index ca3b4f8..fe832aa 100644
@@ -176,7 +176,6 @@ EXPORT_SYMBOL(pps_unregister_source);
 void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
 		void *data)
 {
-	unsigned long flags;
 	int captured = 0;
 	struct pps_ktime ts_real;
 
@@ -190,7 +189,7 @@ void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
 
 	timespec_to_pps_ktime(&ts_real, ts->ts_real);
 
-	spin_lock_irqsave(&pps->lock, flags);
+	spin_lock(&pps->lock);
 
 	/* Must call the echo function? */
 	if ((pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)))
@@ -237,7 +236,7 @@ void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
 		kill_fasync(&pps->async_queue, SIGIO, POLL_IN);
 	}
 
-	spin_unlock_irqrestore(&pps->lock, flags);
+	spin_unlock(&pps->lock);
 }
 EXPORT_SYMBOL(pps_event);
 
drivers/pps/pps.c +12 -12
index f642558..d3267f3 100644
@@ -74,12 +74,12 @@ static long pps_cdev_ioctl(struct file *file,
 	case PPS_GETPARAMS:
 		dev_dbg(pps->dev, "PPS_GETPARAMS\n");
 
-		spin_lock_irq(&pps->lock);
+		spin_lock(&pps->lock);
 
 		/* Get the current parameters */
 		params = pps->params;
 
-		spin_unlock_irq(&pps->lock);
+		spin_unlock(&pps->lock);
 
 		err = copy_to_user(uarg, &params, sizeof(struct pps_kparams));
 		if (err)
@@ -110,7 +110,7 @@ static long pps_cdev_ioctl(struct file *file,
 			return -EINVAL;
 		}
 
-		spin_lock_irq(&pps->lock);
+		spin_lock(&pps->lock);
 
 		/* Save the new parameters */
 		pps->params = params;
@@ -126,7 +126,7 @@ static long pps_cdev_ioctl(struct file *file,
 			pps->params.mode |= PPS_CANWAIT;
 		pps->params.api_version = PPS_API_VERS;
 
-		spin_unlock_irq(&pps->lock);
+		spin_unlock(&pps->lock);
 
 		break;
 
@@ -181,7 +181,7 @@ static long pps_cdev_ioctl(struct file *file,
 		}
 
 		/* Return the fetched timestamp */
-		spin_lock_irq(&pps->lock);
+		spin_lock(&pps->lock);
 
 		fdata.info.assert_sequence = pps->assert_sequence;
 		fdata.info.clear_sequence = pps->clear_sequence;
@@ -189,7 +189,7 @@ static long pps_cdev_ioctl(struct file *file,
 		fdata.info.clear_tu = pps->clear_tu;
 		fdata.info.current_mode = pps->current_mode;
 
-		spin_unlock_irq(&pps->lock);
+		spin_unlock(&pps->lock);
 
 		err = copy_to_user(uarg, &fdata, sizeof(struct pps_fdata));
 		if (err)
@@ -239,9 +239,9 @@ static void pps_device_destruct(struct device *dev)
 
 	/* release id here to protect others from using it while it's
 	 * still in use */
-	spin_lock_irq(&pps_idr_lock);
+	spin_lock(&pps_idr_lock);
 	idr_remove(&pps_idr, pps->id);
-	spin_unlock_irq(&pps_idr_lock);
+	spin_unlock(&pps_idr_lock);
 
 	kfree(dev);
 	kfree(pps);
@@ -260,9 +260,9 @@ int pps_register_cdev(struct pps_device *pps)
 	 * After idr_get_new() calling the new source will be freely available
 	 * into the kernel.
 	 */
-	spin_lock_irq(&pps_idr_lock);
+	spin_lock(&pps_idr_lock);
 	err = idr_get_new(&pps_idr, pps, &pps->id);
-	spin_unlock_irq(&pps_idr_lock);
+	spin_unlock(&pps_idr_lock);
 
 	if (err < 0)
 		return err;
@@ -302,9 +302,9 @@ del_cdev:
 	cdev_del(&pps->cdev);
 
 free_idr:
-	spin_lock_irq(&pps_idr_lock);
+	spin_lock(&pps_idr_lock);
 	idr_remove(&pps_idr, pps->id);
-	spin_unlock_irq(&pps_idr_lock);
+	spin_unlock(&pps_idr_lock);
 
 	return err;
 }
Signature · 1 line
1.7.2.3
maintainer
#26 lore raw

Re: [PATCHv4 09/17] pps: don't disable interrupts when using spin locks

Quoted 3 lines · Alexander Gordeev

Now all PPS spin locks are never used in interrupt context so we can safely replace spin_lock_irq*/spin_unlock_irq* with plain spin_lock/spin_unlock.

This patch must be delayed till the previous one is not fixed.

Ciao,

Rodolfo

Signature · 5 lines
GNU/Linux Solutions                  e-mail: [email protected]
Linux Device Driver                          [email protected]
Embedded Systems                     phone:  +39 349 2432127
UNIX programming                     skype:  rodolfo.giometti
Freelance ICT Italia - Consulente ICT Italia - www.consulenti-ict.it
#27 lore raw

PATCH v4 10/17 pps: use BUG_ON for kernel API safety checks

In reply to Alexander Gordeev

This way less overhead is involved when running production kernel. If you want to debug a pps client module please define DEBUG to enable the checks.

---
 drivers/pps/kapi.c |   33 ++++++++++-----------------------
 1 files changed, 10 insertions(+), 23 deletions(-)
drivers/pps/kapi.c +10 -23
index fe832aa..54261c4 100644
@@ -81,25 +81,14 @@ struct pps_device *pps_register_source(struct pps_source_info *info,
 	int err;
 
 	/* Sanity checks */
-	if ((info->mode & default_params) != default_params) {
-		pr_err("pps: %s: unsupported default parameters\n",
-					info->name);
-		err = -EINVAL;
-		goto pps_register_source_exit;
-	}
-	if ((info->mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) != 0 &&
-			info->echo == NULL) {
-		pr_err("pps: %s: echo function is not defined\n",
-					info->name);
-		err = -EINVAL;
-		goto pps_register_source_exit;
-	}
-	if ((info->mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
-		pr_err("pps: %s: unspecified time format\n",
-					info->name);
-		err = -EINVAL;
-		goto pps_register_source_exit;
-	}
+
+	/* default_params should be supported */
+	BUG_ON((info->mode & default_params) != default_params);
+	/* echo function should be defined if we are asked to call it */
+	BUG_ON((info->mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) != 0 &&
+			info->echo == NULL);
+	/* time format should be specified */
+	BUG_ON((info->mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0);
 
 	/* Allocate memory for the new PPS source struct */
 	pps = kzalloc(sizeof(struct pps_device), GFP_KERNEL);
@@ -179,10 +168,8 @@ void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
 	int captured = 0;
 	struct pps_ktime ts_real;
 
-	if ((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0) {
-		dev_err(pps->dev, "unknown event (%x)\n", event);
-		return;
-	}
+	/* check event type */
+	BUG_ON((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0);
 
 	dev_dbg(pps->dev, "PPS event at %ld.%09ld\n",
 			ts->ts_real.tv_sec, ts->ts_real.tv_nsec);
Signature · 1 line
1.7.2.3
maintainer NAK
#28 lore raw

Re: [PATCHv4 10/17] pps: use BUG_ON for kernel API safety checks

Quoted 34 lines · Alexander Gordeev

This way less overhead is involved when running production kernel. If you want to debug a pps client module please define DEBUG to enable the checks.

---
 drivers/pps/kapi.c |   33 ++++++++++-----------------------
 1 files changed, 10 insertions(+), 23 deletions(-)
drivers/pps/kapi.c +0 -17
index fe832aa..54261c4 100644
@@ -81,25 +81,14 @@ struct pps_device *pps_register_source(struct pps_source_info *info,
 	int err;
 
 	/* Sanity checks */
-	if ((info->mode & default_params) != default_params) {
-		pr_err("pps: %s: unsupported default parameters\n",
-					info->name);
-		err = -EINVAL;
-		goto pps_register_source_exit;
-	}
-	if ((info->mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) != 0 &&
-			info->echo == NULL) {
-		pr_err("pps: %s: echo function is not defined\n",
-					info->name);
-		err = -EINVAL;
-		goto pps_register_source_exit;
-	}
-	if ((info->mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
-		pr_err("pps: %s: unspecified time format\n",
-					info->name);
-		err = -EINVAL;

drivers/pps/kapi.c struct pps_device *pps_register_source(struct pps_source_info *info, · line 91

Quoted 10 lines
drivers/pps/kapi.c +8 -2
-		goto pps_register_source_exit;
-	}
+
+	/* default_params should be supported */
+	BUG_ON((info->mode & default_params) != default_params);
+	/* echo function should be defined if we are asked to call it */
+	BUG_ON((info->mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) != 0 &&
+			info->echo == NULL);
+	/* time format should be specified */
+	BUG_ON((info->mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0);

Nack.

If the userland gives us some wrong parameters this is not the same of a kernel bug (which BUG_ON is used for). The userland must be notified about the wrong input.

drivers/pps/kapi.c void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event, · line 172

Quoted 12 lines
drivers/pps/kapi.c +2 -4
 	/* Allocate memory for the new PPS source struct */
 	pps = kzalloc(sizeof(struct pps_device), GFP_KERNEL);
@@ -179,10 +168,8 @@ void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
 	int captured = 0;
 	struct pps_ktime ts_real;
 
-	if ((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0) {
-		dev_err(pps->dev, "unknown event (%x)\n", event);
-		return;
-	}
+	/* check event type */
+	BUG_ON((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0);

Ack.

This is a correct usage of BUG_ON. :)

Quoted 5 lines
drivers/pps/kapi.c +0 -0
 	dev_dbg(pps->dev, "PPS event at %ld.%09ld\n",
 			ts->ts_real.tv_sec, ts->ts_real.tv_nsec);
Signature · 1 line
1.7.2.3

Ciao,

Rodolfo

Signature · 5 lines
GNU/Linux Solutions                  e-mail: [email protected]
Linux Device Driver                          [email protected]
Embedded Systems                     phone:  +39 349 2432127
UNIX programming                     skype:  rodolfo.giometti
Freelance ICT Italia - Consulente ICT Italia - www.consulenti-ict.it
#29 lore raw

Re: [PATCHv4 10/17] pps: use BUG_ON for kernel API safety checks

Quoted 5 lines · Rodolfo Giometti
Quoted 4 lines · Alexander Gordeev

This way less overhead is involved when running production kernel. If you want to debug a pps client module please define DEBUG to enable the checks.

[]

Quoted 14 lines

drivers/pps/kapi.c void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event, · line 172

Quoted 11 lines
drivers/pps/kapi.c +2 -4
@@ -179,10 +168,8 @@ void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
 	int captured = 0;
 	struct pps_ktime ts_real;
 
-	if ((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0) {
-		dev_err(pps->dev, "unknown event (%x)\n", event);
-		return;
-	}
+	/* check event type */
+	BUG_ON((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0);

Ack.

Quoted 1 line

This is a correct usage of BUG_ON. :)

I don't think that's true.

/*
* Don't use BUG() or BUG_ON() unless there's really no way out; one
* example might be detecting data structure corruption in the middle
* of an operation that can't be backed out of. If the (sub)system
* can somehow continue operating, perhaps with reduced functionality,
* it's probably not BUG-worthy.
*
* If you're tempted to BUG(), think again: is completely giving up
* really the *only* solution? There are usually better options, where
* users don't need to reboot ASAP and can mostly shut down cleanly. */

maintainer
#30 lore raw

Re: [PATCHv4 10/17] pps: use BUG_ON for kernel API safety checks

Quoted 13 lines · Joe Perches
Quoted 10 lines

drivers/pps/kapi.c void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event, · line 172

Quoted 6 lines
drivers/pps/kapi.c +2 -4
-	if ((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0) {
-		dev_err(pps->dev, "unknown event (%x)\n", event);
-		return;
-	}
+	/* check event type */
+	BUG_ON((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0);

Ack.

This is a correct usage of BUG_ON. :)

I don't think that's true.

Quoted 11 lines

/*
* Don't use BUG() or BUG_ON() unless there's really no way out; one
* example might be detecting data structure corruption in the middle
* of an operation that can't be backed out of. If the (sub)system
* can somehow continue operating, perhaps with reduced functionality,
* it's probably not BUG-worthy.
*
* If you're tempted to BUG(), think again: is completely giving up
* really the *only* solution? There are usually better options, where
* users don't need to reboot ASAP and can mostly shut down cleanly. */

If (event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0 then PPS's data has been corrupted. The PPS subsystem doesn't work correctly even if the whole system still continues running.

However I already have not acknowledged the patch.

Ciao,

Rodolfo

Signature · 5 lines
GNU/Linux Solutions                  e-mail: [email protected]
Linux Device Driver                          [email protected]
Embedded Systems                     phone:  +39 349 2432127
UNIX programming                     skype:  rodolfo.giometti
Freelance ICT Italia - Consulente ICT Italia - www.consulenti-ict.it
#31 lore raw

Re: [PATCHv4 10/17] pps: use BUG_ON for kernel API safety checks

In reply to Joe Perches

В Sat, 20 Nov 2010 09:01:41 -0800
Joe Perches <[email protected]> пишет:

Quoted 25 lines · Rodolfo Giometti
Quoted 5 lines · Alexander Gordeev
Quoted 4 lines

This way less overhead is involved when running production kernel. If you want to debug a pps client module please define DEBUG to enable the checks.

[]

Quoted 14 lines

drivers/pps/kapi.c void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event, · line 172

Quoted 11 lines
drivers/pps/kapi.c +2 -4
@@ -179,10 +168,8 @@ void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
 	int captured = 0;
 	struct pps_ktime ts_real;
 
-	if ((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0) {
-		dev_err(pps->dev, "unknown event (%x)\n", event);
-		return;
-	}
+	/* check event type */
+	BUG_ON((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0);

Ack.

Quoted 1 line

This is a correct usage of BUG_ON. :)

I don't think that's true.

Quoted 11 lines

/*
* Don't use BUG() or BUG_ON() unless there's really no way out; one
* example might be detecting data structure corruption in the middle
* of an operation that can't be backed out of. If the (sub)system
* can somehow continue operating, perhaps with reduced functionality,
* it's probably not BUG-worthy.
*
* If you're tempted to BUG(), think again: is completely giving up
* really the *only* solution? There are usually better options, where
* users don't need to reboot ASAP and can mostly shut down cleanly. */

Hmm, didn't check that before. What is more appropriate in this situation?

Signature · 1 line
  Alexander
#32 lore raw

Re: [PATCHv4 10/17] pps: use BUG_ON for kernel API safety checks

Quoted 33 lines · Alexander Gordeev

В Sat, 20 Nov 2010 09:01:41 -0800
Joe Perches <[email protected]> пишет:

Quoted 19 lines · Rodolfo Giometti
Quoted 5 lines · Alexander Gordeev
Quoted 4 lines

This way less overhead is involved when running production kernel. If you want to debug a pps client module please define DEBUG to enable the checks.

[]

Quoted 4 lines
Quoted 4 lines
drivers/pps/kapi.c +0 -0
@@ -179,10 +168,8 @@ void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
 	int captured = 0;
 	struct pps_ktime ts_real;
Quoted 8 lines

drivers/pps/kapi.c void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event, · line 172

Quoted 6 lines
drivers/pps/kapi.c +2 -4
-	if ((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0) {
-		dev_err(pps->dev, "unknown event (%x)\n", event);
-		return;
-	}
+	/* check event type */
+	BUG_ON((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0);

Ack.
This is a correct usage of BUG_ON. :)

Quoted 12 lines

I don't think that's true.
/*
* Don't use BUG() or BUG_ON() unless there's really no way out; one
* example might be detecting data structure corruption in the middle
* of an operation that can't be backed out of. If the (sub)system
* can somehow continue operating, perhaps with reduced functionality,
* it's probably not BUG-worthy.
*
* If you're tempted to BUG(), think again: is completely giving up
* really the *only* solution? There are usually better options, where
* users don't need to reboot ASAP and can mostly shut down cleanly. */

Quoted 2 lines

Hmm, didn't check that before. What is more appropriate in this situation?

If it's really a non-recoverable event, perhaps emit a dev_crit and then do a module_put?

maintainer
#33 lore raw

Re: [PATCHv4 10/17] pps: use BUG_ON for kernel API safety checks

In reply to Alexander Gordeev
Quoted 40 lines · Alexander Gordeev

?? Sat, 20 Nov 2010 09:01:41 -0800
Joe Perches <[email protected]> ??????????:

Quoted 25 lines · Rodolfo Giometti
Quoted 5 lines · Alexander Gordeev
Quoted 4 lines

This way less overhead is involved when running production kernel. If you want to debug a pps client module please define DEBUG to enable the checks.

[]

Quoted 14 lines

drivers/pps/kapi.c void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event, · line 172

Quoted 11 lines
drivers/pps/kapi.c +2 -4
@@ -179,10 +168,8 @@ void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
 	int captured = 0;
 	struct pps_ktime ts_real;
 
-	if ((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0) {
-		dev_err(pps->dev, "unknown event (%x)\n", event);
-		return;
-	}
+	/* check event type */
+	BUG_ON((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0);

Ack.

Quoted 1 line

This is a correct usage of BUG_ON. :)

I don't think that's true.

Quoted 11 lines

/*
* Don't use BUG() or BUG_ON() unless there's really no way out; one
* example might be detecting data structure corruption in the middle
* of an operation that can't be backed out of. If the (sub)system
* can somehow continue operating, perhaps with reduced functionality,
* it's probably not BUG-worthy.
*
* If you're tempted to BUG(), think again: is completely giving up
* really the *only* solution? There are usually better options, where
* users don't need to reboot ASAP and can mostly shut down cleanly. */

Quoted 2 lines

Hmm, didn't check that before. What is more appropriate in this situation?

IMHO here you can use BUG_ON() but not into pps_register_source().

Ciao,

Rodolfo

Signature · 5 lines
GNU/Linux Solutions                  e-mail: [email protected]
Linux Device Driver                          [email protected]
Embedded Systems                     phone:  +39 349 2432127
UNIX programming                     skype:  rodolfo.giometti
Freelance ICT Italia - Consulente ICT Italia - www.consulenti-ict.it
#34 lore raw

Re: [PATCHv4 10/17] pps: use BUG_ON for kernel API safety checks

In reply to Rodolfo Giometti

В Sat, 20 Nov 2010 17:13:51 +0100
Rodolfo Giometti <[email protected]> пишет:

Quoted 48 lines · Alexander Gordeev
Quoted 34 lines

This way less overhead is involved when running production kernel. If you want to debug a pps client module please define DEBUG to enable the checks.

---
 drivers/pps/kapi.c |   33 ++++++++++-----------------------
 1 files changed, 10 insertions(+), 23 deletions(-)
drivers/pps/kapi.c +0 -17
index fe832aa..54261c4 100644
@@ -81,25 +81,14 @@ struct pps_device *pps_register_source(struct pps_source_info *info,
 	int err;
 
 	/* Sanity checks */
-	if ((info->mode & default_params) != default_params) {
-		pr_err("pps: %s: unsupported default parameters\n",
-					info->name);
-		err = -EINVAL;
-		goto pps_register_source_exit;
-	}
-	if ((info->mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) != 0 &&
-			info->echo == NULL) {
-		pr_err("pps: %s: echo function is not defined\n",
-					info->name);
-		err = -EINVAL;
-		goto pps_register_source_exit;
-	}
-	if ((info->mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
-		pr_err("pps: %s: unspecified time format\n",
-					info->name);
-		err = -EINVAL;

drivers/pps/kapi.c struct pps_device *pps_register_source(struct pps_source_info *info, · line 91

Quoted 10 lines
drivers/pps/kapi.c +8 -2
-		goto pps_register_source_exit;
-	}
+
+	/* default_params should be supported */
+	BUG_ON((info->mode & default_params) != default_params);
+	/* echo function should be defined if we are asked to call it */
+	BUG_ON((info->mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) != 0 &&
+			info->echo == NULL);
+	/* time format should be specified */
+	BUG_ON((info->mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0);

Nack.

Quoted 3 lines

If the userland gives us some wrong parameters this is not the same of a kernel bug (which BUG_ON is used for). The userland must be notified about the wrong input.

I agree with what you said completely but this is not a user-space API. pps_register_source() can only be called from other kernel code.

Quoted 27 lines

drivers/pps/kapi.c void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event, · line 172

Quoted 12 lines
drivers/pps/kapi.c +2 -4
 	/* Allocate memory for the new PPS source struct */
 	pps = kzalloc(sizeof(struct pps_device), GFP_KERNEL);
@@ -179,10 +168,8 @@ void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
 	int captured = 0;
 	struct pps_ktime ts_real;
 
-	if ((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0) {
-		dev_err(pps->dev, "unknown event (%x)\n", event);
-		return;
-	}
+	/* check event type */
+	BUG_ON((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0);

Ack.

This is a correct usage of BUG_ON. :)

Quoted 5 lines
drivers/pps/kapi.c +0 -0
 	dev_dbg(pps->dev, "PPS event at %ld.%09ld\n",
 			ts->ts_real.tv_sec, ts->ts_real.tv_nsec);
Signature · 1 line
1.7.2.3

Ciao,

Rodolfo

Signature · 1 line
  Alexander
maintainer
#35 lore raw

Re: [PATCHv4 10/17] pps: use BUG_ON for kernel API safety checks

Quoted 51 lines · Alexander Gordeev

?? Sat, 20 Nov 2010 17:13:51 +0100
Rodolfo Giometti <[email protected]> ??????????:

Quoted 48 lines · Alexander Gordeev
Quoted 34 lines

This way less overhead is involved when running production kernel. If you want to debug a pps client module please define DEBUG to enable the checks.

---
 drivers/pps/kapi.c |   33 ++++++++++-----------------------
 1 files changed, 10 insertions(+), 23 deletions(-)
drivers/pps/kapi.c +0 -17
index fe832aa..54261c4 100644
@@ -81,25 +81,14 @@ struct pps_device *pps_register_source(struct pps_source_info *info,
 	int err;
 
 	/* Sanity checks */
-	if ((info->mode & default_params) != default_params) {
-		pr_err("pps: %s: unsupported default parameters\n",
-					info->name);
-		err = -EINVAL;
-		goto pps_register_source_exit;
-	}
-	if ((info->mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) != 0 &&
-			info->echo == NULL) {
-		pr_err("pps: %s: echo function is not defined\n",
-					info->name);
-		err = -EINVAL;
-		goto pps_register_source_exit;
-	}
-	if ((info->mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
-		pr_err("pps: %s: unspecified time format\n",
-					info->name);
-		err = -EINVAL;

drivers/pps/kapi.c struct pps_device *pps_register_source(struct pps_source_info *info, · line 91

Quoted 10 lines
drivers/pps/kapi.c +8 -2
-		goto pps_register_source_exit;
-	}
+
+	/* default_params should be supported */
+	BUG_ON((info->mode & default_params) != default_params);
+	/* echo function should be defined if we are asked to call it */
+	BUG_ON((info->mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) != 0 &&
+			info->echo == NULL);
+	/* time format should be specified */
+	BUG_ON((info->mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0);

Nack.

Quoted 6 lines
Quoted 3 lines

If the userland gives us some wrong parameters this is not the same of a kernel bug (which BUG_ON is used for). The userland must be notified about the wrong input.

I agree with what you said completely but this is not a user-space API. pps_register_source() can only be called from other kernel code.

Yes, but in turn pps_register_source() is called in a function called from userspace. The user should know if he/she cannot safely register his/her new PPS source.

The BUG_ON() should be used for a kernel bug like "Ehi! This can't happen! Data are corrupted"... but here we are just checking some input parameters.

Ciao,

Rodolfo

Signature · 5 lines
GNU/Linux Solutions                  e-mail: [email protected]
Linux Device Driver                          [email protected]
Embedded Systems                     phone:  +39 349 2432127
UNIX programming                     skype:  rodolfo.giometti
Freelance ICT Italia - Consulente ICT Italia - www.consulenti-ict.it
#36 lore raw

PATCH v4 11/17 pps: simplify conditions a bit

In reply to Alexander Gordeev

Bitwise conjunction is distributive so we can simplify some conditions.

---
 drivers/pps/kapi.c |    6 ++----
 1 files changed, 2 insertions(+), 4 deletions(-)
drivers/pps/kapi.c +2 -4
index 54261c4..2bdfbed 100644
@@ -184,8 +184,7 @@ void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
 
 	/* Check the event */
 	pps->current_mode = pps->params.mode;
-	if ((event & PPS_CAPTUREASSERT) &
-			(pps->params.mode & PPS_CAPTUREASSERT)) {
+	if (event & pps->params.mode & PPS_CAPTUREASSERT) {
 		/* We have to add an offset? */
 		if (pps->params.mode & PPS_OFFSETASSERT)
 			pps_add_offset(&ts_real,
@@ -199,8 +198,7 @@ void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
 
 		captured = ~0;
 	}
-	if ((event & PPS_CAPTURECLEAR) &
-			(pps->params.mode & PPS_CAPTURECLEAR)) {
+	if (event & pps->params.mode & PPS_CAPTURECLEAR) {
 		/* We have to add an offset? */
 		if (pps->params.mode & PPS_OFFSETCLEAR)
 			pps_add_offset(&ts_real,
Signature · 1 line
1.7.2.3
maintainer Acked-by on v4 11/17
#37 lore raw

Re: [PATCHv4 11/17] pps: simplify conditions a bit

Quoted 34 lines · Alexander Gordeev

Bitwise conjunction is distributive so we can simplify some conditions.

---
 drivers/pps/kapi.c |    6 ++----
 1 files changed, 2 insertions(+), 4 deletions(-)
drivers/pps/kapi.c +2 -4
index 54261c4..2bdfbed 100644
@@ -184,8 +184,7 @@ void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
 
 	/* Check the event */
 	pps->current_mode = pps->params.mode;
-	if ((event & PPS_CAPTUREASSERT) &
-			(pps->params.mode & PPS_CAPTUREASSERT)) {
+	if (event & pps->params.mode & PPS_CAPTUREASSERT) {
 		/* We have to add an offset? */
 		if (pps->params.mode & PPS_OFFSETASSERT)
 			pps_add_offset(&ts_real,
@@ -199,8 +198,7 @@ void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
 
 		captured = ~0;
 	}
-	if ((event & PPS_CAPTURECLEAR) &
-			(pps->params.mode & PPS_CAPTURECLEAR)) {
+	if (event & pps->params.mode & PPS_CAPTURECLEAR) {
 		/* We have to add an offset? */
 		if (pps->params.mode & PPS_OFFSETCLEAR)
 			pps_add_offset(&ts_real,
Signature · 1 line
1.7.2.3
Signature · 5 lines
GNU/Linux Solutions                  e-mail: [email protected]
Linux Device Driver                          [email protected]
Embedded Systems                     phone:  +39 349 2432127
UNIX programming                     skype:  rodolfo.giometti
Freelance ICT Italia - Consulente ICT Italia - www.consulenti-ict.it
#38 lore raw

PATCH v4 12/17 pps: timestamp is always passed to dcd_change()

In reply to Alexander Gordeev

Remove the code that gatheres timestamp in pps_tty_dcd_change() in case passed ts parameter is NULL because it never happens in the current code. Fix comments as well.

---
 Documentation/serial/tty.txt    |    2 +-
 drivers/pps/clients/pps-ldisc.c |    8 --------
 include/linux/tty_ldisc.h       |    2 +-
 3 files changed, 2 insertions(+), 10 deletions(-)
Documentation/serial/tty.txt +1 -1
index 7c90050..540db41 100644
@@ -107,7 +107,7 @@ write_wakeup()	-	May be called at any point between open and close.
 
 dcd_change()	-	Report to the tty line the current DCD pin status
 			changes and the relative timestamp. The timestamp
-			can be NULL.
+			cannot be NULL.
 
 
 Driver Access
drivers/pps/clients/pps-ldisc.c +0 -8
index 7006f85..57130a5 100644
@@ -35,16 +35,8 @@ static void pps_tty_dcd_change(struct tty_struct *tty, unsigned int status,
 				struct pps_event_time *ts)
 {
 	struct pps_device *pps;
-	struct pps_event_time __ts;
 	unsigned long flags;
 
-	/* First of all we get the time stamp... */
-	pps_get_ts(&__ts);
-
-	/* Does caller give us a timestamp? */
-	if (!ts)	/* No. Do it ourself! */
-		ts = &__ts;
-
 	spin_lock_irqsave(&pps_ldisc_lock, flags);
 
 	/* Now do the PPS event report */
include/linux/tty_ldisc.h +1 -1
index 763e061..ff7dc08 100644
@@ -104,7 +104,7 @@
  *			struct pps_event_time *ts)
  *
  *	Tells the discipline that the DCD pin has changed its status and
- *	the relative timestamp. Pointer ts can be NULL.
+ *	the relative timestamp. Pointer ts cannot be NULL.
  */
 
 #include <linux/fs.h>
Signature · 1 line
1.7.2.3
maintainer
#39 lore raw

Re: [PATCHv4 12/17] pps: timestamp is always passed to dcd_change()

Quoted 16 lines · Alexander Gordeev

Remove the code that gatheres timestamp in pps_tty_dcd_change() in case passed ts parameter is NULL because it never happens in the current code. Fix comments as well.

---
 Documentation/serial/tty.txt    |    2 +-
 drivers/pps/clients/pps-ldisc.c |    8 --------
 include/linux/tty_ldisc.h       |    2 +-
 3 files changed, 2 insertions(+), 10 deletions(-)
Documentation/serial/tty.txt +0 -0
index 7c90050..540db41 100644

Documentation/serial/tty.txt write_wakeup() - May be called at any point between open and close. · line 110

Quoted 5 lines
Documentation/serial/tty.txt +1 -1
@@ -107,7 +107,7 @@ write_wakeup()	-	May be called at any point between open and close.
 
 dcd_change()	-	Report to the tty line the current DCD pin status
 			changes and the relative timestamp. The timestamp
-			can be NULL.
+			cannot be NULL.

Can you please explain why it cannot be null? O_o

Who does supply the timestamp to dcd_change?

Ciao,

Rodolfo

Signature · 5 lines
GNU/Linux Solutions                  e-mail: [email protected]
Linux Device Driver                          [email protected]
Embedded Systems                     phone:  +39 349 2432127
UNIX programming                     skype:  rodolfo.giometti
Freelance ICT Italia - Consulente ICT Italia - www.consulenti-ict.it
#40 lore raw

Re: [PATCHv4 12/17] pps: timestamp is always passed to dcd_change()

В Sat, 20 Nov 2010 17:23:04 +0100
Rodolfo Giometti <[email protected]> пишет:

Quoted 18 lines · Alexander Gordeev
Quoted 17 lines

Remove the code that gatheres timestamp in pps_tty_dcd_change() in case passed ts parameter is NULL because it never happens in the current code. Fix comments as well.

---
 Documentation/serial/tty.txt    |    2 +-
 drivers/pps/clients/pps-ldisc.c |    8 --------
 include/linux/tty_ldisc.h       |    2 +-
 3 files changed, 2 insertions(+), 10 deletions(-)
Documentation/serial/tty.txt +0 -0
index 7c90050..540db41 100644
@@ -107,7 +107,7 @@ write_wakeup()	-	May be called at any point between open and close.
 
Quoted 8 lines

Documentation/serial/tty.txt write_wakeup() - May be called at any point between open and close. · line 110

Quoted 4 lines
Documentation/serial/tty.txt +1 -1
 dcd_change()	-	Report to the tty line the current DCD pin status
 			changes and the relative timestamp. The timestamp
-			can be NULL.
+			cannot be NULL.

Can you please explain why it cannot be null? O_o

Who does supply the timestamp to dcd_change?

dcd_change() is called only from one place - uart_handle_dcd_change() - which always obtains and passes timestamps to the former. So no need to check if ts is NULL and obtain ts again.

Signature · 1 line
  Alexander
maintainer Acked-by
#41 lore raw

Re: [PATCHv4 12/17] pps: timestamp is always passed to dcd_change()

Quoted 28 lines · Alexander Gordeev

?? Sat, 20 Nov 2010 17:23:04 +0100
Rodolfo Giometti <[email protected]> ??????????:

Quoted 25 lines · Alexander Gordeev
Quoted 16 lines

Remove the code that gatheres timestamp in pps_tty_dcd_change() in case passed ts parameter is NULL because it never happens in the current code. Fix comments as well.

---
 Documentation/serial/tty.txt    |    2 +-
 drivers/pps/clients/pps-ldisc.c |    8 --------
 include/linux/tty_ldisc.h       |    2 +-
 3 files changed, 2 insertions(+), 10 deletions(-)
Documentation/serial/tty.txt +0 -0
index 7c90050..540db41 100644

Documentation/serial/tty.txt write_wakeup() - May be called at any point between open and close. · line 110

Quoted 5 lines
Documentation/serial/tty.txt +1 -1
@@ -107,7 +107,7 @@ write_wakeup()	-	May be called at any point between open and close.
 
 dcd_change()	-	Report to the tty line the current DCD pin status
 			changes and the relative timestamp. The timestamp
-			can be NULL.
+			cannot be NULL.

Can you please explain why it cannot be null? O_o

Quoted 5 lines
Quoted 1 line

Who does supply the timestamp to dcd_change?

dcd_change() is called only from one place - uart_handle_dcd_change() - which always obtains and passes timestamps to the former. So no need to check if ts is NULL and obtain ts again.

Signature · 5 lines
GNU/Linux Solutions                  e-mail: [email protected]
Linux Device Driver                          [email protected]
Embedded Systems                     phone:  +39 349 2432127
UNIX programming                     skype:  rodolfo.giometti
Freelance ICT Italia - Consulente ICT Italia - www.consulenti-ict.it
#42 lore raw

PATCH v4 13/17 ntp: add hardpps implementation

In reply to Alexander Gordeev

This commit adds hardpps() implementation based upon the original one from the NTPv4 reference kernel code from David Mills. However, it is highly optimized towards very fast syncronization and maximum stickness to PPS signal. The typical error is less then a microsecond. To make it sync faster I had to throw away exponential phase filter so that the full phase offset is corrected immediately. Then I also had to throw away median phase filter because it gives a bigger error itself if used without exponential filter.
Maybe we will find an appropriate filtering scheme in the future but it's not necessary if the signal quality is ok.

---
 drivers/pps/Kconfig   |    7 +
 include/linux/timex.h |    1 +
 kernel/time/ntp.c     |  425 +++++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 418 insertions(+), 15 deletions(-)
drivers/pps/Kconfig +7 -0
index 1afe4e0..33c9126 100644
@@ -30,6 +30,13 @@ config PPS_DEBUG
 	  messages to the system log.  Select this if you are having a
 	  problem with PPS support and want to see more of what is going on.
 
+config NTP_PPS
+	bool "PPS kernel consumer support"
+	depends on PPS
+	help
+	  This option adds support for direct in-kernel time
+	  syncronization using an external PPS signal.
+
 source drivers/pps/clients/Kconfig
 
 endmenu
include/linux/timex.h +1 -0
index 32d852f..d23999f 100644
@@ -268,6 +268,7 @@ extern u64 tick_length;
 extern void second_overflow(void);
 extern void update_ntp_one_tick(void);
 extern int do_adjtimex(struct timex *);
+extern void hardpps(const struct timespec *, const struct timespec *);
 
 int read_current_timer(unsigned long *timer_val);
 
kernel/time/ntp.c +410 -15

484 lines not on this page · raw message

Signature · 1 line
1.7.2.3
maintainer
#43 lore raw

Re: [PATCHv4 13/17] ntp: add hardpps implementation

Quoted 10 lines · Alexander Gordeev

This commit adds hardpps() implementation based upon the original one from the NTPv4 reference kernel code from David Mills. However, it is highly optimized towards very fast syncronization and maximum stickness to PPS signal. The typical error is less then a microsecond. To make it sync faster I had to throw away exponential phase filter so that the full phase offset is corrected immediately. Then I also had to throw away median phase filter because it gives a bigger error itself if used without exponential filter.
Maybe we will find an appropriate filtering scheme in the future but it's not necessary if the signal quality is ok.

This patch (and follows) adds a new functionality to PPS subsystem, so let me suggest to you splitting in two parts this patch set: a first patch set who fixes up the current PPS implementation, and a second patch set who adds kernel consumer (and follows).

Ciao,

Rodolfo

Signature · 5 lines
GNU/Linux Solutions                  e-mail: [email protected]
Linux Device Driver                          [email protected]
Embedded Systems                     phone:  +39 349 2432127
UNIX programming                     skype:  rodolfo.giometti
Freelance ICT Italia - Consulente ICT Italia - www.consulenti-ict.it
#44 lore raw

Re: [PATCHv4 13/17] ntp: add hardpps implementation

В Sat, 20 Nov 2010 17:27:18 +0100
Rodolfo Giometti <[email protected]> пишет:

Quoted 12 lines · Alexander Gordeev
Quoted 10 lines

This commit adds hardpps() implementation based upon the original one from the NTPv4 reference kernel code from David Mills. However, it is highly optimized towards very fast syncronization and maximum stickness to PPS signal. The typical error is less then a microsecond. To make it sync faster I had to throw away exponential phase filter so that the full phase offset is corrected immediately. Then I also had to throw away median phase filter because it gives a bigger error itself if used without exponential filter.
Maybe we will find an appropriate filtering scheme in the future but it's not necessary if the signal quality is ok.

Quoted 4 lines

This patch (and follows) adds a new functionality to PPS subsystem, so let me suggest to you splitting in two parts this patch set: a first patch set who fixes up the current PPS implementation, and a second patch set who adds kernel consumer (and follows).

But the patches that add hardpps and parport client+generator depend on the previous ones because the latter change lots of things in PPS subsystem. I don't want to maintain them separately because we use all of them in the production.

However I can tell here that it'll be ok for me if not all the patches enter mainline at the same time. For example, patches 1-3 are already ACKed and are actually bugfixes (patch 2 is trivial and patch 3 depends on it) so I think they could be merged in the next rc. I placed them in the beginning exactly for this reason. Whom should I mail them?

Same thing with other patches. Smaller patchset -> me (and probably users) more happy. :)

Signature · 1 line
  Alexander
#45 lore raw

PATCH v4 14/17 pps: capture MONOTONIC_RAW timestamps as well

In reply to Alexander Gordeev

MONOTONIC_RAW clock timestamps are ideally suited for frequency calculation and also fit well into the original NTP hardpps design. Now phase and frequency can be adjusted separately: the former based on REALTIME clock and the latter based on MONOTONIC_RAW clock. A new function getnstime_raw_and_real is added to timekeeping subsystem to capture both timestamps at the same time and atomically.

---
 include/linux/pps_kernel.h |    3 ++-
 include/linux/time.h       |    2 ++
 kernel/time/timekeeping.c  |   38 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 42 insertions(+), 1 deletions(-)
include/linux/pps_kernel.h +2 -1
index 5af0498..39fc7125 100644
@@ -48,6 +48,7 @@ struct pps_source_info {
 };
 
 struct pps_event_time {
+	struct timespec ts_raw;
 	struct timespec ts_real;
 };
 
@@ -111,7 +112,7 @@ static inline void timespec_to_pps_ktime(struct pps_ktime *kt,
 
 static inline void pps_get_ts(struct pps_event_time *ts)
 {
-	getnstimeofday(&ts->ts_real);
+	getnstime_raw_and_real(&ts->ts_raw, &ts->ts_real);
 }
 
 #endif /* LINUX_PPS_KERNEL_H */
include/linux/time.h +2 -0
index 9f15ac7..1e6d3b5 100644
@@ -158,6 +158,8 @@ extern unsigned int alarm_setitimer(unsigned int seconds);
 extern int do_getitimer(int which, struct itimerval *value);
 extern void getnstimeofday(struct timespec *tv);
 extern void getrawmonotonic(struct timespec *ts);
+extern void getnstime_raw_and_real(struct timespec *ts_raw,
+		struct timespec *ts_real);
 extern void getboottime(struct timespec *ts);
 extern void monotonic_to_bootbased(struct timespec *ts);
 
kernel/time/timekeeping.c +38 -0
index 49010d8..947c7dc 100644
@@ -285,6 +285,44 @@ void ktime_get_ts(struct timespec *ts)
 EXPORT_SYMBOL_GPL(ktime_get_ts);
 
 /**
+ * getnstime_raw_and_real - Returns both the time of day an raw
+ * monotonic time in a timespec format
+ * @ts_mono_raw:	pointer to the timespec to be set to raw
+ *			monotonic time
+ * @ts_real:		pointer to the timespec to be set to the time
+ *			of day
+ */
+void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real)
+{
+	unsigned long seq;
+	s64 nsecs_raw, nsecs_real;
+
+	WARN_ON_ONCE(timekeeping_suspended);
+
+	do {
+		u32 arch_offset;
+
+		seq = read_seqbegin(&xtime_lock);
+
+		*ts_raw = raw_time;
+		*ts_real = xtime;
+
+		nsecs_raw = timekeeping_get_ns_raw();
+		nsecs_real = timekeeping_get_ns();
+
+		/* If arch requires, add in gettimeoffset() */
+		arch_offset = arch_gettimeoffset();
+		nsecs_raw += arch_offset;
+		nsecs_real += arch_offset;
+
+	} while (read_seqretry(&xtime_lock, seq));
+
+	timespec_add_ns(ts_raw, nsecs_raw);
+	timespec_add_ns(ts_real, nsecs_real);
+}
+EXPORT_SYMBOL(getnstime_raw_and_real);
+
+/**
  * do_gettimeofday - Returns the time of day in a timeval
  * @tv:		pointer to the timeval to be set
  *
Signature · 1 line
1.7.2.3
Acked-by on v4 14/17
#46 lore raw

Re: [PATCHv4 14/17] pps: capture MONOTONIC_RAW timestamps as well

Quoted 8 lines · Alexander Gordeev

MONOTONIC_RAW clock timestamps are ideally suited for frequency calculation and also fit well into the original NTP hardpps design. Now phase and frequency can be adjusted separately: the former based on REALTIME clock and the latter based on MONOTONIC_RAW clock. A new function getnstime_raw_and_real is added to timekeeping subsystem to capture both timestamps at the same time and atomically.

Quoted 89 lines
---
 include/linux/pps_kernel.h |    3 ++-
 include/linux/time.h       |    2 ++
 kernel/time/timekeeping.c  |   38 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 42 insertions(+), 1 deletions(-)
include/linux/pps_kernel.h +2 -1
index 5af0498..39fc7125 100644
@@ -48,6 +48,7 @@ struct pps_source_info {
 };
 
 struct pps_event_time {
+	struct timespec ts_raw;
 	struct timespec ts_real;
 };
 
@@ -111,7 +112,7 @@ static inline void timespec_to_pps_ktime(struct pps_ktime *kt,
 
 static inline void pps_get_ts(struct pps_event_time *ts)
 {
-	getnstimeofday(&ts->ts_real);
+	getnstime_raw_and_real(&ts->ts_raw, &ts->ts_real);
 }
 
 #endif /* LINUX_PPS_KERNEL_H */
include/linux/time.h +2 -0
index 9f15ac7..1e6d3b5 100644
@@ -158,6 +158,8 @@ extern unsigned int alarm_setitimer(unsigned int seconds);
 extern int do_getitimer(int which, struct itimerval *value);
 extern void getnstimeofday(struct timespec *tv);
 extern void getrawmonotonic(struct timespec *ts);
+extern void getnstime_raw_and_real(struct timespec *ts_raw,
+		struct timespec *ts_real);
 extern void getboottime(struct timespec *ts);
 extern void monotonic_to_bootbased(struct timespec *ts);
 
kernel/time/timekeeping.c +38 -0
index 49010d8..947c7dc 100644
@@ -285,6 +285,44 @@ void ktime_get_ts(struct timespec *ts)
 EXPORT_SYMBOL_GPL(ktime_get_ts);
 
 /**
+ * getnstime_raw_and_real - Returns both the time of day an raw
+ * monotonic time in a timespec format
+ * @ts_mono_raw:	pointer to the timespec to be set to raw
+ *			monotonic time
+ * @ts_real:		pointer to the timespec to be set to the time
+ *			of day
+ */
+void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real)
+{
+	unsigned long seq;
+	s64 nsecs_raw, nsecs_real;
+
+	WARN_ON_ONCE(timekeeping_suspended);
+
+	do {
+		u32 arch_offset;
+
+		seq = read_seqbegin(&xtime_lock);
+
+		*ts_raw = raw_time;
+		*ts_real = xtime;
+
+		nsecs_raw = timekeeping_get_ns_raw();
+		nsecs_real = timekeeping_get_ns();
+
+		/* If arch requires, add in gettimeoffset() */
+		arch_offset = arch_gettimeoffset();
+		nsecs_raw += arch_offset;
+		nsecs_real += arch_offset;
+
+	} while (read_seqretry(&xtime_lock, seq));
+
+	timespec_add_ns(ts_raw, nsecs_raw);
+	timespec_add_ns(ts_real, nsecs_real);
+}
+EXPORT_SYMBOL(getnstime_raw_and_real);
+
+/**
  * do_gettimeofday - Returns the time of day in a timeval
  * @tv:		pointer to the timeval to be set
  *
#47 lore raw

PATCH v4 15/17 pps: add kernel consumer support

In reply to Alexander Gordeev

Add an optional feature of PPSAPI, kernel consumer support, which uses the added hardpps() function.

---
 Documentation/ioctl/ioctl-number.txt |    2 +-
 drivers/pps/Kconfig                  |    1 +
 drivers/pps/kapi.c                   |   23 ++++++++++++
 drivers/pps/pps.c                    |   62 +++++++++++++++++++++++++++++++++-
 include/linux/pps.h                  |    7 ++++
 include/linux/pps_kernel.h           |    6 +++
 6 files changed, 99 insertions(+), 2 deletions(-)
Documentation/ioctl/ioctl-number.txt +1 -1
index 33223ff..de2b113 100644
@@ -249,7 +249,7 @@ Code  Seq#(hex)	Include File		Comments
 'p'	40-7F	linux/nvram.h
 'p'	80-9F	linux/ppdev.h		user-space parport
 					<mailto:[email protected]>
-'p'	A1-A4	linux/pps.h		LinuxPPS
+'p'	A1-A5	linux/pps.h		LinuxPPS
 					<mailto:[email protected]>
 'q'	00-1F	linux/serio.h
 'q'	80-FF	linux/telephony.h	Internet PhoneJACK, Internet LineJACK
drivers/pps/Kconfig +1 -0
index 33c9126..4823e47 100644
@@ -7,6 +7,7 @@ menu "PPS support"
 config PPS
 	tristate "PPS support"
 	depends on EXPERIMENTAL
+	select NTP_PPS
 	---help---
 	  PPS (Pulse Per Second) is a special pulse provided by some GPS
 	  antennae. Userland can use it to get a high-precision time
drivers/pps/kapi.c +23 -0
index 2bdfbed..0026c99 100644
@@ -26,6 +26,7 @@
 #include <linux/init.h>
 #include <linux/sched.h>
 #include <linux/time.h>
+#include <linux/timex.h>
 #include <linux/spinlock.h>
 #include <linux/fs.h>
 #include <linux/pps_kernel.h>
@@ -38,6 +39,12 @@
 /* PPS event workqueue */
 struct workqueue_struct *pps_event_workqueue;
 
+/* state variables to bind kernel consumer */
+/* PPS API (RFC 2783): current source and mode for ``kernel consumer'' */
+DEFINE_SPINLOCK(pps_kc_hardpps_lock);
+void	*pps_kc_hardpps_dev;	/* some unique pointer to device */
+int	pps_kc_hardpps_mode;	/* mode bits for kernel consumer */
+
 /*
  * Local functions
  */
@@ -141,6 +148,16 @@ EXPORT_SYMBOL(pps_register_source);
 
 void pps_unregister_source(struct pps_device *pps)
 {
+	spin_lock(&pps_kc_hardpps_lock);
+	if (pps == pps_kc_hardpps_dev) {
+		pps_kc_hardpps_mode = 0;
+		pps_kc_hardpps_dev = NULL;
+		spin_unlock(&pps_kc_hardpps_lock);
+		dev_info(pps->dev, "unbound kernel consumer"
+				" on device removal\n");
+	} else
+		spin_unlock(&pps_kc_hardpps_lock);
+
 	pps_unregister_cdev(pps);
 
 	/* don't have to kfree(pps) here because it will be done on
@@ -213,6 +230,12 @@ void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
 		captured = ~0;
 	}
 
+	/* Pass some events to kernel consumer if activated */
+	spin_lock(&pps_kc_hardpps_lock);
+	if (pps == pps_kc_hardpps_dev && event & pps_kc_hardpps_mode)
+		hardpps(&ts->ts_real, &ts->ts_raw);
+	spin_unlock(&pps_kc_hardpps_lock);
+
 	/* Wake up if captured something */
 	if (captured) {
 		pps->last_ev++;
drivers/pps/pps.c +61 -1

70 lines not on this page · raw message

include/linux/pps.h +7 -0
index 0194ab0..a9bb1d9 100644
@@ -114,11 +114,18 @@ struct pps_fdata {
 	struct pps_ktime timeout;
 };
 
+struct pps_bind_args {
+	int tsformat;	/* format of time stamps */
+	int edge;	/* selected event type */
+	int consumer;	/* selected kernel consumer */
+};
+
 #include <linux/ioctl.h>
 
 #define PPS_GETPARAMS		_IOR('p', 0xa1, struct pps_kparams *)
 #define PPS_SETPARAMS		_IOW('p', 0xa2, struct pps_kparams *)
 #define PPS_GETCAP		_IOR('p', 0xa3, int *)
 #define PPS_FETCH		_IOWR('p', 0xa4, struct pps_fdata *)
+#define PPS_KC_BIND		_IOW('p', 0xa5, struct pps_bind_args *)
 
 #endif /* _PPS_H_ */
include/linux/pps_kernel.h +6 -0
index 39fc7125..10e10c8 100644
@@ -87,6 +87,12 @@ struct pps_device {
 
 extern struct device_attribute pps_attrs[];
 
+/* state variables to bind kernel consumer */
+/* PPS API (RFC 2783): current source and mode for ``kernel consumer'' */
+extern spinlock_t pps_kc_hardpps_lock;
+extern void *pps_kc_hardpps_dev;	/* some unique pointer to device */
+extern int pps_kc_hardpps_mode;		/* mode bits for kernel consumer */
+
 extern struct workqueue_struct *pps_event_workqueue;
 
 /*
Signature · 1 line
1.7.2.3
#48 lore raw

PATCH v4 16/17 pps: add parallel port PPS client

In reply to Alexander Gordeev

Add parallel port PPS client. It uses a standard method for capturing timestamps for assert edge transitions: getting a timestamp soon after an interrupt has happened. This is not a very precise source of time information due to interrupt handling delays. However, timestamps for clear edge transitions are much more precise because the interrupt handler continuously polls hardware port until the transition is done. Hardware port operations require only about 1us so the maximum error should not exceed this value. This was my primary goal when developing this client.
Clear edge capture could be disabled using clear_wait parameter.

---
 drivers/pps/clients/Kconfig       |    7 +
 drivers/pps/clients/Makefile      |    1 +
 drivers/pps/clients/pps_parport.c |  247 +++++++++++++++++++++++++++++++++++++
 3 files changed, 255 insertions(+), 0 deletions(-)
 create mode 100644 drivers/pps/clients/pps_parport.c
drivers/pps/clients/Kconfig +7 -0
index 4e801bd..8520a7f 100644
@@ -22,4 +22,11 @@ config PPS_CLIENT_LDISC
 	  If you say yes here you get support for a PPS source connected
 	  with the CD (Carrier Detect) pin of your serial port.
 
+config PPS_CLIENT_PARPORT
+	tristate "Parallel port PPS client"
+	depends on PPS && PARPORT
+	help
+	  If you say yes here you get support for a PPS source connected
+	  with the interrupt pin of your parallel port.
+
 endif
drivers/pps/clients/Makefile +1 -0
index 812c9b1..42517da 100644
@@ -4,6 +4,7 @@
 
 obj-$(CONFIG_PPS_CLIENT_KTIMER)	+= pps-ktimer.o
 obj-$(CONFIG_PPS_CLIENT_LDISC)	+= pps-ldisc.o
+obj-$(CONFIG_PPS_CLIENT_PARPORT) += pps_parport.o
 
 ifeq ($(CONFIG_PPS_DEBUG),y)
 EXTRA_CFLAGS += -DDEBUG
drivers/pps/clients/pps_parport.c new file +247 -0

247 lines not on this page · raw message

Signature · 1 line
1.7.2.3
#49 lore raw

PATCH v4 17/17 pps: add parallel port PPS signal generator

In reply to Alexander Gordeev

Add PPS signal generator which utilizes STROBE pin of a parallel port to send PPS signals. It uses parport abstraction layer and hrtimers to precisely control the signal.

---
 Documentation/pps/pps.txt                |   46 +++++
 drivers/pps/Kconfig                      |    2 +
 drivers/pps/Makefile                     |    2 +-
 drivers/pps/generators/Kconfig           |   17 ++
 drivers/pps/generators/Makefile          |    9 +
 drivers/pps/generators/pps_gen_parport.c |  275 ++++++++++++++++++++++++++++++
 6 files changed, 350 insertions(+), 1 deletions(-)
 create mode 100644 drivers/pps/generators/Kconfig
 create mode 100644 drivers/pps/generators/Makefile
 create mode 100644 drivers/pps/generators/pps_gen_parport.c
Documentation/pps/pps.txt +46 -0
index 125f4ab..d35dcdd 100644
@@ -170,3 +170,49 @@ and the run ppstest as follow:
 
 Please, note that to compile userland programs you need the file timepps.h
 (see Documentation/pps/).
+
+
+Generators
+----------
+
+Sometimes one needs to be able not only to catch PPS signals but to produce
+them also. For example, running a distributed simulation, which requires
+computers' clock to be synchronized very tightly. One way to do this is to
+invent some complicated hardware solutions but it may be neither necessary
+nor affordable. The cheap way is to load a PPS generator on one of the
+computers (master) and PPS clients on others (slaves), and use very simple
+cables to deliver signals using parallel ports, for example.
+
+Parallel port cable pinout:
+pin	name	master      slave
+1	STROBE	  *------     *
+2	D0	  *     |     *
+3	D1	  *     |     *
+4	D2	  *     |     *
+5	D3	  *     |     *
+6	D4	  *     |     *
+7	D5	  *     |     *
+8	D6	  *     |     *
+9	D7	  *     |     *
+10	ACK	  *     ------*
+11	BUSY	  *           *
+12	PE	  *           *
+13	SEL	  *           *
+14	AUTOFD	  *           *
+15	ERROR	  *           *
+16	INIT	  *           *
+17	SELIN	  *           *
+18-25	GND	  *-----------*
+
+Please note that parallel port interrupt occurs only on high->low transition,
+so it is used for PPS assert edge. PPS clear edge can be determined only
+using polling in the interrupt handler which actually can be done way more
+precisely because interrupt handling delays can be quite big and random. So
+current parport PPS generator implementation (pps_gen_parport module) is
+geared towards using the clear edge for time synchronization.
+
+Clear edge polling is done with disabled interrupts so it's better to select
+delay between assert and clear edge as small as possible to reduce system
+latencies. But if it is too small slave won't be able to capture clear edge
+transition. The default of 30us should be good enough in most situations.
+The delay can be selected using 'delay' pps_gen_parport module parameter.
drivers/pps/Kconfig +2 -0
index 4823e47..79bda60 100644
@@ -40,4 +40,6 @@ config NTP_PPS
 
 source drivers/pps/clients/Kconfig
 
+source drivers/pps/generators/Kconfig
+
 endmenu
drivers/pps/Makefile +1 -1
index 98960dd..1906eb70 100644
@@ -4,6 +4,6 @@
 
 pps_core-y			:= pps.o kapi.o sysfs.o
 obj-$(CONFIG_PPS)		:= pps_core.o
-obj-y				+= clients/
+obj-y				+= clients/ generators/
 
 ccflags-$(CONFIG_PPS_DEBUG) := -DDEBUG
drivers/pps/generators/Kconfig new file +17 -0
new file mode 100644
index 0000000..5fbd614
@@ -0,0 +1,17 @@
+#
+# PPS generators configuration
+#
+
+if PPS
+
+comment "PPS generators support"
+
+config PPS_GENERATOR_PARPORT
+	tristate "Parallel port PPS signal generator"
+	depends on PARPORT != n && GENERIC_TIME
+	help
+	  If you say yes here you get support for a PPS signal generator which
+	  utilizes STROBE pin of a parallel port to send PPS signals. It uses
+	  parport abstraction layer and hrtimers to precisely control the signal.
+
+endif
drivers/pps/generators/Makefile new file +9 -0
new file mode 100644
index 0000000..303304a
@@ -0,0 +1,9 @@
+#
+# Makefile for PPS generators.
+#
+
+obj-$(CONFIG_PPS_GENERATOR_PARPORT) += pps_gen_parport.o
+
+ifeq ($(CONFIG_PPS_DEBUG),y)
+EXTRA_CFLAGS += -DDEBUG
+endif
drivers/pps/generators/pps_gen_parport.c new file +275 -0

275 lines not on this page · raw message

Signature · 1 line
1.7.2.3