@@ -112,20 +112,40 @@ void pipe_double_lock(struct pipe_inode_info *pipe1,
112
112
pipe_lock (pipe2 );
113
113
}
114
114
115
+ static struct page * anon_pipe_get_page (struct pipe_inode_info * pipe )
116
+ {
117
+ for (int i = 0 ; i < ARRAY_SIZE (pipe -> tmp_page ); i ++ ) {
118
+ if (pipe -> tmp_page [i ]) {
119
+ struct page * page = pipe -> tmp_page [i ];
120
+ pipe -> tmp_page [i ] = NULL ;
121
+ return page ;
122
+ }
123
+ }
124
+
125
+ return alloc_page (GFP_HIGHUSER | __GFP_ACCOUNT );
126
+ }
127
+
128
+ static void anon_pipe_put_page (struct pipe_inode_info * pipe ,
129
+ struct page * page )
130
+ {
131
+ if (page_count (page ) == 1 ) {
132
+ for (int i = 0 ; i < ARRAY_SIZE (pipe -> tmp_page ); i ++ ) {
133
+ if (!pipe -> tmp_page [i ]) {
134
+ pipe -> tmp_page [i ] = page ;
135
+ return ;
136
+ }
137
+ }
138
+ }
139
+
140
+ put_page (page );
141
+ }
142
+
115
143
static void anon_pipe_buf_release (struct pipe_inode_info * pipe ,
116
144
struct pipe_buffer * buf )
117
145
{
118
146
struct page * page = buf -> page ;
119
147
120
- /*
121
- * If nobody else uses this page, and we don't already have a
122
- * temporary page, let's keep track of it as a one-deep
123
- * allocation cache. (Otherwise just release our reference to it)
124
- */
125
- if (page_count (page ) == 1 && !pipe -> tmp_page )
126
- pipe -> tmp_page = page ;
127
- else
128
- put_page (page );
148
+ anon_pipe_put_page (pipe , page );
129
149
}
130
150
131
151
static bool anon_pipe_buf_try_steal (struct pipe_inode_info * pipe ,
@@ -247,7 +267,7 @@ static inline unsigned int pipe_update_tail(struct pipe_inode_info *pipe,
247
267
}
248
268
249
269
static ssize_t
250
- pipe_read (struct kiocb * iocb , struct iov_iter * to )
270
+ anon_pipe_read (struct kiocb * iocb , struct iov_iter * to )
251
271
{
252
272
size_t total_len = iov_iter_count (to );
253
273
struct file * filp = iocb -> ki_filp ;
@@ -274,7 +294,6 @@ pipe_read(struct kiocb *iocb, struct iov_iter *to)
274
294
/* Read ->head with a barrier vs post_one_notification() */
275
295
unsigned int head = smp_load_acquire (& pipe -> head );
276
296
unsigned int tail = pipe -> tail ;
277
- unsigned int mask = pipe -> ring_size - 1 ;
278
297
279
298
#ifdef CONFIG_WATCH_QUEUE
280
299
if (pipe -> note_loss ) {
@@ -301,7 +320,7 @@ pipe_read(struct kiocb *iocb, struct iov_iter *to)
301
320
#endif
302
321
303
322
if (!pipe_empty (head , tail )) {
304
- struct pipe_buffer * buf = & pipe -> bufs [ tail & mask ] ;
323
+ struct pipe_buffer * buf = pipe_buf ( pipe , tail ) ;
305
324
size_t chars = buf -> len ;
306
325
size_t written ;
307
326
int error ;
@@ -359,29 +378,9 @@ pipe_read(struct kiocb *iocb, struct iov_iter *to)
359
378
break ;
360
379
}
361
380
mutex_unlock (& pipe -> mutex );
362
-
363
381
/*
364
382
* We only get here if we didn't actually read anything.
365
383
*
366
- * However, we could have seen (and removed) a zero-sized
367
- * pipe buffer, and might have made space in the buffers
368
- * that way.
369
- *
370
- * You can't make zero-sized pipe buffers by doing an empty
371
- * write (not even in packet mode), but they can happen if
372
- * the writer gets an EFAULT when trying to fill a buffer
373
- * that already got allocated and inserted in the buffer
374
- * array.
375
- *
376
- * So we still need to wake up any pending writers in the
377
- * _very_ unlikely case that the pipe was full, but we got
378
- * no data.
379
- */
380
- if (unlikely (wake_writer ))
381
- wake_up_interruptible_sync_poll (& pipe -> wr_wait , EPOLLOUT | EPOLLWRNORM );
382
- kill_fasync (& pipe -> fasync_writers , SIGIO , POLL_OUT );
383
-
384
- /*
385
384
* But because we didn't read anything, at this point we can
386
385
* just return directly with -ERESTARTSYS if we're interrupted,
387
386
* since we've done any required wakeups and there's no need
@@ -390,7 +389,6 @@ pipe_read(struct kiocb *iocb, struct iov_iter *to)
390
389
if (wait_event_interruptible_exclusive (pipe -> rd_wait , pipe_readable (pipe )) < 0 )
391
390
return - ERESTARTSYS ;
392
391
393
- wake_writer = false;
394
392
wake_next_reader = true;
395
393
mutex_lock (& pipe -> mutex );
396
394
}
@@ -403,8 +401,15 @@ pipe_read(struct kiocb *iocb, struct iov_iter *to)
403
401
if (wake_next_reader )
404
402
wake_up_interruptible_sync_poll (& pipe -> rd_wait , EPOLLIN | EPOLLRDNORM );
405
403
kill_fasync (& pipe -> fasync_writers , SIGIO , POLL_OUT );
404
+ return ret ;
405
+ }
406
+
407
+ static ssize_t
408
+ fifo_pipe_read (struct kiocb * iocb , struct iov_iter * to )
409
+ {
410
+ int ret = anon_pipe_read (iocb , to );
406
411
if (ret > 0 )
407
- file_accessed (filp );
412
+ file_accessed (iocb -> ki_filp );
408
413
return ret ;
409
414
}
410
415
@@ -424,7 +429,7 @@ static inline bool pipe_writable(const struct pipe_inode_info *pipe)
424
429
}
425
430
426
431
static ssize_t
427
- pipe_write (struct kiocb * iocb , struct iov_iter * from )
432
+ anon_pipe_write (struct kiocb * iocb , struct iov_iter * from )
428
433
{
429
434
struct file * filp = iocb -> ki_filp ;
430
435
struct pipe_inode_info * pipe = filp -> private_data ;
@@ -471,8 +476,7 @@ pipe_write(struct kiocb *iocb, struct iov_iter *from)
471
476
was_empty = pipe_empty (head , pipe -> tail );
472
477
chars = total_len & (PAGE_SIZE - 1 );
473
478
if (chars && !was_empty ) {
474
- unsigned int mask = pipe -> ring_size - 1 ;
475
- struct pipe_buffer * buf = & pipe -> bufs [(head - 1 ) & mask ];
479
+ struct pipe_buffer * buf = pipe_buf (pipe , head - 1 );
476
480
int offset = buf -> offset + buf -> len ;
477
481
478
482
if ((buf -> flags & PIPE_BUF_FLAG_CAN_MERGE ) &&
@@ -503,54 +507,44 @@ pipe_write(struct kiocb *iocb, struct iov_iter *from)
503
507
504
508
head = pipe -> head ;
505
509
if (!pipe_full (head , pipe -> tail , pipe -> max_usage )) {
506
- unsigned int mask = pipe -> ring_size - 1 ;
507
510
struct pipe_buffer * buf ;
508
- struct page * page = pipe -> tmp_page ;
511
+ struct page * page ;
509
512
int copied ;
510
513
511
- if (!page ) {
512
- page = alloc_page (GFP_HIGHUSER | __GFP_ACCOUNT );
513
- if (unlikely (!page )) {
514
- ret = ret ? : - ENOMEM ;
515
- break ;
516
- }
517
- pipe -> tmp_page = page ;
514
+ page = anon_pipe_get_page (pipe );
515
+ if (unlikely (!page )) {
516
+ if (!ret )
517
+ ret = - ENOMEM ;
518
+ break ;
518
519
}
519
520
520
- /* Allocate a slot in the ring in advance and attach an
521
- * empty buffer. If we fault or otherwise fail to use
522
- * it, either the reader will consume it or it'll still
523
- * be there for the next write.
524
- */
525
- pipe -> head = head + 1 ;
521
+ copied = copy_page_from_iter (page , 0 , PAGE_SIZE , from );
522
+ if (unlikely (copied < PAGE_SIZE && iov_iter_count (from ))) {
523
+ anon_pipe_put_page (pipe , page );
524
+ if (!ret )
525
+ ret = - EFAULT ;
526
+ break ;
527
+ }
526
528
529
+ pipe -> head = head + 1 ;
527
530
/* Insert it into the buffer array */
528
- buf = & pipe -> bufs [ head & mask ] ;
531
+ buf = pipe_buf ( pipe , head ) ;
529
532
buf -> page = page ;
530
533
buf -> ops = & anon_pipe_buf_ops ;
531
534
buf -> offset = 0 ;
532
- buf -> len = 0 ;
533
535
if (is_packetized (filp ))
534
536
buf -> flags = PIPE_BUF_FLAG_PACKET ;
535
537
else
536
538
buf -> flags = PIPE_BUF_FLAG_CAN_MERGE ;
537
- pipe -> tmp_page = NULL ;
538
539
539
- copied = copy_page_from_iter (page , 0 , PAGE_SIZE , from );
540
- if (unlikely (copied < PAGE_SIZE && iov_iter_count (from ))) {
541
- if (!ret )
542
- ret = - EFAULT ;
543
- break ;
544
- }
545
- ret += copied ;
546
540
buf -> len = copied ;
541
+ ret += copied ;
547
542
548
543
if (!iov_iter_count (from ))
549
544
break ;
550
- }
551
545
552
- if (!pipe_full (head , pipe -> tail , pipe -> max_usage ))
553
546
continue ;
547
+ }
554
548
555
549
/* Wait for buffer space to become available. */
556
550
if ((filp -> f_flags & O_NONBLOCK ) ||
@@ -602,11 +596,21 @@ pipe_write(struct kiocb *iocb, struct iov_iter *from)
602
596
kill_fasync (& pipe -> fasync_readers , SIGIO , POLL_IN );
603
597
if (wake_next_writer )
604
598
wake_up_interruptible_sync_poll (& pipe -> wr_wait , EPOLLOUT | EPOLLWRNORM );
605
- if (ret > 0 && sb_start_write_trylock (file_inode (filp )-> i_sb )) {
606
- int err = file_update_time (filp );
607
- if (err )
608
- ret = err ;
609
- sb_end_write (file_inode (filp )-> i_sb );
599
+ return ret ;
600
+ }
601
+
602
+ static ssize_t
603
+ fifo_pipe_write (struct kiocb * iocb , struct iov_iter * from )
604
+ {
605
+ int ret = anon_pipe_write (iocb , from );
606
+ if (ret > 0 ) {
607
+ struct file * filp = iocb -> ki_filp ;
608
+ if (sb_start_write_trylock (file_inode (filp )-> i_sb )) {
609
+ int err = file_update_time (filp );
610
+ if (err )
611
+ ret = err ;
612
+ sb_end_write (file_inode (filp )-> i_sb );
613
+ }
610
614
}
611
615
return ret ;
612
616
}
@@ -853,8 +857,10 @@ void free_pipe_info(struct pipe_inode_info *pipe)
853
857
if (pipe -> watch_queue )
854
858
put_watch_queue (pipe -> watch_queue );
855
859
#endif
856
- if (pipe -> tmp_page )
857
- __free_page (pipe -> tmp_page );
860
+ for (i = 0 ; i < ARRAY_SIZE (pipe -> tmp_page ); i ++ ) {
861
+ if (pipe -> tmp_page [i ])
862
+ __free_page (pipe -> tmp_page [i ]);
863
+ }
858
864
kfree (pipe -> bufs );
859
865
kfree (pipe );
860
866
}
@@ -874,6 +880,8 @@ static const struct dentry_operations pipefs_dentry_operations = {
874
880
.d_dname = pipefs_dname ,
875
881
};
876
882
883
+ static const struct file_operations pipeanon_fops ;
884
+
877
885
static struct inode * get_pipe_inode (void )
878
886
{
879
887
struct inode * inode = new_inode_pseudo (pipe_mnt -> mnt_sb );
@@ -891,7 +899,7 @@ static struct inode * get_pipe_inode(void)
891
899
inode -> i_pipe = pipe ;
892
900
pipe -> files = 2 ;
893
901
pipe -> readers = pipe -> writers = 1 ;
894
- inode -> i_fop = & pipefifo_fops ;
902
+ inode -> i_fop = & pipeanon_fops ;
895
903
896
904
/*
897
905
* Mark the inode dirty from the very beginning,
@@ -934,7 +942,7 @@ int create_pipe_files(struct file **res, int flags)
934
942
935
943
f = alloc_file_pseudo (inode , pipe_mnt , "" ,
936
944
O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT )),
937
- & pipefifo_fops );
945
+ & pipeanon_fops );
938
946
if (IS_ERR (f )) {
939
947
free_pipe_info (inode -> i_pipe );
940
948
iput (inode );
@@ -945,7 +953,7 @@ int create_pipe_files(struct file **res, int flags)
945
953
f -> f_pipe = 0 ;
946
954
947
955
res [0 ] = alloc_file_clone (f , O_RDONLY | (flags & O_NONBLOCK ),
948
- & pipefifo_fops );
956
+ & pipeanon_fops );
949
957
if (IS_ERR (res [0 ])) {
950
958
put_pipe_info (inode , inode -> i_pipe );
951
959
fput (f );
@@ -1109,8 +1117,8 @@ static void wake_up_partner(struct pipe_inode_info *pipe)
1109
1117
1110
1118
static int fifo_open (struct inode * inode , struct file * filp )
1111
1119
{
1120
+ bool is_pipe = inode -> i_fop == & pipeanon_fops ;
1112
1121
struct pipe_inode_info * pipe ;
1113
- bool is_pipe = inode -> i_sb -> s_magic == PIPEFS_MAGIC ;
1114
1122
int ret ;
1115
1123
1116
1124
filp -> f_pipe = 0 ;
@@ -1234,8 +1242,19 @@ static int fifo_open(struct inode *inode, struct file *filp)
1234
1242
1235
1243
const struct file_operations pipefifo_fops = {
1236
1244
.open = fifo_open ,
1237
- .read_iter = pipe_read ,
1238
- .write_iter = pipe_write ,
1245
+ .read_iter = fifo_pipe_read ,
1246
+ .write_iter = fifo_pipe_write ,
1247
+ .poll = pipe_poll ,
1248
+ .unlocked_ioctl = pipe_ioctl ,
1249
+ .release = pipe_release ,
1250
+ .fasync = pipe_fasync ,
1251
+ .splice_write = iter_file_splice_write ,
1252
+ };
1253
+
1254
+ static const struct file_operations pipeanon_fops = {
1255
+ .open = fifo_open ,
1256
+ .read_iter = anon_pipe_read ,
1257
+ .write_iter = anon_pipe_write ,
1239
1258
.poll = pipe_poll ,
1240
1259
.unlocked_ioctl = pipe_ioctl ,
1241
1260
.release = pipe_release ,
@@ -1271,6 +1290,10 @@ int pipe_resize_ring(struct pipe_inode_info *pipe, unsigned int nr_slots)
1271
1290
struct pipe_buffer * bufs ;
1272
1291
unsigned int head , tail , mask , n ;
1273
1292
1293
+ /* nr_slots larger than limits of pipe->{head,tail} */
1294
+ if (unlikely (nr_slots > (pipe_index_t )- 1u ))
1295
+ return - EINVAL ;
1296
+
1274
1297
bufs = kcalloc (nr_slots , sizeof (* bufs ),
1275
1298
GFP_KERNEL_ACCOUNT | __GFP_NOWARN );
1276
1299
if (unlikely (!bufs ))
@@ -1390,7 +1413,9 @@ struct pipe_inode_info *get_pipe_info(struct file *file, bool for_splice)
1390
1413
{
1391
1414
struct pipe_inode_info * pipe = file -> private_data ;
1392
1415
1393
- if (file -> f_op != & pipefifo_fops || !pipe )
1416
+ if (!pipe )
1417
+ return NULL ;
1418
+ if (file -> f_op != & pipefifo_fops && file -> f_op != & pipeanon_fops )
1394
1419
return NULL ;
1395
1420
if (for_splice && pipe_has_watch_queue (pipe ))
1396
1421
return NULL ;
0 commit comments