[RFC PATCH 6/6] arm64: mm: support PMD page coalescing in the linear map

Adrian Barnaś abarnas at google.com
Mon Nov 23 01:47:21 PST 2026


On Mon, Jul 06, 2026 at 05:33:50PM +0300, Mike Rapoport wrote:
>On Mon, Jul 06, 2026 at 12:41:29PM +0000, Adrian Barnaś wrote:
>> Hi Mike,
>>
>> On Fri, Jun 19, 2026 at 02:40:40PM +0100, Ryan Roberts wrote:
>>
>> > I don't think this is safe in general. Let's say we have a 2M region split into
>> > 512 x 4K PTEs. It's possible that the first 1M is one object and the second 1M
>> > is another object. Different CPUs could set_memory_*() on those 2 objects
>> > concurrently. If one of them then calls this function, we could end up
>> > collapsing the whole 2M while the other is trying to modify the PTEs and they
>> > will race.
>> >
>> > Note that splitting _is_ safe (and protected by this lock) because you'd have 2
>> > objects backed by the same PMD, so they would both have to split before
>> > modifying the PTEs.
>> >
>> > I think you'd need to ensure mutual exclusion at a higher level if doing this;
>> > probably execmem is the place that can ensure that no objects within a 2M region
>> > are racily trying to modify their permissions?
>> >
>> > Thanks,
>> > Ryan
>>
>> I wonder what do you think about enforcing mutual exclusion for permission
>> changes to the ROX_CACHE block as Ryan sugest?
>>
>> Would it be appropriate to add locks (over `&execmem_cache.mutex`) to
>> `execmem_force_rw` and `execmem_restore_rox`?
>>                                                                             I
>> see that x86 has a lock that wraps entire process of updating attributes
>> (including spliting and collapsing pages):
>> ```
>> spin_lock(&cpa_lock);
>> ret = __change_page_attr(cpa, primary);
>> spin_unlock(&cpa_lock);                                            ```
>> As I understand it makes it safe already.
>> However, on ARM64 there is a split lock (which works fine for spliting) but
>> for collapsing at least on my implementation it is not sufficient as
>> Ryan pointed out.
>
>I think the lock should be at set_memory level so that set_memory callers
>shouldn't care about the differences in arch implementations.
>
>I also suspect that the lockless page table walk in update_range_prot()
>isn't safe today.
>
>> Best regards
>> Adrian
>
>-- 
>Sincerely yours,
>Mike.

Hi Mike

I have recently got back to the topic, and I have one thing to clarify.

Currently the collapsing mechanism that is backing execmem cache, 
following execmem_restore_rox() description

```
   * Restores read-only-execute permissions on a range [@ptr, @ptr + @size)
   * after it was temporarily remapped as writable. Relies on architecture
   * implementation of set_memory_rox() to restore mapping using large pages.

```
is driven entirely by the architecture code. It has drawbacks, that could 
be addresed by passing some of the responsibility to memory owner,
execmem rox cache in this case.

Arch code at no point is aware if the piece of memory it makes RW/RO, 
is ment to be collapsed. Every permission change can cause entire block
to be collapsable so we need to try collapse on every run. We can probably
make RCU-like mechanism to batch those events but still there is a lot
of pointless runs just to find out we can't collapse or collapse of memory
block that already have split operation waiting for lock release.

I was wondering if it would be acceptable to create a try_collapse operation
in set_memory api that would tell the arch code "I will probably not touch
this anymore for a while, you can collapse if you want". And call it 
after predifined delay from last execmem_cache_restore_rox call.

 From execmem side it can be based on the same delayed work mechanism that 
execmem_cache_free is using. 

I have a PoC locally but it is mostly vibecoded and I have already 
discovered some bugs in it. But conceptualy it worked. After a batch load
of modules it collapsed all execmem cache blocks in both direct map and 
vmalloc in a single run. 

Before I craft a proper patch set I want to ask if it is in any way
acceptable approach? 

Best regards
Adrian



More information about the linux-arm-kernel mailing list