← Documents Documentation/arch/arm/mem_alignment.rst GitHub 원문 ↗

Linux 6.18.37 · Architecture

Memory alignment

ARM unaligned access를 안전하게 처리하는 원칙과 /proc/cpu/alignment의 warning·fixup·SIGBUS bit를 설명합니다.

Source pathDocumentation/arch/arm/mem_alignment.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

요약과 해설

mem_alignment.rst:1-63

alignment trap은 잘못 정렬된 접근을 살려 내는 비상 경로이지 정상 data path가 아닙니다. 예측 가능한 접근은 `include/linux/unaligned.h`로 명시하고, 사용자 code는 `SIGBUS` mode로 결함을 찾아 고치는 편이 안전합니다.

Alignment trap mode bits
bit역할운영 판단
0상세 warning결함 위치 추적
1software fixup느림, production 비권장
2SIGBUS잘못된 사용자 code 조기 발견
undefinedundefined
undefinedundefined

각 bit는 warning, software fixup, signal이라는 서로 다른 대응을 선택합니다.

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

원문 전체 펼치기
1 ================
2 Memory alignment
3 ================
4
5 Too many problems popped up because of unnoticed misaligned memory access in
6 kernel code lately. Therefore the alignment fixup is now unconditionally
7 configured in for SA11x0 based targets. According to Alan Cox, this is a
8 bad idea to configure it out, but Russell King has some good reasons for
9 doing so on some f***ed up ARM architectures like the EBSA110. However
10 this is not the case on many design I'm aware of, like all SA11x0 based
11 ones.
12
13 Of course this is a bad idea to rely on the alignment trap to perform
14 unaligned memory access in general. If those access are predictable, you
15 are better to use the macros provided by include/linux/unaligned.h. The
16 alignment trap can fixup misaligned access for the exception cases, but at
17 a high performance cost. It better be rare.
18
19 Now for user space applications, it is possible to configure the alignment
20 trap to SIGBUS any code performing unaligned access (good for debugging bad
21 code), or even fixup the access by software like for kernel code. The later
22 mode isn't recommended for performance reasons (just think about the
23 floating point emulation that works about the same way). Fix your code
24 instead!
25
26 Please note that randomly changing the behaviour without good thought is
27 real bad - it changes the behaviour of all unaligned instructions in user
28 space, and might cause programs to fail unexpectedly.
29
30 To change the alignment trap behavior, simply echo a number into
31 /proc/cpu/alignment. The number is made up from various bits:
32
33 === ========================================================
34 bit behavior when set
35 === ========================================================
36 0 A user process performing an unaligned memory access
37 will cause the kernel to print a message indicating
38 process name, pid, pc, instruction, address, and the
39 fault code.
40
41 1 The kernel will attempt to fix up the user process
42 performing the unaligned access. This is of course
43 slow (think about the floating point emulator) and
44 not recommended for production use.
45
46 2 The kernel will send a SIGBUS signal to the user process
47 performing the unaligned access.
48 === ========================================================
49
50 Note that not all combinations are supported - only values 0 through 5.
51 (6 and 7 don't make sense).
52
53 For example, the following will turn on the warnings, but without
54 fixing up or sending SIGBUS signals::
55
56 echo 1 > /proc/cpu/alignment
57
58 You can also read the content of the same file to get statistical
59 information on unaligned access occurrences plus the current mode of
60 operation for user space code.
61
62
63 Nicolas Pitre, Mar 13, 2001. Modified Russell King, Nov 30, 2001.
64

3. 한국어 전문 번역

영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.

Memory alignment

1-4

ARM의 memory alignment 처리 정책과 사용자 공간 제어 방법을 설명합니다.

정렬 예외에 의존하지 않는 접근

5-29

최근 kernel code에서 눈에 띄지 않은 misaligned memory access 때문에 문제가 너무 많이 발생했습니다. 그래서 SA11x0 기반 target에는 alignment fixup이 무조건 설정됩니다. Alan Cox는 이를 빼는 것이 좋지 않다고 보았지만, Russell King은 EBSA110처럼 문제가 있는 일부 ARM architecture에서는 제외할 이유가 있다고 보았습니다. 다만 알려진 SA11x0 설계에는 그 사정이 적용되지 않습니다.

일반적인 unaligned memory access를 alignment trap에 맡기면 안 됩니다. 접근을 예측할 수 있다면 `include/linux/unaligned.h`가 제공하는 macro를 사용해야 합니다. trap은 예외적인 misaligned access를 고칠 수 있지만 성능 비용이 크므로 드물게만 발생해야 합니다.

사용자 프로그램에는 unaligned access를 수행한 code에 `SIGBUS`를 보내 잘못된 code를 찾는 방식과, kernel code처럼 software로 접근을 fixup하는 방식이 있습니다. 후자는 floating-point emulation과 비슷하게 매우 느리므로 권장하지 않습니다. 근본적으로 code를 고쳐야 합니다.

충분히 검토하지 않고 동작을 바꾸면 사용자 공간의 모든 unaligned instruction에 영향을 주어 기존 프로그램이 예기치 않게 실패할 수 있습니다.

/proc/cpu/alignment

30-62

`/proc/cpu/alignment`에 여러 bit를 조합한 숫자를 쓰면 alignment trap 동작을 바꿀 수 있습니다.

bit설정했을 때의 동작
`0`unaligned access를 한 process의 `process name`, `pid`, `pc`, `instruction`, `address`, `fault code`를 kernel message로 출력
`1`kernel이 사용자 process의 접근을 software로 fixup. 느리며 production 사용은 권장하지 않음
`2`해당 사용자 process에 `SIGBUS` signal 전송

지원하는 값은 `0`부터 `5`까지입니다. `6`과 `7`은 의미가 없는 조합입니다. 다음 예는 warning만 켜고 fixup이나 `SIGBUS`는 사용하지 않습니다.

echo 1 > /proc/cpu/alignment

같은 파일을 읽으면 unaligned access 발생 통계와 현재 사용자 공간 동작 mode를 확인할 수 있습니다.

저자와 수정 기록

63-63

Nicolas Pitre가 2001년 3월 13일 작성했고 Russell King이 2001년 11월 30일 수정했습니다.