implementation of O_NONBLOCK for chaoskey
Subsystems (2)
This should implement O_NONBLOCK for chaoskey devices, but I need a tester.
PATCH 1/4 chaoskey: O_NONBLOCK in concurrent reads
This changes the locking in chaoskey_read() to correctly handle O_NONBLOCK in the case of concurrent readers.
--- drivers/usb/misc/chaoskey.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/usb/misc/chaoskey.c +13 -3
@@ -435,9 +435,19 @@ static ssize_t chaoskey_read(struct file *file, goto bail;mutex_unlock (&dev->rng_lock); - result =mutex_lock_interruptible (&dev->lock); -if (result) - goto bail; +if (file->f_flags &O_NONBLOCK ) { + result =mutex_trylock (&dev->lock); +if (result ==0 ) { + result = -EAGAIN ; + goto bail; + }else { + result =0 ; + } + }else { + result =mutex_lock_interruptible (&dev->lock); +if (result) + goto bail; + }if (dev->valid == dev->used) { result =_chaoskey_fill (dev);if (result <0 ) {
Signature · 1 line
2.16.4
PATCH 2/4 chaoskey: introduce asynchronous reads
In reply to Oliver NeukumThis divides requesting IO and waiting for IO from each other.
--- drivers/usb/misc/chaoskey.c | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-)
drivers/usb/misc/chaoskey.c +31 -8
@@ -336,15 +336,13 @@ static void chaos_read_callback(struct urb *urb)wake_up (&dev->wait_q); } -/* Fill the buffer. Called with dev->lock held -*/ -static int _chaoskey_fill (struct chaoskey *dev) +static int chaoskey_request_fill (struct chaoskey *dev) {DEFINE_WAIT (wait);int result;bool started; -usb_dbg (dev->interface,"fill" ); +usb_dbg (dev->interface,"request fill" );/* Return immediately if someone called before the buffer was * empty */
@@ -382,10 +380,26 @@ static int _chaoskey_fill(struct chaoskey *dev)*/ started = dev->reads_started; dev->reads_started = true; +/* +* powering down while a read is under way +* is blocked in suspend() +*/ +usb_autopm_put_interface (dev->interface); +return 0 ; +out: +usb_autopm_put_interface (dev->interface); +return result; +} + +static int chaoskey_wait_fill (struct chaoskey *dev) +{ +DEFINE_WAIT (wait); +int result; + result =wait_event_interruptible_timeout ( dev->wait_q, !dev->reading, - (started ?NAK_TIMEOUT :ALEA_FIRST_TIMEOUT ) ); + (dev->reads_started ?NAK_TIMEOUT :ALEA_FIRST_TIMEOUT ) );if (result <0 ) {usb_kill_urb (dev->urb);
@@ -400,7 +414,6 @@ static int _chaoskey_fill(struct chaoskey *dev) } out:/* Let the device go back to sleep eventually */ -usb_autopm_put_interface (dev->interface);usb_dbg (dev->interface,"read %d bytes" , dev->valid);
@@ -449,7 +462,12 @@ static ssize_t chaoskey_read(struct file *file, goto bail; }if (dev->valid == dev->used) { - result =_chaoskey_fill (dev); + result =chaoskey_request_fill (dev); +if (result <0 ) { +mutex_unlock (&dev->lock); + goto bail; + } + result =chaoskey_wait_fill (dev);if (result <0 ) {mutex_unlock (&dev->lock); goto bail;
@@ -517,7 +535,7 @@ static int chaoskey_rng_read(struct hwrng *rng, void *data,* the buffer will still be empty */ if (dev->valid == dev->used) - (void )_chaoskey_fill (dev); + (void )chaoskey_request_fill (dev); this_time = dev->valid - dev->used;if (this_time > max)
@@ -537,6 +555,11 @@ static int chaoskey_rng_read(struct hwrng *rng, void *data,static int chaoskey_suspend (struct usb_interface *interface,pm_message_t message) { +struct chaoskey *dev =usb_get_intfdata (interface); + +if (dev->reading &&PMSG_IS_AUTO (message)) +return -EBUSY ; +usb_dbg (interface,"suspend" );return 0 ; }
Signature · 1 line
2.16.4
kernel test robot
build botRe: [RFT 2/4] chaoskey: introduce asynchronous reads
I love your patch! Perhaps something to improve:
Hi Oliver,
I love your patch! Perhaps something to improve:
[auto build test WARNING on usb/usb-testing]
[also build test WARNING on v5.9-rc6 next-20200921]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Oliver-Neukum/chaoskey-O_NONBLOCK-in-concurrent-reads/20200921-203804 base: https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb.git usb-testing config: sh-allmodconfig (attached as .config) compiler: sh4-linux-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=sh
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
drivers/usb/misc/chaoskey.c: In function 'chaoskey_request_fill':
>> drivers/usb/misc/chaoskey.c:340:7: warning: variable 'started' set but not used [-Wunused-but-set-variable]
340 | bool started;
| ^~~~~~~# https://github.com/0day-ci/linux/commit/4393931662d77cd7c609d4cfd82ad59900c9b13b
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Oliver-Neukum/chaoskey-O_NONBLOCK-in-concurrent-reads/20200921-203804 git checkout 4393931662d77cd7c609d4cfd82ad59900c9b13b
vim +/started +340 drivers/usb/misc/chaoskey.c
0ca10122ca08d2 Oliver Neukum 2016-02-17 335
4393931662d77c Oliver Neukum 2020-09-21 336 static int chaoskey_request_fill(struct chaoskey *dev)
66e3e591891da9 Keith Packard 2015-03-19 337 {
66e3e591891da9 Keith Packard 2015-03-19 338 DEFINE_WAIT(wait);
66e3e591891da9 Keith Packard 2015-03-19 339 int result;
e4a886e811cd07 Bob Ham 2016-06-03 @340 bool started;
66e3e591891da9 Keith Packard 2015-03-19 341
4393931662d77c Oliver Neukum 2020-09-21 342 usb_dbg(dev->interface, "request fill");
66e3e591891da9 Keith Packard 2015-03-19 343
66e3e591891da9 Keith Packard 2015-03-19 344 /* Return immediately if someone called before the buffer was
66e3e591891da9 Keith Packard 2015-03-19 345 * empty */
66e3e591891da9 Keith Packard 2015-03-19 346 if (dev->valid != dev->used) {
66e3e591891da9 Keith Packard 2015-03-19 347 usb_dbg(dev->interface, "not empty yet (valid %d used %d)",
66e3e591891da9 Keith Packard 2015-03-19 348 dev->valid, dev->used);
66e3e591891da9 Keith Packard 2015-03-19 349 return 0;
66e3e591891da9 Keith Packard 2015-03-19 350 }
66e3e591891da9 Keith Packard 2015-03-19 351
66e3e591891da9 Keith Packard 2015-03-19 352 /* Bail if the device has been removed */
66e3e591891da9 Keith Packard 2015-03-19 353 if (!dev->present) {
66e3e591891da9 Keith Packard 2015-03-19 354 usb_dbg(dev->interface, "device not present");
66e3e591891da9 Keith Packard 2015-03-19 355 return -ENODEV;
66e3e591891da9 Keith Packard 2015-03-19 356 }
66e3e591891da9 Keith Packard 2015-03-19 357
66e3e591891da9 Keith Packard 2015-03-19 358 /* Make sure the device is awake */
66e3e591891da9 Keith Packard 2015-03-19 359 result = usb_autopm_get_interface(dev->interface);
66e3e591891da9 Keith Packard 2015-03-19 360 if (result) {
66e3e591891da9 Keith Packard 2015-03-19 361 usb_dbg(dev->interface, "wakeup failed (result %d)", result);
66e3e591891da9 Keith Packard 2015-03-19 362 return result;
66e3e591891da9 Keith Packard 2015-03-19 363 }
66e3e591891da9 Keith Packard 2015-03-19 364
0ca10122ca08d2 Oliver Neukum 2016-02-17 365 dev->reading = true;
0ca10122ca08d2 Oliver Neukum 2016-02-17 366 result = usb_submit_urb(dev->urb, GFP_KERNEL);
0ca10122ca08d2 Oliver Neukum 2016-02-17 367 if (result < 0) {
0ca10122ca08d2 Oliver Neukum 2016-02-17 368 result = usb_translate_errors(result);
0ca10122ca08d2 Oliver Neukum 2016-02-17 369 dev->reading = false;
0ca10122ca08d2 Oliver Neukum 2016-02-17 370 goto out;
0ca10122ca08d2 Oliver Neukum 2016-02-17 371 }
0ca10122ca08d2 Oliver Neukum 2016-02-17 372
e4a886e811cd07 Bob Ham 2016-06-03 373 /* The first read on the Alea takes a little under 2 seconds.
e4a886e811cd07 Bob Ham 2016-06-03 374 * Reads after the first read take only a few microseconds
e4a886e811cd07 Bob Ham 2016-06-03 375 * though. Presumably the entropy-generating circuit needs
e4a886e811cd07 Bob Ham 2016-06-03 376 * time to ramp up. So, we wait longer on the first read.
e4a886e811cd07 Bob Ham 2016-06-03 377 */
e4a886e811cd07 Bob Ham 2016-06-03 378 started = dev->reads_started;
e4a886e811cd07 Bob Ham 2016-06-03 379 dev->reads_started = true;
4393931662d77c Oliver Neukum 2020-09-21 380 /*
4393931662d77c Oliver Neukum 2020-09-21 381 * powering down while a read is under way
4393931662d77c Oliver Neukum 2020-09-21 382 * is blocked in suspend()
4393931662d77c Oliver Neukum 2020-09-21 383 */
4393931662d77c Oliver Neukum 2020-09-21 384 usb_autopm_put_interface(dev->interface);
4393931662d77c Oliver Neukum 2020-09-21 385 return 0;
4393931662d77c Oliver Neukum 2020-09-21 386 out:
4393931662d77c Oliver Neukum 2020-09-21 387 usb_autopm_put_interface(dev->interface);
4393931662d77c Oliver Neukum 2020-09-21 388 return result;
4393931662d77c Oliver Neukum 2020-09-21 389 }
4393931662d77c Oliver Neukum 2020-09-21 390 ---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
PATCH 3/4 chaoskey: make read() obey O_NONBLOCK
In reply to Oliver NeukumThis skips waiting for a read if O_NONBLOCK is set.
--- drivers/usb/misc/chaoskey.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/usb/misc/chaoskey.c +8 -4
@@ -467,10 +467,14 @@ static ssize_t chaoskey_read(struct file *file,mutex_unlock (&dev->lock); goto bail; } - result =chaoskey_wait_fill (dev); -if (result <0 ) { -mutex_unlock (&dev->lock); - goto bail; +if (!(file->f_flags &O_NONBLOCK )) { + result =chaoskey_wait_fill (dev); +if (result <0 ) { +mutex_unlock (&dev->lock); + goto bail; + } + }else { + result = -EAGAIN ; } }
Signature · 1 line
2.16.4
PATCH 4/4 chaoskey: request data asynchronously
In reply to Oliver NeukumThis requests more data if a read has exhausted the buffer just to have it ready sooner.
--- drivers/usb/misc/chaoskey.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/usb/misc/chaoskey.c +7 -1
@@ -358,6 +358,9 @@ static int chaoskey_request_fill(struct chaoskey *dev)return -ENODEV ; } +if (dev->reading) +return -EBUSY ; +/* Make sure the device is awake */ result =usb_autopm_get_interface (dev->interface);if (result) {
@@ -500,13 +503,16 @@ static ssize_t chaoskey_read(struct file *file, dev->used += this_time;mutex_unlock (&dev->lock); } +/* request data on suspicion that it will eventually be used */ +if (dev->valid == dev->used) + (void )chaoskey_request_fill (dev); bail:if (read_count) {usb_dbg (dev->interface,"read %zu bytes" , read_count);return read_count; }usb_dbg (dev->interface,"empty read, result %d" , result); -if (result == -ETIMEDOUT ) +if (result == -ETIMEDOUT || result == -EBUSY ) result = -EAGAIN ;return result; }
Signature · 1 line
2.16.4
Greg KH
maintainerQuoted 3 lines · Oliver Neukum
This should implement
O_NONBLOCKfor chaoskey devices, but I need a tester.
I have one of these somewhere, what type of testing is needed?
thanks,
greg k-h
Am Freitag, den 02.10.2020, 11:22 +0200 schrieb Greg KH:
Quoted 6 lines · Oliver Neukum
Quoted 3 lines
This should implement
O_NONBLOCKfor chaoskey devices, but I need a tester.I have one of these somewhere, what type of testing is needed?
Hi,
can you open the device with O_NONBLOCK and read from it? And did I butcher the normal case?
Regards
Oliver