최신커널의특징과 디바이스드라이버개발에대한이해
Contents 1. 최신커널의특징 2. 커널최적화 3. 커널핸들링 4. 커널해킹 5. 디바이스드라이버구조 6. 커널동기화개요및기법 7. Unified device model 기반의디바이스드라이버 8. Interrupt Handling과지연작업
1. 최신커널의특징 General Preemptable Kernel O(1) Scheduler New Kernel Device Structure (kdev_t) Improved Posix Threading Support (NGPT and NPTL) New Driver Model & Unified Device Structure Faster Internal Clock Frequency Paring Down the BKL (Big Kernel Lock) Better in Place Kernel Debugging Smarter IRQ Balancing ACPI Improvements Software Suspend to Disk and RAM* Support for USB 2.0 ALSA (Advanced Linux Sound Architecture) LSM (Linux Security Module) Hardware Sensors Driver (lm-sensors)
1. 최신커널의특징 General CPU Clock and Voltage Scaling Setting Processor Affinity Improved NUMA Support Reverse Mapping VM System (rmap) Large Page Support High Resolution Posix Timers New Serial Port Driver Rewrite and API Other New Items New Module Implementation and Utilities New System Call Mechanism
1. 최신커널의특징 I/O Layer Rewrite of Block I/O Layer (BIO) Asynchronous I/O IDE Layer Update ACL Support (Access Control List) New NTFS Driver Networking NFS v4 Zero-Copy NFS TCP Segmentation Offload* SCTP Support (Stream Control Transmission Protocol) Bluetooth Support (not experimental) NAPI (Network Interrupt Mitigation)
1. 최신커널의특징 Removed Features Export of sys_call_table End of Task Queues
2. 커널최적화 리눅스성능측정도구 LMBench 운영체제, disk I/O, Memory 등의 Bandwidth 와 Latency 측정도구 HackBench 스케쥴러비교 리눅스지연요소 ( 실시간성능측정 ) 인터럽트지연 (Interrupt Latency), 커널선점지연 (Kernel Preemption Latency), 문맥교환지연 (Context Switch Latency)
2. 커널최적화 커널최적화기법 파라미터기반최적화 부팅최적화 컴파일러기반최적화
2. 커널최적화 Linux-2.6/Documentation/kernel-parameters.txt CONFIG_PRINTK_TIME Configure it in the Kernel Hacking Section Adds timing information to kernel messages. Simple and robust Disable console output The output of kernel bootup messages to the console time Even worse : scrolling up in framebuffer consoles Console output no needed in production systems Console output can be disabled with the quiet argument in the Linux kernel command line (bootloader settings) Example : root=/dev/ram0 rw init=/startup.sh quiet Benchmarks: can reduce boot time by 30 or even 50%
2. 커널최적화 Preset loops_per_jiffy At each boot, the Linux kernel calibrate a delay loop (for the udelay function). This measures a loops_per_jiffy(lpj) value. This takes about 25 jiffies (1 jiffy = time between 2 timer interrupts) At the boot, start Linux with the below option: lpj=<value> Kernel XIP Executes the kernel in place, i.e. directly from flash Saves time reading the kernel from flash, uncompressing it and copying it to RAM Savings depend on the kernel size Faster rebooting (1) Kexec system call: executes a new kernel from a running one. Useful for automatic rebooting after kernel panics
2. 커널최적화 Faster rebooting (2) Use reboot=soft in the kernel command line When you reboot, the firmware will be skipped. Supported platforms : i386, x86)64, arm, arm26 Skip memory allocation Idea : spare memory at boot time and manage it by yourself Assume you have 32M of RAM Boot your kernel with mem=30m Copy kernel from flash to Ram using DAM Reducing probe time on some IDE operations Multithreaded device probing
2. 커널최적화 SysV init Initng: http://initng.thinktux.net Some takes can be run in parallel Reading ahead Linux keeps the contents of all the files it reads in RAM(in the page cache), as long as it doesn t need the RAM pages for something else Load files in RAM cache before using them Readahead-list utility http://freshmeat.net/projects/readahead-list Using busybox, you can use the readahead command Application XIP Load files directly from flash or ROM Don t copy them to RAM first
2. 커널최적화 Compiler speed optimizations By default, most tools are compiled with compiler optimizations. -O2 is the most common optimization switch of gcc. -O3 can be also be used for speed critical executables. However, there is done at the expense of code size (example : inline function) Using processor acceleration instructions Liboil http://liboil.freedesktop.org Library of functions optimized for special instructions from several processors (Altivec, MMX, SSE, etc) Prelinking http://people.redhat.com/jakub/prelink Prelink modifies executables and shared libraries to simplify the dynamic linker relocation work
3. 커널핸들링 커널컴파일과정이해 커널컴파일옵션및커널내부구조와의관계 커널이미지구축및이미지구축과정이해 커널소스기반부팅과정이해 부팅메시지분석
4. 커널해킹 커널 Panic 핸들링 Call Tracing 커널디버깅핸들링 커널심볼관리 nm 유틸리티 System.map 파일 커널컴파일시에생성 vmlinux.lds 스크립트 ksymtab 커널심볼테이블 심볼 export 매크로
커널패닉 커널패닉개요 시스템이부팅할경우혹은특정서비스를하는동안에발생하는일종의오류 커널패닉종류 소프트패닉 Oops 소프트웨어기반의커널패닉 하드패닉 Aieee 하드웨어기반의커널패닉 시스템운용중의커널패닉
Call Tracing Call Tracing 트랩을받았을경우에발생하는시스템호출트레이싱의일종 arch/arch/kernel/traps.c의 die() 함수에정의 레지스터, 스택, 호출단계를출력하는기법 출력메시지분석을통해원인분석가능
커널디버깅 질의를통한디버깅 proc 프로그래밍기법 seq 인터페이스사용 ioctl 사용 Tool 을사용한디버깅 strace / ltrace kdb kernel debugger 사용 ltt 사용
oom killer 상황개요 시스템의메모리가모두소진된상태에서중요한프로세스가 oom killer에게죽는현상이발생가능 oom killer 개요 Out of Memory killer 특정메모리가부족한상황에서동작하고 특정알고리즘에의해프로세스를선택하고 해당프로세스를 kill해서메모리를확보 Overcommit 실제로필요로하는메모리보다더많은메모리를가상적으로할당하는정책을사용 프로세스는자신이요청한메모리를모두사용하지않는다는가정 최대한할당은늦게하고한번할당하면계속사용한다는가정 oom killer 해결방안은?
5. 디바이스드라이버모델 (2.6) New Driver Model and & Unified Device Structure Treat all devices in a unified fashion driverfs virtual filesystem can be mounted and seen as a tree by bus New API void device_init_dev(struct device *) struct device *device_alloc(); int device_register(struct device *); void get_device (struct device *); void put_device (struct device *); int valid_device (struct device *); void lock_device (struct device *); void unlock_device (struct device *); New Driver Model and & Unified Device Structure Initializing, Exiting int device_driver_init(); void device_driver_exit(); New data structures struct device struct device_driver
6. 커널동기화개요및기법 Wait Queue Semaphore Spinlock Memory barrier Completion Functions Atomic Operations Bit Operations Big kernel lock Big kernel semaphore Work queue Linked List Deferred functions Dynamic Timers
7. Unified device model 디바이스드라이버 리눅스디바이스드라이버개요 디바이스등록및해제 드라이버등록및해제 문자디바이스드라이버 디바이스드라이버의객체개념 kref, kobject, ktype, kset, hotplug udev firmware
디바이스드라이버의객체개념 Device Model은왜필요한가? Power management and system shutdown Communications with user space Hotpluggable devices Device classes Object lifecycles 출처 :http://lwn.net/kernel/ldd3/
디바이스드라이버의객체개념 kobject kset ktype subsystem class sysfs hotplug udev subsystem kset kset kset ktype kobj kobj kobj
udev udev의목표 /dev를대체하여동적으로생성 ->udev 디바이스이름을생성 -> namedev 시스템디바이스 (sysfs) 에서정보를취합할 API 제공 -> libsysfs
8. Interrupt Handling 과지연작업 Top Half 와 Bottom Half Interrupt Handler Handling Deferred Processing softirq 구조
Interrupt 개요 interrupt 처리의조건 kernel이가능한빨리인터럽트에서벗어나야한다. interrupt는언제든지발생할수있으므로가능한많이허용해야한다. interrupt 처리코드는임계영역이므로가능한제한되어있어야한다. 인터럽트타임때할수없는일들 인터럽트는프로세스컨텍스트내에있지않기때문에 current를이용하여 task_struct 필드에접근할수없다. sleep() 함수를사용할수없다. schedule() 함수를호출할수없다. semaphore 루틴 down() 을호출할수없다. 하지만 wake_up() 이나 up() 을할수있다. request_module() 로 module loading 요청을할수없다. 사용자 space와커널 space 사이에데이터전송을할수없다.
Interrupt 핸들러 타이머인터럽트를제외한모든인터럽트를쓰레드화 serialize 된인터럽트처리를우선순위화 각인터럽트쓰레드에게실시간우선순위와 SCHED_FIFO 스케줄링 policy 를할당 스케줄링을통한예측가능성
Bottom Half 와지연작업 Bottom Half 란? 인터럽트핸들러에의해처리되지않는인터럽트관련작업을처리 작업지연개요 왜작업을지연하는가? softirq 와 tasklet softirq : 정적으로할당된하반부작업으로어떤 CPU에서든지동시에실행이가능하다. tasklet : softirq를기반으로하여동적으로생성된하반부작업