LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 06-29-2017, 04:02 AM   #1
maksymilian.piechota
LQ Newbie
 
Registered: Jun 2017
Posts: 2

Rep: Reputation: Disabled
creating a sysfs directory with kobjects


Hi,

I'm trying to create a simple directory with file inside using kobjects. I follow kobject-example.c from sample/kobjects directory in kernel source tree. My code is following:

Code:
struct kobject *dirret;
static int foo;

static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr,
		char *buf)
{
	return sprintf(buf, "%d\n", foo);
}

ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr,
		const char *buf, size_t count)
{
	sscanf(buf, "%du", &foo);
	return count;
}

struct kobj_attribute foo_attribute = __ATTR(foo, 0660, foo_show, foo_store);

static struct attribute * attrs[] =
{
		&foo_attribute.attr,
		NULL,
};

static struct attribute_group attr_group = {
		.attrs = attrs,
};

struct semaphore Semaphore;

int init_task(void)
{
	printk(KERN_INFO "Task module init.\n");

	dirret = kobject_create_and_add("eudyptula", NULL);

	printk(KERN_INFO "eudyptula dir pointer: %i\n", (int)dirret);

	filevalue = sysfs_create_group(dirret, &attr_group);
	if(filevalue){
		kobject_put(dirret);
		return filevalue;
	}

	sema_init(&Semaphore, 1);

	/* 
	 * A non 0 return means init_module failed; module can't be loaded. 
	 */
	return 0;
}
module_init(init_task);
and when I load this module process is killed and dmesg shows:
Code:
[   72.377242] task: loading out-of-tree module taints kernel.
[   72.377311] task: module verification failed: signature and/or required key missing - tainting kernel
[   72.379915] Task module init.
[   72.379928] eudyptula dir pointer: 1817486104
[   72.379931] BUG: unable to handle kernel NULL pointer dereference at           (null)
[   72.379939] IP: init_module+0x51/0x148 [task]
[   72.379941] PGD 0 
[   72.379941] P4D 0 

[   72.379945] Oops: 0002 [#1] SMP
[   72.379947] Modules linked in: task(OE+) rfcomm vmw_vsock_vmci_transport vsock bnep vmw_balloon snd_ens1371 snd_ac97_codec ac97_bus gameport snd_pcm coretemp crct10dif_pclmul crc32_pclmul ghash_clmulni_intel pcbc snd_seq_midi aesni_intel aes_x86_64 crypto_simd glue_helper cryptd snd_seq_midi_event intel_rapl_perf snd_rawmidi input_leds snd_seq joydev serio_raw uvcvideo videobuf2_vmalloc snd_seq_device videobuf2_memops snd_timer btusb videobuf2_v4l2 videobuf2_core btrtl videodev btbcm btintel bluetooth snd media soundcore ecdh_generic vmw_vmci i2c_piix4 shpchp nfit mac_hid parport_pc ppdev lp parport autofs4 vmw_pvscsi vmxnet3 vmwgfx hid_generic ttm drm_kms_helper syscopyarea sysfillrect sysimgblt psmouse fb_sys_fops drm ahci usbhid e1000 hid mptspi libahci mptscsih mptbase scsi_transport_spi pata_acpi
[   72.379989] CPU: 1 PID: 2880 Comm: insmod Tainted: G    B      OE   4.12.0-rc2+ #3
[   72.379991] Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 07/02/2015
[   72.379993] task: ffff9fabf8253280 task.stack: ffffafc1449b8000
[   72.379996] RIP: 0010:init_module+0x51/0x148 [task]
[   72.379997] RSP: 0018:ffffafc1449bbc88 EFLAGS: 00010282
[   72.379998] RAX: 0000000000000000 RBX: ffffffffc0209080 RCX: 0000000000000006
[   72.379999] RDX: 00000000000001b6 RSI: ffffffffc0209000 RDI: ffff9fac8164df40
[   72.380000] RBP: ffffafc1449bbc90 R08: 0000000000000001 R09: 00000000000007d9
[   72.380001] R10: ffff9fac6c54a318 R11: 00000000000007d9 R12: ffffffffc0207060
[   72.380002] R13: 0000000000000000 R14: ffff9fabe6d1fb18 R15: 0000000000000001
[   72.380003] FS:  00007fd2a5809700(0000) GS:ffff9fac81640000(0000) knlGS:0000000000000000
[   72.380004] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   72.380005] CR2: 0000000000000000 CR3: 0000000104dc9000 CR4: 00000000001406e0
[   72.380049] Call Trace:
[   72.380056]  do_one_initcall+0x53/0x190
[   72.380060]  ? kmem_cache_alloc_trace+0x18f/0x1e0
[   72.380062]  ? kfree+0x18e/0x1a0
[   72.380067]  do_init_module+0x5f/0x20d
[   72.380071]  load_module+0x282d/0x2c10
[   72.380074]  ? ima_post_read_file+0x7d/0xa0
[   72.380078]  ? security_kernel_post_read_file+0x6b/0x80
[   72.380081]  SYSC_finit_module+0xdf/0x110
[   72.380084]  ? SYSC_finit_module+0xdf/0x110
[   72.380087]  SyS_finit_module+0xe/0x10
[   72.380091]  entry_SYSCALL_64_fastpath+0x1e/0xa9
[   72.380092] RIP: 0033:0x7fd2a5338499
[   72.380094] RSP: 002b:00007ffc28371f38 EFLAGS: 00000202 ORIG_RAX: 0000000000000139
[   72.380096] RAX: ffffffffffffffda RBX: 00007fd2a55fbb20 RCX: 00007fd2a5338499
[   72.380098] RDX: 0000000000000000 RSI: 000055a6b2723246 RDI: 0000000000000003
[   72.380100] RBP: 0000000000001011 R08: 0000000000000000 R09: 00007fd2a55fdea0
[   72.380101] R10: 0000000000000003 R11: 0000000000000202 R12: 00007fd2a55fbb78
[   72.380102] R13: 00007fd2a55fbb78 R14: 000000000000270f R15: 00007fd2a55fc1a8
[   72.380104] Code: c4 e3 d8 48 c7 c7 4b 80 20 c0 89 c6 48 89 05 37 24 00 00 e8 f1 88 ba d8 48 8b 05 23 24 00 00 ba b6 01 00 00 48 c7 c6 00 90 20 c0 <48> c7 00 68 80 20 c0 48 8b 05 09 24 00 00 48 8b 3d 0a 24 00 00 
[   72.380126] RIP: init_module+0x51/0x148 [task] RSP: ffffafc1449bbc88
[   72.380127] CR2: 0000000000000000
[   72.380128] ---[ end trace a7920e8cfd6a71df ]---
Any ideas what went wrong? Many thanks in advance.

Maks
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] "kobject and sysfs does not create a directory in sys/kernel" mrigendra Linux - Newbie 1 04-04-2017 11:44 AM
Is there a Kobject associated to every sysfs directory ? akhilbaai Linux - Kernel 1 02-24-2017 07:47 AM
[SOLVED] Creating a directory city83 Linux - Newbie 4 10-09-2011 02:55 PM
Useradd not creating home directory when creating newuser meneedham Linux - Newbie 4 10-05-2007 12:11 PM
libbridge_private.h:36:28: sysfs/libsysfs.h: No such file or directory cellist Linux - Software 1 12-07-2004 10:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 12:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration