Skip to content

Commit 3732d8f

Browse files
committed
Merge patch series "pipe: Trivial cleanups"
K Prateek Nayak <kprateek.nayak@amd.com> says: Based on the suggestion on the RFC, the treewide conversion of references to pipe->{head,tail} from unsigned int to pipe_index_t has been dropped for now. The series contains trivial cleanup suggested to limit the nr_slots in pipe_resize_ring() to be covered between pipe_index_t limits of pipe->{head,tail} and using pipe_buf() to remove the open-coded usage of masks to access pipe buffer building on Linus' cleanup of fs/fuse/dev.c in commit ebb0f38 ("fs/pipe: fix pipe buffer index use in FUSE") * patches from https://lore.kernel.org/r/20250307052919.34542-1-kprateek.nayak@amd.com: fs/splice: Use pipe_buf() helper to retrieve pipe buffer fs/pipe: Use pipe_buf() helper to retrieve pipe buffer kernel/watch_queue: Use pipe_buf() to retrieve the pipe buffer fs/pipe: Limit the slots in pipe_resize_ring() Link: https://lore.kernel.org/r/20250307052919.34542-1-kprateek.nayak@amd.com Signed-off-by: Christian Brauner <brauner@kernel.org>
2 parents 38962d9 + d5c6cb0 commit 3732d8f

3 files changed

Lines changed: 24 additions & 36 deletions

File tree

fs/pipe.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,6 @@ anon_pipe_read(struct kiocb *iocb, struct iov_iter *to)
294294
/* Read ->head with a barrier vs post_one_notification() */
295295
unsigned int head = smp_load_acquire(&pipe->head);
296296
unsigned int tail = pipe->tail;
297-
unsigned int mask = pipe->ring_size - 1;
298297

299298
#ifdef CONFIG_WATCH_QUEUE
300299
if (pipe->note_loss) {
@@ -321,7 +320,7 @@ anon_pipe_read(struct kiocb *iocb, struct iov_iter *to)
321320
#endif
322321

323322
if (!pipe_empty(head, tail)) {
324-
struct pipe_buffer *buf = &pipe->bufs[tail & mask];
323+
struct pipe_buffer *buf = pipe_buf(pipe, tail);
325324
size_t chars = buf->len;
326325
size_t written;
327326
int error;
@@ -477,8 +476,7 @@ anon_pipe_write(struct kiocb *iocb, struct iov_iter *from)
477476
was_empty = pipe_empty(head, pipe->tail);
478477
chars = total_len & (PAGE_SIZE-1);
479478
if (chars && !was_empty) {
480-
unsigned int mask = pipe->ring_size - 1;
481-
struct pipe_buffer *buf = &pipe->bufs[(head - 1) & mask];
479+
struct pipe_buffer *buf = pipe_buf(pipe, head - 1);
482480
int offset = buf->offset + buf->len;
483481

484482
if ((buf->flags & PIPE_BUF_FLAG_CAN_MERGE) &&
@@ -509,7 +507,6 @@ anon_pipe_write(struct kiocb *iocb, struct iov_iter *from)
509507

510508
head = pipe->head;
511509
if (!pipe_full(head, pipe->tail, pipe->max_usage)) {
512-
unsigned int mask = pipe->ring_size - 1;
513510
struct pipe_buffer *buf;
514511
struct page *page;
515512
int copied;
@@ -531,7 +528,7 @@ anon_pipe_write(struct kiocb *iocb, struct iov_iter *from)
531528

532529
pipe->head = head + 1;
533530
/* Insert it into the buffer array */
534-
buf = &pipe->bufs[head & mask];
531+
buf = pipe_buf(pipe, head);
535532
buf->page = page;
536533
buf->ops = &anon_pipe_buf_ops;
537534
buf->offset = 0;
@@ -1293,6 +1290,10 @@ int pipe_resize_ring(struct pipe_inode_info *pipe, unsigned int nr_slots)
12931290
struct pipe_buffer *bufs;
12941291
unsigned int head, tail, mask, n;
12951292

1293+
/* nr_slots larger than limits of pipe->{head,tail} */
1294+
if (unlikely(nr_slots > (pipe_index_t)-1u))
1295+
return -EINVAL;
1296+
12961297
bufs = kcalloc(nr_slots, sizeof(*bufs),
12971298
GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
12981299
if (unlikely(!bufs))

fs/splice.c

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
200200
unsigned int spd_pages = spd->nr_pages;
201201
unsigned int tail = pipe->tail;
202202
unsigned int head = pipe->head;
203-
unsigned int mask = pipe->ring_size - 1;
204203
ssize_t ret = 0;
205204
int page_nr = 0;
206205

@@ -214,7 +213,7 @@ ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
214213
}
215214

216215
while (!pipe_full(head, tail, pipe->max_usage)) {
217-
struct pipe_buffer *buf = &pipe->bufs[head & mask];
216+
struct pipe_buffer *buf = pipe_buf(pipe, head);
218217

219218
buf->page = spd->pages[page_nr];
220219
buf->offset = spd->partial[page_nr].offset;
@@ -247,7 +246,6 @@ ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
247246
{
248247
unsigned int head = pipe->head;
249248
unsigned int tail = pipe->tail;
250-
unsigned int mask = pipe->ring_size - 1;
251249
int ret;
252250

253251
if (unlikely(!pipe->readers)) {
@@ -256,7 +254,7 @@ ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
256254
} else if (pipe_full(head, tail, pipe->max_usage)) {
257255
ret = -EAGAIN;
258256
} else {
259-
pipe->bufs[head & mask] = *buf;
257+
*pipe_buf(pipe, head) = *buf;
260258
pipe->head = head + 1;
261259
return buf->len;
262260
}
@@ -447,11 +445,10 @@ static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_des
447445
{
448446
unsigned int head = pipe->head;
449447
unsigned int tail = pipe->tail;
450-
unsigned int mask = pipe->ring_size - 1;
451448
int ret;
452449

453450
while (!pipe_empty(head, tail)) {
454-
struct pipe_buffer *buf = &pipe->bufs[tail & mask];
451+
struct pipe_buffer *buf = pipe_buf(pipe, tail);
455452

456453
sd->len = buf->len;
457454
if (sd->len > sd->total_len)
@@ -495,8 +492,7 @@ static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_des
495492
static inline bool eat_empty_buffer(struct pipe_inode_info *pipe)
496493
{
497494
unsigned int tail = pipe->tail;
498-
unsigned int mask = pipe->ring_size - 1;
499-
struct pipe_buffer *buf = &pipe->bufs[tail & mask];
495+
struct pipe_buffer *buf = pipe_buf(pipe, tail);
500496

501497
if (unlikely(!buf->len)) {
502498
pipe_buf_release(pipe, buf);
@@ -690,7 +686,7 @@ iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
690686
while (sd.total_len) {
691687
struct kiocb kiocb;
692688
struct iov_iter from;
693-
unsigned int head, tail, mask;
689+
unsigned int head, tail;
694690
size_t left;
695691
int n;
696692

@@ -711,12 +707,11 @@ iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
711707

712708
head = pipe->head;
713709
tail = pipe->tail;
714-
mask = pipe->ring_size - 1;
715710

716711
/* build the vector */
717712
left = sd.total_len;
718713
for (n = 0; !pipe_empty(head, tail) && left && n < nbufs; tail++) {
719-
struct pipe_buffer *buf = &pipe->bufs[tail & mask];
714+
struct pipe_buffer *buf = pipe_buf(pipe, tail);
720715
size_t this_len = buf->len;
721716

722717
/* zero-length bvecs are not supported, skip them */
@@ -752,7 +747,7 @@ iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
752747
/* dismiss the fully eaten buffers, adjust the partial one */
753748
tail = pipe->tail;
754749
while (ret) {
755-
struct pipe_buffer *buf = &pipe->bufs[tail & mask];
750+
struct pipe_buffer *buf = pipe_buf(pipe, tail);
756751
if (ret >= buf->len) {
757752
ret -= buf->len;
758753
buf->len = 0;
@@ -809,7 +804,7 @@ ssize_t splice_to_socket(struct pipe_inode_info *pipe, struct file *out,
809804
pipe_lock(pipe);
810805

811806
while (len > 0) {
812-
unsigned int head, tail, mask, bc = 0;
807+
unsigned int head, tail, bc = 0;
813808
size_t remain = len;
814809

815810
/*
@@ -846,10 +841,9 @@ ssize_t splice_to_socket(struct pipe_inode_info *pipe, struct file *out,
846841

847842
head = pipe->head;
848843
tail = pipe->tail;
849-
mask = pipe->ring_size - 1;
850844

851845
while (!pipe_empty(head, tail)) {
852-
struct pipe_buffer *buf = &pipe->bufs[tail & mask];
846+
struct pipe_buffer *buf = pipe_buf(pipe, tail);
853847
size_t seg;
854848

855849
if (!buf->len) {
@@ -894,7 +888,7 @@ ssize_t splice_to_socket(struct pipe_inode_info *pipe, struct file *out,
894888
len -= ret;
895889
tail = pipe->tail;
896890
while (ret > 0) {
897-
struct pipe_buffer *buf = &pipe->bufs[tail & mask];
891+
struct pipe_buffer *buf = pipe_buf(pipe, tail);
898892
size_t seg = min_t(size_t, ret, buf->len);
899893

900894
buf->offset += seg;
@@ -1725,7 +1719,6 @@ static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
17251719
struct pipe_buffer *ibuf, *obuf;
17261720
unsigned int i_head, o_head;
17271721
unsigned int i_tail, o_tail;
1728-
unsigned int i_mask, o_mask;
17291722
int ret = 0;
17301723
bool input_wakeup = false;
17311724

@@ -1747,9 +1740,7 @@ static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
17471740
pipe_double_lock(ipipe, opipe);
17481741

17491742
i_tail = ipipe->tail;
1750-
i_mask = ipipe->ring_size - 1;
17511743
o_head = opipe->head;
1752-
o_mask = opipe->ring_size - 1;
17531744

17541745
do {
17551746
size_t o_len;
@@ -1792,8 +1783,8 @@ static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
17921783
goto retry;
17931784
}
17941785

1795-
ibuf = &ipipe->bufs[i_tail & i_mask];
1796-
obuf = &opipe->bufs[o_head & o_mask];
1786+
ibuf = pipe_buf(ipipe, i_tail);
1787+
obuf = pipe_buf(opipe, o_head);
17971788

17981789
if (len >= ibuf->len) {
17991790
/*
@@ -1862,7 +1853,6 @@ static ssize_t link_pipe(struct pipe_inode_info *ipipe,
18621853
struct pipe_buffer *ibuf, *obuf;
18631854
unsigned int i_head, o_head;
18641855
unsigned int i_tail, o_tail;
1865-
unsigned int i_mask, o_mask;
18661856
ssize_t ret = 0;
18671857

18681858
/*
@@ -1873,9 +1863,7 @@ static ssize_t link_pipe(struct pipe_inode_info *ipipe,
18731863
pipe_double_lock(ipipe, opipe);
18741864

18751865
i_tail = ipipe->tail;
1876-
i_mask = ipipe->ring_size - 1;
18771866
o_head = opipe->head;
1878-
o_mask = opipe->ring_size - 1;
18791867

18801868
do {
18811869
if (!opipe->readers) {
@@ -1896,8 +1884,8 @@ static ssize_t link_pipe(struct pipe_inode_info *ipipe,
18961884
pipe_full(o_head, o_tail, opipe->max_usage))
18971885
break;
18981886

1899-
ibuf = &ipipe->bufs[i_tail & i_mask];
1900-
obuf = &opipe->bufs[o_head & o_mask];
1887+
ibuf = pipe_buf(ipipe, i_tail);
1888+
obuf = pipe_buf(opipe, o_head);
19011889

19021890
/*
19031891
* Get a reference to this pipe buffer,

kernel/watch_queue.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,11 @@ static bool post_one_notification(struct watch_queue *wqueue,
101101
struct pipe_inode_info *pipe = wqueue->pipe;
102102
struct pipe_buffer *buf;
103103
struct page *page;
104-
unsigned int head, tail, mask, note, offset, len;
104+
unsigned int head, tail, note, offset, len;
105105
bool done = false;
106106

107107
spin_lock_irq(&pipe->rd_wait.lock);
108108

109-
mask = pipe->ring_size - 1;
110109
head = pipe->head;
111110
tail = pipe->tail;
112111
if (pipe_full(head, tail, pipe->ring_size))
@@ -124,7 +123,7 @@ static bool post_one_notification(struct watch_queue *wqueue,
124123
memcpy(p + offset, n, len);
125124
kunmap_atomic(p);
126125

127-
buf = &pipe->bufs[head & mask];
126+
buf = pipe_buf(pipe, head);
128127
buf->page = page;
129128
buf->private = (unsigned long)wqueue;
130129
buf->ops = &watch_queue_pipe_buf_ops;
@@ -147,7 +146,7 @@ static bool post_one_notification(struct watch_queue *wqueue,
147146
return done;
148147

149148
lost:
150-
buf = &pipe->bufs[(head - 1) & mask];
149+
buf = pipe_buf(pipe, head - 1);
151150
buf->flags |= PIPE_BUF_FLAG_LOSS;
152151
goto out;
153152
}

0 commit comments

Comments
 (0)