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

Re: [PATCH] x86/shadow: don't use #if in macro invocations


  • To: Jan Beulich <jbeulich@xxxxxxxx>
  • From: Nicola Vetrini <nicola.vetrini@xxxxxxxxxxx>
  • Date: Wed, 18 Feb 2026 18:35:02 +0100
  • Arc-authentication-results: i=1; bugseng.com; arc=none smtp.remote-ip=162.55.131.47
  • Arc-message-signature: i=1; d=bugseng.com; s=openarc; a=rsa-sha256; c=relaxed/relaxed; t=1771436102; h=MIME-Version:Date:From:To:Cc:Subject:In-Reply-To:References: Message-ID:X-Sender:Organization:Content-Type: Content-Transfer-Encoding; bh=f5mTqAhO4yMhbGETNkMlHBOPiyW1jV9cH35c+Gm1F60=; b=C2th59N435j/I+RSGeLq87otN8fgdzSDgx2ryK5IE1bsUPom0qABf0qXuqvfQOShcNOx 9YVIngfLakooFs/MbLaYSSKf9S+wQcUDBf7UljqL2+e88GhAtAQsx5QVKIKxDdR8YMei1 0IVEZvQgj3eV1XhrBZjHhHKAQC4CXOLxyslP6rLHDO9gaSvnnWIPH009CqNfB+N4P38/y TLasaEw6oSbrggDTNxuo+mMczUTZlCzUpz3QXfa2zhLx2VeD0jJpg0byy6Xe2l3dmKfNW AHJC4DOK1MlNEfxi3qj50LWC/k3oZdeVfKAe4OZ6v1z6nw8e9qgpv7WknjsrxcFpkbaQr H10A4B8kCxWMXglnmRGXOMQ9Z4hS0FiKFcG30OPm44atWiGZSzl1RF1DQgxjTBGgH257a tITOHZAum82tSmaJ2Gn2GpzJ6XZ/hHj1fXe1Jazs/BOGxE46yTURROtJMUnV10MaKbjMm 2Pvq/hP3bJ2ARjoDuLaAO5EXcuk2jbdYs+KxLTsa6pmtXE+C7kxTg3hu+tNSbiFwAZFUN Z/5BYi51LiOgkfIXkXTHwynMv6iF4jjwytxZG+YeJGZ/mjriFwQ4kgaTwbGGrlUPylMOg GvlXeZbdeXYYdT5lEKgTBLzPfZVLuO4Psg5VJJscMx/z7UiO2bAP1oEYjuPPKgU=
  • Arc-seal: i=1; d=bugseng.com; s=openarc; a=rsa-sha256; cv=none; t=1771436102; b=nyig5CSGCK2zBrWyVIJDyzyGSaODA9vA4yRH1xaAzuq73mgS3H7uskWjM3XpxGnqN9Xx jOMXrqFHYqxhIGyZlxaa4aLacIaQKoj52UM9x8TymFRVLCCpRrdj2FPJRkxke/Y9kVn8o 3RIri8YmoJG+ZoTkFdMFkuyObZZQpt2F9aSLv7Ik9ro/K8fzpuN1jKo943NDFSl4m1Im7 rtSoVwwBQfeAMZqsrwoUMpIS7uEHdQO0ToZkueHOd7W/riY+Fxy9w8d0Ke42FlpbQ7Ag+ FX6I9L+6EZK1ETbm79lldkcfoD6m0FQzLJ5zv4BZwN2a8qROZeVSKBGn5OYvSS7ZpiJNL I9/EF/qaZAxAxCyizB+IcW9vktWIP1foob1KW6wgH/9iVJnIfGnU2ET3neE+xk2wus7bm K4ThFcnXIHoKP70H1k9BJqRnEj81tN3YFe9/yga8DKiHRwMH0NnhZ7g0Kxsi89we/YD9e 9G4n4QGlgjmDR+2A0OcYwaRVSFCau5IlZvr28rrEf16/7DPED94eQ/6z4ozAEI4bdcAuf /sVCclpnH8rHOLoN8R2dSctdvwpkbqU2JTj4Bsk4+UpwBtrlcfYg45Ao2vOegsZXiuYxI om95JvmPugtBlkiH3Z16zFZNj6CXFddjS2jFnBOdy0dmU9qWp5ka+5GEgaeJGsg=
  • Authentication-results: bugseng.com; arc=none smtp.remote-ip=162.55.131.47
  • Cc: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, Roger Pau Monné <roger.pau@xxxxxxxxxx>, xen-devel@xxxxxxxxxxxxxxxxxxxx
  • Delivery-date: Wed, 18 Feb 2026 17:35:21 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

On 2026-02-18 13:42, Jan Beulich wrote:
On 18.02.2026 12:30, Andrew Cooper wrote:
On 18/02/2026 9:03 am, Jan Beulich wrote:
As per the standard this is UB, i.e. we're building on a defacto extension
in the compilers we use.

Is it a real extension, or just something that happens to work?

I was hoping I would not need to go through that large swath of gcc doc to
actually figure, because ...

 Misra C:2012 rule 20.6 disallows this altogether,
though.

... this I assumed was reason enough. Still, now that you forced me to: In The C Preprocessor the behavior is described as intentional, but not as an
extension (section "Directives Within Macro Arguments"). Now you get to
judge whether that's a "real" extension or a "de-facto" one.


Well, since another alternative preprocessor may behave differently or even choke on this construct (on any instance!) my guess would be to regard this as a GNU extension.

FWIW MISRA disallows this completely because it can lead to UB 87 from C99: "There are sequences of preprocessing tokens within the list of macro arguments that would otherwise act as preprocessing directives (6.10.3)."

So it just sidesteps the issue without having to look at the actual token being formed and make our lives as tool implementors a tad easier.

Perhaps a reference to the GCC preprocessor docs could be added in the commit message or in the code, just to save some brain cycles again next time.

Use helper always-inline functions instead.

In sh_audit_l1_table(), along with reducing the scope of "gfn", which now isn't used anymore by the if() side of the conditional, also reduce the
scope of two other adjacent variables.

For audit_magic() note that both which parameters are needed and what
their types are is attributed to AUDIT_FAIL() accessing variables which
aren't passed as arguments to it.

This is grammatically awkward.  IMO it would be clearer to say "For
audit_magic() note that there are more parameters than might seem
necessary, caused by the expectations of AUDIT_FAIL()." 

I've switched to using that, but one aspect is lost this way: I would have
preferred both gl1e and sl1e to be plain entries, not pointers to ones.

---
Leaving even the fetching of current to the helper in
sh_rm_write_access_from_l1() looks tidier to me overall, albeit this means
the fetch will now occur once per present L1E.

This will not make a dent in the performance of the shadow code.

Converting the #if to if() and #ifdef to if(IS_ENABLED()) wouldn't work here, as identifiers are used which aren't available when the respective
conditions are false.

Personally, I'd have put this in the main commit message, because it's
the justification for why out-of-line static inline's need to be used.

I was wondering, so I've moved this up.

--- a/xen/arch/x86/mm/shadow/multi.c
+++ b/xen/arch/x86/mm/shadow/multi.c
@@ -395,7 +395,7 @@ static inline mfn_t cf_check sh_next_pag
shadow_set_l2e(d, sl2e, new_sl2e, sl2mfn, SH_type_fl1_shadow, sh_next_page)

 static inline u32
-guest_index(void *ptr)
+guest_index(const void *ptr)
 {
return (u32)((unsigned long)ptr & ~PAGE_MASK) / sizeof(guest_l1e_t);
 }

While fine per say, this doesn't appear to be related to the patch?

It does, the compiler told me to: type_from_gl3e() uses it, and I really
want to keep the const-s on both of its parameters.

Jan

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



 


Rackspace

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