Skip to contentSkip to conversation

printk: drop logbuf_cpu volatile qualifier

PATCH In review Alex Elder 3 participants 6 messages Nov 14, 2014 – Nov 18, 2014 Printk

lore
Share & tools
lore

Alex Elder

author
#0 lore raw

PATCH 1/1 printk: drop logbuf_cpu volatile qualifier

Pranith Kumar posted a patch in which removed the "volatile" qualifier for the "logbuf_cpu" variable in vprintk_emit().

    https://lkml.org/lkml/2014/11/13/894

In his patch, he used ACCESS_ONCE() for all references to that symbol to provide whatever protection was intended.

There was some discussion that followed, and in the end Stephen Rostedt concluded that not only was "volatile" not needed, neither was it required to use ACCESS_ONCE(). I offered an elaborate description that concluded Stephen was right, and Pranith asked me to submit an alternative patch. And this is it.

The basic reason "volatile" is not needed is that "logbuf_cpu" has static storage duration, and vprintk_emit() is an exported interface. This means that the value of logbuf_cpu must be read from memory the first time it is used in a particular call of vprintk_emit(). The variable's value is read only once in that function, when it's read it'll be the copy from memory (or cache).

In addition, the value of "logbuf_cpu" is only ever written under protection of a spinlock. So the value that is read is the "real" value (and not an out-of-date cached one). If its value is not UINT_MAX, it is the current CPU's processor id, and it will have been last written by the running CPU.

---
Note!!! I am not able to stress test this patch.

 kernel/printk/printk.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/printk/printk.c +1 -1
index ced2b84..fefc8d0 100644
@@ -1627,7 +1627,7 @@ asmlinkage int vprintk_emit(int facility, int level,
 	int printed_len = 0;
 	bool in_sched = false;
 	/* cpu currently holding logbuf_lock in this function */
-	static volatile unsigned int logbuf_cpu = UINT_MAX;
+	static unsigned int logbuf_cpu = UINT_MAX;
 
 	if (level == SCHED_MESSAGE_LOGLEVEL) {
 		level = -1;
Signature · 1 line
1.9.1
#1 lore raw
Quoted 8 lines · On Fri, 14 Nov 2014 13:19:31 -0600 Alex Elder <[email protected]> wrote:

Pranith Kumar posted a patch in which removed the "volatile" qualifier for the "logbuf_cpu" variable in vprintk_emit().

    https://lkml.org/lkml/2014/11/13/894

In his patch, he used ACCESS_ONCE() for all references to that symbol to provide whatever protection was intended.

There was some discussion that followed, and in the end Stephen Rostedt concluded that not only was "volatile" not

I don't know of a Stephen Rostedt. J

Quoted 2 lines

needed, neither was it required to use ACCESS_ONCE(). I offered an elaborate description that concluded Stephen

Who is this Stephen you talk about?

Quoted 10 lines

was right, and Pranith asked me to submit an alternative patch. And this is it.

The basic reason "volatile" is not needed is that "logbuf_cpu" has static storage duration, and vprintk_emit() is an exported interface. This means that the value of logbuf_cpu must be read from memory the first time it is used in a particular call of vprintk_emit(). The variable's value is read only once in that function, when it's read it'll be the copy from memory (or cache).

Quoted 8 lines

In addition, the value of "logbuf_cpu" is only ever written under protection of a spinlock. So the value that is read is the "real" value (and not an out-of-date cached one). If its value is not UINT_MAX, it is the current CPU's processor id, and it will have been last written by the running CPU.

Ah, I know of him!

Quoted 3 lines

---
Note!!! I am not able to stress test this patch.

I know the guys at SuSE are stressing printk a bit. Maybe they can come up with something. (Cc'd).

Thanks,

-- Steve

Quoted 17 lines
 kernel/printk/printk.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/printk/printk.c +1 -1
index ced2b84..fefc8d0 100644
@@ -1627,7 +1627,7 @@ asmlinkage int vprintk_emit(int facility, int level,
 	int printed_len = 0;
 	bool in_sched = false;
 	/* cpu currently holding logbuf_lock in this function */
-	static volatile unsigned int logbuf_cpu = UINT_MAX;
+	static unsigned int logbuf_cpu = UINT_MAX;
 
 	if (level == SCHED_MESSAGE_LOGLEVEL) {
 		level = -1;
author
#2 lore raw
Quoted 9 lines · Steven Rostedt
Quoted 6 lines · On Fri, 14 Nov 2014 13:19:31 -0600 Alex Elder <[email protected]> wrote:

Pranith Kumar posted a patch in which removed the "volatile" qualifier for the "logbuf_cpu" variable in vprintk_emit().

    https://lkml.org/lkml/2014/11/13/894

In his patch, he used ACCESS_ONCE() for all references to that symbol to provide whatever protection was intended.

Quoted 4 lines
Quoted 2 lines

There was some discussion that followed, and in the end Stephen Rostedt concluded that not only was "volatile" not

I don't know of a Stephen Rostedt. J

Sorry. I'm sure that's a source of annoyance. -Alex

Quoted 4 lines
Quoted 2 lines

needed, neither was it required to use ACCESS_ONCE(). I offered an elaborate description that concluded Stephen

Who is this Stephen you talk about?

. . .

#3 lore raw
Quoted 10 lines · On Fri, 14 Nov 2014 13:46:44 -0600 Alex Elder <[email protected]> wrote:
Quoted 9 lines · Steven Rostedt
Quoted 6 lines · On Fri, 14 Nov 2014 13:19:31 -0600 Alex Elder <[email protected]> wrote:

Pranith Kumar posted a patch in which removed the "volatile" qualifier for the "logbuf_cpu" variable in vprintk_emit().

    https://lkml.org/lkml/2014/11/13/894

In his patch, he used ACCESS_ONCE() for all references to that symbol to provide whatever protection was intended.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

There was some discussion that followed, and in the end Stephen Rostedt concluded that not only was "volatile" not

I don't know of a Stephen Rostedt. J

Sorry. I'm sure that's a source of annoyance. -Alex

No problem. If you are wondering about that "J".

https://www.facebook.com/mcgrof/posts/10104459749108919

It's my new thing. Maybe I can start a trend. J

-- Steve

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

needed, neither was it required to use ACCESS_ONCE(). I offered an elaborate description that concluded Stephen

Who is this Stephen you talk about?

. . .

author
#4 lore raw
Quoted 13 lines · Steven Rostedt
Quoted 10 lines · On Fri, 14 Nov 2014 13:46:44 -0600 Alex Elder <[email protected]> wrote:
Quoted 9 lines · Steven Rostedt
Quoted 6 lines · On Fri, 14 Nov 2014 13:19:31 -0600 Alex Elder <[email protected]> wrote:

Pranith Kumar posted a patch in which removed the "volatile" qualifier for the "logbuf_cpu" variable in vprintk_emit().

    https://lkml.org/lkml/2014/11/13/894

In his patch, he used ACCESS_ONCE() for all references to that symbol to provide whatever protection was intended.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

There was some discussion that followed, and in the end Stephen Rostedt concluded that not only was "volatile" not

I don't know of a Stephen Rostedt. J

Sorry. I'm sure that's a source of annoyance. -Alex

No problem. If you are wondering about that "J".

Yes, as a matter of fact, I did wonder.

Quoted 3 lines

https://www.facebook.com/mcgrof/posts/10104459749108919

It's my new thing. Maybe I can start a trend. J

I'm not big on smileys. L

But I'll remain neutral in this case. K

-Alex

Quoted 10 lines

-- Steve

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

needed, neither was it required to use ACCESS_ONCE(). I offered an elaborate description that concluded Stephen

Who is this Stephen you talk about?

. . .

Reviewed-by on v1 1/1
#5 lore raw
In reply to Alex Elder
Quoted 26 lines · Alex Elder

Pranith Kumar posted a patch in which removed the "volatile" qualifier for the "logbuf_cpu" variable in vprintk_emit().

    https://lkml.org/lkml/2014/11/13/894

In his patch, he used ACCESS_ONCE() for all references to that symbol to provide whatever protection was intended.

There was some discussion that followed, and in the end Stephen Rostedt concluded that not only was "volatile" not needed, neither was it required to use ACCESS_ONCE(). I offered an elaborate description that concluded Stephen was right, and Pranith asked me to submit an alternative patch. And this is it.

The basic reason "volatile" is not needed is that "logbuf_cpu" has static storage duration, and vprintk_emit() is an exported interface. This means that the value of logbuf_cpu must be read from memory the first time it is used in a particular call of vprintk_emit(). The variable's value is read only once in that function, when it's read it'll be the copy from memory (or cache).

In addition, the value of "logbuf_cpu" is only ever written under protection of a spinlock. So the value that is read is the "real" value (and not an out-of-date cached one). If its value is not UINT_MAX, it is the current CPU's processor id, and it will have been last written by the running CPU.

Quoted 5 lines

---
Note!!! I am not able to stress test this patch.

The patch looks good to me. You can add:

BTW, stress-testing this is tough. I never saw printk recursion happening
in practice - only when I screwed up something in printk code when experimenting.

Honza

Quoted 19 lines
 kernel/printk/printk.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/printk/printk.c +1 -1
index ced2b84..fefc8d0 100644
@@ -1627,7 +1627,7 @@ asmlinkage int vprintk_emit(int facility, int level,
 	int printed_len = 0;
 	bool in_sched = false;
 	/* cpu currently holding logbuf_lock in this function */
-	static volatile unsigned int logbuf_cpu = UINT_MAX;
+	static unsigned int logbuf_cpu = UINT_MAX;
 
 	if (level == SCHED_MESSAGE_LOGLEVEL) {
 		level = -1;
Signature · 1 line
1.9.1
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR