All of lore.kernel.org
 help / color / mirror / Atom feed
From: Nicola Vetrini <nicola.vetrini@bugseng.com>
To: Jan Beulich <jbeulich@suse.com>
Cc: "Andrew Cooper" <andrew.cooper3@citrix.com>,
	Xen-devel <xen-devel@lists.xenproject.org>,
	"Roger Pau Monné" <roger.pau@citrix.com>
Subject: Re: [PATCH 09/22] x86/traps: Move load_system_tables() into traps-setup.c
Date: Fri, 15 Aug 2025 10:40:12 +0200	[thread overview]
Message-ID: <219ecea713dd9dac4c788d91c727f401@bugseng.com> (raw)
In-Reply-To: <75886e26-29b3-4aab-9780-7301330b4bb1@suse.com>

On 2025-08-15 10:30, Jan Beulich wrote:
> On 14.08.2025 20:20, Andrew Cooper wrote:
>> On 14/08/2025 8:26 am, Jan Beulich wrote:
>>> On 13.08.2025 13:36, Andrew Cooper wrote:
>>>> On 12/08/2025 10:43 am, Nicola Vetrini wrote:
>>>>> On 2025-08-08 22:23, Andrew Cooper wrote:
>>>>>> diff --git a/xen/arch/x86/traps-setup.c 
>>>>>> b/xen/arch/x86/traps-setup.c
>>>>>> index 8ca379c9e4cb..13b8fcf0ba51 100644
>>>>>> --- a/xen/arch/x86/traps-setup.c
>>>>>> +++ b/xen/arch/x86/traps-setup.c
>>>>>> @@ -19,6 +20,124 @@ boolean_param("ler", opt_ler);
>>>>>> 
>>>>>>  void nocall entry_PF(void);
>>>>>> 
>>>>>> +/*
>>>>>> + * Sets up system tables and descriptors for IDT devliery.
>>>>>> + *
>>>>>> + * - Sets up TSS with stack pointers, including ISTs
>>>>>> + * - Inserts TSS selector into regular and compat GDTs
>>>>>> + * - Loads GDT, IDT, TR then null LDT
>>>>>> + * - Sets up IST references in the IDT
>>>>>> + */
>>>>>> +static void load_system_tables(void)
>>>>>> +{
>>>>>> +    unsigned int i, cpu = smp_processor_id();
>>>>>> +    unsigned long stack_bottom = get_stack_bottom(),
>>>>>> +        stack_top = stack_bottom & ~(STACK_SIZE - 1);
>>>>>> +    /*
>>>>>> +     * NB: define tss_page as a local variable because clang 3.5
>>>>>> doesn't
>>>>>> +     * support using ARRAY_SIZE against per-cpu variables.
>>>>>> +     */
>>>>>> +    struct tss_page *tss_page = &this_cpu(tss_page);
>>>>>> +    idt_entry_t *idt = this_cpu(idt);
>>>>>> +
>>>>> Given the clang baseline this might not be needed anymore?
>>>> Hmm.  While true, looking at 51461114e26, the code is definitely 
>>>> better
>>>> written with the tss_page variable and we wouldn't want to go back 
>>>> to
>>>> the old form.
>>>> 
>>>> I think that I'll simply drop the comment.
>>>> 
>>>> ~Andrew
>>>> 
>>>> P.S.
>>>> 
>>>> Generally speaking, because of the RELOC_HIDE() in this_cpu(), any 
>>>> time
>>>> you ever want two accesses to a variable, it's better (code gen 
>>>> wise) to
>>>> construct a pointer to it and use the point multiple times.
>>>> 
>>>> I don't understand why there's a RELOC_HIDE() in this_cpu().  The
>>>> justification doesn't make sense, but I've not had time to explore 
>>>> what
>>>> happens if we take it out.
>>> There's no justification in xen/percpu.h?
>> 
>> Well, it's given in compiler.h by RELOC_HIDE().
>> 
>> /* This macro obfuscates arithmetic on a variable address so that gcc
>>    shouldn't recognize the original var, and make assumptions about it 
>> */
>> 
>> 
>> But this is far from convincing.
>> 
>>> 
>>> My understanding is that we simply may not expose any accesses to 
>>> per_cpu_*
>>> variables directly to the compiler, or there's a risk that it might 
>>> access
>>> the "master" variable (i.e. CPU0's on at least x86).
>> 
>> RELOC_HIDE() doesn't do anything about the correctness of the pointer
>> arithmetic expression to make the access work.
>> 
>> I don't see how a correct expression can ever access CPU0's data by
>> accident.
> 
> Hmm, upon another look I agree. I wonder whether we inherited this from
> Linux, where in turn it may have been merely a workaround to deal with
> preemptible code not correctly accessing per-CPU data (i.e. not
> accounting for get_per_cpu_offset() not being stable across 
> preemption).
> Yet then per_cpu() would have been of similar concern when "cpu" isn't
> properly re-fetched after any possible preemption point ...
> 
> Jan

Probably inherited with a stripped-down comment on top of RELOC_HIDE, 
see [1]. In a way it does make sense that the compiler may decide to 
optimize based on this assumption, though I don't know whether wrapping 
is meant to happen with per-CPU variables.

[1] 
https://elixir.bootlin.com/linux/v6.16/source/include/linux/compiler-gcc.h#L31

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


  reply	other threads:[~2025-08-15  8:40 UTC|newest]

Thread overview: 120+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-08 20:22 [PATCH 00/22] x86: FRED support, part 1 (stacks and exceptions) Andrew Cooper
2025-08-08 20:22 ` [PATCH 01/22] x86/msr: Rename MSR_INTERRUPT_SSP_TABLE to MSR_ISST Andrew Cooper
2025-08-12  8:06   ` Jan Beulich
2025-08-13  9:02     ` Andrew Cooper
2025-08-08 20:22 ` [PATCH 02/22] x86/msr: Rename wrmsr_ns() to wrmsrns(), and take 64bit value Andrew Cooper
2025-08-11  6:36   ` Andrew Cooper
2025-08-12  8:08     ` Jan Beulich
2025-08-08 20:22 ` [PATCH 03/22] x86/traps: Drop incorrect BUILD_BUG_ON() and comment in load_system_tables() Andrew Cooper
2025-08-12  8:11   ` Jan Beulich
2025-08-13  9:40     ` Andrew Cooper
2025-08-14  8:50       ` Jan Beulich
2025-08-08 20:22 ` [PATCH 04/22] x86/idt: Minor improvements to _update_gate_addr_lower() Andrew Cooper
2025-08-12  8:16   ` Jan Beulich
2025-08-13  9:48     ` Andrew Cooper
2025-08-08 20:22 ` [PATCH 05/22] x86/traps: Rename early_traps_init() to bsp_early_traps_init() Andrew Cooper
2025-08-12  8:17   ` Jan Beulich
2025-08-08 20:22 ` [PATCH 06/22] x86/traps: Introduce bsp_traps_reinit() Andrew Cooper
2025-08-12  8:19   ` Jan Beulich
2025-08-13  9:51     ` Andrew Cooper
2025-08-08 20:22 ` [PATCH 07/22] x86/spec-ctrl: Rework init_shadow_spec_ctrl_state() to take an info pointer Andrew Cooper
2025-08-12  8:27   ` Jan Beulich
2025-08-13 10:35     ` Andrew Cooper
2025-08-08 20:23 ` [PATCH 08/22] x86/traps: Introduce ap_early_traps_init() and set up exception handling earlier Andrew Cooper
2025-08-12  8:41   ` Jan Beulich
2025-08-13 11:13     ` Andrew Cooper
2025-08-14  8:53       ` Jan Beulich
2025-08-14 18:07   ` [PATCH v1.1 08/22] x86/traps: Introduce percpu_early_traps_init() " Andrew Cooper
2025-08-15  9:24     ` Jan Beulich
2025-08-08 20:23 ` [PATCH 09/22] x86/traps: Move load_system_tables() into traps-setup.c Andrew Cooper
2025-08-12  9:19   ` Jan Beulich
2025-08-13 11:25     ` Andrew Cooper
2025-08-14  8:55       ` Jan Beulich
2025-08-14 18:09         ` Andrew Cooper
2025-08-15  8:22           ` Jan Beulich
2025-08-15  8:28             ` Andrew Cooper
2025-08-15  8:32               ` Jan Beulich
2025-08-12  9:43   ` Nicola Vetrini
2025-08-13 11:36     ` Andrew Cooper
2025-08-14  7:26       ` Jan Beulich
2025-08-14 18:20         ` Andrew Cooper
2025-08-15  8:30           ` Jan Beulich
2025-08-15  8:40             ` Nicola Vetrini [this message]
2025-08-15  8:49               ` Jan Beulich
2025-08-08 20:23 ` [PATCH 10/22] x86/traps: Move subarch_percpu_traps_init() " Andrew Cooper
2025-08-11  8:17   ` Andrew Cooper
2025-08-12  9:52     ` Jan Beulich
2025-08-13 11:53       ` Andrew Cooper
2025-08-14  8:58         ` Jan Beulich
2025-08-14 10:17           ` Andrew Cooper
2025-08-14 10:52             ` Jan Beulich
2025-08-14 11:02               ` Andrew Cooper
2025-08-08 20:23 ` [PATCH 11/22] x86/traps: Fold x86_64/traps.c into traps.c Andrew Cooper
2025-08-12  9:53   ` Jan Beulich
2025-08-08 20:23 ` [PATCH 12/22] x86/traps: Unexport show_code() and show_stack_overflow() Andrew Cooper
2025-08-12  9:54   ` Jan Beulich
2025-08-08 20:23 ` [PATCH 13/22] x86: FRED enumerations Andrew Cooper
2025-08-13 12:28   ` Andrew Cooper
2025-08-14  7:30     ` Jan Beulich
2025-08-14 11:20   ` Jan Beulich
2025-08-14 11:42     ` Andrew Cooper
2025-08-14 11:44       ` Jan Beulich
2025-08-14 11:47         ` Andrew Cooper
2025-08-14 19:37           ` Nicola Vetrini
2025-08-14 19:44             ` Andrew Cooper
2025-08-14 21:27               ` Nicola Vetrini
2025-08-14 20:18             ` Nicola Vetrini
2025-08-14 13:19     ` Jan Beulich
2025-08-14 18:45       ` Andrew Cooper
2025-08-15  8:34         ` Jan Beulich
2025-08-21 21:23     ` Andrew Cooper
2025-08-18  9:02   ` Jan Beulich
2025-08-08 20:23 ` [PATCH 14/22] x86/traps: Extend struct cpu_user_regs/cpu_info with FRED fields Andrew Cooper
2025-08-14 13:12   ` Jan Beulich
2025-08-14 15:07     ` Andrew Cooper
2025-08-08 20:23 ` [PATCH 15/22] x86/traps: Introduce opt_fred Andrew Cooper
2025-08-14 13:30   ` Jan Beulich
2025-08-14 19:16     ` Andrew Cooper
2025-08-15  8:37       ` Jan Beulich
2025-08-21 21:52         ` Andrew Cooper
2025-08-25  9:08           ` Jan Beulich
2025-08-08 20:23 ` [PATCH 16/22] x86/boot: Adjust CR4 handling around ap_early_traps_init() Andrew Cooper
2025-08-14 14:47   ` Jan Beulich
2025-08-14 14:54     ` Andrew Cooper
2025-08-14 14:56       ` Jan Beulich
2025-08-14 19:22         ` Andrew Cooper
2025-08-08 20:23 ` [PATCH 17/22] x86/S3: Switch to using RSTORSSP to recover SSP on resume Andrew Cooper
2025-08-14 14:54   ` Jan Beulich
2025-08-08 20:23 ` [PATCH 18/22] x86/traps: Set MSR_PL0_SSP in load_system_tables() Andrew Cooper
2025-08-14 15:00   ` Jan Beulich
2025-08-14 19:37     ` Andrew Cooper
2025-08-15  8:52       ` Jan Beulich
2025-08-15 13:49         ` Andrew Cooper
2025-08-08 20:23 ` [PATCH 19/22] x86/boot: Use RSTORSSP to establish SSP Andrew Cooper
2025-08-14 15:11   ` Jan Beulich
2025-08-14 20:09     ` Andrew Cooper
2025-08-15  9:03       ` Jan Beulich
2025-08-21 22:09         ` Andrew Cooper
2025-08-25  9:12           ` Jan Beulich
2025-08-08 20:23 ` [PATCH 20/22] x86/traps: Alter switch_stack_and_jump() for FRED mode Andrew Cooper
2025-08-14 15:35   ` Jan Beulich
2025-08-14 20:55     ` Andrew Cooper
2025-08-15  9:10       ` Jan Beulich
2025-08-21 22:56         ` Andrew Cooper
2025-08-25  9:19           ` Jan Beulich
2025-08-08 20:23 ` [PATCH 21/22] x86/traps: Introduce FRED entrypoints Andrew Cooper
2025-08-11 11:38   ` Andrew Cooper
2025-08-14 15:57   ` Jan Beulich
2025-08-14 20:40     ` Andrew Cooper
2025-08-15  9:22       ` Jan Beulich
2025-08-18  8:59       ` Jan Beulich
2025-08-18 10:03   ` Jan Beulich
2025-08-18 10:09     ` Andrew Cooper
2025-08-08 20:23 ` [PATCH 22/22] x86/traps: Enable FRED when requested Andrew Cooper
2025-08-18  9:35   ` Jan Beulich
2025-08-18  9:47     ` Andrew Cooper
2025-08-18  9:53       ` Jan Beulich
2025-08-08 23:49 ` [PATCH 23/22] x86/vmx: Adjust NMI handling for FRED Andrew Cooper
2025-08-18 10:02   ` Jan Beulich
2025-08-18 17:18     ` Andrew Cooper
2025-08-19  6:31       ` Jan Beulich

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=219ecea713dd9dac4c788d91c727f401@bugseng.com \
    --to=nicola.vetrini@bugseng.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=jbeulich@suse.com \
    --cc=roger.pau@citrix.com \
    --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.