Skip to contentSkip to conversation

Fixes for liblockdep

PATCH In review Ben Hutchings 3 participants 11 messages Jun 14, 2016 Stable branch

lore
Share & tools
lore
#0 lore raw

cover Fixes for liblockdep

Here are a number of fixes for liblockdep. The first three need to go into 4.7 and 4.6-stable; the second should probably go to all stable branches.

Ben.

Ben Hutchings (7):
liblockdep: Fix undefined symbol prandom_u32
liblockdep: Reduce MAX_LOCK_DEPTH to avoid overflowing
lock_chain::depth
liblockdep: Define the ARRAY_SIZE() macro
liblockdep: Enable -Wall by default
liblockdep: Fix 'unused value' warnings
liblockdep: Fix 'set but not used' warnings
liblockdep: Fix 'defined but not used' warning for init_utsname()

 tools/lib/lockdep/Makefile                     |  1 +
 tools/lib/lockdep/common.c                     |  6 ++++++
 tools/lib/lockdep/lockdep.c                    | 10 ++++++++++
 tools/lib/lockdep/uinclude/linux/debug_locks.h |  2 +-
 tools/lib/lockdep/uinclude/linux/irqflags.h    |  8 ++++----
 tools/lib/lockdep/uinclude/linux/kernel.h      | 14 +++++++++++---
 tools/lib/lockdep/uinclude/linux/lockdep.h     | 18 ++++++------------
 7 files changed, 39 insertions(+), 20 deletions(-)
#1 lore raw

PATCH 1/7 liblockdep: Fix undefined symbol prandom_u32

__lock_pin_lock() now calls prandom_u32() which is not defined in liblockdep. __lock_pin_lock() and its caller lock_pin_lock() are dead code in liblockdep, but we still need to provide a definition of prandom_u32() in case lazy binding is disabled.

Fixes e7904a28f533 ("locking/lockdep, sched/core: Implement a better ...")
---
 tools/lib/lockdep/common.c | 6 ++++++
 1 file changed, 6 insertions(+)
tools/lib/lockdep/common.c +6 -0
index d1c89cc06f5f..405c17667c4d 100644
@@ -1,5 +1,6 @@
 #include <stddef.h>
 #include <stdbool.h>
+#include <stdlib.h>
 #include <linux/compiler.h>
 #include <linux/lockdep.h>
 #include <unistd.h>
@@ -10,6 +11,11 @@ static __thread struct task_struct current_obj;
 /* lockdep wants these */
 bool debug_locks = true;
 bool debug_locks_silent;
+u32 prandom_u32(void)
+{
+	/* Used only by lock_pin_lock() which is dead code */
+	abort();
+}
 
 __attribute__((destructor)) static void liblockdep_exit(void)
 {
#3 lore raw

PATCH 2/7 liblockdep: Reduce MAX_LOCK_DEPTH to avoid overflowing lock_chain::depth

In reply to Ben Hutchings

liblockdep has been broken since commit 75dd602a5198 ("lockdep: Fix lock_chain::base size"), as that adds a check that MAX_LOCK_DEPTH is within the range of lock_chain::depth and in liblockdep it is much too large.

That should have resulted in a compiler error, but didn't because:

- the check uses ARRAY_SIZE(), which isn't yet defined in liblockdep so is assumed to be an (undeclared) function
- putting a function call inside a BUILD_BUG_ON() expression quietly turns it into some nonsense involving a variable-length array

It did produce a compiler warning, but I didn't notice because liblockdep already produces too many warnings if -Wall is enabled (which I'll fix shortly).

Even before that commit, which reduced lock_chain::depth from 8 bits to 6, MAX_LOCK_DEPTH was too large.

Cc <[email protected]> # for versions before 4.6, use a value of 255
---
 tools/lib/lockdep/uinclude/linux/lockdep.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/lockdep/uinclude/linux/lockdep.h +1 -1
index c808c7d02d21..d30214221920 100644
@@ -8,7 +8,7 @@
 #include <linux/utsname.h>
 #include <linux/compiler.h>
 
-#define MAX_LOCK_DEPTH 2000UL
+#define MAX_LOCK_DEPTH 63UL
 
 #define asmlinkage
 #define __visible
#5 lore raw

PATCH 3/7 liblockdep: Define the ARRAY_SIZE() macro

In reply to Ben Hutchings

lockdep.c now uses ARRAY_SIZE().

Fixes 75dd602a5198 ("lockdep: Fix lock_chain::base size")
---
 tools/lib/lockdep/uinclude/linux/kernel.h | 2 ++
 1 file changed, 2 insertions(+)
tools/lib/lockdep/uinclude/linux/kernel.h +2 -0
index 276c7a8b2ed1..da87bd9ad2c1 100644
@@ -7,6 +7,8 @@
 #include <linux/hardirq.h>
 #include <linux/kern_levels.h>
 
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+
 #ifndef container_of
 #define container_of(ptr, type, member) ({			\
 	const typeof(((type *)0)->member) * __mptr = (ptr);	\
#6 lore raw

PATCH 4/7 liblockdep: Enable -Wall by default

In reply to Ben Hutchings

Regressions in liblockdep may be missed because it doesn't enable warnings.

Adding -Wall immediately introduces a lot of warnings, but those will be fixed by the following commits.

---
 tools/lib/lockdep/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/lib/lockdep/Makefile +1 -0
index 1d57af56814b..710a0edfe1b1 100644
@@ -79,6 +79,7 @@ INCLUDES = -I. -I./uinclude -I./include -I../../include $(CONFIG_INCLUDES)
 # Set compile option CFLAGS if not set elsewhere
 CFLAGS ?= -g -DCONFIG_LOCKDEP -DCONFIG_STACKTRACE -DCONFIG_PROVE_LOCKING -DBITS_PER_LONG=__WORDSIZE -DLIBLOCKDEP_VERSION='"$(LIBLOCKDEP_VERSION)"' -rdynamic -O0 -g
 CFLAGS += -fPIC
+CFLAGS += -Wall
 
 override CFLAGS += $(CONFIG_FLAGS) $(INCLUDES) $(PLUGIN_DIR_SQ)
 
#7 lore raw

PATCH 5/7 liblockdep: Fix 'unused value' warnings

In reply to Ben Hutchings

liblockdep defines various macros that may expand to an expression with no effect, while the in-kernel definition does have an effect. This results in warnings from gcc when -Wunused-value is enabled, and is is enabled by -Wall. Fix this by introducing trivial functions, as function return values are generally allowed to be ignored.

---
 tools/lib/lockdep/uinclude/linux/debug_locks.h |  2 +-
 tools/lib/lockdep/uinclude/linux/kernel.h      | 12 +++++++++---
 tools/lib/lockdep/uinclude/linux/lockdep.h     |  6 +++++-
 3 files changed, 15 insertions(+), 5 deletions(-)
tools/lib/lockdep/uinclude/linux/debug_locks.h +1 -1
index f38eb64df794..1d4fbec5c649 100644
@@ -4,7 +4,7 @@
 #include <stddef.h>
 #include <linux/compiler.h>
 
-#define DEBUG_LOCKS_WARN_ON(x) (x)
+#define DEBUG_LOCKS_WARN_ON(x) WARN_ON(x)
 
 extern bool debug_locks;
 extern bool debug_locks_silent;
tools/lib/lockdep/uinclude/linux/kernel.h +9 -3
index da87bd9ad2c1..021cff4f4e3d 100644
@@ -22,10 +22,16 @@
 	_max1 > _max2 ? _max1 : _max2; })
 
 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
-#define WARN_ON(x) (x)
-#define WARN_ON_ONCE(x) (x)
+
+static inline int lockdep_warn(int condition)
+{
+	return condition;
+}
+#define WARN_ON(x) lockdep_warn(x)
+#define WARN_ON_ONCE(x) WARN_ON(x)
+#define WARN(x, y...) WARN_ON(x)
+
 #define likely(x) (x)
-#define WARN(x, y...) (x)
 #define uninitialized_var(x) x
 #define __init
 #define noinline
tools/lib/lockdep/uinclude/linux/lockdep.h +5 -1
index d30214221920..d1079034a14d 100644
@@ -29,7 +29,11 @@ extern struct task_struct *__curr(void);
 
 #define current (__curr())
 
-#define debug_locks_off() 1
+static inline int debug_locks_off(void)
+{
+	return 1;
+}
+
 #define task_pid_nr(tsk) ((tsk)->pid)
 
 #define KSYM_NAME_LEN 128
#8 lore raw

PATCH 6/7 liblockdep: Fix 'set but not used' warnings

In reply to Ben Hutchings

liblockdep defines trivial macros for working with interrupt flags, as interrupts are never disabled in userland. This results in warnings from gcc when -Wunused-but-set-variable is enabled, and it is enabled by -Wall. Fix this by evaluating the flags parameter and casting it to void.

---
 tools/lib/lockdep/uinclude/linux/irqflags.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
tools/lib/lockdep/uinclude/linux/irqflags.h +4 -4
index 6cc296f0fad0..df77669cfe1c 100644
@@ -17,19 +17,19 @@
 #define raw_local_irq_disable() do { } while (0)
 #define raw_local_irq_enable() do { } while (0)
 #define raw_local_irq_save(flags) ((flags) = 0)
-#define raw_local_irq_restore(flags) do { } while (0)
+#define raw_local_irq_restore(flags) ((void)(flags))
 #define raw_local_save_flags(flags) ((flags) = 0)
-#define raw_irqs_disabled_flags(flags) do { } while (0)
+#define raw_irqs_disabled_flags(flags) ((void)(flags))
 #define raw_irqs_disabled() 0
 #define raw_safe_halt()
 
 #define local_irq_enable() do { } while (0)
 #define local_irq_disable() do { } while (0)
 #define local_irq_save(flags) ((flags) = 0)
-#define local_irq_restore(flags) do { } while (0)
+#define local_irq_restore(flags) ((void)(flags))
 #define local_save_flags(flags)	((flags) = 0)
 #define irqs_disabled() (1)
-#define irqs_disabled_flags(flags) (0)
+#define irqs_disabled_flags(flags) ((void)(flags), 0)
 #define safe_halt() do { } while (0)
 
 #define trace_lock_release(x, y)
#9 lore raw

PATCH 7/7 liblockdep: Fix 'defined but not used' warning for init_utsname()

In reply to Ben Hutchings

We define init_utsname() as static but not inline, resulting in a warning for every source file that includes lockdep.h but doesn't call it.

Since it is only used by lockdep.c, define it in there.

---
 tools/lib/lockdep/lockdep.c                | 10 ++++++++++
 tools/lib/lockdep/uinclude/linux/lockdep.h | 10 ----------
 2 files changed, 10 insertions(+), 10 deletions(-)
tools/lib/lockdep/lockdep.c +10 -0
index a0a2e3a266af..513140ea8a58 100644
@@ -5,4 +5,14 @@
 #define hlist_add_head_rcu		hlist_add_head
 #define hlist_del_rcu			hlist_del
 
+static struct new_utsname *init_utsname(void)
+{
+	static struct new_utsname n = (struct new_utsname) {
+		.release = "liblockdep",
+		.version = LIBLOCKDEP_VERSION,
+	};
+
+	return &n;
+}
+
 #include "../../../kernel/locking/lockdep.c"
tools/lib/lockdep/uinclude/linux/lockdep.h +0 -10
index d1079034a14d..c157242e0417 100644
@@ -44,16 +44,6 @@ static inline int debug_locks_off(void)
 #define atomic_t unsigned long
 #define atomic_inc(x) ((*(x))++)
 
-static struct new_utsname *init_utsname(void)
-{
-	static struct new_utsname n = (struct new_utsname) {
-		.release = "liblockdep",
-		.version = LIBLOCKDEP_VERSION,
-	};
-
-	return &n;
-}
-
 #define print_tainted() ""
 #define static_obj(x) 1
 
#4 lore raw

Re: [PATCH 2/7] liblockdep: Reduce MAX_LOCK_DEPTH to avoid overflowing lock_chain::depth

In reply to Ben Hutchings

tools/lib/lockdep/uinclude/linux/lockdep.h line 11

Quoted 5 lines · Ben Hutchings

Even before that commit, which reduced lock_chain::depth from 8 bits to 6, MAX_LOCK_DEPTH was too large.

tools/lib/lockdep/uinclude/linux/lockdep.h +1 -1
-#define MAX_LOCK_DEPTH 2000UL
+#define MAX_LOCK_DEPTH 63UL

So per that commit; there still is a 4 byte hole we could fill. So if a bigger number is desired here, there is room to make that happen.

Good to see those assertions did their job, albeit somewhat belated.

#2 lore raw

Re: [PATCH 1/7] liblockdep: Fix undefined symbol prandom_u32

In reply to Ben Hutchings
Quoted 23 lines · Ben Hutchings

__lock_pin_lock() now calls prandom_u32() which is not defined in liblockdep. __lock_pin_lock() and its caller lock_pin_lock() are dead code in liblockdep, but we still need to provide a definition of prandom_u32() in case lazy binding is disabled.

Fixes e7904a28f533 ("locking/lockdep, sched/core: Implement a better ...")
---
 tools/lib/lockdep/common.c | 6 ++++++
 1 file changed, 6 insertions(+)
tools/lib/lockdep/common.c +1 -0
index d1c89cc06f5f..405c17667c4d 100644
@@ -1,5 +1,6 @@
 #include <stddef.h>
 #include <stdbool.h>
+#include <stdlib.h>
 #include <linux/compiler.h>
 #include <linux/lockdep.h>
 #include <unistd.h>

tools/lib/lockdep/common.c static __thread struct task_struct current_obj; · line 18

Quoted 8 lines
tools/lib/lockdep/common.c +5 -0
@@ -10,6 +11,11 @@ static __thread struct task_struct current_obj;
 /* lockdep wants these */
 bool debug_locks = true;
 bool debug_locks_silent;
+u32 prandom_u32(void)
+{
+	/* Used only by lock_pin_lock() which is dead code */
+	abort();
+}

I had to place this bit in lockdep.c rather than common.c, since lockdep.c is the one building kernel/lockdep.c.

Thanks,
Sasha

#10 lore raw
In reply to Ben Hutchings
Quoted 3 lines · Ben Hutchings

Here are a number of fixes for liblockdep. The first three need to go into 4.7 and 4.6-stable; the second should probably go to all stable branches.

Thanks Ben! I've added it all to the queue and will send it along.

Thanks,
Sasha