blob: 50afd0d0084caf38fa314004c453a8d9a6695dc0 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef _LINUX_PIPE_FS_I_H
3#define _LINUX_PIPE_FS_I_H
4
Jens Axboe35f3d142010-05-20 10:43:18 +02005#define PIPE_DEF_BUFFERS 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07006
Jens Axboe14328732006-05-03 10:35:26 +02007#define PIPE_BUF_FLAG_LRU 0x01 /* page is on the LRU */
8#define PIPE_BUF_FLAG_ATOMIC 0x02 /* was atomically mapped */
9#define PIPE_BUF_FLAG_GIFT 0x04 /* page is a gift */
Linus Torvalds98830352012-04-29 13:12:42 -070010#define PIPE_BUF_FLAG_PACKET 0x08 /* read() as a packet */
Christoph Hellwigf6dd9752020-05-20 17:58:12 +020011#define PIPE_BUF_FLAG_CAN_MERGE 0x10 /* can merge buffers */
Linus Torvalds6c329782020-06-13 09:56:21 -070012#define PIPE_BUF_FLAG_WHOLE 0x20 /* read() must return entire buffer or error */
David Howellse7d553d2020-01-14 17:07:12 +000013#ifdef CONFIG_WATCH_QUEUE
Linus Torvalds6c329782020-06-13 09:56:21 -070014#define PIPE_BUF_FLAG_LOSS 0x40 /* Message loss happened after this buffer */
David Howellse7d553d2020-01-14 17:07:12 +000015#endif
Jens Axboe3e7ee3e2006-04-02 23:11:04 +020016
Jens Axboe08457182007-06-12 20:51:32 +020017/**
18 * struct pipe_buffer - a linux kernel pipe buffer
19 * @page: the page containing the data for the pipe buffer
20 * @offset: offset of data inside the @page
21 * @len: length of data inside the @page
22 * @ops: operations associated with this buffer. See @pipe_buf_operations.
23 * @flags: pipe buffer flags. See above.
24 * @private: private data owned by the ops.
25 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -070026struct pipe_buffer {
27 struct page *page;
28 unsigned int offset, len;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -080029 const struct pipe_buf_operations *ops;
Jens Axboe3e7ee3e2006-04-02 23:11:04 +020030 unsigned int flags;
Jens Axboe497f9622007-06-11 12:00:45 +020031 unsigned long private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032};
33
Jens Axboe08457182007-06-12 20:51:32 +020034/**
35 * struct pipe_inode_info - a linux kernel pipe
Al Viro72b0d9a2013-03-21 02:32:24 -040036 * @mutex: mutex protecting the whole thing
Randy Dunlap0bf999f2020-02-09 19:36:14 -080037 * @rd_wait: reader wait point in case of empty pipe
38 * @wr_wait: writer wait point in case of full pipe
David Howells8cefc102019-11-15 13:30:32 +000039 * @head: The point of buffer production
40 * @tail: The point of buffer consumption
David Howellse7d553d2020-01-14 17:07:12 +000041 * @note_loss: The next read() should insert a data-lost message
David Howells6718b6f2019-10-16 16:47:32 +010042 * @max_usage: The maximum number of slots that may be used in the ring
David Howells8cefc102019-11-15 13:30:32 +000043 * @ring_size: total number of buffers (should be a power of 2)
David Howellsc73be612020-01-14 17:07:11 +000044 * @nr_accounted: The amount this pipe accounts for in user->pipe_bufs
Jens Axboe08457182007-06-12 20:51:32 +020045 * @tmp_page: cached released page
46 * @readers: number of current readers of this pipe
47 * @writers: number of current writers of this pipe
Masanari Iidae2278672014-02-18 22:54:36 +090048 * @files: number of struct file referring this pipe (protected by ->i_lock)
Jens Axboe08457182007-06-12 20:51:32 +020049 * @r_counter: reader counter
50 * @w_counter: writer counter
51 * @fasync_readers: reader side fasync
52 * @fasync_writers: writer side fasync
Jens Axboe08457182007-06-12 20:51:32 +020053 * @bufs: the circular array of pipe buffers
Willy Tarreau759c0112016-01-18 16:36:09 +010054 * @user: the user who created this pipe
David Howellsc73be612020-01-14 17:07:11 +000055 * @watch_queue: If this pipe is a watch_queue, this is the stuff for that
Jens Axboe08457182007-06-12 20:51:32 +020056 **/
Jens Axboe17374ff2007-06-04 15:03:12 +020057struct pipe_inode_info {
Al Viro72b0d9a2013-03-21 02:32:24 -040058 struct mutex mutex;
Linus Torvalds0ddad212019-12-09 09:48:27 -080059 wait_queue_head_t rd_wait, wr_wait;
David Howells8cefc102019-11-15 13:30:32 +000060 unsigned int head;
61 unsigned int tail;
David Howells6718b6f2019-10-16 16:47:32 +010062 unsigned int max_usage;
David Howells8cefc102019-11-15 13:30:32 +000063 unsigned int ring_size;
David Howellse7d553d2020-01-14 17:07:12 +000064#ifdef CONFIG_WATCH_QUEUE
65 bool note_loss;
66#endif
David Howellsc73be612020-01-14 17:07:11 +000067 unsigned int nr_accounted;
Jens Axboe17374ff2007-06-04 15:03:12 +020068 unsigned int readers;
69 unsigned int writers;
Al Viroba5bb142013-03-21 02:21:19 -040070 unsigned int files;
Jens Axboe17374ff2007-06-04 15:03:12 +020071 unsigned int r_counter;
72 unsigned int w_counter;
Jens Axboe35f3d142010-05-20 10:43:18 +020073 struct page *tmp_page;
Jens Axboe17374ff2007-06-04 15:03:12 +020074 struct fasync_struct *fasync_readers;
75 struct fasync_struct *fasync_writers;
Jens Axboe35f3d142010-05-20 10:43:18 +020076 struct pipe_buffer *bufs;
Willy Tarreau759c0112016-01-18 16:36:09 +010077 struct user_struct *user;
David Howellsc73be612020-01-14 17:07:11 +000078#ifdef CONFIG_WATCH_QUEUE
79 struct watch_queue *watch_queue;
80#endif
Jens Axboe17374ff2007-06-04 15:03:12 +020081};
82
Jens Axboef84d7512006-05-01 19:59:03 +020083/*
84 * Note on the nesting of these functions:
85 *
Jens Axboecac36bb02007-06-14 13:10:48 +020086 * ->confirm()
Christoph Hellwigc928f642020-05-20 17:58:16 +020087 * ->try_steal()
Jens Axboef84d7512006-05-01 19:59:03 +020088 *
Christoph Hellwigc928f642020-05-20 17:58:16 +020089 * That is, ->try_steal() must be called on a confirmed buffer. See below for
90 * the meaning of each operation. Also see the kerneldoc in fs/pipe.c for the
91 * pipe and generic variants of these hooks.
Jens Axboef84d7512006-05-01 19:59:03 +020092 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093struct pipe_buf_operations {
Jens Axboe08457182007-06-12 20:51:32 +020094 /*
Jens Axboe08457182007-06-12 20:51:32 +020095 * ->confirm() verifies that the data in the pipe buffer is there
96 * and that the contents are good. If the pages in the pipe belong
97 * to a file system, we may need to wait for IO completion in this
98 * hook. Returns 0 for good, or a negative error value in case of
Christoph Hellwigb8d9e7f2020-05-20 17:58:15 +020099 * error. If not present all pages are considered good.
Jens Axboe08457182007-06-12 20:51:32 +0200100 */
Jens Axboecac36bb02007-06-14 13:10:48 +0200101 int (*confirm)(struct pipe_inode_info *, struct pipe_buffer *);
Jens Axboe08457182007-06-12 20:51:32 +0200102
103 /*
104 * When the contents of this pipe buffer has been completely
105 * consumed by a reader, ->release() is called.
106 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 void (*release)(struct pipe_inode_info *, struct pipe_buffer *);
Jens Axboe08457182007-06-12 20:51:32 +0200108
109 /*
110 * Attempt to take ownership of the pipe buffer and its contents.
Christoph Hellwigc928f642020-05-20 17:58:16 +0200111 * ->try_steal() returns %true for success, in which case the contents
112 * of the pipe (the buf->page) is locked and now completely owned by the
113 * caller. The page may then be transferred to a different mapping, the
114 * most often used case is insertion into different file address space
115 * cache.
Jens Axboe08457182007-06-12 20:51:32 +0200116 */
Christoph Hellwigc928f642020-05-20 17:58:16 +0200117 bool (*try_steal)(struct pipe_inode_info *, struct pipe_buffer *);
Jens Axboe08457182007-06-12 20:51:32 +0200118
119 /*
120 * Get a reference to the pipe buffer.
121 */
Matthew Wilcox15fab632019-04-05 14:02:10 -0700122 bool (*get)(struct pipe_inode_info *, struct pipe_buffer *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123};
124
Miklos Szeredi7bf2d1d2016-09-27 10:45:12 +0200125/**
David Howells8cefc102019-11-15 13:30:32 +0000126 * pipe_empty - Return true if the pipe is empty
127 * @head: The pipe ring head pointer
128 * @tail: The pipe ring tail pointer
129 */
130static inline bool pipe_empty(unsigned int head, unsigned int tail)
131{
132 return head == tail;
133}
134
135/**
136 * pipe_occupancy - Return number of slots used in the pipe
137 * @head: The pipe ring head pointer
138 * @tail: The pipe ring tail pointer
139 */
140static inline unsigned int pipe_occupancy(unsigned int head, unsigned int tail)
141{
142 return head - tail;
143}
144
145/**
146 * pipe_full - Return true if the pipe is full
147 * @head: The pipe ring head pointer
148 * @tail: The pipe ring tail pointer
149 * @limit: The maximum amount of slots available.
150 */
151static inline bool pipe_full(unsigned int head, unsigned int tail,
152 unsigned int limit)
153{
154 return pipe_occupancy(head, tail) >= limit;
155}
156
157/**
158 * pipe_space_for_user - Return number of slots available to userspace
159 * @head: The pipe ring head pointer
160 * @tail: The pipe ring tail pointer
161 * @pipe: The pipe info structure
162 */
163static inline unsigned int pipe_space_for_user(unsigned int head, unsigned int tail,
164 struct pipe_inode_info *pipe)
165{
166 unsigned int p_occupancy, p_space;
167
168 p_occupancy = pipe_occupancy(head, tail);
David Howells6718b6f2019-10-16 16:47:32 +0100169 if (p_occupancy >= pipe->max_usage)
David Howells8cefc102019-11-15 13:30:32 +0000170 return 0;
171 p_space = pipe->ring_size - p_occupancy;
David Howells6718b6f2019-10-16 16:47:32 +0100172 if (p_space > pipe->max_usage)
173 p_space = pipe->max_usage;
David Howells8cefc102019-11-15 13:30:32 +0000174 return p_space;
175}
176
177/**
Miklos Szeredi7bf2d1d2016-09-27 10:45:12 +0200178 * pipe_buf_get - get a reference to a pipe_buffer
179 * @pipe: the pipe that the buffer belongs to
180 * @buf: the buffer to get a reference to
Matthew Wilcox15fab632019-04-05 14:02:10 -0700181 *
182 * Return: %true if the reference was successfully obtained.
Miklos Szeredi7bf2d1d2016-09-27 10:45:12 +0200183 */
Matthew Wilcox15fab632019-04-05 14:02:10 -0700184static inline __must_check bool pipe_buf_get(struct pipe_inode_info *pipe,
Miklos Szeredi7bf2d1d2016-09-27 10:45:12 +0200185 struct pipe_buffer *buf)
186{
Matthew Wilcox15fab632019-04-05 14:02:10 -0700187 return buf->ops->get(pipe, buf);
Miklos Szeredi7bf2d1d2016-09-27 10:45:12 +0200188}
189
Miklos Szeredia7796382016-09-27 10:45:12 +0200190/**
191 * pipe_buf_release - put a reference to a pipe_buffer
192 * @pipe: the pipe that the buffer belongs to
193 * @buf: the buffer to put a reference to
194 */
195static inline void pipe_buf_release(struct pipe_inode_info *pipe,
196 struct pipe_buffer *buf)
197{
198 const struct pipe_buf_operations *ops = buf->ops;
199
200 buf->ops = NULL;
201 ops->release(pipe, buf);
202}
203
Miklos Szeredifba597d2016-09-27 10:45:12 +0200204/**
205 * pipe_buf_confirm - verify contents of the pipe buffer
206 * @pipe: the pipe that the buffer belongs to
207 * @buf: the buffer to confirm
208 */
209static inline int pipe_buf_confirm(struct pipe_inode_info *pipe,
210 struct pipe_buffer *buf)
211{
Christoph Hellwigb8d9e7f2020-05-20 17:58:15 +0200212 if (!buf->ops->confirm)
213 return 0;
Miklos Szeredifba597d2016-09-27 10:45:12 +0200214 return buf->ops->confirm(pipe, buf);
215}
216
Miklos Szeredica76f5b2016-09-27 10:45:12 +0200217/**
Christoph Hellwigc928f642020-05-20 17:58:16 +0200218 * pipe_buf_try_steal - attempt to take ownership of a pipe_buffer
Miklos Szeredica76f5b2016-09-27 10:45:12 +0200219 * @pipe: the pipe that the buffer belongs to
220 * @buf: the buffer to attempt to steal
221 */
Christoph Hellwigc928f642020-05-20 17:58:16 +0200222static inline bool pipe_buf_try_steal(struct pipe_inode_info *pipe,
223 struct pipe_buffer *buf)
Miklos Szeredica76f5b2016-09-27 10:45:12 +0200224{
Christoph Hellwigc928f642020-05-20 17:58:16 +0200225 if (!buf->ops->try_steal)
226 return false;
227 return buf->ops->try_steal(pipe, buf);
Miklos Szeredica76f5b2016-09-27 10:45:12 +0200228}
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230/* Differs from PIPE_BUF in that PIPE_SIZE is the length of the actual
231 memory allocation, whereas PIPE_BUF makes atomicity guarantees. */
232#define PIPE_SIZE PAGE_SIZE
233
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200234/* Pipe lock and unlock operations */
235void pipe_lock(struct pipe_inode_info *);
236void pipe_unlock(struct pipe_inode_info *);
237void pipe_double_lock(struct pipe_inode_info *, struct pipe_inode_info *);
238
Eric Biggers4c2e4be2018-02-06 15:41:45 -0800239extern unsigned int pipe_max_size;
Willy Tarreau759c0112016-01-18 16:36:09 +0100240extern unsigned long pipe_user_pages_hard;
241extern unsigned long pipe_user_pages_soft;
Jens Axboeff9da692010-06-03 14:54:39 +0200242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243/* Drop the inode semaphore and wait for a pipe event, atomically */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200244void pipe_wait(struct pipe_inode_info *pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Al Viro7bee1302013-03-21 11:04:15 -0400246struct pipe_inode_info *alloc_pipe_info(void);
Al Viro4b8a8f12013-03-21 11:06:46 -0400247void free_pipe_info(struct pipe_inode_info *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Jens Axboef84d7512006-05-01 19:59:03 +0200249/* Generic pipe buffer ops functions */
Matthew Wilcox15fab632019-04-05 14:02:10 -0700250bool generic_pipe_buf_get(struct pipe_inode_info *, struct pipe_buffer *);
Christoph Hellwigc928f642020-05-20 17:58:16 +0200251bool generic_pipe_buf_try_steal(struct pipe_inode_info *, struct pipe_buffer *);
Miklos Szeredi68181732009-05-07 15:37:36 +0200252void generic_pipe_buf_release(struct pipe_inode_info *, struct pipe_buffer *);
Jens Axboef84d7512006-05-01 19:59:03 +0200253
Miklos Szeredi28a625c2014-01-22 19:36:57 +0100254extern const struct pipe_buf_operations nosteal_pipe_buf_ops;
255
David Howellsc73be612020-01-14 17:07:11 +0000256#ifdef CONFIG_WATCH_QUEUE
257unsigned long account_pipe_buffers(struct user_struct *user,
258 unsigned long old, unsigned long new);
259bool too_many_pipe_buffers_soft(unsigned long user_bufs);
260bool too_many_pipe_buffers_hard(unsigned long user_bufs);
261bool pipe_is_unprivileged_user(void);
262#endif
263
Jens Axboe35f3d142010-05-20 10:43:18 +0200264/* for F_SETPIPE_SZ and F_GETPIPE_SZ */
David Howellsc73be612020-01-14 17:07:11 +0000265#ifdef CONFIG_WATCH_QUEUE
266int pipe_resize_ring(struct pipe_inode_info *pipe, unsigned int nr_slots);
267#endif
Jens Axboe35f3d142010-05-20 10:43:18 +0200268long pipe_fcntl(struct file *, unsigned int, unsigned long arg);
David Howellsc73be612020-01-14 17:07:11 +0000269struct pipe_inode_info *get_pipe_info(struct file *file, bool for_splice);
Linus Torvaldsc66fb342010-11-28 14:09:57 -0800270
Al Viroe4fad8e2012-07-21 15:33:25 +0400271int create_pipe_files(struct file **, int);
Eric Biggers96e99be402018-02-06 15:42:00 -0800272unsigned int round_pipe_size(unsigned long size);
Al Viroe4fad8e2012-07-21 15:33:25 +0400273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274#endif