--- old/usr/src/tools/smatch/src/check_kernel.c	Mon Aug  5 08:38:00 2019
+++ new/usr/src/tools/smatch/src/check_kernel.c	Mon Aug  5 08:38:00 2019
@@ -23,13 +23,19 @@
 #include "smatch.h"
 #include "smatch_extra.h"
 
+static sval_t err_ptr_min;
+static sval_t err_ptr_max;
+static sval_t null_ptr;
+
 static int implied_err_cast_return(struct expression *call, void *unused, struct range_list **rl)
 {
 	struct expression *arg;
 
 	arg = get_argument_from_call_expr(call->args, 0);
-	if (!get_implied_rl(arg, rl))
-		*rl = alloc_rl(ll_to_sval(-4095), ll_to_sval(-1));
+	if (!get_implied_rl(arg, rl)) {
+		*rl = alloc_rl(err_ptr_min, err_ptr_max);
+		*rl = cast_rl(get_type(arg), *rl);
+	}
 	return 1;
 }
 
@@ -78,10 +84,18 @@
 	struct expression *arg;
 	struct smatch_state *pre_state;
 	struct smatch_state *end_state;
+	struct range_list *rl;
 
 	arg = get_argument_from_call_expr(call_expr->args, param);
 	pre_state = get_state_expr(SMATCH_EXTRA, arg);
-	end_state = estate_filter_range(pre_state, ll_to_sval(-4095), ll_to_sval(0));
+	if (estate_rl(pre_state)) {
+		rl = estate_rl(pre_state);
+		rl = remove_range(rl, null_ptr, null_ptr);
+		rl = remove_range(rl, err_ptr_min, err_ptr_max);
+	} else {
+		rl = alloc_rl(valid_ptr_min_sval, valid_ptr_max_sval);
+	}
+	end_state = alloc_estate_rl(rl);
 	set_extra_expr_nomod(arg, end_state);
 }
 
@@ -96,9 +110,9 @@
 
 	arg = get_argument_from_call_expr(call_expr->args, param);
 	pre_state = get_state_expr(SMATCH_EXTRA, arg);
-	rl = alloc_rl(ll_to_sval(-4095), ll_to_sval(0));
+	call_results_to_rl(call_expr, &ptr_ctype, "0,(-4095)-(-1)", &rl);
 	rl = rl_intersection(estate_rl(pre_state), rl);
-	rl = cast_rl(estate_type(pre_state), rl);
+	rl = cast_rl(get_type(arg), rl);
 	end_state = alloc_estate_rl(rl);
 	set_extra_expr_nomod(arg, end_state);
 }
@@ -108,12 +122,18 @@
 {
 	struct expression *arg;
 	struct smatch_state *pre_state;
-	struct smatch_state *new_state;
+	struct range_list *rl;
 
 	arg = get_argument_from_call_expr(call_expr->args, 0);
 	pre_state = get_state_expr(SMATCH_EXTRA, arg);
-	new_state = estate_filter_range(pre_state, sval_type_min(&long_ctype), ll_to_sval(-1));
-	set_extra_expr_nomod(arg, new_state);
+	if (estate_rl(pre_state)) {
+		rl = estate_rl(pre_state);
+		rl = remove_range(rl, err_ptr_min, err_ptr_max);
+	} else {
+		rl = alloc_rl(valid_ptr_min_sval, valid_ptr_max_sval);
+	}
+	rl = cast_rl(get_type(arg), rl);
+	set_extra_expr_nomod(arg, alloc_estate_rl(rl));
 }
 
 static void match_err(const char *fn, struct expression *call_expr,
@@ -121,13 +141,16 @@
 {
 	struct expression *arg;
 	struct smatch_state *pre_state;
-	struct smatch_state *new_state;
+	struct range_list *rl;
 
 	arg = get_argument_from_call_expr(call_expr->args, 0);
 	pre_state = get_state_expr(SMATCH_EXTRA, arg);
-	new_state = estate_filter_range(pre_state, sval_type_min(&long_ctype), ll_to_sval(-4096));
-	new_state = estate_filter_range(new_state, ll_to_sval(0), sval_type_max(&long_ctype));
-	set_extra_expr_nomod(arg, new_state);
+	rl = estate_rl(pre_state);
+	if (!rl)
+		rl = alloc_rl(err_ptr_min, err_ptr_max);
+	rl = rl_intersection(rl, alloc_rl(err_ptr_min, err_ptr_max));
+	rl = cast_rl(get_type(arg), rl);
+	set_extra_expr_nomod(arg, alloc_estate_rl(rl));
 }
 
 static void match_container_of_macro(const char *fn, struct expression *expr, void *unused)
@@ -379,11 +402,36 @@
 	__in_fake_assign--;
 }
 
+bool is_ignored_kernel_data(const char *name)
+{
+	if (option_project != PROJ_KERNEL)
+		return false;
+
+	/*
+	 * On the file I was looking at lockdep was 25% of the DB.
+	 */
+	if (strstr(name, ".dep_map."))
+		return true;
+	if (strstr(name, ".lockdep_map."))
+		return true;
+	return false;
+}
+
 void check_kernel(int id)
 {
 	if (option_project != PROJ_KERNEL)
 		return;
 
+	err_ptr_min.type = &ptr_ctype;
+	err_ptr_min.value = -4095;
+	err_ptr_max.type = &ptr_ctype;
+	err_ptr_max.value = -1l;
+	null_ptr.type = &ptr_ctype;
+	null_ptr.value = 0;
+
+	err_ptr_min = sval_cast(&ptr_ctype, err_ptr_min);
+	err_ptr_max = sval_cast(&ptr_ctype, err_ptr_max);
+
 	add_implied_return_hook("ERR_PTR", &implied_err_cast_return, NULL);
 	add_implied_return_hook("ERR_CAST", &implied_err_cast_return, NULL);
 	add_implied_return_hook("PTR_ERR", &implied_err_cast_return, NULL);
