Skip to content

Commit d5c6cb0

Browse files
kudureranganathbrauner
authored andcommitted
fs/splice: Use pipe_buf() helper to retrieve pipe buffer
Use pipe_buf() helper to retrieve the pipe buffer throughout the file replacing the open-coded the logic. Suggested-by: Oleg Nesterov <oleg@redhat.com> Signed-off-by: K Prateek Nayak <kprateek.nayak@amd.com> Link: https://lore.kernel.org/r/20250307052919.34542-5-kprateek.nayak@amd.com Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent ba08220 commit d5c6cb0

1 file changed

Lines changed: 14 additions & 26 deletions

File tree

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,

0 commit comments

Comments
 (0)