Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write is sometimes called from the System process #6

Open
GoogleCodeExporter opened this issue Mar 23, 2015 · 3 comments
Open

Write is sometimes called from the System process #6

GoogleCodeExporter opened this issue Mar 23, 2015 · 3 comments

Comments

@GoogleCodeExporter
Copy link

The driver's write function must be called within the context of the writing 
process, because only then we're able to collect information about the writer. 
However, write is sometimes called from a system worker thread (PID 4).

We need to find out why this happens and how to avoid it. Alternatively, we 
might be able to find the writing process even in that situation.

Original issue reported on code.google.com by sebastian.poeplau on 29 Jun 2012 at 9:11

@GoogleCodeExporter
Copy link
Author

If a process keeps an open handle to the device and waits for some device 
control operation to complete, write calls from the system pseudo-process seem 
to occur more often.

There is no guarantee that our code is called in the originating process's 
context, so we should find a way to find the originator even if we're called in 
system context.

Original comment by sebastian.poeplau on 7 Aug 2012 at 9:15

@GoogleCodeExporter
Copy link
Author

We might be able to intercept a *create* from the original process and later 
correlate it to the write attempt.

Original comment by sebastian.poeplau on 29 Oct 2012 at 10:50

@sammyifelse
Copy link

Overview
Write is sometimes called from the System process [#6]

Approach
To ensure that the driver's write function is called within a writing process context and not from a system worker thread (PID 4), we can use the current pointer, which is a pointer to the current process's task_struct. This pointer is only valid in process context and not in interrupt context.

Here's an example of how you can use the current pointer to check if the driver's write function is being called from a process context or not:

if (in_interrupt()) {
    /* Handle interrupt context */
} else if (current) {
    /* Handle process context */
    struct file *file = current->files->fdt[0];
    if (file && file->f_op && file->f_op->write) {
        /* Call the driver's write function */
        file->f_op->write(file, buffer, count, &pos);
    }
} else {
    /* Handle other contexts, such as system worker threads */
}

In this example, the in_interrupt() function is used to check if the driver's write function is being called from an interrupt context. If it is, then the interrupt context code is executed. If it's not, then the current pointer is checked to see if it's valid. If it is, then the driver's write function is called using the file->f_op->write() function. If the current pointer is not valid, then the code for other contexts, such as system worker threads, is executed.

By using the current pointer in this way, we can ensure that the driver's write function is called within a writing process context and not from a system worker thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants