db/fixup_kernel.sh: fix clear_user() handling
[smatch.git] / check_sleep_info.c
blob9d37b0a5d42beec0413fe3aef95d781aaa03341a
1 /*
2 * Copyright (C) 2021 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
18 #include "smatch.h"
19 #include "smatch_extra.h"
20 #include "smatch_slist.h"
22 static struct expr_fn_list *hooks;
24 static int my_id;
26 STATE(sleep);
28 unsigned long GFP_DIRECT_RECLAIM(void)
30 static unsigned long saved_flags = -1;
31 struct symbol *macro_sym;
33 if (saved_flags != -1)
34 return saved_flags;
36 macro_sym = lookup_macro_symbol("___GFP_DIRECT_RECLAIM");
37 if (!macro_sym || !macro_sym->expansion)
38 return 0;
39 if (token_type(macro_sym->expansion) != TOKEN_NUMBER)
40 return 0;
42 saved_flags = strtoul(macro_sym->expansion->number, NULL, 0);
43 return saved_flags;
46 static void do_sleep(struct expression *expr)
48 call_expr_fns(hooks, expr);
50 if (!function_decrements_preempt())
51 set_state(my_id, "sleep", NULL, &sleep);
52 clear_preempt_cnt();
53 clear_irq_context();
56 static void match_sleep(const char *fn, struct expression *expr, void *unused)
58 do_sleep(expr);
61 static void match_might_sleep_fn(const char *fn, struct expression *expr, void *unused)
63 struct range_list *rl;
64 sval_t sval;
66 if (!get_implied_rl_from_call_str(expr, "$0", &rl))
67 return;
68 if (!rl_to_sval(rl, &sval) || sval.value != 0)
69 return;
71 do_sleep(expr);
74 static void match_might_sleep_macro(struct statement *stmt)
76 const char *macro;
78 macro = get_macro_name(stmt->pos);
79 if (!macro ||
80 strcmp(macro, "might_sleep") != 0)
81 return;
83 do_sleep(NULL);
86 static void select_sleep(struct expression *call, struct expression *arg, char *key, char *unused)
88 do_sleep(call);
91 static bool is_strange_GFP_function(struct expression *expr)
93 char *name;
94 bool ret = false;
96 if (!expr || expr->type != EXPR_CALL)
97 return false;
99 name = expr_to_str(expr->fn);
100 if (!name)
101 return false;
103 if (strncmp(name, "__xa_", 5) == 0 ||
104 strncmp(name, "xa_", 3) == 0 ||
105 strcmp(name, "ttm_bo_swapout") == 0 ||
106 strcmp(name, "mas_store_gfp") == 0 ||
107 strcmp(name, "mas_alloc_cyclic") == 0)
108 ret = true;
110 free_string(name);
111 return ret;
114 static void match_gfp_t(struct expression *expr)
116 struct expression *arg;
117 sval_t sval;
118 int param;
120 param = get_gfp_param(expr);
121 if (param < 0)
122 return;
123 arg = get_argument_from_call_expr(expr->args, param);
124 if (!get_implied_value(arg, &sval))
125 return;
127 if (!(sval.value & GFP_DIRECT_RECLAIM()))
128 return;
130 if (is_strange_GFP_function(expr))
131 return;
133 do_sleep(expr);
136 static void insert_sleep(void)
138 if (get_state(my_id, "sleep", NULL) != &sleep)
139 return;
140 sql_insert_return_implies(SLEEP, -2, "", "");
143 void add_sleep_callback(expr_func *fn)
145 add_ptr_list(&hooks, fn);
148 void check_sleep_info(int id)
150 my_id = id;
152 if (option_project != PROJ_KERNEL)
153 return;
155 add_function_hook("schedule", &match_sleep, NULL);
156 add_function_hook("msleep", &match_sleep, NULL);
157 add_function_hook("might_resched", &match_sleep, NULL);
158 add_function_hook("vfree", &match_sleep, NULL);
159 add_function_hook("__might_sleep", &match_might_sleep_fn, NULL);
160 add_function_hook("___might_sleep", &match_might_sleep_fn, NULL);
161 add_hook(&match_might_sleep_macro, STMT_HOOK);
163 add_hook(&match_gfp_t, FUNCTION_CALL_HOOK);
165 all_return_states_hook(&insert_sleep);
166 select_return_implies_hook_early(SLEEP, &select_sleep);