Re: [PATCH 1/9] x86/mm/pat: flush the TLB when only an alias mapping changed

From: Edgecombe, Rick P

Date: Tue Nov 24 2026 - 17:49:39 EST


On Thu, 2026-11-12 at 18:31 +0200, Mike Rapoport (Microsoft) wrote:
> CPA flushes the TLB only when CPA_FLUSHTLB is set. cpa_process_alias()
> sets the flag only in alias_cpa and never in the original request.
>
> When the requested range already has the new protection and only its
> alias changes, the alias keeps stale TLB entries. For example:
>
> /* the vmalloc mapping is created read-only */
> p = vmap(pages, nr, VM_MAP, PAGE_KERNEL_RO);
>
> /* only the direct map changes and nothing is flushed */
> set_memory_ro((unsigned long)p, nr);
>
> Setting ->force_flush_all in cpa_process_alias() does not help: it only
> chooses how the TLB is flushed, and cpa_flush() is skipped entirely
> without CPA_FLUSHTLB.
>
> Worse, when the alias wasn't changed at all, for example when the primary
> mapping changes from ROX to RO or vice versa, ->force_flush_all in
> cpa_process_alias() causes an unnecessary flush of all TLBs.
>
> Propagate CPA_FLUSHTLB from the alias update to the original request, and
> flush the entire TLB only when an alias actually changed.
>
> Fixes: ab5130186d74 ("x86/mm/cpa: Flush direct map alias during cpa")

I think this^ only added a flush where there wasn't one before, fixing a
different issue. I don't think it is the origination of this behavior for the
case of when the primary target already has the right permission, but not the
alias.

BTW does this type of permission change happen anywhere in the kernel, where the
primary and alias start mismatched? How frequently does it happen?

> Signed-off-by: Mike Rapoport (Microsoft) <rppt@xxxxxxxxxx>