mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Waiman Long <longman@redhat.com>
To: Naveen Kumar Chaudhary <naveen.osdev@gmail.com>,
	peterz@infradead.org, mingo@redhat.com, will@kernel.org,
	boqun@kernel.org
Cc: linux-kernel@vger.kernel.org
Subject: Re: [PATCH] locking/lockdep: Validate class index in lock_chain_get_class()
Date: Wed, 17 Jun 2026 13:26:49 -0400	[thread overview]
Message-ID: <d93e0933-d9ad-46b4-a786-aa46098f7ebd@redhat.com> (raw)
In-Reply-To: <hgkp7qdqmyhp7z2mwci6q3m2gevec3mdparwquasyoqhkqm4mu@e2ig6ill3us5>

On 6/16/26 12:40 PM, Naveen Kumar Chaudhary wrote:
> lock_chain_get_class() extracts a class index from chain_hlocks[] and
> uses it to index directly into lock_classes[] without any validation.
> If the chain data references a class that has been zapped (e.g., after
> module unload), the class_idx bit will be cleared in lock_classes_in_use
> and the function returns a pointer to a stale entry.
>
> Add a DEBUG_LOCKS_WARN_ON() check using test_bit() on lock_classes_in_use.
> Return NULL on failure so callers can handle it gracefully.
>
> Update the sole caller in lockdep_proc.c to handle the NULL return.
>
> Signed-off-by: Naveen Kumar Chaudhary <naveen.osdev@gmail.com>
> ---
>   kernel/locking/lockdep.c      | 3 +++
>   kernel/locking/lockdep_proc.c | 2 +-
>   2 files changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
> index 2d4c5bab5af8..ca255269b714 100644
> --- a/kernel/locking/lockdep.c
> +++ b/kernel/locking/lockdep.c
> @@ -3561,6 +3561,9 @@ struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
>   	u16 chain_hlock = chain_hlocks[chain->base + i];
>   	unsigned int class_idx = chain_hlock_class_idx(chain_hlock);
>   
> +	if (DEBUG_LOCKS_WARN_ON(!test_bit(class_idx, lock_classes_in_use)))
> +		return NULL;
> +
>   	return lock_classes + class_idx;
>   }
>   
> diff --git a/kernel/locking/lockdep_proc.c b/kernel/locking/lockdep_proc.c
> index 1916db9aa46b..2d1f6b43edd5 100644
> --- a/kernel/locking/lockdep_proc.c
> +++ b/kernel/locking/lockdep_proc.c
> @@ -170,7 +170,7 @@ static int lc_show(struct seq_file *m, void *v)
>   
>   	for (i = 0; i < chain->depth; i++) {
>   		class = lock_chain_get_class(chain, i);
> -		if (!class->key)
> +		if (!class || !class->key)
>   			continue;
>   
>   		seq_printf(m, "[%p] ", class->key);

When a class is zapped, that particular class should have been removed 
from the lock chains. Have you hit any cases where this warning can be 
triggered? Or is this just for making sure that this condition won't be 
triggered silently if it ever happens?

Regards,
Longman


  reply	other threads:[~2026-06-17 17:26 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-16 16:40 Naveen Kumar Chaudhary
2026-06-17 17:26 ` Waiman Long [this message]
2026-06-20  3:43   ` Naveen Kumar Chaudhary

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d93e0933-d9ad-46b4-a786-aa46098f7ebd@redhat.com \
    --to=longman@redhat.com \
    --cc=boqun@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=naveen.osdev@gmail.com \
    --cc=peterz@infradead.org \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®