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).
38 #include "smatch_slist.h"
42 int get_preempt_cnt(void)
44 struct smatch_state
*state
;
46 state
= get_state(my_id
, "preempt", NULL
);
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)
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
;
73 start
= get_start_states();
74 orig
= __swap_cur_stree(start
);
76 ret
= get_preempt_cnt();
78 __swap_cur_stree(orig
);
82 static bool passes_MSG_DONTWAIT(struct expression
*expr
)
84 struct expression
*arg
;
87 FOR_EACH_PTR(expr
->args
, arg
) {
88 macro
= get_macro_name(arg
->pos
);
89 if (macro
&& strcmp(macro
, "MSG_DONTWAIT") == 0)
91 } END_FOR_EACH_PTR(arg
);
96 static bool is_no_preempt_caller(void)
98 char *fn
= get_function();
102 if (strcmp(fn
, "sg_miter_next") == 0)
107 static void match_call_info(struct expression
*expr
)
111 const char *disables
= "";
113 if (is_fn_ptr(expr
->fn
))
115 cnt
= get_preempt_cnt();
118 if (passes_MSG_DONTWAIT(expr
))
120 if (is_no_preempt_caller())
122 start_cnt
= get_start_preempt_cnt();
124 disables
= "<- disables preempt";
126 param
= get_gfp_param(expr
);
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
))
139 if (PTR_INT(s1
->data
) < PTR_INT(s2
->data
))
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
);
176 FOR_EACH_PTR(sm
->possible
, tmp
) {
177 if (tmp
->state
->data
) {
178 possibly_atomic
= true;
181 } END_FOR_EACH_PTR(tmp
);
183 if (!possibly_atomic
)
186 set_state(my_id
, "preempt", NULL
, alloc_state_num(1));
189 static int set_sleeps(void *_sleeps
, int argc
, char **argv
, char **azColName
)
191 int *fn_sleeps
= _sleeps
;
197 static void silence_duplicates(struct symbol
*sym
)
201 if (!sym
|| !sym
->ident
)
204 run_sql(set_sleeps
, &fn_sleeps
,
205 "select * from return_implies where %s and type = %d;",
206 get_static_filter(sym
), SLEEP
);
209 set_state(my_id
, "preempt", NULL
, alloc_state_num(0));
212 void check_preempt(int id
)
216 if (option_project
!= PROJ_KERNEL
)
219 add_function_data(&fn_decrements_preempt
);
220 set_dynamic_states(my_id
);
221 add_merge_hook(my_id
, &merge_func
);
223 return_implies_exact("preempt_count", int_zero
, int_zero
, &match_preempt_count_zero
, NULL
);
224 return_implies_exact("preempt_count", int_one
, int_max
, &match_preempt_count_non_zero
, NULL
);
226 select_caller_info_hook(&select_call_info
, PREEMPT_ADD
);
227 add_hook(&silence_duplicates
, AFTER_DEF_HOOK
);
228 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);