Skip to contentSkip to conversation

Per-process page size

LSF/MM/BPF TOPIC Dev Jain 10 participants 23 messages Feb 17 – Mar 25 Memory management

lore
Share & tools
lore

Dev Jain

author
#0 lore raw

Hi everyone,

We propose per-process page size on arm64. Although the proposal is for arm64, perhaps the concept can be extended to other arches, thus the generic topic name.

-------------
INTRODUCTION
-------------
While mTHP has brought the performance of many workloads running on an arm64 4K kernel closer to that of the performance on an arm64 64K kernel, a performance gap still remains. This is attributed to a combination of greater number of pgtable levels, less reach within the walk cache and higher data cache footprint for pgtable memory. At the same time, 64K is not suitable for general purpose environments due to it's significantly higher memory footprint.

To solve this, we have been experimenting with a concept called "per-process page size". This breaks the historic assumption of a single page size for the entire system: a process will now operate on a page size ABI that is greater than or equal to the kernel's page size. This is enabled by a key architectural feature on Arm: the separation of user and kernel page tables.

This can also lead to a future of a single kernel image instead of 4K, 16K and 64K images.

--------------
CURRENT DESIGN
--------------
The design is based on one core idea; most of the kernel continues to believe there is only one page size in use across the whole system. That page size is the size selected at compile-time, as is done today. But every process (more accurately mm_struct) has a page size ABI which is one of the 3 page sizes (4K, 16K or 64K) as long as that page size is greater than or equal to the kernel page size (kernel page size is the macro PAGE_SIZE).

Pagesize selection
------------------
A process' selected page size ABI comes into force at execve() time and remains fixed until the process exits or until the next execve(). Any forked processes inherit the page size of their parent.
The personality() mechanism already exists for similar cases, so we propose to extend it to enable specifying the required page size.

There are 3 layers to the design. The first two are not arch-dependent, and makes Linux support a per-process pagesize ABI. The last layer is arch-specific.

1. ABI adapter
--------------
A translation layer is added at the syscall boundary to convert between the process page size and the kernel page size. This effectively means enforcing alignment requirements for addresses passed to syscalls and ensuring that quantities passed as “number of pages” are interpreted relative to the process page size and not the kernel page size. In this way the process has the illusion that it is working in units of its page size, but the kernel is working in units of the kernel page size.

2. Generic Linux MM enlightenment
---------------------------------
We enlighten the Linux MM code to always hand out memory in the granularity of process pages. Most of this work is greatly simplified because of the existing mTHP allocation paths, and the ongoing support for large folios across different areas of the kernel. The process order will be used as the hard minimum mTHP order to allocate.

File memory
-----------
For a growing list of compliant file systems, large folios can already be stored in the page cache. There is even a mechanism, introduced to support filesystems with block sizes larger than the system page size, to set a hard-minimum size for folios on a per-address-space basis. This mechanism will be reused and extended to service the per-process page size requirements.

One key reason that the 64K kernel currently consumes considerably more memory than the 4K kernel is that Linux systems often have lots of small configuration files which each require a page in the page cache. But these small files are (likely) only used by certain processes. So, we prefer to continue to cache those using a 4K page.
Therefore, if a process with a larger page size maps a file whose pagecache contains smaller folios, we drop them and re-read the range with a folio order at least that of the process order.

3. Translation from Linux pagetable to native pagetable
-------------------------------------------------------
Assume the case of a kernel pagesize of 4K and app pagesize of 64K. Now that enlightenment is done, it is guaranteed that every single mapping in the 4K pagetable (which we call the Linux pagetable) is of granularity at least 64K. In the arm64 MM code, we maintain a "native" pagetable per mm_struct, which is based off a 64K geometry. Because of the guarantee aforementioned, any pagetable operation on the Linux pagetable (set_ptes, clear_flush_ptes, modify_prot_start_ptes, etc) is going to happen at a granularity of at least 16 PTEs - therefore we can translate this operation to modify a single PTE entry in the native pagetable. Given that enlightenment may miss corner cases, we insert a warning in the architecture code - on being presented with an operation not translatable into a native operation, we fallback to the Linux pagetable, thus losing the benefits borne out of the pagetable geometry but keeping
the emulation intact.

-----------------------
What we want to discuss
-----------------------
- Are there other arches which could benefit from this?
- What level of compatibility we can achieve - is it even possible to contain userspace within the emulated ABI?
- Rough edges of compatibility layer - pfnmaps, ksm, procfs, etc. For example, what happens when a 64K process opens a procfs file of a 4K process?
- native pgtable implementation - perhaps inspiration can be taken from other arches with an involved pgtable logic (ppc, s390)?

-------------
Key Attendees
-------------
- Ryan Roberts (co-presenter)
- mm folks (David Hildenbrand, Matthew Wilcox, Liam Howlett, Lorenzo Stoakes, and many others)
- arch folks

#1 lore raw
Quoted 3 lines · Dev Jain

2. Generic Linux MM enlightenment
---------------------------------
We enlighten the Linux MM code to always hand out memory in the granularity

Please don't use the term "enlighten". Tht's used to describe something something or other with hypervisors. Come up with a new term or use one that already exists.

Quoted 8 lines

File memory
-----------
For a growing list of compliant file systems, large folios can already be stored in the page cache. There is even a mechanism, introduced to support filesystems with block sizes larger than the system page size, to set a hard-minimum size for folios on a per-address-space basis. This mechanism will be reused and extended to service the per-process page size requirements.

Quoted 8 lines

One key reason that the 64K kernel currently consumes considerably more memory than the 4K kernel is that Linux systems often have lots of small configuration files which each require a page in the page cache. But these small files are (likely) only used by certain processes. So, we prefer to continue to cache those using a 4K page.
Therefore, if a process with a larger page size maps a file whose pagecache contains smaller folios, we drop them and re-read the range with a folio order at least that of the process order.

That's going to be messy. I don't have a good idea for solving this problem, but the page cache really isn't set up to change minimum folio order while the inode is in use.

Quoted 1 line

- Are there other arches which could benefit from this?

Some architectures walk the page tables entirely in software, but on the other hand, those tend to be, er, "legacy" architectures these days and it's doubtful that anybody would invest in adding support.

Sounds like a good question for Arnd ;-)

Quoted 7 lines

- What level of compatibility we can achieve - is it even possible to contain userspace within the emulated ABI?
- Rough edges of compatibility layer - pfnmaps, ksm, procfs, etc. For example, what happens when a 64K process opens a procfs file of a 4K process?
- native pgtable implementation - perhaps inspiration can be taken from other arches with an involved pgtable logic (ppc, s390)?

I question who decides what page size a particular process will use. The programmer? The sysadmin? It seems too disruptive for the kernel to monitor and decide for the app what page size it will use.

#2 lore raw
Quoted 26 lines · Matthew Wilcox
Quoted 3 lines · Dev Jain

2. Generic Linux MM enlightenment
---------------------------------
We enlighten the Linux MM code to always hand out memory in the granularity

Please don't use the term "enlighten". Tht's used to describe something something or other with hypervisors. Come up with a new term or use one that already exists.

Quoted 8 lines

File memory
-----------
For a growing list of compliant file systems, large folios can already be stored in the page cache. There is even a mechanism, introduced to support filesystems with block sizes larger than the system page size, to set a hard-minimum size for folios on a per-address-space basis. This mechanism will be reused and extended to service the per-process page size requirements.

Quoted 8 lines

One key reason that the 64K kernel currently consumes considerably more memory than the 4K kernel is that Linux systems often have lots of small configuration files which each require a page in the page cache. But these small files are (likely) only used by certain processes. So, we prefer to continue to cache those using a 4K page.
Therefore, if a process with a larger page size maps a file whose pagecache contains smaller folios, we drop them and re-read the range with a folio order at least that of the process order.

Quoted 3 lines

That's going to be messy. I don't have a good idea for solving this problem, but the page cache really isn't set up to change minimum folio order while the inode is in use.

In a private conversation I also raised that some situations might make it impossible/hard to drop+re-read.

One example I cam up with if a folio is simply long-term R/O pinned. But I am also not quite sure how mlock might interfere here.

So yes, I think the page cache is likely the one of the most problematic/messy thing to handle.

Signature · 3 lines
Cheers,

David
#3 lore raw
Quoted 27 lines · David Hildenbrand (Arm)
Quoted 26 lines · Matthew Wilcox
Quoted 3 lines · Dev Jain

2. Generic Linux MM enlightenment
---------------------------------
We enlighten the Linux MM code to always hand out memory in the granularity

Please don't use the term "enlighten".  Tht's used to describe something something or other with hypervisors.  Come up with a new term or use one that already exists.

Quoted 16 lines

File memory
-----------
For a growing list of compliant file systems, large folios can already be stored in the page cache. There is even a mechanism, introduced to support filesystems with block sizes larger than the system page size, to set a hard-minimum size for folios on a per-address-space basis. This mechanism will be reused and extended to service the per-process page size requirements.

One key reason that the 64K kernel currently consumes considerably more memory than the 4K kernel is that Linux systems often have lots of small configuration files which each require a page in the page cache. But these small files are (likely) only used by certain processes. So, we prefer to continue to cache those using a 4K page.
Therefore, if a process with a larger page size maps a file whose pagecache contains smaller folios, we drop them and re-read the range with a folio order at least that of the process order.

Quoted 3 lines
Quoted 3 lines

That's going to be messy.  I don't have a good idea for solving this problem, but the page cache really isn't set up to change minimum folio order while the inode is in use.

Dev has a prototype up and running, but based on your comments, I'm guessing there is some horrible race that hasn't hit yet. Would be good to debug the gap in understanding at some point!

Quoted 10 lines

In a private conversation I also raised that some situations might make it impossible/hard to drop+re-read.

One example I cam up with if a folio is simply long-term R/O pinned. But I am also not quite sure how mlock might interfere here.

So yes, I think the page cache is likely the one of the most problematic/messy thing to handle.

I guess we could side step the problem for now, by initially requiring that the minimum folio size always be the maximum supported process page size. That would allow us to get something up and running at least. But then we lose the memory saving benefits.

Of course, I'm conveniently ignoring that not all filesystems support large folios, but perhaps we could do a generic fallback adapter with a bounce buffer for that case?

author
#8 lore raw
In reply to Matthew Wilcox
Quoted 7 lines · Matthew Wilcox
Quoted 3 lines · Dev Jain

2. Generic Linux MM enlightenment
---------------------------------
We enlighten the Linux MM code to always hand out memory in the granularity

Please don't use the term "enlighten". Tht's used to describe something something or other with hypervisors. Come up with a new term or use one that already exists.

Sure.

Quoted 17 lines
Quoted 9 lines

File memory
-----------
For a growing list of compliant file systems, large folios can already be stored in the page cache. There is even a mechanism, introduced to support filesystems with block sizes larger than the system page size, to set a hard-minimum size for folios on a per-address-space basis. This mechanism will be reused and extended to service the per-process page size requirements.

Quoted 8 lines

One key reason that the 64K kernel currently consumes considerably more memory than the 4K kernel is that Linux systems often have lots of small configuration files which each require a page in the page cache. But these small files are (likely) only used by certain processes. So, we prefer to continue to cache those using a 4K page.
Therefore, if a process with a larger page size maps a file whose pagecache contains smaller folios, we drop them and re-read the range with a folio order at least that of the process order.

Quoted 3 lines

That's going to be messy. I don't have a good idea for solving this problem, but the page cache really isn't set up to change minimum folio order while the inode is in use.

Holding mapping->invalidate_lock, bumping mapping->min_folio_order and dropping-rereading the range suffers from a race - filemap_fault operating on some other partially populated 64K range will observe in filemap_get_folio that nothing is in the pagecache. Then, it will read the updated min_order in __filemap_get_folio, then use filemap_add_folio to add a 64K folio, but since the 64K range is partially populated, we get stuck in an infinite loop due to -EEXIST.

So I figured that deleting the entire pagecache is simpler. We will also bail out early in __filemap_add_folio if the folio order asked by the caller to create is less than mapping_min_folio_order. Eventually the caller is going to read the correct min order. This algorithm avoids the race above, however...

my assumption here was that we are synchronized on mapping->invalidate_lock. The kerneldoc above read_cache_folio() and some other comments convinced me of that, but I just checked with a VM_WARN_ON(!is_rwsem_locked()) in __filemap_add_folio and this doesn't seem to be the case for all code paths... If the algorithm sounds reasonable, I wonder what is the correct synchronization mechanism here.

Quoted 15 lines
Quoted 2 lines

- Are there other arches which could benefit from this?

Some architectures walk the page tables entirely in software, but on the other hand, those tend to be, er, "legacy" architectures these days and it's doubtful that anybody would invest in adding support.

Sounds like a good question for Arnd ;-)

Quoted 7 lines

- What level of compatibility we can achieve - is it even possible to contain userspace within the emulated ABI?
- Rough edges of compatibility layer - pfnmaps, ksm, procfs, etc. For example, what happens when a 64K process opens a procfs file of a 4K process?
- native pgtable implementation - perhaps inspiration can be taken from other arches with an involved pgtable logic (ppc, s390)?

Quoted 3 lines

I question who decides what page size a particular process will use. The programmer? The sysadmin? It seems too disruptive for the kernel to monitor and decide for the app what page size it will use.

It's the sysadmin. The latter method you mention is similar to the problem of the kernel choosing the correct mTHP order, which we don't have an elegant idea for solving yet.

author
#9 lore raw
Quoted 41 lines · Dev Jain
Quoted 7 lines · Matthew Wilcox
Quoted 3 lines · Dev Jain

2. Generic Linux MM enlightenment
---------------------------------
We enlighten the Linux MM code to always hand out memory in the granularity

Please don't use the term "enlighten". Tht's used to describe something something or other with hypervisors. Come up with a new term or use one that already exists.

Sure.

Quoted 16 lines
Quoted 8 lines

File memory
-----------
For a growing list of compliant file systems, large folios can already be stored in the page cache. There is even a mechanism, introduced to support filesystems with block sizes larger than the system page size, to set a hard-minimum size for folios on a per-address-space basis. This mechanism will be reused and extended to service the per-process page size requirements.

Quoted 8 lines

One key reason that the 64K kernel currently consumes considerably more memory than the 4K kernel is that Linux systems often have lots of small configuration files which each require a page in the page cache. But these small files are (likely) only used by certain processes. So, we prefer to continue to cache those using a 4K page.
Therefore, if a process with a larger page size maps a file whose pagecache contains smaller folios, we drop them and re-read the range with a folio order at least that of the process order.

Quoted 3 lines

That's going to be messy. I don't have a good idea for solving this problem, but the page cache really isn't set up to change minimum folio order while the inode is in use.

Holding mapping->invalidate_lock, bumping mapping->min_folio_order and dropping-rereading the range suffers from a race - filemap_fault operating on some other partially populated 64K range will observe in filemap_get_folio that nothing is in the pagecache. Then, it will read the updated min_order in __filemap_get_folio, then use filemap_add_folio to add a 64K folio, but since the 64K range is partially populated, we get stuck in an infinite loop due to -EEXIST.

So I figured that deleting the entire pagecache is simpler. We will also bail out early in __filemap_add_folio if the folio order asked by the caller to create is less than mapping_min_folio_order. Eventually the caller is going to read the correct min order. This algorithm avoids the race above, however...

Quoted 6 lines

my assumption here was that we are synchronized on mapping->invalidate_lock. The kerneldoc above read_cache_folio() and some other comments convinced me of that, but I just checked with a VM_WARN_ON(!is_rwsem_locked()) in __filemap_add_folio and this doesn't seem to be the case for all code paths... If the algorithm sounds reasonable, I wonder what is the correct synchronization mechanism here.

I may have been vague here... to avoid the race I described above, we must ensure that after all folios have been dropped from pagecache, and min order is bumped up, no other code path remembers the old order and partially populates a 64K range. For this we need synchronization.

Quoted 23 lines
Quoted 15 lines
Quoted 2 lines

- Are there other arches which could benefit from this?

Some architectures walk the page tables entirely in software, but on the other hand, those tend to be, er, "legacy" architectures these days and it's doubtful that anybody would invest in adding support.

Sounds like a good question for Arnd ;-)

Quoted 7 lines

- What level of compatibility we can achieve - is it even possible to contain userspace within the emulated ABI?
- Rough edges of compatibility layer - pfnmaps, ksm, procfs, etc. For example, what happens when a 64K process opens a procfs file of a 4K process?
- native pgtable implementation - perhaps inspiration can be taken from other arches with an involved pgtable logic (ppc, s390)?

Quoted 3 lines

I question who decides what page size a particular process will use. The programmer? The sysadmin? It seems too disruptive for the kernel to monitor and decide for the app what page size it will use.

It's the sysadmin. The latter method you mention is similar to the problem of the kernel choosing the correct mTHP order, which we don't have an elegant idea for solving yet.

#10 lore raw
Quoted 30 lines · Dev Jain
Quoted 21 lines · Dev Jain
Quoted 3 lines · Matthew Wilcox

Please don't use the term "enlighten". Tht's used to describe something something or other with hypervisors. Come up with a new term or use one that already exists.

Sure.

Quoted 3 lines

That's going to be messy. I don't have a good idea for solving this problem, but the page cache really isn't set up to change minimum folio order while the inode is in use.

Holding mapping->invalidate_lock, bumping mapping->min_folio_order and dropping-rereading the range suffers from a race - filemap_fault operating on some other partially populated 64K range will observe in filemap_get_folio that nothing is in the pagecache. Then, it will read the updated min_order in __filemap_get_folio, then use filemap_add_folio to add a 64K folio, but since the 64K range is partially populated, we get stuck in an infinite loop due to -EEXIST.

So I figured that deleting the entire pagecache is simpler. We will also bail out early in __filemap_add_folio if the folio order asked by the caller to create is less than mapping_min_folio_order. Eventually the caller is going to read the correct min order. This algorithm avoids the race above, however...

Quoted 6 lines

my assumption here was that we are synchronized on mapping->invalidate_lock. The kerneldoc above read_cache_folio() and some other comments convinced me of that, but I just checked with a VM_WARN_ON(!is_rwsem_locked()) in __filemap_add_folio and this doesn't seem to be the case for all code paths... If the algorithm sounds reasonable, I wonder what is the correct synchronization mechanism here.

Quoted 4 lines

I may have been vague here... to avoid the race I described above, we must ensure that after all folios have been dropped from pagecache, and min order is bumped up, no other code path remembers the old order and partially populates a 64K range. For this we need synchronization.

And I don't think you can reliably do that when other processes might be using the files concurrently.

It's best to start like Ryan suggested: lifting min_order on these systems for now and leaving dynamically switching the min order as future work.

Signature · 3 lines
Cheers,

David
#4 lore raw
In reply to David Hildenbrand (Arm)
Quoted 8 lines · David Hildenbrand (Arm)

In a private conversation I also raised that some situations might make it impossible/hard to drop+re-read.

One example I cam up with if a folio is simply long-term R/O pinned. But I am also not quite sure how mlock might interfere here.

So yes, I think the page cache is likely the one of the most problematic/messy thing to handle.

So what if we convert to max-supported-order the first time somebody calls mmap on a given file? Most files are never mmaped, so it won't affect them. And files that are mmaped are generally not written to. So there should not be much in the page cache for the common case. And if no pages from the file have been mmaped yet, they cannot be pinned or mlocked.

#11 lore raw
In reply to Matthew Wilcox
Quoted 9 lines · Matthew Wilcox
Quoted 2 lines · Dev Jain

- Are there other arches which could benefit from this?

Some architectures walk the page tables entirely in software, but on the other hand, those tend to be, er, "legacy" architectures these days and it's doubtful that anybody would invest in adding support.

Sounds like a good question for Arnd ;-)

I think Loongarch and RISC-V are the candidates for doing whatever Arm does here. MIPS and PowerPC64 could do it in theory, but it's less clear that someone will spend the effort here.

Quoted 3 lines
Quoted 3 lines

- Rough edges of compatibility layer - pfnmaps, ksm, procfs, etc. For example, what happens when a 64K process opens a procfs file of a 4K process?

This would also be my main concern. There are hundreds of device drivers that implement a custom .mmap() file operation, and a few dozen file systems, all of which need to be audited and likely changed to allow mapping larger granules.

Quoted 5 lines
Quoted 2 lines

- native pgtable implementation - perhaps inspiration can be taken from other arches with an involved pgtable logic (ppc, s390)?

I question who decides what page size a particular process will use. The programmer? The sysadmin?

I would expect this to be done by a combination of these two, it seems simple enough to have a wrapper like numactl or setarch to start an application one way or another.

Another concern I have is for the actual performance trade-offs here. As I understand it, the idea is to have most of the memory size advantages of a 4KB page kernel, and most of the performance advantages of a 64KB page kernel for the special applications that care about this. However, the same is true for 16KB page kernel, which also aims for the same trade-off with a much simpler model and a different set of compatibility problems.

Do we expect per-process page size kernels to actually be better than fixed 16KB page kernels, and better enough that it's worth the added complexity? In particular, this approach would likely only get the advantages of the TLB but not the file systems using larger pages, while also suffering from the extra overhead of compacting smaller pages in order to map them.

Arnd

#12 lore raw
In reply to Dev Jain
Quoted 6 lines · Dev Jain

Hi everyone,

We propose per-process page size on arm64. Although the proposal is for arm64, perhaps the concept can be extended to other arches, thus the generic topic name.

Quoted 9 lines

-------------
INTRODUCTION
-------------
While mTHP has brought the performance of many workloads running on an arm64 4K kernel closer to that of the performance on an arm64 64K kernel, a performance gap still remains. This is attributed to a combination of greater number of pgtable levels, less reach within the walk cache and higher data cache footprint for pgtable memory. At the same time, 64K is not suitable for general purpose environments due to it's significantly higher memory footprint.

Could this perhaps be because of larger-page-size kernels being able to use mTHP (and THP) more aggressively? It would be interesting to compare arm64 "4K" vs "4K with mTHP" vs "4K with _only_ mTHP" vs "64K" vs "64K with mTHP".

Quoted 67 lines

To solve this, we have been experimenting with a concept called "per-process page size". This breaks the historic assumption of a single page size for the entire system: a process will now operate on a page size ABI that is greater than or equal to the kernel's page size. This is enabled by a key architectural feature on Arm: the separation of user and kernel page tables.

This can also lead to a future of a single kernel image instead of 4K, 16K and 64K images.

--------------
CURRENT DESIGN
--------------
The design is based on one core idea; most of the kernel continues to believe there is only one page size in use across the whole system. That page size is the size selected at compile-time, as is done today. But every process (more accurately mm_struct) has a page size ABI which is one of the 3 page sizes (4K, 16K or 64K) as long as that page size is greater than or equal to the kernel page size (kernel page size is the macro PAGE_SIZE).

Pagesize selection
------------------
A process' selected page size ABI comes into force at execve() time and remains fixed until the process exits or until the next execve(). Any forked processes inherit the page size of their parent.
The personality() mechanism already exists for similar cases, so we propose to extend it to enable specifying the required page size.

There are 3 layers to the design. The first two are not arch-dependent, and makes Linux support a per-process pagesize ABI. The last layer is arch-specific.

1. ABI adapter
--------------
A translation layer is added at the syscall boundary to convert between the process page size and the kernel page size. This effectively means enforcing alignment requirements for addresses passed to syscalls and ensuring that quantities passed as “number of pages” are interpreted relative to the process page size and not the kernel page size. In this way the process has the illusion that it is working in units of its page size, but the kernel is working in units of the kernel page size.

2. Generic Linux MM enlightenment
---------------------------------
We enlighten the Linux MM code to always hand out memory in the granularity of process pages. Most of this work is greatly simplified because of the existing mTHP allocation paths, and the ongoing support for large folios across different areas of the kernel. The process order will be used as the hard minimum mTHP order to allocate.

File memory
-----------
For a growing list of compliant file systems, large folios can already be stored in the page cache. There is even a mechanism, introduced to support filesystems with block sizes larger than the system page size, to set a hard-minimum size for folios on a per-address-space basis. This mechanism will be reused and extended to service the per-process page size requirements.

One key reason that the 64K kernel currently consumes considerably more memory than the 4K kernel is that Linux systems often have lots of small configuration files which each require a page in the page cache. But these small files are (likely) only used by certain processes. So, we prefer to continue to cache those using a 4K page.
Therefore, if a process with a larger page size maps a file whose pagecache contains smaller folios, we drop them and re-read the range with a folio order at least that of the process order.

Quoted 16 lines

3. Translation from Linux pagetable to native pagetable
-------------------------------------------------------
Assume the case of a kernel pagesize of 4K and app pagesize of 64K. Now that enlightenment is done, it is guaranteed that every single mapping in the 4K pagetable (which we call the Linux pagetable) is of granularity at least 64K. In the arm64 MM code, we maintain a "native" pagetable per mm_struct, which is based off a 64K geometry. Because of the guarantee aforementioned, any pagetable operation on the Linux pagetable (set_ptes, clear_flush_ptes, modify_prot_start_ptes, etc) is going to happen at a granularity of at least 16 PTEs - therefore we can translate this operation to modify a single PTE entry in the native pagetable. Given that enlightenment may miss corner cases, we insert a warning in the architecture code - on being presented with an operation not translatable into a native operation, we fallback to the Linux pagetable, thus losing the benefits borne out of the pagetable geometry but keeping
the emulation intact.

I don't understand. What exactly are you trying to do here? Maintain 2 different paging structures, one for core mm and the other for the arch? As done in architectures with no radix tree paging structures?

If so, that's wildly inefficient, unless you're willing to go into reclaimable page tables on the arm64 side. And that brings extra problems and extra fun :)

Signature · 1 line
Pedro
#5 lore raw
In reply to Matthew Wilcox
Quoted 7 lines · Matthew Wilcox
Quoted 6 lines · David Hildenbrand (Arm)

In a private conversation I also raised that some situations might make it impossible/hard to drop+re-read.

One example I cam up with if a folio is simply long-term R/O pinned. But I am also not quite sure how mlock might interfere here.

Quoted 6 lines
Quoted 2 lines

So yes, I think the page cache is likely the one of the most problematic/messy thing to handle.

So what if we convert to max-supported-order the first time somebody calls mmap on a given file? Most files are never mmaped, so it won't affect them.

Yes!

Quoted 1 line

And files that are mmaped are generally not written to.

Well, let's say many mmaped files are not written to. :)

Quoted 1 line

So there should not be much in the page cache for the common case.

You'd assume many files to either get mmaped or read/written, yes.

Quoted 2 lines

And if no pages from the file have been mmaped yet, they cannot be pinned or mlocked.

Is there some other way for someone to block a page from getting evicted from the pagecache?

We have this memfd_pin_folios() thing, but I don't think we have something comparable for ordinary pagecache files.

... putting them into a pipe and never reading from the pipe maybe (I assume that's what splice() does, but not sure if it actually places the pages in there or whether it creates a copy first)?

But yes, doing the conversion on first mmap() should handle mlock+gup.

Signature · 3 lines
Cheers,

David
author
#13 lore raw
In reply to Pedro Falcato
Quoted 17 lines
Quoted 17 lines

3. Translation from Linux pagetable to native pagetable
-------------------------------------------------------
Assume the case of a kernel pagesize of 4K and app pagesize of 64K. Now that enlightenment is done, it is guaranteed that every single mapping in the 4K pagetable (which we call the Linux pagetable) is of granularity at least 64K. In the arm64 MM code, we maintain a "native" pagetable per mm_struct, which is based off a 64K geometry. Because of the guarantee aforementioned, any pagetable operation on the Linux pagetable (set_ptes, clear_flush_ptes, modify_prot_start_ptes, etc) is going to happen at a granularity of at least 16 PTEs - therefore we can translate this operation to modify a single PTE entry in the native pagetable. Given that enlightenment may miss corner cases, we insert a warning in the architecture code - on being presented with an operation not translatable into a native operation, we fallback to the Linux pagetable, thus losing the benefits borne out of the pagetable geometry but keeping
the emulation intact.

Quoted 3 lines

I don't understand. What exactly are you trying to do here? Maintain 2 different paging structures, one for core mm and the other for the arch? As done in architectures with no radix tree paging structures?

The mm->pgd will be the software pagetable. So suppose that do_anonymous_page is doing set_ptes on the PTE table belonging to the software pagetable. We will hook a "native_set_ptes" into set_ptes, which will set the ptes on a different pagetable maintained by arm64 code (probably mm_context_t->native_pgd).

Quoted 3 lines

If so, that's wildly inefficient, unless you're willing to go into reclaimable page tables on the arm64 side. And that brings extra problems and extra fun :)

I didn't understand the reclaimable reference, but yes we need to make this efficient. So for the above example I gave, native_set_ptes knows the virtual address to set - walking the native hierarchy from native_pgd->native_pmd->native_pte (in case of 64K native geometry) is inefficient. So we need to maintain a lookup mechanism from a linux pgtable pointer to the native pgtable pointer.
The idea we have currently is to store such lookup in the struct ptdesc of the pagetable page. For 4K Linux pagetable and 64K native pagetable, 512M/2M = 256 Linux PTE tables correspond to different sections of the native PTE table. We will maintain the pointer to the relevant section in the native PTE table, in the struct ptdesc of the pagetable page of the Linux PTE table.
The other case is that a single Linux pgtable leaf entry corresponds to multiple native leaf entries - take the case of a Linux PMD table which maps 1G of memory, this corresponds to 2 native PTE tables (2 x 512M). We will have to store a list of pointers here.

Quoted 1 line
#14 lore raw
Quoted 17 lines · Dev Jain
Quoted 17 lines
Quoted 17 lines

3. Translation from Linux pagetable to native pagetable
-------------------------------------------------------
Assume the case of a kernel pagesize of 4K and app pagesize of 64K. Now that enlightenment is done, it is guaranteed that every single mapping in the 4K pagetable (which we call the Linux pagetable) is of granularity at least 64K. In the arm64 MM code, we maintain a "native" pagetable per mm_struct, which is based off a 64K geometry. Because of the guarantee aforementioned, any pagetable operation on the Linux pagetable (set_ptes, clear_flush_ptes, modify_prot_start_ptes, etc) is going to happen at a granularity of at least 16 PTEs - therefore we can translate this operation to modify a single PTE entry in the native pagetable. Given that enlightenment may miss corner cases, we insert a warning in the architecture code - on being presented with an operation not translatable into a native operation, we fallback to the Linux pagetable, thus losing the benefits borne out of the pagetable geometry but keeping
the emulation intact.

Quoted 8 lines
Quoted 3 lines

I don't understand. What exactly are you trying to do here? Maintain 2 different paging structures, one for core mm and the other for the arch? As done in architectures with no radix tree paging structures?

The mm->pgd will be the software pagetable. So suppose that do_anonymous_page is doing set_ptes on the PTE table belonging to the software pagetable. We will hook a "native_set_ptes" into set_ptes, which will set the ptes on a different pagetable maintained by arm64 code (probably mm_context_t->native_pgd).

Traditionally, you do this kind of funky manipulation in update_mmu_cache.

But this is still an extremely complex and invasive change (that I assume most people would not like to see) with dubious benefit.

Quoted 6 lines
Quoted 4 lines

If so, that's wildly inefficient, unless you're willing to go into reclaimable page tables on the arm64 side. And that brings extra problems and extra fun :)

I didn't understand the reclaimable reference, but yes we need to make this efficient.

I'm not talking about CPU runtime efficiency, but memory efficiency. Doing this makes you essentially duplicate page tables - not exactly ideal. This is a Known Problem in classic UNIX systems which do something similar (but not the same): anonymous memory pointers are stored in some intermediary structure (SunOS and UVM call it "amap"), and paging structures are entirely redundant there. They can freely tear down a page table because they can freely put it together from the amap and file mappings (what they call vm_object and we call address_space).

Anyway, I'm boring you with these funny historical details so you can understand the similarities: the Linux page table format generally matches hardware, and we store anonymous memory "state" there, so you can't ever tear-down a pgtable without losing state of whatever was mapped there before. However, if you go down the "arm64 now has a separate pgtable structure", the roles switch:
arm64's internal page table format makes for the real page tables, and linux's pgtable structure is nothing more than an "amap". So you could (and perhaps should) freely reclaim arm64 MMU page tables once memory pressure hits, because they are freely discardable.

Does this make sense?

Signature · 1 line
Pedro
#15 lore raw
Quoted 41 lines · Pedro Falcato
Quoted 8 lines · Dev Jain
Quoted 3 lines

I don't understand. What exactly are you trying to do here? Maintain 2 different paging structures, one for core mm and the other for the arch? As done in architectures with no radix tree paging structures?

The mm->pgd will be the software pagetable. So suppose that do_anonymous_page is doing set_ptes on the PTE table belonging to the software pagetable. We will hook a "native_set_ptes" into set_ptes, which will set the ptes on a different pagetable maintained by arm64 code (probably mm_context_t->native_pgd).

Traditionally, you do this kind of funky manipulation in update_mmu_cache.

But this is still an extremely complex and invasive change (that I assume most people would not like to see) with dubious benefit.

Quoted 6 lines
Quoted 4 lines

If so, that's wildly inefficient, unless you're willing to go into reclaimable page tables on the arm64 side. And that brings extra problems and extra fun :)

I didn't understand the reclaimable reference, but yes we need to make this efficient.

I'm not talking about CPU runtime efficiency, but memory efficiency. Doing this makes you essentially duplicate page tables - not exactly ideal. This is a Known Problem in classic UNIX systems which do something similar (but not the same): anonymous memory pointers are stored in some intermediary structure (SunOS and UVM call it "amap"), and paging structures are entirely redundant there. They can freely tear down a page table because they can freely put it together from the amap and file mappings (what they call vm_object and we call address_space).

Anyway, I'm boring you with these funny historical details so you can understand the similarities: the Linux page table format generally matches hardware, and we store anonymous memory "state" there, so you can't ever tear-down a pgtable without losing state of whatever was mapped there before. However, if you go down the "arm64 now has a separate pgtable structure", the roles switch:
arm64's internal page table format makes for the real page tables, and linux's pgtable structure is nothing more than an "amap". So you could (and perhaps should) freely reclaim arm64 MMU page tables once memory pressure hits, because they are freely discardable.

Quoted 1 line

Does this make sense?

I've been thinking about building the 64k page tables similar to how HMM/KVM handles it, invalidating them through mmu notifiers etc and building them on demand.

Considering the 64k MMU of a process just like a special device that builds its own page tables.

This way, they could get reclaimed more easily and most of the core + arm64 page able manipulation code could be kept as is.

However, I don't know how much the performance impact of that approach would be.

Signature · 3 lines
Cheers,

David
#6 lore raw
In reply to David Hildenbrand (Arm)
Quoted 7 lines · David Hildenbrand (Arm) via Lsf-pc
Quoted 2 lines · Matthew Wilcox

And if no pages from the file have been mmaped yet, they cannot be pinned or mlocked.

Is there some other way for someone to block a page from getting evicted from the pagecache?

Quoted 6 lines

We have this memfd_pin_folios() thing, but I don't think we have something comparable for ordinary pagecache files.

... putting them into a pipe and never reading from the pipe maybe (I assume that's what splice() does, but not sure if it actually places the pages in there or whether it creates a copy first)?

Standard splice copies data first (it's using standard IO callbacks such as ->read_iter) so that doesn't pin page cache AFAICT. Only vmsplice(2) does but that requires mmap.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#16 lore raw
In reply to Dev Jain
Quoted 1 line · Dev Jain

I didn't understand the reclaimable reference, but yes we need to make this efficient.

this goes over 80 columns so much and so often, it's painful to read. so i didn't.

Quoted 15 lines

So for the above example I gave, native_set_ptes knows the virtual address to set - walking the native hierarchy from native_pgd->native_pmd->native_pte (in case of 64K native geometry) is inefficient. So we need to maintain a lookup mechanism from a linux pgtable pointer to the native pgtable pointer.
The idea we have currently is to store such lookup in the struct ptdesc of the pagetable page. For 4K Linux pagetable and 64K native pagetable, 512M/2M = 256 Linux PTE tables correspond to different sections of the native PTE table. We will maintain the pointer to the relevant section in the native PTE table, in the struct ptdesc of the pagetable page of the Linux PTE table.
The other case is that a single Linux pgtable leaf entry corresponds to multiple native leaf entries - take the case of a Linux PMD table which maps 1G of memory, this corresponds to 2 native PTE tables (2 x 512M). We will have to store a list of pointers here.

Quoted 1 line
#17 lore raw
Quoted 5 lines · Matthew Wilcox
Quoted 1 line · Dev Jain

I didn't understand the reclaimable reference, but yes we need to make this efficient.

this goes over 80 columns so much and so often, it's painful to read. so i didn't.

I just found out that Thunderbird was lying to me the whole time.

If you're using "Toggle Line Wrap" plugin you might think that mails are properly wrapped, you know, like *they are displayed*. And even lore displays them properly.

But in the back, Thunderbird set "format=flowed" and screws you.

So, if anyone else believes that they are sending properly wrapped mails with Thunderbird, read

Documentation/process/email-clients.rst

and make sure that "mailnews.send_plaintext_flowed" is set to false.

Thanks Jens for the pointer!

Signature · 3 lines
Cheers,

David
author
#18 lore raw
Quoted 18 lines · David Hildenbrand (Arm)
Quoted 5 lines · Matthew Wilcox
Quoted 1 line · Dev Jain

I didn't understand the reclaimable reference, but yes we need to make this efficient.

this goes over 80 columns so much and so often, it's painful to read. so i didn't.

I just found out that Thunderbird was lying to me the whole time.

If you're using "Toggle Line Wrap" plugin you might think that mails are properly wrapped, you know, like *they are displayed*. And even lore displays them properly.

But in the back, Thunderbird set "format=flowed" and screws you.

So, if anyone else believes that they are sending properly wrapped mails with Thunderbird, read

Quoted 7 lines

Documentation/process/email-clients.rst

and make sure that "mailnews.send_plaintext_flowed" is set to false.

Thanks Jens for the pointer!

Thanks for letting me know about Toggle Line Wrap. This works, along with mailnews.wraplength. If I set this to 0, which is what email-clients.rst sugggests, it doesn't work. Thunderbird is confusing.

#19 lore raw
In reply to Dev Jain
Quoted 100 lines · Dev Jain

Hi everyone,

We propose per-process page size on arm64. Although the proposal is for arm64, perhaps the concept can be extended to other arches, thus the generic topic name.

-------------
INTRODUCTION
-------------
While mTHP has brought the performance of many workloads running on an arm64 4K kernel closer to that of the performance on an arm64 64K kernel, a performance gap still remains. This is attributed to a combination of greater number of pgtable levels, less reach within the walk cache and higher data cache footprint for pgtable memory. At the same time, 64K is not suitable for general purpose environments due to it's significantly higher memory footprint.

To solve this, we have been experimenting with a concept called "per-process page size". This breaks the historic assumption of a single page size for the entire system: a process will now operate on a page size ABI that is greater than or equal to the kernel's page size. This is enabled by a key architectural feature on Arm: the separation of user and kernel page tables.

This can also lead to a future of a single kernel image instead of 4K, 16K and 64K images.

--------------
CURRENT DESIGN
--------------
The design is based on one core idea; most of the kernel continues to believe there is only one page size in use across the whole system. That page size is the size selected at compile-time, as is done today. But every process (more accurately mm_struct) has a page size ABI which is one of the 3 page sizes (4K, 16K or 64K) as long as that page size is greater than or equal to the kernel page size (kernel page size is the macro PAGE_SIZE).

Pagesize selection
------------------
A process' selected page size ABI comes into force at execve() time and remains fixed until the process exits or until the next execve(). Any forked processes inherit the page size of their parent.
The personality() mechanism already exists for similar cases, so we propose to extend it to enable specifying the required page size.

There are 3 layers to the design. The first two are not arch-dependent, and makes Linux support a per-process pagesize ABI. The last layer is arch-specific.

1. ABI adapter
--------------
A translation layer is added at the syscall boundary to convert between the process page size and the kernel page size. This effectively means enforcing alignment requirements for addresses passed to syscalls and ensuring that quantities passed as “number of pages” are interpreted relative to the process page size and not the kernel page size. In this way the process has the illusion that it is working in units of its page size, but the kernel is working in units of the kernel page size.

2. Generic Linux MM enlightenment
---------------------------------
We enlighten the Linux MM code to always hand out memory in the granularity of process pages. Most of this work is greatly simplified because of the existing mTHP allocation paths, and the ongoing support for large folios across different areas of the kernel. The process order will be used as the hard minimum mTHP order to allocate.

File memory
-----------
For a growing list of compliant file systems, large folios can already be stored in the page cache. There is even a mechanism, introduced to support filesystems with block sizes larger than the system page size, to set a hard-minimum size for folios on a per-address-space basis. This mechanism will be reused and extended to service the per-process page size requirements.

One key reason that the 64K kernel currently consumes considerably more memory than the 4K kernel is that Linux systems often have lots of small configuration files which each require a page in the page cache. But these small files are (likely) only used by certain processes. So, we prefer to continue to cache those using a 4K page.
Therefore, if a process with a larger page size maps a file whose pagecache contains smaller folios, we drop them and re-read the range with a folio order at least that of the process order.

3. Translation from Linux pagetable to native pagetable
-------------------------------------------------------
Assume the case of a kernel pagesize of 4K and app pagesize of 64K. Now that enlightenment is done, it is guaranteed that every single mapping in the 4K pagetable (which we call the Linux pagetable) is of granularity at least 64K. In the arm64 MM code, we maintain a "native" pagetable per mm_struct, which is based off a 64K geometry. Because of the guarantee aforementioned, any pagetable operation on the Linux pagetable (set_ptes, clear_flush_ptes, modify_prot_start_ptes, etc) is going to happen at a granularity of at least 16 PTEs - therefore we can translate this operation to modify a single PTE entry in the native pagetable. Given that enlightenment may miss corner cases, we insert a warning in the architecture code - on being presented with an operation not translatable into a native operation, we fallback to the Linux pagetable, thus losing the benefits borne out of the pagetable geometry but keeping
the emulation intact.

Quoted 12 lines

-----------------------
What we want to discuss
-----------------------
- Are there other arches which could benefit from this?
- What level of compatibility we can achieve - is it even possible to contain userspace within the emulated ABI?
- Rough edges of compatibility layer - pfnmaps, ksm, procfs, etc. For example, what happens when a 64K process opens a procfs file of a 4K process?
- native pgtable implementation - perhaps inspiration can be taken from other arches with an involved pgtable logic (ppc, s390)?

Hi Dev, Ryan,

I'd be very interested in joining this discussion at LSF/MM.

On Android, we have a separate but very related use case: we emulate a larger userspace page size on x86, primarily to allow app developers to test their apps for 16KB compatibility using x86 emulators [1].

Similar to your proposed "ABI adapter" layer, our approach works by enforcing a larger 16KB granularity and alignment on the VMAs to emulate the userspace page size, while the underlying kernel still operates on a 4KB granularity [2].

In our emulation experience, we've run into a few specific rough edges:

1. mmap and SIGBUS: Enforcing a larger VMA granularity means that mapping files can easily extend the VMA beyond the end of the file's valid offset. When userspace touches this padded area, the 4KB filemap fault cannot resolve to a valid index, resulting in a SIGBUS that applications aren't expecting.

2. userfaultfd: This inherently operates at the strict PTE granularity of the underlying kernel (4KB). Hiding this from a userspace that expects a 16KB/64KB fault granularity while the kernel still operates on 4KB granularity is messy ...

3. pagemap and PFN interfaces: As you noted with procfs, interfaces that expose or consume PFNs are problematic. Userspace tools reading /proc/pid/pagemap, /proc/kpagecount, /proc/kpageflags, /proc/kpagecgroup, and /sys/kernel/mm/page_idle/bitmap calculate offsets based on the userspace page size ABI, but the kernel returns 4KB PFNs which breaks such users.

It would be great to explore if we can align on a unified approach to solve these.

[1] https://developer.android.com/guide/practices/page-sizes#16kb-emulator
[2] https://source.android.com/docs/core/architecture/16kb-page-size/getting-started-cf-x86-64-pgagnostic

Thanks,
Kalesh

Quoted 8 lines

-------------
Key Attendees
-------------
- Ryan Roberts (co-presenter)
- mm folks (David Hildenbrand, Matthew Wilcox, Liam Howlett, Lorenzo Stoakes, and many others)
- arch folks

author
#20 lore raw
Quoted 114 lines · Kalesh Singh
Quoted 100 lines · Dev Jain

Hi everyone,

We propose per-process page size on arm64. Although the proposal is for arm64, perhaps the concept can be extended to other arches, thus the generic topic name.

-------------
INTRODUCTION
-------------
While mTHP has brought the performance of many workloads running on an arm64 4K kernel closer to that of the performance on an arm64 64K kernel, a performance gap still remains. This is attributed to a combination of greater number of pgtable levels, less reach within the walk cache and higher data cache footprint for pgtable memory. At the same time, 64K is not suitable for general purpose environments due to it's significantly higher memory footprint.

To solve this, we have been experimenting with a concept called "per-process page size". This breaks the historic assumption of a single page size for the entire system: a process will now operate on a page size ABI that is greater than or equal to the kernel's page size. This is enabled by a key architectural feature on Arm: the separation of user and kernel page tables.

This can also lead to a future of a single kernel image instead of 4K, 16K and 64K images.

--------------
CURRENT DESIGN
--------------
The design is based on one core idea; most of the kernel continues to believe there is only one page size in use across the whole system. That page size is the size selected at compile-time, as is done today. But every process (more accurately mm_struct) has a page size ABI which is one of the 3 page sizes (4K, 16K or 64K) as long as that page size is greater than or equal to the kernel page size (kernel page size is the macro PAGE_SIZE).

Pagesize selection
------------------
A process' selected page size ABI comes into force at execve() time and remains fixed until the process exits or until the next execve(). Any forked processes inherit the page size of their parent.
The personality() mechanism already exists for similar cases, so we propose to extend it to enable specifying the required page size.

There are 3 layers to the design. The first two are not arch-dependent, and makes Linux support a per-process pagesize ABI. The last layer is arch-specific.

1. ABI adapter
--------------
A translation layer is added at the syscall boundary to convert between the process page size and the kernel page size. This effectively means enforcing alignment requirements for addresses passed to syscalls and ensuring that quantities passed as “number of pages” are interpreted relative to the process page size and not the kernel page size. In this way the process has the illusion that it is working in units of its page size, but the kernel is working in units of the kernel page size.

2. Generic Linux MM enlightenment
---------------------------------
We enlighten the Linux MM code to always hand out memory in the granularity of process pages. Most of this work is greatly simplified because of the existing mTHP allocation paths, and the ongoing support for large folios across different areas of the kernel. The process order will be used as the hard minimum mTHP order to allocate.

File memory
-----------
For a growing list of compliant file systems, large folios can already be stored in the page cache. There is even a mechanism, introduced to support filesystems with block sizes larger than the system page size, to set a hard-minimum size for folios on a per-address-space basis. This mechanism will be reused and extended to service the per-process page size requirements.

One key reason that the 64K kernel currently consumes considerably more memory than the 4K kernel is that Linux systems often have lots of small configuration files which each require a page in the page cache. But these small files are (likely) only used by certain processes. So, we prefer to continue to cache those using a 4K page.
Therefore, if a process with a larger page size maps a file whose pagecache contains smaller folios, we drop them and re-read the range with a folio order at least that of the process order.

3. Translation from Linux pagetable to native pagetable
-------------------------------------------------------
Assume the case of a kernel pagesize of 4K and app pagesize of 64K. Now that enlightenment is done, it is guaranteed that every single mapping in the 4K pagetable (which we call the Linux pagetable) is of granularity at least 64K. In the arm64 MM code, we maintain a "native" pagetable per mm_struct, which is based off a 64K geometry. Because of the guarantee aforementioned, any pagetable operation on the Linux pagetable (set_ptes, clear_flush_ptes, modify_prot_start_ptes, etc) is going to happen at a granularity of at least 16 PTEs - therefore we can translate this operation to modify a single PTE entry in the native pagetable. Given that enlightenment may miss corner cases, we insert a warning in the architecture code - on being presented with an operation not translatable into a native operation, we fallback to the Linux pagetable, thus losing the benefits borne out of the pagetable geometry but keeping
the emulation intact.

Quoted 12 lines

-----------------------
What we want to discuss
-----------------------
- Are there other arches which could benefit from this?
- What level of compatibility we can achieve - is it even possible to contain userspace within the emulated ABI?
- Rough edges of compatibility layer - pfnmaps, ksm, procfs, etc. For example, what happens when a 64K process opens a procfs file of a 4K process?
- native pgtable implementation - perhaps inspiration can be taken from other arches with an involved pgtable logic (ppc, s390)?

Quoted 3 lines

Hi Dev, Ryan,

I'd be very interested in joining this discussion at LSF/MM.

Thanks Kalesh for your interest!

Quoted 10 lines

On Android, we have a separate but very related use case: we emulate a larger userspace page size on x86, primarily to allow app developers to test their apps for 16KB compatibility using x86 emulators [1].

Similar to your proposed "ABI adapter" layer, our approach works by enforcing a larger 16KB granularity and alignment on the VMAs to emulate the userspace page size, while the underlying kernel still operates on a 4KB granularity [2].

Quoted 7 lines

In our emulation experience, we've run into a few specific rough edges:

1. mmap and SIGBUS: Enforcing a larger VMA granularity means that mapping files can easily extend the VMA beyond the end of the file's valid offset. When userspace touches this padded area, the 4KB filemap fault cannot resolve to a valid index, resulting in a SIGBUS that applications aren't expecting.

You did mention in the other email the links below, and I went ahead to compare :) I was puzzled to see some sort of VMA padding approach in your patches. OTOH our approach pads anonymous pages. So for example, if a 64K process maps a 12K sized file, we will map 52K/4K = 13 anonymous pages into the 64K-aligned VMA.

Implementation-wise, we detect such a condition in filemap_fault and return VM_FAULT_NEED_ANONPAGE, and redirect that to do_anonymous_page to map 4K pages.

Quoted 5 lines

2. userfaultfd: This inherently operates at the strict PTE granularity of the underlying kernel (4KB). Hiding this from a userspace that expects a 16KB/64KB fault granularity while the kernel still operates on 4KB granularity is messy ...

Indeed. We will have to fault in 16 4K pages.

Quoted 26 lines

3. pagemap and PFN interfaces: As you noted with procfs, interfaces that expose or consume PFNs are problematic. Userspace tools reading /proc/pid/pagemap, /proc/kpagecount, /proc/kpageflags, /proc/kpagecgroup, and /sys/kernel/mm/page_idle/bitmap calculate offsets based on the userspace page size ABI, but the kernel returns 4KB PFNs which breaks such users.

It would be great to explore if we can align on a unified approach to solve these.

[1] https://developer.android.com/guide/practices/page-sizes#16kb-emulator
[2] https://source.android.com/docs/core/architecture/16kb-page-size/getting-started-cf-x86-64-pgagnostic

Thanks,
Kalesh

Quoted 8 lines

-------------
Key Attendees
-------------
- Ryan Roberts (co-presenter)
- mm folks (David Hildenbrand, Matthew Wilcox, Liam Howlett, Lorenzo Stoakes, and many others)
- arch folks

#21 lore raw
Quoted 148 lines · Dev Jain
Quoted 114 lines · Kalesh Singh
Quoted 100 lines · Dev Jain

Hi everyone,

We propose per-process page size on arm64. Although the proposal is for arm64, perhaps the concept can be extended to other arches, thus the generic topic name.

-------------
INTRODUCTION
-------------
While mTHP has brought the performance of many workloads running on an arm64 4K kernel closer to that of the performance on an arm64 64K kernel, a performance gap still remains. This is attributed to a combination of greater number of pgtable levels, less reach within the walk cache and higher data cache footprint for pgtable memory. At the same time, 64K is not suitable for general purpose environments due to it's significantly higher memory footprint.

To solve this, we have been experimenting with a concept called "per-process page size". This breaks the historic assumption of a single page size for the entire system: a process will now operate on a page size ABI that is greater than or equal to the kernel's page size. This is enabled by a key architectural feature on Arm: the separation of user and kernel page tables.

This can also lead to a future of a single kernel image instead of 4K, 16K and 64K images.

--------------
CURRENT DESIGN
--------------
The design is based on one core idea; most of the kernel continues to believe there is only one page size in use across the whole system. That page size is the size selected at compile-time, as is done today. But every process (more accurately mm_struct) has a page size ABI which is one of the 3 page sizes (4K, 16K or 64K) as long as that page size is greater than or equal to the kernel page size (kernel page size is the macro PAGE_SIZE).

Pagesize selection
------------------
A process' selected page size ABI comes into force at execve() time and remains fixed until the process exits or until the next execve(). Any forked processes inherit the page size of their parent.
The personality() mechanism already exists for similar cases, so we propose to extend it to enable specifying the required page size.

There are 3 layers to the design. The first two are not arch-dependent, and makes Linux support a per-process pagesize ABI. The last layer is arch-specific.

1. ABI adapter
--------------
A translation layer is added at the syscall boundary to convert between the process page size and the kernel page size. This effectively means enforcing alignment requirements for addresses passed to syscalls and ensuring that quantities passed as “number of pages” are interpreted relative to the process page size and not the kernel page size. In this way the process has the illusion that it is working in units of its page size, but the kernel is working in units of the kernel page size.

2. Generic Linux MM enlightenment
---------------------------------
We enlighten the Linux MM code to always hand out memory in the granularity of process pages. Most of this work is greatly simplified because of the existing mTHP allocation paths, and the ongoing support for large folios across different areas of the kernel. The process order will be used as the hard minimum mTHP order to allocate.

File memory
-----------
For a growing list of compliant file systems, large folios can already be stored in the page cache. There is even a mechanism, introduced to support filesystems with block sizes larger than the system page size, to set a hard-minimum size for folios on a per-address-space basis. This mechanism will be reused and extended to service the per-process page size requirements.

One key reason that the 64K kernel currently consumes considerably more memory than the 4K kernel is that Linux systems often have lots of small configuration files which each require a page in the page cache. But these small files are (likely) only used by certain processes. So, we prefer to continue to cache those using a 4K page.
Therefore, if a process with a larger page size maps a file whose pagecache contains smaller folios, we drop them and re-read the range with a folio order at least that of the process order.

3. Translation from Linux pagetable to native pagetable
-------------------------------------------------------
Assume the case of a kernel pagesize of 4K and app pagesize of 64K. Now that enlightenment is done, it is guaranteed that every single mapping in the 4K pagetable (which we call the Linux pagetable) is of granularity at least 64K. In the arm64 MM code, we maintain a "native" pagetable per mm_struct, which is based off a 64K geometry. Because of the guarantee aforementioned, any pagetable operation on the Linux pagetable (set_ptes, clear_flush_ptes, modify_prot_start_ptes, etc) is going to happen at a granularity of at least 16 PTEs - therefore we can translate this operation to modify a single PTE entry in the native pagetable. Given that enlightenment may miss corner cases, we insert a warning in the architecture code - on being presented with an operation not translatable into a native operation, we fallback to the Linux pagetable, thus losing the benefits borne out of the pagetable geometry but keeping
the emulation intact.

Quoted 12 lines

-----------------------
What we want to discuss
-----------------------
- Are there other arches which could benefit from this?
- What level of compatibility we can achieve - is it even possible to contain userspace within the emulated ABI?
- Rough edges of compatibility layer - pfnmaps, ksm, procfs, etc. For example, what happens when a 64K process opens a procfs file of a 4K process?
- native pgtable implementation - perhaps inspiration can be taken from other arches with an involved pgtable logic (ppc, s390)?

Quoted 3 lines

Hi Dev, Ryan,

I'd be very interested in joining this discussion at LSF/MM.

Thanks Kalesh for your interest!

Quoted 10 lines

On Android, we have a separate but very related use case: we emulate a larger userspace page size on x86, primarily to allow app developers to test their apps for 16KB compatibility using x86 emulators [1].

Similar to your proposed "ABI adapter" layer, our approach works by enforcing a larger 16KB granularity and alignment on the VMAs to emulate the userspace page size, while the underlying kernel still operates on a 4KB granularity [2].

Quoted 7 lines

In our emulation experience, we've run into a few specific rough edges:

1. mmap and SIGBUS: Enforcing a larger VMA granularity means that mapping files can easily extend the VMA beyond the end of the file's valid offset. When userspace touches this padded area, the 4KB filemap fault cannot resolve to a valid index, resulting in a SIGBUS that applications aren't expecting.

You did mention in the other email the links below, and I went ahead to compare :) I was puzzled to see some sort of VMA padding approach in your patches. OTOH our approach pads anonymous pages. So for example, if a 64K process maps a 12K sized file, we will map 52K/4K = 13 anonymous pages into the 64K-aligned VMA.

Quoted 3 lines

Implementation-wise, we detect such a condition in filemap_fault and return VM_FAULT_NEED_ANONPAGE, and redirect that to do_anonymous_page to map 4K pages.

Ah, the VMA padding patches you saw are actually for a different feature.

To handle the file mapping overhang, we currently insert a separate anonymous VMA to cover the remainder of the emulated page range. Tough I think your approach of returning VM_FAULT_NEED_ANONPAGE to fault anonymous pages without needing to manage extra VMAs is a much cleaner design :)

Thanks,
Kalesh

Quoted 36 lines
Quoted 6 lines

2. userfaultfd: This inherently operates at the strict PTE granularity of the underlying kernel (4KB). Hiding this from a userspace that expects a 16KB/64KB fault granularity while the kernel still operates on 4KB granularity is messy ...

Indeed. We will have to fault in 16 4K pages.

Quoted 26 lines

3. pagemap and PFN interfaces: As you noted with procfs, interfaces that expose or consume PFNs are problematic. Userspace tools reading /proc/pid/pagemap, /proc/kpagecount, /proc/kpageflags, /proc/kpagecgroup, and /sys/kernel/mm/page_idle/bitmap calculate offsets based on the userspace page size ABI, but the kernel returns 4KB PFNs which breaks such users.

It would be great to explore if we can align on a unified approach to solve these.

[1] https://developer.android.com/guide/practices/page-sizes#16kb-emulator
[2] https://source.android.com/docs/core/architecture/16kb-page-size/getting-started-cf-x86-64-pgagnostic

Thanks,
Kalesh

Quoted 8 lines

-------------
Key Attendees
-------------
- Ryan Roberts (co-presenter)
- mm folks (David Hildenbrand, Matthew Wilcox, Liam Howlett, Lorenzo Stoakes, and many others)
- arch folks

#7 lore raw
In reply to Jan Kara

Jan Kara <[email protected]>:

Quoted 6 lines
Quoted 6 lines

Is there some other way for someone to block a page from getting evicted from the pagecache?

We have this memfd_pin_folios() thing, but I don't think we have something comparable for ordinary pagecache files.

Quoted 7 lines
Quoted 3 lines

... putting them into a pipe and never reading from the pipe maybe (I assume that's what splice() does, but not sure if it actually places the pages in there or whether it creates a copy first)?

Standard splice copies data first (it's using standard IO callbacks such as ->read_iter) so that doesn't pin page cache AFAICT. Only vmsplice(2) does but that requires mmap.

Splice from page cache to pipe sometimes doesn't copy data.

That is why test program mentioned here may print "new":
https://lore.kernel.org/all/CAHk-=wjixHw6n_R5TQWW1r0a+GgFAPGw21KMj6obkzr3qXXbYA@mail.gmail.com/

Signature · 1 line
Askar Safin
#22 lore raw
In reply to Dev Jain

Sorry if I repeat points others have raised, but I've just not had the time to look at LSF/MM topics.

Quoted 5 lines · Dev Jain

Hi everyone,

We propose per-process page size on arm64. Although the proposal is for arm64, perhaps the concept can be extended to other arches, thus the generic topic name.

Well it's inevitably going to interact with core mm right and be super invasive? So it's not just an arm64-only thing unless you feel you can keep it all in arch/arm?

So you're asking essentially core mm to be radically altered to support an arch-dependent feature.

I think we should be clear about that.

Quoted 5 lines

-------------
INTRODUCTION
-------------
While mTHP has brought the performance of many workloads running on an arm64 4K

Honestly this whole suggestion to me - instinctively - feels like 'mTHP doesn't do what we want, so let's work around it'.

Wouldn't it be better to improve mTHP to provide what you are looking for?

Quoted 4 lines

kernel closer to that of the performance on an arm64 64K kernel, a performance gap still remains. This is attributed to a combination of greater number of pgtable levels, less reach within the walk cache and higher data cache footprint for pgtable memory. At the same time, 64K is not suitable for general

This all sounds like things mTHP can address.

Quoted 5 lines

purpose environments due to it's significantly higher memory footprint.

To solve this, we have been experimenting with a concept called "per-process page size". This breaks the historic assumption of a single page size for the entire system: a process will now operate on a page size ABI that is greater

Fundamentally you'd be adding a great deal more complexity into mm, so the trade-off has to be really excellent for that to be worthwhile.

It also essentially, to me, spells a giving up on mTHP ever being something that automatically gives us what we want in these regards.

Quoted 2 lines

than or equal to the kernel's page size. This is enabled by a key architectural feature on Arm: the separation of user and kernel page tables.

Isn't that true of every architecture? I mean we map userland and kernel memory separately everywhere right?

Presumably you mean that arm64 has a feature whereby there are always entirely separate page table structures for each.

But the kernel _regularly_ manipulates userland memory mappings, directly via uaccess, via GUP, page table set up and manipulation via rmap etc. etc. etc.

uaccess means the kernel uses the same page tables that userland uses.

So is the proposed mechanism contpte or actually having a base page size at higher granularity? Presumably the latter.

GUP fast as well is another concern - how can that be kept working with all this?

And TLB maintenance, now you're dealing with fundamentally different granularity, the kernel will have to surely become 'enlightened' somehow (IOW - radically altered adding further complexity and bug risk) to handle that?

Quoted 3 lines

This can also lead to a future of a single kernel image instead of 4K, 16K and 64K images.

Except then every bit of kernel memory is at what 4K? Surely there's perf impact there too..

I wonder about how on earth pageblocks are supposed to work in this mechanism. Because now the whole heuristic means of avoiding fragmentation for PMD sized pages won't work anywhere, because any given process might need more.

And pageblocks are currently broken for 64KB page size anyway, as the reserves required are huge due to 64KB PMD size being gigantic (512GB isn't it?)

And then, there's the page cache :) but I guess we address that below.

I do wonder how this is supposed to interact with THP and mTHP which now will have to maintain entirely separate PMD sizes depending on process surely?

I mean correct me if I've misinterpreted and you intend to implement this using contpte's or something, but if you are I'd ask why don't we just improve mTHP?

Quoted 11 lines

--------------
CURRENT DESIGN
--------------
The design is based on one core idea; most of the kernel continues to believe there is only one page size in use across the whole system. That page size is the size selected at compile-time, as is done today. But every process (more accurately mm_struct) has a page size ABI which is one of the 3 page sizes (4K, 16K or 64K) as long as that page size is greater than or equal to the kernel page size (kernel page size is the macro PAGE_SIZE).

Quoted 7 lines

Pagesize selection
------------------
A process' selected page size ABI comes into force at execve() time and remains fixed until the process exits or until the next execve(). Any forked processes inherit the page size of their parent.
The personality() mechanism already exists for similar cases, so we propose to extend it to enable specifying the required page size.

Well it's designed for providing basic compatibility layers and address space limits and so on, this feels like something _hugely_ more impactful.

Doesn't personality() _immediately_ change the current process's state? I don't think we should implement that that way.

If we were to do this, I'd say something like a clone flag would be better maybe? or an elf setting.

I also wonder the degree to which kernel mechanisms already assume PAGE_SIZE == userland PAGE_SIZE.

We'd have to rework all of the page table walkers for one no?

Quoted 5 lines

There are 3 layers to the design. The first two are not arch-dependent, and makes Linux support a per-process pagesize ABI. The last layer is arch-specific.

Quoted 9 lines

1. ABI adapter
--------------
A translation layer is added at the syscall boundary to convert between the process page size and the kernel page size. This effectively means enforcing alignment requirements for addresses passed to syscalls and ensuring that quantities passed as “number of pages” are interpreted relative to the process page size and not the kernel page size. In this way the process has the illusion that it is working in units of its page size, but the kernel is working in units of the kernel page size.

I mean I think we'd also need to radically alter a lot of other things. But this is already adding a bunch of complexity and ways for things to be buggy and broken.

Quoted 8 lines

2. Generic Linux MM enlightenment
---------------------------------
We enlighten the Linux MM code to always hand out memory in the granularity of process pages. Most of this work is greatly simplified because of the existing mTHP allocation paths, and the ongoing support for large folios across different areas of the kernel. The process order will be used as the hard minimum mTHP order to allocate.

I mean, this is again another radical change everywhere. I don't understand 'the process order will be used as the hard minimum mTHP order to allocate'.

So now we're allocating all physical memory for these processes using mTHP somehow? How does the mTHP mechanism interact with this?

What will cat /sys/kernel/mm/transparent_hugepage/hpage_pmd_size show? And HugePageSize in /proc/meminfo?

Presumably:

sysconf(_SC_PAGESIZE); / getconf PAGE_SIZE/PAGESIZE

Also there's the thorny thing of in /proc/$pid/smaps 'MMUPageSize' vs. 'KernelPageSize', which was instituted to handle some PPC-specific I think variation in the two.

This was updated after recent discussions by David at:

https://lore.kernel.org/linux-mm/[email protected]/

"KernelPageSize" always corresponds to "MMUPageSize", except when a larger kernel page size is emulated on a system with a smaller page size used by the MMU, which is the case for some PPC64 setups with hugetlb."

This will be invalidated if we change these, but surely you'd have to?

Also w.r.t. hugetlb how will PMD sharing work across processes with different page size?

And what about drivers that allocate PAGE_SIZE memory to share with userland? This does happen, or at the wrong granularity?

How can they be 'educated' to allocate the right amount of memory? I don't see a way around one that actually.

Also what about bounce buffers for e.g. /proc/kcore?

And talking about proc, what about procfs interfaces that count per-page, except they won't be counting per-page any more, they'll be counting per-kernel page...?

What about bpf hooks that rely on page size? Surely we're potentially breaking things there?

(Sorry just asking questions as they come up in my mind)

THP now surely has multiple different PMD sizes to contend with, but is also being used to provide the mTHP-sized folios to the process?...

I mean unless you intend that these processes are just implementing page table size via contpte only, and keep userland page size = kernel page size except for this, but then the question again becomes 'what in mTHP is not working such that you feel you need to do this'?

Quoted 9 lines

File memory
-----------
For a growing list of compliant file systems, large folios can already be stored in the page cache. There is even a mechanism, introduced to support filesystems with block sizes larger than the system page size, to set a hard-minimum size for folios on a per-address-space basis. This mechanism will be reused and extended to service the per-process page size requirements.

Quoted 8 lines

One key reason that the 64K kernel currently consumes considerably more memory than the 4K kernel is that Linux systems often have lots of small configuration files which each require a page in the page cache. But these small files are (likely) only used by certain processes. So, we prefer to continue to cache those using a 4K page.
Therefore, if a process with a larger page size maps a file whose pagecache contains smaller folios, we drop them and re-read the range with a folio order at least that of the process order.

Yeah this is surely terrible? One process can cause evictions from the page cache for others, then when they get scheduled they drop them again and again over and over and over just because files are being accessed by different processes no?

I don't think this is at all workable, unless I'm missing something?

This feels like you need a complete rework of the page cache to implement this really, and I wonder at that being a realistic goal.

Quoted 8 lines

3. Translation from Linux pagetable to native pagetable
-------------------------------------------------------
Assume the case of a kernel pagesize of 4K and app pagesize of 64K. Now that enlightenment is done, it is guaranteed that every single mapping in the 4K pagetable (which we call the Linux pagetable) is of granularity at least 64K. In the arm64 MM code, we maintain a "native" pagetable per mm_struct, which is based off a 64K geometry. Because of the guarantee

I mean yeah, this 'enlightenment' feels a lot more like - radically altering a ton of mm code and never quite being sure it will all work :)

Quoted 6 lines

aforementioned, any pagetable operation on the Linux pagetable (set_ptes, clear_flush_ptes, modify_prot_start_ptes, etc) is going to happen at a granularity of at least 16 PTEs - therefore we can translate this operation to modify a single PTE entry in the native pagetable. Given that enlightenment may miss corner cases, we insert a warning in the architecture code - on being presented with an operation not translatable

Yeah this is already hugely concerning, introducing subtle ways to break things.

Quoted 3 lines

into a native operation, we fallback to the Linux pagetable, thus losing the benefits borne out of the pagetable geometry but keeping the emulation intact.

I really dislike the idea of maintaining an 'emulation' between userland and the kernel on a fundamental basis. The kernel is supposed to deal with the system 'as it is'.

Adding layers of abstraction just adds ever more possibilities of ever more subtle and difficult-to-solve bugs.

Quoted 4 lines

-----------------------
What we want to discuss
-----------------------

I think we also need to discuss whether this is generally viable or a good idea :)

I suppose that is implicit.

Quoted 9 lines

- Are there other arches which could benefit from this?
- What level of compatibility we can achieve - is it even possible to contain userspace within the emulated ABI?
- Rough edges of compatibility layer - pfnmaps, ksm, procfs, etc. For example, what happens when a 64K process opens a procfs file of a 4K process?
- native pgtable implementation - perhaps inspiration can be taken from other arches with an involved pgtable logic (ppc, s390)?

Quoted 6 lines

-------------
Key Attendees
-------------
- Ryan Roberts (co-presenter)
- mm folks (David Hildenbrand, Matthew Wilcox, Liam Howlett, Lorenzo Stoakes, and many others)

Thanks :)

Quoted 1 line

- arch folks

Overall this feels like a workaround for mTHP not doing what you want, and to me that speaks to us needing to improve mTHP rather than add a ton of complexity.

I fear we are only touching the tip of the iceberg of ways in which this could break.

Regards, Lorenzo