Skip to contentSkip to conversation

Best method for sending messages to user space?

DISCUSSION Timur Tabi 2 participants 6 messages Oct 21, 2008

lore
Share & tools
lore

Timur Tabi

author
#0 lore raw

I have a driver that provides services for a hypervisor that my company is creating. The hypervisor can send notifications to the driver via interrupts. I want to send a message to user space whenever the driver receives one of the interrupts. The messages don't have any payload.

Should I be creating kobjects and using kobject_uevent(..., KOBJ_CHANGE) to send these messages? I want to take advantage of the standard (hotplug?) user-space interface for this sort of thing, so that I don't need to have a custom-written daemon running that waits on blocking ioctl calls.

Signature · 2 lines
Timur Tabi
Linux kernel developer at Freescale
#1 lore raw
Quoted 10 lines · Timur Tabi

I have a driver that provides services for a hypervisor that my company is creating. The hypervisor can send notifications to the driver via interrupts. I want to send a message to user space whenever the driver receives one of the interrupts. The messages don't have any payload.

Should I be creating kobjects and using kobject_uevent(..., KOBJ_CHANGE) to send these messages? I want to take advantage of the standard (hotplug?) user-space interface for this sort of thing, so that I don't need to have a custom-written daemon running that waits on blocking ioctl calls.

I really like the sysfs_notify() approach that Neil Brown has put together for delivering md-raid events. Now with 2.6.28 you will be able do this from an atomic context with sysfs_notify_dirent() [1].

--
Dan

[1] http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commitdiff;h=f1282c84

author
#2 lore raw
Quoted 2 lines · Dan Williams

I really like the sysfs_notify() approach that Neil Brown has put together for delivering md-raid events.

Is there any documentation for sysfs_notify? It doesn't appear to do anything whenever I call it.

Quoted 2 lines

Now with 2.6.28 you will be
able do this from an atomic context with sysfs_notify_dirent() [1].

That sounds like something I could use.

Kumar, can you update the repository I'm using to include sysfs_notify_dirent()?

Signature · 2 lines
Timur Tabi
Linux kernel developer at Freescale
#3 lore raw
Quoted 8 lines · Timur Tabi
Quoted 2 lines · Dan Williams

I really like the sysfs_notify() approach that Neil Brown has put together for delivering md-raid events.

Is there any documentation for sysfs_notify? It doesn't appear to do anything whenever I call it.

It simply allows you to write an event loop in userspace using a sysfs attribute file descriptor. Pseudo-example:

fd = open("/sys/block/md0/md/array_state");

do {
	read(buf, fd, len);
	act_on_message(buf);
	select(...); /* wait for next sysfs_notify event */
	lseek(fd, 0, SEEK_SET); /* seek back so we can read the new state */
} while (1);
author
#4 lore raw
Quoted 7 lines · Dan Williams

fd = open("/sys/block/md0/md/array_state");

do {
	read(buf, fd, len);
	act_on_message(buf);
	select(...); /* wait for next sysfs_notify event */
	lseek(fd, 0, SEEK_SET); /* seek back so we can read the new state */
} while (1);

I meant from the kernel side. I added a call to sysfs_notify(), and I didn't see any sysfs entries being created, so I presume I need to set up sysfs before I call sysfs_notify(), but I can't figure out what that setup is.

Signature · 2 lines
Timur Tabi
Linux kernel developer at Freescale
#5 lore raw
Quoted 10 lines · Timur Tabi
Quoted 7 lines · Dan Williams

fd = open("/sys/block/md0/md/array_state");
do {
read(buf, fd, len);
act_on_message(buf);
select(...); /* wait for next sysfs_notify event */ lseek(fd, 0, SEEK_SET); /* seek back so we can read the new state */
} while (1);

Quoted 4 lines

I meant from the kernel side. I added a call to sysfs_notify(), and I didn't see any sysfs entries being created, so I presume I need to set up sysfs before I call sysfs_notify(), but I can't figure out what that setup is.

sysfs_notify() will not create any files it only operates on pre-existing attributes created via kobject_{init_and_}add(). See:
http://lxr.linux.no/linux+v2.6.27/drivers/md/md.c#L3473
http://lxr.linux.no/linux+v2.6.27/drivers/md/md.c#L3761