Skip to contentSkip to conversation

perf/x86/intel/uncore: Fix iounmap() leak on global_init failure

PATCH Superseded Zide Chen 3 participants 7 messages Jan 13 – Jan 14

Subsystems (3)

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Zide Chen

author
#0 lore raw

PATCH 1/1 perf/x86/intel/uncore: Fix iounmap() leak on global_init failure

If domain->global_init() fails in __parse_discovery_table(), the mapped MMIO region is not released before returning, resulting in an iounmap() leak.

Fixes b575fc0e3357 ("perf/x86/intel/uncore: Add domain global init callback")
---
 arch/x86/events/intel/uncore_discovery.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/events/intel/uncore_discovery.c +3 -1
index 0e414cecb6f2..f64661ad56b2 100644
@@ -286,8 +286,10 @@ static int __parse_discovery_table(struct uncore_discovery_domain *domain,
 	if (!io_addr)
 		return -ENOMEM;
 
-	if (domain->global_init && domain->global_init(global.ctl))
+	if (domain->global_init && domain->global_init(global.ctl)) {
+		iounmap(io_addr);
 		return -ENODEV;
+	}
 
 	/* Parsing Unit Discovery State */
 	for (i = 0; i < global.max_units; i++) {
Signature · 1 line
2.52.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 17 lines · Zide Chen

If domain->global_init() fails in __parse_discovery_table(), the mapped MMIO region is not released before returning, resulting in an iounmap() leak.

Fixes b575fc0e3357 ("perf/x86/intel/uncore: Add domain global init callback")
---
 arch/x86/events/intel/uncore_discovery.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/events/intel/uncore_discovery.c +0 -0
index 0e414cecb6f2..f64661ad56b2 100644
@@ -286,8 +286,10 @@ static int __parse_discovery_table(struct uncore_discovery_domain *domain,
 	if (!io_addr)

arch/x86/events/intel/uncore_discovery.c static int __parse_discovery_table(struct uncore_discovery_domain *domain, · line 295

Quoted 10 lines
arch/x86/events/intel/uncore_discovery.c +3 -1
 		return -ENOMEM;
 
-	if (domain->global_init && domain->global_init(global.ctl))
+	if (domain->global_init && domain->global_init(global.ctl)) {
+		iounmap(io_addr);
 		return -ENODEV;
+	}
 
 	/* Parsing Unit Discovery State */
 	for (i = 0; i < global.max_units; i++) {
#2 lore raw
In reply to Zide Chen
Quoted 3 lines

If domain->global_init() fails in __parse_discovery_table(), the mapped MMIO region is not released before returning, resulting in an iounmap() leak.

How do you think about to avoid a bit of duplicate source code here?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v6.19-rc5#n526

See also once more:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.19-rc5#n94

Regards,
Markus

author
#3 lore raw
Quoted 7 lines · Markus Elfring
Quoted 3 lines

If domain->global_init() fails in __parse_discovery_table(), the mapped MMIO region is not released before returning, resulting in an iounmap() leak.

How do you think about to avoid a bit of duplicate source code here?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v6.19-rc5#n526

Thank you for the suggestion!

Yes, I agree this is better. In V1 I followed the existing style in this API.

I will post a v2 with this change:

@@ -264,6 +264,7 @@ static int __parse_discovery_table(struct uncore_discovery_domain *domain,

        struct uncore_unit_discovery unit;
        void __iomem *io_addr;
        unsigned long size;
+       int ret = 0;
        int i;

        size = UNCORE_DISCOVERY_GLOBAL_MAP_SIZE;

@@ -273,21 +274,23 @@ static int __parse_discovery_table(struct uncore_discovery_domain *domain,

/* Read Global Discovery State */ memcpy_fromio(&global, io_addr, sizeof(struct
uncore_global_discovery));
+ iounmap(io_addr);
+

        if (uncore_discovery_invalid_unit(global)) {
                pr_info("Invalid Global Discovery State: 0x%llx 0x%llx
0x%llx\n",
                        global.table1, global.ctl, global.table3);
-               iounmap(io_addr);
                return -EINVAL;
        }

- iounmap(io_addr);

        size = (1 + global.max_units) * global.stride * 8;
        io_addr = ioremap(addr, size);
        if (!io_addr)
                return -ENOMEM;

- if (domain->global_init && domain->global_init(global.ctl))
- return -ENODEV;
+ if (domain->global_init && domain->global_init(global.ctl)) {
+ ret = -ENODEV;
+ goto out;
+ }

        /* Parsing Unit Discovery State */
        for (i = 0; i < global.max_units; i++) {
@@ -307,8 +310,10 @@ static int __parse_discovery_table(struct
uncore_discovery_domain *domain,
        }

        *parsed = true;
+
+out:
        iounmap(io_addr);

- return 0;
+ return ret;
}

static int parse_discovery_table(struct uncore_discovery_domain

Quoted 2 lines

See also once more:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.19-rc5#n94

Are you suggesting that I add a Closes tag?
This issue was reported by Intel internal LKP, and there is no public URL available.

Quoted 2 lines

Regards,
Markus

#4 lore raw

Am 13.01.26 um 23:04 schrieb Chen, Zide:

Quoted 83 lines · Markus Elfring
Quoted 7 lines
Quoted 3 lines

If domain->global_init() fails in __parse_discovery_table(), the mapped MMIO region is not released before returning, resulting in an iounmap() leak.

How do you think about to avoid a bit of duplicate source code here?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v6.19-rc5#n526

Thank you for the suggestion!

Yes, I agree this is better. In V1 I followed the existing style in this API.

I will post a v2 with this change:

(quoted diff) +1 -0
@@ -264,6 +264,7 @@ static int __parse_discovery_table(struct
 uncore_discovery_domain *domain,
        struct uncore_unit_discovery unit;
        void __iomem *io_addr;
        unsigned long size;
+       int ret = 0;
        int i;
 
        size = UNCORE_DISCOVERY_GLOBAL_MAP_SIZE;
(quoted diff) +6 -4
@@ -273,21 +274,23 @@ static int __parse_discovery_table(struct
 uncore_discovery_domain *domain,
 
        /* Read Global Discovery State */
        memcpy_fromio(&global, io_addr, sizeof(struct
 uncore_global_discovery));
+       iounmap(io_addr);
+
        if (uncore_discovery_invalid_unit(global)) {
                pr_info("Invalid Global Discovery State: 0x%llx 0x%llx
 0x%llx\n",
                        global.table1, global.ctl, global.table3);
-               iounmap(io_addr);
                return -EINVAL;
        }
-       iounmap(io_addr);
 
        size = (1 + global.max_units) * global.stride * 8;
        io_addr = ioremap(addr, size);
        if (!io_addr)
                return -ENOMEM;
 
-       if (domain->global_init && domain->global_init(global.ctl))
-               return -ENODEV;
+       if (domain->global_init && domain->global_init(global.ctl)) {
+               ret = -ENODEV;
+               goto out;
+       }
        /* Parsing Unit Discovery State */
        for (i = 0; i < global.max_units; i++) {
(quoted diff) +3 -1
@@ -307,8 +310,10 @@ static int __parse_discovery_table(struct
 uncore_discovery_domain *domain,
        }
 
        *parsed = true;
+
+out:
        iounmap(io_addr);
-       return 0;
+       return ret;
 }
 

static int parse_discovery_table(struct uncore_discovery_domain

Quoted 2 lines

See also once more:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.19-rc5#n94

Are you suggesting that I add a Closes tag?
This issue was reported by Intel internal LKP, and there is no public URL available.

Quoted 2 lines

Regards,
Markus

#5 lore raw
In reply to Chen, Zide
Quoted 10 lines
Quoted 6 lines
Quoted 3 lines

If domain->global_init() fails in __parse_discovery_table(), the mapped MMIO region is not released before returning, resulting in an iounmap() leak.

How do you think about to avoid a bit of duplicate source code here?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v6.19-rc5#n526

Thank you for the suggestion!

Yes, I agree this is better.

Thanks for this positive feedback.

Quoted 1 line

In V1 I followed the existing style in this API.

This variant might be nicer for backporting.

Review static int __parse_discovery_table(struct · line 269

Quoted 9 lines

I will post a v2 with this change:

(quoted diff) +1 -0
@@ -264,6 +264,7 @@ static int __parse_discovery_table(struct
 uncore_discovery_domain *domain,
        struct uncore_unit_discovery unit;
        void __iomem *io_addr;
        unsigned long size;
+       int ret = 0;
        int i;

Would scope adjustments become helpful for any of these local vartiables?

Quoted 2 lines
(quoted diff) +0 -0
        size = UNCORE_DISCOVERY_GLOBAL_MAP_SIZE;
@@ -273,21 +274,23 @@ static int __parse_discovery_table(struct

…

Quoted 6 lines
arch/x86/events/intel/uncore_discovery.c +4 -2
-       if (domain->global_init && domain->global_init(global.ctl))
-               return -ENODEV;
+       if (domain->global_init && domain->global_init(global.ctl)) {
+               ret = -ENODEV;
+               goto out;
+       }

…

Review

Quoted 3 lines
        *parsed = true;
(quoted diff) +2 -0
+
+out:

Would an other label be a bit clearer here?

unmap_io:

Quoted 9 lines
        iounmap(io_addr);
(quoted diff) +1 -1
-       return 0;
+       return ret;
 }
 
 static int parse_discovery_table(struct uncore_discovery_domain
Quoted 3 lines

See also once more:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.19-rc5#n94

Will another imperative wording become helpful for an improved change description?

Quoted 1 line

Are you suggesting that I add a Closes tag?

It depends …

Quoted 2 lines

This issue was reported by Intel internal LKP, and there is no public URL available.

Thanks for such a bit of background information.
Some contributors would appreciate further hints on involved development tools (and known source code analysis approaches).

Regards,
Markus

author
#6 lore raw
Quoted 21 lines · Markus Elfring
Quoted 10 lines
Quoted 6 lines
Quoted 3 lines

If domain->global_init() fails in __parse_discovery_table(), the mapped MMIO region is not released before returning, resulting in an iounmap() leak.

How do you think about to avoid a bit of duplicate source code here?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v6.19-rc5#n526

Thank you for the suggestion!

Yes, I agree this is better.

Thanks for this positive feedback.

Quoted 1 line

In V1 I followed the existing style in this API.

This variant might be nicer for backporting.

Quoted 2 lines

I will post a v2 with this change:

Quoted 9 lines

Review static int __parse_discovery_table(struct · line 269

Quoted 7 lines
(quoted diff) +1 -0
@@ -264,6 +264,7 @@ static int __parse_discovery_table(struct
 uncore_discovery_domain *domain,
        struct uncore_unit_discovery unit;
        void __iomem *io_addr;
        unsigned long size;
+       int ret = 0;
        int i;

Would scope adjustments become helpful for any of these local vartiables?

Yes, I agree that moving int i into the for loop would be better, but I’d prefer to keep this patch focused and leave that change for future cleanup.

Quoted 10 lines
Quoted 3 lines
(quoted diff) +0 -0
        size = UNCORE_DISCOVERY_GLOBAL_MAP_SIZE;
@@ -273,21 +274,23 @@ static int __parse_discovery_table(struct

…

arch/x86/events/intel/uncore_discovery.c static int __parse_discovery_table(struct uncore_discovery_domain *domain, · line 292

Quoted 6 lines
arch/x86/events/intel/uncore_discovery.c +4 -2
-       if (domain->global_init && domain->global_init(global.ctl))
-               return -ENODEV;
+       if (domain->global_init && domain->global_init(global.ctl)) {
+               ret = -ENODEV;
+               goto out;
+       }
Quoted 8 lines

…

Review

Quoted 3 lines
        *parsed = true;
(quoted diff) +2 -0
+
+out:

Would an other label be a bit clearer here?

unmap_io:

It seems that the perf driver generally uses simple labels such as done, out, or err. Additionally, since there is only a single error path here, I would prefer to keep the label out for style consistency.

Quoted 6 lines
Quoted 6 lines
        iounmap(io_addr);
(quoted diff) +1 -1
-       return 0;
+       return ret;
 }
Quoted 6 lines
Quoted 4 lines

static int parse_discovery_table(struct uncore_discovery_domain

Quoted 2 lines

See also once more:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.19-rc5#n94

Will another imperative wording become helpful for an improved change description?

Sure, I’ll try my best to improve the description.

Quoted 11 lines
Quoted 1 line

Are you suggesting that I add a Closes tag?

It depends …

Quoted 2 lines

This issue was reported by Intel internal LKP, and there is no public URL available.

Thanks for such a bit of background information.
Some contributors would appreciate further hints on involved development tools (and known source code analysis approaches).

I may add the following report to the commit message for additional context:

Unverified Error/Warning (likely false positive, kindly check if interested):
arch/x86/events/intel/uncore_discovery.c:293:2-8:
ERROR: missing iounmap; ioremap on line 288 and execution via conditional on line 292

Quoted 2 lines

Regards,
Markus