Skip to content

Commit de4643a

Browse files
bvanasscheingomolnar
authored andcommitted
locking/lockdep: Reuse lock chains that have been freed
A previous patch introduced a lock chain leak. Fix that leak by reusing lock chains that have been freed. Signed-off-by: Bart Van Assche <bvanassche@acm.org> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Johannes Berg <johannes@sipsolutions.net> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Waiman Long <longman@redhat.com> Cc: Will Deacon <will.deacon@arm.com> Cc: johannes.berg@intel.com Cc: tj@kernel.org Link: https://lkml.kernel.org/r/20190214230058.196511-16-bvanassche@acm.org Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 527af3e commit de4643a

1 file changed

Lines changed: 37 additions & 20 deletions

File tree

‎kernel/locking/lockdep.c‎

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,12 @@ static LIST_HEAD(free_lock_classes);
292292
/**
293293
* struct pending_free - information about data structures about to be freed
294294
* @zapped: Head of a list with struct lock_class elements.
295+
* @lock_chains_being_freed: Bitmap that indicates which lock_chains[] elements
296+
* are about to be freed.
295297
*/
296298
struct pending_free {
297299
struct list_head zapped;
300+
DECLARE_BITMAP(lock_chains_being_freed, MAX_LOCKDEP_CHAINS);
298301
};
299302

300303
/**
@@ -2096,8 +2099,8 @@ check_prevs_add(struct task_struct *curr, struct held_lock *next)
20962099
return 0;
20972100
}
20982101

2099-
static unsigned long nr_lock_chains;
21002102
struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
2103+
static DECLARE_BITMAP(lock_chains_in_use, MAX_LOCKDEP_CHAINS);
21012104
int nr_chain_hlocks;
21022105
static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
21032106

@@ -2236,12 +2239,25 @@ static int check_no_collision(struct task_struct *curr,
22362239
*/
22372240
long lockdep_next_lockchain(long i)
22382241
{
2239-
return i + 1 < nr_lock_chains ? i + 1 : -2;
2242+
i = find_next_bit(lock_chains_in_use, ARRAY_SIZE(lock_chains), i + 1);
2243+
return i < ARRAY_SIZE(lock_chains) ? i : -2;
22402244
}
22412245

22422246
unsigned long lock_chain_count(void)
22432247
{
2244-
return nr_lock_chains;
2248+
return bitmap_weight(lock_chains_in_use, ARRAY_SIZE(lock_chains));
2249+
}
2250+
2251+
/* Must be called with the graph lock held. */
2252+
static struct lock_chain *alloc_lock_chain(void)
2253+
{
2254+
int idx = find_first_zero_bit(lock_chains_in_use,
2255+
ARRAY_SIZE(lock_chains));
2256+
2257+
if (unlikely(idx >= ARRAY_SIZE(lock_chains)))
2258+
return NULL;
2259+
__set_bit(idx, lock_chains_in_use);
2260+
return lock_chains + idx;
22452261
}
22462262

22472263
/*
@@ -2260,11 +2276,6 @@ static inline int add_chain_cache(struct task_struct *curr,
22602276
struct lock_chain *chain;
22612277
int i, j;
22622278

2263-
/*
2264-
* Allocate a new chain entry from the static array, and add
2265-
* it to the hash:
2266-
*/
2267-
22682279
/*
22692280
* The caller must hold the graph lock, ensure we've got IRQs
22702281
* disabled to make this an IRQ-safe lock.. for recursion reasons
@@ -2273,15 +2284,15 @@ static inline int add_chain_cache(struct task_struct *curr,
22732284
if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
22742285
return 0;
22752286

2276-
if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
2287+
chain = alloc_lock_chain();
2288+
if (!chain) {
22772289
if (!debug_locks_off_graph_unlock())
22782290
return 0;
22792291

22802292
print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
22812293
dump_stack();
22822294
return 0;
22832295
}
2284-
chain = lock_chains + nr_lock_chains++;
22852296
chain->chain_key = chain_key;
22862297
chain->irq_context = hlock->irq_context;
22872298
i = get_first_held_lock(curr, hlock);
@@ -4208,7 +4219,8 @@ void lockdep_reset(void)
42084219
}
42094220

42104221
/* Remove a class from a lock chain. Must be called with the graph lock held. */
4211-
static void remove_class_from_lock_chain(struct lock_chain *chain,
4222+
static void remove_class_from_lock_chain(struct pending_free *pf,
4223+
struct lock_chain *chain,
42124224
struct lock_class *class)
42134225
{
42144226
#ifdef CONFIG_PROVE_LOCKING
@@ -4246,6 +4258,7 @@ static void remove_class_from_lock_chain(struct lock_chain *chain,
42464258
* hlist_for_each_entry_rcu() loop is safe.
42474259
*/
42484260
hlist_del_rcu(&chain->entry);
4261+
__set_bit(chain - lock_chains, pf->lock_chains_being_freed);
42494262
if (chain->depth == 0)
42504263
return;
42514264
/*
@@ -4254,22 +4267,19 @@ static void remove_class_from_lock_chain(struct lock_chain *chain,
42544267
*/
42554268
if (lookup_chain_cache(chain_key))
42564269
return;
4257-
if (WARN_ON_ONCE(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
4270+
new_chain = alloc_lock_chain();
4271+
if (WARN_ON_ONCE(!new_chain)) {
42584272
debug_locks_off();
42594273
return;
42604274
}
4261-
/*
4262-
* Leak *chain because it is not safe to reinsert it before an RCU
4263-
* grace period has expired.
4264-
*/
4265-
new_chain = lock_chains + nr_lock_chains++;
42664275
*new_chain = *chain;
42674276
hlist_add_head_rcu(&new_chain->entry, chainhashentry(chain_key));
42684277
#endif
42694278
}
42704279

42714280
/* Must be called with the graph lock held. */
4272-
static void remove_class_from_lock_chains(struct lock_class *class)
4281+
static void remove_class_from_lock_chains(struct pending_free *pf,
4282+
struct lock_class *class)
42734283
{
42744284
struct lock_chain *chain;
42754285
struct hlist_head *head;
@@ -4278,7 +4288,7 @@ static void remove_class_from_lock_chains(struct lock_class *class)
42784288
for (i = 0; i < ARRAY_SIZE(chainhash_table); i++) {
42794289
head = chainhash_table + i;
42804290
hlist_for_each_entry_rcu(chain, head, entry) {
4281-
remove_class_from_lock_chain(chain, class);
4291+
remove_class_from_lock_chain(pf, chain, class);
42824292
}
42834293
}
42844294
}
@@ -4317,7 +4327,7 @@ static void zap_class(struct pending_free *pf, struct lock_class *class)
43174327
class->name);
43184328
}
43194329

4320-
remove_class_from_lock_chains(class);
4330+
remove_class_from_lock_chains(pf, class);
43214331
}
43224332

43234333
static void reinit_class(struct lock_class *class)
@@ -4383,6 +4393,12 @@ static void __free_zapped_classes(struct pending_free *pf)
43834393
reinit_class(class);
43844394

43854395
list_splice_init(&pf->zapped, &free_lock_classes);
4396+
4397+
#ifdef CONFIG_PROVE_LOCKING
4398+
bitmap_andnot(lock_chains_in_use, lock_chains_in_use,
4399+
pf->lock_chains_being_freed, ARRAY_SIZE(lock_chains));
4400+
bitmap_clear(pf->lock_chains_being_freed, 0, ARRAY_SIZE(lock_chains));
4401+
#endif
43864402
}
43874403

43884404
static void free_zapped_rcu(struct rcu_head *ch)
@@ -4623,6 +4639,7 @@ void __init lockdep_init(void)
46234639
#ifdef CONFIG_PROVE_LOCKING
46244640
+ sizeof(lock_cq)
46254641
+ sizeof(lock_chains)
4642+
+ sizeof(lock_chains_in_use)
46264643
+ sizeof(chain_hlocks)
46274644
#endif
46284645
) / 1024

0 commit comments

Comments
 (0)