Monday, August 23, 2010

Linux Device Driver One Liners

Types of Driver Class.

Char Driver : Permits sequential Read/Write only.
Block Driver : Permits random + buffered/block Read /write.
Network Driver : Directly transfer packet of data to kernel. Not mapped to file system.

Tainted Kernel is any part of kernel code without GPL ( or friends ) License.

Device Driver Structure

__init() -> Any Code within this is executed first, at the time of loading. Not mandatory.
__exit() -> This part is executed an the time of driver unloading.

Parameters can be passed to device driver using "module_params()". If R/W is given then it can be changed on the fly with " $ echo "value" > /sys/module/parameter/"

Finally, Any sys call the device driver handles are mapped to the "fops" entry points of the driver. For example , the sys-call close() is mapped to "release()" entry point of the driver.

Major Number are used to identify driver associated with device
Minor Number are used only within the device driver to distinguish different functionality.

Important Structures

File Structure : Kernel Created new file structure on every open(). This is passed to rest of the function. Separate allocated memory per open() is called "private data'.
Inode Structure : There is just 1 inode structure per device.

task_struct = keeps all info about tasks.

Interrupt & Exception

  • Interrupt can never be lost.
  • Interrupt are shared. That is all handlers get the interrupt, the one which is responsible consumes it. The rest discards it.
  • Interrupts are Maskable and Non-Maskable interrupts.
  • Under SMP only one CPU will handle interrupt of same type.
  • Cannot sleep in interrupt context.
  • Cannot call schedule().

Top Half : Actual Routine, which ack interrupt. Copy data from device to buffer. Small and fast.
Bottom Half : Do delay processing work here. There are 3 types of bottom half.
  • Softirq : Run in interrupt context
  • Tasklets : Run in interrupt context
  • Workqueues: Run in process context. Can sleep.

Timers

Jiffies : Is a counter which increments on every timer interrupt
TSC : Time stamp clock, register which increments on every clock cycle. H/W dependent.
HPET : High precision event timer. Is an hardware.

IOCTLS

Is an driver entry point. User can call driver entry point using ioctls.

PROCFS

Read only FS in memory. Used for debugging and information gathering.

SysFS

Virtual File system in memory.

Race conditions.

Prevent two or more threads accessing same resource.

Atomic function : Execute single instruction which cannot be interrupt.

Mutex: Sleepable locking mechanism.
  • interrupt-able mutex: single can break it.
  • non-interrupt-able mutex: only user / owner can break it
  • its non recursive.
Spin lock : Non sleepable locking mechanism.


Semaphore : Sleepable locking mechanism.

  • Counting semaphore : More than one thread can hold a semaphore.
  • binary semaphore : Same as mutex.

Memory management

  • Zones:
  • 0-16mb : DMA
  • 16-896 : Normal
  • 896 - 4GB : High.

kmalloc() : Allocate memory.
GFP_KERNEL: NORMAL , CAN SLEEP AND BLOCK.
GFP_ATOMIC: INTERRUPT, CANNOT BLOCK
GFP_DMA : USED FOR DMA
kfree(): Free memory.

vmalloc() : Get continuous memory from virtual address. And cannot be used in interrupt context and DMA.
vfree() : free memory allocated from vmalloc.

Boot Memory + early allocation

Device driver can only allocated 4MB max. To circumvent this "alloc_bootmem()" is used which can allocate any amount.

Reading / Writing To and from user space to kernel space use below functions as dereferencing pointer passed from user space into kernel space is bad.
  • put_user()
  • get_user()
  • copy_to_user()
  • copy_from_user()

Storage Systems One Liners

NAS : Network Attached Storage exposes file system. E.g NFS,CIFS,SMB.

SAN : Storage Area Network exposes volumes or block devices. ( scsi )

iSCSI : Exposes scsi devices as volumes. Can be accessed via tcp/ip.
  • iscsi Target : Actual data destination, the real storage space. ( Server )
  • iscsi Initiator : Client side. A way to find and connect target.
FC : Fiber Channel a storage protocol. Normally used with SAN.

AOE : ATA over Ethernet. Normal IDE over LAN.

FCoE : Fiber channel over Ethernet.

Benefits of Storage
  • Disaster Recovery
  • Easy backup / Storage points
  • Easy management

Unix Therory One Liners.

Type of Files

  • HardLink : Multiple path to same inode. Cannot Span different file system.
  • SoftLink : Alternate path to file. Can span file systems
  • Special Files : Kernel object represented as files. E.g Char,Block.
  • Pipes : IPC mechanism. Its supports one way communication within same process parent child realm.
  • FIFO : Also called Named pipes. IPC mechanism. Unrelated process can communicated.
  • Sockets : IPC mechanism. Two or more unrelated process can communicate. Two or more unrelated machines can communicate.

Process vs Threads

  • Process a program in memory. Running, and can be scheduled by the CPU. Takes memory / space.
  • Threads a unit of execution within a process. Shares most of the data structure of the process except the stack. Which is separate for each thread. Two threading library under UNIX is pthreads and NPTL .

  • fork() = creates a new process. New process is called the "child". Its an exact replica of the parent when its created. Its gets its own stack and heap on COW.
  • fork() returns twice, once for child (0) and other for parent ( pid of child ).

  • If child die before parent is called a zombie.
  • If parent die before child the child process is called an orphan.


What child process gets ...

  • All signal which are not ignored.

What child does not get ...

  • File locks in parent.
  • No pending signals.

  • exit() - Kills a process.
  • atexit() - Another variation of exit which calls an user defined method before exiting.

Parent getting status of child.

  • wait() - Parent retrieves information when child dies.
  • waitpid() - Get information of a single child whose pid matches.

Deamons = process running in background with not terminal attached.


I/O

  • Buffered I/O = Write / Read in buffers size. Can be forced to flush(). The opposite is non-buffered ( here user manages its own buffer write/read frequency ).
  • Multiplexed I/O = Write/Read with more than one file descriptors. ( select(), poll() )
  • Scatter/Gather Write/Read = Read/Write to many buffers in a single call.
  • MMAP = map file into memory.

Locking

  • Advisory = Not enforced. ( eg spin lock)
  • Mandatory = Enforced.
Signals

  • Are asynchronous notifications.