arm64: Fix resource leaks in find_pmu_cpumask()

Close file descriptors on read_file() failure and close the directory
stream before returning from the function.

Signed-off-by: Zongmin Zhou <zhouzongmin@kylinos.cn>
Reviewed-by: Alexandru Elisei <alexandru.elisei@arm.com>
Signed-off-by: Will Deacon <will@kernel.org>
diff --git a/arm64/pmu.c b/arm64/pmu.c
index 78c15f1..ef7faca 100644
--- a/arm64/pmu.c
+++ b/arm64/pmu.c
@@ -75,7 +75,7 @@
 	unsigned long val;
 	ssize_t fd_sz;
 	int fd, ret;
-	DIR *dir;
+	DIR *dir = NULL;
 
 	memset(buf, 0, sizeof(buf));
 
@@ -107,11 +107,12 @@
 			goto next_dir;
 
 		fd_sz = read_file(fd, cpulist, PAGE_SIZE);
+		ret = errno;
+		close(fd);
 		if (fd_sz < 0) {
-			pmu_id = -errno;
+			pmu_id = -ret;
 			goto out_free;
 		}
-		close(fd);
 
 		ret = cpulist_parse(cpulist, &pmu_cpumask);
 		if (ret) {
@@ -140,11 +141,12 @@
 			goto next_dir;
 
 		fd_sz = read_file(fd, buf, PMU_ID_MAXLEN - 1);
+		ret = errno;
+		close(fd);
 		if (fd_sz < 0) {
-			pmu_id = -errno;
+			pmu_id = -ret;
 			goto out_free;
 		}
-		close(fd);
 
 		val = strtoul(buf, NULL, 10);
 		if (val > INT_MAX) {
@@ -162,6 +164,8 @@
 	}
 
 out_free:
+	if (dir)
+		closedir(dir);
 	free(path);
 	free(cpulist);
 	return pmu_id;