@@ -108,16 +108,19 @@ void pipe_double_lock(struct pipe_inode_info *pipe1,
108
108
/* Drop the inode semaphore and wait for a pipe event, atomically */
109
109
void pipe_wait (struct pipe_inode_info * pipe )
110
110
{
111
- DEFINE_WAIT (wait );
111
+ DEFINE_WAIT (rdwait );
112
+ DEFINE_WAIT (wrwait );
112
113
113
114
/*
114
115
* Pipes are system-local resources, so sleeping on them
115
116
* is considered a noninteractive wait:
116
117
*/
117
- prepare_to_wait (& pipe -> wait , & wait , TASK_INTERRUPTIBLE );
118
+ prepare_to_wait (& pipe -> rd_wait , & rdwait , TASK_INTERRUPTIBLE );
119
+ prepare_to_wait (& pipe -> wr_wait , & wrwait , TASK_INTERRUPTIBLE );
118
120
pipe_unlock (pipe );
119
121
schedule ();
120
- finish_wait (& pipe -> wait , & wait );
122
+ finish_wait (& pipe -> rd_wait , & rdwait );
123
+ finish_wait (& pipe -> wr_wait , & wrwait );
121
124
pipe_lock (pipe );
122
125
}
123
126
@@ -286,7 +289,7 @@ pipe_read(struct kiocb *iocb, struct iov_iter *to)
286
289
size_t total_len = iov_iter_count (to );
287
290
struct file * filp = iocb -> ki_filp ;
288
291
struct pipe_inode_info * pipe = filp -> private_data ;
289
- bool was_full ;
292
+ bool was_full , wake_next_reader = false ;
290
293
ssize_t ret ;
291
294
292
295
/* Null read succeeds. */
@@ -344,10 +347,10 @@ pipe_read(struct kiocb *iocb, struct iov_iter *to)
344
347
345
348
if (!buf -> len ) {
346
349
pipe_buf_release (pipe , buf );
347
- spin_lock_irq (& pipe -> wait .lock );
350
+ spin_lock_irq (& pipe -> rd_wait .lock );
348
351
tail ++ ;
349
352
pipe -> tail = tail ;
350
- spin_unlock_irq (& pipe -> wait .lock );
353
+ spin_unlock_irq (& pipe -> rd_wait .lock );
351
354
}
352
355
total_len -= chars ;
353
356
if (!total_len )
@@ -384,7 +387,7 @@ pipe_read(struct kiocb *iocb, struct iov_iter *to)
384
387
* no data.
385
388
*/
386
389
if (unlikely (was_full )) {
387
- wake_up_interruptible_sync_poll (& pipe -> wait , EPOLLOUT | EPOLLWRNORM );
390
+ wake_up_interruptible_sync_poll (& pipe -> wr_wait , EPOLLOUT | EPOLLWRNORM );
388
391
kill_fasync (& pipe -> fasync_writers , SIGIO , POLL_OUT );
389
392
}
390
393
@@ -394,18 +397,23 @@ pipe_read(struct kiocb *iocb, struct iov_iter *to)
394
397
* since we've done any required wakeups and there's no need
395
398
* to mark anything accessed. And we've dropped the lock.
396
399
*/
397
- if (wait_event_interruptible (pipe -> wait , pipe_readable (pipe )) < 0 )
400
+ if (wait_event_interruptible_exclusive (pipe -> rd_wait , pipe_readable (pipe )) < 0 )
398
401
return - ERESTARTSYS ;
399
402
400
403
__pipe_lock (pipe );
401
404
was_full = pipe_full (pipe -> head , pipe -> tail , pipe -> max_usage );
405
+ wake_next_reader = true;
402
406
}
407
+ if (pipe_empty (pipe -> head , pipe -> tail ))
408
+ wake_next_reader = false;
403
409
__pipe_unlock (pipe );
404
410
405
411
if (was_full ) {
406
- wake_up_interruptible_sync_poll (& pipe -> wait , EPOLLOUT | EPOLLWRNORM );
412
+ wake_up_interruptible_sync_poll (& pipe -> wr_wait , EPOLLOUT | EPOLLWRNORM );
407
413
kill_fasync (& pipe -> fasync_writers , SIGIO , POLL_OUT );
408
414
}
415
+ if (wake_next_reader )
416
+ wake_up_interruptible_sync_poll (& pipe -> rd_wait , EPOLLIN | EPOLLRDNORM );
409
417
if (ret > 0 )
410
418
file_accessed (filp );
411
419
return ret ;
@@ -437,6 +445,7 @@ pipe_write(struct kiocb *iocb, struct iov_iter *from)
437
445
size_t total_len = iov_iter_count (from );
438
446
ssize_t chars ;
439
447
bool was_empty = false;
448
+ bool wake_next_writer = false;
440
449
441
450
/* Null write succeeds. */
442
451
if (unlikely (total_len == 0 ))
@@ -515,16 +524,16 @@ pipe_write(struct kiocb *iocb, struct iov_iter *from)
515
524
* it, either the reader will consume it or it'll still
516
525
* be there for the next write.
517
526
*/
518
- spin_lock_irq (& pipe -> wait .lock );
527
+ spin_lock_irq (& pipe -> rd_wait .lock );
519
528
520
529
head = pipe -> head ;
521
530
if (pipe_full (head , pipe -> tail , pipe -> max_usage )) {
522
- spin_unlock_irq (& pipe -> wait .lock );
531
+ spin_unlock_irq (& pipe -> rd_wait .lock );
523
532
continue ;
524
533
}
525
534
526
535
pipe -> head = head + 1 ;
527
- spin_unlock_irq (& pipe -> wait .lock );
536
+ spin_unlock_irq (& pipe -> rd_wait .lock );
528
537
529
538
/* Insert it into the buffer array */
530
539
buf = & pipe -> bufs [head & mask ];
@@ -576,14 +585,17 @@ pipe_write(struct kiocb *iocb, struct iov_iter *from)
576
585
*/
577
586
__pipe_unlock (pipe );
578
587
if (was_empty ) {
579
- wake_up_interruptible_sync_poll (& pipe -> wait , EPOLLIN | EPOLLRDNORM );
588
+ wake_up_interruptible_sync_poll (& pipe -> rd_wait , EPOLLIN | EPOLLRDNORM );
580
589
kill_fasync (& pipe -> fasync_readers , SIGIO , POLL_IN );
581
590
}
582
- wait_event_interruptible (pipe -> wait , pipe_writable (pipe ));
591
+ wait_event_interruptible_exclusive (pipe -> wr_wait , pipe_writable (pipe ));
583
592
__pipe_lock (pipe );
584
593
was_empty = pipe_empty (pipe -> head , pipe -> tail );
594
+ wake_next_writer = true;
585
595
}
586
596
out :
597
+ if (pipe_full (pipe -> head , pipe -> tail , pipe -> max_usage ))
598
+ wake_next_writer = false;
587
599
__pipe_unlock (pipe );
588
600
589
601
/*
@@ -596,9 +608,11 @@ pipe_write(struct kiocb *iocb, struct iov_iter *from)
596
608
* wake up pending jobs
597
609
*/
598
610
if (was_empty ) {
599
- wake_up_interruptible_sync_poll (& pipe -> wait , EPOLLIN | EPOLLRDNORM );
611
+ wake_up_interruptible_sync_poll (& pipe -> rd_wait , EPOLLIN | EPOLLRDNORM );
600
612
kill_fasync (& pipe -> fasync_readers , SIGIO , POLL_IN );
601
613
}
614
+ if (wake_next_writer )
615
+ wake_up_interruptible_sync_poll (& pipe -> wr_wait , EPOLLOUT | EPOLLWRNORM );
602
616
if (ret > 0 && sb_start_write_trylock (file_inode (filp )-> i_sb )) {
603
617
int err = file_update_time (filp );
604
618
if (err )
@@ -642,12 +656,15 @@ pipe_poll(struct file *filp, poll_table *wait)
642
656
unsigned int head , tail ;
643
657
644
658
/*
645
- * Reading only -- no need for acquiring the semaphore.
659
+ * Reading pipe state only -- no need for acquiring the semaphore.
646
660
*
647
661
* But because this is racy, the code has to add the
648
662
* entry to the poll table _first_ ..
649
663
*/
650
- poll_wait (filp , & pipe -> wait , wait );
664
+ if (filp -> f_mode & FMODE_READ )
665
+ poll_wait (filp , & pipe -> rd_wait , wait );
666
+ if (filp -> f_mode & FMODE_WRITE )
667
+ poll_wait (filp , & pipe -> wr_wait , wait );
651
668
652
669
/*
653
670
* .. and only then can you do the racy tests. That way,
@@ -706,7 +723,8 @@ pipe_release(struct inode *inode, struct file *file)
706
723
pipe -> writers -- ;
707
724
708
725
if (pipe -> readers || pipe -> writers ) {
709
- wake_up_interruptible_sync_poll (& pipe -> wait , EPOLLIN | EPOLLOUT | EPOLLRDNORM | EPOLLWRNORM | EPOLLERR | EPOLLHUP );
726
+ wake_up_interruptible_sync_poll (& pipe -> rd_wait , EPOLLIN | EPOLLRDNORM | EPOLLERR | EPOLLHUP );
727
+ wake_up_interruptible_sync_poll (& pipe -> wr_wait , EPOLLOUT | EPOLLWRNORM | EPOLLERR | EPOLLHUP );
710
728
kill_fasync (& pipe -> fasync_readers , SIGIO , POLL_IN );
711
729
kill_fasync (& pipe -> fasync_writers , SIGIO , POLL_OUT );
712
730
}
@@ -789,7 +807,8 @@ struct pipe_inode_info *alloc_pipe_info(void)
789
807
GFP_KERNEL_ACCOUNT );
790
808
791
809
if (pipe -> bufs ) {
792
- init_waitqueue_head (& pipe -> wait );
810
+ init_waitqueue_head (& pipe -> rd_wait );
811
+ init_waitqueue_head (& pipe -> wr_wait );
793
812
pipe -> r_counter = pipe -> w_counter = 1 ;
794
813
pipe -> max_usage = pipe_bufs ;
795
814
pipe -> ring_size = pipe_bufs ;
@@ -1007,7 +1026,8 @@ static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
1007
1026
1008
1027
static void wake_up_partner (struct pipe_inode_info * pipe )
1009
1028
{
1010
- wake_up_interruptible (& pipe -> wait );
1029
+ wake_up_interruptible (& pipe -> rd_wait );
1030
+ wake_up_interruptible (& pipe -> wr_wait );
1011
1031
}
1012
1032
1013
1033
static int fifo_open (struct inode * inode , struct file * filp )
@@ -1118,13 +1138,13 @@ static int fifo_open(struct inode *inode, struct file *filp)
1118
1138
1119
1139
err_rd :
1120
1140
if (!-- pipe -> readers )
1121
- wake_up_interruptible (& pipe -> wait );
1141
+ wake_up_interruptible (& pipe -> wr_wait );
1122
1142
ret = - ERESTARTSYS ;
1123
1143
goto err ;
1124
1144
1125
1145
err_wr :
1126
1146
if (!-- pipe -> writers )
1127
- wake_up_interruptible (& pipe -> wait );
1147
+ wake_up_interruptible (& pipe -> rd_wait );
1128
1148
ret = - ERESTARTSYS ;
1129
1149
goto err ;
1130
1150
@@ -1251,7 +1271,8 @@ static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long arg)
1251
1271
pipe -> max_usage = nr_slots ;
1252
1272
pipe -> tail = tail ;
1253
1273
pipe -> head = head ;
1254
- wake_up_interruptible_all (& pipe -> wait );
1274
+ wake_up_interruptible_all (& pipe -> rd_wait );
1275
+ wake_up_interruptible_all (& pipe -> wr_wait );
1255
1276
return pipe -> max_usage * PAGE_SIZE ;
1256
1277
1257
1278
out_revert_acct :
0 commit comments