docs/misra/safe.json | 8 ++++++++ xen/drivers/passthrough/pci.c | 5 ++--- 2 files changed, 10 insertions(+), 3 deletions(-)
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@epam.com>
---
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);
--
2.43.0
On 11/17/26 22:55, 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@epam.com>
> ---
> 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);
This can be used as alternative to this Jan's patch:
https://lists.xenproject.org/archives/html/xen-devel/2026-09/msg00048.html
BR, Dmytro.
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@epam.com>
> ---
> 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);
I'm sorry you're in the middle of a fight between the maintainers, but
this too is absurd.
There is nothing about pdev->sbdf worthy of an exception like this. We
have loads of other structures with identifiers that **if and only if we
were writing C++** would be const members. From a practical standpoint
d->domain_id or v->vcpu_id would be far more worthy than pdev->sbdf of
special casing in this way.
But, Xen is written in C.
I accept that this isn't creating a class of undefined behaviour, but
it's bogus-looking code try to try and use a C++-ism. In the modern
world of AI's, we're at much greater risk of an LLM seeing this and
deciding it's an acceptable code pattern in Xen.
Please just delete the const and make a plain SBDF assignment to drop
the current casts.
If I need to call a formal governance vote to put an end to the
maintainer infighting, I will. It has gone on far too long, and is
continuing to waste time we simply don't have.
~Andrew
On 17.11.2026 22:33, 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@epam.com>
>> ---
>> 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);
>
> I'm sorry you're in the middle of a fight between the maintainers, but
> this too is absurd.
>
> There is nothing about pdev->sbdf worthy of an exception like this. We
> have loads of other structures with identifiers that **if and only if we
> were writing C++** would be const members. From a practical standpoint
> d->domain_id or v->vcpu_id would be far more worthy than pdev->sbdf of
> special casing in this way.
As there doesn't look to be any interest in widening the pattern (as you
say, there are many more fields where such protection would help), I
agree ...
> But, Xen is written in C.
>
> I accept that this isn't creating a class of undefined behaviour, but
> it's bogus-looking code try to try and use a C++-ism. In the modern
> world of AI's, we're at much greater risk of an LLM seeing this and
> deciding it's an acceptable code pattern in Xen.
>
>
> Please just delete the const and make a plain SBDF assignment to drop
> the current casts.
... there's no point having such special treatment just here. Hence ...
> If I need to call a formal governance vote to put an end to the
> maintainer infighting, I will. It has gone on far too long, and is
> continuing to waste time we simply don't have.
... I then think no vote is going to be necessary (since aiui it's just
me you would want to outvote).
Jan
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@epam.com>
> > ---
> > 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;
© 2016 - 2026 Red Hat, Inc.