Skip to contentSkip to conversation

sched: Add sched_clock_register_new()

PATCH In review Paul Cercueil 2 participants 4 messages Feb 10, 2020 – Feb 11, 2020 Clocksource, clockevent drivers

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/2 sched: Add sched_clock_register_new()

The sched_clock_register_new() behaves like sched_clock_register() but takes an extra parameter which is passed to the read callback.

---

Notes:
v4: New patch

 include/linux/sched_clock.h |  2 ++
 kernel/time/sched_clock.c   | 36 +++++++++++++++++++++++++-----------
 2 files changed, 27 insertions(+), 11 deletions(-)
include/linux/sched_clock.h +2 -0
index 0bb04a96a6d4..15509487fd16 100644
@@ -10,6 +10,8 @@ extern void generic_sched_clock_init(void);
 
 extern void sched_clock_register(u64 (*read)(void), int bits,
 				 unsigned long rate);
+extern void sched_clock_register_new(u64 (*read)(void *), int bits,
+				     unsigned long rate, void *data);
 #else
 static inline void generic_sched_clock_init(void) { }
 
kernel/time/sched_clock.c +25 -11
index e4332e3e2d56..9ba61814ea48 100644
@@ -27,6 +27,7 @@
  * @sched_clock_mask:   Bitmask for two's complement subtraction of non 64bit
  *			clocks.
  * @read_sched_clock:	Current clock source (or dummy source when suspended).
+ * @data:		Callback data for the current clock source.
  * @mult:		Multipler for scaled math conversion.
  * @shift:		Shift value for scaled math conversion.
  *
@@ -39,7 +40,8 @@ struct clock_read_data {
 	u64 epoch_ns;
 	u64 epoch_cyc;
 	u64 sched_clock_mask;
-	u64 (*read_sched_clock)(void);
+	u64 (*read_sched_clock)(void *);
+	void *data;
 	u32 mult;
 	u32 shift;
 };
@@ -53,6 +55,7 @@ struct clock_read_data {
  * @read_data:		Data required to read from sched_clock.
  * @wrap_kt:		Duration for which clock can run before wrapping.
  * @rate:		Tick rate of the registered clock.
+ * @data:		Callback data for the registered clock read function.
  * @actual_read_sched_clock: Registered hardware level clock read function.
  *
  * The ordering of this structure has been chosen to optimize cache
@@ -65,7 +68,8 @@ struct clock_data {
 	ktime_t			wrap_kt;
 	unsigned long		rate;
 
-	u64 (*actual_read_sched_clock)(void);
+	void			*data;
+	u64 (*actual_read_sched_clock)(void *);
 };
 
 static struct hrtimer sched_clock_timer;
@@ -73,7 +77,7 @@ static int irqtime = -1;
 
 core_param(irqtime, irqtime, int, 0400);
 
-static u64 notrace jiffy_sched_clock_read(void)
+static u64 notrace jiffy_sched_clock_read(void *d)
 {
 	/*
 	 * We don't need to use get_jiffies_64 on 32-bit arches here
@@ -103,7 +107,7 @@ unsigned long long notrace sched_clock(void)
 		seq = raw_read_seqcount(&cd.seq);
 		rd = cd.read_data + (seq & 1);
 
-		cyc = (rd->read_sched_clock() - rd->epoch_cyc) &
+		cyc = (rd->read_sched_clock(rd->data) - rd->epoch_cyc) &
 		      rd->sched_clock_mask;
 		res = rd->epoch_ns + cyc_to_ns(cyc, rd->mult, rd->shift);
 	} while (read_seqcount_retry(&cd.seq, seq));
@@ -147,7 +151,7 @@ static void update_sched_clock(void)
 
 	rd = cd.read_data[0];
 
-	cyc = cd.actual_read_sched_clock();
+	cyc = cd.actual_read_sched_clock(cd.data);
 	ns = rd.epoch_ns + cyc_to_ns((cyc - rd.epoch_cyc) & rd.sched_clock_mask, rd.mult, rd.shift);
 
 	rd.epoch_ns = ns;
@@ -165,7 +169,8 @@ static enum hrtimer_restart sched_clock_poll(struct hrtimer *hrt)
 }
 
 void __init
-sched_clock_register(u64 (*read)(void), int bits, unsigned long rate)
+sched_clock_register_new(u64 (*read)(void *), int bits,
+			 unsigned long rate, void *data)
 {
 	u64 res, wrap, new_mask, new_epoch, cyc, ns;
 	u32 new_mult, new_shift;
@@ -192,12 +197,14 @@ sched_clock_register(u64 (*read)(void), int bits, unsigned long rate)
 	rd = cd.read_data[0];
 
 	/* Update epoch for new counter and update 'epoch_ns' from old counter*/
-	new_epoch = read();
-	cyc = cd.actual_read_sched_clock();
+	new_epoch = read(data);
+	cyc = cd.actual_read_sched_clock(cd.data);
 	ns = rd.epoch_ns + cyc_to_ns((cyc - rd.epoch_cyc) & rd.sched_clock_mask, rd.mult, rd.shift);
 	cd.actual_read_sched_clock = read;
+	cd.data = data;
 
 	rd.read_sched_clock	= read;
+	rd.data			= data;
 	rd.sched_clock_mask	= new_mask;
 	rd.mult			= new_mult;
 	rd.shift		= new_shift;
@@ -239,6 +246,12 @@ sched_clock_register(u64 (*read)(void), int bits, unsigned long rate)
 	pr_debug("Registered %pS as sched_clock source\n", read);
 }
 
+void __init
+sched_clock_register(u64 (*read)(void), int bits, unsigned long rate)
+{
+	sched_clock_register_new((u64 (*)(void *))read, bits, rate, NULL);
+}
+
 void __init generic_sched_clock_init(void)
 {
 	/*
@@ -246,7 +259,8 @@ void __init generic_sched_clock_init(void)
 	 * make it the final one one.
 	 */
 	if (cd.actual_read_sched_clock == jiffy_sched_clock_read)
-		sched_clock_register(jiffy_sched_clock_read, BITS_PER_LONG, HZ);
+		sched_clock_register_new(jiffy_sched_clock_read,
+					 BITS_PER_LONG, HZ, NULL);
 
 	update_sched_clock();
 
@@ -270,7 +284,7 @@ void __init generic_sched_clock_init(void)
  * at the end of the critical section to be sure we observe the
  * correct copy of 'epoch_cyc'.
  */
-static u64 notrace suspended_sched_clock_read(void)
+static u64 notrace suspended_sched_clock_read(void *d)
 {
 	unsigned int seq = raw_read_seqcount(&cd.seq);
 
@@ -292,7 +306,7 @@ void sched_clock_resume(void)
 {
 	struct clock_read_data *rd = &cd.read_data[0];
 
-	rd->epoch_cyc = cd.actual_read_sched_clock();
+	rd->epoch_cyc = cd.actual_read_sched_clock(cd.data);
 	hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL);
 	rd->read_sched_clock = cd.actual_read_sched_clock;
 }
Signature · 1 line
2.24.1
#1 lore raw

PATCH v4 2/2 clocksource: Add driver for the Ingenic JZ47xx OST

From: Maarten ter Huurne <[email protected]>

OST is the OS Timer, a 64-bit timer/counter with buffered reading.

SoCs before the JZ4770 had (if any) a 32-bit OST; the JZ4770 and JZ4780 have a 64-bit OST.

This driver will register both a clocksource and a sched_clock to the system.

---

Notes:
v2: local_irq_save/restore were moved within sched_clock_register
v3: Only register as 32-bit clocksource to simplify things
v4: - Bypass regmap to read counters.
- Use sched_clock_register_new() to avoid having a global pointer to the ingenic_ost.
- Don't print error codes in probe, since they will be printed anyway if the probe function fails.
- Rebased on 5.6-rc1.

 drivers/clocksource/Kconfig       |   8 ++
 drivers/clocksource/Makefile      |   1 +
 drivers/clocksource/ingenic-ost.c | 194 ++++++++++++++++++++++++++++++
 3 files changed, 203 insertions(+)
 create mode 100644 drivers/clocksource/ingenic-ost.c
drivers/clocksource/Kconfig +8 -0
index cc909e465823..f2142e6bbea3 100644
@@ -697,6 +697,14 @@ config INGENIC_TIMER
 	help
 	  Support for the timer/counter unit of the Ingenic JZ SoCs.
 
+config INGENIC_OST
+	bool "Clocksource for Ingenic OS Timer"
+	depends on MIPS || COMPILE_TEST
+	depends on COMMON_CLK
+	select MFD_SYSCON
+	help
+	  Support for the Operating System Timer of the Ingenic JZ SoCs.
+
 config MICROCHIP_PIT64B
 	bool "Microchip PIT64B support"
 	depends on OF || COMPILE_TEST
drivers/clocksource/Makefile +1 -0
index 713686faa549..641ba5383ab5 100644
@@ -80,6 +80,7 @@ obj-$(CONFIG_ASM9260_TIMER)		+= asm9260_timer.o
 obj-$(CONFIG_H8300_TMR8)		+= h8300_timer8.o
 obj-$(CONFIG_H8300_TMR16)		+= h8300_timer16.o
 obj-$(CONFIG_H8300_TPU)			+= h8300_tpu.o
+obj-$(CONFIG_INGENIC_OST)		+= ingenic-ost.o
 obj-$(CONFIG_INGENIC_TIMER)		+= ingenic-timer.o
 obj-$(CONFIG_CLKSRC_ST_LPC)		+= clksrc_st_lpc.o
 obj-$(CONFIG_X86_NUMACHIP)		+= numachip.o
drivers/clocksource/ingenic-ost.c new file +194 -0

194 lines not on this page · raw message

Signature · 1 line
2.24.1
#2 lore raw
In reply to Paul Cercueil

Paul!

Quoted 1 line · Paul Cercueil

The sched_clock_register_new() behaves like sched_clock_register() but

This function name does not make any sense. Two years from now you are going to provide sched_clock_register_new_2_dot_0() ?

Quoted 1 line

takes an extra parameter which is passed to the read callback.

This lacks any form of justification why this function and the data pointer is required.

kernel/time/sched_clock.c struct clock_read_data { · line 43

Quoted 13 lines

* @sched_clock_mask: Bitmask for two's complement subtraction of non 64bit
* clocks.
* @read_sched_clock: Current clock source (or dummy source when suspended).
+ * @data: Callback data for the current clock source.
* @mult: Multipler for scaled math conversion.
* @shift: Shift value for scaled math conversion.
*

kernel/time/sched_clock.c +1 -1
@@ -39,7 +40,8 @@ struct clock_read_data {
 	u64 epoch_ns;
 	u64 epoch_cyc;
 	u64 sched_clock_mask;
-	u64 (*read_sched_clock)(void);
+	u64 (*read_sched_clock)(void *);

How is that supposed to work without fixing up _all_ sched clock instances? So the below typecast

kernel/time/sched_clock.c sched_clock_register(u64 (*read)(void), int bits, unsigned long rate) · line 252

Quoted 4 lines
kernel/time/sched_clock.c +4 -0
+void __init
+sched_clock_register(u64 (*read)(void), int bits, unsigned long rate)
+{
+	sched_clock_register_new((u64 (*)(void *))read, bits, rate, NULL);

makes it compile.

By pure luck this does not explode in your face at runtime when the existing read(void) functions are called with an argument. Any stack based argument passing calling convention would fall flat on it's nose.

While clever this is really an ugly hack.

As the clocksource for which you are doing this is a single instance, what's wrong with having some static storage for the information you need as any other driver which has the same problem does as well?

If there is really a point in avoiding a few bytes of static storage, then this needs to be cleaned up treewide and not hacked around.

Thanks,

tglx

#3 lore raw

Hi Thomas,

Quoted 9 lines · Thomas Gleixner

Paul!

Quoted 2 lines · Paul Cercueil

The sched_clock_register_new() behaves like sched_clock_register()
but

This function name does not make any sense. Two years from now you are going to provide sched_clock_register_new_2_dot_0() ?

I'm open to suggestions :)
The point of using a different function was to avoid a huge patchset to fix the 50+ drivers that use sched_clock_register().

Quoted 30 lines
Quoted 1 line

takes an extra parameter which is passed to the read callback.

This lacks any form of justification why this function and the data pointer is required.

Quoted 7 lines

* @sched_clock_mask: Bitmask for two's complement subtraction
of non 64bit
* clocks.
* @read_sched_clock: Current clock source (or dummy source when
suspended).
+ * @data: Callback data for the current clock source.
* @mult: Multipler for scaled math conversion.

Quoted 8 lines

* @shift: Shift value for scaled math conversion.
*
@@ -39,7 +40,8 @@ struct clock_read_data {
u64 epoch_ns;
u64 epoch_cyc;
u64 sched_clock_mask;
- u64 (*read_sched_clock)(void);
+ u64 (*read_sched_clock)(void *);

How is that supposed to work without fixing up _all_ sched clock instances? So the below typecast

Quoted 5 lines

+void __init
+sched_clock_register(u64 (*read)(void), int bits, unsigned long
rate)
+{
+ sched_clock_register_new((u64 (*)(void *))read, bits, rate, NULL);

Quoted 7 lines

makes it compile.

By pure luck this does not explode in your face at runtime when the existing read(void) functions are called with an argument. Any stack based argument passing calling convention would fall flat on it's nose.

While clever this is really an ugly hack.

Alright, I really didn't think it was that bad. Next time I'll use a wrapper.

Quoted 3 lines

As the clocksource for which you are doing this is a single instance, what's wrong with having some static storage for the information you need as any other driver which has the same problem does as well?

The fact that every other driver with the same problem decides to add a workaround instead of a proper fix does not mean that the problem does not exist.

Quoted 2 lines

If there is really a point in avoiding a few bytes of static storage, then this needs to be cleaned up treewide and not hacked around.

My allergy of static storage is not worth the trouble of a treewide pathset so I'll just drop this patch and send a v5.

Thanks,
-Paul