Linux Kernel Things

Table of Contents

GFPKERNEL and GFPATOMIC

GFPATOMIC is used when (a) you are inside a completion handler, an interrupt, bottom half, tasklet or timer, or (b) you are holding a spinlock or rwlock (does not apply to semaphores), or (c) current->state != TASKRUNNING, this is the case only after you've changed it.

Changed to GPFATOMIC and syslog is quiet!!!

Linking to a kernel module a precompiled object file

build a shipped object file(3.3 Binary Blobs in Document/kbuild/modules.txt and 3. Creating a Kbuild File for an External Module)

  1. build the obj file
    ifneq ($(KERNELRELEASE),)
    obj-y   := xxxx.o #your obj files goes here.
    else
    KERNELDIR ?= /lib/modules/$(shell uname -r)/build
    PWD       := $(shell pwd)
    
    default:
           $(MAKE) -C $(KERNELDIR) M=$(PWD)
    
    endif
    
    clean:
           rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
    
    depend .depend dep:
           $(CC) $(CFLAGS) -M *.c > .depend
    
    
    ifeq (.depend,$(wildcard .depend))
    include .depend
    endif
    
  2. rename the shipped .o file to .oshipped
    mv ex_obj.o ex_obj.o_shipped
    
  3. build
test-y := ex_obj.o
=== 3. Creating a Kbuild File for an External Module

In the last section we saw the command to build a module for the
running kernel. The module is not actually built, however, because a
build file is required. Contained in this file will be the name of
the module(s) being built, along with the list of requisite source
files. The file may be as simple as a single line:

        obj-m := <module_name>.o

The kbuild system will build <module_name>.o from <module_name>.c,
and, after linking, will result in the kernel module <module_name>.ko.
The above line can be put in either a "Kbuild" file or a "Makefile."
When the module is built from multiple sources, an additional line is
needed listing the files:

        <module_name>-y := <src1>.o <src2>.o ...

NOTE: Further documentation describing the syntax used by kbuild is
located in Documentation/kbuild/makefiles.txt.

The examples below demonstrate how to create a build file for the
module 8123.ko, which is built from the following files:

        8123_if.c
        8123_if.h
        8123_pci.c
        8123_bin.o_shipped      <= Binary blob

--- 3.1 Shared Makefile

        An external module always includes a wrapper makefile that
        supports building the module using "make" with no arguments.
        This target is not used by kbuild; it is only for convenience.
        Additional functionality, such as test targets, can be included
        but should be filtered out from kbuild due to possible name
        clashes.

        Example 1:
                --> filename: Makefile
                ifneq ($(KERNELRELEASE),)
                # kbuild part of makefile
                obj-m  := 8123.o
                8123-y := 8123_if.o 8123_pci.o 8123_bin.o

                else
                # normal makefile
                KDIR ?= /lib/modules/`uname -r`/build

                default:
                        $(MAKE) -C $(KDIR) M=$$PWD

                # Module specific targets
                genbin:
                        echo "X" > 8123_bin.o_shipped

                endif

        The check for KERNELRELEASE is used to separate the two parts
        of the makefile. In the example, kbuild will only see the two
        assignments, whereas "make" will see everything except these
        two assignments. This is due to two passes made on the file:
        the first pass is by the "make" instance run on the command
        line; the second pass is by the kbuild system, which is
        initiated by the parameterized "make" in the default target.

--- 3.2 Separate Kbuild File and Makefile

        In newer versions of the kernel, kbuild will first look for a
        file named "Kbuild," and only if that is not found, will it
        then look for a makefile. Utilizing a "Kbuild" file allows us
        to split up the makefile from example 1 into two files:

        Example 2:
                --> filename: Kbuild
                obj-m  := 8123.o
                8123-y := 8123_if.o 8123_pci.o 8123_bin.o

                --> filename: Makefile
                KDIR ?= /lib/modules/`uname -r`/build

                default:
                        $(MAKE) -C $(KDIR) M=$$PWD

                # Module specific targets
                genbin:
                        echo "X" > 8123_bin.o_shipped

        The split in example 2 is questionable due to the simplicity of
        each file; however, some external modules use makefiles
        consisting of several hundred lines, and here it really pays
        off to separate the kbuild part from the rest.

        The next example shows a backward compatible version.

        Example 3:
                --> filename: Kbuild
                obj-m  := 8123.o
                8123-y := 8123_if.o 8123_pci.o 8123_bin.o

                --> filename: Makefile
                ifneq ($(KERNELRELEASE),)
                # kbuild part of makefile
                include Kbuild

                else
                # normal makefile
                KDIR ?= /lib/modules/`uname -r`/build

                default:
                        $(MAKE) -C $(KDIR) M=$$PWD

                # Module specific targets
                genbin:
                        echo "X" > 8123_bin.o_shipped

                endif

        Here the "Kbuild" file is included from the makefile. This
        allows an older version of kbuild, which only knows of
        makefiles, to be used when the "make" and kbuild parts are
        split into separate files.

--- 3.3 Binary Blobs

        Some external modules need to include an object file as a blob.
        kbuild has support for this, but requires the blob file to be
        named <filename>_shipped. When the kbuild rules kick in, a copy
        of <filename>_shipped is created with _shipped stripped off,
        giving us <filename>. This shortened filename can be used in
        the assignment to the module.

        Throughout this section, 8123_bin.o_shipped has been used to
        build the kernel module 8123.ko; it has been included as
        8123_bin.o.

                8123-y := 8123_if.o 8123_pci.o 8123_bin.o

        Although there is no distinction between the ordinary source
        files and the binary file, kbuild will pick up different rules
        when creating the object file for the module.

--- 3.4 Building Multiple Modules

        kbuild supports building multiple modules with a single build
        file. For example, if you wanted to build two modules, foo.ko
        and bar.ko, the kbuild lines would be:

                obj-m := foo.o bar.o
                foo-y := <foo_srcs>
                bar-y := <bar_srcs>

        It is that simple!

Kernel Timers

example:

struct timer_list       mac_phy_timer;
init_timer(&mac->mac_phy_timer);
mac->mac_phy_timer.data     = (unsigned long)mac;
mac->mac_phy_timer.function = (void *)athr_gmac_timer_func;
athr_gmac_timer_func(mac);

// start 1
mac->mac_phy_timer.expires = jiffies + mac->timer_freq;
// start 2
mod_timer(&mac->mac_phy_timer,(jiffies + mac->timer_freq));
// del
del_timer(&mac->mac_phy_timer);

mod_timer(timer, expires) is equivalent to: del_timer(timer); timer->expires = expires; add_timer(timer);

cc



Author: Shi Shougang

Created: 2016-09-21 Wed 00:09

Emacs 24.3.1 (Org mode 8.2.10)

Validate