Pranay Tummalapalli
Pranay Tummalapalli

Reputation: 1

kthread_stop() causes memory segmentation when unloading a beaglebone black module

I am just starting to learn kernel module development on the BeagleBone Black platform. Just trying to run a simple thread and exit it (find code below). when i load the module everything works fine but when i try to unload the module, im getting memory segmentation errors as you can see from the log below. What am i doing wrong here?

#include<linux/module.h>     // included for all kernel modules
#include<linux/init.h>       // included for __init and __exit macros
#include<linux/kernel.h>
#include<linux/kthread.h>    // included for threading related functions
// #include<linux/io.h>
#include<linux/sched.h>      // included to create tesk_struct
#include<linux/delay.h>      // included for the sleep/delay function in the thread

static struct task_struct *kthread;

/*
*@thread function that will contain the main functionality to run inside the kthread
*/
int thread_function(void *idx){
    pr_info("GPIO toggle thread running!\n");
    return 0;
}

/*
*@init function for loading the module
*/
static int __init mod_init(void){
    pr_info("Initialising thread module\n");

    kthread = kthread_create(thread_function, NULL, "GPIO_Thread");
    if(kthread != NULL){
        wake_up_process(kthread);
        pr_info("%s is running\n", "th_name");
    }else{
        pr_info("kthread %s could not be created...\n", "th_name");
        return -1;
    }
    pr_info("the thread is initialised and running!\n");
    return 0;
}

/*
*@exit module to unload the module
*/
static void __exit mod_exit(void){
    pr_info("exiting thread module...\n");
    // if(!kthread_stop(&toggle_thread)){
    //     pr_info("cant stop process thread...\n");
    // }
    int kstp = kthread_stop(kthread);
    pr_info("kthread val = %d", kstp);
    pr_info("thread module stopped!\n");
} 

module_init(mod_init);
module_exit(mod_exit);

MODULE_LICENSE("GPL");

These are the error logs i get from dmesg:

[   38.940983] Initialising thread module
[   38.941259] th_name is running
[   38.941273] the thread is initialised and running!
[   38.950336] GPIO toggle thread running!
[   56.697560] exiting thread module...
[   56.697595] ------------[ cut here ]------------
[   56.697603] WARNING: CPU: 0 PID: 226 at kernel/kthread.c:73 to_kthread+0x24/0x30
[   56.697639] Modules linked in: gpio_kthread_toggle(O-) sch_fq_codel
[   56.697670] CPU: 0 PID: 226 Comm: rmmod Tainted: G           O      5.15.54-yocto-standard #1
[   56.697684] Hardware name: Generic AM33XX (Flattened Device Tree)
[   56.697705] [<c010e378>] (unwind_backtrace) from [<c010ab3c>] (show_stack+0x18/0x1c)
[   56.697740] [<c010ab3c>] (show_stack) from [<c012c268>] (__warn+0xc4/0xf0)
[   56.697763] [<c012c268>] (__warn) from [<c0b395dc>] (warn_slowpath_fmt+0x80/0xc0)
[   56.697792] [<c0b395dc>] (warn_slowpath_fmt) from [<c0149e60>] (to_kthread+0x24/0x30)
[   56.697813] [<c0149e60>] (to_kthread) from [<c014b5e4>] (kthread_stop+0xb0/0x190)
[   56.697834] [<c014b5e4>] (kthread_stop) from [<bf006038>] (mod_exit+0x18/0xfe0 [gpio_kthread_toggle])
[   56.697869] [<bf006038>] (mod_exit [gpio_kthread_toggle]) from [<c01a800c>] (sys_delete_module+0x184/0x1d4)
[   56.697904] [<c01a800c>] (sys_delete_module) from [<c0100060>] (ret_fast_syscall+0x0/0x48)
[   56.697928] Exception stack(0xc2ff7fa8 to 0xc2ff7ff0)
[   56.697944] 7fa0:                   00490140 bedc3bf0 0049017c 00000800 00000000 00000000
[   56.697959] 7fc0: 00490140 bedc3bf0 00000000 00000081 bedc3efd 0048f190 00000001 bedc3dfc
[   56.697970] 7fe0: 0048df78 bedc3b9c 00472e1c b6e310ac
[   56.697980] ---[ end trace cd1d2e4e6a03f146 ]---
[   56.698005] 8<--- cut here ---
[   56.698011] Unable to handle kernel paging request at virtual address b6fc9088
[   56.698020] pgd = d1e1572e
[   56.698035] [b6fc9088] *pgd=839ea831, *pte=00000000, *ppte=00000000
[   56.698063] Internal error: Oops: 17 [#1] PREEMPT ARM
[   56.701845] Modules linked in: gpio_kthread_toggle(O-) sch_fq_codel
[   56.706842] CPU: 0 PID: 226 Comm: rmmod Tainted: G        W  O      5.15.54-yocto-standard #1
[   56.714098] Hardware name: Generic AM33XX (Flattened Device Tree)
[   56.718908] PC is at kthread_stop+0xbc/0x190
[   56.721893] LR is at to_kthread+0x24/0x30
[   56.724614] pc : [<c014b5f0>]    lr : [<c0149e60>]    psr: 60070093
[   56.729600] sp : c2ff7f40  ip : 00000000  fp : bedc3dfc
[   56.733537] r10: 00000081  r9 : c2ff6000  r8 : c01002c8
[   56.737474] r7 : 00000081  r6 : b6fc9088  r5 : c3cb3a00  r4 : c3cb3a08
[   56.742722] r3 : 00000000  r2 : 60070013  r1 : c13859e0  r0 : b6fc9088
[   56.747970] Flags: nZCv  IRQs off  FIQs on  Mode SVC_32  ISA ARM  Segment none
[   56.753918] Control: 10c5387d  Table: 82d8c019  DAC: 00000051
[   56.758377] Register r0 information: non-paged memory
[   56.762148] Register r1 information: non-slab/vmalloc memory
[   56.766526] Register r2 information: non-paged memory
[   56.770292] Register r3 information: NULL pointer
[   56.773708] Register r4 information: slab task_struct start c3cb3a00 pointer offset 8
[   56.780285] Register r5 information: slab task_struct start c3cb3a00 pointer offset 0
[   56.786855] Register r6 information: non-paged memory
[   56.790620] Register r7 information: non-paged memory
[   56.794385] Register r8 information: non-slab/vmalloc memory
[   56.798762] Register r9 information: non-slab/vmalloc memory
[   56.803138] Register r10 information: non-paged memory
[   56.806991] Register r11 information: non-paged memory
[   56.810844] Register r12 information: NULL pointer
[   56.814349] Process rmmod (pid: 226, stack limit = 0x96081cd1)
[   56.818900] Stack: (0xc2ff7f40 to 0xc2ff8000)
[   56.821973] 7f40: bf008000 00000000 00000000 bf006038 bf008000 c01a800c 6f697067 68746b5f
[   56.828883] 7f60: 64616572 676f745f 00656c67 c02e4768 c3cea600 c0148870 c3cea300 c3cea300
[   56.835791] 7f80: c3cea600 c323d64c 00000000 00000000 00f615a0 c323d64c 00000006 00490140
[   56.842701] 7fa0: bedc3bf0 c0100060 00490140 bedc3bf0 0049017c 00000800 00000000 00000000
[   56.849609] 7fc0: 00490140 bedc3bf0 00000000 00000081 bedc3efd 0048f190 00000001 bedc3dfc
[   56.856517] 7fe0: 0048df78 bedc3b9c 00472e1c b6e310ac 20070010 0049017c 00000000 00000000
[   56.863421] [<c014b5f0>] (kthread_stop) from [<bf006038>] (mod_exit+0x18/0xfe0 [gpio_kthread_toggle])
[   56.871391] [<bf006038>] (mod_exit [gpio_kthread_toggle]) from [<c01a800c>] (sys_delete_module+0x184/0x1d4)
[   56.879885] [<c01a800c>] (sys_delete_module) from [<c0100060>] (ret_fast_syscall+0x0/0x48)
[   56.886888] Exception stack(0xc2ff7fa8 to 0xc2ff7ff0)
[   56.890656] 7fa0:                   00490140 bedc3bf0 0049017c 00000800 00000000 00000000
[   56.897565] 7fc0: 00490140 bedc3bf0 00000000 00000081 bedc3efd 0048f190 00000001 bedc3dfc
[   56.904471] 7fe0: 0048df78 bedc3b9c 00472e1c b6e310ac
[   56.908240] Code: ebfffa15 e1a06000 e10f2000 f10c0080 (e5903000) 
[   56.913054] ---[ end trace cd1d2e4e6a03f147 ]---

Upvotes: 0

Views: 57

Answers (0)

Related Questions