跳到主要內容

發表文章

eBPF 為什麼不能隨便讀記憶體?看懂 Linux Verifier 的安全檢查 / Why the Linux eBPF Verifier Refuses Unsafe Memory Access

daily-tech:2026-11-16 現象:一個指標,為什麼不能直接讀? / Symptom: Why can’t one pointer simply be read? 寫 eBPF 時,最容易踩到的坑不是語法,而是「我明明先算出位址,為什麼載入核心就被拒絕?」原因是 eBPF 程式會在核心內執行;Linux 不能等錯誤發生後才補救,因此載入前先由 verifier 檢查指標型別、數值範圍、對齊與控制流程。 When writing eBPF, the frustrating failure is often not syntax: “I calculated the address, so why did the kernel reject the program?” eBPF runs inside the kernel. Linux therefore checks pointer types, value ranges, alignment, and control flow before loading instead of repairing a fault afterward. 機制:verifier 追蹤的是狀態,不是猜測 / Mechanism: the verifier tracks states, not guesses 官方文件把暫存器分成 scalar 與多種 pointer。scalar 會保存最小、最大值與未知位元;指標則帶著來源與可存取範圍。以封包為例,先取出 data 與 data_end,再確認 data 加上標頭長度沒有越過 data_end,之後那個分支內的讀取才被視為安全。若中間把指標做成無法證明範圍的運算,狀態會退化成 scalar,後面的記憶體讀取就會失效。 The kernel documentation describes registers as scalars or several pointer types. A scalar carries bounds and unknown bits; a pointer carries its origin and an accessible range. For packet data, the program loads data and data_end,...
最近的文章

Linux PSI:CPU 沒滿卻變慢?找出記憶體與 I/O 壓力 / Linux PSI: Finding Hidden Contention

現象: AI 推論、編譯或大量 I/O 同時跑時,CPU 可能沒有 100%,但延遲突然變長。只看 top 很難知道工作卡在 CPU、記憶體還是 I/O。 Phenomenon: During inference, builds, or heavy I/O, latency can jump while CPU is below 100%. top alone cannot identify whether work waits on CPU, memory, or I/O. PSI pressure-stall flow runnable work is delayed kernel aggregates some / full stall /proc/pressure/* time windows correlate with p95 tune reversibly 圖 1|PSI 將想執行卻被延遲的時間彙整成可觀察訊號。 Figure 1 | PSI turns delayed progress into an observable signal. 機制:PSI 量的是有工作卻前進不了 / Mechanism: PSI measures stalled progress Pressure Stall Information(PSI)統計任務因資源競爭而停滯的時間。some 表示至少部分任務受阻;full 表示一段時間內所有非閒置工作都受阻。Linux 將 CPU、記憶體與 I/O 分別放在 /proc/pressure/。 Pressure Stall Information (PSI) measures time lost to contention. some means some tasks are stalled; full means all non-idle work is stalled for an interval. Linux exposes CPU, memory, and I/O separately under /proc/pressure/. 影響:先分辨壓力類型 / Impact: classify pressure first 邊緣 LLM 若 memory some 持續升高,可能是回收、匿名頁或記憶體頻寬造成延遲;若 io...

Linux OOM Killer 不只看 RAM 用量:從 oom_score_adj 到 cgroup 邊界的排查路徑 / Linux OOM Killer: Beyond RAM Usage

現象: 服務明明還有一些可用記憶體,Linux 卻突然結束程序,甚至讓容器內的 AI 推論中斷。這不一定是整台機器的 RAM 用光;配置範圍、swap、memcg 限制與保護分數都可能讓核心進入 OOM 路徑。 Phenomenon: Linux may terminate a process even when the machine appears to have free memory. The trigger can be a cgroup, NUMA policy, swap, or allocation scope—not only total RAM. Linux OOM decision path allocation cannot reclaim enough scope: system / memcg / NUMA score memory + oom_score_adj kill selected task; continue 圖 1|Linux OOM killer 的簡化決策路徑:先確認耗盡範圍,再以記憶體用量與 oom_score_adj 評分。 Figure 1 | Simplified Linux OOM-killer path: identify the exhausted scope, then score candidates using memory usage and oom_score_adj. 機制:OOM 先看耗盡範圍,再挑候選者 / Mechanism: scope first, victim second 核心在無法透過 reclaim 或 compaction 滿足配置時,才會進入 OOM killer。 mm/oom_kill.c 的 oom_badness() 依候選程序記憶體用量與 oom_score_adj 計算分數; -1000 代表強力保護。允許使用的記憶體集合取決於當次 OOM 上下文,因此 memcg OOM 不等同於整機 OOM。 The kernel enters the OOM path only after reclaim and compaction cannot satisfy the allocation. In mm/oom_kill.c , oom_badness() ...

Linux io_uring 為什麼仍可能卡住?從固定 Buffer 到 Completion Queue 的 I/O 路徑 / Why Linux io_uring Can Still Stall: Fixed Buffers and Completion Queues

 現象:io_uring 非同步,為什麼服務仍會卡住? / Symptom: Why can an io_uring service still stall? 你把大量檔案讀取改成 Linux io_uring,CPU 使用率下降,卻仍看到請求延遲突然拉高。提交請求變得非同步,不代表儲存裝置立刻完成;queue depth、檔案系統與 block I/O 仍會把等待時間留在 completion 之前。 You moved file reads to Linux io_uring, CPU usage fell, yet request latency still spikes. Submission becomes asynchronous; storage, the filesystem, and block I/O can still delay completion. io_uring 的基本路徑是:應用程式準備 submission queue entry(SQE),核心取出並執行,最後把結果寫成 completion queue entry(CQE)。共享 ring 減少系統呼叫,但 CQE 的 res 才是實際結果;不能只看到 SQE 已提交,就把工作算成完成。 The path is: the application prepares an SQE, the kernel executes it, and a CQE reports the result. Shared rings reduce syscall traffic, but CQE res is the real outcome. Submission is not completion. 圖一|io_uring 的 SQE→核心執行→CQE 路徑;固定 buffer 的生命週期與完成延遲必須分開觀察。 / Figure 1 | The SQE-to-kernel-to-CQE path; buffer lifetime and completion latency are separate signals. Original conceptual diagram. 機制:固定 Buffer 解決什麼,又帶來什麼責任? / Mechanism: what registered bu...

Linux 為什麼還沒 OOM 就開始卡頓?用 PSI 讀懂 CPU、記憶體與 I/O 壓力 / Before OOM: Reading Linux Pressure Stall Information

現象:為什麼還沒 OOM 就開始卡? / Symptom: Why does Linux stall before OOM? 你是否遇過這種情況:記憶體還有剩、CPU 也沒有 100%,但 SSH 變慢、服務延遲突然拉高,甚至工作負載尚未被 OOM Killer 終止就已經「像死機」?這不是單看使用率就能回答的問題。Linux Pressure Stall Information(PSI)量的是工作真的因資源不足而等待了多久,讓「還能跑」與「已經在浪費時間」分開。 Have you seen a Linux host become sluggish while memory is still available and CPU is below 100%? PSI addresses the missing signal: not how full a resource looks, but how long runnable work is actually delayed by contention. A machine can be operational and already wasting latency before the OOM killer appears. PSI 的核心不是額外的效能計數器,而是把資源競爭轉成「停滯時間」。核心分別在 /proc/pressure/cpu、memory、io 匯出資料;some 表示至少一部分工作正在等待,full 則表示所有非閒置工作同時卡住。memory full 或 io full 長時間上升,比單獨看到 free 變少更接近「系統正在失去生產力」的現象。 PSI turns contention into stall time. The kernel exports cpu, memory, and io pressure files under /proc/pressure. “some” means at least some work is stalled; “full” means all non-idle work is stalled at once. A sustained memory-full or io-full signal is often more actionable tha...

【Linux 排程機制】從 CFS 到 EEVDF:虛擬截止時間如何給 Linux 排程帶來延遲保證 / [Linux Scheduling] From CFS to EEVDF: How Earliest Eligible Virtual Deadline First Brings Latency Guarantees to Linux

你有沒有遇過這種情況:開著幾個重度應用,滑鼠游標明明沒動,系統卻突然停頓半秒,播放的影片掉幀、遊戲瞬間卡住?這往往不是記憶體不足,而是「排程延遲」——核心決定哪個進程下次使用 CPU 的時間點,沒有給即時工作負載足夠的保證。舊版 Linux 預設排程器 CFS(完全公平排程)追求整體公平,但對「這個任務必須在多久內執行」的延遲上限,缺乏明確承諾。 Have you ever experienced this: several heavy applications running, the mouse cursor sits still, yet the system suddenly freezes for half a second, video stutters, or a game hitches? This is often not memory exhaustion, but "scheduling latency" — the kernel's decision of which process gets the CPU next fails to guarantee timing for latency-sensitive workloads. The default Linux scheduler CFS (Completely Fair Scheduler) pursues overall fairness, but lacks a clear upper-bound commitment on "this task must run within how long." struct sched_entity (排程實體) Linux 6.18.55 · kernel/sched/fair.c · include/linux/sched.h EEVDF 決策欄位 load struct load_weight — 加權負載,決定分攤時間比例 run_node struct rb_node — 掛在 CFS 紅黑樹 tasks_timeline 上的節點 vrun...

【Android 底層篇】Binder IPC 內部機制拆解:一次拷貝背後的 mmap 記憶體映射黑魔法與執行緒池調度 / [Android Internals] Demystifying Binder IPC: The "Single-Copy" mmap Black Magic and Thread Pool Scheduling

只要開發過 Android 應用程式,你幾乎肯定遇過令人抓狂的 TransactionTooLargeException 。當你試圖在 Intent、Bundle 或 AIDL 介面塞入一張稍微清晰的高解析圖片或龐大資料列表時,應用程式便無情崩潰。為什麼在動輒配備 16 GB 記憶體的旗艦手機上,一次跨行程通訊只要超過區區 1 MB 就會引發系統報警? If you have ever developed an Android application, you have almost certainly encountered the notorious TransactionTooLargeException . When attempting to pass a high-resolution bitmap or an extensive serialized dataset across an Intent, Bundle, or AIDL interface, the application abruptly terminates. Why does an inter-process transaction exceeding a mere 1 MB trigger immediate system panic on a flagship smartphone packing 16 GB of physical RAM? 要徹底解開這個謎題,我們必須告別抽象的 Java 介面,穿透 Android Runtime(ART),直接潛入 Linux 核心驅動 drivers/android/binder.c 。Binder 作為 Android 系統的血脈神經,憑藉著「僅需一次記憶體拷貝」的精巧設計,支撐起整座作業系統的骨架。今天我們就來深度拆解 Binder 的核心映射機制、執行緒池調度與工程師除錯指南。 To unravel this puzzle completely, we must look beyond abstract Java interfaces, bypass the Android Runtime (ART), and dive directly into the Linux kernel driver at driver...