[PATCH v3 2/5] xen/console: introduce console_get_domid()

dmkhn@proton.me posted 5 patches 5 months, 2 weeks ago
There is a newer version of this series
[PATCH v3 2/5] xen/console: introduce console_get_domid()
Posted by dmkhn@proton.me 5 months, 2 weeks ago
From: Denis Mukhin <dmukhin@ford.com>

Add console_get_domid() to the console driver to retrieve the current console
owner domain ID.

Use the function in vpl011 emulator which leads to simpler code.

Make console_{get,put}_domain() private to the console driver.

No functional change intended.

Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
Changes since v2:
- changed commit subject line
- Link to v2: https://lore.kernel.org/xen-devel/20250331230508.440198-8-dmukhin@ford.com/
---
 xen/arch/arm/vpl011.c      |  5 +----
 xen/drivers/char/console.c | 11 +++++++++--
 xen/include/xen/console.h  |  3 +--
 3 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/xen/arch/arm/vpl011.c b/xen/arch/arm/vpl011.c
index 66047bf33c..0f58b2c900 100644
--- a/xen/arch/arm/vpl011.c
+++ b/xen/arch/arm/vpl011.c
@@ -78,12 +78,11 @@ static void vpl011_write_data_xen(struct domain *d, uint8_t data)
     unsigned long flags;
     struct vpl011 *vpl011 = &d->arch.vpl011;
     struct vpl011_xen_backend *intf = vpl011->backend.xen;
-    struct domain *input = console_get_domain();
 
     VPL011_LOCK(d, flags);
 
     intf->out[intf->out_prod++] = data;
-    if ( d == input )
+    if ( d->domain_id == console_get_domid() )
     {
         if ( intf->out_prod == 1 )
         {
@@ -123,8 +122,6 @@ static void vpl011_write_data_xen(struct domain *d, uint8_t data)
     vpl011_update_interrupt_status(d);
 
     VPL011_UNLOCK(d, flags);
-
-    console_put_domain(input);
 }
 
 /*
diff --git a/xen/drivers/char/console.c b/xen/drivers/char/console.c
index c8dde38376..86fd0b427d 100644
--- a/xen/drivers/char/console.c
+++ b/xen/drivers/char/console.c
@@ -474,19 +474,26 @@ static unsigned int __read_mostly console_rx = 0;
 
 #define max_console_rx (max_init_domid + 1)
 
-struct domain *console_get_domain(void)
+static struct domain *console_get_domain(void)
 {
     if ( console_rx == 0 )
             return NULL;
     return rcu_lock_domain_by_id(console_rx - 1);
 }
 
-void console_put_domain(struct domain *d)
+static void console_put_domain(struct domain *d)
 {
     if ( d )
         rcu_unlock_domain(d);
 }
 
+domid_t console_get_domid(void)
+{
+    if ( console_rx == 0 )
+        return DOMID_XEN;
+    return console_rx - 1;
+}
+
 static void console_switch_input(void)
 {
     unsigned int next_rx = console_rx;
diff --git a/xen/include/xen/console.h b/xen/include/xen/console.h
index 83cbc9fbda..c6f9d84d80 100644
--- a/xen/include/xen/console.h
+++ b/xen/include/xen/console.h
@@ -32,8 +32,7 @@ void console_end_sync(void);
 void console_start_log_everything(void);
 void console_end_log_everything(void);
 
-struct domain *console_get_domain(void);
-void console_put_domain(struct domain *d);
+domid_t console_get_domid(void);
 
 /*
  * Steal output from the console. Returns +ve identifier, else -ve error.
-- 
2.34.1
Re: [PATCH v3 2/5] xen/console: introduce console_get_domid()
Posted by Jan Beulich 5 months, 2 weeks ago
On 19.05.2025 22:12, dmkhn@proton.me wrote:
> --- a/xen/arch/arm/vpl011.c
> +++ b/xen/arch/arm/vpl011.c
> @@ -78,12 +78,11 @@ static void vpl011_write_data_xen(struct domain *d, uint8_t data)
>      unsigned long flags;
>      struct vpl011 *vpl011 = &d->arch.vpl011;
>      struct vpl011_xen_backend *intf = vpl011->backend.xen;
> -    struct domain *input = console_get_domain();
>  
>      VPL011_LOCK(d, flags);
>  
>      intf->out[intf->out_prod++] = data;
> -    if ( d == input )
> +    if ( d->domain_id == console_get_domid() )

How do you know d isn't a domain different from the one that was the
console "owner" prior to being destroyed? Original code guaranteed this
up to ...

> @@ -123,8 +122,6 @@ static void vpl011_write_data_xen(struct domain *d, uint8_t data)
>      vpl011_update_interrupt_status(d);
>  
>      VPL011_UNLOCK(d, flags);
> -
> -    console_put_domain(input);

... here.

Jan
Re: [PATCH v3 2/5] xen/console: introduce console_get_domid()
Posted by dmkhn@proton.me 5 months, 1 week ago
On Tue, May 20, 2025 at 08:20:16AM +0200, Jan Beulich wrote:
> On 19.05.2025 22:12, dmkhn@proton.me wrote:
> > --- a/xen/arch/arm/vpl011.c
> > +++ b/xen/arch/arm/vpl011.c
> > @@ -78,12 +78,11 @@ static void vpl011_write_data_xen(struct domain *d, uint8_t data)
> >      unsigned long flags;
> >      struct vpl011 *vpl011 = &d->arch.vpl011;
> >      struct vpl011_xen_backend *intf = vpl011->backend.xen;
> > -    struct domain *input = console_get_domain();
> >
> >      VPL011_LOCK(d, flags);
> >
> >      intf->out[intf->out_prod++] = data;
> > -    if ( d == input )
> > +    if ( d->domain_id == console_get_domid() )
> 
> How do you know d isn't a domain different from the one that was the
> console "owner" prior to being destroyed? Original code guaranteed this
> up to ...
> 
> > @@ -123,8 +122,6 @@ static void vpl011_write_data_xen(struct domain *d, uint8_t data)
> >      vpl011_update_interrupt_status(d);
> >
> >      VPL011_UNLOCK(d, flags);
> > -
> > -    console_put_domain(input);
> 
> ... here.

Agreed; I will drop the code change in the next iteration.
Thanks.

> 
> Jan