-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcheck_sleep_info.c
167 lines (132 loc) · 3.76 KB
/
check_sleep_info.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
/*
* Copyright (C) 2021 Oracle.
*
* 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; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
*/
#include "smatch.h"
#include "smatch_extra.h"
#include "smatch_slist.h"
static struct expr_fn_list *hooks;
static int my_id;
STATE(sleep);
unsigned long GFP_DIRECT_RECLAIM(void)
{
static unsigned long saved_flags = -1;
struct symbol *macro_sym;
if (saved_flags != -1)
return saved_flags;
macro_sym = lookup_macro_symbol("___GFP_DIRECT_RECLAIM");
if (!macro_sym || !macro_sym->expansion)
return 0;
if (token_type(macro_sym->expansion) != TOKEN_NUMBER)
return 0;
saved_flags = strtoul(macro_sym->expansion->number, NULL, 0);
return saved_flags;
}
static void do_sleep(struct expression *expr)
{
call_expr_fns(hooks, expr);
if (!function_decrements_preempt())
set_state(my_id, "sleep", NULL, &sleep);
clear_preempt_cnt();
clear_irq_context();
}
static void match_sleep(const char *fn, struct expression *expr, void *unused)
{
do_sleep(expr);
}
static void match_might_sleep_fn(const char *fn, struct expression *expr, void *unused)
{
struct range_list *rl;
sval_t sval;
if (!get_implied_rl_from_call_str(expr, "$0", &rl))
return;
if (!rl_to_sval(rl, &sval) || sval.value != 0)
return;
do_sleep(expr);
}
static void match_might_sleep_macro(struct statement *stmt)
{
const char *macro;
macro = get_macro_name(stmt->pos);
if (!macro ||
strcmp(macro, "might_sleep") != 0)
return;
do_sleep(NULL);
}
static void select_sleep(struct expression *call, struct expression *arg, char *key, char *unused)
{
do_sleep(call);
}
static bool is_strange_GFP_function(struct expression *expr)
{
char *name;
bool ret = false;
if (!expr || expr->type != EXPR_CALL)
return false;
name = expr_to_str(expr->fn);
if (!name)
return false;
if (strncmp(name, "__xa_", 5) == 0 ||
strncmp(name, "xa_", 3) == 0 ||
strcmp(name, "ttm_bo_swapout") == 0 ||
strcmp(name, "mas_store_gfp") == 0 ||
strcmp(name, "mas_alloc_cyclic") == 0)
ret = true;
free_string(name);
return ret;
}
static void match_gfp_t(struct expression *expr)
{
struct expression *arg;
sval_t sval;
int param;
param = get_gfp_param(expr);
if (param < 0)
return;
arg = get_argument_from_call_expr(expr->args, param);
if (!get_implied_value(arg, &sval))
return;
if (!(sval.value & GFP_DIRECT_RECLAIM()))
return;
if (is_strange_GFP_function(expr))
return;
do_sleep(expr);
}
static void insert_sleep(void)
{
if (get_state(my_id, "sleep", NULL) != &sleep)
return;
sql_insert_return_implies(SLEEP, -2, "", "");
}
void add_sleep_callback(expr_func *fn)
{
add_ptr_list(&hooks, fn);
}
void check_sleep_info(int id)
{
my_id = id;
if (option_project != PROJ_KERNEL)
return;
add_function_hook("schedule", &match_sleep, NULL);
add_function_hook("msleep", &match_sleep, NULL);
add_function_hook("might_resched", &match_sleep, NULL);
add_function_hook("vfree", &match_sleep, NULL);
add_function_hook("__might_sleep", &match_might_sleep_fn, NULL);
add_function_hook("___might_sleep", &match_might_sleep_fn, NULL);
add_hook(&match_might_sleep_macro, STMT_HOOK);
add_hook(&match_gfp_t, FUNCTION_CALL_HOOK);
all_return_states_hook(&insert_sleep);
select_return_implies_hook_early(SLEEP, &select_sleep);
}