Linux suspend/resume operations' userspace notification - linux

I'm working on an embedded linux platform.
When I do "echo "mem" > /sys/power/state", system will suspend.
I know that kernel and driver can know that suspend operation's coming. But would it be possible that a user space process or application can get the notification that the system will suspend? How?
For example, I have an application who writes 'A' continually into a buffer whose start address is given by a device driver. Would it be possible that this application be notified that the system will suspend so that it could replace all this buffer with 'B' so that when driver is resumed, all what driver sees are 'B'?
Thanks a lot.

Been searching for the same thing. But unfortunately, I didn't find any user space notification during suspend/resume. Applications are just refrigerated/frozen and they will never know they are suspended.
However, one possible approach would be to add a generic netlink message sending or uevent from any driver's suspend/resume function that you can modify. Still the application may never get enough time to process it before it is frozen and might lead to race conditions. Say it received the suspend message and got frozen before it could process it. And once resumed, it will be processing the suspend message.
IMO, it is better to handle the scenario in the driver. Leave the user space alone.

I'm not sure whether it's useful for you in particular, given the mention of "embedded", however systemd can notify you over DBus: https://www.freedesktop.org/wiki/Software/systemd/inhibit/

Related

Automatically suspend process

I saw that Windows/Linux has the ability to suspend a process.
It is wired for me why background applications are not suspended automatically.
For example, lots of resources are used by Chrome when it is in the background. Easily it can be suspended. So it will stay in RAM and it can unsuspend quickly but it will not use CPU and GPU.
My question contains two parts:
Why Windows/Linux (or applications) don't use suspend feature? (sth similar to pause in Android but in the different way)
Is there any way to suspend a background task and unsuspend it when it gets focus (when it goes to foreground)?
A process like Chrome might not have input focus on the user interface but still be "running." (Chrome consists of a set of related processes and threads.)
Yes, Linux does have the ability to actually "suspend" a process using the STOP/CONT signals, but this would be disruptive to the user interface because Chrome, now being literally frozen, could no longer respond to messages sent to it by the user interface.
Processes and threads only consume CPU resources when they actually need to (they are "runnable"), and then only when the operating system gives them a time-slice. If a thread or process is, say, "waiting for the user interface to send it a message," it's not considered to be "runnable" until a message arrives.
It's also typical that, when a process doesn't have input focus, its priority is slightly reduced so that it always gives-way to the process that does. In some systems, the priority is even more reduced when you minimize the window. (When several processes are "runnable," the operating system uses "priority" to help it to decide which one to run next.)

shared mem betwwen kernel/user space and select

I have some specific hardware which run on FreeBSD and Linux.
I have to do an user space application which will work with the driver using shared memory between kernel/user space application.
My application does busy poll on the shared mem from user space.
Is there any idea how I can use a mechanism such select to sleep and to get notify on shared memory change (buy the driver) ?
I dont want to implement some communication like netlink, because the idea with select is to sleep, to wake up if something happens, and to keep awake and keep processing data without handing more IPC with the kernel.
And then, when it will be done, the application can call again select and wait again.
Thank you.
You are looking for the kqueue(2) interface on FreeBSD.
On Linux there is inotify/epoll.

How do I inform a user space application that the driver has received an interrupt in linux?

I have a PCIe device that will send a hardware interrupt when a data buffer is ready to be read. I believe the best approach for this is to use signals but I'm not entirely sure how. What I believe I need to do is:
Save the PID of the user space application so the driver knows where to send the signal
In the interrupt handler of the PCIe device driver, send a signal to the user space application
In the User space application implement a signal handler function for processing the signal
I'm not sure how to do either of these.
How/Where do I save the PID of the user space application?
How do I send a signal to that specific PID from the driver's interrupt handler (I believe I should use the kill command but I'm more interested in the syntax of getting the PID)?
How do I have the user space application wait for the signal after saving its PID?
Is it possible to have the user space application continue to run after saving its PID, run the signal handler function when a signal is received, and continue running where it was before the signal arrived? (similar to how an ISR works)
Don't use signals for this. Implement a character device. The userspace application will open it, then call read and will be blocked until your driver determines there is data available.
See Linux Device Drivers chapter 3, I think.
Have to handle interrupt directly in kernel. In order to pass PID to kernel, have to use the device file abstraction (e.g. ioctl() calls), but it provides asynchronous notification through read/select as well so signal solution is replaced.
This is an old question, but to manage IRQ from application userspace, the better way now is to use UIO driver and poll/select call on /dev/uioX.

application hang without any message when accessing an invalid physical address

I'm trying to read a physical address using mmap in a application. Due to some reason, that physical address has some hardware fault and the ack on the bus will never come back when trying to read it.
When read this address, we found that the application hangs immediately without any message output, but the application can be cancelled or suspended, which means the OS is still alive without being impacted any.
1).I'm just curious what the application is doing and how the hang could happen?
my understand is that the CPU should have timeout detection when the ack not coming back at the specified time slot, the application should not stop at the read instruction and there should be some exception being triggered to inform the kernel.
2).We are doing a lot of hardware testing and so we want the application or the kernel output something when the hang happens. Is there a way of adding something to do this?
thanks a lot in advance!

Using user-space processes to assist kernel modules

I'm working on one piece of a very high performance piece of hardware that works under Linux. We'd like to cache some data but we're worried about memory consumption - so the idea is to create a user process to manage the cache. That way, the cache can be in virtual memory, not in kernel space, et cetera.
The question is: what's the best way to do this? My first instinct is to have the kernel module create a character device file, and have a user program that opens that file, then sits on a select statement waiting for commands to arrive on it. But I'm concerned that this might not be optimal. A friend mentioned he knew of a socket-based interface, but when pressed he couldn't provide any details....
Any suggestions?
I think you're looking for the netlink interface. See Why and How to Use Netlink Socket [sic] for more information. Be careful of security issues when talking between the kernel and user space; there was a recent vulnerability when udev neglected to check that messages were coming from the kernel rather than user space.

Resources