usb: gadget: f_mtp: Avoid race between mtp_read and mtp_function_disable
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / lib / bug.c
CommitLineData
7664c5a1
JF
1/*
2 Generic support for BUG()
3
4 This respects the following config options:
5
6 CONFIG_BUG - emit BUG traps. Nothing happens without this.
7 CONFIG_GENERIC_BUG - enable this code.
b93a531e
JB
8 CONFIG_GENERIC_BUG_RELATIVE_POINTERS - use 32-bit pointers relative to
9 the containing struct bug_entry for bug_addr and file.
7664c5a1
JF
10 CONFIG_DEBUG_BUGVERBOSE - emit full file+line information for each BUG
11
12 CONFIG_BUG and CONFIG_DEBUG_BUGVERBOSE are potentially user-settable
13 (though they're generally always on).
14
15 CONFIG_GENERIC_BUG is set by each architecture using this code.
16
17 To use this, your architecture must:
18
19 1. Set up the config options:
20 - Enable CONFIG_GENERIC_BUG if CONFIG_BUG
21
22 2. Implement BUG (and optionally BUG_ON, WARN, WARN_ON)
23 - Define HAVE_ARCH_BUG
24 - Implement BUG() to generate a faulting instruction
25 - NOTE: struct bug_entry does not have "file" or "line" entries
26 when CONFIG_DEBUG_BUGVERBOSE is not enabled, so you must generate
27 the values accordingly.
28
29 3. Implement the trap
30 - In the illegal instruction trap handler (typically), verify
31 that the fault was in kernel mode, and call report_bug()
32 - report_bug() will return whether it was a false alarm, a warning,
33 or an actual bug.
34 - You must implement the is_valid_bugaddr(bugaddr) callback which
35 returns true if the eip is a real kernel address, and it points
36 to the expected BUG trap instruction.
37
38 Jeremy Fitzhardinge <jeremy@goop.org> 2006
39 */
c56ba703
FF
40
41#define pr_fmt(fmt) fmt
42
7664c5a1
JF
43#include <linux/list.h>
44#include <linux/module.h>
da9eac89 45#include <linux/kernel.h>
7664c5a1 46#include <linux/bug.h>
608e2619 47#include <linux/sched.h>
7664c5a1 48
1cac41cb
MB
49#ifdef CONFIG_SEC_DEBUG
50#include <linux/sec_debug.h>
51#endif
52
7664c5a1
JF
53extern const struct bug_entry __start___bug_table[], __stop___bug_table[];
54
b93a531e
JB
55static inline unsigned long bug_addr(const struct bug_entry *bug)
56{
57#ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
58 return bug->bug_addr;
59#else
60 return (unsigned long)bug + bug->bug_addr_disp;
61#endif
62}
63
7664c5a1 64#ifdef CONFIG_MODULES
1fb9341a 65/* Updates are protected by module mutex */
7664c5a1
JF
66static LIST_HEAD(module_bug_list);
67
68static const struct bug_entry *module_find_bug(unsigned long bugaddr)
69{
70 struct module *mod;
0286b5ea 71 const struct bug_entry *bug = NULL;
7664c5a1 72
0be964be 73 rcu_read_lock_sched();
0286b5ea 74 list_for_each_entry_rcu(mod, &module_bug_list, bug_list) {
7664c5a1
JF
75 unsigned i;
76
0286b5ea 77 bug = mod->bug_table;
7664c5a1 78 for (i = 0; i < mod->num_bugs; ++i, ++bug)
b93a531e 79 if (bugaddr == bug_addr(bug))
0286b5ea 80 goto out;
7664c5a1 81 }
0286b5ea
MH
82 bug = NULL;
83out:
0be964be 84 rcu_read_unlock_sched();
0286b5ea
MH
85
86 return bug;
7664c5a1
JF
87}
88
5336377d
LT
89void module_bug_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
90 struct module *mod)
7664c5a1
JF
91{
92 char *secstrings;
93 unsigned int i;
94
0be964be
PZ
95 lockdep_assert_held(&module_mutex);
96
7664c5a1
JF
97 mod->bug_table = NULL;
98 mod->num_bugs = 0;
99
100 /* Find the __bug_table section, if present */
101 secstrings = (char *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
102 for (i = 1; i < hdr->e_shnum; i++) {
103 if (strcmp(secstrings+sechdrs[i].sh_name, "__bug_table"))
104 continue;
105 mod->bug_table = (void *) sechdrs[i].sh_addr;
106 mod->num_bugs = sechdrs[i].sh_size / sizeof(struct bug_entry);
107 break;
108 }
109
110 /*
111 * Strictly speaking this should have a spinlock to protect against
112 * traversals, but since we only traverse on BUG()s, a spinlock
113 * could potentially lead to deadlock and thus be counter-productive.
0286b5ea
MH
114 * Thus, this uses RCU to safely manipulate the bug list, since BUG
115 * must run in non-interruptive state.
7664c5a1 116 */
0286b5ea 117 list_add_rcu(&mod->bug_list, &module_bug_list);
7664c5a1
JF
118}
119
120void module_bug_cleanup(struct module *mod)
121{
0be964be 122 lockdep_assert_held(&module_mutex);
0286b5ea 123 list_del_rcu(&mod->bug_list);
7664c5a1
JF
124}
125
126#else
127
128static inline const struct bug_entry *module_find_bug(unsigned long bugaddr)
129{
130 return NULL;
131}
132#endif
133
134const struct bug_entry *find_bug(unsigned long bugaddr)
135{
136 const struct bug_entry *bug;
137
138 for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
b93a531e 139 if (bugaddr == bug_addr(bug))
7664c5a1
JF
140 return bug;
141
142 return module_find_bug(bugaddr);
143}
144
608e2619 145enum bug_trap_type report_bug(unsigned long bugaddr, struct pt_regs *regs)
7664c5a1
JF
146{
147 const struct bug_entry *bug;
148 const char *file;
149 unsigned line, warning;
150
151 if (!is_valid_bugaddr(bugaddr))
152 return BUG_TRAP_TYPE_NONE;
153
154 bug = find_bug(bugaddr);
155
7664c5a1
JF
156 file = NULL;
157 line = 0;
158 warning = 0;
159
160 if (bug) {
161#ifdef CONFIG_DEBUG_BUGVERBOSE
b93a531e 162#ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
7664c5a1 163 file = bug->file;
b93a531e
JB
164#else
165 file = (const char *)bug + bug->file_disp;
166#endif
7664c5a1
JF
167 line = bug->line;
168#endif
169 warning = (bug->flags & BUGFLAG_WARNING) != 0;
170 }
171
172 if (warning) {
173 /* this is a WARN_ON rather than BUG/BUG_ON */
c56ba703 174 pr_warn("------------[ cut here ]------------\n");
e2e7e093 175
7664c5a1 176 if (file)
c56ba703 177 pr_warn("WARNING: at %s:%u\n", file, line);
7664c5a1 178 else
c56ba703
FF
179 pr_warn("WARNING: at %p [verbose debug info unavailable]\n",
180 (void *)bugaddr);
7664c5a1 181
e2e7e093 182 print_modules();
863a6049 183 print_oops_end_marker();
373d4d09
RR
184 /* Just a warning, don't kill lockdep. */
185 add_taint(BUG_GET_TAINT(bug), LOCKDEP_STILL_OK);
7664c5a1
JF
186 return BUG_TRAP_TYPE_WARN;
187 }
188
b0f4c4b3 189 printk(KERN_DEFAULT "------------[ cut here ]------------\n");
e2e7e093 190
1cac41cb
MB
191#ifdef CONFIG_SEC_DEBUG_EXTRA_INFO
192 if (file)
193 sec_debug_set_extra_info_bug(file, line);
194#endif
195
7664c5a1 196 if (file)
1cac41cb 197 pr_auto(ASL1, "kernel BUG at %s:%u!\n", file, line);
7664c5a1 198 else
1cac41cb 199 pr_auto(ASL1, "Kernel BUG at %p [verbose debug info unavailable]\n",
c56ba703 200 (void *)bugaddr);
7664c5a1
JF
201
202 return BUG_TRAP_TYPE_BUG;
203}