Re: [PATCH v6 6/6] locking/lockdep: Reuse freed chain_hlocks entries

From: Waiman Long
Date: Thu Feb 06 2020 - 12:07:03 EST


On 2/6/20 11:03 AM, Peter Zijlstra wrote:
> On Thu, Feb 06, 2020 at 10:24:08AM -0500, Waiman Long wrote:
>> +#define for_each_chain_block(bucket, prev, curr) \
>> + for ((prev) = -1, (curr) = chain_block_buckets[bucket]; \
>> + (curr) >= 0; \
>> + (prev) = (curr), (curr) = chain_block_next(curr))
>> +static inline void add_chain_block(int offset, int size)
>> +{
>> + int bucket = size_to_bucket(size);
>> + int next = chain_block_buckets[bucket];
>> + int prev, curr;
>> +
>> + if (unlikely(size < 2)) {
>> + /*
>> + * We can't store single entries on the freelist. Leak them.
>> + *
>> + * One possible way out would be to uniquely mark them, other
>> + * than with CHAIN_BLK_FLAG, such that we can recover them when
>> + * the block before it is re-added.
>> + */
>> + if (size)
>> + nr_lost_chain_hlocks++;
>> + return;
>> + }
>> +
>> + nr_free_chain_hlocks += size;
>> + if (!bucket) {
>> + nr_large_chain_blocks++;
>> +
>> + if (unlikely(next >= 0)) {
> I was surprised by this condition..

Yes, this condition is optional and the code will still work as expected
without that. I added that so that for the common case where there is
only 1 chain block in block 0 and it gets deleted and added
repetitively, it will go to the simpler code path instead of the more
complicated one.

Cheers,
Longman