-
Notifications
You must be signed in to change notification settings - Fork 3
/
fault-injection.c
2390 lines (1994 loc) · 52.3 KB
/
fault-injection.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Fault injection framework.
*
* Copyright (c) 2018 ProfitBricks GmbH.
* Authors: Roman Pen <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, version 2 of the
* License.
*/
#include <linux/slab.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/delay.h>
#include <linux/string.h>
#include <linux/random.h>
#include <linux/uaccess.h>
#include <linux/debugfs.h>
#define FAULT_INJECT_MODULE
#include "fault-injection.h"
#define FAULT_HEADER "fault"
#define GROUP_HEADER "group"
#define CLASS_HEADER "class"
#define ADDR_HEADER "address"
#define FUNC_HEADER "function+off/size"
#define FILE_HEADER "file:line"
#define FAULT_LINE_SZ 128 /* 126 letters, 1 newline, 1 null byte.
* before changing anything check
* 'format_fault_point' */
enum fault_type {
DELAY_FAULT = 0, /* place first faults which do not generate
* errors to keep probing all of them */
ERROR_FAULT,
PANIC_FAULT,
FAULTS_NUM /* should be the last */
};
const char fault_type_chars[FAULTS_NUM] = {
'D', 'E', 'P',
};
const char *fault_type_strings[FAULTS_NUM] = {
"delay", "error", "panic",
};
struct fault_cfg {
struct dentry *dentry;
const char *name;
enum fault_type type;
bool enabled;
bool task_filter;
atomic64_t hits;
atomic64_t injected;
atomic_t times;
unsigned short probability;
unsigned short interval;
struct fault_group *group;
union {
struct {
unsigned int beg;
unsigned int end;
} delay_us;
DECLARE_BITMAP(error_mask, FAULT_ERRORS_SZ);
};
};
struct fault_group {
struct fault_fs_ref fs_ref;
struct fault_inject *inj;
struct dentry *dentry;
unsigned short id;
atomic_t ref;
struct list_head list;
struct mutex lock;
wait_queue_head_t wait;
struct fault_cfg faults[FAULTS_NUM];
};
/* Keep in sync with asm constructor, see arch_static_branch */
struct fault_point {
struct static_key key; /* always the first */
unsigned int magic;
unsigned int line;
const char *file;
const char *class;
struct fault_group *group;
};
static inline void fault_fs_ref_init(struct fault_fs_ref *fs_ref)
{
atomic_set(&fs_ref->ref, 0);
init_waitqueue_head(&fs_ref->wait);
}
static inline void *priv_from_dentry(struct dentry *dentry)
{
return dentry->d_inode->i_private;
}
static inline struct fault_fs_ref *ref_from_fault_inject(
void *priv)
{
struct fault_inject *inj = priv;
return &inj->fs_ref;
}
static inline struct fault_fs_ref *ref_from_fault_group(
void *priv)
{
struct fault_group *group = priv;
return &group->fs_ref;
}
static inline struct fault_fs_ref *ref_from_fault_cfg(
void *priv)
{
struct fault_cfg *cfg = priv;
return &cfg->group->fs_ref;
}
/**
* fault_fs_ref_get() - Returns private pointer of dentry if dentry is still
* hashed.
*
* Why do we need this? Because debugfs has stupid race when someone calls
* debugfs_remove(), but another task performs file open and eventually
* succeeds, but caller of debugfs_remove() is absolutely sure that dentry
* was removed. Here we check under the lock that dentry is still hashed,
* if so the reference is increased, so even after the lock is released
* out data is protected by the reference counter.
*/
static void *fault_fs_ref_get(
struct dentry *dentry,
struct fault_fs_ref *(*conv)(void *))
{
struct fault_fs_ref *fs_ref;
void *priv;
spin_lock(&dentry->d_lock);
if (d_unhashed(dentry))
priv = NULL;
else {
priv = priv_from_dentry(dentry);
fs_ref = conv(priv);
atomic_inc(&fs_ref->ref);
}
spin_unlock(&dentry->d_lock);
return priv;
}
static void fault_fs_ref_put(struct fault_fs_ref *fs_ref)
{
BUG_ON(atomic_read(&fs_ref->ref) == 0);
if (atomic_dec_and_test(&fs_ref->ref))
wake_up(&fs_ref->wait);
}
static void fault_fs_ref_wait_grace_period(struct fault_fs_ref *fs_ref)
{
wait_event(fs_ref->wait, atomic_read(&fs_ref->ref) == 0);
}
static void fault_dentry_remove(struct fault_fs_ref *fs_ref,
struct dentry *dentry)
{
debugfs_remove_recursive(dentry);
fault_fs_ref_wait_grace_period(fs_ref);
}
static struct fault_group *group_allocate(struct fault_inject *inj,
unsigned int id)
{
int i;
struct fault_group *group;
group = kzalloc(sizeof(*group), GFP_KERNEL);
if (!group)
return NULL;
group->inj = inj;
group->id = id;
fault_fs_ref_init(&group->fs_ref);
atomic_set(&group->ref, 0);
INIT_LIST_HEAD(&group->list);
mutex_init(&group->lock);
init_waitqueue_head(&group->wait);
BUILD_BUG_ON(ARRAY_SIZE(group->faults) != FAULTS_NUM);
BUILD_BUG_ON(ARRAY_SIZE(group->faults) !=
ARRAY_SIZE(fault_type_strings));
for (i = 0; i < ARRAY_SIZE(group->faults); i++) {
group->faults[i] = (struct fault_cfg) {
.type = (enum fault_type)i,
.name = fault_type_strings[i],
.enabled = false,
.task_filter = false,
.hits = ATOMIC64_INIT(0),
.injected = ATOMIC64_INIT(0),
.times = ATOMIC_INIT(-1),
.probability = 100,
.interval = 1,
.delay_us = {0, 0},
.group = group,
};
}
return group;
}
static void group_free(struct fault_group *group)
{
kfree(group);
}
static void __group_get(struct fault_group *group)
{
BUG_ON(atomic_read(&group->ref) < 0);
atomic_inc(&group->ref);
}
/**
* group_get() - Gets group pointer from the fault point.
*
* If fault point has a group pointer, reference will be increased and group
* will be returned. In case of error NULL will be returned.
*
* Function can be called from fault injection, thus any kind of contexts are
* possible, even it can be called from interrupts.
*/
static struct fault_group *group_get(struct fault_inject *inj,
struct fault_point *fault)
{
unsigned long flags;
struct fault_group *group;
spin_lock_irqsave(&inj->lock, flags);
group = fault->group;
if (likely(group)) {
__group_get(group);
BUG_ON(group->inj != inj);
}
spin_unlock_irqrestore(&inj->lock, flags);
return group;
}
static bool group_put(struct fault_group *group)
{
int ref;
ref = atomic_sub_return(1, &group->ref);
BUG_ON(ref < 0);
if (ref == 0) {
group_free(group);
return true;
} else if (ref == 1) {
/*
* We do not wake up on 0 reference, since it is stupid to
* wait for group->ref == 0, because an object will be deleted
* underneath waiter. Here we do wake up on last reference.
*/
wake_up(&group->wait);
}
return false;
}
static void group_wait_last_ref(struct fault_group *group)
{
wait_event(group->wait, atomic_read(&group->ref) == 1);
}
static bool group_list_del_and_put(struct fault_group *group)
{
struct fault_group *group2put = NULL;
spin_lock_irq(&group->inj->lock);
if (likely(!list_empty(&group->list))) {
list_del_init(&group->list);
group2put = group;
}
spin_unlock_irq(&group->inj->lock);
if (group2put) {
group_put(group2put);
return true;
} else
return false;
}
static bool __group_unlink_and_put(struct fault_group *group,
struct fault_point *fault)
{
struct fault_group *group2put = NULL;
spin_lock_irq(&group->inj->lock);
if (fault->group == group) {
group2put = fault->group;
fault->group = NULL;
}
spin_unlock_irq(&group->inj->lock);
if (group2put) {
group_put(group2put);
return true;
} else
return false;
}
static bool group_unlink_and_put(struct fault_group *group,
struct fault_point *fault)
{
int i;
bool ok;
mutex_lock(&group->lock);
ok = __group_unlink_and_put(group, fault);
if (unlikely(!ok)) {
mutex_unlock(&group->lock);
return false;
}
for (i = 0; i < ARRAY_SIZE(group->faults); i++)
if (group->faults[i].enabled)
static_key_slow_dec(&fault->key);
mutex_unlock(&group->lock);
return true;
}
static int
group_unlink_from_fault(struct fault_inject *inj, struct fault_point *fault,
struct jump_entry *entry, void *priv)
{
bool ok;
struct fault_group *group = priv;
if (fault->group == group) {
ok = group_unlink_and_put(group, fault);
BUG_ON(!ok);
}
return 0;
}
static void group_list_add_and_get(struct fault_group *group)
{
spin_lock_irq(&group->inj->lock);
list_add(&group->list, &group->inj->list_groups);
__group_get(group);
spin_unlock_irq(&group->inj->lock);
}
static bool group_link_and_get(struct fault_group *group,
struct fault_point *fault)
{
bool ok = false;
spin_lock_irq(&group->inj->lock);
if (fault->group == NULL) {
fault->group = group;
__group_get(group);
ok = true;
}
spin_unlock_irq(&group->inj->lock);
return ok;
}
static struct fault_group *group_find_and_get(struct fault_inject *inj,
unsigned int id)
{
struct fault_group *group;
spin_lock_irq(&inj->lock);
list_for_each_entry(group, &inj->list_groups, list) {
if (group->id == id) {
BUG_ON(atomic_read(&group->ref) <= 0);
__group_get(group);
BUG_ON(group->inj != inj);
spin_unlock_irq(&inj->lock);
return group;
}
}
spin_unlock_irq(&inj->lock);
return NULL;
}
struct fault_iter {
struct jump_entry *start;
struct jump_entry *iter;
struct jump_entry *stop;
struct fault_group *group;
};
static inline struct fault_point *__fault_iter_to_valid(
struct fault_inject *inj,
struct fault_iter *it)
{
for (; it->iter < it->stop; it->iter++) {
struct fault_point *f;
f = (struct fault_point *)it->iter->key;
if (f->magic != FAULT_MAGIC)
continue;
/*
* See 'jump_label_invalidate_module_init', if we are in
* a module init code - it will be set to zero. Skip it.\
*/
if (it->iter->code == 0x0)
continue;
/* Warn if somebody is naughty */
WARN(within_module_init(it->iter->code, inj->mod),
"Fault was injected into module init code, addr 0x%016llx",
it->iter->code);
/* Check group match if specified */
if (it->group && it->group != f->group)
continue;
return f;
}
return NULL;
}
static inline struct fault_point *fault_iter_next(
struct fault_inject *inj,
struct fault_iter *it)
{
if (it->iter >= it->stop)
return NULL;
it->iter++;
return __fault_iter_to_valid(inj, it);
}
static inline struct fault_point *fault_iter_curr(
struct fault_inject *inj,
struct fault_iter *it)
{
return __fault_iter_to_valid(inj, it);
}
static inline bool fault_iter_init(struct fault_inject *inj,
struct fault_iter *it,
struct fault_group *group,
unsigned int pos)
{
if (inj->mod->num_jump_entries && pos >= inj->mod->num_jump_entries)
return false;
it->start = inj->mod->jump_entries;
it->iter = it->start + pos;
it->stop = it->start + inj->mod->num_jump_entries;
it->group = group;
return true;
}
static inline unsigned int fault_iter_pos(struct fault_iter *it)
{
return (it->iter - it->start);
}
static int for_each_fault_inject_entry(struct fault_inject *inj,
int (*cb)(struct fault_inject *inj,
struct fault_point *fault,
struct jump_entry *entry,
void *priv),
void *priv)
{
struct fault_iter it;
struct fault_point *f;
bool init;
int rc;
init = fault_iter_init(inj, &it, NULL /* no group */, 0);
BUG_ON(!init);
for (f = fault_iter_curr(inj, &it); f; f = fault_iter_next(inj, &it)) {
rc = cb(inj, f, it.iter, priv);
if (rc)
return rc;
}
return 0;
}
struct fault_as_ret {
struct fault_point *fault;
void *addr;
};
static int fault_by_target_addr(struct fault_inject *inj,
struct fault_point *fault,
struct jump_entry *entry,
void *priv)
{
struct fault_as_ret *ret = priv;
if (entry->target == (unsigned long)ret->addr) {
ret->fault = fault;
return 1;
}
return 0;
}
static int fault_by_code_addr(struct fault_inject *inj,
struct fault_point *fault,
struct jump_entry *entry, void *priv)
{
struct fault_as_ret *ret = priv;
if (entry->code == (unsigned long)ret->addr) {
ret->fault = fault;
return 1;
}
return 0;
}
static struct fault_point *find_fault_by_target(struct fault_inject *inj,
void *addr)
{
struct fault_as_ret ret = {
.addr = addr,
.fault = NULL,
};
for_each_fault_inject_entry(
inj, fault_by_target_addr, &ret);
return ret.fault;
}
static inline void __do_udelay(unsigned int delay_us)
{
unsigned int d;
while (delay_us) {
d = min(delay_us, 1000u);
udelay(d);
delay_us -= d;
}
}
static inline void do_fault_delay(struct fault_cfg *cfg)
{
unsigned int beg, end, delay;
/* Fetch them first to avoid non-atomic set. See @delay_us_store */
beg = cfg->delay_us.beg;
end = cfg->delay_us.end;
if (end <= beg)
delay = beg;
else
delay = beg + prandom_u32_max(end - beg + 1);
__do_udelay(delay);
}
static inline bool task_may_inject_fault(struct fault_cfg *cfg,
struct task_struct *task)
{
/* This is Linux kernel configuration, see /proc/{pid}/make-it-fail */
#ifdef CONFIG_FAULT_INJECTION
return !cfg->task_filter || (!in_interrupt() && task->make_it_fail);
#else
return true;
#endif
}
static int inject_fault(struct fault_cfg *cfg)
{
int ret;
unsigned int bit_off, bit;
unsigned long long hits, injected;
hits = atomic64_add_return(1, &cfg->hits);
if (!task_may_inject_fault(cfg, current))
return 0;
if (atomic_read(&cfg->times) == 0)
return 0;
BUG_ON(cfg->interval == 0);
if ((hits - 1) % cfg->interval)
return 0;
BUG_ON(cfg->probability == 0);
if (cfg->probability < 100 &&
cfg->probability <= prandom_u32() % 100)
return 0;
injected = atomic64_add_return(1, &cfg->injected);
atomic_dec_if_positive(&cfg->times);
switch (cfg->type) {
case ERROR_FAULT:
/* Except zero bit */
bit_off = injected % (FAULT_ERRORS_SZ - 1) + 1;
bit = find_next_bit(cfg->error_mask,
FAULT_ERRORS_SZ, bit_off);
/* Start from the beginning */
if (bit >= FAULT_ERRORS_SZ && bit_off > 1) {
bit = find_next_bit(cfg->error_mask, bit_off, 1);
if (bit >= bit_off)
bit = FAULT_ERRORS_SZ;
}
if (bit < FAULT_ERRORS_SZ)
ret = -bit;
else
ret = 0;
break;
case DELAY_FAULT:
do_fault_delay(cfg);
ret = 0;
break;
case PANIC_FAULT:
panic("Panic from fault injection");
ret = 0;
break;
default:
BUG_ON(1);
ret = 0;
break;
}
return ret;
}
int inject_faults_by_target(struct fault_inject *inj, void *addr)
{
int i, err = 0;
struct fault_group *group;
struct fault_cfg *cfg;
struct fault_point *fp;
fp = find_fault_by_target(inj, addr);
BUG_ON(fp == NULL);
group = group_get(inj, fp);
if (!group)
return 0;
for (i = 0; i < ARRAY_SIZE(group->faults); i++) {
cfg = &group->faults[i];
if (!cfg->enabled)
continue;
err = inject_fault(cfg);
if (err)
goto out;
}
out:
group_put(group);
return err;
}
EXPORT_SYMBOL_GPL(inject_faults_by_target);
static __printf(2, 3) int __seq_printf(struct seq_file *sf,
const char *fmt, ...)
{
va_list args;
int i;
va_start(args, fmt);
if (sf) {
i = sf->count;
seq_vprintf(sf, fmt, args);
i = sf->count - i;
}
else
i = vsnprintf(NULL, 0, fmt, args);
va_end(args);
return i;
}
static int seq_format_header(struct seq_file *sf, struct fault_inject *inj)
{
unsigned int width;
BUG_ON(inj->max_func_sz < sizeof(FUNC_HEADER) - 1);
width = inj->max_func_sz -
(sizeof(FUNC_HEADER) - 1) +
(sizeof(FILE_HEADER) - 1);
return __seq_printf(sf, "%s %s %s %s %s %*s\n",
FAULT_HEADER,
GROUP_HEADER,
CLASS_HEADER,
ADDR_HEADER,
FUNC_HEADER,
width,
FILE_HEADER);
}
static int seq_format_fault_point(struct seq_file *sf,
struct fault_inject *inj,
struct fault_point *fault,
struct jump_entry *entry)
{
unsigned long func_sz;
unsigned int file_sz;
const char *file = fault->file;
struct fault_group *group;
/* The following arrays should be in sync with FAULT_LINE_SZ */
char fault_buf[6] = "-----";
char group_buf[6] = "-----";
char class_buf[6] = "-----";
char addr_buf[19];
char func_buf[65];
char file_buf[25];
char *tmp;
BUILD_BUG_ON(sizeof(fault_buf) + sizeof(group_buf) +
sizeof(class_buf) + sizeof(addr_buf) +
sizeof(func_buf) + sizeof(file_buf) + 1
!= FAULT_LINE_SZ);
group = group_get(inj, fault);
if (group) {
int i;
BUILD_BUG_ON(ARRAY_SIZE(fault->group->faults) >=
sizeof(fault_buf));
/* Fill in fault buffer */
for (i = 0; i < ARRAY_SIZE(fault->group->faults); i++) {
if (!fault->group->faults[i].enabled)
continue;
fault_buf[i] = fault_type_chars[
fault->group->faults[i].type];
}
/* Fill in group buffer, never overflows */
snprintf(group_buf, sizeof(group_buf), "%5u",
fault->group->id);
group_put(group);
group = NULL;
}
/* Fill in addr buffer, never overflows */
snprintf(addr_buf, sizeof(addr_buf), "0x%016llx", entry->code);
/* Fill in class buffer, can overflow */
if (fault->class)
snprintf(class_buf, sizeof(class_buf), "%5s", fault->class);
/* Fill in func buffer */
func_sz = snprintf(func_buf, sizeof(func_buf), "%pF",
(void *)entry->code);
tmp = strchr(func_buf, ' ');
if (tmp) {
/* Skip module name, we really aware where we are */
func_sz = tmp - func_buf;
func_buf[func_sz] = '\0';
}
if (func_sz >= sizeof(func_buf)) {
/* Overflow? Mark with dots and do size correction */
memcpy(func_buf + sizeof(func_buf) - 4, "...", 3);
func_sz = sizeof(func_buf) - 1;
}
/* Fill in file buffer */
file = strrchr(fault->file, '/');
if (file)
file++;
else
file = fault->file;
file_sz = snprintf(file_buf, sizeof(file_buf), "%s:%u",
file, fault->line);
if (file_sz >= sizeof(file_buf)) {
/* Overflow? Mark with dots and do size correction */
memcpy(file_buf + sizeof(file_buf) - 4, "...", 3);
file_sz = sizeof(file_buf) - 1;
}
/* Update max */
if (inj->max_func_sz < func_sz)
inj->max_func_sz = func_sz;
return __seq_printf(sf, "%s %s %s %s %s %*s\n",
fault_buf,
group_buf,
class_buf,
addr_buf,
func_buf,
(unsigned int)(inj->max_func_sz - func_sz + file_sz),
file_buf);
}
static int dry_format_fault_point(struct fault_inject *inj,
struct fault_point *fault,
struct jump_entry *entry, void *notused)
{
int rc;
rc = seq_format_fault_point(NULL, inj, fault, entry);
if (unlikely(rc < 0))
return rc;
return 0;
}
struct fault_line {
void *priv;
struct mutex mutex;
unsigned int loff;
unsigned int len;
char buf[FAULT_LINE_SZ];
};
static inline struct fault_line *fault_line_create(void *priv)
{
struct fault_line *line;
line = kmalloc(sizeof(*line), GFP_KERNEL);
if (unlikely(line == NULL))
return NULL;
mutex_init(&line->mutex);
line->priv = priv;
line->loff = 0;
line->len = 0;
return line;
}
static ssize_t cache_user_line(struct fault_line *line,
const char __user *from,
size_t *off, size_t size)
{
unsigned int len, res, loff;
char *end;
BUG_ON(*off >= size);
BUG_ON(line->loff >= sizeof(line->buf));
len = min(size - *off, sizeof(line->buf) - line->loff);
res = copy_from_user(line->buf + line->loff, from + *off, len);
if (res) {
line->len = 0;
line->loff = 0;
return -EFAULT;
}
loff = line->loff;
line->loff += len;
end = strnstr(line->buf, "\n", line->loff);
if (!end) {
if (line->loff == sizeof(line->buf)) {
line->len = 0;
line->loff = 0;
/* Buffer is over but where is \n? */
return -EINVAL;
}
return *off + len;
}
end[0] = '\0';
line->len = end - line->buf;
line->loff = 0;
/* Advance the offset to the beginning of a new line */
*off += line->len - loff + 1;
return 0;
}
/**
* The end of common fault code, a lot of debugfs stuff is the following.
*/
#define FAULT_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt, __conv) \
static int __fops ## _open(struct inode *inode, struct file *file) \
{ \
void *priv; \
\
__simple_attr_check_format(__fmt, 0ull); \
priv = fault_fs_ref_get(file->f_path.dentry, __conv); \
if (unlikely(!priv)) \
return -ENOENT; \
return simple_attr_open(inode, file, __get, __set, __fmt); \
} \
static int __fops ## _close(struct inode *inode, struct file *file) \
{ \
simple_attr_release(inode, file); \
fault_fs_ref_put(__conv(priv_from_dentry(file->f_path.dentry)));\
return 0; \
} \
static const struct file_operations __fops = { \
.owner = THIS_MODULE, \
.open = __fops ## _open, \
.release = __fops ## _close, \
.read = simple_attr_read, \
.write = simple_attr_write, \
.llseek = generic_file_llseek, \
}
static int enable_read_op(void *data, u64 *val)
{
struct fault_cfg *cfg = data;
*val = cfg->enabled;
return 0;
}
struct fault_point_enabled {
struct fault_group *group;
bool to_enable;
};
static int __fault_point_enable(struct fault_inject *inj,
struct fault_point *fault,
struct jump_entry *entry, void *priv)
{
struct fault_point_enabled *e = priv;
if (e->group == fault->group) {
if (e->to_enable)
static_key_slow_inc(&fault->key);
else
static_key_slow_dec(&fault->key);
}
return 0;
}
static int enable_write_op(void *data, u64 val)
{
struct fault_cfg *cfg = data;
struct fault_point_enabled e;
e.group = cfg->group;
e.to_enable = !!val;
mutex_lock(&cfg->group->lock);
if (likely(e.to_enable ^ cfg->enabled)) {
for_each_fault_inject_entry(
e.group->inj, __fault_point_enable, &e);
cfg->enabled = e.to_enable;
}
mutex_unlock(&cfg->group->lock);
return 0;
}
FAULT_SIMPLE_ATTRIBUTE(enable_fault_fops, enable_read_op,
enable_write_op, "%llu\n",
ref_from_fault_cfg);
static int hits_read_op(void *data, u64 *val)
{
struct fault_cfg *cfg = data;
*val = atomic64_read(&cfg->hits);
return 0;
}
FAULT_SIMPLE_ATTRIBUTE(hits_fault_fops, hits_read_op,
NULL, "%llu\n",
ref_from_fault_cfg);
static int injected_read_op(void *data, u64 *val)
{
struct fault_cfg *cfg = data;
*val = atomic64_read(&cfg->injected);
return 0;
}
FAULT_SIMPLE_ATTRIBUTE(injected_fault_fops, injected_read_op,
NULL, "%llu\n",
ref_from_fault_cfg);
static int times_read_op(void *data, u64 *val)
{
struct fault_cfg *cfg = data;
*val = atomic_read(&cfg->times);
return 0;
}
static int times_write_op(void *data, u64 val)
{