xref: /openbmc/linux/drivers/hwtracing/stm/policy.c (revision a8fe58ce)
1 /*
2  * System Trace Module (STM) master/channel allocation policy management
3  * Copyright (c) 2014, Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * A master/channel allocation policy allows mapping string identifiers to
15  * master and channel ranges, where allocation can be done.
16  */
17 
18 #define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
19 
20 #include <linux/types.h>
21 #include <linux/module.h>
22 #include <linux/device.h>
23 #include <linux/configfs.h>
24 #include <linux/slab.h>
25 #include <linux/stm.h>
26 #include "stm.h"
27 
28 /*
29  * STP Master/Channel allocation policy configfs layout.
30  */
31 
32 struct stp_policy {
33 	struct config_group	group;
34 	struct stm_device	*stm;
35 };
36 
37 struct stp_policy_node {
38 	struct config_group	group;
39 	struct stp_policy	*policy;
40 	unsigned int		first_master;
41 	unsigned int		last_master;
42 	unsigned int		first_channel;
43 	unsigned int		last_channel;
44 };
45 
46 static struct configfs_subsystem stp_policy_subsys;
47 
48 void stp_policy_node_get_ranges(struct stp_policy_node *policy_node,
49 				unsigned int *mstart, unsigned int *mend,
50 				unsigned int *cstart, unsigned int *cend)
51 {
52 	*mstart	= policy_node->first_master;
53 	*mend	= policy_node->last_master;
54 	*cstart	= policy_node->first_channel;
55 	*cend	= policy_node->last_channel;
56 }
57 
58 static inline char *stp_policy_node_name(struct stp_policy_node *policy_node)
59 {
60 	return policy_node->group.cg_item.ci_name ? : "<none>";
61 }
62 
63 static inline struct stp_policy *to_stp_policy(struct config_item *item)
64 {
65 	return item ?
66 		container_of(to_config_group(item), struct stp_policy, group) :
67 		NULL;
68 }
69 
70 static inline struct stp_policy_node *
71 to_stp_policy_node(struct config_item *item)
72 {
73 	return item ?
74 		container_of(to_config_group(item), struct stp_policy_node,
75 			     group) :
76 		NULL;
77 }
78 
79 static ssize_t
80 stp_policy_node_masters_show(struct config_item *item, char *page)
81 {
82 	struct stp_policy_node *policy_node = to_stp_policy_node(item);
83 	ssize_t count;
84 
85 	count = sprintf(page, "%u %u\n", policy_node->first_master,
86 			policy_node->last_master);
87 
88 	return count;
89 }
90 
91 static ssize_t
92 stp_policy_node_masters_store(struct config_item *item, const char *page,
93 			      size_t count)
94 {
95 	struct stp_policy_node *policy_node = to_stp_policy_node(item);
96 	unsigned int first, last;
97 	struct stm_device *stm;
98 	char *p = (char *)page;
99 	ssize_t ret = -ENODEV;
100 
101 	if (sscanf(p, "%u %u", &first, &last) != 2)
102 		return -EINVAL;
103 
104 	mutex_lock(&stp_policy_subsys.su_mutex);
105 	stm = policy_node->policy->stm;
106 	if (!stm)
107 		goto unlock;
108 
109 	/* must be within [sw_start..sw_end], which is an inclusive range */
110 	if (first > INT_MAX || last > INT_MAX || first > last ||
111 	    first < stm->data->sw_start ||
112 	    last > stm->data->sw_end) {
113 		ret = -ERANGE;
114 		goto unlock;
115 	}
116 
117 	ret = count;
118 	policy_node->first_master = first;
119 	policy_node->last_master = last;
120 
121 unlock:
122 	mutex_unlock(&stp_policy_subsys.su_mutex);
123 
124 	return ret;
125 }
126 
127 static ssize_t
128 stp_policy_node_channels_show(struct config_item *item, char *page)
129 {
130 	struct stp_policy_node *policy_node = to_stp_policy_node(item);
131 	ssize_t count;
132 
133 	count = sprintf(page, "%u %u\n", policy_node->first_channel,
134 			policy_node->last_channel);
135 
136 	return count;
137 }
138 
139 static ssize_t
140 stp_policy_node_channels_store(struct config_item *item, const char *page,
141 			       size_t count)
142 {
143 	struct stp_policy_node *policy_node = to_stp_policy_node(item);
144 	unsigned int first, last;
145 	struct stm_device *stm;
146 	char *p = (char *)page;
147 	ssize_t ret = -ENODEV;
148 
149 	if (sscanf(p, "%u %u", &first, &last) != 2)
150 		return -EINVAL;
151 
152 	mutex_lock(&stp_policy_subsys.su_mutex);
153 	stm = policy_node->policy->stm;
154 	if (!stm)
155 		goto unlock;
156 
157 	if (first > INT_MAX || last > INT_MAX || first > last ||
158 	    last >= stm->data->sw_nchannels) {
159 		ret = -ERANGE;
160 		goto unlock;
161 	}
162 
163 	ret = count;
164 	policy_node->first_channel = first;
165 	policy_node->last_channel = last;
166 
167 unlock:
168 	mutex_unlock(&stp_policy_subsys.su_mutex);
169 
170 	return ret;
171 }
172 
173 static void stp_policy_node_release(struct config_item *item)
174 {
175 	kfree(to_stp_policy_node(item));
176 }
177 
178 static struct configfs_item_operations stp_policy_node_item_ops = {
179 	.release		= stp_policy_node_release,
180 };
181 
182 CONFIGFS_ATTR(stp_policy_node_, masters);
183 CONFIGFS_ATTR(stp_policy_node_, channels);
184 
185 static struct configfs_attribute *stp_policy_node_attrs[] = {
186 	&stp_policy_node_attr_masters,
187 	&stp_policy_node_attr_channels,
188 	NULL,
189 };
190 
191 static struct config_item_type stp_policy_type;
192 static struct config_item_type stp_policy_node_type;
193 
194 static struct config_group *
195 stp_policy_node_make(struct config_group *group, const char *name)
196 {
197 	struct stp_policy_node *policy_node, *parent_node;
198 	struct stp_policy *policy;
199 
200 	if (group->cg_item.ci_type == &stp_policy_type) {
201 		policy = container_of(group, struct stp_policy, group);
202 	} else {
203 		parent_node = container_of(group, struct stp_policy_node,
204 					   group);
205 		policy = parent_node->policy;
206 	}
207 
208 	if (!policy->stm)
209 		return ERR_PTR(-ENODEV);
210 
211 	policy_node = kzalloc(sizeof(struct stp_policy_node), GFP_KERNEL);
212 	if (!policy_node)
213 		return ERR_PTR(-ENOMEM);
214 
215 	config_group_init_type_name(&policy_node->group, name,
216 				    &stp_policy_node_type);
217 
218 	policy_node->policy = policy;
219 
220 	/* default values for the attributes */
221 	policy_node->first_master = policy->stm->data->sw_start;
222 	policy_node->last_master = policy->stm->data->sw_end;
223 	policy_node->first_channel = 0;
224 	policy_node->last_channel = policy->stm->data->sw_nchannels - 1;
225 
226 	return &policy_node->group;
227 }
228 
229 static void
230 stp_policy_node_drop(struct config_group *group, struct config_item *item)
231 {
232 	config_item_put(item);
233 }
234 
235 static struct configfs_group_operations stp_policy_node_group_ops = {
236 	.make_group	= stp_policy_node_make,
237 	.drop_item	= stp_policy_node_drop,
238 };
239 
240 static struct config_item_type stp_policy_node_type = {
241 	.ct_item_ops	= &stp_policy_node_item_ops,
242 	.ct_group_ops	= &stp_policy_node_group_ops,
243 	.ct_attrs	= stp_policy_node_attrs,
244 	.ct_owner	= THIS_MODULE,
245 };
246 
247 /*
248  * Root group: policies.
249  */
250 static ssize_t stp_policy_device_show(struct config_item *item,
251 				      char *page)
252 {
253 	struct stp_policy *policy = to_stp_policy(item);
254 	ssize_t count;
255 
256 	count = sprintf(page, "%s\n",
257 			(policy && policy->stm) ?
258 			policy->stm->data->name :
259 			"<none>");
260 
261 	return count;
262 }
263 
264 CONFIGFS_ATTR_RO(stp_policy_, device);
265 
266 static struct configfs_attribute *stp_policy_attrs[] = {
267 	&stp_policy_attr_device,
268 	NULL,
269 };
270 
271 void stp_policy_unbind(struct stp_policy *policy)
272 {
273 	struct stm_device *stm = policy->stm;
274 
275 	if (WARN_ON_ONCE(!policy->stm))
276 		return;
277 
278 	mutex_lock(&stm->policy_mutex);
279 	stm->policy = NULL;
280 	mutex_unlock(&stm->policy_mutex);
281 
282 	policy->stm = NULL;
283 
284 	stm_put_device(stm);
285 }
286 
287 static void stp_policy_release(struct config_item *item)
288 {
289 	struct stp_policy *policy = to_stp_policy(item);
290 
291 	stp_policy_unbind(policy);
292 	kfree(policy);
293 }
294 
295 static struct configfs_item_operations stp_policy_item_ops = {
296 	.release		= stp_policy_release,
297 };
298 
299 static struct configfs_group_operations stp_policy_group_ops = {
300 	.make_group	= stp_policy_node_make,
301 };
302 
303 static struct config_item_type stp_policy_type = {
304 	.ct_item_ops	= &stp_policy_item_ops,
305 	.ct_group_ops	= &stp_policy_group_ops,
306 	.ct_attrs	= stp_policy_attrs,
307 	.ct_owner	= THIS_MODULE,
308 };
309 
310 static struct config_group *
311 stp_policies_make(struct config_group *group, const char *name)
312 {
313 	struct config_group *ret;
314 	struct stm_device *stm;
315 	char *devname, *p;
316 
317 	devname = kasprintf(GFP_KERNEL, "%s", name);
318 	if (!devname)
319 		return ERR_PTR(-ENOMEM);
320 
321 	/*
322 	 * node must look like <device_name>.<policy_name>, where
323 	 * <device_name> is the name of an existing stm device and
324 	 * <policy_name> is an arbitrary string
325 	 */
326 	p = strchr(devname, '.');
327 	if (!p) {
328 		kfree(devname);
329 		return ERR_PTR(-EINVAL);
330 	}
331 
332 	*p++ = '\0';
333 
334 	stm = stm_find_device(devname);
335 	kfree(devname);
336 
337 	if (!stm)
338 		return ERR_PTR(-ENODEV);
339 
340 	mutex_lock(&stm->policy_mutex);
341 	if (stm->policy) {
342 		ret = ERR_PTR(-EBUSY);
343 		goto unlock_policy;
344 	}
345 
346 	stm->policy = kzalloc(sizeof(*stm->policy), GFP_KERNEL);
347 	if (!stm->policy) {
348 		ret = ERR_PTR(-ENOMEM);
349 		goto unlock_policy;
350 	}
351 
352 	config_group_init_type_name(&stm->policy->group, name,
353 				    &stp_policy_type);
354 	stm->policy->stm = stm;
355 
356 	ret = &stm->policy->group;
357 
358 unlock_policy:
359 	mutex_unlock(&stm->policy_mutex);
360 
361 	if (IS_ERR(ret))
362 		stm_put_device(stm);
363 
364 	return ret;
365 }
366 
367 static struct configfs_group_operations stp_policies_group_ops = {
368 	.make_group	= stp_policies_make,
369 };
370 
371 static struct config_item_type stp_policies_type = {
372 	.ct_group_ops	= &stp_policies_group_ops,
373 	.ct_owner	= THIS_MODULE,
374 };
375 
376 static struct configfs_subsystem stp_policy_subsys = {
377 	.su_group = {
378 		.cg_item = {
379 			.ci_namebuf	= "stp-policy",
380 			.ci_type	= &stp_policies_type,
381 		},
382 	},
383 };
384 
385 /*
386  * Lock the policy mutex from the outside
387  */
388 static struct stp_policy_node *
389 __stp_policy_node_lookup(struct stp_policy *policy, char *s)
390 {
391 	struct stp_policy_node *policy_node, *ret;
392 	struct list_head *head = &policy->group.cg_children;
393 	struct config_item *item;
394 	char *start, *end = s;
395 
396 	if (list_empty(head))
397 		return NULL;
398 
399 	/* return the first entry if everything else fails */
400 	item = list_entry(head->next, struct config_item, ci_entry);
401 	ret = to_stp_policy_node(item);
402 
403 next:
404 	for (;;) {
405 		start = strsep(&end, "/");
406 		if (!start)
407 			break;
408 
409 		if (!*start)
410 			continue;
411 
412 		list_for_each_entry(item, head, ci_entry) {
413 			policy_node = to_stp_policy_node(item);
414 
415 			if (!strcmp(start,
416 				    policy_node->group.cg_item.ci_name)) {
417 				ret = policy_node;
418 
419 				if (!end)
420 					goto out;
421 
422 				head = &policy_node->group.cg_children;
423 				goto next;
424 			}
425 		}
426 		break;
427 	}
428 
429 out:
430 	return ret;
431 }
432 
433 
434 struct stp_policy_node *
435 stp_policy_node_lookup(struct stm_device *stm, char *s)
436 {
437 	struct stp_policy_node *policy_node = NULL;
438 
439 	mutex_lock(&stp_policy_subsys.su_mutex);
440 
441 	mutex_lock(&stm->policy_mutex);
442 	if (stm->policy)
443 		policy_node = __stp_policy_node_lookup(stm->policy, s);
444 	mutex_unlock(&stm->policy_mutex);
445 
446 	if (policy_node)
447 		config_item_get(&policy_node->group.cg_item);
448 	mutex_unlock(&stp_policy_subsys.su_mutex);
449 
450 	return policy_node;
451 }
452 
453 void stp_policy_node_put(struct stp_policy_node *policy_node)
454 {
455 	config_item_put(&policy_node->group.cg_item);
456 }
457 
458 int __init stp_configfs_init(void)
459 {
460 	int err;
461 
462 	config_group_init(&stp_policy_subsys.su_group);
463 	mutex_init(&stp_policy_subsys.su_mutex);
464 	err = configfs_register_subsystem(&stp_policy_subsys);
465 
466 	return err;
467 }
468 
469 void __exit stp_configfs_exit(void)
470 {
471 	configfs_unregister_subsystem(&stp_policy_subsys);
472 }
473