Commit Graph

117 Commits

Author SHA1 Message Date
Bob Lee
620bb0f5f8
Fix missing function call in error handling path
Previously, the code did not call kobject_put when sysfs_create_file
failed, leading to a potential memory leak. This commit adds the missing
kobject_put to ensure proper resource cleanup.
2024-11-05 19:48:42 +08:00
Bob Lee
b482911efb
Fix potential memory leak in vinput
In the export_store function, the error handling paths followed a
successful vinput_alloc_vdevice call are missing a corresponding
input_free_device call. Since vinput_alloc_vdevice internally calls
input_allocate_device, and input_register_device has not been called
yet, input_free_device should be used to properly free the allocated
input_device struct in this scenario[1].

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/input/input.c#n2094
2024-10-25 12:22:29 +08:00
Bob Lee
3ec01e2cf1
Fix potential reference count leak in vinput
In the export_store function, when vinput_register_vdevice fails, the
current error handling process calls device_unregister followed by
vinput_destroy_vdevice. However, the device_unregister here triggers its
release function, which already includes a call to
vinput_destroy_vdevice. As a result, vinput_destroy_vdevice is called
twice. Since vinput_destroy_vdevice contains module_put, this double
call must be avoided to prevent potential reference count issues.
2024-10-24 19:24:01 +08:00
Bob Lee
9b89d0ca04
Fix incorrect failure handling in vinput_init
After class_register failure, the wrong function class_unregister was
used. The correct cleanup function should be unregister_chrdev, as
register_chrdev was used before class_register.
2024-10-21 00:33:06 +08:00
ChinYikMing
4002426f7a Fix typo
The "Message" should be in lower case, so that it matches the variable
which declared as "message".
2024-10-17 02:23:21 +08:00
Jim Huang
756871e427
Merge pull request #273 from NOVBobLee/pr_patch
Some checks failed
build-deploy-assets / build (push) Has been cancelled
deploy-github-page / build (push) Has been cancelled
status-checks / validate (push) Has been cancelled
Correct the function type
2024-10-09 00:56:51 +08:00
Bob Lee
93b35f7819
Fix function declaration
The function pointer type of myvariable_store was incompatible with the
store member of struct kobj_attribute, leading to a compilation error
when the type conversion (void *) was omitted. This patch corrects the
function declaration and eliminates the need for type conversion.
2024-10-09 00:45:04 +08:00
Bob Lee
2a04a64041
Remove the redundant code
As myvariable is declared as an integer, it is meaningless that putting
u after %d in the format specifier of sscanf for scanning an integer.
This patch removes the u in the format specifier for avoiding
misleading.
2024-10-08 12:21:25 +08:00
Bob Lee
d1d2a2b031
Fix a weird behavior of a function
The procfile_write prints the content what user writes into. However,
when the content size is greater than or equal to PROCFS_MAX_SIZE,
procfile_write will print nothing, because the index for appending the
tail NULL character will be modulo to 0, which is an off-by-one error.

This fixes the problem by changing the upper bound of procfs_buffer_size
to (PROCFS_MAX_SIZE - 1), leaving one byte for NULL character. After
the change, we can discard the modulo because the range of
procfs_buffer_size is already between 0 and (PROCFS_MAX_SIZE - 1).
2024-10-07 12:05:43 +08:00
Bob Lee
8c641a2deb
Remove the redundant code
proc_remove does nothing when the argument is NULL[1]. We don't need
proc_remove after proc_create failed.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/proc/generic.c#n789
2024-10-06 14:25:49 +08:00
Jim Huang
3a09a1e473 Apply editorial changes
Some checks failed
build-deploy-assets / build (push) Has been cancelled
deploy-github-page / build (push) Has been cancelled
status-checks / validate (push) Has been cancelled
2024-10-05 12:01:45 +08:00
Andrew Kreimer
98201b85aa Fix typos via codespell
atleast -> at least
encouter -> encounter
2024-10-04 19:08:50 +03:00
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