From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from SEVP216CU002.outbound.protection.outlook.com (mail-koreacentralazolkn19012051.outbound.protection.outlook.com [52.103.74.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E86FC2472AE for ; Thu, 9 Jul 2026 23:03:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.74.51 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783638190; cv=fail; b=HN2FWVDG/nldn09zD+wn3QeDzjIwD0I7x3+cGFr5vbz+DAwACdlUGWqDeSUs/hQeuH3vaJ1xoKuXmiQvfWP35SDabB7CkeAUOUf/EMARf2Y3hbbxOvt7fISf0u6h0RJqKmYt7bCZOYfKxAgRWU9uFOkORiVB1ggIMFrXOa4rDik= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783638190; c=relaxed/simple; bh=lL4edVTUKG+ihwfISvED+p59E0J6viyArKntmouf+gE=; h=From:To:CC:Subject:Date:Message-ID:Content-Type:MIME-Version; b=JEcum5tikJvHYPoGnyk4YDOkE4FPeNygu/rzi8rkl+YD+QsriMIAERt6UViVCm1vdiAx2N1l87jHQoBmp2lycUQ8S/SKxIECfbJ6O/qQ4dwN6xNGSi7i4j27dZzl98fEf9nJzpWY8eYyaqfADD+CXVPHIdcNd+kxPCl+flt/agk= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=JDkuvolX; arc=fail smtp.client-ip=52.103.74.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="JDkuvolX" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=jIL8A/x8DEyVXemln4G5iq+HHUlFzBNQ5JgAKNvAXIVYQ92Mz9ZeG5pf20YZrf/PxtibT9OO9hfZj/cYYTQONJpu8rdKUOsaCcMaiII/GpnbU49KQZEyBsExUxLgRmqX4Qnv3vS62lo/HKQhTtFPNnkG7r4xIsPJvMOixeeiI/RbdDG6WLhZQe0/Wb7VYr/6j1Mrk8NvcKZU/camrz+toR6s3DdUwFA3BU1yIZ2mvcq+Cuhjoar0iFlmgbC2OqtOwd/JLPoPeyVOFDrgnb2VM+J2rCZ/MWJlmagk6q3TdyGXX0yUKFdSRYktqcK2VMOh+Rn7e+msGV786rosxKOfBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=2BplQGBCjOfhTISpk33cLJShz0pnoiIgahBdBsft17c=; b=cPAG+9aaoR1G87EzZ2oNQ+f/C0sIW2+A4w570QFkt5QrYWocrW7aayEbdbQDlriz2UZwtqliNG1fs82EDiI7jixfbf9ksyScRjlLVvpk6csrYjk/iHPDZMPjD+x7Oif86sx+BjzkGwTiHD/s1LMJkNdf5o43r0I9zaC1b1ImYQfVrEGlzMOrOdRUl7n6c0pb0woNo35Ph+4o0oPEDFzrJWnAR0dUjWG4Yzua2Y9ZVZ1xj5Lx3fA8/Xju0rGdWePO44PNbj5eEXeMvbChgb2EF/k7ukXVrQfHnTAlgXnY1Xabi72rRHITsOc6nTriY207WJJgEBWdXPqYsEsqq8alew== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=2BplQGBCjOfhTISpk33cLJShz0pnoiIgahBdBsft17c=; b=JDkuvolXTtLkwGmfqNrGyhgEg0YgFKouRt5ewYQU/17A0jcYxy7zxZL6KYt4EqGdTDi4nCIMGxDhtkQPs3cKXEcgH2ZU8EVZ/Zjg94HyVTjkl3JyyMBqCS8Z916klE1iAh90aWozvxATxr8BEKs96BPdkNhJxpgtYdVHb21MOEo3UOV1RGMNzMXaP2z0eie13jPzUIaLTIQak0k4q7FgEkFXjGUF2Z1MopPlLcQ37MRxGWMcQqAFxYFlZZbXFQA3TLBUWvhRRMUExSWxte9+APoIvlKtREzSwujTgIXhAY/tEi7cX1SB2E32mnvCAiYnRz0ipvE/BE60ZnTPR04t8A== Received: from SL2P216MB2619.KORP216.PROD.OUTLOOK.COM (2603:1096:101:1bb::7) by PS2P216MB1347.KORP216.PROD.OUTLOOK.COM (2603:1096:301:98::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.181.17; Thu, 9 Jul 2026 23:03:04 +0000 Received: from SL2P216MB2619.KORP216.PROD.OUTLOOK.COM ([fe80::1d3a:3f80:a5af:5e59]) by SL2P216MB2619.KORP216.PROD.OUTLOOK.COM ([fe80::1d3a:3f80:a5af:5e59%4]) with mapi id 15.21.0181.016; Thu, 9 Jul 2026 23:03:04 +0000 From: "monios114514@outlook.com" To: "mingo@redhat.com" CC: "peterz@infradead.org" , "juri.lelli@redhat.com" , "vincent.guittot@linaro.org" , "dietmar.eggemann@arm.com" , "rostedt@goodmis.org" , "bsegall@google.com" , "mgorman@suse.de" , "vschneid@redhat.com" , "kprateek.nayak@amd.com" , "linux-kernel@vger.kernel.org" Subject: [PATCH] sched/topology: fix memory leaks in allocation failure paths Thread-Topic: [PATCH] sched/topology: fix memory leaks in allocation failure paths Thread-Index: Ad0P9OFid5sp1vgfR8awoB2Ppde9fA== Date: Thu, 9 Jul 2026 23:03:04 +0000 Message-ID: Accept-Language: zh-CN, en-US Content-Language: zh-CN X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-publictraffictype: Email x-ms-traffictypediagnostic: SL2P216MB2619:EE_|PS2P216MB1347:EE_ x-ms-office365-filtering-correlation-id: dd11c056-fef7-4206-f9e8-08dede0e3b8b x-microsoft-antispam: BCL:0;ARA:14566002|19101099003|15080799012|25010399006|31061999003|13091999003|41001999006|51005399006|19110799012|8060799015|8062599012|12091999003|440099028|3412199025|102099032|40105399003; x-microsoft-antispam-message-info: =?us-ascii?Q?BvcV4fKQ480rsgA75aij+/ed1NiEdX/uQ10vYm8XhhP6sE1Bek5J5Wuf38JC?= =?us-ascii?Q?ht8H212luXSAJ2Xujy5ArRKjpm6DKBhj6LvFeyWJX1jf+7D58tylM5SR9cVx?= =?us-ascii?Q?8v0VXJbznkEIs7odtEFMFyNISDaFP5YCaK/D/SWgXWD2qVZ61skWFrDAwHjG?= =?us-ascii?Q?2b456ZLAGxJRmPPe6r06brXeWF90l3O9kFms0VMnWRk/+S0ETZ0CvFB1r58e?= =?us-ascii?Q?5vBZjLzwsSnJMxE6ZTnBHlPeuM0Z89QoaB9jjNUTc79FTcYuBl/vKoVrMoyC?= =?us-ascii?Q?ECS8Tp+KF583JRf81Z1iCtpPCfe3SchjPkviIL303XsTcQTHP3JeV+Y8ZdPx?= =?us-ascii?Q?3EkipDNg/ryraiVK5UQO5lRkgcdQ87G0yWcV6Db11cPMuYKS1ae1Jgnh1dYO?= =?us-ascii?Q?PNfOnUKeyiI2IxgutFqAqEJkB4VpD/8NoC6Q5Qy+apr9ODtHOb6OTr3tbXEy?= =?us-ascii?Q?u3QhCbDmgfe13AF23jVYXJr3FfJO76lbsIat/G1xwWNJxzG7EjlpsAwFt+sl?= =?us-ascii?Q?e+/7l8RElipvLnGiuF3g+VcjczvYAZO4E2ngTBB7TeZLoLOruuxLhbn7daRP?= =?us-ascii?Q?eaffbOkoY7ISi3IXW7qf/AfIsNuL2HLw0+gangtAEe/9FNsAgZmGLeM/+o+x?= =?us-ascii?Q?wYKYyZrbnRXcxUVxwcZAQ/kj96rb4a9+XPEse6/RG875C6c8yYfeQHc4Eww2?= =?us-ascii?Q?I33mY9TSUSkVvglMipYReV4dloUI4YG3xZj1E1bck6BaYP8+ifM1qKLfQxpM?= =?us-ascii?Q?Qdt1KlvLyoLg/41OIC29iG2dnEN/WvxUIEv6ddgjUFgDYg2QezhUy1zhr+fW?= =?us-ascii?Q?iMZAFbtDiVzIGmBqonvd+N6Qj6zGgCYuhjGsN8kxtPxEtHVz+YmzHCfmqc3R?= =?us-ascii?Q?abXBnrNRerIXMP52AgenUKMLWKlp/ek6qnu3eMhAbRXEtMpkAFjhD5sTRO5T?= =?us-ascii?Q?BadIWSG8ifP0UA0bSuoqSgWdA7J7qHcevuKnklVEDFe9luymAPBe8or9oGjA?= =?us-ascii?Q?xyq5qor4nAwnUz3qcsz+pqSgrQ=3D=3D?= x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?ADhBDmLH/ubmEO7JWUSRuiU4+xXOIE3Th3NhEruvj7x9hXso2pB87w0xB02m?= =?us-ascii?Q?2vHIi9ktuZmxwB9WBxfbVGA9aXVdRkYwDcepNis/WQEWuS90ON/MQ/GpV0gK?= =?us-ascii?Q?8klSNhrlPfIF7edFwmYCmFPjo0V9meYDTk2fs1EWKsHELImVqDJOGYPsMxbe?= =?us-ascii?Q?mWZICoHIIpwP1rPp1+mWRQyt677O8ycUHXzG9Kg0B7nNN7T93ihNzjOfo12p?= =?us-ascii?Q?dQDsiddesErEGIkv1Uc02C9unthX9NOMaZF2Yawm1A1P/jjhdpTjTgFaR2a0?= =?us-ascii?Q?Ku8h5+2INTGipK2CRXLIDi0PQToas/FSYQtlm0QnHpiIE6IOyNWotnM7/F8T?= =?us-ascii?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RsGfqIo7AknHMTL?= =?us-ascii?Q?9+hEdT3gWqLcPBLDnyyXqGFwJTe6EgrfStFdGML3mg4s8gqUqfGNc7AtmFQE?= =?us-ascii?Q?wN7LMDLdCNTbBREzxUrKIWpY5TMb6riCmTYt2xBzbEWtUXHHQWra1eRwL62S?= =?us-ascii?Q?nt4lHnAToQzV4PqZrmnQRB5SbHZL?= Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: SL2P216MB2619.KORP216.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: dd11c056-fef7-4206-f9e8-08dede0e3b8b X-MS-Exchange-CrossTenant-originalarrivaltime: 09 Jul 2026 23:03:04.2904 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: PS2P216MB1347 >From c58bc2361ab78a5d978a3962dd9e88089a09d11b Mon Sep 17 00:00:00 2001 From: cpumsconfig Date: Thu, 9 Jul 2026 21:58:41 +0800 Subject: [PATCH] sched/topology: fix memory leaks in allocation failure pat= hs Fix three memory leaks in the scheduler topology code when memory allocation fails: 1. sched_init_numa(): When per-node masks[i] or inner mask allocation fails, previously allocated masks[0..i-1] and the masks array itself are not freed before return. The separately allocated domain_distances also leaks. 2. __sdt_alloc(): When per-CPU sd/sg/sgc allocation fails in the inner loop, already allocated per-CPU pointers for completed CPUs leak. Fix by using a unified fail label that calls __sdt_free(). 3. __sds_alloc(): When percpu d->sds allocation succeeds but inner kzalloc_node fails, d->sds leaks. Fix by calling __sds_free(). Signed-off-by: cpumsconfig --- kernel/sched/topology.c | 99 ++++++++++++++++++++++------------------- 1 file changed, 53 insertions(+), 46 deletions(-) diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c index 622e2e019..fb5200b58 100644 --- a/kernel/sched/topology.c +++ b/kernel/sched/topology.c @@ -2320,12 +2320,10 @@ void sched_init_numa(int offline_node) int *distances, *domain_distances; struct cpumask ***masks; =20 - /* Record the NUMA distances from SLIT table */ if (sched_record_numa_dist(offline_node, numa_node_dist, &distances, &nr_node_levels)) return; =20 - /* Record modified NUMA distances for building sched domains */ if (modified_sched_node_distance()) { if (sched_record_numa_dist(offline_node, arch_sched_node_distance, &domain_distances, &nr_levels)) { @@ -2340,45 +2338,43 @@ void sched_init_numa(int offline_node) WRITE_ONCE(sched_max_numa_distance, distances[nr_node_levels - 1]); WRITE_ONCE(sched_numa_node_levels, nr_node_levels); =20 - /* - * 'nr_levels' contains the number of unique distances - * - * The sched_domains_numa_distance[] array includes the actual distance - * numbers. - */ - - /* - * Here, we should temporarily reset sched_domains_numa_levels to 0. - * If it fails to allocate memory for array sched_domains_numa_masks[][], - * the array will contain less then 'nr_levels' members. This could be - * dangerous when we use it to iterate array sched_domains_numa_masks[][] - * in other functions. - * - * We reset it to 'nr_levels' at the end of this function. - */ rcu_assign_pointer(sched_domains_numa_distance, domain_distances); =20 sched_domains_numa_levels =3D 0; =20 masks =3D kzalloc(sizeof(void *) * nr_levels, GFP_KERNEL); if (!masks) - return; + goto free_distance; =20 - /* - * Now for each level, construct a mask per node which contains all - * CPUs of nodes that are that many hops away from us. - */ for (i =3D 0; i < nr_levels; i++) { masks[i] =3D kzalloc(nr_node_ids * sizeof(void *), GFP_KERNEL); - if (!masks[i]) - return; + if (!masks[i]) { + for (i =3D i - 1; i >=3D 0; i--) { + if (!masks[i]) + continue; + for_each_cpu_node_but(j, offline_node) + kfree(masks[i][j]); + kfree(masks[i]); + } + kfree(masks); + goto free_distance; + } =20 for_each_cpu_node_but(j, offline_node) { struct cpumask *mask =3D kzalloc(cpumask_size(), GFP_KERNEL); int k; =20 - if (!mask) - return; + if (!mask) { + for (i =3D i; i >=3D 0; i--) { + if (!masks[i]) + continue; + for_each_cpu_node_but(j, offline_node) + kfree(masks[i][j]); + kfree(masks[i]); + } + kfree(masks); + goto free_distance; + } =20 masks[i][j] =3D mask; =20 @@ -2398,28 +2394,28 @@ void sched_init_numa(int offline_node) } rcu_assign_pointer(sched_domains_numa_masks, masks); =20 - /* Compute default topology size */ for (i =3D 0; sched_domain_topology[i].mask; i++); =20 tl =3D kzalloc((i + nr_levels + 1) * sizeof(struct sched_domain_topology_level), GFP_KERNEL); - if (!tl) - return; + if (!tl) { + rcu_assign_pointer(sched_domains_numa_masks, NULL); + for (i =3D nr_levels - 1; i >=3D 0; i--) { + if (!masks[i]) + continue; + for_each_cpu_node_but(j, offline_node) + kfree(masks[i][j]); + kfree(masks[i]); + } + kfree(masks); + goto free_distance; + } =20 - /* - * Copy the default topology bits.. - */ for (i =3D 0; sched_domain_topology[i].mask; i++) tl[i] =3D sched_domain_topology[i]; =20 - /* - * Add the NUMA identity distance, aka single NODE. - */ tl[i++] =3D SDTL_INIT(sd_numa_mask, NULL, NODE); =20 - /* - * .. and append 'j' levels of NUMA goodness. - */ for (j =3D 1; j < nr_levels; i++, j++) { tl[i] =3D SDTL_INIT(sd_numa_mask, cpu_numa_flags, NUMA); tl[i].numa_level =3D j; @@ -2431,6 +2427,11 @@ void sched_init_numa(int offline_node) sched_domains_numa_levels =3D nr_levels; =20 init_numa_topology_type(offline_node); + return; + +free_distance: + if (domain_distances !=3D distances) + kfree(domain_distances); } =20 =20 @@ -2670,15 +2671,15 @@ static int __sdt_alloc(const struct cpumask *cpu_ma= p) =20 sdd->sd =3D alloc_percpu(struct sched_domain *); if (!sdd->sd) - return -ENOMEM; + goto fail; =20 sdd->sg =3D alloc_percpu(struct sched_group *); if (!sdd->sg) - return -ENOMEM; + goto fail; =20 sdd->sgc =3D alloc_percpu(struct sched_group_capacity *); if (!sdd->sgc) - return -ENOMEM; + goto fail; =20 for_each_cpu(j, cpu_map) { struct sched_domain *sd; @@ -2688,14 +2689,14 @@ static int __sdt_alloc(const struct cpumask *cpu_ma= p) sd =3D kzalloc_node(sizeof(struct sched_domain) + cpumask_size(), GFP_KERNEL, cpu_to_node(j)); if (!sd) - return -ENOMEM; + goto fail; =20 *per_cpu_ptr(sdd->sd, j) =3D sd; =20 sg =3D kzalloc_node(sizeof(struct sched_group) + cpumask_size(), GFP_KERNEL, cpu_to_node(j)); if (!sg) - return -ENOMEM; + goto fail; =20 sg->next =3D sg; =20 @@ -2704,7 +2705,7 @@ static int __sdt_alloc(const struct cpumask *cpu_map) sgc =3D kzalloc_node(sizeof(struct sched_group_capacity) + cpumask_size= (), GFP_KERNEL, cpu_to_node(j)); if (!sgc) - return -ENOMEM; + goto fail; =20 sgc->id =3D j; =20 @@ -2713,6 +2714,10 @@ static int __sdt_alloc(const struct cpumask *cpu_map= ) } =20 return 0; + +fail: + __sdt_free(cpu_map); + return -ENOMEM; } =20 static void __sdt_free(const struct cpumask *cpu_map) @@ -2760,8 +2765,10 @@ static int __sds_alloc(struct s_data *d, const struc= t cpumask *cpu_map) =20 sds =3D kzalloc_node(sizeof(struct sched_domain_shared), GFP_KERNEL, cpu_to_node(j)); - if (!sds) + if (!sds) { + __sds_free(d, cpu_map); return -ENOMEM; + } =20 *per_cpu_ptr(d->sds, j) =3D sds; } --=20 2.54.0.windows.1