linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Catalin Marinas <catalin.marinas@arm.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Waiman Long <longman@redhat.com>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	Audra Mitchell <aubaker@redhat.com>
Subject: Re: [PATCH v2] mm/kmemleak: Don't hold kmemleak_lock when calling printk()
Date: Wed, 27 Mar 2024 17:43:08 +0000	[thread overview]
Message-ID: <ZgRarOvI3Zhos9Gl@arm.com> (raw)
In-Reply-To: <20240307114630.32702099ac24c182b91da517@linux-foundation.org>

On Thu, Mar 07, 2024 at 11:46:30AM -0800, Andrew Morton wrote:
> On Thu,  7 Mar 2024 13:47:07 -0500 Waiman Long <longman@redhat.com> wrote:
> > When some error conditions happen (like OOM), some kmemleak functions
> > call printk() to dump out some useful debugging information while holding
> > the kmemleak_lock. This may cause deadlock as the printk() function
> > may need to allocate additional memory leading to a create_object()
> > call acquiring kmemleak_lock again.
> > 
> > An abbreviated lockdep splat is as follows:
> >
> > ...
> > 
> > Fix this deadlock issue by making sure that printk() is only called
> > after releasing the kmemleak_lock.
> > 
> > ...
> >
> > @@ -427,9 +442,19 @@ static struct kmemleak_object *__lookup_object(unsigned long ptr, int alias,
> >  		else if (untagged_objp == untagged_ptr || alias)
> >  			return object;
> >  		else {
> > +			if (!get_object(object))
> > +				break;
> > +			/*
> > +			 * Release kmemleak_lock temporarily to avoid deadlock
> > +			 * in printk(). dump_object_info() is called without
> > +			 * holding object->lock (race unlikely).
> > +			 */
> > +			raw_spin_unlock(&kmemleak_lock);
> >  			kmemleak_warn("Found object by alias at 0x%08lx\n",
> >  				      ptr);
> >  			dump_object_info(object);
> > +			put_object(object);
> > +			raw_spin_lock(&kmemleak_lock);
> >  			break;
> 
> Please include a full description of why this is safe.  Once we've
> dropped that lock, the tree is in an unknown state and we shouldn't
> touch it again.  This consideration should be added to the relevant
> functions' interface documentation and the code should be reviewed to
> ensure that we're actually adhering to this.  Or something like that.
> 
> To simply drop and reacquire a lock without supporting analysis and
> comments does not inspire confidence!

I agree it looks fragile. I think it works, the code tends to bail out
on those errors and doesn't expect the protected data to have remained
intact. But we may change it in the future and forgot about this.

I wonder whether we can actually make things slightly easier to reason
about, defer the printing until unlock, store the details in some
per-cpu variable. Another option would be to have a per-CPU array to
store potential recursive kmemleak_*() callbacks during the critical
regions. This should be bounded since the interrupts are disabled. On
unlock, we'd replay the array and add those pointers.

-- 
Catalin


  reply	other threads:[~2024-03-27 17:43 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-07 18:47 [PATCH v2] mm/kmemleak: Don't hold kmemleak_lock when calling printk() Waiman Long
2024-03-07 18:49 ` Waiman Long
2024-03-07 19:46 ` Andrew Morton
2024-03-27 17:43   ` Catalin Marinas [this message]
2024-03-27 18:58     ` Waiman Long

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=ZgRarOvI3Zhos9Gl@arm.com \
    --to=catalin.marinas@arm.com \
    --cc=akpm@linux-foundation.org \
    --cc=aubaker@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=longman@redhat.com \
    /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;
as well as URLs for NNTP newsgroup(s).