All of lore.kernel.org
 help / color / mirror / Atom feed
From: Daniel Thompson <daniel.thompson@linaro.org>
To: Doug Anderson <dianders@chromium.org>
Cc: kgdb-bugreport@lists.sourceforge.net,
	Christophe JAILLET <christophe.jaillet@wanadoo.fr>,
	Jason Wessel <jason.wessel@windriver.com>,
	Thorsten Blum <thorsten.blum@toblux.com>,
	Yuran Pereira <yuran.pereira@hotmail.com>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH 13/13] kdb: Add mdi, mdiW / mdiWcN commands to show iomapped memory
Date: Fri, 21 Jun 2024 16:43:49 +0100	[thread overview]
Message-ID: <20240621154349.GE285771@aspen.lan> (raw)
In-Reply-To: <CAD=FV=VTegKBcHY9pgfFUs7T1Ug5r1yg+CxLE6sBhBBt4csfkw@mail.gmail.com>

On Tue, Jun 18, 2024 at 12:33:05PM -0700, Doug Anderson wrote:
> Hi,
>
> On Tue, Jun 18, 2024 at 8:59 AM Daniel Thompson
> <daniel.thompson@linaro.org> wrote:
> >
> > On Mon, Jun 17, 2024 at 05:34:47PM -0700, Douglas Anderson wrote:
> > > Add commands that are like the other "md" commands but that allow you
> > > to read memory that's in the IO space.
> > >
> > > Signed-off-by: Douglas Anderson <dianders@chromium.org>
> >
> > Sorry to be the bearer of bad news but...
> >
> >
> > > ---
> > > <snip>
> > > +/*
> > > + * kdb_getioword
> > > + * Inputs:
> > > + *   word    Pointer to the word to receive the result.
> > > + *   addr    Address of the area to copy.
> > > + *   size    Size of the area.
> > > + * Returns:
> > > + *   0 for success, < 0 for error.
> > > + */
> > > +int kdb_getioword(unsigned long *word, unsigned long addr, size_t size)
> > > +{
> > > +     void __iomem *mapped = ioremap(addr, size);
> >
> > ioremap() is a might_sleep() function. It's unsafe to call it from the
> > debug trap handler.
>
> Hmmmm. Do you have a pointer to documentation or code showing that
> it's a might_sleep() function. I was worried about this initially but
> I couldn't find any documentation or code indicating it to be so. I
> also got no warnings when I ran my prototype code and then the code
> worked fine, so I assumed that it must somehow work. Sigh...
>
> Looking more closely, maybe this is:
>
> ioremap() -> ioremap_prot() -> generic_ioremap_prot() ->
> __get_vm_area_caller() -> __get_vm_area_node() -> __get_vm_area_node()
>
> ...and that has a kernel allocation with GFP_KERNEL?

To be honest there were a lot of problems, I just simplified.

__get_vm_area_node() already commences with a BUG_ON(in_interrupt())
before it ends up doing a GFP_KERNEL memory allocation.

I think there are multiple calls to might_sleep() (for example from
__get_vm_area_node() -> alloc_vmap_area() ).

However even if we had preallocated some vmap addresses for peek/poke
there are still problems in ioremap_page_range() too. For example:

generic_ioremap_prot()
  -> ioremap_page_range()
    -> find_vm_area()
      -> find_vmap_area()
        -> spin_lock()

We could go further down the rabbit hole and pre-lookup the VM area too
but then we hit.

generic_ioremap_prot()
  -> ioremap_page_range()
    -> vmap_page_range()
      -> vmap_range_noflush()
        -> might_sleep()

It is remotely possible that the only lock vmap_page_range() takes is
init_mm->page_table_lock but I doubt we can be sure of that.


> I guess it also then calls alloc_vmap_area()  which has might_sleep()...
>
> I'll have to track down why no warnings triggered...
>
> > I'm afraid I don't know a safe alternative either. Machinary such as
> > kmap_atomic() needs a page and iomem won't have one.
>
> Ugh. It would be nice to come up with something since it's not
> uncommon to need to look at the state of hardware registers when a
> crash happens. In the past I've managed to get into gdb, track down a
> global variable where someone has already mapped the memory, and then
> use gdb to look at the memory. It's always a big pain, though...
>
> ...even if I could just look up the virtual address where someone else
> had already mapped it that might be enough. At least I wouldn't need
> to go track down the globals myself...
>
> ...anyway, I guess I'll ponder on it and poke if I have time...

I've often thought about implementing longjmp-on-spin-wait for kgdb for
these kind of reasons. For example I have long wanted to be able to let
the user see /proc/interrupts before the usespace comes up but the spin
locks get in the way.

This approach wouldn't make calling ioremap() safe (since we could end
up bailing out halfway through a non-atomic operation) but it could at
least give control back to kdb and let the user know they have ruined
their system.

I know... there is a *reason* I've never quite got round to writing
this!


Daniel.

  reply	other threads:[~2024-06-21 15:43 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-18  0:34 [PATCH 00/13] kdb: Add the ability to read iomapped memory via kdb + clean up "md" commands Douglas Anderson
2024-06-18  0:34 ` [PATCH 01/13] kdb: Get rid of "minlen" for the "md" command Douglas Anderson
2024-06-18  0:34 ` [PATCH 02/13] kdb: Document the various "md" commands better Douglas Anderson
2024-06-18 11:24   ` Daniel Thompson
2024-06-18 14:42     ` Doug Anderson
2024-06-18  0:34 ` [PATCH 03/13] kdb: Use "bool" in "md" implementation where appropriate Douglas Anderson
2024-06-18  0:34 ` [PATCH 04/13] kdb: Drop "offset" and "name" args to kdbgetaddrarg() Douglas Anderson
2024-06-18  0:34 ` [PATCH 05/13] kdb: Separate out "mdr" handling Douglas Anderson
2024-06-18 11:29   ` Daniel Thompson
2024-06-18  0:34 ` [PATCH 06/13] kdb: Remove "mdW" and "mdWcN" handling of "W" == 0 Douglas Anderson
2024-06-18 11:37   ` Daniel Thompson
2024-06-18 14:42     ` Doug Anderson
2024-06-18  0:34 ` [PATCH 07/13] kdb: Tweak "repeat" handling code for "mdW" and "mdWcN" Douglas Anderson
2024-06-18 12:57   ` Daniel Thompson
2024-06-18 14:43     ` Doug Anderson
2024-06-18  0:34 ` [PATCH 08/13] kdb: In kdb_md() make `repeat` and `mdcount` calculations more obvious Douglas Anderson
2024-06-18  0:34 ` [PATCH 09/13] kdb: Use 'unsigned int' in kdb_md() where appropriate Douglas Anderson
2024-06-18 15:26   ` Daniel Thompson
2024-06-18  0:34 ` [PATCH 10/13] kdb: Replease simple_strtoul() with kstrtouint() in kdb_md() Douglas Anderson
2024-06-18  0:34 ` [PATCH 11/13] kdb: Abstract out parsing for mdWcN Douglas Anderson
2024-06-18 21:02   ` kernel test robot
2024-06-18  0:34 ` [PATCH 12/13] kdb: Add mdpW / mdpWcN commands Douglas Anderson
2024-06-18  0:34 ` [PATCH 13/13] kdb: Add mdi, mdiW / mdiWcN commands to show iomapped memory Douglas Anderson
2024-06-18 15:59   ` Daniel Thompson
2024-06-18 19:33     ` Doug Anderson
2024-06-21 15:43       ` Daniel Thompson [this message]
2024-06-21 19:52         ` Doug Anderson

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=20240621154349.GE285771@aspen.lan \
    --to=daniel.thompson@linaro.org \
    --cc=christophe.jaillet@wanadoo.fr \
    --cc=dianders@chromium.org \
    --cc=jason.wessel@windriver.com \
    --cc=kgdb-bugreport@lists.sourceforge.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=thorsten.blum@toblux.com \
    --cc=yuran.pereira@hotmail.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.