forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-01-31 f70575805708cabdedea7498aaa3f710fde4d920
kernel/drivers/hwtracing/stm/policy.c
....@@ -33,7 +33,17 @@
3333 unsigned int last_master;
3434 unsigned int first_channel;
3535 unsigned int last_channel;
36
+ /* this is the one that's exposed to the attributes */
37
+ unsigned char priv[];
3638 };
39
+
40
+void *stp_policy_node_priv(struct stp_policy_node *pn)
41
+{
42
+ if (!pn)
43
+ return NULL;
44
+
45
+ return pn->priv;
46
+}
3747
3848 static struct configfs_subsystem stp_policy_subsys;
3949
....@@ -67,6 +77,14 @@
6777 group) :
6878 NULL;
6979 }
80
+
81
+void *to_pdrv_policy_node(struct config_item *item)
82
+{
83
+ struct stp_policy_node *node = to_stp_policy_node(item);
84
+
85
+ return stp_policy_node_priv(node);
86
+}
87
+EXPORT_SYMBOL_GPL(to_pdrv_policy_node);
7088
7189 static ssize_t
7290 stp_policy_node_masters_show(struct config_item *item, char *page)
....@@ -163,7 +181,9 @@
163181
164182 static void stp_policy_node_release(struct config_item *item)
165183 {
166
- kfree(to_stp_policy_node(item));
184
+ struct stp_policy_node *node = to_stp_policy_node(item);
185
+
186
+ kfree(node);
167187 }
168188
169189 static struct configfs_item_operations stp_policy_node_item_ops = {
....@@ -182,10 +202,34 @@
182202 static const struct config_item_type stp_policy_type;
183203 static const struct config_item_type stp_policy_node_type;
184204
205
+const struct config_item_type *
206
+get_policy_node_type(struct configfs_attribute **attrs)
207
+{
208
+ struct config_item_type *type;
209
+ struct configfs_attribute **merged;
210
+
211
+ type = kmemdup(&stp_policy_node_type, sizeof(stp_policy_node_type),
212
+ GFP_KERNEL);
213
+ if (!type)
214
+ return NULL;
215
+
216
+ merged = memcat_p(stp_policy_node_attrs, attrs);
217
+ if (!merged) {
218
+ kfree(type);
219
+ return NULL;
220
+ }
221
+
222
+ type->ct_attrs = merged;
223
+
224
+ return type;
225
+}
226
+
185227 static struct config_group *
186228 stp_policy_node_make(struct config_group *group, const char *name)
187229 {
230
+ const struct config_item_type *type = &stp_policy_node_type;
188231 struct stp_policy_node *policy_node, *parent_node;
232
+ const struct stm_protocol_driver *pdrv;
189233 struct stp_policy *policy;
190234
191235 if (group->cg_item.ci_type == &stp_policy_type) {
....@@ -199,12 +243,20 @@
199243 if (!policy->stm)
200244 return ERR_PTR(-ENODEV);
201245
202
- policy_node = kzalloc(sizeof(struct stp_policy_node), GFP_KERNEL);
246
+ pdrv = policy->stm->pdrv;
247
+ policy_node =
248
+ kzalloc(offsetof(struct stp_policy_node, priv[pdrv->priv_sz]),
249
+ GFP_KERNEL);
203250 if (!policy_node)
204251 return ERR_PTR(-ENOMEM);
205252
206
- config_group_init_type_name(&policy_node->group, name,
207
- &stp_policy_node_type);
253
+ if (pdrv->policy_node_init)
254
+ pdrv->policy_node_init((void *)policy_node->priv);
255
+
256
+ if (policy->stm->pdrv_node_type)
257
+ type = policy->stm->pdrv_node_type;
258
+
259
+ config_group_init_type_name(&policy_node->group, name, type);
208260
209261 policy_node->policy = policy;
210262
....@@ -254,8 +306,25 @@
254306
255307 CONFIGFS_ATTR_RO(stp_policy_, device);
256308
309
+static ssize_t stp_policy_protocol_show(struct config_item *item,
310
+ char *page)
311
+{
312
+ struct stp_policy *policy = to_stp_policy(item);
313
+ ssize_t count;
314
+
315
+ count = sprintf(page, "%s\n",
316
+ (policy && policy->stm) ?
317
+ policy->stm->pdrv->name :
318
+ "<none>");
319
+
320
+ return count;
321
+}
322
+
323
+CONFIGFS_ATTR_RO(stp_policy_, protocol);
324
+
257325 static struct configfs_attribute *stp_policy_attrs[] = {
258326 &stp_policy_attr_device,
327
+ &stp_policy_attr_protocol,
259328 NULL,
260329 };
261330
....@@ -276,6 +345,11 @@
276345 stm->policy = NULL;
277346 policy->stm = NULL;
278347
348
+ /*
349
+ * Drop the reference on the protocol driver and lose the link.
350
+ */
351
+ stm_put_protocol(stm->pdrv);
352
+ stm->pdrv = NULL;
279353 stm_put_device(stm);
280354 }
281355
....@@ -311,11 +385,14 @@
311385 };
312386
313387 static struct config_group *
314
-stp_policies_make(struct config_group *group, const char *name)
388
+stp_policy_make(struct config_group *group, const char *name)
315389 {
390
+ const struct config_item_type *pdrv_node_type;
391
+ const struct stm_protocol_driver *pdrv;
392
+ char *devname, *proto, *p;
316393 struct config_group *ret;
317394 struct stm_device *stm;
318
- char *devname, *p;
395
+ int err;
319396
320397 devname = kasprintf(GFP_KERNEL, "%s", name);
321398 if (!devname)
....@@ -326,6 +403,7 @@
326403 * <device_name> is the name of an existing stm device; may
327404 * contain dots;
328405 * <policy_name> is an arbitrary string; may not contain dots
406
+ * <device_name>:<protocol_name>.<policy_name>
329407 */
330408 p = strrchr(devname, '.');
331409 if (!p) {
....@@ -335,11 +413,28 @@
335413
336414 *p = '\0';
337415
416
+ /*
417
+ * look for ":<protocol_name>":
418
+ * + no protocol suffix: fall back to whatever is available;
419
+ * + unknown protocol: fail the whole thing
420
+ */
421
+ proto = strrchr(devname, ':');
422
+ if (proto)
423
+ *proto++ = '\0';
424
+
338425 stm = stm_find_device(devname);
426
+ if (!stm) {
427
+ kfree(devname);
428
+ return ERR_PTR(-ENODEV);
429
+ }
430
+
431
+ err = stm_lookup_protocol(proto, &pdrv, &pdrv_node_type);
339432 kfree(devname);
340433
341
- if (!stm)
434
+ if (err) {
435
+ stm_put_device(stm);
342436 return ERR_PTR(-ENODEV);
437
+ }
343438
344439 mutex_lock(&stm->policy_mutex);
345440 if (stm->policy) {
....@@ -355,25 +450,33 @@
355450
356451 config_group_init_type_name(&stm->policy->group, name,
357452 &stp_policy_type);
358
- stm->policy->stm = stm;
359453
454
+ stm->pdrv = pdrv;
455
+ stm->pdrv_node_type = pdrv_node_type;
456
+ stm->policy->stm = stm;
360457 ret = &stm->policy->group;
361458
362459 unlock_policy:
363460 mutex_unlock(&stm->policy_mutex);
364461
365
- if (IS_ERR(ret))
462
+ if (IS_ERR(ret)) {
463
+ /*
464
+ * pdrv and stm->pdrv at this point can be quite different,
465
+ * and only one of them needs to be 'put'
466
+ */
467
+ stm_put_protocol(pdrv);
366468 stm_put_device(stm);
469
+ }
367470
368471 return ret;
369472 }
370473
371
-static struct configfs_group_operations stp_policies_group_ops = {
372
- .make_group = stp_policies_make,
474
+static struct configfs_group_operations stp_policy_root_group_ops = {
475
+ .make_group = stp_policy_make,
373476 };
374477
375
-static const struct config_item_type stp_policies_type = {
376
- .ct_group_ops = &stp_policies_group_ops,
478
+static const struct config_item_type stp_policy_root_type = {
479
+ .ct_group_ops = &stp_policy_root_group_ops,
377480 .ct_owner = THIS_MODULE,
378481 };
379482
....@@ -381,7 +484,7 @@
381484 .su_group = {
382485 .cg_item = {
383486 .ci_namebuf = "stp-policy",
384
- .ci_type = &stp_policies_type,
487
+ .ci_type = &stp_policy_root_type,
385488 },
386489 },
387490 };
....@@ -392,17 +495,13 @@
392495 static struct stp_policy_node *
393496 __stp_policy_node_lookup(struct stp_policy *policy, char *s)
394497 {
395
- struct stp_policy_node *policy_node, *ret;
498
+ struct stp_policy_node *policy_node, *ret = NULL;
396499 struct list_head *head = &policy->group.cg_children;
397500 struct config_item *item;
398501 char *start, *end = s;
399502
400503 if (list_empty(head))
401504 return NULL;
402
-
403
- /* return the first entry if everything else fails */
404
- item = list_entry(head->next, struct config_item, ci_entry);
405
- ret = to_stp_policy_node(item);
406505
407506 next:
408507 for (;;) {
....@@ -449,25 +548,25 @@
449548
450549 if (policy_node)
451550 config_item_get(&policy_node->group.cg_item);
452
- mutex_unlock(&stp_policy_subsys.su_mutex);
551
+ else
552
+ mutex_unlock(&stp_policy_subsys.su_mutex);
453553
454554 return policy_node;
455555 }
456556
457557 void stp_policy_node_put(struct stp_policy_node *policy_node)
458558 {
559
+ lockdep_assert_held(&stp_policy_subsys.su_mutex);
560
+
561
+ mutex_unlock(&stp_policy_subsys.su_mutex);
459562 config_item_put(&policy_node->group.cg_item);
460563 }
461564
462565 int __init stp_configfs_init(void)
463566 {
464
- int err;
465
-
466567 config_group_init(&stp_policy_subsys.su_group);
467568 mutex_init(&stp_policy_subsys.su_mutex);
468
- err = configfs_register_subsystem(&stp_policy_subsys);
469
-
470
- return err;
569
+ return configfs_register_subsystem(&stp_policy_subsys);
471570 }
472571
473572 void __exit stp_configfs_exit(void)