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

From: Mike Rapoport

Date: Wed Nov 25 2026 - 04:00:11 EST


On Tue, Nov 24, 2026 at 10:49:28PM +0000, Edgecombe, Rick P wrote:
> 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.

Right, it was c31c7d4844ea ("x86: CPA, fix alias checks") that moved the
alias update into a copy and from thenn the alias's flag never got back to
the primary request.

ab5130186d74 only adds the unnecessary flush when alias does not change.

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

Like the cover letter said it's "unlikely to happen in practice" :)

Currently there are no callers in the tree that could trigger that.

I'm working the generic set_memory_core [1] and the bots keep yelling at me
whenever I touch anything around.

So while is a defense against potential future issues, the change is simple
and I think it's better to have it.

Might also save us from reviewing several LLM generated reports that would
find the same thing ;-)

[1] https://lore.kernel.org/all/20260721-generic-set-memory-v0-1-v1-0-2c1fc62306b3@xxxxxxxxxx

--
Sincerely yours,
Mike.