missing_error_code2: warn about return ret where ret is zero
[smatch.git] / check_preempt.c
blob5dc72b4f01848caefa76e667ff387d95e8562e15
1 /*
2 * Copyright (C) 2018 Oracle.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * This code works with check_preempt_info.c. The info.c file records
20 * return_states (this code handles caller_info states). The main
21 * thing that this code does is it provides get_preempt_count() function
22 * the check_scheduling_in_atomic.c file.
24 * The preempt count is a counter and when it's non-zero then we are not
25 * allowed to call schedule().
27 * If we're called with a lock held then we say that preempt is one. (In
28 * real life, it could be higher but this is easier to program). So if
29 * any caller is holding the lock we have preempt = 1.
31 * If during the course of parsing the call, the preempt count gets out of
32 * sync on one side of a branch statement, then we assume the lower preempt
33 * count is correct. (In other words, we choose to miss some bugs rather
34 * than add false postives).
37 #include "smatch.h"
38 #include "smatch_slist.h"
40 static int my_id;
42 int get_preempt_cnt(void)
44 struct smatch_state *state;
46 state = get_state(my_id, "preempt", NULL);
47 if (!state)
48 return 0;
49 return PTR_INT(state->data);
52 void clear_preempt_cnt(void)
54 struct smatch_state *state;
56 state = get_state(my_id, "preempt", NULL);
57 if (!state || PTR_INT(state->data) == 0)
58 return;
59 set_state(my_id, "preempt", NULL, alloc_state_num(0));
62 static unsigned long fn_decrements_preempt;
63 bool function_decrements_preempt(void)
65 return fn_decrements_preempt;
68 static int get_start_preempt_cnt(void)
70 struct stree *orig, *start;
71 int ret;
73 start = get_start_states();
74 orig = __swap_cur_stree(start);
76 ret = get_preempt_cnt();
78 __swap_cur_stree(orig);
79 return ret;
82 static bool passes_MSG_DONTWAIT(struct expression *expr)
84 struct expression *arg;
85 char *macro;
87 FOR_EACH_PTR(expr->args, arg) {
88 macro = get_macro_name(arg->pos);
89 if (macro && strcmp(macro, "MSG_DONTWAIT") == 0)
90 return true;
91 } END_FOR_EACH_PTR(arg);
93 return false;
96 static bool is_no_preempt_caller(void)
98 char *fn = get_function();
100 if (!fn)
101 return false;
102 if (strcmp(fn, "sg_miter_next") == 0)
103 return true;
104 return false;
107 static void match_call_info(struct expression *expr)
109 int start_cnt, cnt;
110 int param;
111 const char *disables = "";
113 if (is_fn_ptr(expr->fn))
114 return;
115 cnt = get_preempt_cnt();
116 if (cnt <= 0)
117 return;
118 if (passes_MSG_DONTWAIT(expr))
119 return;
120 if (is_no_preempt_caller())
121 return;
122 start_cnt = get_start_preempt_cnt();
123 if (start_cnt < cnt)
124 disables = "<- disables preempt";
126 param = get_gfp_param(expr);
127 if (param >= 0)
128 return;
129 sql_insert_caller_info(expr, PREEMPT_ADD, -1, "", disables);
132 static struct smatch_state *merge_func(struct smatch_state *s1, struct smatch_state *s2)
134 if (__in_function_def) {
135 if (PTR_INT(s1->data) > PTR_INT(s2->data))
136 return s1;
137 return s2;
139 if (PTR_INT(s1->data) < PTR_INT(s2->data))
140 return s1;
141 return s2;
144 static void select_call_info(const char *name, struct symbol *sym, char *key, char *value)
146 set_state(my_id, "preempt", NULL, alloc_state_num(1));
149 void __preempt_add(void)
151 set_state(my_id, "preempt", NULL, alloc_state_num(get_preempt_cnt() + 1));
154 void __preempt_sub(void)
156 fn_decrements_preempt = 1;
157 set_state(my_id, "preempt", NULL, alloc_state_num(get_preempt_cnt() - 1));
160 static void match_preempt_count_zero(const char *fn, struct expression *call_expr,
161 struct expression *assign_expr, void *_param)
163 set_state(my_id, "preempt", NULL, alloc_state_num(0));
166 static void match_preempt_count_non_zero(const char *fn, struct expression *call_expr,
167 struct expression *assign_expr, void *_param)
169 struct sm_state *sm, *tmp;
170 bool possibly_atomic = false;
172 sm = get_sm_state(my_id, "preempt", NULL);
173 if (!sm)
174 return;
176 FOR_EACH_PTR(sm->possible, tmp) {
177 if (tmp->state->data) {
178 possibly_atomic = true;
179 break;
181 } END_FOR_EACH_PTR(tmp);
183 if (!possibly_atomic)
184 return;
186 set_state(my_id, "preempt", NULL, alloc_state_num(1));
189 void check_preempt(int id)
191 my_id = id;
193 if (option_project != PROJ_KERNEL)
194 return;
196 add_function_data(&fn_decrements_preempt);
197 set_dynamic_states(my_id);
198 add_merge_hook(my_id, &merge_func);
200 return_implies_exact("preempt_count", int_zero, int_zero, &match_preempt_count_zero, NULL);
201 return_implies_exact("preempt_count", int_one, int_max, &match_preempt_count_non_zero, NULL);
203 select_caller_info_hook(&select_call_info, PREEMPT_ADD);
204 add_hook(&match_call_info, FUNCTION_CALL_HOOK);