1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
|
/* SPDX-License-Identifier: (GPL-2.0-only OR LGPL-2.1-only)
*
* ringbuffer/backend.h
*
* Ring buffer backend (API).
*
* Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
*
* Credits to Steven Rostedt for proposing to use an extra-subbuffer owned by
* the reader in flight recorder mode.
*/
#ifndef _LIB_RING_BUFFER_BACKEND_H
#define _LIB_RING_BUFFER_BACKEND_H
#include <linux/types.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/wait.h>
#include <linux/poll.h>
#include <linux/list.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <wrapper/uaccess.h>
#include <lttng/probe-user.h>
/* Internal helpers */
#include <ringbuffer/backend_internal.h>
#include <ringbuffer/frontend_internal.h>
/* Ring buffer backend API */
/* Ring buffer backend access (read/write) */
extern size_t lib_ring_buffer_read(struct lttng_kernel_ring_buffer_backend *bufb,
size_t offset, void *dest, size_t len);
extern int __lib_ring_buffer_copy_to_user(struct lttng_kernel_ring_buffer_backend *bufb,
size_t offset, void __user *dest,
size_t len);
extern int lib_ring_buffer_read_cstr(struct lttng_kernel_ring_buffer_backend *bufb,
size_t offset, void *dest, size_t len);
extern unsigned long *
lib_ring_buffer_read_get_pfn(struct lttng_kernel_ring_buffer_backend *bufb, size_t offset,
void ***virt);
/*
* Return the address where a given offset is located.
* Should be used to get the current subbuffer header pointer. Given we know
* it's never on a page boundary, it's safe to write directly to this address,
* as long as the write is never bigger than a page size.
*/
extern void *
lib_ring_buffer_offset_address(struct lttng_kernel_ring_buffer_backend *bufb,
size_t offset);
extern void *
lib_ring_buffer_read_offset_address(struct lttng_kernel_ring_buffer_backend *bufb,
size_t offset);
/**
* lib_ring_buffer_write - write data to a buffer backend
* @config : ring buffer instance configuration
* @ctx: ring buffer context. (input arguments only)
* @src : source pointer to copy from
* @len : length of data to copy
*
* This function copies "len" bytes of data from a source pointer to a buffer
* backend, at the current context offset. This is more or less a buffer
* backend-specific memcpy() operation. Calls the slow path (_ring_buffer_write)
* if copy is crossing a page boundary.
*/
static inline __attribute__((always_inline))
void lib_ring_buffer_write(const struct lttng_kernel_ring_buffer_config *config,
struct lttng_kernel_ring_buffer_ctx *ctx,
const void *src, size_t len)
{
struct lttng_kernel_ring_buffer_backend *bufb = &ctx->priv.buf->backend;
struct channel_backend *chanb = &ctx->priv.chan->backend;
size_t index, bytes_left_in_page;
size_t offset = ctx->priv.buf_offset;
struct lttng_kernel_ring_buffer_backend_pages *backend_pages;
if (unlikely(!len))
return;
backend_pages =
lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
offset &= chanb->buf_size - 1;
index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
bytes_left_in_page = min_t(size_t, len, (-offset) & ~PAGE_MASK);
if (likely(bytes_left_in_page == len))
lib_ring_buffer_do_copy(config,
backend_pages->p[index].virt
+ (offset & ~PAGE_MASK),
src, len);
else
_lib_ring_buffer_write(bufb, offset, src, len);
ctx->priv.buf_offset += len;
}
/**
* lib_ring_buffer_memset - write len bytes of c to a buffer backend
* @config : ring buffer instance configuration
* @bufb : ring buffer backend
* @offset : offset within the buffer
* @c : the byte to copy
* @len : number of bytes to copy
*
* This function writes "len" bytes of "c" to a buffer backend, at a specific
* offset. This is more or less a buffer backend-specific memset() operation.
* Calls the slow path (_ring_buffer_memset) if write is crossing a page
* boundary.
*/
static inline
void lib_ring_buffer_memset(const struct lttng_kernel_ring_buffer_config *config,
struct lttng_kernel_ring_buffer_ctx *ctx, int c, size_t len)
{
struct lttng_kernel_ring_buffer_backend *bufb = &ctx->priv.buf->backend;
struct channel_backend *chanb = &ctx->priv.chan->backend;
size_t index, bytes_left_in_page;
size_t offset = ctx->priv.buf_offset;
struct lttng_kernel_ring_buffer_backend_pages *backend_pages;
if (unlikely(!len))
return;
backend_pages =
lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
offset &= chanb->buf_size - 1;
index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
bytes_left_in_page = min_t(size_t, len, (-offset) & ~PAGE_MASK);
if (likely(bytes_left_in_page == len))
lib_ring_buffer_do_memset(backend_pages->p[index].virt
+ (offset & ~PAGE_MASK),
c, len);
else
_lib_ring_buffer_memset(bufb, offset, c, len);
ctx->priv.buf_offset += len;
}
/*
* Copy up to @len string bytes from @src to @dest. Stop whenever a NULL
* terminating character is found in @src. Returns the number of bytes
* copied. Does *not* terminate @dest with NULL terminating character.
*/
static inline __attribute__((always_inline))
size_t lib_ring_buffer_do_strcpy(const struct lttng_kernel_ring_buffer_config *config,
char *dest, const char *src, size_t len)
{
size_t count;
for (count = 0; count < len; count++) {
char c;
/*
* Only read source character once, in case it is
* modified concurrently.
*/
c = LTTNG_READ_ONCE(src[count]);
if (!c)
break;
lib_ring_buffer_do_copy(config, &dest[count], &c, 1);
}
return count;
}
/*
* Copy up to @len string bytes from @src to @dest. Stop whenever a NULL
* terminating character is found in @src, or when a fault occurs.
* Returns the number of bytes copied. Does *not* terminate @dest with
* NULL terminating character.
*
* This function deals with userspace pointers, it should never be called
* directly without having the src pointer checked with access_ok()
* previously.
*/
static inline __attribute__((always_inline))
size_t lib_ring_buffer_do_strcpy_from_user_inatomic(const struct lttng_kernel_ring_buffer_config *config,
char *dest, const char __user *src, size_t len)
{
size_t count;
for (count = 0; count < len; count++) {
int ret;
char c;
ret = __copy_from_user_inatomic(&c, src + count, 1);
if (ret || !c)
break;
lib_ring_buffer_do_copy(config, &dest[count], &c, 1);
}
return count;
}
/**
* lib_ring_buffer_strcpy - write string data to a buffer backend
* @config : ring buffer instance configuration
* @ctx: ring buffer context. (input arguments only)
* @src : source pointer to copy from
* @len : length of data to copy
* @pad : character to use for padding
*
* This function copies @len - 1 bytes of string data from a source
* pointer to a buffer backend, followed by a terminating '\0'
* character, at the current context offset. This is more or less a
* buffer backend-specific strncpy() operation. If a terminating '\0'
* character is found in @src before @len - 1 characters are copied, pad
* the buffer with @pad characters (e.g. '#'). Calls the slow path
* (_ring_buffer_strcpy) if copy is crossing a page boundary.
*/
static inline
void lib_ring_buffer_strcpy(const struct lttng_kernel_ring_buffer_config *config,
struct lttng_kernel_ring_buffer_ctx *ctx,
const char *src, size_t len, int pad)
{
struct lttng_kernel_ring_buffer_backend *bufb = &ctx->priv.buf->backend;
struct channel_backend *chanb = &ctx->priv.chan->backend;
size_t index, bytes_left_in_page;
size_t offset = ctx->priv.buf_offset;
struct lttng_kernel_ring_buffer_backend_pages *backend_pages;
if (unlikely(!len))
return;
backend_pages =
lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
offset &= chanb->buf_size - 1;
index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
bytes_left_in_page = min_t(size_t, len, (-offset) & ~PAGE_MASK);
if (likely(bytes_left_in_page == len)) {
size_t count;
count = lib_ring_buffer_do_strcpy(config,
backend_pages->p[index].virt
+ (offset & ~PAGE_MASK),
src, len - 1);
offset += count;
/* Padding */
if (unlikely(count < len - 1)) {
size_t pad_len = len - 1 - count;
lib_ring_buffer_do_memset(backend_pages->p[index].virt
+ (offset & ~PAGE_MASK),
pad, pad_len);
offset += pad_len;
}
/* Ending '\0' */
lib_ring_buffer_do_memset(backend_pages->p[index].virt
+ (offset & ~PAGE_MASK),
'\0', 1);
} else {
_lib_ring_buffer_strcpy(bufb, offset, src, len, pad);
}
ctx->priv.buf_offset += len;
}
/**
* lib_ring_buffer_pstrcpy - write kernel C-string (input) to a buffer backend P-string
* @config : ring buffer instance configuration
* @ctx: ring buffer context. (input arguments only)
* @src : source pointer to copy from
* @len : length of data to copy
* @pad : character to use for padding
*
* This function copies up to @len bytes of data from a source pointer
* to a Pascal String into the buffer backend. If a terminating '\0'
* character is found in @src before @len characters are copied, pad the
* buffer with @pad characters (e.g. '\0').
*
* The length of the pascal strings in the ring buffer is explicit: it
* is either the array or sequence length.
*/
static inline
void lib_ring_buffer_pstrcpy(const struct lttng_kernel_ring_buffer_config *config,
struct lttng_kernel_ring_buffer_ctx *ctx,
const char *src, size_t len, char pad)
__attribute__((always_inline));
static inline
void lib_ring_buffer_pstrcpy(const struct lttng_kernel_ring_buffer_config *config,
struct lttng_kernel_ring_buffer_ctx *ctx,
const char *src, size_t len, char pad)
{
struct lttng_kernel_ring_buffer_backend *bufb = &ctx->priv.buf->backend;
struct channel_backend *chanb = &ctx->priv.chan->backend;
size_t index, bytes_left_in_page;
size_t offset = ctx->priv.buf_offset;
struct lttng_kernel_ring_buffer_backend_pages *backend_pages;
if (unlikely(!len))
return;
backend_pages =
lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
offset &= chanb->buf_size - 1;
index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
bytes_left_in_page = min_t(size_t, len, (-offset) & ~PAGE_MASK);
if (likely(bytes_left_in_page == len)) {
size_t count;
count = lib_ring_buffer_do_strcpy(config,
backend_pages->p[index].virt
+ (offset & ~PAGE_MASK),
src, len);
offset += count;
/* Padding */
if (unlikely(count < len)) {
size_t pad_len = len - count;
lib_ring_buffer_do_memset(backend_pages->p[index].virt
+ (offset & ~PAGE_MASK),
pad, pad_len);
offset += pad_len;
}
} else {
_lib_ring_buffer_pstrcpy(bufb, offset, src, len, pad);
}
ctx->priv.buf_offset += len;
}
/**
* lib_ring_buffer_copy_from_user_inatomic - write userspace data to a buffer backend
* @config : ring buffer instance configuration
* @ctx: ring buffer context. (input arguments only)
* @src : userspace source pointer to copy from
* @len : length of data to copy
*
* This function copies "len" bytes of data from a userspace pointer to a
* buffer backend, at the current context offset. This is more or less a buffer
* backend-specific memcpy() operation. Calls the slow path
* (_ring_buffer_write_from_user_inatomic) if copy is crossing a page boundary.
* Disable the page fault handler to ensure we never try to take the mmap_sem.
*/
static inline __attribute__((always_inline))
void lib_ring_buffer_copy_from_user_inatomic(const struct lttng_kernel_ring_buffer_config *config,
struct lttng_kernel_ring_buffer_ctx *ctx,
const void __user *src, size_t len)
{
struct lttng_kernel_ring_buffer_backend *bufb = &ctx->priv.buf->backend;
struct channel_backend *chanb = &ctx->priv.chan->backend;
size_t index, bytes_left_in_page;
size_t offset = ctx->priv.buf_offset;
struct lttng_kernel_ring_buffer_backend_pages *backend_pages;
unsigned long ret;
if (unlikely(!len))
return;
backend_pages =
lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
offset &= chanb->buf_size - 1;
index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
bytes_left_in_page = min_t(size_t, len, (-offset) & ~PAGE_MASK);
if (unlikely(!lttng_access_ok(VERIFY_READ, src, len)))
goto fill_buffer;
pagefault_disable();
if (likely(bytes_left_in_page == len)) {
ret = lib_ring_buffer_do_copy_from_user_inatomic(
backend_pages->p[index].virt + (offset & ~PAGE_MASK),
src, len);
if (unlikely(ret > 0)) {
/* Copy failed. */
goto fill_buffer_enable_pf;
}
} else {
_lib_ring_buffer_copy_from_user_inatomic(bufb, offset, src, len);
}
pagefault_enable();
ctx->priv.buf_offset += len;
return;
fill_buffer_enable_pf:
pagefault_enable();
fill_buffer:
/*
* In the error path we call the slow path version to avoid
* the pollution of static inline code.
*/
_lib_ring_buffer_memset(bufb, offset, 0, len);
ctx->priv.buf_offset += len;
}
/**
* lib_ring_buffer_strcpy_from_user_inatomic - write userspace string data to a buffer backend
* @config : ring buffer instance configuration
* @ctx: ring buffer context (input arguments only)
* @src : userspace source pointer to copy from
* @len : length of data to copy
* @pad : character to use for padding
*
* This function copies @len - 1 bytes of string data from a userspace
* source pointer to a buffer backend, followed by a terminating '\0'
* character, at the current context offset. This is more or less a
* buffer backend-specific strncpy() operation. If a terminating '\0'
* character is found in @src before @len - 1 characters are copied, pad
* the buffer with @pad characters (e.g. '#'). Calls the slow path
* (_ring_buffer_strcpy_from_user_inatomic) if copy is crossing a page
* boundary. Disable the page fault handler to ensure we never try to
* take the mmap_sem.
*/
static inline
void lib_ring_buffer_strcpy_from_user_inatomic(const struct lttng_kernel_ring_buffer_config *config,
struct lttng_kernel_ring_buffer_ctx *ctx,
const void __user *src, size_t len, int pad)
{
struct lttng_kernel_ring_buffer_backend *bufb = &ctx->priv.buf->backend;
struct channel_backend *chanb = &ctx->priv.chan->backend;
size_t index, bytes_left_in_page;
size_t offset = ctx->priv.buf_offset;
struct lttng_kernel_ring_buffer_backend_pages *backend_pages;
if (unlikely(!len))
return;
backend_pages =
lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
offset &= chanb->buf_size - 1;
index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
bytes_left_in_page = min_t(size_t, len, (-offset) & ~PAGE_MASK);
if (unlikely(!lttng_access_ok(VERIFY_READ, src, len)))
goto fill_buffer;
pagefault_disable();
if (likely(bytes_left_in_page == len)) {
size_t count;
count = lib_ring_buffer_do_strcpy_from_user_inatomic(config,
backend_pages->p[index].virt
+ (offset & ~PAGE_MASK),
src, len - 1);
offset += count;
/* Padding */
if (unlikely(count < len - 1)) {
size_t pad_len = len - 1 - count;
lib_ring_buffer_do_memset(backend_pages->p[index].virt
+ (offset & ~PAGE_MASK),
pad, pad_len);
offset += pad_len;
}
/* Ending '\0' */
lib_ring_buffer_do_memset(backend_pages->p[index].virt
+ (offset & ~PAGE_MASK),
'\0', 1);
} else {
_lib_ring_buffer_strcpy_from_user_inatomic(bufb, offset, src,
len, pad);
}
pagefault_enable();
ctx->priv.buf_offset += len;
return;
fill_buffer:
/*
* In the error path we call the slow path version to avoid
* the pollution of static inline code.
*/
_lib_ring_buffer_memset(bufb, offset, pad, len - 1);
offset += len - 1;
_lib_ring_buffer_memset(bufb, offset, '\0', 1);
ctx->priv.buf_offset += len;
}
/**
* lib_ring_buffer_pstrcpy_from_user_inatomic - write user-space C-string (input) to a buffer backend P-string
* @config : ring buffer instance configuration
* @ctx: ring buffer context. (input arguments only)
* @src : source pointer to copy from
* @len : length of data to copy
* @pad : character to use for padding
*
* This function copies up to @len bytes of data from a source pointer
* to a Pascal String into the buffer backend. If a terminating '\0'
* character is found in @src before @len characters are copied, pad the
* buffer with @pad characters (e.g. '\0').
*
* The length of the pascal strings in the ring buffer is explicit: it
* is either the array or sequence length.
*/
static inline
void lib_ring_buffer_pstrcpy_from_user_inatomic(const struct lttng_kernel_ring_buffer_config *config,
struct lttng_kernel_ring_buffer_ctx *ctx,
const char __user *src, size_t len, char pad)
__attribute__((always_inline));
static inline
void lib_ring_buffer_pstrcpy_from_user_inatomic(const struct lttng_kernel_ring_buffer_config *config,
struct lttng_kernel_ring_buffer_ctx *ctx,
const char __user *src, size_t len, char pad)
{
struct lttng_kernel_ring_buffer_backend *bufb = &ctx->priv.buf->backend;
struct channel_backend *chanb = &ctx->priv.chan->backend;
size_t index, bytes_left_in_page;
size_t offset = ctx->priv.buf_offset;
struct lttng_kernel_ring_buffer_backend_pages *backend_pages;
if (unlikely(!len))
return;
backend_pages =
lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
offset &= chanb->buf_size - 1;
index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
bytes_left_in_page = min_t(size_t, len, (-offset) & ~PAGE_MASK);
if (unlikely(!lttng_access_ok(VERIFY_READ, src, len)))
goto fill_buffer;
pagefault_disable();
if (likely(bytes_left_in_page == len)) {
size_t count;
count = lib_ring_buffer_do_strcpy_from_user_inatomic(config,
backend_pages->p[index].virt
+ (offset & ~PAGE_MASK),
src, len);
offset += count;
/* Padding */
if (unlikely(count < len)) {
size_t pad_len = len - count;
lib_ring_buffer_do_memset(backend_pages->p[index].virt
+ (offset & ~PAGE_MASK),
pad, pad_len);
offset += pad_len;
}
} else {
_lib_ring_buffer_pstrcpy_from_user_inatomic(bufb, offset, src, len, pad);
}
ctx->priv.buf_offset += len;
pagefault_enable();
return;
fill_buffer:
/*
* In the error path we call the slow path version to avoid
* the pollution of static inline code.
*/
_lib_ring_buffer_memset(bufb, offset, pad, len);
ctx->priv.buf_offset += len;
}
/*
* This accessor counts the number of unread records in a buffer.
* It only provides a consistent value if no reads not writes are performed
* concurrently.
*/
static inline
unsigned long lib_ring_buffer_get_records_unread(
const struct lttng_kernel_ring_buffer_config *config,
struct lttng_kernel_ring_buffer *buf)
{
struct lttng_kernel_ring_buffer_backend *bufb = &buf->backend;
struct lttng_kernel_ring_buffer_backend_pages *pages;
unsigned long records_unread = 0, sb_bindex, id;
unsigned int i;
for (i = 0; i < bufb->chan->backend.num_subbuf; i++) {
id = bufb->buf_wsb[i].id;
sb_bindex = subbuffer_id_get_index(config, id);
pages = bufb->array[sb_bindex];
records_unread += v_read(config, &pages->records_unread);
}
if (config->mode == RING_BUFFER_OVERWRITE) {
id = bufb->buf_rsb.id;
sb_bindex = subbuffer_id_get_index(config, id);
pages = bufb->array[sb_bindex];
records_unread += v_read(config, &pages->records_unread);
}
return records_unread;
}
/*
* We use __copy_from_user_inatomic to copy userspace data after
* checking with access_ok() and disabling page faults.
*
* Return 0 if OK, nonzero on error.
*/
static inline
unsigned long lib_ring_buffer_copy_from_user_check_nofault(void *dest,
const void __user *src,
unsigned long len)
{
return lttng_copy_from_user_check_nofault(dest, src, len);
}
#endif /* _LIB_RING_BUFFER_BACKEND_H */
|