2021-05-04 20:54:58

by Thomas Gleixner

[permalink] [raw]
Subject: Re: [RFC PATCH v5 07/16] x86/watchdog/hardlockup: Add an HPET-based hardlockup detector

Ricardo,

On Tue, May 04 2021 at 12:05, Ricardo Neri wrote:
> +static int hardlockup_detector_nmi_handler(unsigned int type,
> + struct pt_regs *regs)
> +{
> + struct hpet_hld_data *hdata = hld_data;
> + int cpu = smp_processor_id();
> +
> + if (is_hpet_wdt_interrupt(hdata)) {
> + /*
> + * Make a copy of the target mask. We need this as once a CPU
> + * gets the watchdog NMI it will clear itself from ipi_cpumask.
> + * Also, target_cpumask will be updated in a workqueue for the
> + * next NMI IPI.
> + */
> + cpumask_copy(hld_data->ipi_cpumask, hld_data->monitored_cpumask);
> + /*
> + * Even though the NMI IPI will be sent to all CPUs but self,
> + * clear the CPU to identify a potential unrelated NMI.
> + */
> + cpumask_clear_cpu(cpu, hld_data->ipi_cpumask);
> + if (cpumask_weight(hld_data->ipi_cpumask))
> + apic->send_IPI_mask_allbutself(hld_data->ipi_cpumask,
> + NMI_VECTOR);

How is this supposed to work correctly?

x2apic_cluster:
x2apic_send_IPI_mask_allbutself()
__x2apic_send_IPI_mask()
tmpmsk = this_cpu_cpumask_var_ptr(ipi_mask);
cpumask_copy(tmpmsk, mask);

So if an NMI hits right after or in the middle of the cpumask_copy()
then the IPI sent from that NMI overwrites tmpmask and when its done
then tmpmask is empty. Similar to when it hits in the middle of
processing, just with the difference that maybe a few IPIs have been
sent already. But the not yet sent ones are lost...

Also anything which ends up in __default_send_IPI_dest_field() is
borked:

__default_send_IPI_dest_field()
cfg = __prepare_ICR2(mask);
native_apic_mem_write(APIC_ICR2, cfg);

<- NMI hits and invokes IPI which invokes __default_send_IPI_dest_field()...

cfg = __prepare_ICR(0, vector, dest);
native_apic_mem_write(APIC_ICR, cfg);

IOW, when the NMI returns ICR2 has been overwritten and the interrupted
IPI goes into lala land.

Thanks,

tglx