diff
+static int check_null(void *ptr, const char *name) {
+	if (!ptr) {
+		pr_err("%s is NULL\n", name);
+		return -EINVAL;
+	}
+	return 0;
+}
+
 static int remove_device_files(struct super_block *sb,
 			       struct qib_devdata *dd)
 {
 	struct dentry *dir;
 	char unit[10];
 
+	if (check_null(sb, "sb") || check_null(sb->s_root, "sb->s_root"))
+		return -EINVAL;
+
 	snprintf(unit, sizeof(unit), "%u", dd->unit);
 	dir = lookup_one_len_unlocked(unit, sb->s_root, strlen(unit));
