]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - drivers/hwtracing/stm/core.c
stm class: Fix a race in unlinking
[thirdparty/kernel/stable.git] / drivers / hwtracing / stm / core.c
CommitLineData
7bd1d409
AS
1/*
2 * System Trace Module (STM) infrastructure
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 * STM class implements generic infrastructure for System Trace Module devices
15 * as defined in MIPI STPv2 specification.
16 */
17
18#include <linux/uaccess.h>
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/device.h>
22#include <linux/compat.h>
23#include <linux/kdev_t.h>
24#include <linux/srcu.h>
25#include <linux/slab.h>
26#include <linux/stm.h>
27#include <linux/fs.h>
28#include <linux/mm.h>
29#include "stm.h"
30
31#include <uapi/linux/stm.h>
32
33static unsigned int stm_core_up;
34
35/*
36 * The SRCU here makes sure that STM device doesn't disappear from under a
37 * stm_source_write() caller, which may want to have as little overhead as
38 * possible.
39 */
40static struct srcu_struct stm_source_srcu;
41
42static ssize_t masters_show(struct device *dev,
43 struct device_attribute *attr,
44 char *buf)
45{
46 struct stm_device *stm = to_stm_device(dev);
47 int ret;
48
49 ret = sprintf(buf, "%u %u\n", stm->data->sw_start, stm->data->sw_end);
50
51 return ret;
52}
53
54static DEVICE_ATTR_RO(masters);
55
56static ssize_t channels_show(struct device *dev,
57 struct device_attribute *attr,
58 char *buf)
59{
60 struct stm_device *stm = to_stm_device(dev);
61 int ret;
62
63 ret = sprintf(buf, "%u\n", stm->data->sw_nchannels);
64
65 return ret;
66}
67
68static DEVICE_ATTR_RO(channels);
69
70static struct attribute *stm_attrs[] = {
71 &dev_attr_masters.attr,
72 &dev_attr_channels.attr,
73 NULL,
74};
75
76ATTRIBUTE_GROUPS(stm);
77
78static struct class stm_class = {
79 .name = "stm",
80 .dev_groups = stm_groups,
81};
82
83static int stm_dev_match(struct device *dev, const void *data)
84{
85 const char *name = data;
86
87 return sysfs_streq(name, dev_name(dev));
88}
89
90/**
91 * stm_find_device() - find stm device by name
92 * @buf: character buffer containing the name
93 *
94 * This is called when either policy gets assigned to an stm device or an
95 * stm_source device gets linked to an stm device.
96 *
97 * This grabs device's reference (get_device()) and module reference, both
98 * of which the calling path needs to make sure to drop with stm_put_device().
99 *
100 * Return: stm device pointer or null if lookup failed.
101 */
102struct stm_device *stm_find_device(const char *buf)
103{
104 struct stm_device *stm;
105 struct device *dev;
106
107 if (!stm_core_up)
108 return NULL;
109
110 dev = class_find_device(&stm_class, NULL, buf, stm_dev_match);
111 if (!dev)
112 return NULL;
113
114 stm = to_stm_device(dev);
115 if (!try_module_get(stm->owner)) {
f7c81c71 116 /* matches class_find_device() above */
7bd1d409
AS
117 put_device(dev);
118 return NULL;
119 }
120
121 return stm;
122}
123
124/**
125 * stm_put_device() - drop references on the stm device
126 * @stm: stm device, previously acquired by stm_find_device()
127 *
128 * This drops the module reference and device reference taken by
f7c81c71 129 * stm_find_device() or stm_char_open().
7bd1d409
AS
130 */
131void stm_put_device(struct stm_device *stm)
132{
133 module_put(stm->owner);
134 put_device(&stm->dev);
135}
136
137/*
138 * Internally we only care about software-writable masters here, that is the
139 * ones in the range [stm_data->sw_start..stm_data..sw_end], however we need
140 * original master numbers to be visible externally, since they are the ones
141 * that will appear in the STP stream. Thus, the internal bookkeeping uses
142 * $master - stm_data->sw_start to reference master descriptors and such.
143 */
144
145#define __stm_master(_s, _m) \
146 ((_s)->masters[(_m) - (_s)->data->sw_start])
147
148static inline struct stp_master *
149stm_master(struct stm_device *stm, unsigned int idx)
150{
151 if (idx < stm->data->sw_start || idx > stm->data->sw_end)
152 return NULL;
153
154 return __stm_master(stm, idx);
155}
156
157static int stp_master_alloc(struct stm_device *stm, unsigned int idx)
158{
159 struct stp_master *master;
160 size_t size;
161
162 size = ALIGN(stm->data->sw_nchannels, 8) / 8;
163 size += sizeof(struct stp_master);
164 master = kzalloc(size, GFP_ATOMIC);
165 if (!master)
166 return -ENOMEM;
167
168 master->nr_free = stm->data->sw_nchannels;
169 __stm_master(stm, idx) = master;
170
171 return 0;
172}
173
174static void stp_master_free(struct stm_device *stm, unsigned int idx)
175{
176 struct stp_master *master = stm_master(stm, idx);
177
178 if (!master)
179 return;
180
181 __stm_master(stm, idx) = NULL;
182 kfree(master);
183}
184
185static void stm_output_claim(struct stm_device *stm, struct stm_output *output)
186{
187 struct stp_master *master = stm_master(stm, output->master);
188
cde4ad83
AS
189 lockdep_assert_held(&stm->mc_lock);
190 lockdep_assert_held(&output->lock);
191
7bd1d409
AS
192 if (WARN_ON_ONCE(master->nr_free < output->nr_chans))
193 return;
194
195 bitmap_allocate_region(&master->chan_map[0], output->channel,
196 ilog2(output->nr_chans));
197
198 master->nr_free -= output->nr_chans;
199}
200
201static void
202stm_output_disclaim(struct stm_device *stm, struct stm_output *output)
203{
204 struct stp_master *master = stm_master(stm, output->master);
205
cde4ad83
AS
206 lockdep_assert_held(&stm->mc_lock);
207 lockdep_assert_held(&output->lock);
208
7bd1d409
AS
209 bitmap_release_region(&master->chan_map[0], output->channel,
210 ilog2(output->nr_chans));
211
212 output->nr_chans = 0;
213 master->nr_free += output->nr_chans;
214}
215
216/*
217 * This is like bitmap_find_free_region(), except it can ignore @start bits
218 * at the beginning.
219 */
220static int find_free_channels(unsigned long *bitmap, unsigned int start,
221 unsigned int end, unsigned int width)
222{
223 unsigned int pos;
224 int i;
225
226 for (pos = start; pos < end + 1; pos = ALIGN(pos, width)) {
227 pos = find_next_zero_bit(bitmap, end + 1, pos);
228 if (pos + width > end + 1)
229 break;
230
231 if (pos & (width - 1))
232 continue;
233
234 for (i = 1; i < width && !test_bit(pos + i, bitmap); i++)
235 ;
236 if (i == width)
237 return pos;
238 }
239
240 return -1;
241}
242
f45f40ad 243static int
7bd1d409
AS
244stm_find_master_chan(struct stm_device *stm, unsigned int width,
245 unsigned int *mstart, unsigned int mend,
246 unsigned int *cstart, unsigned int cend)
247{
248 struct stp_master *master;
249 unsigned int midx;
250 int pos, err;
251
252 for (midx = *mstart; midx <= mend; midx++) {
253 if (!stm_master(stm, midx)) {
254 err = stp_master_alloc(stm, midx);
255 if (err)
256 return err;
257 }
258
259 master = stm_master(stm, midx);
260
261 if (!master->nr_free)
262 continue;
263
264 pos = find_free_channels(master->chan_map, *cstart, cend,
265 width);
266 if (pos < 0)
267 continue;
268
269 *mstart = midx;
270 *cstart = pos;
271 return 0;
272 }
273
274 return -ENOSPC;
275}
276
277static int stm_output_assign(struct stm_device *stm, unsigned int width,
278 struct stp_policy_node *policy_node,
279 struct stm_output *output)
280{
281 unsigned int midx, cidx, mend, cend;
282 int ret = -EINVAL;
283
284 if (width > stm->data->sw_nchannels)
285 return -EINVAL;
286
287 if (policy_node) {
288 stp_policy_node_get_ranges(policy_node,
289 &midx, &mend, &cidx, &cend);
290 } else {
291 midx = stm->data->sw_start;
292 cidx = 0;
293 mend = stm->data->sw_end;
294 cend = stm->data->sw_nchannels - 1;
295 }
296
297 spin_lock(&stm->mc_lock);
cde4ad83 298 spin_lock(&output->lock);
7bd1d409
AS
299 /* output is already assigned -- shouldn't happen */
300 if (WARN_ON_ONCE(output->nr_chans))
301 goto unlock;
302
303 ret = stm_find_master_chan(stm, width, &midx, mend, &cidx, cend);
f45f40ad 304 if (ret < 0)
7bd1d409
AS
305 goto unlock;
306
307 output->master = midx;
308 output->channel = cidx;
309 output->nr_chans = width;
310 stm_output_claim(stm, output);
311 dev_dbg(&stm->dev, "assigned %u:%u (+%u)\n", midx, cidx, width);
312
313 ret = 0;
314unlock:
cde4ad83 315 spin_unlock(&output->lock);
7bd1d409
AS
316 spin_unlock(&stm->mc_lock);
317
318 return ret;
319}
320
321static void stm_output_free(struct stm_device *stm, struct stm_output *output)
322{
323 spin_lock(&stm->mc_lock);
cde4ad83 324 spin_lock(&output->lock);
7bd1d409
AS
325 if (output->nr_chans)
326 stm_output_disclaim(stm, output);
cde4ad83 327 spin_unlock(&output->lock);
7bd1d409
AS
328 spin_unlock(&stm->mc_lock);
329}
330
cde4ad83
AS
331static void stm_output_init(struct stm_output *output)
332{
333 spin_lock_init(&output->lock);
334}
335
7bd1d409
AS
336static int major_match(struct device *dev, const void *data)
337{
338 unsigned int major = *(unsigned int *)data;
339
340 return MAJOR(dev->devt) == major;
341}
342
343static int stm_char_open(struct inode *inode, struct file *file)
344{
345 struct stm_file *stmf;
346 struct device *dev;
347 unsigned int major = imajor(inode);
348 int err = -ENODEV;
349
350 dev = class_find_device(&stm_class, NULL, &major, major_match);
351 if (!dev)
352 return -ENODEV;
353
354 stmf = kzalloc(sizeof(*stmf), GFP_KERNEL);
355 if (!stmf)
356 return -ENOMEM;
357
cde4ad83 358 stm_output_init(&stmf->output);
7bd1d409
AS
359 stmf->stm = to_stm_device(dev);
360
361 if (!try_module_get(stmf->stm->owner))
362 goto err_free;
363
364 file->private_data = stmf;
365
366 return nonseekable_open(inode, file);
367
368err_free:
f7c81c71
AS
369 /* matches class_find_device() above */
370 put_device(dev);
7bd1d409
AS
371 kfree(stmf);
372
373 return err;
374}
375
376static int stm_char_release(struct inode *inode, struct file *file)
377{
378 struct stm_file *stmf = file->private_data;
379
380 stm_output_free(stmf->stm, &stmf->output);
f7c81c71
AS
381
382 /*
383 * matches the stm_char_open()'s
384 * class_find_device() + try_module_get()
385 */
7bd1d409
AS
386 stm_put_device(stmf->stm);
387 kfree(stmf);
388
389 return 0;
390}
391
392static int stm_file_assign(struct stm_file *stmf, char *id, unsigned int width)
393{
394 struct stm_device *stm = stmf->stm;
395 int ret;
396
397 stmf->policy_node = stp_policy_node_lookup(stm, id);
398
399 ret = stm_output_assign(stm, width, stmf->policy_node, &stmf->output);
400
401 if (stmf->policy_node)
402 stp_policy_node_put(stmf->policy_node);
403
404 return ret;
405}
406
f8560a9b
AS
407static ssize_t stm_write(struct stm_data *data, unsigned int master,
408 unsigned int channel, const char *buf, size_t count)
7bd1d409
AS
409{
410 unsigned int flags = STP_PACKET_TIMESTAMPED;
411 const unsigned char *p = buf, nil = 0;
412 size_t pos;
413 ssize_t sz;
414
415 for (pos = 0, p = buf; count > pos; pos += sz, p += sz) {
416 sz = min_t(unsigned int, count - pos, 8);
417 sz = data->packet(data, master, channel, STP_PACKET_DATA, flags,
418 sz, p);
419 flags = 0;
f8560a9b
AS
420
421 if (sz < 0)
422 break;
7bd1d409
AS
423 }
424
425 data->packet(data, master, channel, STP_PACKET_FLAG, 0, 0, &nil);
f8560a9b
AS
426
427 return pos;
7bd1d409
AS
428}
429
430static ssize_t stm_char_write(struct file *file, const char __user *buf,
431 size_t count, loff_t *ppos)
432{
433 struct stm_file *stmf = file->private_data;
434 struct stm_device *stm = stmf->stm;
435 char *kbuf;
436 int err;
437
f08b1826
AS
438 if (count + 1 > PAGE_SIZE)
439 count = PAGE_SIZE - 1;
440
7bd1d409
AS
441 /*
442 * if no m/c have been assigned to this writer up to this
443 * point, use "default" policy entry
444 */
445 if (!stmf->output.nr_chans) {
446 err = stm_file_assign(stmf, "default", 1);
447 /*
448 * EBUSY means that somebody else just assigned this
449 * output, which is just fine for write()
450 */
451 if (err && err != -EBUSY)
452 return err;
453 }
454
455 kbuf = kmalloc(count + 1, GFP_KERNEL);
456 if (!kbuf)
457 return -ENOMEM;
458
459 err = copy_from_user(kbuf, buf, count);
460 if (err) {
461 kfree(kbuf);
462 return -EFAULT;
463 }
464
f8560a9b
AS
465 count = stm_write(stm->data, stmf->output.master, stmf->output.channel,
466 kbuf, count);
7bd1d409
AS
467
468 kfree(kbuf);
469
470 return count;
471}
472
473static int stm_char_mmap(struct file *file, struct vm_area_struct *vma)
474{
475 struct stm_file *stmf = file->private_data;
476 struct stm_device *stm = stmf->stm;
477 unsigned long size, phys;
478
479 if (!stm->data->mmio_addr)
480 return -EOPNOTSUPP;
481
482 if (vma->vm_pgoff)
483 return -EINVAL;
484
485 size = vma->vm_end - vma->vm_start;
486
487 if (stmf->output.nr_chans * stm->data->sw_mmiosz != size)
488 return -EINVAL;
489
490 phys = stm->data->mmio_addr(stm->data, stmf->output.master,
491 stmf->output.channel,
492 stmf->output.nr_chans);
493
494 if (!phys)
495 return -EINVAL;
496
497 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
498 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
499 vm_iomap_memory(vma, phys, size);
500
501 return 0;
502}
503
504static int stm_char_policy_set_ioctl(struct stm_file *stmf, void __user *arg)
505{
506 struct stm_device *stm = stmf->stm;
507 struct stp_policy_id *id;
508 int ret = -EINVAL;
509 u32 size;
510
511 if (stmf->output.nr_chans)
512 return -EBUSY;
513
514 if (copy_from_user(&size, arg, sizeof(size)))
515 return -EFAULT;
516
517 if (size >= PATH_MAX + sizeof(*id))
518 return -EINVAL;
519
520 /*
521 * size + 1 to make sure the .id string at the bottom is terminated,
522 * which is also why memdup_user() is not useful here
523 */
524 id = kzalloc(size + 1, GFP_KERNEL);
525 if (!id)
526 return -ENOMEM;
527
528 if (copy_from_user(id, arg, size)) {
529 ret = -EFAULT;
530 goto err_free;
531 }
532
533 if (id->__reserved_0 || id->__reserved_1)
534 goto err_free;
535
536 if (id->width < 1 ||
537 id->width > PAGE_SIZE / stm->data->sw_mmiosz)
538 goto err_free;
539
540 ret = stm_file_assign(stmf, id->id, id->width);
541 if (ret)
542 goto err_free;
543
544 ret = 0;
545
546 if (stm->data->link)
547 ret = stm->data->link(stm->data, stmf->output.master,
548 stmf->output.channel);
549
f7c81c71 550 if (ret)
7bd1d409 551 stm_output_free(stmf->stm, &stmf->output);
7bd1d409
AS
552
553err_free:
554 kfree(id);
555
556 return ret;
557}
558
559static int stm_char_policy_get_ioctl(struct stm_file *stmf, void __user *arg)
560{
561 struct stp_policy_id id = {
562 .size = sizeof(id),
563 .master = stmf->output.master,
564 .channel = stmf->output.channel,
565 .width = stmf->output.nr_chans,
566 .__reserved_0 = 0,
567 .__reserved_1 = 0,
568 };
569
570 return copy_to_user(arg, &id, id.size) ? -EFAULT : 0;
571}
572
573static long
574stm_char_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
575{
576 struct stm_file *stmf = file->private_data;
577 struct stm_data *stm_data = stmf->stm->data;
578 int err = -ENOTTY;
579 u64 options;
580
581 switch (cmd) {
582 case STP_POLICY_ID_SET:
583 err = stm_char_policy_set_ioctl(stmf, (void __user *)arg);
584 if (err)
585 return err;
586
587 return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
588
589 case STP_POLICY_ID_GET:
590 return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
591
592 case STP_SET_OPTIONS:
593 if (copy_from_user(&options, (u64 __user *)arg, sizeof(u64)))
594 return -EFAULT;
595
596 if (stm_data->set_options)
597 err = stm_data->set_options(stm_data,
598 stmf->output.master,
599 stmf->output.channel,
600 stmf->output.nr_chans,
601 options);
602
603 break;
604 default:
605 break;
606 }
607
608 return err;
609}
610
611#ifdef CONFIG_COMPAT
612static long
613stm_char_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
614{
615 return stm_char_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
616}
617#else
618#define stm_char_compat_ioctl NULL
619#endif
620
621static const struct file_operations stm_fops = {
622 .open = stm_char_open,
623 .release = stm_char_release,
624 .write = stm_char_write,
625 .mmap = stm_char_mmap,
626 .unlocked_ioctl = stm_char_ioctl,
627 .compat_ioctl = stm_char_compat_ioctl,
628 .llseek = no_llseek,
629};
630
631static void stm_device_release(struct device *dev)
632{
633 struct stm_device *stm = to_stm_device(dev);
634
635 kfree(stm);
636}
637
638int stm_register_device(struct device *parent, struct stm_data *stm_data,
639 struct module *owner)
640{
641 struct stm_device *stm;
642 unsigned int nmasters;
643 int err = -ENOMEM;
644
645 if (!stm_core_up)
646 return -EPROBE_DEFER;
647
648 if (!stm_data->packet || !stm_data->sw_nchannels)
649 return -EINVAL;
650
7b3bb0e7 651 nmasters = stm_data->sw_end - stm_data->sw_start + 1;
7bd1d409
AS
652 stm = kzalloc(sizeof(*stm) + nmasters * sizeof(void *), GFP_KERNEL);
653 if (!stm)
654 return -ENOMEM;
655
656 stm->major = register_chrdev(0, stm_data->name, &stm_fops);
657 if (stm->major < 0)
658 goto err_free;
659
660 device_initialize(&stm->dev);
661 stm->dev.devt = MKDEV(stm->major, 0);
662 stm->dev.class = &stm_class;
663 stm->dev.parent = parent;
664 stm->dev.release = stm_device_release;
665
666 err = kobject_set_name(&stm->dev.kobj, "%s", stm_data->name);
667 if (err)
668 goto err_device;
669
670 err = device_add(&stm->dev);
671 if (err)
672 goto err_device;
673
c74f7e82 674 mutex_init(&stm->link_mutex);
7bd1d409
AS
675 spin_lock_init(&stm->link_lock);
676 INIT_LIST_HEAD(&stm->link_list);
677
678 spin_lock_init(&stm->mc_lock);
679 mutex_init(&stm->policy_mutex);
680 stm->sw_nmasters = nmasters;
681 stm->owner = owner;
682 stm->data = stm_data;
683 stm_data->stm = stm;
684
685 return 0;
686
687err_device:
f7c81c71 688 /* matches device_initialize() above */
7bd1d409
AS
689 put_device(&stm->dev);
690err_free:
691 kfree(stm);
692
693 return err;
694}
695EXPORT_SYMBOL_GPL(stm_register_device);
696
b4ca34aa
AS
697static int __stm_source_link_drop(struct stm_source_device *src,
698 struct stm_device *stm);
7bd1d409
AS
699
700void stm_unregister_device(struct stm_data *stm_data)
701{
702 struct stm_device *stm = stm_data->stm;
703 struct stm_source_device *src, *iter;
b4ca34aa 704 int i, ret;
7bd1d409 705
c74f7e82 706 mutex_lock(&stm->link_mutex);
7bd1d409 707 list_for_each_entry_safe(src, iter, &stm->link_list, link_entry) {
b4ca34aa
AS
708 ret = __stm_source_link_drop(src, stm);
709 /*
710 * src <-> stm link must not change under the same
711 * stm::link_mutex, so complain loudly if it has;
712 * also in this situation ret!=0 means this src is
713 * not connected to this stm and it should be otherwise
714 * safe to proceed with the tear-down of stm.
715 */
716 WARN_ON_ONCE(ret);
7bd1d409 717 }
c74f7e82 718 mutex_unlock(&stm->link_mutex);
7bd1d409
AS
719
720 synchronize_srcu(&stm_source_srcu);
721
722 unregister_chrdev(stm->major, stm_data->name);
723
724 mutex_lock(&stm->policy_mutex);
725 if (stm->policy)
726 stp_policy_unbind(stm->policy);
727 mutex_unlock(&stm->policy_mutex);
728
73a3ed19 729 for (i = stm->data->sw_start; i <= stm->data->sw_end; i++)
7bd1d409
AS
730 stp_master_free(stm, i);
731
732 device_unregister(&stm->dev);
733 stm_data->stm = NULL;
734}
735EXPORT_SYMBOL_GPL(stm_unregister_device);
736
c74f7e82
AS
737/*
738 * stm::link_list access serialization uses a spinlock and a mutex; holding
739 * either of them guarantees that the list is stable; modification requires
740 * holding both of them.
741 *
742 * Lock ordering is as follows:
743 * stm::link_mutex
744 * stm::link_lock
745 * src::link_lock
746 */
747
7bd1d409
AS
748/**
749 * stm_source_link_add() - connect an stm_source device to an stm device
750 * @src: stm_source device
751 * @stm: stm device
752 *
753 * This function establishes a link from stm_source to an stm device so that
754 * the former can send out trace data to the latter.
755 *
756 * Return: 0 on success, -errno otherwise.
757 */
758static int stm_source_link_add(struct stm_source_device *src,
759 struct stm_device *stm)
760{
761 char *id;
762 int err;
763
c74f7e82 764 mutex_lock(&stm->link_mutex);
7bd1d409
AS
765 spin_lock(&stm->link_lock);
766 spin_lock(&src->link_lock);
767
768 /* src->link is dereferenced under stm_source_srcu but not the list */
769 rcu_assign_pointer(src->link, stm);
770 list_add_tail(&src->link_entry, &stm->link_list);
771
772 spin_unlock(&src->link_lock);
773 spin_unlock(&stm->link_lock);
c74f7e82 774 mutex_unlock(&stm->link_mutex);
7bd1d409
AS
775
776 id = kstrdup(src->data->name, GFP_KERNEL);
777 if (id) {
778 src->policy_node =
779 stp_policy_node_lookup(stm, id);
780
781 kfree(id);
782 }
783
784 err = stm_output_assign(stm, src->data->nr_chans,
785 src->policy_node, &src->output);
786
787 if (src->policy_node)
788 stp_policy_node_put(src->policy_node);
789
790 if (err)
791 goto fail_detach;
792
793 /* this is to notify the STM device that a new link has been made */
794 if (stm->data->link)
795 err = stm->data->link(stm->data, src->output.master,
796 src->output.channel);
797
798 if (err)
799 goto fail_free_output;
800
801 /* this is to let the source carry out all necessary preparations */
802 if (src->data->link)
803 src->data->link(src->data);
804
805 return 0;
806
807fail_free_output:
808 stm_output_free(stm, &src->output);
7bd1d409
AS
809
810fail_detach:
c74f7e82 811 mutex_lock(&stm->link_mutex);
7bd1d409
AS
812 spin_lock(&stm->link_lock);
813 spin_lock(&src->link_lock);
814
815 rcu_assign_pointer(src->link, NULL);
816 list_del_init(&src->link_entry);
817
818 spin_unlock(&src->link_lock);
819 spin_unlock(&stm->link_lock);
c74f7e82 820 mutex_unlock(&stm->link_mutex);
7bd1d409
AS
821
822 return err;
823}
824
825/**
826 * __stm_source_link_drop() - detach stm_source from an stm device
827 * @src: stm_source device
828 * @stm: stm device
829 *
830 * If @stm is @src::link, disconnect them from one another and put the
831 * reference on the @stm device.
832 *
c74f7e82 833 * Caller must hold stm::link_mutex.
7bd1d409 834 */
b4ca34aa
AS
835static int __stm_source_link_drop(struct stm_source_device *src,
836 struct stm_device *stm)
7bd1d409 837{
0df771de 838 struct stm_device *link;
b4ca34aa 839 int ret = 0;
0df771de 840
c74f7e82
AS
841 lockdep_assert_held(&stm->link_mutex);
842
c74f7e82
AS
843 /* for stm::link_list modification, we hold both mutex and spinlock */
844 spin_lock(&stm->link_lock);
7bd1d409 845 spin_lock(&src->link_lock);
0df771de 846 link = srcu_dereference_check(src->link, &stm_source_srcu, 1);
b4ca34aa
AS
847
848 /*
849 * The linked device may have changed since we last looked, because
850 * we weren't holding the src::link_lock back then; if this is the
851 * case, tell the caller to retry.
852 */
853 if (link != stm) {
854 ret = -EAGAIN;
1810f2c4 855 goto unlock;
b4ca34aa 856 }
7bd1d409 857
0df771de 858 stm_output_free(link, &src->output);
7bd1d409
AS
859 list_del_init(&src->link_entry);
860 /* matches stm_find_device() from stm_source_link_store() */
0df771de 861 stm_put_device(link);
7bd1d409
AS
862 rcu_assign_pointer(src->link, NULL);
863
1810f2c4 864unlock:
7bd1d409 865 spin_unlock(&src->link_lock);
c74f7e82 866 spin_unlock(&stm->link_lock);
b4ca34aa
AS
867
868 if (!ret && src->data->unlink)
869 src->data->unlink(src->data);
870
871 return ret;
7bd1d409
AS
872}
873
874/**
875 * stm_source_link_drop() - detach stm_source from its stm device
876 * @src: stm_source device
877 *
878 * Unlinking means disconnecting from source's STM device; after this
879 * writes will be unsuccessful until it is linked to a new STM device.
880 *
881 * This will happen on "stm_source_link" sysfs attribute write to undo
882 * the existing link (if any), or on linked STM device's de-registration.
883 */
884static void stm_source_link_drop(struct stm_source_device *src)
885{
886 struct stm_device *stm;
b4ca34aa 887 int idx, ret;
7bd1d409 888
b4ca34aa 889retry:
7bd1d409 890 idx = srcu_read_lock(&stm_source_srcu);
b4ca34aa
AS
891 /*
892 * The stm device will be valid for the duration of this
893 * read section, but the link may change before we grab
894 * the src::link_lock in __stm_source_link_drop().
895 */
7bd1d409
AS
896 stm = srcu_dereference(src->link, &stm_source_srcu);
897
b4ca34aa 898 ret = 0;
7bd1d409 899 if (stm) {
c74f7e82 900 mutex_lock(&stm->link_mutex);
b4ca34aa 901 ret = __stm_source_link_drop(src, stm);
c74f7e82 902 mutex_unlock(&stm->link_mutex);
7bd1d409
AS
903 }
904
905 srcu_read_unlock(&stm_source_srcu, idx);
b4ca34aa
AS
906
907 /* if it did change, retry */
908 if (ret == -EAGAIN)
909 goto retry;
7bd1d409
AS
910}
911
912static ssize_t stm_source_link_show(struct device *dev,
913 struct device_attribute *attr,
914 char *buf)
915{
916 struct stm_source_device *src = to_stm_source_device(dev);
917 struct stm_device *stm;
918 int idx, ret;
919
920 idx = srcu_read_lock(&stm_source_srcu);
921 stm = srcu_dereference(src->link, &stm_source_srcu);
922 ret = sprintf(buf, "%s\n",
923 stm ? dev_name(&stm->dev) : "<none>");
924 srcu_read_unlock(&stm_source_srcu, idx);
925
926 return ret;
927}
928
929static ssize_t stm_source_link_store(struct device *dev,
930 struct device_attribute *attr,
931 const char *buf, size_t count)
932{
933 struct stm_source_device *src = to_stm_source_device(dev);
934 struct stm_device *link;
935 int err;
936
937 stm_source_link_drop(src);
938
939 link = stm_find_device(buf);
940 if (!link)
941 return -EINVAL;
942
943 err = stm_source_link_add(src, link);
f7c81c71
AS
944 if (err) {
945 /* matches the stm_find_device() above */
7bd1d409 946 stm_put_device(link);
f7c81c71 947 }
7bd1d409
AS
948
949 return err ? : count;
950}
951
952static DEVICE_ATTR_RW(stm_source_link);
953
954static struct attribute *stm_source_attrs[] = {
955 &dev_attr_stm_source_link.attr,
956 NULL,
957};
958
959ATTRIBUTE_GROUPS(stm_source);
960
961static struct class stm_source_class = {
962 .name = "stm_source",
963 .dev_groups = stm_source_groups,
964};
965
966static void stm_source_device_release(struct device *dev)
967{
968 struct stm_source_device *src = to_stm_source_device(dev);
969
970 kfree(src);
971}
972
973/**
974 * stm_source_register_device() - register an stm_source device
975 * @parent: parent device
976 * @data: device description structure
977 *
978 * This will create a device of stm_source class that can write
979 * data to an stm device once linked.
980 *
981 * Return: 0 on success, -errno otherwise.
982 */
983int stm_source_register_device(struct device *parent,
984 struct stm_source_data *data)
985{
986 struct stm_source_device *src;
987 int err;
988
989 if (!stm_core_up)
990 return -EPROBE_DEFER;
991
992 src = kzalloc(sizeof(*src), GFP_KERNEL);
993 if (!src)
994 return -ENOMEM;
995
996 device_initialize(&src->dev);
997 src->dev.class = &stm_source_class;
998 src->dev.parent = parent;
999 src->dev.release = stm_source_device_release;
1000
1001 err = kobject_set_name(&src->dev.kobj, "%s", data->name);
1002 if (err)
1003 goto err;
1004
1005 err = device_add(&src->dev);
1006 if (err)
1007 goto err;
1008
cde4ad83 1009 stm_output_init(&src->output);
7bd1d409
AS
1010 spin_lock_init(&src->link_lock);
1011 INIT_LIST_HEAD(&src->link_entry);
1012 src->data = data;
1013 data->src = src;
1014
1015 return 0;
1016
1017err:
1018 put_device(&src->dev);
1019 kfree(src);
1020
1021 return err;
1022}
1023EXPORT_SYMBOL_GPL(stm_source_register_device);
1024
1025/**
1026 * stm_source_unregister_device() - unregister an stm_source device
1027 * @data: device description that was used to register the device
1028 *
1029 * This will remove a previously created stm_source device from the system.
1030 */
1031void stm_source_unregister_device(struct stm_source_data *data)
1032{
1033 struct stm_source_device *src = data->src;
1034
1035 stm_source_link_drop(src);
1036
1037 device_destroy(&stm_source_class, src->dev.devt);
1038}
1039EXPORT_SYMBOL_GPL(stm_source_unregister_device);
1040
1041int stm_source_write(struct stm_source_data *data, unsigned int chan,
1042 const char *buf, size_t count)
1043{
1044 struct stm_source_device *src = data->src;
1045 struct stm_device *stm;
1046 int idx;
1047
1048 if (!src->output.nr_chans)
1049 return -ENODEV;
1050
1051 if (chan >= src->output.nr_chans)
1052 return -EINVAL;
1053
1054 idx = srcu_read_lock(&stm_source_srcu);
1055
1056 stm = srcu_dereference(src->link, &stm_source_srcu);
1057 if (stm)
f8560a9b
AS
1058 count = stm_write(stm->data, src->output.master,
1059 src->output.channel + chan,
1060 buf, count);
7bd1d409
AS
1061 else
1062 count = -ENODEV;
1063
1064 srcu_read_unlock(&stm_source_srcu, idx);
1065
1066 return count;
1067}
1068EXPORT_SYMBOL_GPL(stm_source_write);
1069
1070static int __init stm_core_init(void)
1071{
1072 int err;
1073
1074 err = class_register(&stm_class);
1075 if (err)
1076 return err;
1077
1078 err = class_register(&stm_source_class);
1079 if (err)
1080 goto err_stm;
1081
1082 err = stp_configfs_init();
1083 if (err)
1084 goto err_src;
1085
1086 init_srcu_struct(&stm_source_srcu);
1087
1088 stm_core_up++;
1089
1090 return 0;
1091
1092err_src:
1093 class_unregister(&stm_source_class);
1094err_stm:
1095 class_unregister(&stm_class);
1096
1097 return err;
1098}
1099
1100module_init(stm_core_init);
1101
1102static void __exit stm_core_exit(void)
1103{
1104 cleanup_srcu_struct(&stm_source_srcu);
1105 class_unregister(&stm_source_class);
1106 class_unregister(&stm_class);
1107 stp_configfs_exit();
1108}
1109
1110module_exit(stm_core_exit);
1111
1112MODULE_LICENSE("GPL v2");
1113MODULE_DESCRIPTION("System Trace Module device class");
1114MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");