[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] PCI: deviate MISRA R11.8 for the one-time SBDF initialization



On Tue, 17 Nov 2026, Andrew Cooper wrote:
> On 17/11/2026 8:55 pm, Dmytro Prokopchuk1 wrote:
> > MISRA C Rule 11.8 states: "A cast shall not remove any const or volatile
> > qualification from the type pointed to by a pointer".
> >
> > alloc_pdev() writes the device identity once. seg, bus and devfn are
> > members of a const union, so that write goes through a cast that removes
> > const. Three such casts performed the same initialization.
> >
> > Store the whole sbdf in one assignment and document that cast with
> > one-line SAF-15-safe comment.
> >
> > No functional changes.
> >
> > Signed-off-by: Dmytro Prokopchuk <dmytro_prokopchuk1@xxxxxxxx>
> > ---
> > Test CI pipeline:
> > https://gitlab.com/xen-project/people/dimaprkp4k/xen/-/pipelines/3081067362
> > ---
> >  docs/misra/safe.json          | 8 ++++++++
> >  xen/drivers/passthrough/pci.c | 5 ++---
> >  2 files changed, 10 insertions(+), 3 deletions(-)
> >
> > diff --git a/docs/misra/safe.json b/docs/misra/safe.json
> > index 3584cb90c6..3503d2e808 100644
> > --- a/docs/misra/safe.json
> > +++ b/docs/misra/safe.json
> > @@ -124,6 +124,14 @@
> >          },
> >          {
> >              "id": "SAF-15-safe",
> > +            "analyser": {
> > +                "eclair": "MC3A2.R11.8"
> > +            },
> > +            "name": "Rule 11.8: one-time init of a const PCI identity",
> > +            "text": "The cast removes const only to initialize the const 
> > pci_dev identity fields (seg, bus, devfn). The const qualification prevents 
> > any later assignment, and this is the only write, performed before the 
> > device is published on alldevs_list."
> > +        },
> > +        {
> > +            "id": "SAF-16-safe",
> >              "analyser": {},
> >              "name": "Sentinel",
> >              "text": "Next ID to be used"
> > diff --git a/xen/drivers/passthrough/pci.c b/xen/drivers/passthrough/pci.c
> > index 578472d068..14d59b1293 100644
> > --- a/xen/drivers/passthrough/pci.c
> > +++ b/xen/drivers/passthrough/pci.c
> > @@ -329,9 +329,8 @@ static struct pci_dev *alloc_pdev(struct pci_seg *pseg, 
> > u8 bus, u8 devfn)
> >      if ( !pdev )
> >          return NULL;
> >  
> > -    *(u16*) &pdev->seg = pseg->nr;
> > -    *((u8*) &pdev->bus) = bus;
> > -    *((u8*) &pdev->devfn) = devfn;
> > +    /* SAF-15-safe R11.8 one-time init of the const PCI identity */
> > +    *(pci_sbdf_t *)&pdev->sbdf = PCI_SBDF(pseg->nr, bus, devfn);
> >      pdev->domain = NULL;
> >  
> >      INIT_LIST_HEAD(&pdev->vf_list);
> 
> Please just delete the const and make a plain SBDF assignment to drop
> the current casts.
> 

I agree. Dropping the const removes the violation instead of deviating
it, and makes the code simpler: one plain assignment, no casts.

Something like this?

diff --git a/xen/drivers/passthrough/pci.c b/xen/drivers/passthrough/pci.c
index 578472d068..ca4d631d8d 100644
--- a/xen/drivers/passthrough/pci.c
+++ b/xen/drivers/passthrough/pci.c
@@ -329,9 +329,7 @@ static struct pci_dev *alloc_pdev(struct pci_seg *pseg, u8 
bus, u8 devfn)
     if ( !pdev )
         return NULL;
 
-    *(u16*) &pdev->seg = pseg->nr;
-    *((u8*) &pdev->bus) = bus;
-    *((u8*) &pdev->devfn) = devfn;
+    pdev->sbdf = PCI_SBDF(pseg->nr, bus, devfn);
     pdev->domain = NULL;
 
     INIT_LIST_HEAD(&pdev->vf_list);
diff --git a/xen/include/xen/pci.h b/xen/include/xen/pci.h
index b7a35371a7..83cbb96db1 100644
--- a/xen/include/xen/pci.h
+++ b/xen/include/xen/pci.h
@@ -109,7 +109,7 @@ struct pci_dev {
 
     struct domain *domain;
 
-    const union {
+    union {
         struct {
             uint8_t devfn;
             uint8_t bus;



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.