1 // SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note
2 /*
3 *
4 * (C) COPYRIGHT 2019-2022 ARM Limited. All rights reserved.
5 *
6 * This program is free software and is provided to you under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation, and any use by you of this program is subject to the terms
9 * of such GNU license.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you can access it online at
18 * http://www.gnu.org/licenses/gpl-2.0.html.
19 *
20 */
21
22 /*
23 * mali_kbase_kinstr_jm.c
24 * Kernel driver public interface to job manager atom tracing
25 */
26
27 #include "mali_kbase_kinstr_jm.h"
28 #include <uapi/gpu/arm/bifrost/mali_kbase_kinstr_jm_reader.h>
29
30 #include "mali_kbase.h"
31 #include "mali_kbase_linux.h"
32
33 #include <backend/gpu/mali_kbase_jm_rb.h>
34
35 #include <asm/barrier.h>
36 #include <linux/anon_inodes.h>
37 #include <linux/circ_buf.h>
38 #include <linux/fs.h>
39 #include <linux/kref.h>
40 #include <linux/ktime.h>
41 #include <linux/log2.h>
42 #include <linux/mutex.h>
43 #include <linux/rculist_bl.h>
44 #include <linux/poll.h>
45 #include <linux/slab.h>
46 #include <linux/spinlock.h>
47 #include <linux/version.h>
48 #include <linux/version_compat_defs.h>
49 #include <linux/wait.h>
50
51 /* Explicitly include epoll header for old kernels. Not required from 4.16. */
52 #if KERNEL_VERSION(4, 16, 0) > LINUX_VERSION_CODE
53 #include <uapi/linux/eventpoll.h>
54 #endif
55
56 /* Define static_assert().
57 *
58 * The macro was introduced in kernel 5.1. But older vendor kernels may define
59 * it too.
60 */
61 #if KERNEL_VERSION(5, 1, 0) <= LINUX_VERSION_CODE
62 #include <linux/build_bug.h>
63 #elif !defined(static_assert)
64 // Stringify the expression if no message is given.
65 #define static_assert(e, ...) __static_assert(e, #__VA_ARGS__, #e)
66 #define __static_assert(e, msg, ...) _Static_assert(e, msg)
67 #endif
68
69 /* The module printing prefix */
70 #define PR_ "mali_kbase_kinstr_jm: "
71
72 /* Allows us to perform ASM goto for the tracing
73 * https://www.kernel.org/doc/Documentation/static-keys.txt
74 */
75 DEFINE_STATIC_KEY_FALSE(basep_kinstr_jm_reader_static_key);
76
77 #define KBASE_KINSTR_JM_VERSION 2
78
79 /**
80 * struct kbase_kinstr_jm - The context for the kernel job manager atom tracing
81 * @readers: a bitlocked list of opened readers. Readers are attached to the
82 * private data of a file descriptor that the user opens with the
83 * KBASE_IOCTL_KINSTR_JM_FD IO control call.
84 * @refcount: reference count for the context. Any reader will have a link
85 * back to the context so that they can remove themselves from the
86 * list.
87 *
88 * This is opaque outside this compilation unit
89 */
90 struct kbase_kinstr_jm {
91 struct hlist_bl_head readers;
92 struct kref refcount;
93 };
94
95 /**
96 * struct kbase_kinstr_jm_atom_state_change - Represents an atom changing to a
97 * new state
98 * @timestamp: Raw monotonic nanoseconds of the state change
99 * @state: The state that the atom has moved to
100 * @atom: The atom number that has changed state
101 * @flags: Flags associated with the state change. See
102 * KBASE_KINSTR_JM_ATOM_STATE_FLAG_* defines.
103 * @reserved: Reserved for future use.
104 * @data: Extra data for the state change. Active member depends on state.
105 * @data.start: Extra data for the state change. Active member depends on
106 * state.
107 * @data.start.slot: Extra data for the state change. Active member depends on
108 * state.
109 * @data.padding: Padding
110 *
111 * We can add new fields to the structure and old user code will gracefully
112 * ignore the new fields.
113 *
114 * We can change the size of the structure and old user code will gracefully
115 * skip over the new size via `struct kbase_kinstr_jm_fd_out->size`.
116 *
117 * If we remove fields, the version field in `struct
118 * kbase_kinstr_jm_fd_out->version` will be incremented and old user code will
119 * gracefully fail and tell the user that the kernel API is too new and has
120 * backwards-incompatible changes. Note that one userspace can opt to handle
121 * multiple kernel major versions of the structure.
122 *
123 * If we need to change the _meaning_ of one of the fields, i.e. the state
124 * machine has had a incompatible change, we can keep the same members in the
125 * structure and update the version as above. User code will no longer
126 * recognise that it has the supported field and can gracefully explain to the
127 * user that the kernel API is no longer supported.
128 *
129 * When making changes to this structure, make sure they are either:
130 * - additions to the end (for minor version bumps (i.e. only a size increase))
131 * such that the layout of existing fields doesn't change, or;
132 * - update the version reported to userspace so that it can fail explicitly.
133 */
134 struct kbase_kinstr_jm_atom_state_change {
135 u64 timestamp;
136 s8 state; /* enum kbase_kinstr_jm_reader_atom_state */
137 u8 atom;
138 u8 flags;
139 u8 reserved[1];
140 /* Tagged union based on state. Ensure members are aligned correctly! */
141 union {
142 struct {
143 u8 slot;
144 } start;
145 u8 padding[4];
146 } data;
147 };
148 static_assert(
149 ((1 << 8 * sizeof(((struct kbase_kinstr_jm_atom_state_change *)0)->state)) - 1) >=
150 KBASE_KINSTR_JM_READER_ATOM_STATE_COUNT);
151
152 #define KBASE_KINSTR_JM_ATOM_STATE_FLAG_OVERFLOW BIT(0)
153
154 /**
155 * struct reader_changes - The circular buffer of kernel atom state changes
156 * @data: The allocated buffer. This is allocated when the user requests
157 * the reader file descriptor. It is released when the user calls
158 * close() on the fd. When accessing this, lock the producer spin
159 * lock to prevent races on the allocated memory. The consume lock
160 * does not need to be held because newly-inserted data will always
161 * be outside the currenly-read range.
162 * @producer: The producing spinlock which allows us to push changes into the
163 * buffer at the same time as a user read occurring. This needs to
164 * be locked when saving/restoring the IRQ because we can receive an
165 * interrupt from the GPU when an atom completes. The CPU could have
166 * a task preempted that is holding this lock.
167 * @consumer: The consuming mutex which locks around the user read().
168 * Must be held when updating the tail of the circular buffer.
169 * @head: The head of the circular buffer. Can be used with Linux @c CIRC_
170 * helpers. The producer should lock and update this with an SMP
171 * store when a new change lands. The consumer can read with an
172 * SMP load. This allows the producer to safely insert new changes
173 * into the circular buffer.
174 * @tail: The tail of the circular buffer. Can be used with Linux @c CIRC_
175 * helpers. The producer should do a READ_ONCE load and the consumer
176 * should SMP store.
177 * @size: The number of changes that are allowed in @c data. Can be used
178 * with Linux @c CIRC_ helpers. Will always be a power of two. The
179 * producer lock should be held when updating this and stored with
180 * an SMP release memory barrier. This means that the consumer can
181 * do an SMP load.
182 * @threshold: The number of changes above which threads polling on the reader
183 * file descriptor will be woken up.
184 */
185 struct reader_changes {
186 struct kbase_kinstr_jm_atom_state_change *data;
187 spinlock_t producer;
188 struct mutex consumer;
189 u32 head;
190 u32 tail;
191 u32 size;
192 u32 threshold;
193 };
194
195 /**
196 * reader_changes_is_valid_size() - Determines if requested changes buffer size
197 * is valid.
198 * @size: The requested memory size
199 *
200 * We have a constraint that the underlying physical buffer must be a
201 * power of two so that we can use the efficient circular buffer helpers that
202 * the kernel provides. It also needs to be representable within a u32.
203 *
204 * Return:
205 * * true - the size is valid
206 * * false - the size is invalid
207 */
reader_changes_is_valid_size(const size_t size)208 static inline bool reader_changes_is_valid_size(const size_t size)
209 {
210 const size_t elem_size = sizeof(*((struct reader_changes *)0)->data);
211 const size_t size_size = sizeof(((struct reader_changes *)0)->size);
212 const size_t size_max = (1ull << (size_size * 8)) - 1;
213
214 return is_power_of_2(size) && /* Is a power of two */
215 ((size / elem_size) <= size_max); /* Small enough */
216 }
217
218 /**
219 * reader_changes_init() - Initializes the reader changes and allocates the
220 * changes buffer
221 * @changes: The context pointer, must point to a zero-inited allocated reader
222 * changes structure. We may support allocating the structure in the
223 * future.
224 * @size: The requested changes buffer size
225 *
226 * Return:
227 * (0, U16_MAX] - the number of data elements allocated
228 * -ERANGE - the requested memory size was invalid
229 * -ENOMEM - could not allocate the memory
230 */
reader_changes_init(struct reader_changes * const changes,const size_t size)231 static int reader_changes_init(struct reader_changes *const changes,
232 const size_t size)
233 {
234 BUILD_BUG_ON((PAGE_SIZE % sizeof(*changes->data)) != 0);
235
236 if (!reader_changes_is_valid_size(size)) {
237 pr_warn(PR_ "invalid size %zu\n", size);
238 return -ERANGE;
239 }
240
241 changes->data = vmalloc(size);
242 if (!changes->data)
243 return -ENOMEM;
244
245 spin_lock_init(&changes->producer);
246 mutex_init(&changes->consumer);
247
248 changes->size = size / sizeof(*changes->data);
249 changes->threshold = min(((size_t)(changes->size)) / 4,
250 ((size_t)(PAGE_SIZE)) / sizeof(*changes->data));
251
252 return changes->size;
253 }
254
255 /**
256 * reader_changes_term() - Cleans up a reader changes structure
257 * @changes: The context to clean up
258 *
259 * Releases the allocated state changes memory
260 */
reader_changes_term(struct reader_changes * const changes)261 static void reader_changes_term(struct reader_changes *const changes)
262 {
263 struct kbase_kinstr_jm_atom_state_change *data = NULL;
264 unsigned long irq;
265
266 /*
267 * Although changes->data is used on the consumer side, too, no active
268 * consumer is possible by the time we clean up the reader changes, so
269 * no need to take the consumer lock. However, we do need the producer
270 * lock because the list removal can race with list traversal.
271 */
272 spin_lock_irqsave(&changes->producer, irq);
273 swap(changes->data, data);
274 spin_unlock_irqrestore(&changes->producer, irq);
275
276 mutex_destroy(&changes->consumer);
277 vfree(data);
278 }
279
280 /**
281 * reader_changes_count_locked() - Retrieves the count of state changes from the
282 * tail to the physical end of the buffer
283 * @changes: The state changes context
284 *
285 * The consumer mutex must be held. Uses the CIRC_CNT_TO_END macro to
286 * determine the count, so there may be more items. However, that's the maximum
287 * number that can be read in one contiguous read.
288 *
289 * Return: the number of changes in the circular buffer until the end of the
290 * allocation
291 */
reader_changes_count_locked(struct reader_changes * const changes)292 static u32 reader_changes_count_locked(struct reader_changes *const changes)
293 {
294 u32 head;
295
296 lockdep_assert_held_once(&changes->consumer);
297
298 head = smp_load_acquire(&changes->head);
299
300 return CIRC_CNT_TO_END(head, changes->tail, changes->size);
301 }
302
303 /**
304 * reader_changes_count() - Retrieves the count of state changes from the
305 * tail to the physical end of the buffer
306 * @changes: The state changes context
307 *
308 * Return: the number of changes in the circular buffer until the end of the
309 * allocation
310 */
reader_changes_count(struct reader_changes * const changes)311 static u32 reader_changes_count(struct reader_changes *const changes)
312 {
313 u32 ret;
314
315 mutex_lock(&changes->consumer);
316 ret = reader_changes_count_locked(changes);
317 mutex_unlock(&changes->consumer);
318 return ret;
319 }
320
321 /**
322 * reader_changes_push() - Pushes a change into the reader circular buffer.
323 * @changes: The buffer to insert the change into
324 * @change: Kernel atom change to insert
325 * @wait_queue: The queue to be kicked when changes should be read from
326 * userspace. Kicked when a threshold is reached or there is
327 * overflow.
328 */
reader_changes_push(struct reader_changes * const changes,const struct kbase_kinstr_jm_atom_state_change * const change,wait_queue_head_t * const wait_queue)329 static void reader_changes_push(
330 struct reader_changes *const changes,
331 const struct kbase_kinstr_jm_atom_state_change *const change,
332 wait_queue_head_t *const wait_queue)
333 {
334 u32 head, tail, size, space;
335 unsigned long irq;
336 struct kbase_kinstr_jm_atom_state_change *data;
337
338 spin_lock_irqsave(&changes->producer, irq);
339
340 /* We may be called for a reader_changes that's awaiting cleanup. */
341 data = changes->data;
342 if (!data)
343 goto unlock;
344
345 size = changes->size;
346 head = changes->head;
347 tail = smp_load_acquire(&changes->tail);
348
349 space = CIRC_SPACE(head, tail, size);
350 if (space >= 1) {
351 data[head] = *change;
352 if (space == 1) {
353 data[head].flags |=
354 KBASE_KINSTR_JM_ATOM_STATE_FLAG_OVERFLOW;
355 pr_warn(PR_ "overflow of circular buffer\n");
356 }
357 smp_store_release(&changes->head, (head + 1) & (size - 1));
358 }
359
360 /* Wake for either overflow or over-threshold cases. */
361 if (CIRC_CNT(head + 1, tail, size) >= changes->threshold)
362 wake_up_interruptible(wait_queue);
363
364 unlock:
365 spin_unlock_irqrestore(&changes->producer, irq);
366 }
367
368 /**
369 * struct reader - Allows the kernel state changes to be read by user space.
370 * @node: The node in the @c readers locked list
371 * @rcu_head: storage for the RCU callback to free this reader (see kfree_rcu)
372 * @changes: The circular buffer of user changes
373 * @wait_queue: A wait queue for poll
374 * @context: a pointer to the parent context that created this reader. Can be
375 * used to remove the reader from the list of readers. Reference
376 * counted.
377 *
378 * The reader is a circular buffer in kernel space. State changes are pushed
379 * into the buffer. The flow from user space is:
380 *
381 * * Request file descriptor with KBASE_IOCTL_KINSTR_JM_FD. This will
382 * allocate the kernel side circular buffer with a size specified in the
383 * ioctl argument.
384 * * The user will then poll the file descriptor for data
385 * * Upon receiving POLLIN, perform a read() on the file descriptor to get
386 * the data out.
387 * * The buffer memory will be freed when the file descriptor is closed
388 */
389 struct reader {
390 struct hlist_bl_node node;
391 struct rcu_head rcu_head;
392 struct reader_changes changes;
393 wait_queue_head_t wait_queue;
394 struct kbase_kinstr_jm *context;
395 };
396
397 static struct kbase_kinstr_jm *
398 kbase_kinstr_jm_ref_get(struct kbase_kinstr_jm *const ctx);
399 static void kbase_kinstr_jm_ref_put(struct kbase_kinstr_jm *const ctx);
400 static int kbase_kinstr_jm_readers_add(struct kbase_kinstr_jm *const ctx,
401 struct reader *const reader);
402 static void kbase_kinstr_jm_readers_del(struct kbase_kinstr_jm *const ctx,
403 struct reader *const reader);
404
405 /**
406 * reader_term() - Terminate a instrumentation job manager reader context.
407 * @reader: Pointer to context to be terminated.
408 */
reader_term(struct reader * const reader)409 static void reader_term(struct reader *const reader)
410 {
411 if (!reader)
412 return;
413
414 kbase_kinstr_jm_readers_del(reader->context, reader);
415 reader_changes_term(&reader->changes);
416 kbase_kinstr_jm_ref_put(reader->context);
417
418 kfree_rcu(reader, rcu_head);
419 }
420
421 /**
422 * reader_init() - Initialise a instrumentation job manager reader context.
423 * @out_reader: Non-NULL pointer to where the pointer to the created context
424 * will be stored on success.
425 * @ctx: the pointer to the parent context. Reference count will be
426 * increased if initialization is successful
427 * @num_changes: The number of changes to allocate a buffer for
428 *
429 * Return: 0 on success, else error code.
430 */
reader_init(struct reader ** const out_reader,struct kbase_kinstr_jm * const ctx,size_t const num_changes)431 static int reader_init(struct reader **const out_reader,
432 struct kbase_kinstr_jm *const ctx,
433 size_t const num_changes)
434 {
435 struct reader *reader = NULL;
436 const size_t change_size = sizeof(struct kbase_kinstr_jm_atom_state_change);
437 int status;
438
439 if (!out_reader || !ctx || !num_changes)
440 return -EINVAL;
441
442 reader = kzalloc(sizeof(*reader), GFP_KERNEL);
443 if (!reader)
444 return -ENOMEM;
445
446 INIT_HLIST_BL_NODE(&reader->node);
447 init_waitqueue_head(&reader->wait_queue);
448
449 reader->context = kbase_kinstr_jm_ref_get(ctx);
450
451 status = reader_changes_init(&reader->changes, num_changes * change_size);
452 if (status < 0)
453 goto fail;
454
455 status = kbase_kinstr_jm_readers_add(ctx, reader);
456 if (status < 0)
457 goto fail;
458
459 *out_reader = reader;
460
461 return 0;
462
463 fail:
464 kbase_kinstr_jm_ref_put(reader->context);
465 kfree(reader);
466 return status;
467 }
468
469 /**
470 * reader_release() - Invoked when the reader file descriptor is released
471 * @node: The inode that the file descriptor that the file corresponds to. In
472 * our case our reader file descriptor is backed by an anonymous node so
473 * not much is in this.
474 * @file: the file data. Our reader context is held in the private data
475 * Return: zero on success
476 */
reader_release(struct inode * const node,struct file * const file)477 static int reader_release(struct inode *const node, struct file *const file)
478 {
479 struct reader *const reader = file->private_data;
480
481 reader_term(reader);
482 file->private_data = NULL;
483
484 return 0;
485 }
486
487 /**
488 * reader_changes_copy_to_user() - Copy any changes from a changes structure to
489 * the user-provided buffer.
490 * @changes: The changes structure from which to copy.
491 * @buffer: The user buffer to copy the data to.
492 * @buffer_size: The number of bytes in the buffer.
493 * Return: The number of bytes copied or negative errno on failure.
494 */
reader_changes_copy_to_user(struct reader_changes * const changes,char __user * buffer,size_t buffer_size)495 static ssize_t reader_changes_copy_to_user(struct reader_changes *const changes,
496 char __user *buffer,
497 size_t buffer_size)
498 {
499 ssize_t ret = 0;
500 struct kbase_kinstr_jm_atom_state_change const *src_buf = READ_ONCE(
501 changes->data);
502 size_t const entry_size = sizeof(*src_buf);
503 size_t changes_tail, changes_count, read_size;
504
505 /* Needed for the quick buffer capacity calculation below.
506 * Note that we can't use is_power_of_2() since old compilers don't
507 * understand it's a constant expression.
508 */
509 #define is_power_of_two(x) ((x) && !((x) & ((x) - 1)))
510 static_assert(is_power_of_two(
511 sizeof(struct kbase_kinstr_jm_atom_state_change)));
512 #undef is_power_of_two
513
514 lockdep_assert_held_once(&changes->consumer);
515
516 /* Read continuously until either:
517 * - we've filled the output buffer, or
518 * - there are no changes when we check.
519 *
520 * If more changes arrive while we're copying to the user, we can copy
521 * those as well, space permitting.
522 */
523 do {
524 changes_tail = changes->tail;
525 changes_count = reader_changes_count_locked(changes);
526 read_size = min(changes_count * entry_size,
527 buffer_size & ~(entry_size - 1));
528
529 if (!read_size)
530 break;
531
532 if (copy_to_user(buffer, &(src_buf[changes_tail]), read_size))
533 return -EFAULT;
534
535 buffer += read_size;
536 buffer_size -= read_size;
537 ret += read_size;
538 changes_tail = (changes_tail + read_size / entry_size) &
539 (changes->size - 1);
540 smp_store_release(&changes->tail, changes_tail);
541 } while (read_size);
542
543 return ret;
544 }
545
546 /**
547 * reader_read() - Handles a read call on the reader file descriptor
548 *
549 * @filp: The file that the read was performed on
550 * @buffer: The destination buffer
551 * @buffer_size: The maximum number of bytes to read
552 * @offset: The offset into the 'file' to read from.
553 *
554 * Note the destination buffer needs to be fully mapped in userspace or the read
555 * will fault.
556 *
557 * Return:
558 * * The number of bytes read or:
559 * * -EBADF - the file descriptor did not have an attached reader
560 * * -EFAULT - memory access fault
561 * * -EAGAIN - if the file is set to nonblocking reads with O_NONBLOCK and there
562 * is no data available
563 *
564 * Note: The number of bytes read will always be a multiple of the size of an
565 * entry.
566 */
reader_read(struct file * const filp,char __user * const buffer,size_t const buffer_size,loff_t * const offset)567 static ssize_t reader_read(struct file *const filp,
568 char __user *const buffer,
569 size_t const buffer_size,
570 loff_t *const offset)
571 {
572 struct reader *const reader = filp->private_data;
573 struct reader_changes *changes;
574 ssize_t ret;
575
576 if (!reader)
577 return -EBADF;
578
579 if (buffer_size < sizeof(struct kbase_kinstr_jm_atom_state_change))
580 return -ENOBUFS;
581
582 #if KERNEL_VERSION(5, 0, 0) <= LINUX_VERSION_CODE
583 if (!access_ok(buffer, buffer_size))
584 return -EIO;
585 #else
586 if (!access_ok(VERIFY_WRITE, buffer, buffer_size))
587 return -EIO;
588 #endif
589
590 changes = &reader->changes;
591
592 mutex_lock(&changes->consumer);
593 if (!reader_changes_count_locked(changes)) {
594 if (filp->f_flags & O_NONBLOCK) {
595 ret = -EAGAIN;
596 goto exit;
597 }
598
599 if (wait_event_interruptible(
600 reader->wait_queue,
601 !!reader_changes_count_locked(changes))) {
602 ret = -EINTR;
603 goto exit;
604 }
605 }
606
607 ret = reader_changes_copy_to_user(changes, buffer, buffer_size);
608
609 exit:
610 mutex_unlock(&changes->consumer);
611 return ret;
612 }
613
614 /**
615 * reader_poll() - Handles a poll call on the reader file descriptor
616 * @file: The file that the poll was performed on
617 * @wait: The poll table
618 *
619 * The results of the poll will be unreliable if there is no mapped memory as
620 * there is no circular buffer to push atom state changes into.
621 *
622 * Return:
623 * * 0 - no data ready
624 * * EPOLLIN | EPOLLRDNORM - state changes have been buffered
625 * * EPOLLHUP | EPOLLERR - IO control arguments were invalid or the file
626 * descriptor did not have an attached reader.
627 */
reader_poll(struct file * const file,struct poll_table_struct * const wait)628 static __poll_t reader_poll(struct file *const file,
629 struct poll_table_struct *const wait)
630 {
631 struct reader *reader;
632 struct reader_changes *changes;
633 __poll_t mask = 0;
634
635 if (unlikely(!file || !wait))
636 return EPOLLHUP | EPOLLERR;
637
638 reader = file->private_data;
639 if (unlikely(!reader))
640 return EPOLLHUP | EPOLLERR;
641
642 changes = &reader->changes;
643 if (reader_changes_count(changes) >= changes->threshold)
644 return EPOLLIN | EPOLLRDNORM;
645
646 poll_wait(file, &reader->wait_queue, wait);
647
648 if (reader_changes_count(changes) > 0)
649 mask |= EPOLLIN | EPOLLRDNORM;
650
651 return mask;
652 }
653
654 /* The file operations virtual function table */
655 static const struct file_operations file_operations = {
656 .owner = THIS_MODULE,
657 .llseek = no_llseek,
658 .read = reader_read,
659 .poll = reader_poll,
660 .release = reader_release
661 };
662
663 /* The maximum amount of readers that can be created on a context. */
664 static const size_t kbase_kinstr_jm_readers_max = 16;
665
666 /**
667 * kbase_kinstr_jm_release() - Invoked when the reference count is dropped
668 * @ref: the context reference count
669 */
kbase_kinstr_jm_release(struct kref * const ref)670 static void kbase_kinstr_jm_release(struct kref *const ref)
671 {
672 struct kbase_kinstr_jm *const ctx =
673 container_of(ref, struct kbase_kinstr_jm, refcount);
674
675 kfree(ctx);
676 }
677
678 /**
679 * kbase_kinstr_jm_ref_get() - Reference counts the instrumentation context
680 * @ctx: the context to reference count
681 * Return: the reference counted context
682 */
683 static struct kbase_kinstr_jm *
kbase_kinstr_jm_ref_get(struct kbase_kinstr_jm * const ctx)684 kbase_kinstr_jm_ref_get(struct kbase_kinstr_jm *const ctx)
685 {
686 if (likely(ctx))
687 kref_get(&ctx->refcount);
688 return ctx;
689 }
690
691 /**
692 * kbase_kinstr_jm_ref_put() - Dereferences the instrumentation context
693 * @ctx: the context to lower the reference count on
694 */
kbase_kinstr_jm_ref_put(struct kbase_kinstr_jm * const ctx)695 static void kbase_kinstr_jm_ref_put(struct kbase_kinstr_jm *const ctx)
696 {
697 if (likely(ctx))
698 kref_put(&ctx->refcount, kbase_kinstr_jm_release);
699 }
700
701 /**
702 * kbase_kinstr_jm_readers_add() - Adds a reader to the list of readers
703 * @ctx: the instrumentation context
704 * @reader: the reader to add
705 *
706 * Return:
707 * 0 - success
708 * -ENOMEM - too many readers already added.
709 */
kbase_kinstr_jm_readers_add(struct kbase_kinstr_jm * const ctx,struct reader * const reader)710 static int kbase_kinstr_jm_readers_add(struct kbase_kinstr_jm *const ctx,
711 struct reader *const reader)
712 {
713 struct hlist_bl_head *const readers = &ctx->readers;
714 struct hlist_bl_node *node;
715 struct reader *temp;
716 size_t count = 0;
717
718 hlist_bl_lock(readers);
719
720 hlist_bl_for_each_entry_rcu(temp, node, readers, node)
721 ++count;
722
723 if (kbase_kinstr_jm_readers_max < count) {
724 hlist_bl_unlock(readers);
725 return -ENOMEM;
726 }
727
728 hlist_bl_add_head_rcu(&reader->node, readers);
729
730 hlist_bl_unlock(readers);
731
732 static_branch_inc(&basep_kinstr_jm_reader_static_key);
733
734 return 0;
735 }
736
737 /**
738 * kbase_kinstr_jm_readers_del() - Deletes a reader from the list of readers
739 * @ctx: the instrumentation context
740 * @reader: the reader to delete
741 */
kbase_kinstr_jm_readers_del(struct kbase_kinstr_jm * const ctx,struct reader * const reader)742 static void kbase_kinstr_jm_readers_del(struct kbase_kinstr_jm *const ctx,
743 struct reader *const reader)
744 {
745 struct hlist_bl_head *const readers = &ctx->readers;
746
747 hlist_bl_lock(readers);
748 hlist_bl_del_rcu(&reader->node);
749 hlist_bl_unlock(readers);
750
751 static_branch_dec(&basep_kinstr_jm_reader_static_key);
752 }
753
kbase_kinstr_jm_get_fd(struct kbase_kinstr_jm * const ctx,union kbase_kinstr_jm_fd * jm_fd_arg)754 int kbase_kinstr_jm_get_fd(struct kbase_kinstr_jm *const ctx,
755 union kbase_kinstr_jm_fd *jm_fd_arg)
756 {
757 struct kbase_kinstr_jm_fd_in const *in;
758 struct reader *reader;
759 size_t const change_size = sizeof(struct
760 kbase_kinstr_jm_atom_state_change);
761 int status;
762 int fd;
763 int i;
764
765 if (!ctx || !jm_fd_arg)
766 return -EINVAL;
767
768 in = &jm_fd_arg->in;
769
770 if (!is_power_of_2(in->count))
771 return -EINVAL;
772
773 for (i = 0; i < sizeof(in->padding); ++i)
774 if (in->padding[i])
775 return -EINVAL;
776
777 status = reader_init(&reader, ctx, in->count);
778 if (status < 0)
779 return status;
780
781 jm_fd_arg->out.version = KBASE_KINSTR_JM_VERSION;
782 jm_fd_arg->out.size = change_size;
783 memset(&jm_fd_arg->out.padding, 0, sizeof(jm_fd_arg->out.padding));
784
785 fd = anon_inode_getfd("[mali_kinstr_jm]", &file_operations, reader,
786 O_CLOEXEC);
787 if (fd < 0)
788 reader_term(reader);
789
790 return fd;
791 }
792
kbase_kinstr_jm_init(struct kbase_kinstr_jm ** const out_ctx)793 int kbase_kinstr_jm_init(struct kbase_kinstr_jm **const out_ctx)
794 {
795 struct kbase_kinstr_jm *ctx = NULL;
796
797 if (!out_ctx)
798 return -EINVAL;
799
800 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
801 if (!ctx)
802 return -ENOMEM;
803
804 INIT_HLIST_BL_HEAD(&ctx->readers);
805 kref_init(&ctx->refcount);
806
807 *out_ctx = ctx;
808
809 return 0;
810 }
811
kbase_kinstr_jm_term(struct kbase_kinstr_jm * const ctx)812 void kbase_kinstr_jm_term(struct kbase_kinstr_jm *const ctx)
813 {
814 kbase_kinstr_jm_ref_put(ctx);
815 }
816
kbasep_kinstr_jm_atom_state(struct kbase_jd_atom * const katom,const enum kbase_kinstr_jm_reader_atom_state state)817 void kbasep_kinstr_jm_atom_state(
818 struct kbase_jd_atom *const katom,
819 const enum kbase_kinstr_jm_reader_atom_state state)
820 {
821 struct kbase_context *const kctx = katom->kctx;
822 struct kbase_kinstr_jm *const ctx = kctx->kinstr_jm;
823 const u8 id = kbase_jd_atom_id(kctx, katom);
824 struct kbase_kinstr_jm_atom_state_change change = {
825 .timestamp = ktime_get_raw_ns(), .atom = id, .state = state
826 };
827 struct reader *reader;
828 struct hlist_bl_node *node;
829
830 WARN(KBASE_KINSTR_JM_READER_ATOM_STATE_COUNT < state || 0 > state,
831 PR_ "unsupported katom (%u) state (%i)", id, state);
832
833 switch (state) {
834 case KBASE_KINSTR_JM_READER_ATOM_STATE_START:
835 change.data.start.slot = katom->slot_nr;
836 break;
837 default:
838 break;
839 }
840
841 rcu_read_lock();
842 hlist_bl_for_each_entry_rcu(reader, node, &ctx->readers, node)
843 reader_changes_push(
844 &reader->changes, &change, &reader->wait_queue);
845 rcu_read_unlock();
846 }
847
848 KBASE_EXPORT_TEST_API(kbasep_kinstr_jm_atom_state);
849
kbasep_kinstr_jm_atom_hw_submit(struct kbase_jd_atom * const katom)850 void kbasep_kinstr_jm_atom_hw_submit(struct kbase_jd_atom *const katom)
851 {
852 struct kbase_context *const kctx = katom->kctx;
853 struct kbase_device *const kbdev = kctx->kbdev;
854 const int slot = katom->slot_nr;
855 struct kbase_jd_atom *const submitted = kbase_gpu_inspect(kbdev, slot, 0);
856
857 BUILD_BUG_ON(SLOT_RB_SIZE != 2);
858
859 lockdep_assert_held(&kbdev->hwaccess_lock);
860
861 if (WARN_ON(slot < 0 || slot >= GPU_MAX_JOB_SLOTS))
862 return;
863 if (WARN_ON(!submitted))
864 return;
865
866 if (submitted == katom)
867 kbase_kinstr_jm_atom_state_start(katom);
868 }
869
kbasep_kinstr_jm_atom_hw_release(struct kbase_jd_atom * const katom)870 void kbasep_kinstr_jm_atom_hw_release(struct kbase_jd_atom *const katom)
871 {
872 struct kbase_context *const kctx = katom->kctx;
873 struct kbase_device *const kbdev = kctx->kbdev;
874 const int slot = katom->slot_nr;
875 struct kbase_jd_atom *const submitted = kbase_gpu_inspect(kbdev, slot, 0);
876 struct kbase_jd_atom *const queued = kbase_gpu_inspect(kbdev, slot, 1);
877
878 BUILD_BUG_ON(SLOT_RB_SIZE != 2);
879
880 lockdep_assert_held(&kbdev->hwaccess_lock);
881
882 if (WARN_ON(slot < 0 || slot >= GPU_MAX_JOB_SLOTS))
883 return;
884 if (WARN_ON(!submitted))
885 return;
886 if (WARN_ON((submitted != katom) && (queued != katom)))
887 return;
888
889 if (queued == katom)
890 return;
891
892 if (katom->gpu_rb_state == KBASE_ATOM_GPU_RB_SUBMITTED)
893 kbase_kinstr_jm_atom_state_stop(katom);
894 if (queued && queued->gpu_rb_state == KBASE_ATOM_GPU_RB_SUBMITTED)
895 kbase_kinstr_jm_atom_state_start(queued);
896 }
897