Skip to content

Commit 8cefc10

Browse files
committed
pipe: Use head and tail pointers for the ring, not cursor and length
Convert pipes to use head and tail pointers for the buffer ring rather than pointer and length as the latter requires two atomic ops to update (or a combined op) whereas the former only requires one. (1) The head pointer is the point at which production occurs and points to the slot in which the next buffer will be placed. This is equivalent to pipe->curbuf + pipe->nrbufs. The head pointer belongs to the write-side. (2) The tail pointer is the point at which consumption occurs. It points to the next slot to be consumed. This is equivalent to pipe->curbuf. The tail pointer belongs to the read-side. (3) head and tail are allowed to run to UINT_MAX and wrap naturally. They are only masked off when the array is being accessed, e.g.: pipe->bufs[head & mask] This means that it is not necessary to have a dead slot in the ring as head == tail isn't ambiguous. (4) The ring is empty if "head == tail". A helper, pipe_empty(), is provided for this. (5) The occupancy of the ring is "head - tail". A helper, pipe_occupancy(), is provided for this. (6) The number of free slots in the ring is "pipe->ring_size - occupancy". A helper, pipe_space_for_user() is provided to indicate how many slots userspace may use. (7) The ring is full if "head - tail >= pipe->ring_size". A helper, pipe_full(), is provided for this. Signed-off-by: David Howells <dhowells@redhat.com>
1 parent f94df98 commit 8cefc10

7 files changed

Lines changed: 448 additions & 292 deletions

File tree

‎drivers/char/virtio_console.c‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,7 @@ static ssize_t port_fops_splice_write(struct pipe_inode_info *pipe,
919919
.pos = *ppos,
920920
.u.data = &sgl,
921921
};
922+
unsigned int occupancy;
922923

923924
/*
924925
* Rproc_serial does not yet support splice. To support splice
@@ -929,29 +930,26 @@ static ssize_t port_fops_splice_write(struct pipe_inode_info *pipe,
929930
if (is_rproc_serial(port->out_vq->vdev))
930931
return -EINVAL;
931932

932-
/*
933-
* pipe->nrbufs == 0 means there are no data to transfer,
934-
* so this returns just 0 for no data.
935-
*/
936933
pipe_lock(pipe);
937-
if (!pipe->nrbufs) {
938-
ret = 0;
934+
ret = 0;
935+
if (pipe_empty(pipe->head, pipe->tail))
939936
goto error_out;
940-
}
941937

942938
ret = wait_port_writable(port, filp->f_flags & O_NONBLOCK);
943939
if (ret < 0)
944940
goto error_out;
945941

946-
buf = alloc_buf(port->portdev->vdev, 0, pipe->nrbufs);
942+
occupancy = pipe_occupancy(pipe->head, pipe->tail);
943+
buf = alloc_buf(port->portdev->vdev, 0, occupancy);
944+
947945
if (!buf) {
948946
ret = -ENOMEM;
949947
goto error_out;
950948
}
951949

952950
sgl.n = 0;
953951
sgl.len = 0;
954-
sgl.size = pipe->nrbufs;
952+
sgl.size = occupancy;
955953
sgl.sg = buf->sg;
956954
sg_init_table(sgl.sg, sgl.size);
957955
ret = __splice_from_pipe(pipe, &sd, pipe_to_sg);

‎fs/fuse/dev.c‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ static int fuse_copy_fill(struct fuse_copy_state *cs)
703703
cs->pipebufs++;
704704
cs->nr_segs--;
705705
} else {
706-
if (cs->nr_segs == cs->pipe->buffers)
706+
if (cs->nr_segs >= cs->pipe->ring_size)
707707
return -EIO;
708708

709709
page = alloc_page(GFP_HIGHUSER);
@@ -879,7 +879,7 @@ static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
879879
struct pipe_buffer *buf;
880880
int err;
881881

882-
if (cs->nr_segs == cs->pipe->buffers)
882+
if (cs->nr_segs >= cs->pipe->ring_size)
883883
return -EIO;
884884

885885
err = unlock_request(cs->req);
@@ -1341,7 +1341,7 @@ static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
13411341
if (!fud)
13421342
return -EPERM;
13431343

1344-
bufs = kvmalloc_array(pipe->buffers, sizeof(struct pipe_buffer),
1344+
bufs = kvmalloc_array(pipe->ring_size, sizeof(struct pipe_buffer),
13451345
GFP_KERNEL);
13461346
if (!bufs)
13471347
return -ENOMEM;
@@ -1353,7 +1353,7 @@ static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
13531353
if (ret < 0)
13541354
goto out;
13551355

1356-
if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1356+
if (pipe_occupancy(pipe->head, pipe->tail) + cs.nr_segs > pipe->ring_size) {
13571357
ret = -EIO;
13581358
goto out;
13591359
}
@@ -1935,6 +1935,7 @@ static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
19351935
struct file *out, loff_t *ppos,
19361936
size_t len, unsigned int flags)
19371937
{
1938+
unsigned int head, tail, mask, count;
19381939
unsigned nbuf;
19391940
unsigned idx;
19401941
struct pipe_buffer *bufs;
@@ -1949,17 +1950,21 @@ static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
19491950

19501951
pipe_lock(pipe);
19511952

1952-
bufs = kvmalloc_array(pipe->nrbufs, sizeof(struct pipe_buffer),
1953-
GFP_KERNEL);
1953+
head = pipe->head;
1954+
tail = pipe->tail;
1955+
mask = pipe->ring_size - 1;
1956+
count = head - tail;
1957+
1958+
bufs = kvmalloc_array(count, sizeof(struct pipe_buffer), GFP_KERNEL);
19541959
if (!bufs) {
19551960
pipe_unlock(pipe);
19561961
return -ENOMEM;
19571962
}
19581963

19591964
nbuf = 0;
19601965
rem = 0;
1961-
for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
1962-
rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
1966+
for (idx = tail; idx < head && rem < len; idx++)
1967+
rem += pipe->bufs[idx & mask].len;
19631968

19641969
ret = -EINVAL;
19651970
if (rem < len)
@@ -1970,16 +1975,16 @@ static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
19701975
struct pipe_buffer *ibuf;
19711976
struct pipe_buffer *obuf;
19721977

1973-
BUG_ON(nbuf >= pipe->buffers);
1974-
BUG_ON(!pipe->nrbufs);
1975-
ibuf = &pipe->bufs[pipe->curbuf];
1978+
BUG_ON(nbuf >= pipe->ring_size);
1979+
BUG_ON(tail == head);
1980+
ibuf = &pipe->bufs[tail & mask];
19761981
obuf = &bufs[nbuf];
19771982

19781983
if (rem >= ibuf->len) {
19791984
*obuf = *ibuf;
19801985
ibuf->ops = NULL;
1981-
pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
1982-
pipe->nrbufs--;
1986+
tail++;
1987+
pipe->tail = tail;
19831988
} else {
19841989
if (!pipe_buf_get(pipe, ibuf))
19851990
goto out_free;

0 commit comments

Comments
 (0)