From: Martin Christian <martin.christian@secunet.com>
To: <kernelnewbies@kernelnewbies.org>
Subject: Unexpected scheduling with mutexes
Date: Wed, 27 Mar 2019 11:56:51 +0100 [thread overview]
Message-ID: <e15449fb-c89d-bc81-8b43-49da235cfe06@secunet.com> (raw)
[-- Attachment #1.1.1: Type: text/plain, Size: 2047 bytes --]
Hi,
I've written a linux kernel module for an USB device. The USB driver
provides 2 read-only character devices, which can be opened only
exclusively by one process:
- `/dev/cdev_a`
- `/dev/cdev_b`
The USB device can only handle one request at a time.
The test setup is a follows:
- Processes A reads data from 1st device: `dd if=/dev/cdev_a of=/tmp/a
bs=X`
- Processes B reads data from 2nd device: `dd if=/dev/cdev_b of=/tmp/b
bs=X`
- Process A and B run in parallel
- After 10 seconds both processes are killed and size of both output
files is compared.
For certain values of `X` there is a significant difference in size
between the two files, which I don't expect.
A read call to the driver does the following:
1. `mutex_lock_interruptible(iolock)`
2. `usb_bulk_msg(dev, pipe, buf, X, timeout)`
3. `mutex_unlock(iolock)`
4. `copy_to_user(buf)`
What I would expect is the following:
1. Proc A: `mutex_lock_interruptible(iolock)`
2. Proc A: `usb_bulk_msg(dev, pipe, buf, X, timeout)`
3. Scheduling: A -> B
4. Proc B: `mutex_lock_interruptible(iolock)` -> blocks
5. Scheduling: B -> A
6. Proc A: `mutex_unlock(iolock)`
7. Proc A: `copy_to_user(buf)`
8. Proc A: `mutex_lock_interruptible(iolock)` -> blocks
9. Scheduling: A -> B
10. Proc B: `usb_bulk_msg(dev, pipe, buf, X, timeout)`
But what I see with ftrace is that in step 8, process A still continues.
And it seems that for certain values of X the time inside the critical
region is a multiple of the time slice, so that process B always gets
the time slice when the critical region is blocked. What would be a best
practise solution for this? I was thinking of calling `schedule()` each
time after copying to user space or playing with nice values or using
wait_queues?
--
Dipl.-Inf. Martin Christian
Senior Berater Entwicklung Hardware
secunet Security Networks AG
Tel.: +49 201 5454-3612, Fax +49 201 5454-1323
E-Mail: martin.christian@secunet.com
Ammonstraße 74, 01067 Dresden
www.secunet.com
[-- Attachment #1.2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
[-- Attachment #2: Type: text/plain, Size: 170 bytes --]
_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
next reply other threads:[~2019-03-27 10:57 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-03-27 10:56 Martin Christian [this message]
2019-03-29 20:01 ` Unexpected scheduling with mutexes Greg KH
2019-03-29 21:45 ` Valdis Klētnieks
2019-03-30 12:25 ` Ruben Safir
2019-03-30 18:35 ` Greg KH
2019-04-03 9:33 ` Martin Christian
2019-04-03 9:48 ` Greg KH
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=e15449fb-c89d-bc81-8b43-49da235cfe06@secunet.com \
--to=martin.christian@secunet.com \
--cc=kernelnewbies@kernelnewbies.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).