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>
This commit is contained in:
K Prateek Nayak 2025-03-07 05:29:19 +00:00 committed by Christian Brauner
parent ba0822021c
commit d5c6cb01b6
No known key found for this signature in database
GPG Key ID: 91C61BC06578DCA2

View File

@ -200,7 +200,6 @@ ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
unsigned int spd_pages = spd->nr_pages; unsigned int spd_pages = spd->nr_pages;
unsigned int tail = pipe->tail; unsigned int tail = pipe->tail;
unsigned int head = pipe->head; unsigned int head = pipe->head;
unsigned int mask = pipe->ring_size - 1;
ssize_t ret = 0; ssize_t ret = 0;
int page_nr = 0; int page_nr = 0;
@ -214,7 +213,7 @@ ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
} }
while (!pipe_full(head, tail, pipe->max_usage)) { while (!pipe_full(head, tail, pipe->max_usage)) {
struct pipe_buffer *buf = &pipe->bufs[head & mask]; struct pipe_buffer *buf = pipe_buf(pipe, head);
buf->page = spd->pages[page_nr]; buf->page = spd->pages[page_nr];
buf->offset = spd->partial[page_nr].offset; 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)
{ {
unsigned int head = pipe->head; unsigned int head = pipe->head;
unsigned int tail = pipe->tail; unsigned int tail = pipe->tail;
unsigned int mask = pipe->ring_size - 1;
int ret; int ret;
if (unlikely(!pipe->readers)) { if (unlikely(!pipe->readers)) {
@ -256,7 +254,7 @@ ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
} else if (pipe_full(head, tail, pipe->max_usage)) { } else if (pipe_full(head, tail, pipe->max_usage)) {
ret = -EAGAIN; ret = -EAGAIN;
} else { } else {
pipe->bufs[head & mask] = *buf; *pipe_buf(pipe, head) = *buf;
pipe->head = head + 1; pipe->head = head + 1;
return buf->len; return buf->len;
} }
@ -447,11 +445,10 @@ static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_des
{ {
unsigned int head = pipe->head; unsigned int head = pipe->head;
unsigned int tail = pipe->tail; unsigned int tail = pipe->tail;
unsigned int mask = pipe->ring_size - 1;
int ret; int ret;
while (!pipe_empty(head, tail)) { while (!pipe_empty(head, tail)) {
struct pipe_buffer *buf = &pipe->bufs[tail & mask]; struct pipe_buffer *buf = pipe_buf(pipe, tail);
sd->len = buf->len; sd->len = buf->len;
if (sd->len > sd->total_len) if (sd->len > sd->total_len)
@ -495,8 +492,7 @@ static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_des
static inline bool eat_empty_buffer(struct pipe_inode_info *pipe) static inline bool eat_empty_buffer(struct pipe_inode_info *pipe)
{ {
unsigned int tail = pipe->tail; unsigned int tail = pipe->tail;
unsigned int mask = pipe->ring_size - 1; struct pipe_buffer *buf = pipe_buf(pipe, tail);
struct pipe_buffer *buf = &pipe->bufs[tail & mask];
if (unlikely(!buf->len)) { if (unlikely(!buf->len)) {
pipe_buf_release(pipe, buf); pipe_buf_release(pipe, buf);
@ -690,7 +686,7 @@ iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
while (sd.total_len) { while (sd.total_len) {
struct kiocb kiocb; struct kiocb kiocb;
struct iov_iter from; struct iov_iter from;
unsigned int head, tail, mask; unsigned int head, tail;
size_t left; size_t left;
int n; int n;
@ -711,12 +707,11 @@ iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
head = pipe->head; head = pipe->head;
tail = pipe->tail; tail = pipe->tail;
mask = pipe->ring_size - 1;
/* build the vector */ /* build the vector */
left = sd.total_len; left = sd.total_len;
for (n = 0; !pipe_empty(head, tail) && left && n < nbufs; tail++) { for (n = 0; !pipe_empty(head, tail) && left && n < nbufs; tail++) {
struct pipe_buffer *buf = &pipe->bufs[tail & mask]; struct pipe_buffer *buf = pipe_buf(pipe, tail);
size_t this_len = buf->len; size_t this_len = buf->len;
/* zero-length bvecs are not supported, skip them */ /* zero-length bvecs are not supported, skip them */
@ -752,7 +747,7 @@ iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
/* dismiss the fully eaten buffers, adjust the partial one */ /* dismiss the fully eaten buffers, adjust the partial one */
tail = pipe->tail; tail = pipe->tail;
while (ret) { while (ret) {
struct pipe_buffer *buf = &pipe->bufs[tail & mask]; struct pipe_buffer *buf = pipe_buf(pipe, tail);
if (ret >= buf->len) { if (ret >= buf->len) {
ret -= buf->len; ret -= buf->len;
buf->len = 0; buf->len = 0;
@ -809,7 +804,7 @@ ssize_t splice_to_socket(struct pipe_inode_info *pipe, struct file *out,
pipe_lock(pipe); pipe_lock(pipe);
while (len > 0) { while (len > 0) {
unsigned int head, tail, mask, bc = 0; unsigned int head, tail, bc = 0;
size_t remain = len; size_t remain = len;
/* /*
@ -846,10 +841,9 @@ ssize_t splice_to_socket(struct pipe_inode_info *pipe, struct file *out,
head = pipe->head; head = pipe->head;
tail = pipe->tail; tail = pipe->tail;
mask = pipe->ring_size - 1;
while (!pipe_empty(head, tail)) { while (!pipe_empty(head, tail)) {
struct pipe_buffer *buf = &pipe->bufs[tail & mask]; struct pipe_buffer *buf = pipe_buf(pipe, tail);
size_t seg; size_t seg;
if (!buf->len) { if (!buf->len) {
@ -894,7 +888,7 @@ ssize_t splice_to_socket(struct pipe_inode_info *pipe, struct file *out,
len -= ret; len -= ret;
tail = pipe->tail; tail = pipe->tail;
while (ret > 0) { while (ret > 0) {
struct pipe_buffer *buf = &pipe->bufs[tail & mask]; struct pipe_buffer *buf = pipe_buf(pipe, tail);
size_t seg = min_t(size_t, ret, buf->len); size_t seg = min_t(size_t, ret, buf->len);
buf->offset += seg; buf->offset += seg;
@ -1725,7 +1719,6 @@ static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
struct pipe_buffer *ibuf, *obuf; struct pipe_buffer *ibuf, *obuf;
unsigned int i_head, o_head; unsigned int i_head, o_head;
unsigned int i_tail, o_tail; unsigned int i_tail, o_tail;
unsigned int i_mask, o_mask;
int ret = 0; int ret = 0;
bool input_wakeup = false; bool input_wakeup = false;
@ -1747,9 +1740,7 @@ retry:
pipe_double_lock(ipipe, opipe); pipe_double_lock(ipipe, opipe);
i_tail = ipipe->tail; i_tail = ipipe->tail;
i_mask = ipipe->ring_size - 1;
o_head = opipe->head; o_head = opipe->head;
o_mask = opipe->ring_size - 1;
do { do {
size_t o_len; size_t o_len;
@ -1792,8 +1783,8 @@ retry:
goto retry; goto retry;
} }
ibuf = &ipipe->bufs[i_tail & i_mask]; ibuf = pipe_buf(ipipe, i_tail);
obuf = &opipe->bufs[o_head & o_mask]; obuf = pipe_buf(opipe, o_head);
if (len >= ibuf->len) { if (len >= ibuf->len) {
/* /*
@ -1862,7 +1853,6 @@ static ssize_t link_pipe(struct pipe_inode_info *ipipe,
struct pipe_buffer *ibuf, *obuf; struct pipe_buffer *ibuf, *obuf;
unsigned int i_head, o_head; unsigned int i_head, o_head;
unsigned int i_tail, o_tail; unsigned int i_tail, o_tail;
unsigned int i_mask, o_mask;
ssize_t ret = 0; ssize_t ret = 0;
/* /*
@ -1873,9 +1863,7 @@ static ssize_t link_pipe(struct pipe_inode_info *ipipe,
pipe_double_lock(ipipe, opipe); pipe_double_lock(ipipe, opipe);
i_tail = ipipe->tail; i_tail = ipipe->tail;
i_mask = ipipe->ring_size - 1;
o_head = opipe->head; o_head = opipe->head;
o_mask = opipe->ring_size - 1;
do { do {
if (!opipe->readers) { if (!opipe->readers) {
@ -1896,8 +1884,8 @@ static ssize_t link_pipe(struct pipe_inode_info *ipipe,
pipe_full(o_head, o_tail, opipe->max_usage)) pipe_full(o_head, o_tail, opipe->max_usage))
break; break;
ibuf = &ipipe->bufs[i_tail & i_mask]; ibuf = pipe_buf(ipipe, i_tail);
obuf = &opipe->bufs[o_head & o_mask]; obuf = pipe_buf(opipe, o_head);
/* /*
* Get a reference to this pipe buffer, * Get a reference to this pipe buffer,