Segmentation fault while trying to boot after adding a device driver

643 Views Asked by At

I'm trying to compile and install linux 4.20.17 after adding my own kernel module (a simple multiplier module) source files. I am successfully able to compile and install linux-4.20.17 with the source files. The problem occurs when i try to boot up the system. I get a segmentation fault when my multiplier module is getting inserted. enter image description here

When i try to insert the module on my own using insmod everything works fine. I can even create a device file, read and write to it.

This is my module_init() function.

int my_init(void)
{
  valueToBeWritten = (int *)kmalloc(INT_BUF_LEN*sizeof(int), GFP_KERNEL);
   msg_bf_Ptr = (char *)valueToBeWritten; //msg_bf_Ptr is in kernel space


  if (msg_bf_Ptr == NULL) {     
    /* Failed to get memory, exit gracefully */
    printk(KERN_ALERT "Unable to allocate needed memory\n");

    return 10;          /* Defining error code of 10 for
                   "Unable to allocate memory" */
  }

  Major = register_chrdev(0, DEVICE_NAME, &fops);

  /* Negative values indicate a problem */
  if (Major < 0) {      

    printk(KERN_ALERT "Registering char device failed with %d\n", Major);

    kfree(valueToBeWritten);

    printk(KERN_INFO "Mapping virtual address...\n");

    printk("Physical address: %x; Virtual address: %x\n.", (unsigned int)virt_addr, (unsigned int)PHY_ADDR);

    return Major;
  }

  sprintf(msg_bf_Ptr, "This device has not been written to yet\n");
  cur_Ptr = msg_bf_Ptr;

  printk(KERN_INFO "Registered a device with dynamic Major number of %d\n", Major);

  printk(KERN_INFO "Create a device file for this device with this command:\n'mknod /dev/%s c %d 0'.\n", DEVICE_NAME, Major);

  return 0;     /* success */
}

I read that "modules cannot reserve resources during boot time". Is that what's causing the problem? Is the kmalloc statement causing the segmentation fault?

EDIT: I booted in through recovery mode and tried to look at the full error trace. But I couldn't find the segmentation fault. But here is the call trace where the multiplier module gets loaded.

Apr 22 16:19:25 student-VirtualBox kernel: [  247.918412] Call Trace:
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918431]  apparmor_sk_alloc_security+0x49/0x70
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918438]  security_sk_alloc+0x37/0x50
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918449]  sk_prot_alloc+0x58/0x130
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918453]  sk_alloc+0x31/0x240
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918459]  unix_create1+0x68/0x1e0
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918464]  unix_stream_connect+0x97/0x4b0
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918469]  __sys_connect+0xa3/0x120
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918476]  __x64_sys_connect+0x1a/0x20
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918481]  do_syscall_64+0x5a/0x120
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918487]  entry_SYSCALL_64_after_hwframe+0x44/0xa9
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918491] RIP: 0033:0x7f32457968e7
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918494] Code: c3 66 90 41 54 55 41 89 d4 53 48 89 f5 89 fb 48 83 ec 10 e8 0b de 00 00 44 89 e2 41 89 c0 48 89 ee 89 df b8 2a 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 33 44 89 c7 89 44 24 0c e8 45 de 00 00 8b 44
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918497] RSP: 002b:00007f32382fb6f0 EFLAGS: 00000293 ORIG_RAX: 000000000000002a
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918500] RAX: ffffffffffffffda RBX: 0000000000000005 RCX: 00007f32457968e7
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918502] RDX: 000000000000006e RSI: 00007f32382fb720 RDI: 0000000000000005
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918504] RBP: 00007f32382fb720 R08: 0000000000000000 R09: 00007f32300145b0
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918507] R10: 0000000000000000 R11: 0000000000000293 R12: 000000000000006e
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918509] R13: 0000000000000000 R14: 00007f32382fb720 R15: 00007f3230017000
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918514] Modules linked in: multiplier(OE) crct10dif_pclmul crc32_pclmul ghash_clmulni_intel aesni_intel vboxvideo(C) aes_x86_64 crypto_simd ttm cryptd drm_kms_helper glue_helper drm joydev intel_rapl_perf input_leds fb_sys_fops syscopyarea serio_raw sysfillrect sysimgblt mac_hid sch_fq_codel parport_pc ppdev lp parport ip_tables x_tables autofs4 hid_generic usbhid hid psmouse ahci libahci pata_acpi e1000 i2c_piix4 video
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918604] ---[ end trace 821aa2233cf391a7 ]---
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918611] RIP: 0010:__kmalloc_track_caller+0xaf/0x210
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918614] Code: 39 7a 49 83 78 10 00 4d 8b 38 0f 84 ee 00 00 00 4d 85 ff 0f 84 e5 00 00 00 41 8b 59 20 49 8b 39 48 8d 4a 01 4c 89 f8 4c 01 fb <48> 33 1b 49 33 99 38 01 00 00 65 48 0f c7 0f 0f 94 c0 84 c0 74 b2
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918617] RSP: 0018:ffffa994814f3d18 EFLAGS: 00010202
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918620] RAX: 06fb5d499a5b6b73 RBX: 06fb5d499a5b6b73 RCX: 00000000000013b4
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918623] RDX: 00000000000013b3 RSI: 00000000006000c0 RDI: 0000000000027060
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918624] RBP: ffffa994814f3d50 R08: ffff98acbda27060 R09: ffff98acbd403b00
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918627] R10: ffffffff870ff020 R11: 304a565a46412d72 R12: 00000000006000c0
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918629] R13: 000000000000000a R14: ffff98acbd403b00 R15: 06fb5d499a5b6b73
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918632] FS:  00007f32382fc700(0000) GS:ffff98acbda00000(0000) knlGS:0000000000000000
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918635] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
Apr 22 16:19:25 student-VirtualBox kernel: [  247.918637] CR2: 00007f32382faff8 CR3: 000000005c9b2001 CR4: 00000000000606f0
Apr 22 16:19:39 student-VirtualBox kernel: [  261.077288] general protection fault: 0000 [#3] SMP PTI
Apr 22 16:19:39 student-VirtualBox kernel: [  261.077306] CPU: 0 PID: 1119 Comm: upowerd Tainted: G      D  C OE     4.20.17 #1
Apr 22 16:19:39 student-VirtualBox kernel: [  261.077312] Hardware name: innotek GmbH VirtualBox/VirtualBox, BIOS VirtualBox 12/01/2006
Apr 22 16:19:39 student-VirtualBox kernel: [  261.077325] RIP: 0010:__kmalloc+0xb3/0x220
Apr 22 16:19:39 student-VirtualBox kernel: [  261.077333] Code: 39 7a 49 83 78 10 00 4d 8b 38 0f 84 fb 00 00 00 4d 85 ff 0f 84 f2 00 00 00 41 8b 59 20 49 8b 39 48 8d 4a 01 4c 89 f8 4c 01 fb <48> 33 1b 49 33 99 38 01 00 00 65 48 0f c7 0f 0f 94 c0 84 c0 74 b2

I'm pretty new to device drivers I can't seem to make sense out of these logs I tried looking up some of the codes and phrases from the log but couldn't arrive at anything solid.

0

There are 0 best solutions below