[主題研究] Linux Memory Management === ###### tags: `twlkh`, `study-group` # Concept * [Gustavo Duarte](http://duartes.org/gustavo/blog/) * [Anatomy of a Program in Memory](http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-memory/) * [How the Kernel Manages Your Memory](http://duartes.org/gustavo/blog/post/how-the-kernel-manages-your-memory/) * [Page Cache, the Affair Between Memory and Files](http://duartes.org/gustavo/blog/post/page-cache-the-affair-between-memory-and-files/) * Rich Liu 分享的投影片 * [Virtual Memory - 1](https://slack-files.com/T1SELBRGR-F331VMCM9-33eec3fbc8) * [Virtual Memory - 2](https://slack-files.com/T1SELBRGR-F333W6YTC-40eddb3704) * [Virtual Memory - 3](https://slack-files.com/T1SELBRGR-F336EUP8V-790000b611) * [What a C programmer should know about memory](http://marek.vavrusa.com/c/memory/2015/02/20/memory/) # Slab allocators (Moved from hackpad article https://twlinuxkernelhackers.hackpad.com/Wiki-M37dy9c6AZt) * Slub 是目前 Linux kernel 上面預設的 slab allocator。所謂的 slab 就是從 buddy system 要一個 page 過來,之後依照不同的 kmalloc 所丟入不同的 object size (such as kmalloc-96, kmalloc-192, kmalloc-8, kmalloc-32, etc) 把 page 內部切成 object size 為單位的格式。之後 return freelist 的第一個 free 的 object 給 kmalloc 的呼叫者。其中,初始化過後,每個 object 的頭 8 個 bytes 儲存 freepointer 指向下一個可得的 available object size。用 linked list 的方式串起整個 slab(page) 內部所有空的 objects。當之後有人 return free 的 object 這時候若是 local return(alloc & free 是同一個 page)則會走 fast path 直接 return 回 per cpu page freelist。若是 remote return 則會走 slowpath return 回 page->freelist。由於有兩條 freelist 的緣故,在 deactivate_slab (把 page 返回 Per node partial list)的時候會計算 page->inuse(把 page->objects 減掉兩條空的總和)就是當下在使用的 object 數量。而分兩條 freelists 的目的是為了加速 allocate & free 的速度不要卡在 critical path。 * 在Ubuntu上面可以用 `sudo cat /proc/slabinfo` or `sudo slabtop` 看到 slab caches的相關資訊。 * 2014年Linux plumber conference上面slub maintainer, Christoph Lameter有非常好的介紹 * [slab allocator](http://events.linuxfoundation.org/sites/events/files/slides/slaballocators.pdf) {%pdf https://events.linuxfoundation.org/sites/events/files/slides/slaballocators.pdf %} * 相關的 youtube video: * [SLAB/SLUB/SLOB: Kernel memory allocator design and philosophy](https://www.youtube.com/watch?v=h0VMLXavx30) {%youtube h0VMLXavx30%} * [SLUB fastpath improvements and potential booster shots through bulk alloc and free](https://www.youtube.com/watch?v=s0lZzP1jOzI) {%youtube s0lZzP1jOzI%} * How does the SLUB allocator work (Joonsoo Kim是sl[au]b的維護者之一,這份同投影片描述了slab vs slub值得一看) https://events.static.linuxfound.org/images/stories/pdf/klf2012_kim.pdf # Buddy system # Book ## Understanding Linux Virtual Memory Manager # Debug ## KASan (Kernel Address Sanitizer) ### Youtube: [How to use KASAN to debug memory corruption in OpenStack environment](https://www.youtube.com/watch?v=ynTgO6A79SA) {%youtube ynTgO6A79SA %} ### Slideshare: How to use KASAN to debug memory corruption in OpenStack environment {%slideshare GavinGuo3/how-to-use-kasan-to-debug-memory-corruption-in-openstack-environment-2 %} ### KernelAddressSanitizer (KASan) A fast memory error detector for the Linux kernel {%pdf https://events.linuxfoundation.org/sites/events/files/slides/LinuxCon%20North%20America%202015%20KernelAddressSanitizer.pdf %} ### [AddressSanitizer: A Fast Address Sanity Checker](https://www.usenix.org/conference/atc12/technical-sessions/presentation/serebryany) {%pdf https://www.usenix.org/sites/default/files/conference/protected-files/serebryany_atc12_slides.pdf %} ### [KASan in a Bare-Metal Hypervisor](http://www.slideshare.net/lfevents/kasan-in-a-baremetal-hypervisor) {%slideshare lfevents/kasan-in-a-baremetal-hypervisor%}