Commit Graph

90 Commits

Author SHA1 Message Date
vax-r
a700043862 Fix typo
Correct "dynamicly" to "dynamically" and correct "initialised" to
"initialized".
2024-02-01 15:09:23 +08:00
毛毛
470fbcd97d
Fix switch-case condition error in sample code (#245)
The code 'case -EINPROGRESS || -EBUSY: ' is the same as
'case -115 || -16 :' at compiler time, as both error code are
implemented with macro like '#define EBUSY 16'.

The code above is essentially the same as 'case 1:'. In C, there is no
real boolean value. Boolean-like value will be converted to 1 or 0.

It does not matter too much if the '-EINPROGRESS || -EBUSY' is
calculated at build time or at runtime. In both case, it will compare
the 'rc' with 1 in the switch expression. It will not compare the
'rc' with any real error code number. When the code is really '-EBUSY',
the execution will fallback to the default branch.

And in practice, most of the compilers will do this simple compile-time
static calculation, and generate code like

static int test_skcipher_result(struct skcipher_def *sk, int rc)
{
    switch (rc) {
    case 0:
        break;
    case 1:
        rc = wait_for_completion_interruptible(&sk->result.completion);
/* code removed for conciseness */
        break;
    default:
        pr_info("skcipher encrypt returned with %d result %d\n", rc,
                sk->result.err);
        break;
    }
    init_completion(&sk->result.completion);
    return rc;
}
2024-01-03 22:13:26 +08:00
Jim Huang
80e400b8d2 syscall-steal: Refine comment 2023-12-22 20:53:21 +08:00
Jim Huang
e61a0d6174 Enforce consistent name scheme 2023-12-22 20:40:27 +08:00
keytouch
f2ad878e9b syscall_steal: rename sys_call_table to fix compile error
sys_call_table is already declared in arch/x86/include/asm/syscall.h but of
cource not exported by the kernel.
before this commit, gcc complains as follows:
/usr/src/linux-headers-6.1.0-16-common/arch/x86/include/asm/syscall.h:21:29:
note: previous declaration of 'sys_call_table' with type 'long int (*
const[])(const struct pt_regs *)'
   21 | extern const sys_call_ptr_t sys_call_table[];
2023-12-22 20:17:02 +08:00
keytouch
a60e84a060 syscall: rename module name to syscall_steal
In Debian, the name syscall conflicts with this patch:
in debian kernel source tree:
debian/patches/features/x86/x86-make-x32-syscall-support-conditional.patch
mailing list url:
https://lore.kernel.org/lkml/1415245982.3398.53.camel@decadent.org.uk/T/#u
which introduces a parameter named syscall.x32. So change our name.
2023-12-22 20:14:42 +08:00
Kuan-Wei Chiu
e1594ba58a
Fix typo (#240) 2023-10-08 17:14:09 +08:00
Amit Dhingra
a49c9baffa
Replace schedule_work with queue_work
schedule_work adds work to global workqueue. In this example, we create a local workqueue. Use the local workqueue by calling queue_work(), instead of putting work on the global workqueue.
2023-10-07 16:32:26 +02:00
Amit Dhingra
e56fc58bf3 Add newline to last pr_info to force dmesg to flush
dmesg only flushes when it encounter a newline. Without a newline, the line
is held in memory pending another printk. In this particular example
(example_atomic.c), the last pr_info in atomic_bitwise() prints when
another printk happens (either by another module, or __exit for this
module.

This can be confusing to new learner. This patch adds a newline to the last
pr_info forcing dmesg to print to the screen when the module is loaded.
2023-09-28 15:32:50 +00:00
Amit Dhingra
bf523b2332 Simplify code by removal of outer struct
Two struct completion(s) are encapsulated within another 'struct machine'.
Simplify the code by removing the outer struct and let the struct
completion(s) be self-standing.

Update description in tex to match code.
2023-09-28 06:02:39 +00:00
Amit Dhingra
d0f86a29f0
Remove module * parameter from class_create() in chardev2.c (#228)
From v6.4, class_create() does not have module *
Same fix as commit 0addb868dc to chardev.c
2023-09-17 18:23:45 +08:00
Amit Dhingra
6341842a9b Correct name/location for ioctl-number.rst
ioctl-number.rst in Documentation has been renamed and moved

See commits
- 049500715e7
- 08536105d93
2023-09-04 17:59:31 +00:00
Amit Dhingra
0addb868dc
Remove module * parameter from class_create() (#222)
From v6.4, class_create() does not have module *
See commit 11ba11 :
> driver core: class: remove module * from class_create()
> 
> The module pointer in class_create() never actually did anything,
> and it shouldn't have been requred to be set as a parameter even
> if it did something.  So just remove it and fix up all callers of the
> function in the kernel tree at the same time.
2023-09-03 02:24:35 +08:00
Chih-En Lin
b9b37fc39f kbleds: Replace magic with driver_name
Since the commit 5052df99d3bc ("tty: remove TTY_DRIVER_MAGIC") [1],
the magic field no longer exists in the tty_driver. Instead of
printing the magic field, use the driver_name field.

[1] https://lore.kernel.org/linux-doc/20220907231109.htnfxzmr6wkuhn2v@tarta.nabijaczleweli.xyz/

Close #216
Close #217
2023-08-10 08:56:19 +08:00
keytouch
5e2b7aa2e3 procfs: Remove unnecessary cleanup on proc_create failure 2023-07-23 18:41:46 +08:00
Sam Erickson
9e9f5a8bf8
static_key: Include <linux/jump_label.h> (#210) 2023-07-05 23:14:49 +08:00
Chih-En Lin
befbaf085b
examples: Add missing __init/__exit (#209)
Close #207
2023-07-05 09:44:21 +08:00
Sam Erickson
8a5463ddd9 include linux/init.h in bottomhalf.c 2023-07-04 10:10:18 -05:00
Peter Lin
421b9b04fe Introduce static key in optimizations section
This patch introduces static key technique and adds a kernel module
to demostrate how to use static key to optimize an almost unlikely
branch, the module can interact with userspace thru a character device.

Signed-off-by: Peter Lin <peterlin@qilai.dev>
2023-05-02 10:33:36 +08:00
Chih-En Lin
23c0a73c78 procfs: Enforce Linux naming style 2023-03-24 11:36:33 +08:00
linD026
4ee80a3e16 completion: Improve the compatibility with v5.17+
Since v5.17-rc1, particularly after the commit cead1855266 ("exit:
Rename complete_and_exit to kthread_complete_and_exit"),
complete_and_exit() is renamed to kthread_complete_and_exit().

Close #188
2023-03-14 08:26:19 +08:00
linD026
8a5879a0fa chardev2: Fix missing header for {get, put}_user 2023-03-14 08:15:06 +08:00
Andy Shevchenko
b8bbcd8a07 treewide: Replace kernel.h by printk.h
The kernel.h should be discouraged for use.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
2023-02-23 12:54:30 +02:00
Andy Shevchenko
e62dff0df4 treewide: Clean up the headers
The rule of thumb is to include the headers we are the direct user of.
In particular, if we need an atomic API, we include <linux/atomic.h>.

On the other hand we should not use headers for no reason. In particular,
if we are not doing any IRQ job, why is the <linux/irq.h> included?

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
2023-02-23 12:54:30 +02:00
Andy Shevchenko
e07bf16bfd devicemodel: Remove duplicate owner assignment
platform_driver_register() macro already does the correct assignment for
the owner of the driver.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
2023-02-21 23:04:41 +02:00
linD026
78e3e062c4 syscall: Use openat() instead of open()
Since sys_open is deprecated and some architectures don't support it.
We switch the implementation to sys_openat.

Moreover, in some architectures like x86-64, the prototype of syscall,
for example, openat(), might have been changed to struct pt_regs [1]
but we cannot promise that so support the two types
(sys_openat and pt_regs).

Also, to prevent other untraced tasks print out the information, add
the uid checking in our_sys_openat().

[1] https://lore.kernel.org/lkml/20180405095307.3730-1-linux@dominikbrodowski.net/

Close #159
2022-12-25 14:27:03 +08:00
linD026
6062f64bcf example/syscall: Fix typo and unmatch types 2022-12-25 12:33:52 +08:00
Peter Lin
3133ee5293 Add error message when sha256 algorithm is not supported
It failed silently when crypto_alloc_tfm() failed, so add an error
message to inform the developer to enable sha256 algorithm support.

Signed-off-by: Yu Chien Peter Lin <peterlin.tw@pm.me>
2022-12-12 23:07:20 +08:00
Chih-En Lin
d81d968b0e vinput: Fix missing error code
Fix the missing error code when register_chrdev() failed.
The report is from Smatch:

Smatch failed: 1 warning(s), 0 error(s)
/home/runner/work/lkmpg/lkmpg/examples/vinput.c:372 vinput_init() warn: missing error code 'err'
2022-11-04 01:16:14 +08:00
Jim Huang
760df73743
Merge pull request #168 from linD026/master
Fix dereference NULL pointer with proc_lseek
2022-09-23 13:52:06 +08:00
linD026
0f9c72631c Fix dereference NULL pointer with proc_lseek
Since the operations are static storage duration, the pointer in
operation structures will initialize with NULL. But, the kernel
doesn't check whether the pointer is NULL or not when calling it.

Related Discussion:
- https://github.com/sysprog21/lkmpg/issues/165
- https://github.com/sysprog21/lkmpg/issues/160

Close #165
2022-09-21 16:57:23 +08:00
linD026
eb2c766da4 Improve the compatibility with kernel < v5.10
min()/max() splited from kernel.h to minmax.h since v5.10-rc1.
Before v5.10, minmax.h doesn't exist [1].

[1] b296a6d533
2022-09-21 13:53:06 +08:00
Jim Huang
c69eff9d1f
Merge pull request #163 from linD026/read-write
procfs{2, 3}: Change to use offset parameter
2022-09-08 22:24:20 +08:00
linD026
39a313dc20 procfs{2, 3}: Change to use offset parameter
To make sure the behavior of the read and write operations are correct
with offset, update it each time. Also, since it is using the offset,
modify the part of read for removing unnecessary variable.
2022-09-08 12:53:29 +08:00
linD026
95a7ca513f Fix the buffer length may cause a read error
Since The length of the message buffer is BUF_LEN. When writing the
BUF_LEN length of the string it will overwrite the last character
(usually it is '\0' from the initialization). And, because the read
operation uses the character in the message buffer ('\0') to stop the
read loop. It will cause the read operation will read out of the
message buffer when the length parameter of read() is not lower than
or equal to BUF_LEN. So add one more byte space to avoid this problem.
2022-09-08 06:08:04 +08:00
asas1asas200
04465b1a44 Add description of sysfs attribute
The description of the attribute was added in sysfs section and referenced in vinput section.

In vinput section, just described the class_attribute and some macros about sysfs class.

Add file name at vinput-related examples begin.
2022-04-19 01:54:24 +08:00
linD026
beb1ff1595 Fix potential concurrent problems in chardev2.c
After forking, Each file descriptor in the child refers to the same
open file description as the parent. So when calling open() before
fork(), the child can access the device file without checking by
exclusive access in device_open(). It may cause race conditions
in device_ioctl().

Because of that, it is unnecessary to check the multiple access
in device_open(). It just needs check in device_ioctl(), since
read(), write(), seek() system call are atomic [1][2].

Related discussion:
- https://github.com/sysprog21/lkmpg/issues/148

[1] https://lore.kernel.org/lkml/53022DB1.4070805@gmail.com/
[2] https://www.kernel.org/doc/html/latest/filesystems/files.html

Close #148
2022-04-17 02:41:25 +08:00
linD026
9f92fe4d6a Fix wrong regular expression of clang-format 2022-04-10 22:51:59 +08:00
linD026
636c1e702d Introduce Virtual Input Device Driver
Add the new section of input device driver, vinput[1].

Also, update the Acknowledgements.

[1] https://github.com/sysprog21/vinput
2022-04-06 17:13:29 +08:00
linD026
85ee0ec67b Improve the description of finding syscall table
Presently, the representation of showing which version will use the
method to find out the system call table is vague.

Related discussion:
* sysprog21/lkmpg: https://github.com/sysprog21/lkmpg/pull/142
2022-03-17 07:51:59 +08:00
Steven Lung
d771c50166 Fix grammar 2022-03-09 14:14:08 +08:00
Steven Lung
5ca9b642a4 Fix typo, grammar and remove duplicated words
Replaced 'a' with 'an' since a vowel sound is after.
2022-03-07 16:56:13 +08:00
Bob Lee
844beed5d7
Fix a logic error in examples/ioctl.c (#137)
Change the "alloc_ret" and "cdev_ret" initial values to non-zero.
According to the source code, "alloc_chrdev_region" and "cdev_add"
return zero on success, and negative code on failure.

So, if the "alloc_chrdev_region" failed, the if condition becomes true,
then we will jump to the label "error" by goto, checking each return
value whether is a success state from both functions mentioned above
and dealing with it properly for exiting the process.

However, it checks the success state by comparing the return value
with zero (means success), and we got "cdev_ret == 0" is true from
the initial value zero, while we didn't execute "cdev_add" yet.
Hence, there was a logic error when the initial value is zero.

Co-authored-by: NOVBobLee <defru04002@gamil.com>
2022-02-21 00:58:47 +08:00
linD026
d8216ff7b6
Fix incoherent ioctl examples (#139)
Previously ioctl.c is a userspace program for chardev2.c and chardev.h [1].
But now, this file is an independent kernel module, and the original code
disappear.
This patch adds back the original userspace code and renames it to
userspace_ioctl.c.

[1] https://tldp.org/LDP/lkmpg/2.4/html/x856.html
2022-02-21 00:53:29 +08:00
linD026
6b6b6635cf
CI: Introduce Smatch for static analysis (#125)
Smatch[1][2] is a pluggable static analysis for C. It may help us find
out the potential problem of the example code.

Doing with smatch, if set the --file-output flag, it will generate the
{}.c.smatch report for each c file. This will make a little bit
complicated to collect all the report messages. So, here we stay at the
default setting, stdout for the smatch messages.

For more information, see:
- https://lwn.net/Articles/696624/
- https://elinux.org/images/d/d3/Bargmann.pdf

Also, fix the warning from Smatch:

Smatch failed: 1 warning(s), 1 error(s)
lkmpg/examples/procfs2.c:57 procfile_write() error: buffer overflow 'procfs_buffer' 1024 <= 1024
lkmpg/examples/kbleds.c:58 kbleds_init() warn: argument 5 to %lx specifier is cast from pointer

Furthermore, the effect of the write operation in procfs2.c is too
implied. So after writing, print the buffer every time.

Close #122

[1] https://github.com/error27/smatch
[2] https://repo.or.cz/w/smatch.git
2021-11-01 08:20:41 +08:00
linD026
d6e1044aa7 CI: introduce GCC static analysis
Since GCC version 10, there has been a new option -fanalyzer for
static analysis. It can make the CI pipeline more comprehensive.

Also, the static analysis updates in GCC 11, but we cannot install
the GCC 11 in ubuntu 20.04 straightforwardly right now, which is
the GitHub workflow environment (see status-check.yaml).
For this reason, we stay at the GCC 10.

Close #117

Reference
- https://gcc.gnu.org/onlinedocs/gcc-10.1.0/gcc/Static-Analyzer-Options.html
- https://developers.redhat.com/blog/2020/03/26/static-analysis-in-gcc-10
- https://lwn.net/Articles/870290/
- https://developers.redhat.com/blog/2021/01/28/static-analysis-updates-in-gcc-11
- 7d73c3e9c5
2021-10-08 19:41:22 +08:00
linD026
1a6fb67cf2
Fix potential concurrent access problems with VFS (#108)
Since Linux v3.14, the read, write and seek operations of "struct file" are
guaranteed for thread safety [1][2]. This patch added an explanation.

Here are the potential problems:
chardev.c:
- Move the "msg_ptr" pointer into the read function to remove unnecessary usage.
- List the clear states of "already_open" by using mnemonic enumeration.

chardev2.c:
- The "buffer" in the write function is user space data. It cannot use in the
  kernel space.
- Reduce the redundant type transformation.
- List the states of "already_open". Same as chardev.c.

[1] https://lore.kernel.org/lkml/20140303210359.26624.qmail@science.horizon.com/T/#u
[2] 9c225f2655
2021-09-23 12:20:10 +08:00
linD026
5070fcd9d0
CI: Run static analysis with Cppcheck (#105)
Cppcheck[1] is integrated into CI pipeline for running static analysis.

However, Cppcheck is known to report false-positive, and we have to
suppress some warnings in advance.

[1] https://cppcheck.sourceforge.io/
2021-09-14 00:52:04 +08:00
linD026
fa035da36e ioctl.c: Remove unnecessary initialization
The "alloc_chrdev_region" function will dynamically choose the
major number and store it at "dev". It is unnecessary to initialize
the "dev" before the "alloc_chrdev_region" function.
2021-09-13 14:55:16 +08:00
linD026
62dbb1b7b8
Adapt the timer API Usage (#98)
Since v4.14 [1], the timer API has been changed to improve memory safety.
The series of improvements ended up at v4.15 [2].

Reference: https://lwn.net/Articles/735887/

Close #97

[1] 686fef928b
[2] 841b86f328
2021-09-10 12:29:48 +08:00