Commit Graph

105 Commits

Author SHA1 Message Date
Aykhan Hagverdili
4adbf1dd9b Fix: blocking O_NONBLOCK process bug in sleep.c
There is a subtle bug that if the atomic flag changes
between the time it was checked and the second time
it was checked, sleep.c would potentially block a
process that had specified O_NONBLOCK. This fixes
the bug by using atomic_cmpxchg instead of atomic_read.
2024-08-11 21:56:09 +02:00
Hao.Dong
950408472e
Add a hook on syscall using Kprobes under x86 (#260)
For x86 architecture, the system call table cannot be used to invoke
a system call after commit 1e3ad78 since v6.9. This commit has been
backported to long term stable kernels, like v5.15.154+, v6.1.85+,
v6.6.26+ and v6.8.5+[1]. In this case, thanks to Kprobes, a hook can be
used instead on the system call entry to intercept the system call.

[1] https://stackoverflow.com/a/78607015

Co-authored-by: Hao Dong <hao.dong.work@outlook.com>
2024-07-05 07:40:53 +08:00
Jim Huang
1d3943a72b CI: Enforce newline at end of files
Reference:
https://medium.com/@alexey.inkin/how-to-force-newline-at-end-of-files-and-why-you-should-do-it-fdf76d1d090e
2024-06-21 16:39:05 +08:00
Kuan-Wei Chiu
0a23ecd027 vinput: Fix incorrect handling on raw_copy_to_user() failure
When raw_copy_to_user() failed in vinput_read(), the function would set
'count' to -EFAULT and then subtract EFAULT from '*offset'. However,
modifying '*offset' on raw_copy_to_user() failure was incorrect. Fix
this behavior by changing count = -EFAULT to return -EFAULT.
2024-05-09 02:01:18 +08:00
Kuan-Wei Chiu
c068fa47e9 vinput: Fix NULL pointer dereference caused by failed kzalloc allocation
When kzalloc fails to allocate memory and returns NULL, it leads to a
NULL pointer dereference error later on. Add a check for the return
value of kzalloc. When kzalloc fails to allocate memory, it prints an
error message and returns ERR_PTR(-ENOMEM).
2024-05-09 02:01:14 +08:00
Kuan-Wei Chiu
1e26743c30 vinput: Remove redundant memset call
Remove redundant memset calls as memory allocated with kzalloc is
already zeroed due to the presence of the __GFP_ZERO gfp flag.
2024-05-09 01:38:14 +08:00
Jim Huang
9952f38afb Fix style 2024-04-21 01:12:33 +08:00
Yo-Jung Lin
0c3aaac3c1 Pass proper cookies to the request_threaded_irq
The last parameter of request_threaded_irq must be a globally unique
cookie[1]. Usually this would be the device struct received by probe().
Since we are not using driver model, pass the gpio structs instead.

[1] https://docs.kernel.org/core-api/genericirq.html
2024-04-21 01:09:32 +08:00
Yo-Jung Lin
65397e2d53 Correct the IRQ numbers passed to the request_threaded_irq 2024-04-21 01:09:32 +08:00
Jim Huang
56f566abe6 Provide a tasklet-free example
Co-authored-by: Bob Mottram <bob@freedombone.net>
2024-04-21 01:09:28 +08:00
Jim Huang
e1b44579b0 Ensure Linux v6.5 compatibility 2024-04-16 06:24:06 +08:00
Jim Huang
1c31bac22c Fix Linux v6.5 compatibility 2024-04-16 06:19:52 +08:00
Jim Huang
f5893d8140 Remove the crypto section due to poor maintenance
The past content in the crypto section lacks informative descriptions,
and there should be a proper procedure to demonstrate how Linux
cryptography works. Due to poor maintenance, let's drop the section.
2024-04-16 05:39:11 +08:00
Jim Huang
1d38d2ed19
Merge pull request #253 from weihsinyeh/master
Fix typo
2024-04-16 04:58:32 +08:00
weihsinyeh
f52c126c43 Fix typo
Correct `parameters name` to `parameter's name` in a comment.
2024-04-16 02:38:07 +08:00
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