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