RCU (Read-Copy-Update) 기초 — Quiescent State·Grace Period
#한 줄 요약
“RCU는 reader 경로를 가볍게 하고 writer가 reclamation 비용을 부담하는 read-mostly 동기화입니다.” 핵심은 grace period입니다. 갱신 전에 시작한 reader가 모두 끝난 뒤에야 옛 객체를 free할 수 있습니다. 이 글은 커널 모듈과 liburcu로 그 비용을 직접 재고, grace period를 건너뛰면 무슨 일이 나는지 AddressSanitizer로 봅니다.
#어떤 상황에서 쓰나
Routing table, 설정 객체, 모듈 목록처럼 읽기는 자주, 쓰기는 가끔인 데이터가 대상입니다. 도서관 열람실에 비유하면, 개정판이 나왔다고 지금 읽고 있는 사람의 책을 빼앗지 않고 새 책을 서가에 꽂아 둡니다. 옛 책은 그때 읽던 사람이 모두 자리를 뜬 뒤에 치웁니다. 새로 오는 사람은 처음부터 새 책을 집습니다.
Linux 커널 문서는 v4.20부터 한 커널에 RCU flavor가 하나만 있고, PREEMPTION=n이면 RCU-sched, PREEMPTION=y면 RCU-preempt라고 적습니다(v6.12 Documentation/RCU/checklist.rst 230행). 사용자 공간에서는 liburcu(Userspace RCU)가 같은 API 이름을 제공합니다.
#핵심 개념
| API | 동작 | 출처 (Linux v6.12) |
|---|---|---|
rcu_read_lock / rcu_read_unlock | read-side 구간 진입·탈출. 구간 안에서 block 금지 | Documentation/RCU/whatisRCU.rst 160–171행 |
rcu_dereference | 보호된 포인터 읽기 | include/linux/rcupdate.h 758행 |
rcu_assign_pointer | 새 객체 publish | rcupdate.h 594행 |
rcu_dereference_protected | updater가 lock을 쥔 채 읽기 | rcupdate.h 748행 |
synchronize_rcu | 그 시점까지 시작한 reader가 모두 끝날 때까지 sleep | kernel/rcu/tree.c 4084행, 4093행에서 rcu_blocking_is_gp()가 might_sleep() 호출(4003행) |
call_rcu | grace period 뒤 실행할 callback을 등록하고 바로 반환 | rcupdate.h 41행, whatisRCU.rst 232–238행 |
전형적인 update 흐름입니다.
- 새 객체를 할당하고 옛 객체를 복사한 뒤 고칩니다.
rcu_assign_pointer(global, new)로 바꿔 끼웁니다.synchronize_rcu()로 기다리거나call_rcu()로 해제를 예약합니다.- 옛 객체를 free합니다.
이 순서 덕분에 갱신 전부터 옛 객체를 들고 있던 reader가 끝나기 전에는 그 객체가 해제되지 않습니다. 대가는 세 가지입니다. Writer가 grace period만큼 기다리거나 callback이 그만큼 늦게 돕니다. 그동안 옛 객체와 새 객체가 함께 메모리에 있습니다. 그리고 writer끼리는 여전히 별도 lock이 필요합니다.
Grace period가 끝났는지는 quiescent state로 압니다. Read-side 구간 안에서는 block할 수 없으므로, 어떤 CPU가 context switch를 했다면 그 CPU는 그 전에 시작한 read-side 구간을 모두 마쳤고, 모든 CPU가 context switch를 한 번씩 하면 그 전의 read-side 구간은 모두 끝납니다(whatisRCU.rst 762–766행). Tree RCU 문서는 CPU가 context switch를 하면 rcu_note_context_switch()가, user mode에서 scheduler-clock 인터럽트를 받으면 rcu_sched_clock_irq()가 quiescent state를 기록한다고 적습니다(Documentation/RCU/Design/Memory-Ordering/Tree-RCU-Memory-Ordering.rst 449–452행). 모든 CPU가 grace period 시작 뒤 quiescent state를 한 번씩 지나면 그 grace period가 끝납니다.
#코드 / 실제 사용 예
#Linux kernel RCU — 측정 모듈
아래 모듈은 설정 객체를 21번 synchronize_rcu()로, 8번 call_rcu()로 갈아 끼우며 시간을 재고, 마지막으로 read-side 비용을 rwlock·spinlock과 비교합니다. 실행 환경은 Debian 13 arm64 guest(QEMU -accel hvf, vCPU 6개, Apple M2 Pro host)의 배포판 커널 6.12.116+deb13-arm64이고, 같은 버전의 linux-headers 패키지로 빌드했습니다.
// rcu_bench.c — grace-period latency and read-side cost on the running kernel#include <linux/module.h>#include <linux/rcupdate.h>#include <linux/slab.h>#include <linux/sort.h>#include <linux/spinlock.h>#include <linux/ktime.h>
struct config { int max_threads; struct rcu_head rcu; u64 queued_ns;};
static struct config __rcu *cfg;static DEFINE_SPINLOCK(cfg_lock);static DEFINE_RWLOCK(cfg_rwlock);static u64 cb_delay[8];static atomic_t cb_done;
static void config_free_cb(struct rcu_head *r){ struct config *c = container_of(r, struct config, rcu); int i = atomic_inc_return(&cb_done) - 1;
if (i < 8) cb_delay[i] = ktime_get_ns() - c->queued_ns; kfree(c);}
static int cmp_u64(const void *a, const void *b){ u64 x = *(const u64 *)a, y = *(const u64 *)b; return x < y ? -1 : x > y;}
static int __init rcu_bench_init(void){ u64 t[21], t0; struct config *new, *old; unsigned long sum = 0; int i;
new = kzalloc(sizeof(*new), GFP_KERNEL); if (!new) return -ENOMEM; RCU_INIT_POINTER(cfg, new);
/* 1) update with synchronize_rcu(): how long does the updater sleep? */ for (i = 0; i < 21; i++) { new = kzalloc(sizeof(*new), GFP_KERNEL); if (!new) return -ENOMEM; new->max_threads = i; spin_lock(&cfg_lock); old = rcu_dereference_protected(cfg, lockdep_is_held(&cfg_lock)); rcu_assign_pointer(cfg, new); spin_unlock(&cfg_lock); t0 = ktime_get_ns(); synchronize_rcu(); t[i] = ktime_get_ns() - t0; kfree(old); } sort(t, 21, sizeof(t[0]), cmp_u64, NULL); pr_info("rcu_bench: synchronize_rcu() x21: min %llu us, median %llu us, max %llu us\n", t[0] / 1000, t[10] / 1000, t[20] / 1000);
/* 2) update with call_rcu(): the updater does not sleep */ for (i = 0; i < 8; i++) { new = kzalloc(sizeof(*new), GFP_KERNEL); if (!new) return -ENOMEM; spin_lock(&cfg_lock); old = rcu_dereference_protected(cfg, lockdep_is_held(&cfg_lock)); rcu_assign_pointer(cfg, new); spin_unlock(&cfg_lock); old->queued_ns = ktime_get_ns(); t0 = ktime_get_ns(); call_rcu(&old->rcu, config_free_cb); t[i] = ktime_get_ns() - t0; } rcu_barrier(); /* wait for all callbacks before reading cb_delay */ sort(t, 8, sizeof(t[0]), cmp_u64, NULL); sort(cb_delay, 8, sizeof(cb_delay[0]), cmp_u64, NULL); pr_info("rcu_bench: call_rcu() returned in max %llu ns; callback ran %llu..%llu us later\n", t[7], cb_delay[0] / 1000, cb_delay[7] / 1000);
/* 3) single-thread read-side cost, 10M iterations each */#define LOOPS 10000000 t0 = ktime_get_ns(); for (i = 0; i < LOOPS; i++) { rcu_read_lock(); sum += rcu_dereference(cfg)->max_threads; rcu_read_unlock(); } t[0] = ktime_get_ns() - t0; t0 = ktime_get_ns(); for (i = 0; i < LOOPS; i++) { read_lock(&cfg_rwlock); sum += rcu_dereference_raw(cfg)->max_threads; read_unlock(&cfg_rwlock); } t[1] = ktime_get_ns() - t0; t0 = ktime_get_ns(); for (i = 0; i < LOOPS; i++) { spin_lock(&cfg_lock); sum += rcu_dereference_raw(cfg)->max_threads; spin_unlock(&cfg_lock); } t[2] = ktime_get_ns() - t0; pr_info("rcu_bench: per read (ps): rcu_read_lock %llu, read_lock %llu, spin_lock %llu (sum %lu)\n", t[0] * 1000 / LOOPS, t[1] * 1000 / LOOPS, t[2] * 1000 / LOOPS, sum); return 0;}
static void __exit rcu_bench_exit(void){ struct config *c = rcu_dereference_protected(cfg, 1);
RCU_INIT_POINTER(cfg, NULL); synchronize_rcu(); kfree(c);}
module_init(rcu_bench_init);module_exit(rcu_bench_exit);MODULE_LICENSE("GPL");obj-m += rcu_bench.o rcu_list.oall: $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules# Debian 13 arm64 guest, kernel 6.12.116+deb13-arm64, CONFIG_HZ=250, CONFIG_PREEMPT_VOLUNTARY=y, CONFIG_PREEMPT_COUNT not set; gcc 14.2.0$ make && insmod rcu_bench.ko && rmmod rcu_bench # 3 times$ insmod rcu_list.ko && rmmod rcu_list$ dmesg[ 3904.176611] rcu_bench: synchronize_rcu() x21: min 15313 us, median 16123 us, max 39015 us[ 3904.196130] rcu_bench: call_rcu() returned in max 792 ns; callback ran 18511..18512 us later[ 3904.411922] rcu_bench: per read (ps): rcu_read_lock 396, read_lock 8191, spin_lock 12933 (sum 0)[ 3904.884189] rcu_bench: synchronize_rcu() x21: min 14419 us, median 16141 us, max 44779 us[ 3904.899484] rcu_bench: call_rcu() returned in max 1000 ns; callback ran 14572..14572 us later[ 3905.035698] rcu_bench: per read (ps): rcu_read_lock 356, read_lock 8589, spin_lock 4618 (sum 0)[ 3905.464290] rcu_bench: synchronize_rcu() x21: min 13206 us, median 16467 us, max 20388 us[ 3905.480140] rcu_bench: call_rcu() returned in max 666 ns; callback ran 15487..15488 us later[ 3905.630218] rcu_bench: per read (ps): rcu_read_lock 312, read_lock 9959, spin_lock 4695 (sum 0)[ 3905.690608] rcu_list: sum before 6[ 3905.704158] rcu_list: sum after removing key 2: 4[ 3905.704354] rcu_list: entries visited by scan(), summed over CPUs: 5읽는 법은 다음과 같습니다.
synchronize_rcu()는 이 커널에서 한 번에 13.244.8 ms 잤습니다(세 번 실행의 중앙값 16.116.5 ms). 이 커널은CONFIG_HZ=250(tick 4 ms)이라 grace period가 tick 여러 개에 걸칩니다. 설정 하나를 바꾸는 데는 문제없지만, 루프에서 부르면 반복마다 이만큼 멈춥니다.call_rcu()는 최대 1,000 ns 만에 반환했고, callback은 14.6~18.5 ms 뒤에 돌았습니다. 8개가 같은 grace period를 기다려 한꺼번에 돌았으므로 최소와 최대가 거의 같습니다. Updater가 기다리지 않는 대신 해제가 그만큼 늦습니다.- Read-side 비용은 반복당 0.31
0.40 ns로,9.96 ns)이나read_lock(8.19spin_lock(4.62~12.93 ns)보다 한 자릿수 이상 작았습니다. 셋 다 경합 없는 단일 스레드 값이고,spin_lock의 큰 값 하나는 세 번 중 한 번에만 나왔습니다. 이 커널은CONFIG_PREEMPT_RCU가 아니어서__rcu_read_lock()이preempt_disable()이고(rcupdate.h91–94행),CONFIG_PREEMPT_COUNT도 꺼져 있어preempt_disable()은barrier(), 즉 컴파일러 장벽일 뿐입니다(include/linux/preempt.h286행). 그래서 측정된 것은 사실상 포인터 하나를 읽고 더하는 비용입니다.CONFIG_PREEMPT_RCU커널에서는 read-side가 카운터를 건드리므로 이 숫자가 그대로 옮겨지지 않습니다.
rwlock·spinlock 숫자는 경합 없는 단일 스레드 값입니다. 여러 reader가 동시에 읽을 때의 차이는 아래 사용자 공간 측정에서 봅니다.
#URCU (User-space RCU)
liburcu의 기본 flavor(urcu.h를 그대로 include하면 urcu-memb)에서는 read-side 구간이 있는 스레드가 rcu_register_thread()로 등록해야 합니다(liburcu 0.15.2 urcu/urcu-memb.h 40행 “Each thread containing read-side critical sections must be registered”). 같은 헤더는 rcu_init()을 “library constructors” 안에서 RCU를 일찍 쓸 때를 위한 명시적 초기화로 설명하므로, 일반 main()에서는 부르지 않아도 됩니다.
아래 프로그램에서는 reader 4개가 설정 객체를 잡고 10 µs 동안 쓰는 사이 updater가 객체를 1,998번 갈아 끼웁니다. -DBUG_FREE_EARLY로 빌드하면 synchronize_rcu()를 빼고 바로 free()합니다.
// urcu_cfg.c — readers use a config object while an updater replaces it#define _LGPL_SOURCE#include <urcu.h>#include <pthread.h>#include <stdatomic.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>
struct config { int max_threads; int timeout_ms; };static struct config *cfg;static atomic_int stop;static pthread_barrier_t start; /* updater waits until all readers run */
static void *reader(void *arg){ unsigned long reads = 0, bad = 0; (void)arg; rcu_register_thread(); pthread_barrier_wait(&start); while (!atomic_load_explicit(&stop, memory_order_relaxed)) { rcu_read_lock(); struct config *c = rcu_dereference(cfg); int a = c->max_threads; usleep(10); /* hold the pointer for a while */ if (c->timeout_ms != a * 10) bad++; /* invariant written by the updater */ rcu_read_unlock(); reads++; } rcu_unregister_thread(); printf("reader: %lu reads, %lu inconsistent\n", reads, bad); return NULL;}
int main(void){ pthread_t t[4]; cfg = malloc(sizeof *cfg); *cfg = (struct config){ 1, 10 }; pthread_barrier_init(&start, NULL, 5); for (int i = 0; i < 4; i++) pthread_create(&t[i], NULL, reader, NULL); pthread_barrier_wait(&start); for (int v = 2; v < 2000; v++) { struct config *new = malloc(sizeof *new); *new = (struct config){ v, v * 10 }; struct config *old = rcu_xchg_pointer(&cfg, new);#ifndef BUG_FREE_EARLY synchronize_rcu(); /* wait for readers that may hold old */#endif free(old); } atomic_store(&stop, 1); for (int i = 0; i < 4; i++) pthread_join(t[i], NULL); puts("updater: 1998 updates done"); return 0;}Debian 13 guest에서 gcc 14.2.0과 liburcu 0.15.2(apt install liburcu-dev)로 AddressSanitizer를 켜고 돌린 결과입니다.
$ gcc -O2 -g -fsanitize=address urcu_cfg.c -o ok -lurcu && ./okreader: 2015 reads, 0 inconsistentreader: 2020 reads, 0 inconsistentreader: 2020 reads, 0 inconsistentreader: 2028 reads, 0 inconsistentupdater: 1998 updates doneexit=0$ gcc -O2 -g -fsanitize=address -DBUG_FREE_EARLY urcu_cfg.c -o bug -lurcu && ./bug===================================================================786258==ERROR: AddressSanitizer: heap-use-after-free on address 0x502000000250 at pc 0xaaaaad1b1b20 bp 0xffffb47ee640 sp 0xffffb47ee658READ of size 4 at 0x502000000250 thread T2 #0 0xaaaaad1b1b1c in reader /root/p9fix/903/urcu_cfg.c:24 #1 0xffffb84ae2a8 in asan_thread_start ../../../../src/libsanitizer/asan/asan_interceptors.cpp:234 #2 0xffffb82e61d4 (/lib/aarch64-linux-gnu/libc.so.6+0x861d4) (BuildId: 5f3926d9963ff284b8173bf3601bbb00c3abad16) #3 0xffffb834e158 (/lib/aarch64-linux-gnu/libc.so.6+0xee158) (BuildId: 5f3926d9963ff284b8173bf3601bbb00c3abad16)
0x502000000250 is located 0 bytes inside of 8-byte region [0x502000000250,0x502000000258)freed by thread T0 here: #0 0xffffb85292b4 in free ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:52 #1 0xaaaaad1b1448 in main /root/p9fix/903/urcu_cfg.c:50 #2 0xffffb8282258 (/lib/aarch64-linux-gnu/libc.so.6+0x22258) (BuildId: 5f3926d9963ff284b8173bf3601bbb00c3abad16) #3 0xffffb8282338 in __libc_start_main (/lib/aarch64-linux-gnu/libc.so.6+0x22338) (BuildId: 5f3926d9963ff284b8173bf3601bbb00c3abad16) #4 0xaaaaad1b15ec in _start (/root/p9fix/903/bug+0x15ec) (BuildId: adfbf933f7d8f405e4c5d299387dafeea00f39e6)
previously allocated by thread T0 here: #0 0xffffb852a400 in malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:69 #1 0xaaaaad1b13e4 in main /root/p9fix/903/urcu_cfg.c:44 #2 0xffffb8282258 (/lib/aarch64-linux-gnu/libc.so.6+0x22258) (BuildId: 5f3926d9963ff284b8173bf3601bbb00c3abad16)...SUMMARY: AddressSanitizer: heap-use-after-free /root/p9fix/903/urcu_cfg.c:24 in readerexit=1synchronize_rcu()가 있으면 reader 네 개가 각자 2,015~2,028번 읽는 동안 어긋난 값도 ASan 경고도 없었습니다. 빼면 reader가 아직 쓰는 객체를 updater가 해제해, ASan이 reader의 c 읽기에서 heap-use-after-free를 잡았습니다. 위 로그는 24행(c->max_threads) 읽기입니다. “freed by thread T0” 스택은 main의 free(old)(50행)를, “previously allocated” 스택은 updater 루프의 malloc(44행)을 가리킵니다. 두 실행 모두 pthread_barrier로 reader 네 개가 읽기를 시작한 뒤에 updater가 돌게 했습니다. 실제 장비에서는 이 버그가 경고 없이 엉뚱한 설정값으로 나타납니다.
#Reader 수가 늘 때 — rwlock vs URCU
같은 guest에서 설정 객체 하나를 reader 1·2·4·5개가 1초 동안 읽고, writer 하나가 1 ms마다 갱신하는 동안 읽은 횟수를 셌습니다.
// readscale.c — read throughput of one shared config under rwlock vs URCU#define _LGPL_SOURCE#include <urcu.h>#include <pthread.h>#include <stdatomic.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <unistd.h>
struct config { long a, b; };static struct config *cfg;static pthread_rwlock_t rw = PTHREAD_RWLOCK_INITIALIZER;static atomic_int stop;static int use_rcu;
static void *reader(void *arg){ unsigned long n = 0; long sum = 0; if (use_rcu) rcu_register_thread(); while (!atomic_load_explicit(&stop, memory_order_relaxed)) { if (use_rcu) { rcu_read_lock(); struct config *c = rcu_dereference(cfg); sum += c->a + c->b; rcu_read_unlock(); } else { pthread_rwlock_rdlock(&rw); sum += cfg->a + cfg->b; pthread_rwlock_unlock(&rw); } n++; } if (use_rcu) rcu_unregister_thread(); *(unsigned long *)arg = n + (sum == 42); /* keep sum alive */ return NULL;}
static void *writer(void *arg) /* one update per millisecond */{ (void)arg; for (long v = 1; !atomic_load_explicit(&stop, memory_order_relaxed); v++) { struct config *new = malloc(sizeof *new); new->a = v; new->b = -v; if (use_rcu) { struct config *old = rcu_xchg_pointer(&cfg, new); synchronize_rcu(); free(old); } else { pthread_rwlock_wrlock(&rw); struct config *old = cfg; cfg = new; pthread_rwlock_unlock(&rw); free(old); } usleep(1000); } return NULL;}
int main(int argc, char **argv){ int readers = atoi(argv[2]); use_rcu = strcmp(argv[1], "rcu") == 0; pthread_t t[16], w; unsigned long cnt[16], total = 0; cfg = calloc(1, sizeof *cfg); for (int i = 0; i < readers; i++) pthread_create(&t[i], NULL, reader, &cnt[i]); pthread_create(&w, NULL, writer, NULL); sleep(1); atomic_store(&stop, 1); for (int i = 0; i < readers; i++) { pthread_join(t[i], NULL); total += cnt[i]; } pthread_join(w, NULL); printf("%-6s readers=%d %6.1f M reads/s\n", argv[1], readers, total / 1e6); return 0;}host: Debian 13 arm64 guest (QEMU -accel hvf, 6 vCPU) on Apple M2 Pro; kernel 6.12.116+deb13-arm64; gcc 14.2.0; liburcu 0.15.2 (urcu-memb flavor)$ gcc -O2 -Wall readscale.c -o readscale -lurcu$ for r in 1 2 3; do echo "# round $r"; for n in 1 2 4 5; do ./readscale rwlock $n; ./readscale rcu $n; done; done# round 1rwlock readers=1 106.4 M reads/srcu readers=1 759.5 M reads/srwlock readers=2 40.0 M reads/srcu readers=2 1507.3 M reads/srwlock readers=4 10.9 M reads/srcu readers=4 2987.0 M reads/srwlock readers=5 10.1 M reads/srcu readers=5 3723.6 M reads/s# round 2rwlock readers=1 108.0 M reads/srcu readers=1 753.4 M reads/srwlock readers=2 40.5 M reads/srcu readers=2 1509.3 M reads/srwlock readers=4 11.0 M reads/srcu readers=4 2968.1 M reads/srwlock readers=5 9.7 M reads/srcu readers=5 3723.6 M reads/s# round 3rwlock readers=1 114.7 M reads/srcu readers=1 747.7 M reads/srwlock readers=2 39.8 M reads/srcu readers=2 1499.7 M reads/srwlock readers=4 12.1 M reads/srcu readers=4 2967.4 M reads/srwlock readers=5 9.7 M reads/srcu readers=5 3720.4 M reads/spthread_rwlock은 reader가 하나일 때 초당 106115M 번이었다가 둘이 되자 39.840.5M 번으로 떨어졌고, 다섯이면 9.710.1M 번이었습니다. Guest의 glibc 2.41에서 읽기 lock은 760M, 5개 3720~3724M). 이것은 이 guest의 숫자이고, 측정 루프가 하는 일이 포인터 두 번 읽기뿐이라 lock 비용이 그대로 드러나는 조건입니다.rwlock->__data.__readers에 atomic_fetch_add_acquire를 합니다(nptl/pthread_rwlock_common.c 353–355행). Reader가 모두 같은 word에 쓰므로 그 cache line이 코어 사이를 오갑니다. URCU reader는 공유 word에 쓰지 않아 reader 수에 따라 늘었습니다(1개 748
#call_rcu (비동기 free)
synchronize_rcu()는 sleep하므로 sleep할 수 없는 문맥에서는 쓸 수 없습니다. 문서는 call_rcu()를 “Instead of blocking, it registers a function and argument which are invoked after all ongoing RCU read-side critical sections have completed”라고 설명하고, block이 금지된 곳이나 update 쪽 성능이 중요한 곳에 쓰라고 적습니다(whatisRCU.rst 232–238행). 위 rcu_bench.c의 config_free_cb()가 그 형태입니다. 객체 안에 struct rcu_head를 두고, callback에서 container_of()로 바깥 객체를 찾아 해제합니다. 모듈을 내리기 전에는 rcu_barrier()로 남은 callback이 모두 돌 때까지 기다려야 합니다. 그래야 callback 코드가 사라진 뒤에 불리는 일이 없습니다.
#List 변경 (rculist.h)
RCU는 reader와 writer 사이만 정리해 줄 뿐 writer끼리의 충돌은 막지 않습니다. 그래서 리스트를 고치는 쪽은 spinlock을 잡고, 읽는 쪽은 rcu_read_lock()만 잡습니다. 아래 모듈은 원소 세 개를 넣고 key 2를 지운 뒤 합계를 다시 셉니다. 같은 모듈에서 per-CPU 카운터도 보여 줍니다.
// rcu_list.c — RCU-protected list: writers serialize on a spinlock, readers do not lock#include <linux/module.h>#include <linux/percpu.h>#include <linux/rculist.h>#include <linux/slab.h>#include <linux/spinlock.h>
struct entry { struct list_head list; int key;};
static LIST_HEAD(g_list);static DEFINE_SPINLOCK(list_lock); /* writer vs writer only */static DEFINE_PER_CPU(unsigned long, hits);
static int add_entry(int key){ struct entry *e = kmalloc(sizeof(*e), GFP_KERNEL);
if (!e) return -ENOMEM; e->key = key; spin_lock(&list_lock); list_add_rcu(&e->list, &g_list); spin_unlock(&list_lock); return 0;}
static void remove_entry(struct entry *e){ spin_lock(&list_lock); list_del_rcu(&e->list); spin_unlock(&list_lock); synchronize_rcu(); /* readers may still be on e */ kfree(e);}
static int scan(void){ struct entry *e; int sum = 0;
rcu_read_lock(); list_for_each_entry_rcu(e, &g_list, list) { sum += e->key; this_cpu_inc(hits); } rcu_read_unlock(); return sum;}
static int __init rcu_list_init(void){ struct entry *e, *victim = NULL; unsigned long total = 0; int cpu, i;
for (i = 1; i <= 3; i++) if (add_entry(i)) return -ENOMEM; pr_info("rcu_list: sum before %d\n", scan());
spin_lock(&list_lock); list_for_each_entry(e, &g_list, list) if (e->key == 2) victim = e; spin_unlock(&list_lock); if (victim) remove_entry(victim); pr_info("rcu_list: sum after removing key 2: %d\n", scan());
for_each_possible_cpu(cpu) total += per_cpu(hits, cpu); pr_info("rcu_list: entries visited by scan(), summed over CPUs: %lu\n", total); return 0;}
static void __exit rcu_list_exit(void){ struct entry *e, *tmp;
list_for_each_entry_safe(e, tmp, &g_list, list) { list_del_rcu(&e->list); synchronize_rcu(); kfree(e); }}
module_init(rcu_list_init);module_exit(rcu_list_exit);MODULE_LICENSE("GPL");위 로그의 rcu_list: 세 줄이 결과입니다. 합계가 6에서 4로 바뀌었고, scan()이 두 번 돌며 원소를 3 + 2 = 5번 방문했습니다. list_add_rcu·list_del_rcu·list_for_each_entry_rcu는 v6.12 include/linux/rculist.h 104, 155, 392행에 있습니다.
#Per-CPU counter
hits는 RCU가 아니라 per-CPU 변수입니다. 각 CPU가 자기 사본만 올리므로 공유 cache line을 쓰지 않고, 합계는 읽을 때 모읍니다. 커널 문서는 this_cpu 연산이 현재 CPU의 변수를 다른 CPU와의 동기화 비용 없이 다룬다고 설명합니다(Documentation/core-api/this_cpu_ops.rst 18–27행). RCU reader가 공유 word에 쓰지 않아서 확장되는 것과 같은 이유입니다. 다만 for_each_possible_cpu 합계는 다른 CPU가 올리는 중에 읽으면 그 순간의 근삿값입니다.
#측정 / 성능 비교
위 실행 결과를 한 표로 모으면 다음과 같습니다.
| 항목 | 측정값 | 환경 |
|---|---|---|
rcu_read_lock + 포인터 읽기 + unlock | 0.31~0.40 ns/반복 | 커널 모듈, 단일 스레드 |
read_lock + 읽기 + unlock | 8.19~9.96 ns/반복 | 같음 |
spin_lock + 읽기 + unlock | 4.62~12.93 ns/반복 | 같음 |
synchronize_rcu() 1회 | 13.2 | CONFIG_HZ=250 |
call_rcu() 반환 | 최대 1,000 ns | callback은 14.6~18.5 ms 뒤 |
reader 5개 처리량, pthread_rwlock | 9.7~10.1M reads/s | liburcu 0.15.2, writer 1 ms 주기 |
| reader 5개 처리량, URCU | 3720~3724M reads/s | 같음 |
환경은 모두 Debian 13 arm64 guest, 커널 6.12.116+deb13-arm64(CONFIG_PREEMPT_VOLUNTARY=y), gcc 14.2.0입니다. 가상 머신이라 host 부하에 따라 흔들리고, 다른 보드에서는 다시 재야 하는 숫자입니다. 표가 보여 주는 방향은 분명합니다. Reader 쪽은 lock보다 훨씬 싸고 reader 수에 따라 늘어나지만, writer 쪽은 수십 ms를 기다리거나 그만큼 늦게 해제합니다. 쓰기가 잦으면 이 대가가 커지므로 read-mostly일 때 고릅니다.
#자주 보는 함정
⚠️
rcu_read_lock밖에서rcu_dereference
struct config *c = rcu_dereference(cfg); /* not inside rcu_read_lock() */rcu_dereference(p)는 rcu_dereference_check(p, 0)이고(rcupdate.h 758행), CONFIG_PROVE_RCU 커널에서는 read-side 구간 밖에서 부르면 “suspicious rcu_dereference_check() usage” 경고를 냅니다(539행). 배포판 커널처럼 이 옵션이 꺼져 있으면 경고 없이 지나가므로, 개발용 커널에서 켜 두고 잡습니다. 구간 밖에서 얻은 포인터는 grace period가 그것을 지켜 주지 않으므로 위 URCU 예의 use-after-free와 같은 상황이 됩니다.
⚠️ Read-side 구간 안에서 sleep
rcu_read_lock();msleep(10); /* illegal: blocks inside an RCU read-side critical section */rcu_read_unlock();문서는 “It is illegal to block while in an RCU read-side critical section”이라고 적습니다(whatisRCU.rst 167–168행). 구간 안에서 sleep해야 한다면 SRCU(include/linux/srcu.h, “Sleepable Read-Copy Update”)를 쓰고, 이때 updater도 synchronize_srcu()/call_srcu()를 같은 srcu_struct로 써야 합니다(checklist.rst 238–240행).
⚠️ Writer끼리 lock 없이 add/del
list_add_rcu(&e->list, &g_list); /* two writers at once corrupt the list */rcu_list.c의 list_lock처럼 writer끼리는 별도 lock이 필요합니다.
⚠️ Hot path에서
synchronize_rcu
for (i = 0; i < N; i++) { rcu_assign_pointer(p, new[i]); synchronize_rcu(); /* ~16 ms median on the kernel measured above */}위 측정 기준이면 N = 100에서 1.6초쯤 멈춥니다. 갱신을 묶어 grace period 한 번으로 덮거나 call_rcu()로 넘깁니다. 문서도 expedited primitive를 루프에서 부르고 있다면 갱신을 묶으라고 권합니다(checklist.rst 220–228행).
⚠️ URCU에서
rcu_register_thread누락
rcu_read_lock(); /* this thread never called rcu_register_thread() */urcu-memb flavor는 read-side 구간이 있는 모든 스레드의 등록을 요구합니다(urcu/urcu-memb.h 40행). 등록하지 않은 스레드는 synchronize_rcu()가 기다려 줄 reader 목록에 없습니다.
#정리
- RCU reader는 이 커널에서 사실상 컴파일러 장벽뿐이라 반복당 0.31~0.40 ns였고,
rwlock과 달리 reader 수에 따라 처리량이 늘었습니다. - 대가는 writer 쪽입니다.
synchronize_rcu()는 13.2~44.8 ms 잤고,call_rcu()callback은 그만큼 늦게 돌았습니다. - Grace period를 건너뛰고 free하면 ASan이 heap-use-after-free를 잡았습니다.
- Writer끼리는 별도 lock이 필요하고, read-side 구간에서는 block할 수 없습니다. Block이 필요하면 SRCU를 씁니다.
- 사용자 공간은 liburcu로 같은 패턴을 쓰고, reader 스레드는 등록해야 합니다.
- 쓰기가 잦은 데이터에는 grace period 비용이 쌓이므로 rwlock 같은 다른 방법을 고릅니다.
다음 편은 9-04: Hazard Pointer입니다. GC 없이 lock-free 구조의 메모리를 회수하는 또 다른 방법입니다.
#관련 항목
Modern Embedded Recipes · 104 of 153 전체 목차 접기
- 1 Modern Embedded Recipes — 모던 임베디드 실전 레시피 시리즈 소개
- 2 디지털 신호 기초 — Voltage Level·Edge·Noise Margin·반사 분석
- 3 임베디드 클럭과 타이밍 — Skew·Jitter·PLL 분석
- 4 GPIO 내부 구조 분해 — Push-Pull·Open-Drain·Schmitt Trigger
- 5 UART 하드웨어 동작 분석 — Baud Rate·Framing·FIFO
- 6 SPI 하드웨어 분석 — Clock Mode·MOSI/MISO·Chip Select
- 7 I2C 하드웨어 분석 — Open-Drain·Clock Stretching·Arbitration
- 8 ADC 동작 원리 — SAR·Sigma-Delta 비교
- 9 DAC 동작 원리 — R-2R Ladder·Sigma-Delta·Settling Time
- 10 PWM 신호 생성 분석 — Duty·Frequency·Dead Time·Center-Aligned
- 11 CAN 버스 전기적 특성 — 종단·전압 레벨·GND 전위차와 1 Mbit/s 버스 길이 예산
- 12 RS-485·RS-422 차동 신호 분석 — Termination·Biasing·Topology
- 13 LVDS 차동 신호 분석 — Common-Mode·Impedance·Eye Pattern
- 14 ARM Cortex-M 시리즈 비교 — M0·M3·M4·M7·M33·M55 분석
- 15 ARM Cortex-A 시리즈 비교 — A53·A55·A72·A78·X1 분석
- 16 Cortex-M 레지스터 구조 분석 — R0~R15·xPSR·CONTROL·Mask Registers
- 17 Cortex-M4 예외 처리 — Vector Table·NVIC 우선순위·Tail-Chaining
- 18 ARM 메모리 맵 분석 — Normal·Device·Strongly-Ordered Region
- 19 Cortex-M7 캐시와 DMA — Cache Line·Write-Back·Clean/Invalidate 순서
- 20 Cortex-M4 MPU 활용 — Region 배치·Stack Guard·Task 격리
- 21 AArch64 Linux MMU 기초 — Translation Table·TLB·ASID
- 22 ARM TrustZone-M 기초 — Secure/Non-Secure·NSC·MPC
- 23 ARM Memory Barrier 실전 — DMB·DSB·ISB·DMA·MMIO
- 24 임베디드 크로스 컴파일러 분석 — GCC·Clang·Sysroot 구성
- 25 C 컴파일 4단계 — Preprocess·Compile·Assemble·Link 추적
- 26 ELF 파일 구조 분석 — Section·Segment·Symbol Table·DWARF
- 27 링커 스크립트 기초 — SECTIONS·MEMORY·entry point
- 28 링커 스크립트 고급 — Overlay·BSS·init_array·LMA/VMA
- 29 임베디드 스타트업 코드 분석 — Reset_Handler·Vector Table·SystemInit
- 30 C 런타임 crt0 분석 — Stack·BSS Zero·Data Copy·atexit
- 31 임베디드 메모리 레이아웃 — .text·.rodata·.data·.bss·.heap·.stack
- 32 임베디드 컴파일러 최적화 분석 — -O0~-O3·-Os·-LTO 비교
- 33 Map 파일 분석 — 링커가 넣은 것, 끌어온 이유, 버린 것 읽기
- 34 Make·CMake 크로스 컴파일 — Toolchain File·Sysroot·Preset
- 35 임베디드 Bootloader 체인 — BootROM·SPL·U-Boot·Kernel·Secure Boot
- 36 첫 bare-metal 프로그램 작성 — Linker·Startup·main의 최소 구성
- 37 MMIO 레지스터 직접 접근 — volatile·Memory Map·Aliasing 분석
- 38 GPIO 드라이버 직접 구현 — STM32F407을 HAL 없이 레지스터로
- 39 임베디드 클럭 설정 분석 — HSE·PLL·SYSCLK·AHB/APB 분주
- 40 Cortex-M 인터럽트 핸들링 — NVIC·Priority·Vector·EXTI
- 41 SysTick 타이머 활용 — 24-bit Counter·1ms Tick·delay 구현
- 42 UART 드라이버 구현 — polling·interrupt·DMA 3가지 방식 비교
- 43 SPI 드라이버 구현 — Master·Slave·CRC·DMA
- 44 I2C 드라이버 구현 — STM32F405/407 Master·Repeated START·Clock Stretching·Bus Recovery
- 45 임베디드 DMA 기초 — Memory-to-Memory·Peripheral·Circular Mode
- 46 저전력 모드 분석 — Sleep·Stop·Standby·Wake-up Source
- 47 STM32F405/407 IWDG·WWDG 워치독 구현 — Independent vs Window 비교
- 48 임베디드 Flash 프로그래밍 — Erase·Program·Read While Write
- 49 DDR 초기화 실패 진단 — Timing·Calibration·Walking Bit Test
- 50 PWM 출력 실전 — LED 밝기·모터 속도 제어
- 51 DC 모터 제어 — H-Bridge·PWM Duty·Encoder Feedback
- 52 스테퍼 모터 제어 — Full Step·Half Step·Microstepping
- 53 서보 모터 제어 — 50 Hz PWM·다채널·Calibration
- 54 Character LCD 제어 — HD44780·4-bit Mode·Custom Char
- 55 SPI OLED 제어 — SSD1306·Frame Buffer·Page 단위 갱신
- 56 TFT 디스플레이 구동 — ILI9341 SPI와 STM32F746 LTDC
- 57 환경 센서 활용 — BME280 온습압·SHT3x 비교
- 58 IMU 센서 활용 — MPU-6050·Data Ready·Complementary Filter
- 59 CAN 통신 구현 — bxCAN Bit Timing·Filter·Mailbox·Error 상태
- 60 USB Device 기초 — TinyUSB CDC·Descriptor·STM32F407 OTG_FS
- 61 Ethernet MAC+PHY 통합 — RMII·MDIO·lwIP Raw API
- 62 SD 카드 SPI 모드 + FatFs — 초기화·diskio 포팅·다중 블록 쓰기와 f_sync
- 63 STM32F4 RTC 활용 — Calendar·Alarm·Sub-second·Tamper·Backup Register
- 64 RTOS 도입 결정 분석 — Super Loop vs RTOS 트레이드오프
- 65 RTOS Task 설계 패턴 — 우선순위·스택·State Machine
- 66 RTOS Scheduler 동작 분석 — Tick·Context Switch·Yield
- 67 RTOS Semaphore 활용 — Binary·Counting·ISR Give
- 68 RTOS Mutex 활용 — Recursive·Priority Inheritance 적용
- 69 RTOS Queue 활용 — By-Value·By-Reference·Timeout 패턴
- 70 RTOS Event Group 활용 — Bit Wait·Sync·Notify
- 71 RTOS Software Timer 활용 — One-shot·Auto-reload·Daemon Task
- 72 ISR-Safe API 설계 — Reentrant·Atomic·Defer 패턴
- 73 Priority Inversion 진단·예방 — Mars Pathfinder Lesson 추적
- 74 Timer Wheel 분석 — Hashed·Hierarchical·O(1) Tick
- 75 RTOS 디버깅 기법 — Stack·Heap·Run-time Stats·SystemView
- 76 i.MX8M Mini EVK Linux 부팅 흐름 — BootROM에서 PID 1까지 단계별 인계
- 77 U-Boot 활용 — bootcmd·env·tftp·boot.scr 분석
- 78 Device Tree 실전 — DTS·DTB·Overlay·Phandle 추적
- 79 Device Tree Overlay 적용 — Runtime fragment·dtoverlay
- 80 임베디드 커널 빌드 — defconfig·menuconfig·Image·deb 패키지
- 81 커널 모듈 기초 — init/exit·Parameter·KBuild·modprobe
- 82 캐릭터 드라이버 작성 — file_operations·cdev·register_chrdev
- 83 Platform 드라이버 작성 — of_match·devm 해제 순서·probe 실패 판정 (Linux v6.12)
- 84 mmap 4가지 모드 — Anonymous·File·Shared·Huge Page
- 85 epoll 실전 — LT·ET·ONESHOT·EXCLUSIVE 비교
- 86 UIO·VFIO 분석 — User-Space Driver와 IOMMU 격리
- 87 sysfs·configfs 활용 — kobject 기반 User 인터페이스
- 88 IRQ Affinity 설정이 거부되거나 되돌아가는 이유 — smp_affinity·effective_affinity·isolcpus·irqbalance
- 89 루트 파일시스템 구축 — Buildroot 기초·Package·Toolchain
- 90 임베디드 동적 메모리 — malloc 위험·결정성·대안 분석
- 91 메모리 정렬과 패딩 분석 — Natural·Strict Alignment·Trap
- 92 Cache Line Alignment — alignas·Padding·SoA 적용
- 93 DMA-Friendly Allocator — dma_alloc_coherent·IOMMU·Pool
- 94 Zero-Copy Pipeline — DMA-BUF·sendfile·io_uring·splice
- 95 NUMA Memory Topology — numactl·numa_alloc·HBM 적용
- 96 SIMD 활용 분석 — Intrinsics·Auto-Vectorization·OpenMP SIMD
- 97 ARM NEON 심화 — Matrix Multiply·FFT·Image Filter 적용
- 98 임베디드 스택 분석 — high-water·overflow 탐지
- 99 임베디드 코드 크기 최적화 — -Os·LTO·Section Garbage Collection
- 100 임베디드 전력 최적화 — Sleep Mode·Clock Gating·DVFS
- 101 WCET 분석 기법 — Static·Measurement·Hybrid 방법론
- 102 Lock-Free Ring Buffer 구현 — SPSC·Power-of-2·Memory Order
- 103 Wait-Free Signaling — Atomic Flag·Sequence·Latest-Value
- 104 RCU (Read-Copy-Update) 기초 — Quiescent State·Grace Period
- 105 Hazard Pointer 분석 — Lock-Free Memory Reclamation
- 106 Compare-And-Swap 패턴 — Stack·Counter·Linked List 적용
- 107 Atomic Operation 비용 분석 — Fence·Cache Line·Contention
- 108 Spinlock vs Mutex 결정 가이드 — Context Switch·Hold Time
- 109 ABA 문제 회피 — Tagged Pointer·Hazard·Generation Counter
- 110 False Sharing 해결 — Cache Line Padding·Per-CPU·thread_local
- 111 MPMC Queue 구현 — Multi-producer Multi-consumer Lock-Free
- 112 임베디드 디버깅 마인드셋 — 가설·격리·재현·이분탐색
- 113 JTAG·SWD 안 붙을 때 — 핀·전압·속도·세션 진단
- 114 GDB 원격 디버깅 — OpenOCD·J-Link·target remote 구성
- 115 Cortex-M 하드폴트 분석 — Stacked Frame·CFSR 읽기
- 116 UART 안 찍힐 때 — Bare-metal 체크리스트
- 117 임베디드 부팅 실패 진단 — 단계별 Isolation
- 118 인터럽트 누락·중복 진단 — Priority·Pending·Re-entry 추적
- 119 메모리 오버플로우·오염 진단 — Canary·MPU·Pattern 분석
- 120 타이밍·Race 진단 — Heisenbug 잡는 법
- 121 통신 프로토콜 분석 — Logic Analyzer와 Protocol Decoder
- 122 임베디드 로깅 시스템 설계 — 레벨·버퍼·SWO·Deferred
- 123 임베디드 포스트모템 분석 — Core Dump와 Field Crash
- 124 FPGA 기초 분석 — LUT·FF·BRAM·DSP 자원 구조
- 125 Vivado 사용법 — Project·Constraint·Synth·Impl·Bitstream
- 126 PCIe BAR 매핑 분석 — Config Space·Enumeration·MMIO 접근
- 127 AXI 인터페이스 — AXI4·AXI4-Lite·AXI-Stream 비교
- 128 Zynq PS-PL 통신 — GP·HP·ACP 인터페이스 선택
- 129 Mailbox Protocol 분석 — Host와 Accelerator를 잇는 Doorbell
- 130 Command Queue·Submission Queue — NVMe·XDMA 공통 패턴
- 131 DMA Completion 메커니즘 — Interrupt·Polling·Completion Ring
- 132 PCIe Streaming 분석 — BAR Type·MSI-X·Kernel Bypass
- 133 Vitis HLS 분석 — Pragma·Pipeline II·Dataflow 실전 감각
- 134 HLS 최적화 기법 — Pipeline·Unroll·Partition·Dataflow
- 135 Vitis AI 분석 — DPU·xmodel·VART
- 136 OpenCL on FPGA — Kernel·Channel·Burst Memory 분석
- 137 Intel Quartus로 넘어가기 전 — 어느 toolchain에서나 쓰는 HDL과 testbench
- 138 Edge Inference 분석 — Cloud vs Edge·Latency·Privacy
- 139 Edge 추론 Pipeline 측정 — Stage Latency·Mailbox 정책·Buffer 수
- 140 NPU 아키텍처 분석 — Ethos·Hexagon·Systolic Array 비교
- 141 딥러닝 Quantization 분석 — PTQ·QAT·INT8·INT4·Calibration
- 142 TensorRT 분석 — ONNX→Engine·FP16·INT8·DLA·Multi-Stream
- 143 TFLite Micro 분석 — Op Resolver·Tensor Arena·Cortex-M
- 144 ONNX Runtime 분석 — Execution Provider와 Cross-Platform 배포
- 145 Edge Thermal Management — Throttling·DVFS·Fan Curve·Sustained
- 146 NVIDIA Jetson 분석 — Nano·Xavier·Orin·Thor·JetPack·DLA·VPI
- 147 Zero-Copy Camera Pipeline — V4L2·DMA-BUF·GPU Import·NPU 직결
- 148 온디바이스 LLM 추론 — llama.cpp·GGUF·MLX·KV Cache·NPU Backend
- 149 Cortex-M33 TF-M·TrustZone — Secure Firmware·PSA·MCUboot
- 150 Matter·Thread 분석 — IoT 통합 표준·Commissioning·Multi-Fabric
- 151 PCIe → CXL 진화 — 같은 PHY 위 cache-coherent 프로토콜 추가
- 152 QEMU CXL Type 3 디바이스 에뮬레이션 — 노트북에서 CXL 개발 환경 구축
- 153 Linux CXL 드라이버 분석 — cxl_pci·cxl_core·region·DAX