The problem of unnecessary readahead
Singh had a couple of example cases; the first had to do with ELF binaries that include extra padding for compatibility with systems that have a large base-page size. On systems with smaller pages, that padding is just useless data that could happily remain on disk, but the readahead machinery is reading it into the page cache anyway. That creates useless I/O and wastes memory with data that nobody will need. Another example was APK (Android package) files, which contain a combination of compressed and uncompressed data in the same file. Readahead is bringing in the compressed data, even though it will never be read.
In both cases, Singh said, readahead is bringing in unwanted data, even if
it is outside of the part of the file that has been mapped. fadvise()
can be used to prevent the readahead, but that affects the entire file,
including the parts where readahead is beneficial. Singh has also tried
placing guard pages in the memory area
where the file is mapped, but they do not stop readahead. David
Hildenbrand suggested that having the readahead mechanism actively look for
guard regions would be a good idea, but this checking might prove to be
expensive.
A participant asked why the whole file, including the padding, is being mapped; Singh said that the alternative would create a lot more virtual memory areas (VMAs), which would have a performance cost of its own. Hildenbrand suggested that madvise() could be used to force the reading of the desired part of the file. The memory of that operation would be lost, though, if the populated pages are reclaimed. This approach could also slow down application load time, he said.
Singh had one other possibility to discuss: optimizing the handling of sparse files. The holes in files could be marked in the kernels and mapped to the zero page if accessed by the process. There would be some pitfalls; there are complications if the zero-page is modified (causing a copy-on-write operation) while pinned into memory for example. It would also complicate the handling of files that have been mapped shared; perhaps, he said, that case does not need to be supported.
As the session (and the conference) ended, Hildenbrand suggested that efforts to optimize for unexpected cases would be misplaced. Perhaps, he said, it would be sufficient to just stop readahead whenever a hole is reached in the file.
Singh has posted
the slides from this session.
| Index entries for this article | |
|---|---|
| Kernel | Memory management/Readahead |
| Conference | Storage, Filesystem, Memory-Management and BPF Summit/2025 |
