654 lines
19 KiB
Diff
654 lines
19 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Tyler Smith <74526874+tylertms@users.noreply.github.com>
|
|
Date: Wed, 4 Nov 2026 06:17:20 +0000
|
|
Subject: [PATCH] refactor(pipe): separate original buffer lifecycle and waits
|
|
|
|
---
|
|
fs/Makefile | 2 +-
|
|
fs/pipe.c | 267 +---------------------------------------------
|
|
fs/pipe_buffers.c | 265 +++++++++++++++++++++++++++++++++++++++++++++
|
|
fs/pipe_buffers.h | 30 ++++++
|
|
4 files changed, 297 insertions(+), 267 deletions(-)
|
|
create mode 100644 fs/pipe_buffers.c
|
|
create mode 100644 fs/pipe_buffers.h
|
|
|
|
diff --git a/fs/Makefile b/fs/Makefile
|
|
index 804f6153031a2b6f9e0e8338ff76546a25a75139..44e20f0023c16898a05bade6c6efa31480eb1a63 100644
|
|
--- a/fs/Makefile
|
|
+++ b/fs/Makefile
|
|
@@ -8,7 +8,7 @@
|
|
|
|
|
|
obj-y := open.o path_open.o filp_close.o read_write.o file_position.o file_table.o file_table_init.o super.o super_anon_dev.o super_lifetime.o \
|
|
- char_dev.o stat.o exec.o task_comm.o pipe.o namei.o path_get.o name_hash.o filename.o fcntl.o file_async.o \
|
|
+ char_dev.o stat.o exec.o task_comm.o pipe.o pipe_buffers.o namei.o path_get.o name_hash.o filename.o fcntl.o file_async.o \
|
|
ioctl.o readdir.o select.o dcache.o dcache_init.o dentry_ancestry.o inode.o inode_cache_init.o inode_timestamps.o inode_evict.o \
|
|
attr.o bad_inode.o bad_inode_ops.o file.o file_ref.o init_fdtable.o filesystems.o namespace.o namespace_tree.o mount_cache.o \
|
|
seq_file.o seq_lifetime.o seq_output.o seq_path.o xattr.o xattr_access.o simple_xattr.o libfs.o pseudo_metadata.o stashed_path.o fs-writeback.o \
|
|
diff --git a/fs/pipe.c b/fs/pipe.c
|
|
index 429b0714ec57558f9057c58151e0f7da63d3cd49..c408ab91231da261458bdb3215434d34bb19d4ef 100644
|
|
--- a/fs/pipe.c
|
|
+++ b/fs/pipe.c
|
|
@@ -32,33 +32,7 @@
|
|
#include <asm/ioctls.h>
|
|
|
|
#include "internal.h"
|
|
-
|
|
-/*
|
|
- * New pipe buffers will be restricted to this size while the user is exceeding
|
|
- * their pipe buffer quota. The general pipe use case needs at least two
|
|
- * buffers: one for data yet to be read, and one for new data. If this is less
|
|
- * than two, then a write to a non-empty pipe may block even if the pipe is not
|
|
- * full. This can occur with GNU make jobserver or similar uses of pipes as
|
|
- * semaphores: multiple processes may be waiting to write tokens back to the
|
|
- * pipe before reading tokens: https://lore.kernel.org/lkml/1628086770.5rn8p04n6j.none@localhost/.
|
|
- *
|
|
- * Users can reduce their pipe buffers with F_SETPIPE_SZ below this at their
|
|
- * own risk, namely: pipe writes to non-full pipes may block until the pipe is
|
|
- * emptied.
|
|
- */
|
|
-#define PIPE_MIN_DEF_BUFFERS 2
|
|
-
|
|
-/*
|
|
- * The max size that a non-root user is allowed to grow the pipe. Can
|
|
- * be set by root in /proc/sys/fs/pipe-max-size
|
|
- */
|
|
-static unsigned int pipe_max_size = 1048576;
|
|
-
|
|
-/* Maximum allocatable pages per user. Hard limit is unset by default, soft
|
|
- * matches default values.
|
|
- */
|
|
-static unsigned long pipe_user_pages_hard;
|
|
-static unsigned long pipe_user_pages_soft = PIPE_DEF_BUFFERS * INR_OPEN_CUR;
|
|
+#include "pipe_buffers.h"
|
|
|
|
/*
|
|
* We use head and tail indices that aren't masked off, except at the point of
|
|
@@ -77,40 +51,6 @@ static unsigned long pipe_user_pages_soft = PIPE_DEF_BUFFERS * INR_OPEN_CUR;
|
|
* -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
|
|
*/
|
|
|
|
-#ifdef CONFIG_PROVE_LOCKING
|
|
-static int pipe_lock_cmp_fn(const struct lockdep_map *a,
|
|
- const struct lockdep_map *b)
|
|
-{
|
|
- return cmp_int((unsigned long) a, (unsigned long) b);
|
|
-}
|
|
-#endif
|
|
-
|
|
-void pipe_lock(struct pipe_inode_info *pipe)
|
|
-{
|
|
- if (pipe->files)
|
|
- mutex_lock(&pipe->mutex);
|
|
-}
|
|
-EXPORT_SYMBOL(pipe_lock);
|
|
-
|
|
-void pipe_unlock(struct pipe_inode_info *pipe)
|
|
-{
|
|
- if (pipe->files)
|
|
- mutex_unlock(&pipe->mutex);
|
|
-}
|
|
-EXPORT_SYMBOL(pipe_unlock);
|
|
-
|
|
-void pipe_double_lock(struct pipe_inode_info *pipe1,
|
|
- struct pipe_inode_info *pipe2)
|
|
-{
|
|
- BUG_ON(pipe1 == pipe2);
|
|
-
|
|
- if (pipe1 > pipe2)
|
|
- swap(pipe1, pipe2);
|
|
-
|
|
- pipe_lock(pipe1);
|
|
- pipe_lock(pipe2);
|
|
-}
|
|
-
|
|
#define PIPE_PREALLOC_MAX 8
|
|
|
|
struct anon_pipe_prealloc {
|
|
@@ -251,82 +191,12 @@ static bool anon_pipe_buf_try_steal(struct pipe_inode_info *pipe,
|
|
return true;
|
|
}
|
|
|
|
-/**
|
|
- * generic_pipe_buf_try_steal - attempt to take ownership of a &pipe_buffer
|
|
- * @pipe: the pipe that the buffer belongs to
|
|
- * @buf: the buffer to attempt to steal
|
|
- *
|
|
- * Description:
|
|
- * This function attempts to steal the &struct page attached to
|
|
- * @buf. If successful, this function returns 0 and returns with
|
|
- * the page locked. The caller may then reuse the page for whatever
|
|
- * he wishes; the typical use is insertion into a different file
|
|
- * page cache.
|
|
- */
|
|
-bool generic_pipe_buf_try_steal(struct pipe_inode_info *pipe,
|
|
- struct pipe_buffer *buf)
|
|
-{
|
|
- struct page *page = buf->page;
|
|
-
|
|
- /*
|
|
- * A reference of one is golden, that means that the owner of this
|
|
- * page is the only one holding a reference to it. lock the page
|
|
- * and return OK.
|
|
- */
|
|
- if (page_count(page) == 1) {
|
|
- lock_page(page);
|
|
- return true;
|
|
- }
|
|
- return false;
|
|
-}
|
|
-EXPORT_SYMBOL(generic_pipe_buf_try_steal);
|
|
-
|
|
-/**
|
|
- * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
|
|
- * @pipe: the pipe that the buffer belongs to
|
|
- * @buf: the buffer to get a reference to
|
|
- *
|
|
- * Description:
|
|
- * This function grabs an extra reference to @buf. It's used in
|
|
- * the tee() system call, when we duplicate the buffers in one
|
|
- * pipe into another.
|
|
- */
|
|
-bool generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
|
|
-{
|
|
- return try_get_page(buf->page);
|
|
-}
|
|
-EXPORT_SYMBOL(generic_pipe_buf_get);
|
|
-
|
|
-/**
|
|
- * generic_pipe_buf_release - put a reference to a &struct pipe_buffer
|
|
- * @pipe: the pipe that the buffer belongs to
|
|
- * @buf: the buffer to put a reference to
|
|
- *
|
|
- * Description:
|
|
- * This function releases a reference to @buf.
|
|
- */
|
|
-void generic_pipe_buf_release(struct pipe_inode_info *pipe,
|
|
- struct pipe_buffer *buf)
|
|
-{
|
|
- put_page(buf->page);
|
|
-}
|
|
-EXPORT_SYMBOL(generic_pipe_buf_release);
|
|
-
|
|
static const struct pipe_buf_operations anon_pipe_buf_ops = {
|
|
.release = anon_pipe_buf_release,
|
|
.try_steal = anon_pipe_buf_try_steal,
|
|
.get = generic_pipe_buf_get,
|
|
};
|
|
|
|
-/* Done while waiting without holding the pipe lock - thus the READ_ONCE() */
|
|
-static inline bool pipe_readable(const struct pipe_inode_info *pipe)
|
|
-{
|
|
- union pipe_index idx = { .head_tail = READ_ONCE(pipe->head_tail) };
|
|
- unsigned int writers = READ_ONCE(pipe->writers);
|
|
-
|
|
- return !pipe_empty(idx.head, idx.tail) || !writers;
|
|
-}
|
|
-
|
|
static inline unsigned int pipe_update_tail(struct pipe_inode_info *pipe,
|
|
struct pipe_buffer *buf,
|
|
unsigned int tail)
|
|
@@ -509,16 +379,6 @@ static inline int is_packetized(struct file *file)
|
|
return (file->f_flags & O_DIRECT) != 0;
|
|
}
|
|
|
|
-/* Done while waiting without holding the pipe lock - thus the READ_ONCE() */
|
|
-static inline bool pipe_writable(const struct pipe_inode_info *pipe)
|
|
-{
|
|
- union pipe_index idx = { .head_tail = READ_ONCE(pipe->head_tail) };
|
|
- unsigned int max_usage = READ_ONCE(pipe->max_usage);
|
|
-
|
|
- return !pipe_full(idx.head, idx.tail, max_usage) ||
|
|
- !READ_ONCE(pipe->readers);
|
|
-}
|
|
-
|
|
static ssize_t
|
|
anon_pipe_write(struct kiocb *iocb, struct iov_iter *from)
|
|
{
|
|
@@ -862,108 +722,6 @@ pipe_fasync(int fd, struct file *filp, int on)
|
|
return retval;
|
|
}
|
|
|
|
-unsigned long account_pipe_buffers(struct user_struct *user,
|
|
- unsigned long old, unsigned long new)
|
|
-{
|
|
- return atomic_long_add_return(new - old, &user->pipe_bufs);
|
|
-}
|
|
-
|
|
-bool too_many_pipe_buffers_soft(unsigned long user_bufs)
|
|
-{
|
|
- unsigned long soft_limit = READ_ONCE(pipe_user_pages_soft);
|
|
-
|
|
- return soft_limit && user_bufs > soft_limit;
|
|
-}
|
|
-
|
|
-bool too_many_pipe_buffers_hard(unsigned long user_bufs)
|
|
-{
|
|
- unsigned long hard_limit = READ_ONCE(pipe_user_pages_hard);
|
|
-
|
|
- return hard_limit && user_bufs > hard_limit;
|
|
-}
|
|
-
|
|
-bool pipe_is_unprivileged_user(void)
|
|
-{
|
|
- return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN);
|
|
-}
|
|
-
|
|
-struct pipe_inode_info *alloc_pipe_info(void)
|
|
-{
|
|
- struct pipe_inode_info *pipe;
|
|
- unsigned long pipe_bufs = PIPE_DEF_BUFFERS;
|
|
- struct user_struct *user = get_current_user();
|
|
- unsigned long user_bufs;
|
|
- unsigned int max_size = READ_ONCE(pipe_max_size);
|
|
-
|
|
- pipe = kzalloc_obj(struct pipe_inode_info, GFP_KERNEL_ACCOUNT);
|
|
- if (pipe == NULL)
|
|
- goto out_free_uid;
|
|
-
|
|
- if (pipe_bufs * PAGE_SIZE > max_size && !capable(CAP_SYS_RESOURCE))
|
|
- pipe_bufs = max_size >> PAGE_SHIFT;
|
|
-
|
|
- user_bufs = account_pipe_buffers(user, 0, pipe_bufs);
|
|
-
|
|
- if (too_many_pipe_buffers_soft(user_bufs) && pipe_is_unprivileged_user()) {
|
|
- user_bufs = account_pipe_buffers(user, pipe_bufs, PIPE_MIN_DEF_BUFFERS);
|
|
- pipe_bufs = PIPE_MIN_DEF_BUFFERS;
|
|
- }
|
|
-
|
|
- if (too_many_pipe_buffers_hard(user_bufs) && pipe_is_unprivileged_user())
|
|
- goto out_revert_acct;
|
|
-
|
|
- pipe->bufs = kzalloc_objs(struct pipe_buffer, pipe_bufs,
|
|
- GFP_KERNEL_ACCOUNT);
|
|
-
|
|
- if (pipe->bufs) {
|
|
- init_waitqueue_head(&pipe->rd_wait);
|
|
- init_waitqueue_head(&pipe->wr_wait);
|
|
- pipe->r_counter = pipe->w_counter = 1;
|
|
- pipe->max_usage = pipe_bufs;
|
|
- pipe->ring_size = pipe_bufs;
|
|
- pipe->nr_accounted = pipe_bufs;
|
|
- pipe->user = user;
|
|
- mutex_init(&pipe->mutex);
|
|
- lock_set_cmp_fn(&pipe->mutex, pipe_lock_cmp_fn, NULL);
|
|
- return pipe;
|
|
- }
|
|
-
|
|
-out_revert_acct:
|
|
- (void) account_pipe_buffers(user, pipe_bufs, 0);
|
|
- kfree(pipe);
|
|
-out_free_uid:
|
|
- free_uid(user);
|
|
- return NULL;
|
|
-}
|
|
-
|
|
-void free_pipe_info(struct pipe_inode_info *pipe)
|
|
-{
|
|
- unsigned int i;
|
|
-
|
|
-#ifdef CONFIG_WATCH_QUEUE
|
|
- if (pipe->watch_queue)
|
|
- watch_queue_clear(pipe->watch_queue);
|
|
-#endif
|
|
-
|
|
- (void) account_pipe_buffers(pipe->user, pipe->nr_accounted, 0);
|
|
- free_uid(pipe->user);
|
|
- for (i = 0; i < pipe->ring_size; i++) {
|
|
- struct pipe_buffer *buf = pipe->bufs + i;
|
|
- if (buf->ops)
|
|
- pipe_buf_release(pipe, buf);
|
|
- }
|
|
-#ifdef CONFIG_WATCH_QUEUE
|
|
- if (pipe->watch_queue)
|
|
- put_watch_queue(pipe->watch_queue);
|
|
-#endif
|
|
- for (i = 0; i < ARRAY_SIZE(pipe->tmp_page); i++) {
|
|
- if (pipe->tmp_page[i])
|
|
- __free_page(pipe->tmp_page[i]);
|
|
- }
|
|
- kfree(pipe->bufs);
|
|
- kfree(pipe);
|
|
-}
|
|
-
|
|
static struct vfsmount *pipe_mnt __ro_after_init;
|
|
|
|
/*
|
|
@@ -1159,29 +917,6 @@ SYSCALL_DEFINE1(pipe, int __user *, fildes)
|
|
return do_pipe2(fildes, 0);
|
|
}
|
|
|
|
-/*
|
|
- * This is the stupid "wait for pipe to be readable or writable"
|
|
- * model.
|
|
- *
|
|
- * See pipe_read/write() for the proper kind of exclusive wait,
|
|
- * but that requires that we wake up any other readers/writers
|
|
- * if we then do not end up reading everything (ie the whole
|
|
- * "wake_next_reader/writer" logic in pipe_read/write()).
|
|
- */
|
|
-void pipe_wait_readable(struct pipe_inode_info *pipe)
|
|
-{
|
|
- pipe_unlock(pipe);
|
|
- wait_event_interruptible(pipe->rd_wait, pipe_readable(pipe));
|
|
- pipe_lock(pipe);
|
|
-}
|
|
-
|
|
-void pipe_wait_writable(struct pipe_inode_info *pipe)
|
|
-{
|
|
- pipe_unlock(pipe);
|
|
- wait_event_interruptible(pipe->wr_wait, pipe_writable(pipe));
|
|
- pipe_lock(pipe);
|
|
-}
|
|
-
|
|
/*
|
|
* This depends on both the wait (here) and the wakeup (wake_up_partner)
|
|
* holding the pipe lock, so "*cnt" is stable and we know a wakeup cannot
|
|
diff --git a/fs/pipe_buffers.c b/fs/pipe_buffers.c
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..9f3df15cb0f9ec86e20b94b665df7f7d5b39f6a0
|
|
--- /dev/null
|
|
+++ b/fs/pipe_buffers.c
|
|
@@ -0,0 +1,265 @@
|
|
+// SPDX-License-Identifier: GPL-2.0
|
|
+/*
|
|
+ * linux/fs/pipe.c
|
|
+ *
|
|
+ * Copyright (C) 1991, 1992, 1999 Linus Torvalds
|
|
+ */
|
|
+
|
|
+#include <linux/capability.h>
|
|
+#include <linux/cred.h>
|
|
+#include <linux/export.h>
|
|
+#include <linux/file.h>
|
|
+#include <linux/pagemap.h>
|
|
+#include <linux/sched/user.h>
|
|
+#include <linux/slab.h>
|
|
+#include <linux/sort.h>
|
|
+#include <linux/watch_queue.h>
|
|
+
|
|
+#include "pipe_buffers.h"
|
|
+
|
|
+/*
|
|
+ * New pipe buffers will be restricted to this size while the user is exceeding
|
|
+ * their pipe buffer quota. The general pipe use case needs at least two
|
|
+ * buffers: one for data yet to be read, and one for new data. If this is less
|
|
+ * than two, then a write to a non-empty pipe may block even if the pipe is not
|
|
+ * full. This can occur with GNU make jobserver or similar uses of pipes as
|
|
+ * semaphores: multiple processes may be waiting to write tokens back to the
|
|
+ * pipe before reading tokens: https://lore.kernel.org/lkml/1628086770.5rn8p04n6j.none@localhost/.
|
|
+ *
|
|
+ * Users can reduce their pipe buffers with F_SETPIPE_SZ below this at their
|
|
+ * own risk, namely: pipe writes to non-full pipes may block until the pipe is
|
|
+ * emptied.
|
|
+ */
|
|
+#define PIPE_MIN_DEF_BUFFERS 2
|
|
+
|
|
+/*
|
|
+ * The max size that a non-root user is allowed to grow the pipe. Can
|
|
+ * be set by root in /proc/sys/fs/pipe-max-size
|
|
+ */
|
|
+unsigned int pipe_max_size = 1048576;
|
|
+
|
|
+/* Maximum allocatable pages per user. Hard limit is unset by default, soft
|
|
+ * matches default values.
|
|
+ */
|
|
+unsigned long pipe_user_pages_hard;
|
|
+unsigned long pipe_user_pages_soft = PIPE_DEF_BUFFERS * INR_OPEN_CUR;
|
|
+
|
|
+#ifdef CONFIG_PROVE_LOCKING
|
|
+static int pipe_lock_cmp_fn(const struct lockdep_map *a,
|
|
+ const struct lockdep_map *b)
|
|
+{
|
|
+ return cmp_int((unsigned long) a, (unsigned long) b);
|
|
+}
|
|
+#endif
|
|
+
|
|
+void pipe_lock(struct pipe_inode_info *pipe)
|
|
+{
|
|
+ if (pipe->files)
|
|
+ mutex_lock(&pipe->mutex);
|
|
+}
|
|
+EXPORT_SYMBOL(pipe_lock);
|
|
+
|
|
+void pipe_unlock(struct pipe_inode_info *pipe)
|
|
+{
|
|
+ if (pipe->files)
|
|
+ mutex_unlock(&pipe->mutex);
|
|
+}
|
|
+EXPORT_SYMBOL(pipe_unlock);
|
|
+
|
|
+void pipe_double_lock(struct pipe_inode_info *pipe1,
|
|
+ struct pipe_inode_info *pipe2)
|
|
+{
|
|
+ BUG_ON(pipe1 == pipe2);
|
|
+
|
|
+ if (pipe1 > pipe2)
|
|
+ swap(pipe1, pipe2);
|
|
+
|
|
+ pipe_lock(pipe1);
|
|
+ pipe_lock(pipe2);
|
|
+}
|
|
+
|
|
+/**
|
|
+ * generic_pipe_buf_try_steal - attempt to take ownership of a &pipe_buffer
|
|
+ * @pipe: the pipe that the buffer belongs to
|
|
+ * @buf: the buffer to attempt to steal
|
|
+ *
|
|
+ * Description:
|
|
+ * This function attempts to steal the &struct page attached to
|
|
+ * @buf. If successful, this function returns 0 and returns with
|
|
+ * the page locked. The caller may then reuse the page for whatever
|
|
+ * he wishes; the typical use is insertion into a different file
|
|
+ * page cache.
|
|
+ */
|
|
+bool generic_pipe_buf_try_steal(struct pipe_inode_info *pipe,
|
|
+ struct pipe_buffer *buf)
|
|
+{
|
|
+ struct page *page = buf->page;
|
|
+
|
|
+ /*
|
|
+ * A reference of one is golden, that means that the owner of this
|
|
+ * page is the only one holding a reference to it. lock the page
|
|
+ * and return OK.
|
|
+ */
|
|
+ if (page_count(page) == 1) {
|
|
+ lock_page(page);
|
|
+ return true;
|
|
+ }
|
|
+ return false;
|
|
+}
|
|
+EXPORT_SYMBOL(generic_pipe_buf_try_steal);
|
|
+
|
|
+/**
|
|
+ * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
|
|
+ * @pipe: the pipe that the buffer belongs to
|
|
+ * @buf: the buffer to get a reference to
|
|
+ *
|
|
+ * Description:
|
|
+ * This function grabs an extra reference to @buf. It's used in
|
|
+ * the tee() system call, when we duplicate the buffers in one
|
|
+ * pipe into another.
|
|
+ */
|
|
+bool generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
|
|
+{
|
|
+ return try_get_page(buf->page);
|
|
+}
|
|
+EXPORT_SYMBOL(generic_pipe_buf_get);
|
|
+
|
|
+/**
|
|
+ * generic_pipe_buf_release - put a reference to a &struct pipe_buffer
|
|
+ * @pipe: the pipe that the buffer belongs to
|
|
+ * @buf: the buffer to put a reference to
|
|
+ *
|
|
+ * Description:
|
|
+ * This function releases a reference to @buf.
|
|
+ */
|
|
+void generic_pipe_buf_release(struct pipe_inode_info *pipe,
|
|
+ struct pipe_buffer *buf)
|
|
+{
|
|
+ put_page(buf->page);
|
|
+}
|
|
+EXPORT_SYMBOL(generic_pipe_buf_release);
|
|
+
|
|
+unsigned long account_pipe_buffers(struct user_struct *user,
|
|
+ unsigned long old, unsigned long new)
|
|
+{
|
|
+ return atomic_long_add_return(new - old, &user->pipe_bufs);
|
|
+}
|
|
+
|
|
+bool too_many_pipe_buffers_soft(unsigned long user_bufs)
|
|
+{
|
|
+ unsigned long soft_limit = READ_ONCE(pipe_user_pages_soft);
|
|
+
|
|
+ return soft_limit && user_bufs > soft_limit;
|
|
+}
|
|
+
|
|
+bool too_many_pipe_buffers_hard(unsigned long user_bufs)
|
|
+{
|
|
+ unsigned long hard_limit = READ_ONCE(pipe_user_pages_hard);
|
|
+
|
|
+ return hard_limit && user_bufs > hard_limit;
|
|
+}
|
|
+
|
|
+bool pipe_is_unprivileged_user(void)
|
|
+{
|
|
+ return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN);
|
|
+}
|
|
+
|
|
+struct pipe_inode_info *alloc_pipe_info(void)
|
|
+{
|
|
+ struct pipe_inode_info *pipe;
|
|
+ unsigned long pipe_bufs = PIPE_DEF_BUFFERS;
|
|
+ struct user_struct *user = get_current_user();
|
|
+ unsigned long user_bufs;
|
|
+ unsigned int max_size = READ_ONCE(pipe_max_size);
|
|
+
|
|
+ pipe = kzalloc_obj(struct pipe_inode_info, GFP_KERNEL_ACCOUNT);
|
|
+ if (pipe == NULL)
|
|
+ goto out_free_uid;
|
|
+
|
|
+ if (pipe_bufs * PAGE_SIZE > max_size && !capable(CAP_SYS_RESOURCE))
|
|
+ pipe_bufs = max_size >> PAGE_SHIFT;
|
|
+
|
|
+ user_bufs = account_pipe_buffers(user, 0, pipe_bufs);
|
|
+
|
|
+ if (too_many_pipe_buffers_soft(user_bufs) && pipe_is_unprivileged_user()) {
|
|
+ user_bufs = account_pipe_buffers(user, pipe_bufs, PIPE_MIN_DEF_BUFFERS);
|
|
+ pipe_bufs = PIPE_MIN_DEF_BUFFERS;
|
|
+ }
|
|
+
|
|
+ if (too_many_pipe_buffers_hard(user_bufs) && pipe_is_unprivileged_user())
|
|
+ goto out_revert_acct;
|
|
+
|
|
+ pipe->bufs = kzalloc_objs(struct pipe_buffer, pipe_bufs,
|
|
+ GFP_KERNEL_ACCOUNT);
|
|
+
|
|
+ if (pipe->bufs) {
|
|
+ init_waitqueue_head(&pipe->rd_wait);
|
|
+ init_waitqueue_head(&pipe->wr_wait);
|
|
+ pipe->r_counter = pipe->w_counter = 1;
|
|
+ pipe->max_usage = pipe_bufs;
|
|
+ pipe->ring_size = pipe_bufs;
|
|
+ pipe->nr_accounted = pipe_bufs;
|
|
+ pipe->user = user;
|
|
+ mutex_init(&pipe->mutex);
|
|
+ lock_set_cmp_fn(&pipe->mutex, pipe_lock_cmp_fn, NULL);
|
|
+ return pipe;
|
|
+ }
|
|
+
|
|
+out_revert_acct:
|
|
+ (void) account_pipe_buffers(user, pipe_bufs, 0);
|
|
+ kfree(pipe);
|
|
+out_free_uid:
|
|
+ free_uid(user);
|
|
+ return NULL;
|
|
+}
|
|
+
|
|
+void free_pipe_info(struct pipe_inode_info *pipe)
|
|
+{
|
|
+ unsigned int i;
|
|
+
|
|
+#ifdef CONFIG_WATCH_QUEUE
|
|
+ if (pipe->watch_queue)
|
|
+ watch_queue_clear(pipe->watch_queue);
|
|
+#endif
|
|
+
|
|
+ (void) account_pipe_buffers(pipe->user, pipe->nr_accounted, 0);
|
|
+ free_uid(pipe->user);
|
|
+ for (i = 0; i < pipe->ring_size; i++) {
|
|
+ struct pipe_buffer *buf = pipe->bufs + i;
|
|
+ if (buf->ops)
|
|
+ pipe_buf_release(pipe, buf);
|
|
+ }
|
|
+#ifdef CONFIG_WATCH_QUEUE
|
|
+ if (pipe->watch_queue)
|
|
+ put_watch_queue(pipe->watch_queue);
|
|
+#endif
|
|
+ for (i = 0; i < ARRAY_SIZE(pipe->tmp_page); i++) {
|
|
+ if (pipe->tmp_page[i])
|
|
+ __free_page(pipe->tmp_page[i]);
|
|
+ }
|
|
+ kfree(pipe->bufs);
|
|
+ kfree(pipe);
|
|
+}
|
|
+
|
|
+/*
|
|
+ * This is the stupid "wait for pipe to be readable or writable"
|
|
+ * model.
|
|
+ *
|
|
+ * See pipe_read/write() for the proper kind of exclusive wait,
|
|
+ * but that requires that we wake up any other readers/writers
|
|
+ * if we then do not end up reading everything (ie the whole
|
|
+ * "wake_next_reader/writer" logic in pipe_read/write()).
|
|
+ */
|
|
+void pipe_wait_readable(struct pipe_inode_info *pipe)
|
|
+{
|
|
+ pipe_unlock(pipe);
|
|
+ wait_event_interruptible(pipe->rd_wait, pipe_readable(pipe));
|
|
+ pipe_lock(pipe);
|
|
+}
|
|
+
|
|
+void pipe_wait_writable(struct pipe_inode_info *pipe)
|
|
+{
|
|
+ pipe_unlock(pipe);
|
|
+ wait_event_interruptible(pipe->wr_wait, pipe_writable(pipe));
|
|
+ pipe_lock(pipe);
|
|
+}
|
|
diff --git a/fs/pipe_buffers.h b/fs/pipe_buffers.h
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..43f556a98b27d2005adecd7790ceb1be09972a80
|
|
--- /dev/null
|
|
+++ b/fs/pipe_buffers.h
|
|
@@ -0,0 +1,30 @@
|
|
+// SPDX-License-Identifier: GPL-2.0
|
|
+#ifndef _FS_PIPE_BUFFERS_H
|
|
+#define _FS_PIPE_BUFFERS_H
|
|
+
|
|
+#include <linux/pipe_fs_i.h>
|
|
+
|
|
+extern unsigned int pipe_max_size;
|
|
+extern unsigned long pipe_user_pages_hard;
|
|
+extern unsigned long pipe_user_pages_soft;
|
|
+
|
|
+/* Done while waiting without holding the pipe lock - thus the READ_ONCE() */
|
|
+static inline bool pipe_readable(const struct pipe_inode_info *pipe)
|
|
+{
|
|
+ union pipe_index idx = { .head_tail = READ_ONCE(pipe->head_tail) };
|
|
+ unsigned int writers = READ_ONCE(pipe->writers);
|
|
+
|
|
+ return !pipe_empty(idx.head, idx.tail) || !writers;
|
|
+}
|
|
+
|
|
+/* Done while waiting without holding the pipe lock - thus the READ_ONCE() */
|
|
+static inline bool pipe_writable(const struct pipe_inode_info *pipe)
|
|
+{
|
|
+ union pipe_index idx = { .head_tail = READ_ONCE(pipe->head_tail) };
|
|
+ unsigned int max_usage = READ_ONCE(pipe->max_usage);
|
|
+
|
|
+ return !pipe_full(idx.head, idx.tail, max_usage) ||
|
|
+ !READ_ONCE(pipe->readers);
|
|
+}
|
|
+
|
|
+#endif
|