4 * Copyright (C) 2009-2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Ring Buffer ABI operations.
8 * Dual LGPL v2.1/GPL v2 license.
13 #include <ust/ring-buffer-abi.h>
15 static int put_ulong(unsigned long val
, unsigned long arg
)
17 return put_user(val
, (unsigned long __user
*)arg
);
21 static int compat_put_ulong(compat_ulong_t val
, unsigned long arg
)
23 return put_user(val
, (compat_ulong_t __user
*)compat_ptr(arg
));
28 * lib_ring_buffer_open - ring buffer open file operation
29 * @inode: opened inode
32 * Open implementation. Makes sure only one open instance of a buffer is
33 * done at a given moment.
35 int lib_ring_buffer_open(struct inode
*inode
, struct file
*file
)
37 struct lttng_ust_lib_ring_buffer
*buf
= inode
->i_private
;
40 ret
= lib_ring_buffer_open_read(buf
);
44 file
->private_data
= buf
;
45 ret
= nonseekable_open(inode
, file
);
51 lib_ring_buffer_release_read(buf
);
56 * lib_ring_buffer_release - ring buffer release file operation
57 * @inode: opened inode
60 * Release implementation.
62 int lib_ring_buffer_release(struct inode
*inode
, struct file
*file
)
64 struct lttng_ust_lib_ring_buffer
*buf
= file
->private_data
;
66 lib_ring_buffer_release_read(buf
);
72 * lib_ring_buffer_poll - ring buffer poll file operation
76 * Poll implementation.
78 unsigned int lib_ring_buffer_poll(struct file
*filp
, poll_table
*wait
)
80 unsigned int mask
= 0;
81 struct lttng_ust_lib_ring_buffer
*buf
= filp
->private_data
;
82 struct channel
*chan
= buf
->backend
.chan
;
83 const struct lttng_ust_lib_ring_buffer_config
*config
= chan
->backend
.config
;
84 int finalized
, disabled
;
86 if (filp
->f_mode
& FMODE_READ
) {
87 poll_wait_set_exclusive(wait
);
88 poll_wait(filp
, &buf
->read_wait
, wait
);
90 finalized
= lib_ring_buffer_is_finalized(config
, buf
);
91 disabled
= lib_ring_buffer_channel_is_disabled(chan
);
94 * lib_ring_buffer_is_finalized() contains a smp_rmb() ordering
95 * finalized load before offsets loads.
97 WARN_ON(atomic_long_read(&buf
->active_readers
) != 1);
102 if (subbuf_trunc(lib_ring_buffer_get_offset(config
, buf
), chan
)
103 - subbuf_trunc(lib_ring_buffer_get_consumed(config
, buf
), chan
)
109 * The memory barriers
110 * __wait_event()/wake_up_interruptible() take
111 * care of "raw_spin_is_locked" memory ordering.
113 if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
119 if (subbuf_trunc(lib_ring_buffer_get_offset(config
, buf
),
121 - subbuf_trunc(lib_ring_buffer_get_consumed(config
, buf
),
123 >= chan
->backend
.buf_size
)
124 return POLLPRI
| POLLRDBAND
;
126 return POLLIN
| POLLRDNORM
;
133 * lib_ring_buffer_ioctl - control ring buffer reader synchronization
139 * This ioctl implements commands necessary for producer/consumer
140 * and flight recorder reader interaction :
141 * RING_BUFFER_GET_NEXT_SUBBUF
142 * Get the next sub-buffer that can be read. It never blocks.
143 * RING_BUFFER_PUT_NEXT_SUBBUF
144 * Release the currently read sub-buffer.
145 * RING_BUFFER_GET_SUBBUF_SIZE
146 * returns the size of the current sub-buffer.
147 * RING_BUFFER_GET_MAX_SUBBUF_SIZE
148 * returns the maximum size for sub-buffers.
149 * RING_BUFFER_GET_NUM_SUBBUF
150 * returns the number of reader-visible sub-buffers in the per cpu
151 * channel (for mmap).
152 * RING_BUFFER_GET_MMAP_READ_OFFSET
153 * returns the offset of the subbuffer belonging to the reader.
154 * Should only be used for mmap clients.
156 long lib_ring_buffer_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
158 struct lttng_ust_lib_ring_buffer
*buf
= filp
->private_data
;
159 struct channel
*chan
= buf
->backend
.chan
;
160 const struct lttng_ust_lib_ring_buffer_config
*config
= chan
->backend
.config
;
162 if (lib_ring_buffer_channel_is_disabled(chan
))
166 case RING_BUFFER_SNAPSHOT
:
167 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
168 &buf
->prod_snapshot
);
169 case RING_BUFFER_SNAPSHOT_GET_CONSUMED
:
170 return put_ulong(buf
->cons_snapshot
, arg
);
171 case RING_BUFFER_SNAPSHOT_GET_PRODUCED
:
172 return put_ulong(buf
->prod_snapshot
, arg
);
173 case RING_BUFFER_GET_SUBBUF
:
175 unsigned long uconsume
;
178 ret
= get_user(uconsume
, (unsigned long __user
*) arg
);
180 return ret
; /* will return -EFAULT */
181 ret
= lib_ring_buffer_get_subbuf(buf
, uconsume
);
183 /* Set file position to zero at each successful "get" */
188 case RING_BUFFER_PUT_SUBBUF
:
189 lib_ring_buffer_put_subbuf(buf
);
192 case RING_BUFFER_GET_NEXT_SUBBUF
:
196 ret
= lib_ring_buffer_get_next_subbuf(buf
);
198 /* Set file position to zero at each successful "get" */
203 case RING_BUFFER_PUT_NEXT_SUBBUF
:
204 lib_ring_buffer_put_next_subbuf(buf
);
206 case RING_BUFFER_GET_SUBBUF_SIZE
:
207 return put_ulong(lib_ring_buffer_get_read_data_size(config
, buf
),
209 case RING_BUFFER_GET_PADDED_SUBBUF_SIZE
:
213 size
= lib_ring_buffer_get_read_data_size(config
, buf
);
214 size
= PAGE_ALIGN(size
);
215 return put_ulong(size
, arg
);
217 case RING_BUFFER_GET_MAX_SUBBUF_SIZE
:
218 return put_ulong(chan
->backend
.subbuf_size
, arg
);
219 case RING_BUFFER_GET_MMAP_LEN
:
221 unsigned long mmap_buf_len
;
223 if (config
->output
!= RING_BUFFER_MMAP
)
225 mmap_buf_len
= chan
->backend
.buf_size
;
226 if (chan
->backend
.extra_reader_sb
)
227 mmap_buf_len
+= chan
->backend
.subbuf_size
;
228 if (mmap_buf_len
> INT_MAX
)
230 return put_ulong(mmap_buf_len
, arg
);
232 case RING_BUFFER_GET_MMAP_READ_OFFSET
:
234 unsigned long sb_bindex
;
236 if (config
->output
!= RING_BUFFER_MMAP
)
238 sb_bindex
= subbuffer_id_get_index(config
,
239 buf
->backend
.buf_rsb
.id
);
240 return put_ulong(buf
->backend
.array
[sb_bindex
]->mmap_offset
,
243 case RING_BUFFER_FLUSH
:
244 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
, handle
);
252 long lib_ring_buffer_compat_ioctl(struct file
*filp
, unsigned int cmd
,
255 struct lttng_ust_lib_ring_buffer
*buf
= filp
->private_data
;
256 struct channel
*chan
= buf
->backend
.chan
;
257 const struct lttng_ust_lib_ring_buffer_config
*config
= chan
->backend
.config
;
259 if (lib_ring_buffer_channel_is_disabled(chan
))
263 case RING_BUFFER_SNAPSHOT
:
264 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
265 &buf
->prod_snapshot
);
266 case RING_BUFFER_SNAPSHOT_GET_CONSUMED
:
267 return compat_put_ulong(buf
->cons_snapshot
, arg
);
268 case RING_BUFFER_SNAPSHOT_GET_PRODUCED
:
269 return compat_put_ulong(buf
->prod_snapshot
, arg
);
270 case RING_BUFFER_GET_SUBBUF
:
273 unsigned long consume
;
276 ret
= get_user(uconsume
, (__u32 __user
*) arg
);
278 return ret
; /* will return -EFAULT */
279 consume
= buf
->cons_snapshot
;
280 consume
&= ~0xFFFFFFFFL
;
282 ret
= lib_ring_buffer_get_subbuf(buf
, consume
);
284 /* Set file position to zero at each successful "get" */
289 case RING_BUFFER_PUT_SUBBUF
:
290 lib_ring_buffer_put_subbuf(buf
);
293 case RING_BUFFER_GET_NEXT_SUBBUF
:
297 ret
= lib_ring_buffer_get_next_subbuf(buf
);
299 /* Set file position to zero at each successful "get" */
304 case RING_BUFFER_PUT_NEXT_SUBBUF
:
305 lib_ring_buffer_put_next_subbuf(buf
);
307 case RING_BUFFER_GET_SUBBUF_SIZE
:
309 unsigned long data_size
;
311 data_size
= lib_ring_buffer_get_read_data_size(config
, buf
);
312 if (data_size
> UINT_MAX
)
314 return put_ulong(data_size
, arg
);
316 case RING_BUFFER_GET_PADDED_SUBBUF_SIZE
:
320 size
= lib_ring_buffer_get_read_data_size(config
, buf
);
321 size
= PAGE_ALIGN(size
);
324 return put_ulong(size
, arg
);
326 case RING_BUFFER_GET_MAX_SUBBUF_SIZE
:
327 if (chan
->backend
.subbuf_size
> UINT_MAX
)
329 return put_ulong(chan
->backend
.subbuf_size
, arg
);
330 case RING_BUFFER_GET_MMAP_LEN
:
332 unsigned long mmap_buf_len
;
334 if (config
->output
!= RING_BUFFER_MMAP
)
336 mmap_buf_len
= chan
->backend
.buf_size
;
337 if (chan
->backend
.extra_reader_sb
)
338 mmap_buf_len
+= chan
->backend
.subbuf_size
;
339 if (mmap_buf_len
> UINT_MAX
)
341 return put_ulong(mmap_buf_len
, arg
);
343 case RING_BUFFER_GET_MMAP_READ_OFFSET
:
345 unsigned long sb_bindex
, read_offset
;
347 if (config
->output
!= RING_BUFFER_MMAP
)
349 sb_bindex
= subbuffer_id_get_index(config
,
350 buf
->backend
.buf_rsb
.id
);
351 read_offset
= buf
->backend
.array
[sb_bindex
]->mmap_offset
;
352 if (read_offset
> UINT_MAX
)
354 return put_ulong(read_offset
, arg
);
356 case RING_BUFFER_FLUSH
:
357 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
, handle
);
365 const struct file_operations lib_ring_buffer_file_operations
= {
366 .open
= lib_ring_buffer_open
,
367 .release
= lib_ring_buffer_release
,
368 .poll
= lib_ring_buffer_poll
,
369 .splice_read
= lib_ring_buffer_splice_read
,
370 .mmap
= lib_ring_buffer_mmap
,
371 .unlocked_ioctl
= lib_ring_buffer_ioctl
,
372 .llseek
= lib_ring_buffer_no_llseek
,
374 .compat_ioctl
= lib_ring_buffer_compat_ioctl
,