10000 Bugfix: replace the kzalloc() with devm_kzalloc(); thanks to @brian-wood · yurembo/Linux-Kernel-Programming@9fcd61f · GitHub
[go: up one dir, main page]

Skip to content

Commit 9fcd61f

Browse files
committed
Bugfix: replace the kzalloc() with devm_kzalloc(); thanks to @brian-wood
for catching this
1 parent e92f966 commit 9fcd61f

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

ch12/1_miscdrv_rdwr_mutexlock/miscdrv_rdwr_mutexlock.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,21 @@
1111
* From: Ch 12 : Kernel Synchronization - Part 1
1212
****************************************************************
1313
* Brief Description:
14-
* This driver is built upon our previous ch12/miscdrv_rdwr/ misc driver.
14+
* This driver is built upon the LKP Part 2 book's first chapter 'misc' driver here:
15+
* https://github.com/PacktPublishing/Linux-Kernel-Programming-Part-2/tree/main/ch1/miscdrv_rdwr
16+
*
1517
* The really important and key difference: previously, we used a few global data
1618
* items throughout *without* protection; this time, we fix this egregious error
1719
* by using a mutex lock to protect the critical sections - the places in the
1820
* code where we access global / shared writeable data.
1921
* The functionality (the get and set of the 'secret') remains identical to the
2022
* original implementation.
2123
*
22-
* For details, please refer the book, Ch 12.
24+
* Note: also do
25+
* make rdwr_test_secret
26+
* to build the user space app for testing...
27+
*
28+
* For details, please refer both the books, Ch 12 and Ch 1 resp.
2329
*/
2430
#define pr_fmt(fmt) "%s:%s(): " fmt, KBUILD_MODNAME, __func__
2531

@@ -286,7 +292,7 @@ static int __init miscdrv_init_mutexlock(void)
286292
* freeing the memory automatically upon driver 'detach' or when the driver
287293
* is unloaded from memory
288294
*/
289-
ctx = kzalloc(sizeof(struct drv_ctx), GFP_KERNEL);
295+
ctx = devm_kzalloc(llkd_miscdev.this_device, sizeof(struct drv_ctx), GFP_KERNEL);
290296
if (unlikely(!ctx))
291297
return -ENOMEM;
292298

ch12/2_miscdrv_rdwr_spinlock/miscdrv_rdwr_spinlock.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,18 @@
1313
* Brief Description:
1414
* This driver is built upon our previous ch12/1_miscdrv_rdwr_mutexlock/
1515
* misc driver.
16-
* The key difference: we use spinlocks in place of the mutex locks (this isn't
16+
*
17+
* The key difference: we use a spinlock in place of the mutex locks (this isn't
1718
* the case everywhere in the driver though; we keep the mutex as well for some
1819
* portions of the driver).
20+
* The functionality (the get and set of the 'secret') remains identical to the
21+
* original implementation.
22+
*
23+
* Note: also do
24+
* make rdwr_test_secret
25+
* to build the user space app for testing...
1926
*
20-
* For details, please refer the book, Ch 12.
27+
* For details, please refer both the books, Ch 12 and Ch 1 resp.
2128
*/
2229
#define pr_fmt(fmt) "%s:%s(): " fmt, KBUILD_MODNAME, __func__
2330

@@ -324,7 +331,7 @@ static int __init miscdrv_init_spinlock(void)
324331
* freeing the memory automatically upon driver 'detach' or when the driver
325332
* is unloaded from memory
326333
*/
327-
ctx = kzalloc(sizeof(struct drv_ctx), GFP_KERNEL);
334+
ctx = devm_kzalloc(llkd_miscdev.this_device, sizeof(struct drv_ctx), GFP_KERNEL);
328335
if (unlikely(!ctx))
329336
return -ENOMEM;
330337

0 commit comments

Comments
 (0)
0