diff --git a/fs/btrfs/backref.c b/fs/btrfs/backref.c
index d888e71e66b69306a34b4d087c13a7404bd72eb6..ea10f7bc99abf7590b89c1fa888cf8d135aac620 100644
--- a/fs/btrfs/backref.c
+++ b/fs/btrfs/backref.c
@@ -1461,6 +1461,7 @@ static int btrfs_find_all_roots_safe(struct btrfs_trans_handle *trans,
 		if (ret < 0 && ret != -ENOENT) {
 			ulist_free(tmp);
 			ulist_free(*roots);
+			*roots = NULL;
 			return ret;
 		}
 		node = ulist_next(tmp, &uiter);