550 likes | 1.19k Views
Hacking Linux Kernel. WANG Cong. xiyou.wangcong@gmail.com. Why This Lecture?. It's the right time to talk about Linux kernel. Linux kernel is fun to hack. Linux kernel needs more volunteers, more testers, and more developers. It's a good chance to spread open-source ideas.
E N D
Hacking Linux Kernel WANG Cong xiyou.wangcong@gmail.com
Why This Lecture? • It's the right time to talk about Linux kernel. • Linux kernel is fun to hack. • Linux kernel needs more volunteers, more testers, and more developers. • It's a good chance to spread open-source ideas.
What's NOT About? • The secrets in the Linux kernel source code. • How the kernel works. • How to use Linux. • Why not BSD, Solaris or Windows. • How to improve my C programming skills.
What's About? • How to hack the Linux kernel. • Stuffs related to Linux kernel development. • Useful tools for Linux kernel development. • The developing cycle of Linux kernel. • Advice for new newbies.
Section One Preliminaries
Where is Linux kernel? • Official website: http://www.kernel.org • Bug report: http://bugzilla.kernel.org • Mail archive site: http://lkml.org
The latest versions information is located at: http://www.kernel.org/kdist/finger_banner • 2.6 stable kernels and patches are available at: http://www.kernel.org/pub/linux/kernel/v2.6/ • The 2.6 -mm kernels live at: ftp://ftp.kernel.org/pub/linux/kernel/people/akpm/patches/2.6/ • -rc kernels and patches for 2.6 are in: http://www.kernel.org/pub/linux/kernel/v2.6/testing/
Retrieving Kernel Source • cd ~ (or cd /usr/src) • wget –proxy=off $URL • tar -xvjf linux-2.6.21.1.tar.bz2 (or tar -xvzf ) • cd linux-2.6.21.1 • mv ../linux-2.6.21.1.tar.bz2 ./
Configuring and Compiling • make xconfig or make gconfig or make menuconfig (make defconfig? make oldconfig?) • make • make modules_install • make install
Need More? • Build docs: make pdfdocs, make psdocs, make htmldocs or make mandocs • Build a single file: make drivers/usb/serial/visor.ko • Build just a portion: make M=drivers/usb/serial • Test: make randconfig, make allyesconfig • Browse code: make ctags, make cscope
Section Two The Development Process
Linux Kernel Versions • Up to version 2.5, the minor version number identified the type of kernel. • This is not true since 2.6.
New Numbering Scheme • 2.6.x(.y) kernels are all stable release. • 2.6.x kernels are the base stable releases released by Linus. • 2.6.x.y kernels are -stable kernels released by stable team, where 'y' denotes patch level. • 2.6.x-rcX, 2.6.x-rcX-mmY, 2.6.x-mmX are development kernels.
Kernel Trees • -rc kenrels are release-candidate kernels, released by Linus. • -git kernels are daily snapshots of Linus's kernel tree. • -mm kernels are experimental kernels released by Andrew Morton.
Stable Release • 2.6.x kernels are maintained by Linus Torvalds. • 2.4.x kernels are maintained by Marcelo Tosatt. • 2.2.x kernels are maintained by Alan Cox. • 2.0.x kernels are maintained by David Weinehall. • -stable kernels are maintained by Greg KH.
Stable Release Rules • It must be obviously correct and tested. • It can not bigger than 100 lines, with context. • It must fix only one problem that is critical. • It can not contain any "trivial" fixes in it. • It must be accepted by the relevant subsystem maintainer.
Review • When the -stable team decides for a review cycle, the patches will be sent to the review committee. • The review committee has a short time in which to ack or nak the patch. • At the end, the acked patches will be added to the latest -stable release.
Bazaar? • Hack away until it works, evolved design by committee, stable API later. • The Cathedral vs the Bazaar.
Section Three Patches
What is a patch? • A patch is a small text document containing a delta of changes between two different versions of a source tree. • Patches are created with the 'diff' program. • `diff -up` or `diff -Nrup` is recommended.
Applying Patches • You apply a patch with the `patch' program. • The patch program reads a diff (or patch) file and makes the changes to the files described in it. • Patches for the Linux kernel are generated relative to the parent directory holding the kernel source dir.
Patch(1) • You should change into your kernel source directory before applying any kernel patches. • You should strip the first element of the path from filenames in the patch file when applying it. • You should check your patch is applied from which to which.
Example • $ cd ~/linux-2.6.12.2 • $ bzcat ../patch-2.6.12.2.bz2 | patch -p1 -R • $ bzcat ../patch-2.6.12.3.bz2 | patch -p1 • $head -5 Makefile • $ cd .. • $ mv linux-2.6.12.2 linux-2.6.12.3
Which Patch Applies to Which Release? • Stable kernel patches apply to the base kernels. • Base kernel release patches only apply to the previous base kernel version. • Incremental patches upgrade from a specific release to the next release.
The incremental ones can be found at ftp://ftp.kernel.org/pub/linux/kernel/v2.6/incr/ • The -rc patches are not incremental, they apply to a base 2.6.x kernel. • The -mm kernels apply to either a base 2.6.x kernel or to a Linus -rc kernel.
Examples • “patch-2.6.21.1.bz2” is for 2.6.21, it changes your 2.6.21 kernel into 2.6.21.1. • “patch-2.6.21.bz2 ” is for 2.6.20 release. • “patch-2.6.17.9-10.bz2” changes 2.6.17.9 release into 2.6.17.10.
More Examples • “patch-2.6.13-rc3.bz2” applies for 2.6.12, makes it to be 2.6.13.rc3. • “2.6.13-rc3-mm3.bz” is for 2.6.13.rc3, updates it to be 2.6.13-rc3-mm3. • “patch-2.6.13-rc2-git3.bz2” makes 2.6.13-rc2 to be 2.6.13-rc2-git3.
Long Jumps • 2.6.20-rc3 to 2.6.21-rc5 ? 2.6.20-rc3=>2.6.19=>2.6.20=>2.6.21-rc5 • 2.6.21-rc3-mm1 to 2.6.22-rc1 ? 2.6.21-rc3-mm1=>2.6.21-rc3 =>2.6.20=>2.6.21=>2.6.22-rc1 • Too long?
Submitting Patches • Before you submit patches, please do read these files: Documentation/Changes, Documentation/CodingStyle, Documentation/SubmittingPatches, Documentation/SubmittingDrivers. • “Patches are delivered via email only. Downloading them from internet servers is a pain.” -- Andrew Morton • Check your email client.
Mailing Lists • The main list: lkml@vger.kernel.org • Read the LKML FAQ before you subscribe. • Linux filesystem development: linux-fsdevel@vger. kernel.org • linux-netdev: netdev@vger.kernel.org
Patch Format • Subject format: [PATCH $version $n/$total] $subsystem: one-line summary • Email body contents: description, patches and your Signed-off-by. • ALWAYS make your email text/plain.
No MIME, no links, no compression and no attachments, if possible. • Select email destination: TO the maintainer in MAINTAINERS file, CC to relative mailing list, developers and/or others who care about this. • ALWAYS make your patch in `patch -p1' form and ALWAYS test your patch.
Example From: WANG Cong <xiyou.wangcong@gmail.com> To: steve@chygwyn.com, akpm@osdl.org Cc: LKML <linux-kernel@vger.kernel.org> Subject: [-mm Patch]nbd: check the return value of sysfs_create_file
Fix this warning: drivers/usb/misc/sisusbvga/sisusb_con.c:1436: warning: initialization from incompatible pointer type • Signed-off-by: WANG Cong <xiyou.wangcong@gmail.com> • --- • <--Put your patch here.-->
Section Four Useful Tools and Advices
Git • $ git-clone http://www.foo.org/git/foo.git • $ cd foo && git-init-db • $ git-add example • $ git-commit -m "Descriptions here." • $ git diff • $ git-checkout foo
Qemu $qemu -kernel linux-2.6.21/arch/i386/boot/bzImage -hda ./linux-0.2.img -append "root=/dev/hda"
Mutt • set sort=threads, set sort_aux=reverse-last-date, subscribe lkml@vger.kernel.org • Group reply: g • Custom your favourite editor, such as vi. • Read my articles on mutt to learn more.
Other Tools • checkpatch.pl: check your patches. • ketchup: retrieve kernels • Quilt: manage your patches, available at: http://savannah.nongnu.org/projects/quilt
Advice • Alan Cox: “Ignore everyone who tells you kernel hacking is hard, special or different. It's a large program, and bug fixing or driver tweaking can be a best starting point. It is however not magic, nor written in a secret language that only deep initiates with beards can read.” • Be persistent, be patient, please.
Recommended Books • Robert Love, Linux Kernel Development, Second Edition, 2005, Sams Publishing. • Jonathan Corbet etc., Linux Device Drivers, Third Edition, 2005, O'Reilly. • Daniel P. Bovet and Marco Cesati, Understanding the Linux Kernel, 3rd Edition, 2005, O'Reilly.