From: Anthony PERARD <anthony.perard@vates.tech>
To: Jiqian Chen <Jiqian.Chen@amd.com>
Cc: xen-devel@lists.xenproject.org, "Jan Beulich" <jbeulich@suse.com>,
"Andrew Cooper" <andrew.cooper3@citrix.com>,
"Roger Pau Monné" <roger.pau@citrix.com>, "Wei Liu" <wl@xen.org>,
"George Dunlap" <gwd@xenproject.org>,
"Julien Grall" <julien@xen.org>,
"Stefano Stabellini" <sstabellini@kernel.org>,
"Juergen Gross" <jgross@suse.com>,
"Daniel P . Smith" <dpsmith@apertussolutions.com>,
"Stewart Hildebrand" <Stewart.Hildebrand@amd.com>,
"Huang Rui" <ray.huang@amd.com>
Subject: Re: [RFC XEN PATCH v12 7/7] tools: Add new function to do PIRQ (un)map on PVH dom0
Date: Mon, 08 Jul 2024 14:57:08 +0000 [thread overview]
Message-ID: <Zov+QeadGqtzOfKY@l14> (raw)
In-Reply-To: <20240708114124.407797-8-Jiqian.Chen@amd.com>
On Mon, Jul 08, 2024 at 07:41:24PM +0800, Jiqian Chen wrote:
> diff --git a/tools/libs/light/libxl_arm.c b/tools/libs/light/libxl_arm.c
> index a4029e3ac810..d869bbec769e 100644
> --- a/tools/libs/light/libxl_arm.c
> +++ b/tools/libs/light/libxl_arm.c
> @@ -1774,6 +1774,16 @@ void libxl__arch_update_domain_config(libxl__gc *gc,
> {
> }
>
> +int libxl__arch_hvm_map_gsi(libxl__gc *gc, uint32_t sbdf, uint32_t domid)
> +{
> + return -1;
It's best to return an ERROR_* for libxl error code instead of -1.
ERROR_NI seems to be the one, it probably means not-implemented. Or
maybe ERROR_INVAL would do to.
> +}
> +
> +int libxl__arch_hvm_unmap_gsi(libxl__gc *gc, uint32_t sbdf, uint32_t domid)
> +{
> + return -1;
> +}
> +
> /*
> * Local variables:
> * mode: C
> diff --git a/tools/libs/light/libxl_pci.c b/tools/libs/light/libxl_pci.c
> index 96cb4da0794e..3d25997921cc 100644
> --- a/tools/libs/light/libxl_pci.c
> +++ b/tools/libs/light/libxl_pci.c
> @@ -17,6 +17,7 @@
> #include "libxl_osdeps.h" /* must come before any other headers */
>
> #include "libxl_internal.h"
> +#include "libxl_arch.h"
>
> #define PCI_BDF "%04x:%02x:%02x.%01x"
> #define PCI_BDF_SHORT "%02x:%02x.%01x"
> @@ -1478,6 +1479,16 @@ static void pci_add_dm_done(libxl__egc *egc,
> fclose(f);
> if (!pci_supp_legacy_irq())
> goto out_no_irq;
> +
> + /*
> + * When dom0 is PVH and mapping a x86 gsi to pirq for domU,
> + * should use gsi to grant irq permission.
> + */
> + if (!libxl__arch_hvm_map_gsi(gc, pci_encode_bdf(pci), domid))
Could you store the result of libxl__arch_hvm_map_gsi() in `rc', then
test that in the condition?
> + goto pci_permissive;
Why do you skip part of the function on success?
But also, please avoid the "goto" coding style, in libxl, it's tolerated
for error handling when used to skip to the end of function to have a
single path (or error path) out of a function.
> + else
> + LOGED(WARN, domid, "libxl__arch_hvm_map_gsi failed (err=%d)", errno);
No one reads logs unless there's a failure or something doesn't work. So
here we just ignore failure returned by libxl__arch_hvm_map_gsi(), is it
the right things to do? Usually, just ignoring error is wrong.
FYI: LOGE* already logs errno.
> +
> sysfs_path = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/irq", pci->domain,
> pci->bus, pci->dev, pci->func);
> f = fopen(sysfs_path, "r");
> @@ -1505,6 +1516,7 @@ static void pci_add_dm_done(libxl__egc *egc,
> }
> fclose(f);
>
> +pci_permissive:
> /* Don't restrict writes to the PCI config space from this VM */
> if (pci->permissive) {
> if ( sysfs_write_bdf(gc, SYSFS_PCIBACK_DRIVER"/permissive",
> @@ -2229,6 +2241,11 @@ skip_bar:
> if (!pci_supp_legacy_irq())
> goto skip_legacy_irq;
>
> + if (!libxl__arch_hvm_unmap_gsi(gc, pci_encode_bdf(pci), domid))
> + goto skip_legacy_irq;
> + else
> + LOGED(WARN, domid, "libxl__arch_hvm_unmap_gsi failed (err=%d)", errno);
> +
> sysfs_path = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/irq", pci->domain,
> pci->bus, pci->dev, pci->func);
>
> diff --git a/tools/libs/light/libxl_x86.c b/tools/libs/light/libxl_x86.c
> index 60643d6f5376..e7756d323cb6 100644
> --- a/tools/libs/light/libxl_x86.c
> +++ b/tools/libs/light/libxl_x86.c
> @@ -879,6 +879,117 @@ void libxl__arch_update_domain_config(libxl__gc *gc,
> libxl_defbool_val(src->b_info.u.hvm.pirq));
> }
>
> +struct pcidev_map_pirq {
> + uint32_t sbdf;
> + uint32_t pirq;
> + XEN_LIST_ENTRY(struct pcidev_map_pirq) entry;
> +};
> +
> +static pthread_mutex_t pcidev_pirq_mutex = PTHREAD_MUTEX_INITIALIZER;
> +static XEN_LIST_HEAD(, struct pcidev_map_pirq) pcidev_pirq_list =
> + XEN_LIST_HEAD_INITIALIZER(pcidev_pirq_list);
> +
> +int libxl__arch_hvm_map_gsi(libxl__gc *gc, uint32_t sbdf, uint32_t domid)
> +{
> + int pirq = -1, gsi, r;
> + xc_domaininfo_t info;
> + struct pcidev_map_pirq *pcidev_pirq;
> + libxl_ctx *ctx = libxl__gc_owner(gc);
Instead of declaring "ctx", you can use the macro "CTX" when you need
"ctx".
> +
> + r = xc_domain_getinfo_single(ctx->xch, LIBXL_TOOLSTACK_DOMID, &info);
> + if (r < 0) {
> + LOGED(ERROR, domid, "getdomaininfo failed (error=%d)", errno);
> + return r;
libxl_*() functions should return only libxl error code, that is return
code from other libxl_* functions, useally store in 'rc', or one of ERROR_*.
> + }
> + if ((info.flags & XEN_DOMINF_hvm_guest) &&
> + !(info.arch_config.emulation_flags & XEN_X86_EMU_USE_PIRQ)) {
> + gsi = xc_physdev_gsi_from_pcidev(ctx->xch, sbdf);
> + if (gsi < 0) {
> + return ERROR_FAIL;
> + }
> + r = xc_physdev_map_pirq(ctx->xch, domid, gsi, &pirq);
> + if (r < 0) {
> + LOGED(ERROR, domid, "xc_physdev_map_pirq gsi=%d (error=%d)",
> + gsi, errno);
> + return r;
> + }
> + r = xc_domain_gsi_permission(ctx->xch, domid, gsi, 1);
> + if (r < 0) {
> + LOGED(ERROR, domid, "xc_domain_gsi_permission gsi=%d (error=%d)",
> + gsi, errno);
> + return r;
> + }
> + } else {
> + return ERROR_FAIL;
Is it really an error?
I few values can be returned here,
* ERROR_INVAL meaing that the function was called on a dom0 that don't
do "GSI",
* 0, that is success, because the function check if it need to do
anything, and since there's nothing to do, we can return success.
> + }
> +
> + /* Save the pirq for the usage of unmapping */
> + pcidev_pirq = malloc(sizeof(struct pcidev_map_pirq));
> + if (!pcidev_pirq) {
> + LOGED(ERROR, domid, "no memory for saving pirq of pcidev info");
> + return ERROR_NOMEM;
> + }
> + pcidev_pirq->sbdf = sbdf;
> + pcidev_pirq->pirq = pirq;
> +
> + assert(!pthread_mutex_lock(&pcidev_pirq_mutex));
> + XEN_LIST_INSERT_HEAD(&pcidev_pirq_list, pcidev_pirq, entry);
I don't think that's going to work as you expect. libxl isn't a daemon
(or sometime it is but used for several domains), so anything store in
memory will be lost, or would be shared with other guest.
Do you need this mappins sbdf<-> pirq ? Is there a way to query this
information later from the environement? If not, you will need to store
the data somewhere else, probably in "libxl_domain_config *d_config" as
libxl can retrive the data with libxl__get_domain_configuration().
There's also the posibility to store that info in xenstore, but we
should probably avoid that.
Thanks,
--
Anthony Perard | Vates XCP-ng Developer
XCP-ng & Xen Orchestra - Vates solutions
web: https://vates.tech
next prev parent reply other threads:[~2024-07-08 14:57 UTC|newest]
Thread overview: 76+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-08 11:41 [XEN PATCH v12 0/7] Support device passthrough when dom0 is PVH on Xen Jiqian Chen
2024-07-08 11:41 ` [XEN PATCH v12 1/7] xen/pci: Add hypercall to support reset of pcidev Jiqian Chen
2024-07-08 14:56 ` Jan Beulich
2024-07-09 2:47 ` Chen, Jiqian
2024-07-09 6:01 ` Jan Beulich
2024-07-31 15:55 ` Roger Pau Monné
2024-07-31 15:58 ` Jan Beulich
2024-07-31 16:13 ` Roger Pau Monné
2024-08-01 6:49 ` Jan Beulich
2024-08-02 2:56 ` Chen, Jiqian
2024-08-02 2:55 ` Chen, Jiqian
2024-08-02 6:25 ` Jan Beulich
2024-08-02 7:41 ` Chen, Jiqian
2024-08-02 7:43 ` Jan Beulich
2024-08-02 7:44 ` Roger Pau Monné
2024-07-08 11:41 ` [XEN PATCH v12 2/7] x86/pvh: Allow (un)map_pirq when dom0 is PVH Jiqian Chen
2024-07-08 14:58 ` Jan Beulich
2024-07-22 21:37 ` Stefano Stabellini
2024-07-30 13:09 ` Andrew Cooper
2024-07-31 1:47 ` Chen, Jiqian
2024-07-31 8:31 ` Chen, Jiqian
2024-07-31 8:42 ` Jan Beulich
2024-07-31 7:50 ` Roger Pau Monné
2024-07-31 7:58 ` Jan Beulich
2024-07-31 8:24 ` Roger Pau Monné
2024-07-31 8:40 ` Jan Beulich
2024-07-31 8:51 ` Roger Pau Monné
2024-07-31 9:02 ` Jan Beulich
2024-07-31 9:37 ` Roger Pau Monné
2024-07-31 9:55 ` Jan Beulich
2024-07-31 11:29 ` Roger Pau Monné
2024-07-31 11:39 ` Jan Beulich
2024-07-31 13:03 ` Roger Pau Monné
2024-08-02 2:37 ` Chen, Jiqian
2024-08-02 8:11 ` Roger Pau Monné
2024-08-02 8:17 ` Chen, Jiqian
2024-08-02 8:35 ` Roger Pau Monné
2024-08-02 8:40 ` Chen, Jiqian
2024-08-02 9:17 ` Jan Beulich
2024-08-02 9:37 ` Jan Beulich
2024-07-31 8:39 ` Chen, Jiqian
2024-07-08 11:41 ` [XEN PATCH v12 3/7] x86/pvh: Add PHYSDEVOP_setup_gsi for PVH dom0 Jiqian Chen
2024-07-10 8:01 ` Chen, Jiqian
2024-07-11 7:58 ` Chen, Jiqian
2024-07-22 21:38 ` Stefano Stabellini
2024-07-08 11:41 ` [XEN PATCH v12 4/7] x86/domctl: Add hypercall to set the access of x86 gsi Jiqian Chen
2024-07-09 13:08 ` Jan Beulich
2024-07-26 6:55 ` Chen, Jiqian
2024-07-22 22:10 ` Stefano Stabellini
2024-07-26 6:53 ` Chen, Jiqian
2024-07-26 20:16 ` Stefano Stabellini
2024-08-01 11:06 ` Roger Pau Monné
2024-08-01 11:36 ` Jan Beulich
2024-08-01 12:41 ` Roger Pau Monné
2024-08-01 13:11 ` Jan Beulich
2024-08-02 3:10 ` Chen, Jiqian
2024-08-02 6:27 ` Jan Beulich
2024-08-02 7:44 ` Chen, Jiqian
2024-08-02 7:59 ` Roger Pau Monné
2024-08-02 8:08 ` Roger Pau Monné
2024-08-02 8:23 ` Chen, Jiqian
2024-08-02 9:40 ` Jan Beulich
2024-08-02 12:05 ` Roger Pau Monné
2024-07-08 11:41 ` [XEN PATCH v12 5/7] tools/libxc: Allow gsi be mapped into a free pirq Jiqian Chen
2024-07-09 13:26 ` Jan Beulich
2024-07-10 7:55 ` Chen, Jiqian
2024-08-01 12:55 ` Roger Pau Monné
2024-07-08 11:41 ` [RFC XEN PATCH v12 6/7] tools: Add new function to get gsi from dev Jiqian Chen
2024-07-08 13:27 ` Anthony PERARD
2024-07-09 3:35 ` Chen, Jiqian
2024-07-29 16:30 ` Anthony PERARD
2024-08-01 13:01 ` Roger Pau Monné
2024-08-02 3:13 ` Chen, Jiqian
2024-07-08 11:41 ` [RFC XEN PATCH v12 7/7] tools: Add new function to do PIRQ (un)map on PVH dom0 Jiqian Chen
2024-07-08 14:57 ` Anthony PERARD [this message]
2024-07-09 6:18 ` Chen, Jiqian
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=Zov+QeadGqtzOfKY@l14 \
--to=anthony.perard@vates.tech \
--cc=Jiqian.Chen@amd.com \
--cc=Stewart.Hildebrand@amd.com \
--cc=andrew.cooper3@citrix.com \
--cc=dpsmith@apertussolutions.com \
--cc=gwd@xenproject.org \
--cc=jbeulich@suse.com \
--cc=jgross@suse.com \
--cc=julien@xen.org \
--cc=ray.huang@amd.com \
--cc=roger.pau@citrix.com \
--cc=sstabellini@kernel.org \
--cc=wl@xen.org \
--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.