xref: /OK3568_Linux_fs/kernel/drivers/gpu/arm/bifrost/tests/kutf/kutf_helpers.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note
2 /*
3  *
4  * (C) COPYRIGHT 2017, 2020-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 /* Kernel UTF test helpers */
23 #include <kutf/kutf_helpers.h>
24 #include <linux/err.h>
25 #include <linux/jiffies.h>
26 #include <linux/sched.h>
27 #include <linux/preempt.h>
28 #include <linux/wait.h>
29 #include <linux/uaccess.h>
30 #include <linux/export.h>
31 
32 static DEFINE_SPINLOCK(kutf_input_lock);
33 
kutf_helper_pending_input(struct kutf_context * context)34 bool kutf_helper_pending_input(struct kutf_context *context)
35 {
36 	bool input_pending;
37 
38 	spin_lock(&kutf_input_lock);
39 
40 	input_pending = !list_empty(&context->userdata.input_head);
41 
42 	spin_unlock(&kutf_input_lock);
43 
44 	return input_pending;
45 }
46 EXPORT_SYMBOL(kutf_helper_pending_input);
47 
kutf_helper_input_dequeue(struct kutf_context * context,size_t * str_size)48 char *kutf_helper_input_dequeue(struct kutf_context *context, size_t *str_size)
49 {
50 	struct kutf_userdata_line *line;
51 
52 	spin_lock(&kutf_input_lock);
53 
54 	while (list_empty(&context->userdata.input_head)) {
55 		int err;
56 
57 		kutf_set_waiting_for_input(context->result_set);
58 
59 		spin_unlock(&kutf_input_lock);
60 
61 		err = wait_event_interruptible(context->userdata.input_waitq,
62 				kutf_helper_pending_input(context));
63 
64 		if (err)
65 			return ERR_PTR(-EINTR);
66 
67 		spin_lock(&kutf_input_lock);
68 	}
69 
70 	line = list_first_entry(&context->userdata.input_head,
71 			struct kutf_userdata_line, node);
72 	if (line->str) {
73 		/*
74 		 * Unless it is the end-of-input marker,
75 		 * remove it from the list
76 		 */
77 		list_del(&line->node);
78 	}
79 
80 	spin_unlock(&kutf_input_lock);
81 
82 	if (str_size)
83 		*str_size = line->size;
84 	return line->str;
85 }
86 
kutf_helper_input_enqueue(struct kutf_context * context,const char __user * str,size_t size)87 int kutf_helper_input_enqueue(struct kutf_context *context,
88 		const char __user *str, size_t size)
89 {
90 	struct kutf_userdata_line *line;
91 
92 	line = kutf_mempool_alloc(&context->fixture_pool,
93 			sizeof(*line) + size + 1);
94 	if (!line)
95 		return -ENOMEM;
96 	if (str) {
97 		unsigned long bytes_not_copied;
98 
99 		line->size = size;
100 		line->str = (void *)(line + 1);
101 		bytes_not_copied = copy_from_user(line->str, str, size);
102 		if (bytes_not_copied != 0)
103 			return -EFAULT;
104 		/* Zero terminate the string */
105 		line->str[size] = '\0';
106 	} else {
107 		/* This is used to mark the end of input */
108 		WARN_ON(size);
109 		line->size = 0;
110 		line->str = NULL;
111 	}
112 
113 	spin_lock(&kutf_input_lock);
114 
115 	list_add_tail(&line->node, &context->userdata.input_head);
116 
117 	kutf_clear_waiting_for_input(context->result_set);
118 
119 	spin_unlock(&kutf_input_lock);
120 
121 	wake_up(&context->userdata.input_waitq);
122 
123 	return 0;
124 }
125 
kutf_helper_input_enqueue_end_of_data(struct kutf_context * context)126 void kutf_helper_input_enqueue_end_of_data(struct kutf_context *context)
127 {
128 	kutf_helper_input_enqueue(context, NULL, 0);
129 }
130 
kutf_helper_ignore_dmesg(struct device * dev)131 void kutf_helper_ignore_dmesg(struct device *dev)
132 {
133 	dev_info(dev, "KUTF: Start ignoring dmesg warnings\n");
134 }
135 EXPORT_SYMBOL(kutf_helper_ignore_dmesg);
136 
kutf_helper_stop_ignoring_dmesg(struct device * dev)137 void kutf_helper_stop_ignoring_dmesg(struct device *dev)
138 {
139 	dev_info(dev, "KUTF: Stop ignoring dmesg warnings\n");
140 }
141 EXPORT_SYMBOL(kutf_helper_stop_ignoring_dmesg);
142