[XEN PATCH] x86/mce: use offsetof to determine section offset

Nicola Vetrini posted 1 patch 1 week, 4 days ago
Patches applied successfully (tree, apply log)
git fetch https://gitlab.com/xen-project/patchew/xen tags/patchew/350bd19ec4b0b190911d748df6ec92c626e7151a.1768415160.git.nicola.vetrini@bugseng.com
xen/arch/x86/cpu/mcheck/mce-apei.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
[XEN PATCH] x86/mce: use offsetof to determine section offset
Posted by Nicola Vetrini 1 week, 4 days ago
There is no reason to use pointer difference.
A violation of MISRA C Rule 18.2 ("Subtraction between pointers
shall only be applied to pointers that address elements of the
same array") is also resolved because the object to the subtraction
is applied is not an array.

Signed-off-by: Nicola Vetrini <nicola.vetrini@bugseng.com>
---
Found while randomly browsing violations of the rule on the allcode-x86_64 scan.
---
 xen/arch/x86/cpu/mcheck/mce-apei.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/xen/arch/x86/cpu/mcheck/mce-apei.c b/xen/arch/x86/cpu/mcheck/mce-apei.c
index b89502088243..21aabe2027d0 100644
--- a/xen/arch/x86/cpu/mcheck/mce-apei.c
+++ b/xen/arch/x86/cpu/mcheck/mce-apei.c
@@ -74,7 +74,8 @@ int apei_write_mce(struct mce *m)
 	rcd.hdr.record_id = cper_next_record_id();
 	rcd.hdr.flags = CPER_HW_ERROR_FLAGS_PREVERR;
 
-	rcd.sec_hdr.section_offset = (void *)&rcd.mce - (void *)&rcd;
+	rcd.sec_hdr.section_offset = offsetof(struct cper_mce_record, mce) -
+		                     offsetof(struct cper_mce_record, hdr);
 	rcd.sec_hdr.section_length = sizeof(rcd.mce);
 	rcd.sec_hdr.revision = CPER_SEC_REV;
 	/* fru_id and fru_text is invalid */
-- 
2.43.0
Re: [XEN PATCH] x86/mce: use offsetof to determine section offset
Posted by Jason Andryuk 1 week, 4 days ago
On 2026-01-14 13:27, Nicola Vetrini wrote:
> There is no reason to use pointer difference.
> A violation of MISRA C Rule 18.2 ("Subtraction between pointers
> shall only be applied to pointers that address elements of the
> same array") is also resolved because the object to the subtraction
> is applied is not an array.
> 
> Signed-off-by: Nicola Vetrini <nicola.vetrini@bugseng.com>
> ---
> Found while randomly browsing violations of the rule on the allcode-x86_64 scan.
> ---
>   xen/arch/x86/cpu/mcheck/mce-apei.c | 3 ++-
>   1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/xen/arch/x86/cpu/mcheck/mce-apei.c b/xen/arch/x86/cpu/mcheck/mce-apei.c
> index b89502088243..21aabe2027d0 100644
> --- a/xen/arch/x86/cpu/mcheck/mce-apei.c
> +++ b/xen/arch/x86/cpu/mcheck/mce-apei.c
> @@ -74,7 +74,8 @@ int apei_write_mce(struct mce *m)
>   	rcd.hdr.record_id = cper_next_record_id();
>   	rcd.hdr.flags = CPER_HW_ERROR_FLAGS_PREVERR;
>   
> -	rcd.sec_hdr.section_offset = (void *)&rcd.mce - (void *)&rcd;
> +	rcd.sec_hdr.section_offset = offsetof(struct cper_mce_record, mce) -
> +		                     offsetof(struct cper_mce_record, hdr);

"= offsetof(struct cper_mce_record, mce);" should be sufficient since 
the offset of hdr is 0?

Regards,
Jason

>   	rcd.sec_hdr.section_length = sizeof(rcd.mce);
>   	rcd.sec_hdr.revision = CPER_SEC_REV;
>   	/* fru_id and fru_text is invalid */
Re: [XEN PATCH] x86/mce: use offsetof to determine section offset
Posted by Nicola Vetrini 1 week, 4 days ago
On 2026-01-14 21:40, Jason Andryuk wrote:
> On 2026-01-14 13:27, Nicola Vetrini wrote:
>> There is no reason to use pointer difference.
>> A violation of MISRA C Rule 18.2 ("Subtraction between pointers
>> shall only be applied to pointers that address elements of the
>> same array") is also resolved because the object to the subtraction
>> is applied is not an array.
>> 
>> Signed-off-by: Nicola Vetrini <nicola.vetrini@bugseng.com>
>> ---
>> Found while randomly browsing violations of the rule on the 
>> allcode-x86_64 scan.
>> ---
>>   xen/arch/x86/cpu/mcheck/mce-apei.c | 3 ++-
>>   1 file changed, 2 insertions(+), 1 deletion(-)
>> 
>> diff --git a/xen/arch/x86/cpu/mcheck/mce-apei.c 
>> b/xen/arch/x86/cpu/mcheck/mce-apei.c
>> index b89502088243..21aabe2027d0 100644
>> --- a/xen/arch/x86/cpu/mcheck/mce-apei.c
>> +++ b/xen/arch/x86/cpu/mcheck/mce-apei.c
>> @@ -74,7 +74,8 @@ int apei_write_mce(struct mce *m)
>>   	rcd.hdr.record_id = cper_next_record_id();
>>   	rcd.hdr.flags = CPER_HW_ERROR_FLAGS_PREVERR;
>>   -	rcd.sec_hdr.section_offset = (void *)&rcd.mce - (void *)&rcd;
>> +	rcd.sec_hdr.section_offset = offsetof(struct cper_mce_record, mce) -
>> +		                     offsetof(struct cper_mce_record, hdr);
> 
> "= offsetof(struct cper_mce_record, mce);" should be sufficient since 
> the offset of hdr is 0?

Yeah, makes sense. Given that the struct layout is coming from the UEFI 
spec it's not likely to change either.

> 
> Regards,
> Jason
> 
>>   	rcd.sec_hdr.section_length = sizeof(rcd.mce);
>>   	rcd.sec_hdr.revision = CPER_SEC_REV;
>>   	/* fru_id and fru_text is invalid */

-- 
Nicola Vetrini, B.Sc.
Software Engineer
BUGSENG (https://bugseng.com)
LinkedIn: https://www.linkedin.com/in/nicola-vetrini-a42471253
Re: [XEN PATCH] x86/mce: use offsetof to determine section offset
Posted by Jan Beulich 1 week, 3 days ago
On 14.01.2026 21:56, Nicola Vetrini wrote:
> On 2026-01-14 21:40, Jason Andryuk wrote:
>> On 2026-01-14 13:27, Nicola Vetrini wrote:
>>> --- a/xen/arch/x86/cpu/mcheck/mce-apei.c
>>> +++ b/xen/arch/x86/cpu/mcheck/mce-apei.c
>>> @@ -74,7 +74,8 @@ int apei_write_mce(struct mce *m)
>>>   	rcd.hdr.record_id = cper_next_record_id();
>>>   	rcd.hdr.flags = CPER_HW_ERROR_FLAGS_PREVERR;
>>>   -	rcd.sec_hdr.section_offset = (void *)&rcd.mce - (void *)&rcd;
>>> +	rcd.sec_hdr.section_offset = offsetof(struct cper_mce_record, mce) -
>>> +		                     offsetof(struct cper_mce_record, hdr);
>>
>> "= offsetof(struct cper_mce_record, mce);" should be sufficient since 
>> the offset of hdr is 0?
> 
> Yeah, makes sense. Given that the struct layout is coming from the UEFI 
> spec it's not likely to change either.

It's okay either way, but I'm happy to adjust to the simpler form while
committing (I'd slightly prefer that, precisely for being simpler, and it
being close to what was there originally):
Acked-by: Jan Beulich <jbeulich@suse.com>
(ftaod: either way).

Jan
Re: [XEN PATCH] x86/mce: use offsetof to determine section offset
Posted by Nicola Vetrini 1 week, 3 days ago
On 2026-01-15 11:17, Jan Beulich wrote:
> On 14.01.2026 21:56, Nicola Vetrini wrote:
>> On 2026-01-14 21:40, Jason Andryuk wrote:
>>> On 2026-01-14 13:27, Nicola Vetrini wrote:
>>>> --- a/xen/arch/x86/cpu/mcheck/mce-apei.c
>>>> +++ b/xen/arch/x86/cpu/mcheck/mce-apei.c
>>>> @@ -74,7 +74,8 @@ int apei_write_mce(struct mce *m)
>>>>   	rcd.hdr.record_id = cper_next_record_id();
>>>>   	rcd.hdr.flags = CPER_HW_ERROR_FLAGS_PREVERR;
>>>>   -	rcd.sec_hdr.section_offset = (void *)&rcd.mce - (void *)&rcd;
>>>> +	rcd.sec_hdr.section_offset = offsetof(struct cper_mce_record, mce) 
>>>> -
>>>> +		                     offsetof(struct cper_mce_record, hdr);
>>> 
>>> "= offsetof(struct cper_mce_record, mce);" should be sufficient since
>>> the offset of hdr is 0?
>> 
>> Yeah, makes sense. Given that the struct layout is coming from the 
>> UEFI
>> spec it's not likely to change either.
> 
> It's okay either way, but I'm happy to adjust to the simpler form while
> committing (I'd slightly prefer that, precisely for being simpler, and 
> it
> being close to what was there originally):
> Acked-by: Jan Beulich <jbeulich@suse.com>
> (ftaod: either way).
> 
> Jan

Thanks

-- 
Nicola Vetrini, B.Sc.
Software Engineer
BUGSENG (https://bugseng.com)
LinkedIn: https://www.linkedin.com/in/nicola-vetrini-a42471253