All of lore.kernel.org
 help / color / mirror / Atom feed
From: dmukhin@xen.org
To: Mykola Kvach <xakep.amatop@gmail.com>
Cc: xen-devel@lists.xenproject.org, andrew.cooper3@citrix.com,
	anthony.perard@vates.tech, jbeulich@suse.com, julien@xen.org,
	michal.orzel@amd.com, roger.pau@citrix.com,
	sstabellini@kernel.org, dmukhin@ford.com
Subject: Re: [PATCH v6 01/15] emul/vuart: introduce framework for UART emulators
Date: Mon, 8 Sep 2025 00:16:18 -0700	[thread overview]
Message-ID: <aL6CwuSme+yyOY0e@kraken> (raw)
In-Reply-To: <CAGeoDV8T8UN7uNXZ9Co0he=B1Bt_gXBWAFDPtiE0jvCGb=MA-g@mail.gmail.com>

On Sat, Sep 06, 2025 at 12:43:01PM +0300, Mykola Kvach wrote:
> Hi Denis,
> 
> On Sat, Sep 6, 2025 at 2:27 AM <dmukhin@xen.org> wrote:
[..]
> > +
> > +const static struct vuart *
> > +vuart_find_by_console_permission(const struct domain *d)
> 
> Other functions that search for a console (e.g., by compatible string or IO
> range) take an argument to specify what to search by. Here,
> vuart_find_by_console_permission takes no argument and just checks a single
> flag. It might be clearer to either add a flags argument to make it general,
> or rename the function to reflect that it checks only this one permission flag.

Agreed, will update.
Thanks for the suggestion.

> 
> > +{
> > +    const struct vuart *vuart = d->console.vuart;
> > +
> > +    if ( !vuart || !vuart->emulator || !vuart->emulator->put_rx ||
> 
> Looking at vuart_init, vuart->emulator is always set when vuart is valid.
> So the vuart->emulator check seems redundant.

Ack.

> 
> If it is truly needed, the same check should also appear in
> vuart_dump_state and vuart_deinit. Otherwise, for consistency we
> could safely assume vuart->emulator is non-NULL after vuart_init.

Agreed, will update.

> 
> > +         !(vuart->flags & VUART_CONSOLE_INPUT))
> > +        return NULL;
> > +
> > +    return vuart;
> > +}
> > +
> > +struct vuart *vuart_find_by_io_range(struct domain *d, unsigned long addr,
> > +                                     unsigned long size)
> > +{
> > +    struct vuart *vuart = d->console.vuart;
> > +
> > +    if ( !vuart || !vuart->info )
> 
> Is it possible to have a valid vuart pointer without a valid info pointer?

Yes, the vuart->info check is redundant, will drop.

> 
> > +        return NULL;
> > +
> > +    if ( addr >= vuart->info->base_addr &&
> > +         addr + size - 1 <= vuart->info->base_addr + vuart->info->size - 1 )
> > +        return vuart;
> > +
> > +    return NULL;
> > +}
> > +
> > +int vuart_init(struct domain *d, struct vuart_info *info)
> > +{
> > +    const struct vuart_emulator *emulator;
> > +    struct vuart *vuart;
> > +    int rc;
> > +
> > +    if ( d->console.vuart )
> > +        return -EBUSY;
> > +
> > +    emulator = vuart_match_by_compatible(d, info->compatible);
> > +    if ( !emulator )
> > +        return -ENODEV;
> > +
> > +    vuart = xzalloc(typeof(*vuart));
> > +    if ( !vuart )
> > +        return -ENOMEM;
> > +
> > +    vuart->info = xvzalloc(typeof(*info));
> > +    if ( !vuart->info )
> > +    {
> > +        rc = -ENOMEM;
> > +        goto err_out;
> > +    }
> > +    memcpy(vuart->info, info, sizeof(*info));
> > +
> > +    vuart->vdev = emulator->alloc(d, vuart->info);
> > +    if ( IS_ERR(vuart->vdev) )
> > +    {
> > +        rc = PTR_ERR(vuart->vdev);
> > +        goto err_out;
> > +    }
> > +
> > +    vuart->emulator = emulator;
> > +    vuart->owner = d;
> > +    vuart->flags |= VUART_CONSOLE_INPUT;
> > +
> > +    d->console.input_allowed = true;
> > +    d->console.vuart = vuart;
> > +
> > +    return 0;
> > +
> > + err_out:
> > +    if ( vuart )
> 
> As far as I can see, it isn’t possible to reach this point when vuart
> is NULL. The err_out label is only jumped to after vuart has been
> successfully allocated, so the check if (vuart) is redundant.

Right, thanks.

> 
> > +        xvfree(vuart->info);
> > +    xvfree(vuart);
> > +
> > +    return rc;
> > +}
> > +
> > +/*
> > + * Release any resources taken by UART emulators.
> > + *
> > + * NB: no flags are cleared, since currently exit() is called only during
> > + * domain destroy.
> > + */
> > +void vuart_deinit(struct domain *d)
> > +{
> > +    struct vuart *vuart = d->console.vuart;
> > +
> > +    if ( vuart )
> > +    {
> > +        vuart->emulator->free(vuart->vdev);
> > +        xvfree(vuart->info);
> > +    }
> > +    XVFREE(d->console.vuart);
> > +}
> > +
> > +void vuart_dump_state(const struct domain *d)
> > +{
> > +    struct vuart *vuart = d->console.vuart;
> > +
> > +    if ( vuart )
> > +        vuart->emulator->dump_state(vuart->vdev);
> > +}
> > +
> > +/*
> > + * Put character to the *first* suitable emulated UART's FIFO.
> > + */
> 
> This comment could be a single line since it doesn’t exceed 80 characters.

I will update the comment.

> 
> > +int vuart_put_rx(struct domain *d, char c)
> > +{
> > +    const struct vuart *vuart = vuart_find_by_console_permission(d);
> 
> If vuart_deinit has already been called, is it possible that vuart
> points to freed memory here or in other places?
> 
> Should we add reference counting or locks to protect against such
> use-after-free, or are we relying on higher-level mechanisms to
> guarantee that these structs aren’t freed while vuart is accessed?

That should be covered with rcu_{un,}lock_domain() calls.

But a dedicated vUART lock will be needed in the future series (vpl011 and
hwdom vuart plumbing into the new framework).

> 
> Should we also check whether the domain is currently being
> destroyed and avoid putting new characters into the emulated UART
> in that case?

There's only one callsite currently (in the console driver) and it is
guaranteed that domain will not be destroyed during the call.

> 
> If we are relying on some upper-level mechanism, I think it deserves a
> comment somewhere to make that guarantee explicit.

Agree, will add some explanations.

Thanks!


  reply	other threads:[~2025-09-08  7:16 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-05 23:26 [PATCH v6 00/15] x86: introduce NS16550-compatible UART emulator dmukhin
2025-09-05 23:27 ` [PATCH v6 01/15] emul/vuart: introduce framework for UART emulators dmukhin
2025-09-06  2:02   ` Stefano Stabellini
2025-09-06  9:43   ` Mykola Kvach
2025-09-08  7:16     ` dmukhin [this message]
2025-09-08  8:29   ` Mykola Kvach
2025-09-08  8:43     ` Jan Beulich
2025-09-08  9:45     ` dmukhin
2025-09-05 23:27 ` [PATCH v6 02/15] xen/8250-uart: update definitions dmukhin
2025-09-06  2:02   ` Stefano Stabellini
2025-09-06 14:57   ` Mykola Kvach
2025-09-08  8:36     ` dmukhin
2025-09-05 23:27 ` [PATCH v6 03/15] emul/ns16x50: implement emulator stub dmukhin
2025-09-06  0:34   ` dmukhin
2025-09-06  2:03   ` Stefano Stabellini
2025-09-06  4:18     ` dmukhin
2025-09-06 20:37   ` Mykola Kvach
2025-09-08  8:32     ` dmukhin
2025-09-08  8:37   ` Mykola Kvach
2025-09-08  9:59     ` dmukhin
2025-09-05 23:27 ` [PATCH v6 04/15] emul/ns16x50: implement DLL/DLM registers dmukhin
2025-09-06  2:03   ` Stefano Stabellini
2025-09-06 21:12   ` Mykola Kvach
2025-09-08  6:41     ` dmukhin
2025-09-05 23:27 ` [PATCH v6 05/15] emul/ns16x50: implement SCR register dmukhin
2025-09-06  2:03   ` Stefano Stabellini
2025-09-06 21:24   ` Mykola Kvach
2025-09-08  3:37     ` dmukhin
2025-09-05 23:27 ` [PATCH v6 06/15] emul/ns16x50: implement IER/IIR registers dmukhin
2025-09-06  1:42   ` Stefano Stabellini
2025-09-08  2:37     ` dmukhin
2025-09-05 23:27 ` [PATCH v6 07/15] emul/ns16x50: implement LCR/LSR registers dmukhin
2025-09-06  2:04   ` Stefano Stabellini
2025-09-05 23:27 ` [PATCH v6 08/15] emul/ns16x50: implement MCR/MSR registers dmukhin
2025-09-06  1:42   ` Stefano Stabellini
2025-09-08  2:35     ` dmukhin
2025-09-05 23:27 ` [PATCH v6 09/15] emul/ns16x50: implement RBR register dmukhin
2025-09-06  2:04   ` Stefano Stabellini
2025-09-05 23:27 ` [PATCH v6 10/15] emul/ns16x50: implement THR register dmukhin
2025-09-06  1:59   ` Stefano Stabellini
2025-09-08  2:50     ` dmukhin
2025-09-08  3:15       ` dmukhin
2025-09-05 23:27 ` [PATCH v6 11/15] emul/ns16x50: implement FCR register (write-only) dmukhin
2025-09-06  2:04   ` Stefano Stabellini
2025-09-05 23:27 ` [PATCH v6 12/15] emul/ns16550: implement dump_state() hook dmukhin
2025-09-05 23:27 ` [PATCH v6 13/15] x86/domain: enable per-domain I/O port bitmaps dmukhin
2025-09-05 23:27 ` [PATCH v6 14/15] xen/domain: allocate d->irq_caps before arch-specific initialization dmukhin
2025-09-05 23:27 ` [PATCH v6 15/15] emul/ns16x50: implement IRQ emulation via vIOAPIC dmukhin
2025-09-06  0:33 ` [PATCH v6 00/15] x86: introduce NS16550-compatible UART emulator dmukhin
2025-09-06  2:01 ` Stefano Stabellini
2025-09-08  9:04   ` Mykola Kvach
2025-09-08 14:19     ` Oleksii Kurochko
2025-09-08 14:23     ` Oleksii Kurochko
2025-09-08 23:51       ` Stefano Stabellini

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=aL6CwuSme+yyOY0e@kraken \
    --to=dmukhin@xen.org \
    --cc=andrew.cooper3@citrix.com \
    --cc=anthony.perard@vates.tech \
    --cc=dmukhin@ford.com \
    --cc=jbeulich@suse.com \
    --cc=julien@xen.org \
    --cc=michal.orzel@amd.com \
    --cc=roger.pau@citrix.com \
    --cc=sstabellini@kernel.org \
    --cc=xakep.amatop@gmail.com \
    --cc=xen-devel@lists.xenproject.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 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.