Skip to content

Commit 664bfc6

Browse files
committed
use the latest better Makefile template
1 parent 8ebdce5 commit 664bfc6

File tree

2 files changed

+84
-48
lines changed

2 files changed

+84
-48
lines changed

ch8/lowlevel_mem/Makefile

Lines changed: 83 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
# Makefile
1+
# ch5/lkm_template/Makefile
2+
# ***************************************************************
3+
# This program is part of the source code released for the book
4+
# "Linux Kernel Programming" 2E
5+
# (c) Author: Kaiwan N Billimoria
6+
# Publisher: Packt
7+
# GitHub repository:
8+
# https://github.com/PacktPublishing/Linux-Kernel-Programming_2E
9+
#
10+
# From: Ch 5 : Writing Your First Kernel Module LKMs, Part 2
211
# ***************************************************************
312
# Brief Description:
413
# A 'better' Makefile template for Linux LKMs (Loadable Kernel Modules); besides
@@ -13,92 +22,117 @@
1322
# To get started, just type:
1423
# make help
1524
#
16-
# For details on this so-called 'better' Makefile, please refer my book
17-
# 'Linux Kernel Programming' 2nd Ed, Packt, 2023, Chapter 5, section
25+
# For details, please refer the book, Ch 5, section
1826
# 'A "better" Makefile template for your kernel modules'.
27+
#
28+
# AUTHOR : Kaiwan N Billimoria
29+
# DESCRIPTION : A simple kernel module 'better' Makefile template
30+
# LICENSE : Dual MIT/GPL
31+
# VERSION : 0.2
1932

2033
#------------------------------------------------------------------
21-
# Set FNAME_C to the kernel module name source filename (without .c)
34+
# IMPORTANT : Set FNAME_C to the kernel module name source filename (without .c).
2235
# This enables you to use this Makefile as a template; just update this variable!
2336
# As well, the MYDEBUG variable (see it below) can be set to 'y' or 'n' (no being
2437
# the default)
2538
FNAME_C := lowlevel_mem
2639
ifeq ($(FNAME_C),)
27-
$(error You MUST pass the C file like this:\
28-
make FNAME_C=csrc-filename-without-.c target-name)
29-
else
30-
$(info [FNAME_C = $(FNAME_C)])
40+
$(error ERROR: you Must set the FNAME_C variable in the Makefile)
3141
endif
3242
#------------------------------------------------------------------
3343

34-
# To support cross-compiling for kernel modules:
44+
#--- To support cross-compiling for kernel modules
3545
# For architecture (cpu) 'arch', invoke make as:
3646
# make ARCH=<arch> CROSS_COMPILE=<cross-compiler-prefix>
47+
# F.e. to cross-compile for the AArch64:
48+
# make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-
49+
#
50+
# Alternately:
51+
# export ARCH=<arch>
52+
# export CROSS_COMPILE=<cross-compiler-prefix>
53+
# make
54+
#
3755
# The KDIR var is set to a sample path below; you're expected to update it on
38-
# your box to the appropriate path to the kernel src tree for that arch.
56+
# your box to the appropriate path to the kernel source tree for that arch.
3957
ifeq ($(ARCH),arm)
4058
# *UPDATE* 'KDIR' below to point to the ARM Linux kernel source tree on your box
41-
KDIR ?= ~/rpi_work/kernel_rpi/linux
59+
KDIR ?= ~/arm_prj/kernel/linux
4260
else ifeq ($(ARCH),arm64)
4361
# *UPDATE* 'KDIR' below to point to the ARM64 (Aarch64) Linux kernel source
4462
# tree on your box
45-
KDIR ?= ~/kernel/linux-5.4
63+
KDIR ?= ~/arm64_prj/kernel/linux-5.10.60
4664
else ifeq ($(ARCH),powerpc)
4765
# *UPDATE* 'KDIR' below to point to the PPC64 Linux kernel source tree on your box
48-
KDIR ?= ~/kernel/linux-5.0
66+
KDIR ?= ~/ppc_prj/kernel/linux-5.4
4967
else
5068
# 'KDIR' is the Linux 'kernel headers' package on your host system; this is
5169
# usually an x86_64, but could be anything, really (f.e. building directly
5270
# on a Raspberry Pi implies that it's the host)
5371
KDIR ?= /lib/modules/$(shell uname -r)/build
5472
endif
73+
#---
5574

5675
# Compiler
5776
CC := $(CROSS_COMPILE)gcc
58-
#CC := clang
77+
#CC := clang
78+
STRIP := ${CROSS_COMPILE}strip
5979

6080
PWD := $(shell pwd)
6181
obj-m += ${FNAME_C}_lkm.o
6282
lowlevel_mem_lkm-objs := ${FNAME_C}.o ../../klib.o
6383

6484
#--- Debug or production mode?
65-
# Set the MYDEBUG variable accordingly to y/n resp.
85+
# Set the MYDEBUG variable accordingly to y/n resp. We keep it off (n) by default.
6686
# (Actually, debug info is always going to be generated when you build the
6787
# module on a debug kernel, where CONFIG_DEBUG_INFO is defined, making this
68-
# setting of the ccflags-y (or EXTRA_CFLAGS) variable mostly redundant (besides
69-
# the -DDEBUG).
88+
# setting of the ccflags-y (or the older EXTRA_CFLAGS) variable mostly redundant
89+
# (besides the still useful -DDEBUG).
7090
# This simply helps us influence the build on a production kernel, forcing
7191
# generation of debug symbols, if so required. Also, realize that the DEBUG
7292
# macro is turned on by many CONFIG_*DEBUG* options; hence, we use a different
7393
# macro var name, MYDEBUG).
7494
MYDEBUG := n
95+
DBG_STRIP := y
7596
ifeq (${MYDEBUG}, y)
76-
7797
# https://www.kernel.org/doc/html/latest/kbuild/makefiles.html#compilation-flags
7898
# EXTRA_CFLAGS deprecated; use ccflags-y
7999
ccflags-y += -DDEBUG -g -ggdb -gdwarf-4 -Wall -fno-omit-frame-pointer -fvar-tracking-assignments
100+
DBG_STRIP := n
80101
else
81-
INSTALL_MOD_STRIP := 1
82102
ccflags-y += -UDEBUG
83103
endif
84104
# We always keep the dynamic debug facility enabled; this allows us to turn
85105
# dynamically turn on/off debug printk's later... To disable it simply comment
86106
# out the following line
87107
ccflags-y += -DDYNAMIC_DEBUG_MODULE
88-
89108
KMODDIR ?= /lib/modules/$(shell uname -r)
90-
STRIP := ${CROSS_COMPILE}strip
91109

92-
# gcc-10 issue:
93-
#ccflags-y += $(call cc-option,--allow-store-data-races)
110+
# Gain access to kernel configs
111+
include $(KDIR)/.config
112+
113+
# Strip the module? Note:
114+
# a) Only strip debug symbols else it won't load correctly
115+
# b) WARNING! Don't strip modules when using CONFIG_MODULE_SIG* crytographic security
116+
ifdef CONFIG_MODULE_SIG
117+
DBG_STRIP := n
118+
endif
119+
ifdef CONFIG_MODULE_SIG_ALL
120+
DBG_STRIP := n
121+
endif
122+
ifdef CONFIG_MODULE_SIG_FORCE
123+
DBG_STRIP := n
124+
endif
125+
94126

95127
all:
96128
@echo
97-
@echo '--- Building : KDIR=${KDIR} ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE} ccflags-y=${ccflags-y} ---'
129+
@echo '--- Building : KDIR=${KDIR} ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE} ccflags-y="${ccflags-y}" MYDEBUG=${MYDEBUG} DBG_STRIP=${DBG_STRIP} ---'
98130
@${CC} --version|head -n1
99131
@echo
100132
make -C $(KDIR) M=$(PWD) modules
101-
$(shell [ "${MYDEBUG}" != "y" ] && ${STRIP} --strip-debug ./${FNAME_C}.ko)
133+
if [ "${DBG_STRIP}" = "y" ]; then \
134+
${STRIP} --strip-debug ${FNAME_C}.ko ; \
135+
fi
102136
install:
103137
@echo
104138
@echo "--- installing ---"
@@ -107,10 +141,11 @@ install:
107141
@echo
108142
@echo " [Now for the 'sudo make install' ]"
109143
sudo make -C $(KDIR) M=$(PWD) modules_install
110-
@echo " sudo depmod"
111144
sudo depmod
112-
@echo " [If !debug, stripping debug info from ${KMODDIR}/extra/${FNAME_C}.ko]"
113-
$(shell if [ "${MYDEBUG}" != "y" ]; then sudo ${STRIP} --strip-debug ${KMODDIR}/extra/${FNAME_C}.ko; fi)
145+
@echo " [If !debug and !(module signing), stripping debug info from ${KMODDIR}/extra/${FNAME_C}.ko]"
146+
if [ "${DBG_STRIP}" = "y" ]; then \
147+
sudo ${STRIP} --strip-debug ${KMODDIR}/extra/${FNAME_C}.ko ; \
148+
fi
114149
nsdeps:
115150
@echo "--- nsdeps (namespace dependencies resolution; for possibly importing ns's) ---"
116151
make -C $(KDIR) M=$(PWD) nsdeps
@@ -119,10 +154,11 @@ clean:
119154
@echo "--- cleaning ---"
120155
@echo
121156
make -C $(KDIR) M=$(PWD) clean
122-
# from 'indent'
157+
# from 'indent'; comment out if you want the backup kept
123158
rm -f *~
124159

125-
# Any usermode programs to build? Insert the build target(s) here
160+
# Any usermode programs to build? Insert the build target(s) below
161+
126162

127163
#--------------- More (useful) targets! -------------------------------
128164
INDENT := indent
@@ -168,12 +204,11 @@ sa_sparse:
168204
ifeq (,$(shell which sparse))
169205
$(error ERROR: install sparse first)
170206
endif
171-
172207
make clean
173208
@echo
174209
@echo "--- static analysis with sparse ---"
175210
@echo
176-
# if you feel it's too much, use C=1 instead
211+
# If you feel it's too much, use C=1 instead
177212
# NOTE: deliberately IGNORING warnings from kernel headers!
178213
make -Wsparse-all C=2 CHECK="/usr/bin/sparse --os=linux --arch=$(ARCH)" -C $(KDIR) M=$(PWD) modules 2>&1 |egrep -v "^\./include/.*\.h|^\./arch/.*\.h"
179214

@@ -207,29 +242,30 @@ endif
207242
@echo
208243
cppcheck -v --force --enable=all -i .tmp_versions/ -i *.mod.c -i bkp/ --suppress=missingIncludeSystem .
209244

210-
# Packaging; just tar.xz as of now
211-
PKG_NAME := ${FNAME_C}
245+
# Packaging: just generates a tar.xz of the source as of now
212246
tarxz-pkg:
213-
rm -f ../${PKG_NAME}.tar.xz 2>/dev/null
247+
rm -f ../${FNAME_C}.tar.xz 2>/dev/null
214248
make clean
215249
@echo
216250
@echo "--- packaging ---"
217251
@echo
218-
tar caf ../${PKG_NAME}.tar.xz *
219-
ls -l ../${PKG_NAME}.tar.xz
220-
@echo '=== package created: ../$(PKG_NAME).tar.xz ==='
221-
@echo 'Tip: when extracting, to extract into a dir of the same name as the tar file,'
222-
@echo ' do: tar -xvf ${PKG_NAME}.tar.xz --one-top-level'
252+
tar caf ../${FNAME_C}.tar.xz *
253+
ls -l ../${FNAME_C}.tar.xz
254+
@echo '=== package created: ../$(FNAME_C).tar.xz ==='
255+
@echo ' TIP: When extracting, to extract into a directory with the same name as the tar file, do this:'
256+
@echo ' tar -xvf ${FNAME_C}.tar.xz --one-top-level'
223257

224258
help:
225259
@echo '=== Makefile Help : additional targets available ==='
226260
@echo
227-
@echo 'TIP: type make <tab><tab> to show all valid targets'
228-
@echo
261+
@echo 'TIP: Type make <tab><tab> to show all valid targets'
262+
@echo 'FYI: KDIR=${KDIR} ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE} ccflags-y="${ccflags-y}" MYDEBUG=${MYDEBUG} DBG_STRIP=${DBG_STRIP}'
229263

264+
@echo
230265
@echo '--- 'usual' kernel LKM targets ---'
231266
@echo 'typing "make" or "all" target : builds the kernel module object (the .ko)'
232-
@echo 'install : installs the kernel module(s) to INSTALL_MOD_PATH (default here: /lib/modules/$(shell uname -r)/)'
267+
@echo 'install : installs the kernel module(s) to INSTALL_MOD_PATH (default here: /lib/modules/$(shell uname -r)/).'
268+
@echo ' : Takes care of performing debug-only symbols stripping iff MYDEBUG=n and not using module signature'
233269
@echo 'nsdeps : namespace dependencies resolution; for possibly importing namespaces'
234270
@echo 'clean : cleanup - remove all kernel objects, temp files/dirs, etc'
235271

@@ -246,17 +282,17 @@ help:
246282
@echo ' sa_gcc : run gcc with option -W1 ("Generally useful warnings") on the source file(s)'
247283
@echo ' sa_flawfinder : run the static analysis flawfinder tool on the source file(s)'
248284
@echo ' sa_cppcheck : run the static analysis cppcheck tool on the source file(s)'
249-
@echo 'TIP: use coccinelle as well (requires spatch): https://www.kernel.org/doc/html/v4.15/dev-tools/coccinelle.html'
285+
@echo 'TIP: use Coccinelle as well: https://www.kernel.org/doc/html/v6.1/dev-tools/coccinelle.html'
250286

251287
@echo
252288
@echo '--- kernel dynamic analysis targets ---'
253289
@echo 'da_kasan : DUMMY target: this is to remind you to run your code with the dynamic analysis KASAN tool enabled; requires configuring the kernel with CONFIG_KASAN On, rebuild and boot it'
254290
@echo 'da_lockdep : DUMMY target: this is to remind you to run your code with the dynamic analysis LOCKDEP tool (for deep locking issues analysis) enabled; requires configuring the kernel with CONFIG_PROVE_LOCKING On, rebuild and boot it'
255-
@echo 'TIP: best to build a debug kernel with several kernel debug config options turned On, boot via it and run all your test cases'
291+
@echo 'TIP: Best to build a debug kernel with several kernel debug config options turned On, boot via it and run all your test cases'
256292

257293
@echo
258294
@echo '--- misc targets ---'
259295
@echo 'tarxz-pkg : tar and compress the LKM source files as a tar.xz into the dir above; allows one to transfer and build the module on another system'
260-
@echo ' Tip: when extracting, to extract into a dir of the same name as the tar file,'
261-
@echo ' do: tar -xvf ${PKG_NAME}.tar.xz --one-top-level'
296+
@echo ' TIP: When extracting, to extract into a directory with the same name as the tar file, do this:'
297+
@echo ' tar -xvf ${FNAME_C}.tar.xz --one-top-level'
262298
@echo 'help : this help target'

ch8/lowlevel_mem/lowlevel_mem.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ static int bsa_alloc(void)
8888
pr_info("1. __get_free_page() 1 page %px\n", gptr1);
8989

9090
/* 2. Allocate 2^bsa_alloc_order pages with the __get_free_pages() API */
91-
numpg2alloc = powerof(2, bsa_alloc_order); // returns 2^bsa_alloc_order
91+
numpg2alloc = powerof(2, bsa_alloc_order); /* returns 2^bsa_alloc_order */
9292
gptr2 = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, bsa_alloc_order);
9393
if (!gptr2) {
9494
/* no error/warning printk now; see above comment */

0 commit comments

Comments
 (0)