요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.
1. 요약·해설
원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.
2. 영어 원문 전체
번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.
원문 전체 펼치기
=============================
No-MMU memory mapping support
=============================
The kernel has limited support for memory mapping under no-MMU conditions, such
as are used in uClinux environments. From the userspace point of view, memory
mapping is made use of in conjunction with the mmap() system call, the shmat()
call and the execve() system call. From the kernel's point of view, execve()
mapping is actually performed by the binfmt drivers, which call back into the
mmap() routines to do the actual work.
Memory mapping behaviour also involves the way fork(), vfork(), clone() and
ptrace() work. Under uClinux there is no fork(), and clone() must be supplied
the CLONE_VM flag.
The behaviour is similar between the MMU and no-MMU cases, but not identical;
and it's also much more restricted in the latter case:
(#) Anonymous mapping, MAP_PRIVATE
In the MMU case: VM regions backed by arbitrary pages; copy-on-write
across fork.
In the no-MMU case: VM regions backed by arbitrary contiguous runs of
pages.
(#) Anonymous mapping, MAP_SHARED
These behave very much like private mappings, except that they're
shared across fork() or clone() without CLONE_VM in the MMU case. Since
the no-MMU case doesn't support these, behaviour is identical to
MAP_PRIVATE there.
(#) File, MAP_PRIVATE, PROT_READ / PROT_EXEC, !PROT_WRITE
In the MMU case: VM regions backed by pages read from file; changes to
the underlying file are reflected in the mapping; copied across fork.
In the no-MMU case:
- If one exists, the kernel will re-use an existing mapping to the
same segment of the same file if that has compatible permissions,
even if this was created by another process.
- If possible, the file mapping will be directly on the backing device
if the backing device has the NOMMU_MAP_DIRECT capability and
appropriate mapping protection capabilities. Ramfs, romfs, cramfs
and mtd might all permit this.
- If the backing device can't or won't permit direct sharing,
but does have the NOMMU_MAP_COPY capability, then a copy of the
appropriate bit of the file will be read into a contiguous bit of
memory and any extraneous space beyond the EOF will be cleared
- Writes to the file do not affect the mapping; writes to the mapping
are visible in other processes (no MMU protection), but should not
happen.
(#) File, MAP_PRIVATE, PROT_READ / PROT_EXEC, PROT_WRITE
In the MMU case: like the non-PROT_WRITE case, except that the pages in
question get copied before the write actually happens. From that point
on writes to the file underneath that page no longer get reflected into
the mapping's backing pages. The page is then backed by swap instead.
In the no-MMU case: works much like the non-PROT_WRITE case, except
that a copy is always taken and never shared.
(#) Regular file / blockdev, MAP_SHARED, PROT_READ / PROT_EXEC / PROT_WRITE
In the MMU case: VM regions backed by pages read from file; changes to
pages written back to file; writes to file reflected into pages backing
mapping; shared across fork.
In the no-MMU case: not supported.
(#) Memory backed regular file, MAP_SHARED, PROT_READ / PROT_EXEC / PROT_WRITE
In the MMU case: As for ordinary regular files.
In the no-MMU case: The filesystem providing the memory-backed file
(such as ramfs or tmpfs) may choose to honour an open, truncate, mmap
sequence by providing a contiguous sequence of pages to map. In that
case, a shared-writable memory mapping will be possible. It will work
as for the MMU case. If the filesystem does not provide any such
support, then the mapping request will be denied.
(#) Memory backed blockdev, MAP_SHARED, PROT_READ / PROT_EXEC / PROT_WRITE
In the MMU case: As for ordinary regular files.
In the no-MMU case: As for memory backed regular files, but the
blockdev must be able to provide a contiguous run of pages without
truncate being called. The ramdisk driver could do this if it allocated
all its memory as a contiguous array upfront.
(#) Memory backed chardev, MAP_SHARED, PROT_READ / PROT_EXEC / PROT_WRITE
In the MMU case: As for ordinary regular files.
In the no-MMU case: The character device driver may choose to honour
the mmap() by providing direct access to the underlying device if it
provides memory or quasi-memory that can be accessed directly. Examples
of such are frame buffers and flash devices. If the driver does not
provide any such support, then the mapping request will be denied.
Further notes on no-MMU MMAP
============================
(#) A request for a private mapping of a file may return a buffer that is not
page-aligned. This is because XIP may take place, and the data may not be
paged aligned in the backing store.
(#) A request for an anonymous mapping will always be page aligned. If
possible the size of the request should be a power of two otherwise some
of the space may be wasted as the kernel must allocate a power-of-2
granule but will only discard the excess if appropriately configured as
this has an effect on fragmentation.
(#) The memory allocated by a request for an anonymous mapping will normally
be cleared by the kernel before being returned in accordance with the
Linux man pages (ver 2.22 or later).
In the MMU case this can be achieved with reasonable performance as
regions are backed by virtual pages, with the contents only being mapped
to cleared physical pages when a write happens on that specific page
(prior to which, the pages are effectively mapped to the global zero page
from which reads can take place). This spreads out the time it takes to
initialize the contents of a page - depending on the write-usage of the
mapping.
In the no-MMU case, however, anonymous mappings are backed by physical
pages, and the entire map is cleared at allocation time. This can cause
significant delays during a userspace malloc() as the C library does an
anonymous mapping and the kernel then does a memset for the entire map.
However, for memory that isn't required to be precleared - such as that
returned by malloc() - mmap() can take a MAP_UNINITIALIZED flag to
indicate to the kernel that it shouldn't bother clearing the memory before
returning it. Note that CONFIG_MMAP_ALLOW_UNINITIALIZED must be enabled
to permit this, otherwise the flag will be ignored.
uClibc uses this to speed up malloc(), and the ELF-FDPIC binfmt uses this
to allocate the brk and stack region.
(#) A list of all the private copy and anonymous mappings on the system is
visible through /proc/maps in no-MMU mode.
(#) A list of all the mappings in use by a process is visible through
/proc/<pid>/maps in no-MMU mode.
(#) Supplying MAP_FIXED or a requesting a particular mapping address will
result in an error.
(#) Files mapped privately usually have to have a read method provided by the
driver or filesystem so that the contents can be read into the memory
allocated if mmap() chooses not to map the backing device directly. An
error will result if they don't. This is most likely to be encountered
with character device files, pipes, fifos and sockets.
Interprocess shared memory
==========================
Both SYSV IPC SHM shared memory and POSIX shared memory is supported in NOMMU
mode. The former through the usual mechanism, the latter through files created
on ramfs or tmpfs mounts.
Futexes
=======
Futexes are supported in NOMMU mode if the arch supports them. An error will
be given if an address passed to the futex system call lies outside the
mappings made by a process or if the mapping in which the address lies does not
support futexes (such as an I/O chardev mapping).
No-MMU mremap
=============
The mremap() function is partially supported. It may change the size of a
mapping, and may move it [#]_ if MREMAP_MAYMOVE is specified and if the new size
of the mapping exceeds the size of the slab object currently occupied by the
memory to which the mapping refers, or if a smaller slab object could be used.
MREMAP_FIXED is not supported, though it is ignored if there's no change of
address and the object does not need to be moved.
Shared mappings may not be moved. Shareable mappings may not be moved either,
even if they are not currently shared.
The mremap() function must be given an exact match for base address and size of
a previously mapped object. It may not be used to create holes in existing
mappings, move parts of existing mappings or resize parts of mappings. It must
act on a complete mapping.
.. [#] Not currently supported.
Providing shareable character device support
============================================
To provide shareable character device support, a driver must provide a
file->f_op->get_unmapped_area() operation. The mmap() routines will call this
to get a proposed address for the mapping. This may return an error if it
doesn't wish to honour the mapping because it's too long, at a weird offset,
under some unsupported combination of flags or whatever.
The driver should also provide backing device information with capabilities set
to indicate the permitted types of mapping on such devices. The default is
assumed to be readable and writable, not executable, and only shareable
directly (can't be copied).
The file->f_op->mmap() operation will be called to actually inaugurate the
mapping. It can be rejected at that point. Returning the ENOSYS error will
cause the mapping to be copied instead if NOMMU_MAP_COPY is specified.
The vm_ops->close() routine will be invoked when the last mapping on a chardev
is removed. An existing mapping will be shared, partially or not, if possible
without notifying the driver.
It is permitted also for the file->f_op->get_unmapped_area() operation to
return -ENOSYS. This will be taken to mean that this operation just doesn't
want to handle it, despite the fact it's got an operation. For instance, it
might try directing the call to a secondary driver which turns out not to
implement it. Such is the case for the framebuffer driver which attempts to
direct the call to the device-specific driver. Under such circumstances, the
mapping request will be rejected if NOMMU_MAP_COPY is not specified, and a
copy mapped otherwise.
.. important::
Some types of device may present a different appearance to anyone
looking at them in certain modes. Flash chips can be like this; for
instance if they're in programming or erase mode, you might see the
status reflected in the mapping, instead of the data.
In such a case, care must be taken lest userspace see a shared or a
private mapping showing such information when the driver is busy
controlling the device. Remember especially: private executable
mappings may still be mapped directly off the device under some
circumstances!
Providing shareable memory-backed file support
==============================================
Provision of shared mappings on memory backed files is similar to the provision
of support for shared mapped character devices. The main difference is that the
filesystem providing the service will probably allocate a contiguous collection
of pages and permit mappings to be made on that.
It is recommended that a truncate operation applied to such a file that
increases the file size, if that file is empty, be taken as a request to gather
enough pages to honour a mapping. This is required to support POSIX shared
memory.
Memory backed devices are indicated by the mapping's backing device info having
the memory_backed flag set.
Providing shareable block device support
========================================
Provision of shared mappings on block device files is exactly the same as for
character devices. If there isn't a real device underneath, then the driver
should allocate sufficient contiguous memory to honour any supported mapping.
Adjusting page trimming behaviour
=================================
NOMMU mmap automatically rounds up to the nearest power-of-2 number of pages
when performing an allocation. This can have adverse effects on memory
fragmentation, and as such, is left configurable. The default behaviour is to
aggressively trim allocations and discard any excess pages back in to the page
allocator. In order to retain finer-grained control over fragmentation, this
behaviour can either be disabled completely, or bumped up to a higher page
watermark where trimming begins.
Page trimming behaviour is configurable via the sysctl ``vm.nr_trim_pages``.
3. 한국어 전문 번역
영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.
No-MMU mapping 개요
1-18Kernel은 uClinux 같은 no-MMU 환경에서 제한적인 memory mapping을 지원합니다. User space에서는 mmap(), shmat(), execve()가 mapping을 사용합니다. Kernel 관점에서 execve() mapping은 binfmt driver가 mmap() routine을 callback하여 실제 작업을 수행합니다.
Memory mapping 동작은 fork(), vfork(), clone(), ptrace() 방식과도 연결됩니다. uClinux에는 fork()가 없고 clone()에는 CLONE_VM flag를 반드시 제공해야 합니다. MMU와 no-MMU의 동작은 비슷하지만 같지 않으며 no-MMU 쪽 제약이 훨씬 큽니다.
Anonymous mapping
19-33| 유형 | MMU | No-MMU |
|---|---|---|
| Anonymous MAP_PRIVATE | 임의의 page로 구성한 VM region, fork() 사이 copy-on-write | 임의의 연속된 page run으로 구성한 VM region |
| Anonymous MAP_SHARED | private mapping과 비슷하지만 fork() 또는 CLONE_VM 없는 clone() 사이에서 공유 | 그런 process 생성 방식을 지원하지 않으므로 MAP_PRIVATE과 동일 |
No-MMU의 anonymous mapping은 임의의 page를 흩어 배치할 수 없고 contiguous run이 필요합니다. 또한 fork()나 CLONE_VM 없는 clone()을 지원하지 않으므로 MAP_SHARED와 MAP_PRIVATE의 차이가 없습니다.
Read/execute private file mapping
34-58File을 MAP_PRIVATE, PROT_READ 또는 PROT_EXEC, !PROT_WRITE로 mapping할 때 MMU에서는 file에서 읽은 page가 VM region을 backing하고 underlying file의 변경이 mapping에 반영되며 fork()에서 복사됩니다.
No-MMU에서는 다음 우선순위와 가시성 규칙을 사용합니다.
| 경로 | No-MMU 동작 |
|---|---|
| 기존 mapping 재사용 | 같은 file segment에 permission-compatible mapping이 있으면 다른 process가 만든 것도 재사용 |
| 직접 mapping | backing device가 NOMMU_MAP_DIRECT와 적절한 protection capability를 가지면 device에 직접 mapping. ramfs, romfs, cramfs, mtd가 허용할 수 있음 |
| copy mapping | 직접 공유할 수 없지만 NOMMU_MAP_COPY가 있으면 file 부분을 contiguous memory로 읽고 EOF 뒤 여분 공간을 clear |
| 변경 가시성 | file write는 mapping에 반영되지 않음. MMU 보호가 없어서 mapping write는 다른 process에 보이지만 발생해서는 안 됨 |
Writable private file mapping
59-68MMU에서 PROT_WRITE가 있는 private file mapping은 쓰기 직전에 대상 page를 복사합니다. 그 뒤 underlying file write는 그 page의 backing page에 더 이상 반영되지 않고, page는 swap으로 backing됩니다.
No-MMU에서는 !PROT_WRITE 사례와 비슷하지만 항상 copy를 만들고 절대 공유하지 않습니다.
No-MMU mmap 추가 제약
106-161| 항목 | 동작·제약 |
|---|---|
| Private file 정렬 | XIP와 backing-store 정렬 때문에 반환 buffer가 page-aligned가 아닐 수 있음 |
| Anonymous 정렬·크기 | 항상 page-aligned. power-of-two가 아니면 kernel의 power-of-2 granule 할당과 trimming 구성에 따라 공간 낭비·fragmentation 발생 |
| Anonymous 초기화 | 일반적으로 반환 전에 전체 physical map을 clear하므로 malloc() 시 C library mmap과 kernel memset이 큰 지연을 만들 수 있음 |
| MAP_UNINITIALIZED | preclear가 필요 없는 memory는 clear를 생략할 수 있으나 CONFIG_MMAP_ALLOW_UNINITIALIZED가 켜져야 함 |
| /proc/maps | system의 모든 private-copy 및 anonymous mapping 표시 |
| /proc/<pid>/maps | 해당 process가 사용하는 모든 mapping 표시 |
| 고정 주소·read method | MAP_FIXED 또는 특정 주소 요청은 오류. Private file을 direct mapping하지 못하면 driver/filesystem read method가 필요 |
Linux man pages (ver 2.22 or later)에 따라 anonymous mapping memory는 일반적으로 반환 전에 kernel이 clear합니다. MMU에서는 anonymous region이 virtual page로 backing되고 실제 write가 일어날 때 clear된 physical page를 붙입니다. 그전에는 global zero page에서 읽을 수 있어 초기화 비용이 write 사용량에 따라 분산됩니다.
No-MMU에서는 anonymous mapping 전체가 physical page로 backing되므로 할당 시 map 전체를 clear합니다. MAP_UNINITIALIZED는 malloc()처럼 preclear가 필요 없는 memory에서 이 비용을 피합니다. uClibc는 malloc() 가속에, ELF-FDPIC binfmt는 brk와 stack region 할당에 사용합니다.
CONFIG_MMAP_ALLOW_UNINITIALIZED가 꺼져 있으면 MAP_UNINITIALIZED flag는 무시됩니다. 초기화되지 않은 memory 노출의 security 영향을 검토해야 합니다.
Private file mapping을 device에 직접 붙이지 못하면 내용을 할당 memory로 읽을 read method가 driver 또는 filesystem에 있어야 합니다. 없으면 오류가 나며 chardev, pipe, fifo, socket에서 만날 가능성이 큽니다.
Shared memory와 futex
162-178NOMMU mode는 SYSV IPC SHM과 POSIX shared memory를 모두 지원합니다. 전자는 일반 mechanism을 사용하고 후자는 ramfs 또는 tmpfs mount에 만든 file을 사용합니다.
Architecture가 지원하면 futex도 사용할 수 있습니다. futex system call에 넘긴 address가 process mapping 밖에 있거나, 그 address가 속한 mapping이 I/O chardev mapping처럼 futex를 지원하지 않으면 오류가 반환됩니다.
No-MMU mremap
179-200mremap()은 일부만 지원합니다. mapping 크기는 바꿀 수 있고 MREMAP_MAYMOVE가 지정되며 새 크기가 현재 slab object보다 크거나 더 작은 slab object를 쓸 수 있으면 이동할 수 있다고 정의되어 있지만, 각주에 따르면 이동은 현재 지원되지 않습니다.
| 기능 | 제약 |
|---|---|
| resize | mapping 크기를 바꿀 수 있고 조건에 따라 MREMAP_MAYMOVE로 이동할 수 있지만 이동은 현재 미지원 |
| MREMAP_FIXED | 지원하지 않음. 주소가 변하지 않고 object를 이동할 필요가 없으면 무시 |
| shared/shareable | 현재 공유 여부와 관계없이 shared 또는 shareable mapping은 이동 불가 |
| 정확한 전체 mapping | 기존 object의 base address와 size가 정확히 일치해야 하며 hole 생성, 일부 이동·resize는 불가 |
mremap()에는 이전에 mapping한 object의 base address와 size를 정확히 넘겨야 합니다. Existing mapping에 hole을 만들거나 일부만 이동·resize할 수 없으며 항상 complete mapping 전체에 작동해야 합니다.
Shareable memory-backed file 지원
247-263Memory-backed file의 shared mapping 지원은 shared-mapped chardev와 비슷합니다. 주된 차이는 service를 제공하는 filesystem이 contiguous page collection을 할당하고 그 위에 mapping을 허용할 가능성이 크다는 점입니다.
빈 file의 크기를 늘리는 truncate는 mapping을 만족할 충분한 page를 모으라는 요청으로 처리하는 것이 권장됩니다. POSIX shared memory를 지원하려면 이 동작이 필요합니다. Memory-backed device는 mapping의 backing device info에 memory_backed flag를 설정해 표시합니다.
Page trimming 조정
272-283NOMMU mmap은 allocation할 때 page 수를 가장 가까운 상위 power-of-2로 자동 반올림합니다. 이는 memory fragmentation에 불리할 수 있어 구성 가능하게 되어 있습니다.
기본 동작은 allocation을 적극적으로 trim하고 excess page를 page allocator에 돌려줍니다. Fragmentation을 더 세밀하게 제어하려면 trimming을 완전히 끄거나 trimming을 시작할 더 높은 page watermark를 지정할 수 있습니다. 이 동작은 sysctl vm.nr_trim_pages로 구성합니다.
운영 핵심
nommu-mmap.rst:1-283No-MMU mapping은 virtual-memory remapping 대신 contiguous physical memory와 device capability에 의존합니다. Mapping flag만 볼 것이 아니라 filesystem·driver의 direct/copy 지원, page 초기화 비용, 이동 제한과 trimming 정책을 함께 검토해야 합니다.