Skip to contentSkip to conversation

consolidate WARN_...ONCE() static variables

PATCH In review Jan Beulich 2 participants 11 messages Feb 27, 2012 – Feb 29, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 consolidate WARN_...ONCE() static variables

Due to the alignment of following variables, these typically consume more than just the single byte that 'bool' requires, and as there are a few hundred instances, the cache pollution (not so much the waste of memory) sums up. Put these variables into their own section, outside of any half way frequently used memory range.

v2: Do the same also to the __warned variable of rcu_lockdep_assert(). (Don't, however, include the ones used by printk_once() and alike, as they can potentially be hot.)

---
 include/asm-generic/bug.h         |    6 +++---
 include/asm-generic/vmlinux.lds.h |    1 +
 include/linux/rcupdate.h          |    2 +-
 3 files changed, 5 insertions(+), 4 deletions(-)
include/asm-generic/bug.h +3 -3
@@ -134,7 +134,7 @@ extern void warn_slowpath_null(const cha
 #endif
 
 #define WARN_ON_ONCE(condition)	({				\
-	static bool __warned;					\
+	static bool __section(.data.unlikely) __warned;		\
 	int __ret_warn_once = !!(condition);			\
 								\
 	if (unlikely(__ret_warn_once))				\
@@ -144,7 +144,7 @@ extern void warn_slowpath_null(const cha
 })
 
 #define WARN_ONCE(condition, format...)	({			\
-	static bool __warned;					\
+	static bool __section(.data.unlikely) __warned;		\
 	int __ret_warn_once = !!(condition);			\
 								\
 	if (unlikely(__ret_warn_once))				\
@@ -154,7 +154,7 @@ extern void warn_slowpath_null(const cha
 })
 
 #define WARN_TAINT_ONCE(condition, taint, format...)	({	\
-	static bool __warned;					\
+	static bool __section(.data.unlikely) __warned;		\
 	int __ret_warn_once = !!(condition);			\
 								\
 	if (unlikely(__ret_warn_once))				\
include/asm-generic/vmlinux.lds.h +1 -0
@@ -167,6 +167,7 @@
 	CPU_KEEP(exit.data)						\
 	MEM_KEEP(init.data)						\
 	MEM_KEEP(exit.data)						\
+	*(.data.unlikely)						\
 	STRUCT_ALIGN();							\
 	*(__tracepoints)						\
 	/* implement dynamic printk debug */				\
include/linux/rcupdate.h +1 -1
@@ -374,7 +374,7 @@ extern int rcu_my_thread_group_empty(voi
  */
 #define rcu_lockdep_assert(c, s)					\
 	do {								\
-		static bool __warned;					\
+		static bool __section(.data.unlikely) __warned;		\
 		if (debug_lockdep_rcu_enabled() && !__warned && !(c)) {	\
 			__warned = true;				\
 			lockdep_rcu_suspicious(__FILE__, __LINE__, s);	\
#1 lore raw
Quoted 9 lines · On Mon, 27 Feb 2012 15:10:34 +0000 "Jan Beulich" <[email protected]> wrote:

Due to the alignment of following variables, these typically consume more than just the single byte that 'bool' requires, and as there are a few hundred instances, the cache pollution (not so much the waste of memory) sums up. Put these variables into their own section, outside of any half way frequently used memory range.

v2: Do the same also to the __warned variable of rcu_lockdep_assert(). (Don't, however, include the ones used by printk_once() and alike, as they can potentially be hot.)

I have a bad feeling that I still don't understand this patch. Ho hum.

What are the rules for the new .data.unlikely section? When should people put variables into this section? Perhaps we can document this somewhere?

#2 lore raw
Quoted 14 lines
Quoted 1 line
Quoted 1 line · Andrew Morton
Quoted 9 lines · On Mon, 27 Feb 2012 15:10:34 +0000 "Jan Beulich" <[email protected]> wrote:

Due to the alignment of following variables, these typically consume more than just the single byte that 'bool' requires, and as there are a few hundred instances, the cache pollution (not so much the waste of memory) sums up. Put these variables into their own section, outside of any half way frequently used memory range.

v2: Do the same also to the __warned variable of rcu_lockdep_assert(). (Don't, however, include the ones used by printk_once() and alike, as they can potentially be hot.)

Quoted 5 lines

I have a bad feeling that I still don't understand this patch. Ho hum.

What are the rules for the new .data.unlikely section? When should people put variables into this section? Perhaps we can document this somewhere?

If I knew the "where" part of this, I could put together a few sentences. I just grep-ed through Documentation/, without finding e.g. any rules or guidelines for using {,un}likely()...

Jan

#3 lore raw
Quoted 16 lines · On Tue, 28 Feb 2012 07:41:54 +0000 "Jan Beulich" <[email protected]> wrote:
Quoted 16 lines
Quoted 1 line
Quoted 1 line · Andrew Morton
Quoted 9 lines · On Mon, 27 Feb 2012 15:10:34 +0000 "Jan Beulich" <[email protected]> wrote:

Due to the alignment of following variables, these typically consume more than just the single byte that 'bool' requires, and as there are a few hundred instances, the cache pollution (not so much the waste of memory) sums up. Put these variables into their own section, outside of any half way frequently used memory range.

v2: Do the same also to the __warned variable of rcu_lockdep_assert(). (Don't, however, include the ones used by printk_once() and alike, as they can potentially be hot.)

I have a bad feeling that I still don't understand this patch. Ho hum.

Quoted 8 lines
Quoted 3 lines

What are the rules for the new .data.unlikely section? When should people put variables into this section? Perhaps we can document this somewhere?

If I knew the "where" part of this, I could put together a few sentences. I just grep-ed through Documentation/, without finding e.g. any rules or guidelines for using {,un}likely()...

At the definition site in vmlinux.lds?

#4 lore raw
Quoted 23 lines
Quoted 1 line
Quoted 1 line · Andrew Morton
Quoted 20 lines · On Tue, 28 Feb 2012 07:41:54 +0000 "Jan Beulich" <[email protected]> wrote:
Quoted 19 lines
Quoted 1 line
Quoted 1 line · Andrew Morton
Quoted 9 lines · On Mon, 27 Feb 2012 15:10:34 +0000 "Jan Beulich" <[email protected]> wrote:

Due to the alignment of following variables, these typically consume more than just the single byte that 'bool' requires, and as there are a few hundred instances, the cache pollution (not so much the waste of memory) sums up. Put these variables into their own section, outside of any half way frequently used memory range.

v2: Do the same also to the __warned variable of rcu_lockdep_assert(). (Don't, however, include the ones used by printk_once() and alike, as they can potentially be hot.)

I have a bad feeling that I still don't understand this patch. Ho hum.

What are the rules for the new .data.unlikely section? When should people put variables into this section? Perhaps we can document this somewhere?

Quoted 6 lines
Quoted 4 lines

If I knew the "where" part of this, I could put together a few sentences. I just grep-ed through Documentation/, without finding e.g. any rules or guidelines for using {,un}likely()...

At the definition site in vmlinux.lds?

Sorry, Andrew, but this makes no sense to me. For one, vmlinux.lds{,h} don't define anything, they merely gather together all input sections. Second, the very similar use of .text.unlikely there isn't being explained in any way either (which makes sense given that that's a compiler generated section, which I just derived the new section's name from).

If anything, I would expect a place where all the special sections (.init*, .exit*, etc) are being explained, but this (if existing at all) is scattered around. While I don't mind adding a few words to something that already exists, I'm not really eager to write something from scratch.

Finally, if we're really looking forward to having this used in a broader manner (and particularly with objects wider than bool), then the section name should get changed again anyway (and perhaps some further abstraction be created): The goal of the patch is not only to get the symbols out of potentially hot cache lines, but (secondary, but nevertheless) also to avoid the non-negligible waste of space due to padding (taking into consideration that the compiler doesn't really do a good job in this regard, nor does it make meaningful attempts at providing the necessary infrastructure for doing so manually).

Jan

#5 lore raw
Quoted 23 lines · On Tue, 28 Feb 2012 08:02:40 +0000 "Jan Beulich" <[email protected]> wrote:
Quoted 23 lines
Quoted 1 line
Quoted 1 line · Andrew Morton
Quoted 20 lines · On Tue, 28 Feb 2012 07:41:54 +0000 "Jan Beulich" <[email protected]> wrote:
Quoted 19 lines
Quoted 1 line
Quoted 1 line · Andrew Morton
Quoted 9 lines · On Mon, 27 Feb 2012 15:10:34 +0000 "Jan Beulich" <[email protected]> wrote:

Due to the alignment of following variables, these typically consume more than just the single byte that 'bool' requires, and as there are a few hundred instances, the cache pollution (not so much the waste of memory) sums up. Put these variables into their own section, outside of any half way frequently used memory range.

v2: Do the same also to the __warned variable of rcu_lockdep_assert(). (Don't, however, include the ones used by printk_once() and alike, as they can potentially be hot.)

I have a bad feeling that I still don't understand this patch. Ho hum.

What are the rules for the new .data.unlikely section? When should people put variables into this section? Perhaps we can document this somewhere?

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

If I knew the "where" part of this, I could put together a few sentences. I just grep-ed through Documentation/, without finding e.g. any rules or guidelines for using {,un}likely()...

At the definition site in vmlinux.lds?

Sorry, Andrew, but this makes no sense to me.

I really don't care - anywhere you like. Just the darn changelog, if nowhere else.

I still don't have an answer to my question :( Some statement describing what the new section is *for*. Seems a pretty important thing?

#6 lore raw
Quoted 33 lines
Quoted 1 line
Quoted 1 line · Andrew Morton
Quoted 30 lines · On Tue, 28 Feb 2012 08:02:40 +0000 "Jan Beulich" <[email protected]> wrote:
Quoted 29 lines
Quoted 1 line
Quoted 1 line · Andrew Morton
Quoted 16 lines · On Tue, 28 Feb 2012 07:41:54 +0000 "Jan Beulich" <[email protected]> wrote:
Quoted 16 lines
Quoted 1 line
Quoted 1 line · Andrew Morton
Quoted 9 lines · On Mon, 27 Feb 2012 15:10:34 +0000 "Jan Beulich" <[email protected]> wrote:

Due to the alignment of following variables, these typically consume more than just the single byte that 'bool' requires, and as there are a few hundred instances, the cache pollution (not so much the waste of memory) sums up. Put these variables into their own section, outside of any half way frequently used memory range.

v2: Do the same also to the __warned variable of rcu_lockdep_assert(). (Don't, however, include the ones used by printk_once() and alike, as they can potentially be hot.)

I have a bad feeling that I still don't understand this patch. Ho hum.

Quoted 8 lines
Quoted 3 lines

What are the rules for the new .data.unlikely section? When should people put variables into this section? Perhaps we can document this somewhere?

If I knew the "where" part of this, I could put together a few sentences. I just grep-ed through Documentation/, without finding e.g. any rules or guidelines for using {,un}likely()...

At the definition site in vmlinux.lds?

Quoted 8 lines
Quoted 1 line

Sorry, Andrew, but this makes no sense to me.

I really don't care - anywhere you like. Just the darn changelog, if nowhere else.

I still don't have an answer to my question :( Some statement describing what the new section is *for*. Seems a pretty important thing?

Oh, sorry - to carry static data the accesses to which are unlikely (i.e., as in the case given, fully contained in code sections inside conditionals which themselves use unlikely() on their primary/only clause - in other words, something that the compiler really could do on its own).

Jan

#7 lore raw
Quoted 5 lines · On Tue, 28 Feb 2012 08:16:40 +0000 "Jan Beulich" <[email protected]> wrote:

Oh, sorry - to carry static data the accesses to which are unlikely (i.e., as in the case given, fully contained in code sections inside conditionals which themselves use unlikely() on their primary/only clause - in other words, something that the compiler really could do on its own).

I think I just learned more about this patch than at any time since we started discussing it.

Why add a new section, rather than using __read_mostly?

I suppose we should add and use a #define for this, like __read_mostly. That would be a good site for documenting it ;)

And I come back to my old friend printk_once(). If I'm understanding things correctly, we can/should make that test unlikely, then mark __print_once as __this_new_section? Otherwise... help!

btw, I don't think there's a significant performance benefit here - if the kernel is ever executing WARN_ON_ONCE(), WARN_ONCE() or printk_once() with any frequency then it is already badly broken.

Which brings us down to saving a bit of space. And I don't think I see how this saves space?

#8 lore raw
Quoted 9 lines
Quoted 1 line
Quoted 1 line · Andrew Morton
Quoted 5 lines · On Tue, 28 Feb 2012 08:16:40 +0000 "Jan Beulich" <[email protected]> wrote:

Oh, sorry - to carry static data the accesses to which are unlikely (i.e., as in the case given, fully contained in code sections inside conditionals which themselves use unlikely() on their primary/only clause - in other words, something that the compiler really could do on its own).

Quoted 4 lines

I think I just learned more about this patch than at any time since we started discussing it.

Why add a new section, rather than using __read_mostly?

Because __read_mostly data frequently is hot.

Quoted 2 lines

I suppose we should add and use a #define for this, like __read_mostly. That would be a good site for documenting it ;)

That's a possibility of course. I'll do so in an eventual v3.

Quoted 3 lines

And I come back to my old friend printk_once(). If I'm understanding things correctly, we can/should make that test unlikely, then mark __print_once as __this_new_section? Otherwise... help!

No. The variable itself serves for the tested condition here, and hence a priori you can't say whether the whole construct sits in a hot path (whether putting it in a hot path is a good idea is another question, but the print-this-one-time-only nature of it may be the very reason why someone considers this reasonable for his code).

As opposed to that, the access to the static variable in WARN_ONCE() is *after* an unlikely() condition was already evaluated, i.e. would generally sit on a code path that we hope doesn't get speculated along (often), by means of the compiler suitably arranging branch targets.

Quoted 3 lines

btw, I don't think there's a significant performance benefit here - if the kernel is ever executing WARN_ON_ONCE(), WARN_ONCE() or printk_once() with any frequency then it is already badly broken.

The intended performance benefit isn't with the WARN_ONCE() constructs - we certainly don't care much about them being executed efficiently - but instead with code path accessing other data in the same cache line (or spilled across multiple cache lines just because of the __warned variable sitting in the middle).

Quoted 2 lines

Which brings us down to saving a bit of space. And I don't think I see how this saves space?

The space saving results from grouping (many) 1-byte entities together, which (when emitted normally) will generally require padding to 4 or 8 bytes (as being adjacent with other static data in the same or next compilation unit). That padding won't occur if all of the items in a given section are of the same size (and alignment).

Jan

#9 lore raw

I think I now understand this patch. This was far, far too hard!

Quoted 9 lines · On Tue, 28 Feb 2012 08:58:58 +0000 "Jan Beulich" <[email protected]> wrote:
Quoted 9 lines
Quoted 1 line
Quoted 1 line · Andrew Morton
Quoted 5 lines · On Tue, 28 Feb 2012 08:16:40 +0000 "Jan Beulich" <[email protected]> wrote:

Oh, sorry - to carry static data the accesses to which are unlikely (i.e., as in the case given, fully contained in code sections inside conditionals which themselves use unlikely() on their primary/only clause - in other words, something that the compiler really could do on its own).

Quoted 6 lines
Quoted 4 lines

I think I just learned more about this patch than at any time since we started discussing it.

Why add a new section, rather than using __read_mostly?

Because __read_mostly data frequently is hot.

I seem to remember discussing this last time. That I had to ask was a sign that it should have been changelogged!

Quoted 16 lines
Quoted 2 lines

I suppose we should add and use a #define for this, like __read_mostly. That would be a good site for documenting it ;)

That's a possibility of course. I'll do so in an eventual v3.

Quoted 3 lines

And I come back to my old friend printk_once(). If I'm understanding things correctly, we can/should make that test unlikely, then mark __print_once as __this_new_section? Otherwise... help!

No. The variable itself serves for the tested condition here, and hence a priori you can't say whether the whole construct sits in a hot path (whether putting it in a hot path is a good idea is another question, but the print-this-one-time-only nature of it may be the very reason why someone considers this reasonable for his code).

Quoted 5 lines

As opposed to that, the access to the static variable in WARN_ONCE() is *after* an unlikely() condition was already evaluated, i.e. would generally sit on a code path that we hope doesn't get speculated along (often), by means of the compiler suitably arranging branch targets.

OK, so there's a distinction here between storage which is frequently touched and storage which is infrequently touched, because it is inside an unlikely() block.

So the idea behind the patch is to use the "unlikely" as a sign that the data is rarely touched, so we can move it into its own section to prevent it from adding sparseness to data which is more frequently touched.

Correct? If so, that's key: please copy-n-paste this into the changelog.

Quoted 9 lines
Quoted 3 lines

btw, I don't think there's a significant performance benefit here - if the kernel is ever executing WARN_ON_ONCE(), WARN_ONCE() or printk_once() with any frequency then it is already badly broken.

The intended performance benefit isn't with the WARN_ONCE() constructs - we certainly don't care much about them being executed efficiently - but instead with code path accessing other data in the same cache line (or spilled across multiple cache lines just because of the __warned variable sitting in the middle).

Right.

Quoted 9 lines
Quoted 2 lines

Which brings us down to saving a bit of space. And I don't think I see how this saves space?

The space saving results from grouping (many) 1-byte entities together, which (when emitted normally) will generally require padding to 4 or 8 bytes (as being adjacent with other static data in the same or next compilation unit). That padding won't occur if all of the items in a given section are of the same size (and alignment).

OK. So the new section should only be used for static bool (or static char)?

#10 lore raw
Quoted 8 lines
Quoted 1 line
Quoted 1 line · Andrew Morton

So the idea behind the patch is to use the "unlikely" as a sign that the data is rarely touched, so we can move it into its own section to prevent it from adding sparseness to data which is more frequently touched.

Correct? If so, that's key: please copy-n-paste this into the changelog.

Done so for the next revision.

Quoted 9 lines
Quoted 6 lines

The space saving results from grouping (many) 1-byte entities together, which (when emitted normally) will generally require padding to 4 or 8 bytes (as being adjacent with other static data in the same or next compilation unit). That padding won't occur if all of the items in a given section are of the same size (and alignment).

OK. So the new section should only be used for static bool (or static char)?

That's a little too strong, but yes, the change would be less efficient when objects with an alignment requirement of more than a byte would get added to that same section.

Avoiding this is difficult though: gcc doesn't allow section name templates (where e.g. the alignment could be embedded in the section name by the compiler), nor does it allow any other means to generate the section name on the fly in all possible cases (it would only be possible when the object has function scope, as there we could use an asm() for doing this). And requiring to _literally_ (i.e. without any alignof() or sizeof()) specify the respective number would be rather ugly (and calling for mistakes). I can't think of other alternatives.

Jan