Skip to content

Commit 1db60cf

Browse files
author
Jens Axboe
committed
relay: use splice_to_pipe() instead of open-coding the pipe loop
It cleans up the relay splice implementation a lot, and gets rid of a lot of internal pipe knowledge that should not be in there. Plus fixes for padding and partial first page (and lots more) from Tom Zanussi. Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
1 parent 497f962 commit 1db60cf

1 file changed

Lines changed: 55 additions & 91 deletions

File tree

‎kernel/relay.c‎

Lines changed: 55 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,19 +1032,23 @@ static ssize_t relay_file_read(struct file *filp,
10321032
NULL, &desc);
10331033
}
10341034

1035+
static void relay_consume_bytes(struct rchan_buf *rbuf, int bytes_consumed)
1036+
{
1037+
rbuf->bytes_consumed += bytes_consumed;
1038+
1039+
if (rbuf->bytes_consumed >= rbuf->chan->subbuf_size) {
1040+
relay_subbufs_consumed(rbuf->chan, rbuf->cpu, 1);
1041+
rbuf->bytes_consumed %= rbuf->chan->subbuf_size;
1042+
}
1043+
}
1044+
10351045
static void relay_pipe_buf_release(struct pipe_inode_info *pipe,
10361046
struct pipe_buffer *buf)
10371047
{
10381048
struct rchan_buf *rbuf;
10391049

10401050
rbuf = (struct rchan_buf *)page_private(buf->page);
1041-
1042-
rbuf->bytes_consumed += PAGE_SIZE;
1043-
1044-
if (rbuf->bytes_consumed == rbuf->chan->subbuf_size) {
1045-
relay_subbufs_consumed(rbuf->chan, rbuf->cpu, 1);
1046-
rbuf->bytes_consumed = 0;
1047-
}
1051+
relay_consume_bytes(rbuf, buf->private);
10481052
}
10491053

10501054
static struct pipe_buf_operations relay_pipe_buf_ops = {
@@ -1067,118 +1071,79 @@ static int subbuf_splice_actor(struct file *in,
10671071
unsigned int flags,
10681072
int *nonpad_ret)
10691073
{
1070-
unsigned int pidx, poff;
1071-
unsigned int subbuf_pages;
1072-
int ret = 0;
1073-
int do_wakeup = 0;
1074+
unsigned int pidx, poff, total_len, subbuf_pages, ret;
10741075
struct rchan_buf *rbuf = in->private_data;
10751076
unsigned int subbuf_size = rbuf->chan->subbuf_size;
10761077
size_t read_start = ((size_t)*ppos) % rbuf->chan->alloc_size;
1077-
size_t avail = subbuf_size - read_start % subbuf_size;
10781078
size_t read_subbuf = read_start / subbuf_size;
10791079
size_t padding = rbuf->padding[read_subbuf];
10801080
size_t nonpad_end = read_subbuf * subbuf_size + subbuf_size - padding;
1081+
struct page *pages[PIPE_BUFFERS];
1082+
struct partial_page partial[PIPE_BUFFERS];
1083+
struct splice_pipe_desc spd = {
1084+
.pages = pages,
1085+
.nr_pages = 0,
1086+
.partial = partial,
1087+
.flags = flags,
1088+
.ops = &relay_pipe_buf_ops,
1089+
};
10811090

10821091
if (rbuf->subbufs_produced == rbuf->subbufs_consumed)
10831092
return 0;
10841093

1085-
if (len > avail)
1086-
len = avail;
1087-
1088-
if (pipe->inode)
1089-
mutex_lock(&pipe->inode->i_mutex);
1094+
/*
1095+
* Adjust read len, if longer than what is available
1096+
*/
1097+
if (len > (subbuf_size - read_start % subbuf_size))
1098+
len = subbuf_size - read_start % subbuf_size;
10901099

10911100
subbuf_pages = rbuf->chan->alloc_size >> PAGE_SHIFT;
10921101
pidx = (read_start / PAGE_SIZE) % subbuf_pages;
10931102
poff = read_start & ~PAGE_MASK;
10941103

1095-
for (;;) {
1096-
unsigned int this_len;
1097-
unsigned int this_end;
1098-
int newbuf = (pipe->curbuf + pipe->nrbufs) & (PIPE_BUFFERS - 1);
1099-
struct pipe_buffer *buf = pipe->bufs + newbuf;
1104+
for (total_len = 0; spd.nr_pages < subbuf_pages; spd.nr_pages++) {
1105+
unsigned int this_len, this_end, private;
1106+
unsigned int cur_pos = read_start + total_len;
11001107

1101-
if (!pipe->readers) {
1102-
send_sig(SIGPIPE, current, 0);
1103-
if (!ret)
1104-
ret = -EPIPE;
1108+
if (!len)
11051109
break;
1106-
}
1107-
1108-
if (pipe->nrbufs < PIPE_BUFFERS) {
1109-
this_len = PAGE_SIZE - poff;
1110-
if (this_len > avail)
1111-
this_len = avail;
1112-
1113-
buf->page = rbuf->page_array[pidx];
1114-
buf->offset = poff;
1115-
this_end = read_start + ret + this_len;
1116-
if (this_end > nonpad_end) {
1117-
if (read_start + ret >= nonpad_end)
1118-
buf->len = 0;
1119-
else
1120-
buf->len = nonpad_end - (read_start + ret);
1121-
} else
1122-
buf->len = this_len;
1123-
1124-
*nonpad_ret += buf->len;
1125-
1126-
buf->ops = &relay_pipe_buf_ops;
1127-
pipe->nrbufs++;
11281110

1129-
avail -= this_len;
1130-
ret += this_len;
1131-
poff = 0;
1132-
pidx = (pidx + 1) % subbuf_pages;
1111+
this_len = min_t(unsigned long, len, PAGE_SIZE - poff);
1112+
private = this_len;
11331113

1134-
if (pipe->inode)
1135-
do_wakeup = 1;
1114+
spd.pages[spd.nr_pages] = rbuf->page_array[pidx];
1115+
spd.partial[spd.nr_pages].offset = poff;
11361116

1137-
if (!avail)
1138-
break;
1139-
1140-
if (pipe->nrbufs < PIPE_BUFFERS)
1141-
continue;
1142-
1143-
break;
1117+
this_end = cur_pos + this_len;
1118+
if (this_end >= nonpad_end) {
1119+
this_len = nonpad_end - cur_pos;
1120+
private = this_len + padding;
11441121
}
1122+
spd.partial[spd.nr_pages].len = this_len;
1123+
spd.partial[spd.nr_pages].private = private;
11451124

1146-
if (flags & SPLICE_F_NONBLOCK) {
1147-
if (!ret)
1148-
ret = -EAGAIN;
1149-
break;
1150-
}
1125+
len -= this_len;
1126+
total_len += this_len;
1127+
poff = 0;
1128+
pidx = (pidx + 1) % subbuf_pages;
11511129

1152-
if (signal_pending(current)) {
1153-
if (!ret)
1154-
ret = -ERESTARTSYS;
1130+
if (this_end >= nonpad_end) {
1131+
spd.nr_pages++;
11551132
break;
11561133
}
1157-
1158-
if (do_wakeup) {
1159-
smp_mb();
1160-
if (waitqueue_active(&pipe->wait))
1161-
wake_up_interruptible_sync(&pipe->wait);
1162-
kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1163-
do_wakeup = 0;
1164-
}
1165-
1166-
pipe->waiting_writers++;
1167-
pipe_wait(pipe);
1168-
pipe->waiting_writers--;
11691134
}
11701135

1171-
if (pipe->inode)
1172-
mutex_unlock(&pipe->inode->i_mutex);
1136+
if (!spd.nr_pages)
1137+
return 0;
11731138

1174-
if (do_wakeup) {
1175-
smp_mb();
1176-
if (waitqueue_active(&pipe->wait))
1177-
wake_up_interruptible(&pipe->wait);
1178-
kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1179-
}
1139+
ret = *nonpad_ret = splice_to_pipe(pipe, &spd);
1140+
if (ret < 0 || ret < total_len)
1141+
return ret;
11801142

1181-
return ret;
1143+
if (read_start + ret == nonpad_end)
1144+
ret += padding;
1145+
1146+
return ret;
11821147
}
11831148

11841149
static ssize_t relay_file_splice_read(struct file *in,
@@ -1199,7 +1164,6 @@ static ssize_t relay_file_splice_read(struct file *in,
11991164
if (ret < 0)
12001165
break;
12011166
else if (!ret) {
1202-
break;
12031167
if (spliced)
12041168
break;
12051169
if (flags & SPLICE_F_NONBLOCK) {

0 commit comments

Comments
 (0)