• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * (C) COPYRIGHT 2019-2020 ARM Limited. All rights reserved.
4  *
5  * This program is free software and is provided to you under the terms of the
6  * GNU General Public License version 2 as published by the Free Software
7  * Foundation, and any use by you of this program is subject to the terms
8  * of such GNU licence.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, you can access it online at
17  * http://www.gnu.org/licenses/gpl-2.0.html.
18  *
19  * SPDX-License-Identifier: GPL-2.0
20  *
21  */
22 
23 /*
24  * mali_kbase_kinstr_jm.c
25  * Kernel driver public interface to job manager atom tracing
26  */
27 
28 #include "mali_kbase_kinstr_jm.h"
29 #include "mali_kbase_kinstr_jm_reader.h"
30 
31 #include "mali_kbase.h"
32 #include "mali_kbase_linux.h"
33 
34 #include <mali_kbase_jm_rb.h>
35 
36 #include <asm/barrier.h>
37 #include <linux/anon_inodes.h>
38 #include <linux/circ_buf.h>
39 #include <linux/fs.h>
40 #include <linux/kref.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 #if KERNEL_VERSION(4, 3, 0) <= LINUX_VERSION_CODE
73 DEFINE_STATIC_KEY_FALSE(basep_kinstr_jm_reader_static_key);
74 #else
75 struct static_key basep_kinstr_jm_reader_static_key = STATIC_KEY_INIT_FALSE;
76 #define static_branch_inc(key) static_key_slow_inc(key)
77 #define static_branch_dec(key) static_key_slow_dec(key)
78 #endif /* KERNEL_VERSION(4 ,3, 0) <= LINUX_VERSION_CODE */
79 
80 #define KBASE_KINSTR_JM_VERSION 1
81 
82 /**
83  * struct kbase_kinstr_jm - The context for the kernel job manager atom tracing
84  * @readers: a bitlocked list of opened readers. Readers are attached to the
85  *           private data of a file descriptor that the user opens with the
86  *           KBASE_IOCTL_KINSTR_JM_FD IO control call.
87  * @refcount: reference count for the context. Any reader will have a link
88  *            back to the context so that they can remove themselves from the
89  *            list.
90  *
91  * This is opaque outside this compilation unit
92  */
93 struct kbase_kinstr_jm {
94     struct hlist_bl_head readers;
95     struct kref refcount;
96 };
97 
98 /**
99  * struct kbase_kinstr_jm_atom_state_change - Represents an atom changing to a
100  *                                            new state
101  * @timestamp: Raw monotonic nanoseconds of the state change
102  * @state:     The state that the atom has moved to
103  * @atom:      The atom number that has changed state
104  * @flags:     Flags associated with the state change. See
105  *             KBASE_KINSTR_JM_ATOM_STATE_FLAG_* defines.
106  * @reserved:  Reserved for future use.
107  * @data:      Extra data for the state change. Active member depends on state.
108  *
109  * We can add new fields to the structure and old user code will gracefully
110  * ignore the new fields.
111  *
112  * We can change the size of the structure and old user code will gracefully
113  * skip over the new size via `struct kbase_kinstr_jm_fd_out->size`.
114  *
115  * If we remove fields, the version field in `struct
116  * kbase_kinstr_jm_fd_out->version` will be incremented and old user code will
117  * gracefully fail and tell the user that the kernel API is too new and has
118  * backwards-incompatible changes. Note that one userspace can opt to handle
119  * multiple kernel major versions of the structure.
120  *
121  * If we need to change the _meaning_ of one of the fields, i.e. the state
122  * machine has had a incompatible change, we can keep the same members in the
123  * structure and update the version as above. User code will no longer
124  * recognise that it has the supported field and can gracefully explain to the
125  * user that the kernel API is no longer supported.
126  *
127  * When making changes to this structure, make sure they are either:
128  *  - additions to the end (for minor version bumps (i.e. only a size increase))
129  *  such that the layout of existing fields doesn't change, or;
130  *  - update the version reported to userspace so that it can fail explicitly.
131  */
132 struct kbase_kinstr_jm_atom_state_change {
133     u64 timestamp;
134     s8 state; /* enum kbase_kinstr_jm_reader_atom_state */
135     u8 atom;
136     u8 flags;
137     u8 reserved[1];
138     /* Tagged union based on state. Ensure members are aligned correctly! */
139     union {
140         struct {
141             u8 slot;
142         } start;
143         u8 padding[4];
144     } data;
145 };
146 static_assert(((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, 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 
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)) / 0x4, ((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(struct reader_changes *const changes,
330                                 const struct kbase_kinstr_jm_atom_state_change *const change,
331                                 wait_queue_head_t *const wait_queue)
332 {
333     u32 head, tail, size, space;
334     unsigned long irq;
335     struct kbase_kinstr_jm_atom_state_change *data;
336 
337     spin_lock_irqsave(&changes->producer, irq);
338 
339     /* We may be called for a reader_changes that's awaiting cleanup. */
340     data = changes->data;
341     if (!data) {
342         goto unlock;
343     }
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 |= KBASE_KINSTR_JM_ATOM_STATE_FLAG_OVERFLOW;
354             pr_warn(PR_ "overflow of circular buffer\n");
355         }
356         smp_store_release(&changes->head, (head + 1) & (size - 1));
357     }
358 
359     /* Wake for either overflow or over-threshold cases. */
360     if (CIRC_CNT(head + 1, tail, size) >= changes->threshold) {
361         wake_up_interruptible(wait_queue);
362     }
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 *kbase_kinstr_jm_ref_get(struct kbase_kinstr_jm *const ctx);
398 static void kbase_kinstr_jm_ref_put(struct kbase_kinstr_jm *const ctx);
399 static int kbase_kinstr_jm_readers_add(struct kbase_kinstr_jm *const ctx, struct reader *const reader);
400 static void kbase_kinstr_jm_readers_del(struct kbase_kinstr_jm *const ctx, struct reader *const reader);
401 
402 /**
403  * reader_term() - Terminate a instrumentation job manager reader context.
404  * @reader: Pointer to context to be terminated.
405  */
reader_term(struct reader * const reader)406 static void reader_term(struct reader *const reader)
407 {
408     if (!reader) {
409         return;
410     }
411 
412     kbase_kinstr_jm_readers_del(reader->context, reader);
413     reader_changes_term(&reader->changes);
414     kbase_kinstr_jm_ref_put(reader->context);
415 
416     kfree_rcu(reader, rcu_head);
417 }
418 
419 /**
420  * reader_init() - Initialise a instrumentation job manager reader context.
421  * @out_reader:  Non-NULL pointer to where the pointer to the created context
422  *               will be stored on success.
423  * @ctx:         the pointer to the parent context. Reference count will be
424  *               increased if initialization is successful
425  * @num_changes: The number of changes to allocate a buffer for
426  *
427  * Return: 0 on success, else error code.
428  */
reader_init(struct reader ** const out_reader,struct kbase_kinstr_jm * const ctx,size_t const num_changes)429 static int reader_init(struct reader **const out_reader, struct kbase_kinstr_jm *const ctx, size_t const num_changes)
430 {
431     struct reader *reader = NULL;
432     const size_t change_size = sizeof(struct kbase_kinstr_jm_atom_state_change);
433     int status;
434 
435     if (!out_reader || !ctx || !num_changes) {
436         return -EINVAL;
437     }
438 
439     reader = kzalloc(sizeof(*reader), GFP_KERNEL);
440     if (!reader) {
441         return -ENOMEM;
442     }
443 
444     INIT_HLIST_BL_NODE(&reader->node);
445     init_waitqueue_head(&reader->wait_queue);
446 
447     reader->context = kbase_kinstr_jm_ref_get(ctx);
448 
449     status = reader_changes_init(&reader->changes, num_changes * change_size);
450     if (status < 0) {
451         goto fail;
452     }
453 
454     status = kbase_kinstr_jm_readers_add(ctx, reader);
455     if (status < 0) {
456         goto fail;
457     }
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, char __user *buffer,
496                                            size_t buffer_size)
497 {
498     ssize_t ret = 0;
499     struct kbase_kinstr_jm_atom_state_change const *src_buf = READ_ONCE(changes->data);
500     size_t const entry_size = sizeof(*src_buf);
501     size_t changes_tail, changes_count, read_size;
502 
503     /* Needed for the quick buffer capacity calculation below.
504      * Note that we can't use is_power_of_2() since old compilers don't
505      * understand it's a constant expression.
506      */
507 #define is_power_of_two(x) ((x) && !((x) & ((x)-1)))
508     static_assert(is_power_of_two(sizeof(struct kbase_kinstr_jm_atom_state_change)));
509 #undef is_power_of_two
510 
511     lockdep_assert_held_once(&changes->consumer);
512 
513     /* Read continuously until either:
514      * - we've filled the output buffer, or
515      * - there are no changes when we check.
516      *
517      * If more changes arrive while we're copying to the user, we can copy
518      * those as well, space permitting.
519      */
520     do {
521         changes_tail = changes->tail;
522         changes_count = reader_changes_count_locked(changes);
523         read_size = min(changes_count * entry_size, buffer_size & ~(entry_size - 1));
524 
525         if (!read_size) {
526             break;
527         }
528 
529         if (copy_to_user(buffer, &(src_buf[changes_tail]), read_size)) {
530             return -EFAULT;
531         }
532 
533         buffer += read_size;
534         buffer_size -= read_size;
535         ret += read_size;
536         changes_tail = (changes_tail + read_size / entry_size) & (changes->size - 1);
537         smp_store_release(&changes->tail, changes_tail);
538     } while (read_size);
539 
540     return ret;
541 }
542 
543 /**
544  * reader_read() - Handles a read call on the reader file descriptor
545  *
546  * @filp: The file that the read was performed on
547  * @buffer: The destination buffer
548  * @buffer_size: The maximum number of bytes to read
549  * @offset: The offset into the 'file' to read from.
550  *
551  * Note the destination buffer needs to be fully mapped in userspace or the read
552  * will fault.
553  *
554  * Return:
555  * * The number of bytes read or:
556  * * -EBADF - the file descriptor did not have an attached reader
557  * * -EFAULT - memory access fault
558  * * -EAGAIN - if the file is set to nonblocking reads with O_NONBLOCK and there
559  *             is no data available
560  *
561  * Note: The number of bytes read will always be a multiple of the size of an
562  * entry.
563  */
reader_read(struct file * const filp,char __user * const buffer,size_t const buffer_size,loff_t * const offset)564 static ssize_t reader_read(struct file *const filp, char __user *const buffer, size_t const buffer_size,
565                            loff_t *const offset)
566 {
567     struct reader *const reader = filp->private_data;
568     struct reader_changes *changes;
569     ssize_t ret;
570 
571     if (!reader) {
572         return -EBADF;
573     }
574 
575     if (buffer_size < sizeof(struct kbase_kinstr_jm_atom_state_change)) {
576         return -ENOBUFS;
577     }
578 
579 #if KERNEL_VERSION(5, 0, 0) <= LINUX_VERSION_CODE
580     if (!access_ok(buffer, buffer_size)) {
581         return -EIO;
582     }
583 #else
584     if (!access_ok(VERIFY_WRITE, buffer, buffer_size)) {
585         return -EIO;
586     }
587 #endif
588 
589     changes = &reader->changes;
590 
591     mutex_lock(&changes->consumer);
592     if (!reader_changes_count_locked(changes)) {
593         if (filp->f_flags & O_NONBLOCK) {
594             ret = -EAGAIN;
595             goto exit;
596         }
597 
598         if (wait_event_interruptible(reader->wait_queue, !!reader_changes_count_locked(changes))) {
599             ret = -EINTR;
600             goto exit;
601         }
602     }
603 
604     ret = reader_changes_copy_to_user(changes, buffer, buffer_size);
605 
606 exit:
607     mutex_unlock(&changes->consumer);
608     return ret;
609 }
610 
611 /**
612  * reader_poll() - Handles a poll call on the reader file descriptor
613  * @file: The file that the poll was performed on
614  * @wait: The poll table
615  *
616  * The results of the poll will be unreliable if there is no mapped memory as
617  * there is no circular buffer to push atom state changes into.
618  *
619  * Return:
620  * * 0 - no data ready
621  * * POLLIN - state changes have been buffered
622  * * -EBADF - the file descriptor did not have an attached reader
623  * * -EINVAL - the IO control arguments were invalid
624  */
reader_poll(struct file * const file,struct poll_table_struct * const wait)625 static __poll_t reader_poll(struct file *const file, struct poll_table_struct *const wait)
626 {
627     struct reader *reader;
628     struct reader_changes *changes;
629 
630     if (unlikely(!file || !wait)) {
631         return -EINVAL;
632     }
633 
634     reader = file->private_data;
635     if (unlikely(!reader)) {
636         return -EBADF;
637     }
638 
639     changes = &reader->changes;
640 
641     if (reader_changes_count(changes) >= changes->threshold) {
642         return POLLIN;
643     }
644 
645     poll_wait(file, &reader->wait_queue, wait);
646 
647     return (reader_changes_count(changes) > 0) ? POLLIN : 0;
648 }
649 
650 /* The file operations virtual function table */
651 static const struct file_operations file_operations = {
652     .owner = THIS_MODULE, .llseek = no_llseek, .read = reader_read, .poll = reader_poll, .release = reader_release};
653 
654 /* The maximum amount of readers that can be created on a context. */
655 static const size_t kbase_kinstr_jm_readers_max = 16;
656 
657 /**
658  * kbasep_kinstr_jm_release() - Invoked when the reference count is dropped
659  * @ref: the context reference count
660  */
kbase_kinstr_jm_release(struct kref * const ref)661 static void kbase_kinstr_jm_release(struct kref *const ref)
662 {
663     struct kbase_kinstr_jm *const ctx = container_of(ref, struct kbase_kinstr_jm, refcount);
664 
665     kfree(ctx);
666 }
667 
668 /**
669  * kbase_kinstr_jm_ref_get() - Reference counts the instrumentation context
670  * @ctx: the context to reference count
671  * Return: the reference counted context
672  */
kbase_kinstr_jm_ref_get(struct kbase_kinstr_jm * const ctx)673 static struct kbase_kinstr_jm *kbase_kinstr_jm_ref_get(struct kbase_kinstr_jm *const ctx)
674 {
675     if (likely(ctx)) {
676         kref_get(&ctx->refcount);
677     }
678     return ctx;
679 }
680 
681 /**
682  * kbase_kinstr_jm_ref_put() - Dereferences the instrumentation context
683  * @ctx: the context to lower the reference count on
684  */
kbase_kinstr_jm_ref_put(struct kbase_kinstr_jm * const ctx)685 static void kbase_kinstr_jm_ref_put(struct kbase_kinstr_jm *const ctx)
686 {
687     if (likely(ctx)) {
688         kref_put(&ctx->refcount, kbase_kinstr_jm_release);
689     }
690 }
691 
692 /**
693  * kbase_kinstr_jm_readers_add() - Adds a reader to the list of readers
694  * @ctx: the instrumentation context
695  * @reader: the reader to add
696  *
697  * Return:
698  * 0 - success
699  * -ENOMEM - too many readers already added.
700  */
kbase_kinstr_jm_readers_add(struct kbase_kinstr_jm * const ctx,struct reader * const reader)701 static int kbase_kinstr_jm_readers_add(struct kbase_kinstr_jm *const ctx, struct reader *const reader)
702 {
703     struct hlist_bl_head *const readers = &ctx->readers;
704     struct hlist_bl_node *node;
705     struct reader *temp;
706     size_t count = 0;
707 
708     hlist_bl_lock(readers);
709 
710     hlist_bl_for_each_entry_rcu(temp, node, readers, node)++ count;
711 
712     if (kbase_kinstr_jm_readers_max < count) {
713         hlist_bl_unlock(readers);
714         return -ENOMEM;
715     }
716 
717     hlist_bl_add_head_rcu(&reader->node, readers);
718 
719     hlist_bl_unlock(readers);
720 
721     static_branch_inc(&basep_kinstr_jm_reader_static_key);
722 
723     return 0;
724 }
725 
726 /**
727  * readers_del() - Deletes a reader from the list of readers
728  * @ctx: the instrumentation context
729  * @reader: the reader to delete
730  */
kbase_kinstr_jm_readers_del(struct kbase_kinstr_jm * const ctx,struct reader * const reader)731 static void kbase_kinstr_jm_readers_del(struct kbase_kinstr_jm *const ctx, struct reader *const reader)
732 {
733     struct hlist_bl_head *const readers = &ctx->readers;
734 
735     hlist_bl_lock(readers);
736     hlist_bl_del_rcu(&reader->node);
737     hlist_bl_unlock(readers);
738 
739     static_branch_dec(&basep_kinstr_jm_reader_static_key);
740 }
741 
kbase_kinstr_jm_get_fd(struct kbase_kinstr_jm * const ctx,union kbase_kinstr_jm_fd * jm_fd_arg)742 int kbase_kinstr_jm_get_fd(struct kbase_kinstr_jm *const ctx, union kbase_kinstr_jm_fd *jm_fd_arg)
743 {
744     struct kbase_kinstr_jm_fd_in const *in;
745     struct reader *reader;
746     size_t const change_size = sizeof(struct kbase_kinstr_jm_atom_state_change);
747     int status;
748     int fd;
749     int i;
750 
751     if (!ctx || !jm_fd_arg) {
752         return -EINVAL;
753     }
754 
755     in = &jm_fd_arg->in;
756 
757     if (!is_power_of_2(in->count)) {
758         return -EINVAL;
759     }
760 
761     for (i = 0; i < sizeof(in->padding); ++i) {
762         if (in->padding[i]) {
763             return -EINVAL;
764         }
765     }
766 
767     status = reader_init(&reader, ctx, in->count);
768     if (status < 0) {
769         return status;
770     }
771 
772     jm_fd_arg->out.version = KBASE_KINSTR_JM_VERSION;
773     jm_fd_arg->out.size = change_size;
774     memset(&jm_fd_arg->out.padding, 0, sizeof(jm_fd_arg->out.padding));
775 
776     fd = anon_inode_getfd("[mali_kinstr_jm]", &file_operations, reader, O_CLOEXEC);
777     if (fd < 0) {
778         reader_term(reader);
779     }
780 
781     return fd;
782 }
783 
kbase_kinstr_jm_init(struct kbase_kinstr_jm ** const out_ctx)784 int kbase_kinstr_jm_init(struct kbase_kinstr_jm **const out_ctx)
785 {
786     struct kbase_kinstr_jm *ctx = NULL;
787 
788     if (!out_ctx) {
789         return -EINVAL;
790     }
791 
792     ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
793     if (!ctx) {
794         return -ENOMEM;
795     }
796 
797     INIT_HLIST_BL_HEAD(&ctx->readers);
798     kref_init(&ctx->refcount);
799 
800     *out_ctx = ctx;
801 
802     return 0;
803 }
804 
kbase_kinstr_jm_term(struct kbase_kinstr_jm * const ctx)805 void kbase_kinstr_jm_term(struct kbase_kinstr_jm *const ctx)
806 {
807     kbase_kinstr_jm_ref_put(ctx);
808 }
809 
kbasep_kinstr_jm_atom_state(struct kbase_jd_atom * const katom,const enum kbase_kinstr_jm_reader_atom_state state)810 void kbasep_kinstr_jm_atom_state(struct kbase_jd_atom *const katom, const enum kbase_kinstr_jm_reader_atom_state state)
811 {
812     struct kbase_context *const kctx = katom->kctx;
813     struct kbase_kinstr_jm *const ctx = kctx->kinstr_jm;
814     const u8 id = kbase_jd_atom_id(kctx, katom);
815     struct kbase_kinstr_jm_atom_state_change change = {.timestamp = ktime_get_raw_ns(), .atom = id, .state = state};
816     struct reader *reader;
817     struct hlist_bl_node *node;
818 
819     WARN(KBASE_KINSTR_JM_READER_ATOM_STATE_COUNT < state || 0 > state, PR_ "unsupported katom (%u) state (%i)", id,
820          state);
821 
822     switch (state) {
823         case KBASE_KINSTR_JM_READER_ATOM_STATE_START:
824             change.data.start.slot = katom->jobslot;
825             break;
826         default:
827             break;
828     }
829 
830     rcu_read_lock();
831     hlist_bl_for_each_entry_rcu(reader, node, &ctx->readers, node)
832         reader_changes_push(&reader->changes, &change, &reader->wait_queue);
833     rcu_read_unlock();
834 }
835 
836 KBASE_EXPORT_TEST_API(kbasep_kinstr_jm_atom_state);
837 
kbasep_kinstr_jm_atom_hw_submit(struct kbase_jd_atom * const katom)838 void kbasep_kinstr_jm_atom_hw_submit(struct kbase_jd_atom *const katom)
839 {
840     struct kbase_context *const kctx = katom->kctx;
841     struct kbase_device *const kbdev = kctx->kbdev;
842     const int slot = katom->slot_nr;
843     struct kbase_jd_atom *const submitted = kbase_gpu_inspect(kbdev, slot, 0);
844 
845     BUILD_BUG_ON(SLOT_RB_SIZE != 0x2);
846 
847     lockdep_assert_held(&kbdev->hwaccess_lock);
848 
849     if (WARN_ON(slot < 0 || slot >= GPU_MAX_JOB_SLOTS)) {
850         return;
851     }
852     if (WARN_ON(!submitted)) {
853         return;
854     }
855 
856     if (submitted == katom) {
857         kbase_kinstr_jm_atom_state_start(katom);
858     }
859 }
860 
kbasep_kinstr_jm_atom_hw_release(struct kbase_jd_atom * const katom)861 void kbasep_kinstr_jm_atom_hw_release(struct kbase_jd_atom *const katom)
862 {
863     struct kbase_context *const kctx = katom->kctx;
864     struct kbase_device *const kbdev = kctx->kbdev;
865     const int slot = katom->slot_nr;
866     struct kbase_jd_atom *const submitted = kbase_gpu_inspect(kbdev, slot, 0);
867     struct kbase_jd_atom *const queued = kbase_gpu_inspect(kbdev, slot, 1);
868 
869     BUILD_BUG_ON(SLOT_RB_SIZE != 0x2);
870 
871     lockdep_assert_held(&kbdev->hwaccess_lock);
872 
873     if (WARN_ON(slot < 0 || slot >= GPU_MAX_JOB_SLOTS)) {
874         return;
875     }
876     if (WARN_ON(!submitted)) {
877         return;
878     }
879     if (WARN_ON((submitted != katom) && (queued != katom))) {
880         return;
881     }
882 
883     if (queued == katom) {
884         return;
885     }
886 
887     if (katom->gpu_rb_state == KBASE_ATOM_GPU_RB_SUBMITTED) {
888         kbase_kinstr_jm_atom_state_stop(katom);
889     }
890     if (queued && queued->gpu_rb_state == KBASE_ATOM_GPU_RB_SUBMITTED) {
891         kbase_kinstr_jm_atom_state_start(queued);
892     }
893 }
894