From nobody Thu Apr 2 14:08:56 2026 Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4F2E2328B6A for ; Tue, 10 Feb 2026 13:47:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770731263; cv=none; b=hvCY29YTgNps+Y8O1MCvR4bmeoyLMo2S+SpW3qKCzk4QpUPe+6tTmYwhOXv0KX3cChSX749dLkaKWXlBos6j+8m9Gsy0nkAb94YhLX7LS7Uyk6LlqlEOU1OTmoDYOIWZ9y1ZUc84qXRyVshYBSQTP/crCE5JTs7g4qIH1ErTwIo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770731263; c=relaxed/simple; bh=GxVbjTQ1dd2sf7IrmoJNJxqjKq6uSLhu80+x4s/VYHY=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=b833BIam6ShXsUQm3E+6EjexuQYpNOip5xIp+rNLnIAHhGE02GSkeEI7GCXfURGPJyK9cd2r6nPzToQinrH/7mPKUnqdgH9IpO2aOzSEGB9TUhjc1h2P7A29RCN8nIHjIcWWcnJTp9WluWo01uPoOgxpZRk/vFYsz1YLqzxMpOg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=XdWLVrnH; arc=none smtp.client-ip=209.85.215.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="XdWLVrnH" Received: by mail-pg1-f178.google.com with SMTP id 41be03b00d2f7-c6de13d9ebbso711269a12.3 for ; Tue, 10 Feb 2026 05:47:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1770731261; x=1771336061; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=qZU1XSzZAyyy+fPtlbYA6gf921bneEHaEOQGF5zJ11o=; b=XdWLVrnHq1pyNKSf1PCWfWeHueJC2kI3BVS/dX1OnqCC/jpfBtYqbS7BP9GJjx1xQZ 5PMM7EDDcp/Gnt72DIoRjvc9XdgNU4z4xQkSg4YRsq5fGEgQrh2TEx9MtVVf+u147Z6v 2UwciWAc9SO2ae3tHe+QE5agzDf6dK2WszYksFFznAyG3QXhBF8T6O6ZCn46znOed4cp LgGnpT0sRB3ezX9dMy6RtD8YUq0x6sc/jWm/7m2rMs1lsCynEHOaj/eUoiRHUl71zI7w 5Eravfq4u40aWE8Cdj82/TDHRfrCslFXLxRtKLEJF2qVqah6zl0yQvVXUw1d8XwlTEi5 h+aQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770731261; x=1771336061; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=qZU1XSzZAyyy+fPtlbYA6gf921bneEHaEOQGF5zJ11o=; b=MIcmeOZeb1Kr0OUEP82MH9WTbRRTncT8GXkZKFv6Bfnc4VmkOIWo40Z/Kj42SmXcxC eeNfpWNPX7oH0WKvOmp7MZfTX3ZdKB4ytaDr/1XQqHYcvU1GL9QSwZMAouryJIk+aX+Y 9np9nQTlZKTMt4cQ0WZV/+DPFqzFqDt5VGh+8crQxNY1pxD2viGvv2Cncu9FjVOvaJTJ rtIXWooT8DhcmgO+DV828rNWxIX0BcBmRU4NOEIAQapgj7zDMurGB+JASd7AsFpy1ISN In/DYHG/17y4+mm9omgm2bt8OlOJ7qjRjX77lzmft0ECGoxBk1Vex2ewLPvyis5Sq4Rj RlJw== X-Gm-Message-State: AOJu0YyR8rhO7hlLjSGXjQ5qLVWYdaYjnCdSFUHs6v0XTqjPQtdbVjGR yUs677jrGPGkFS8eide1Rc+NQeKdtfNlo1vmDCfLT8jBTT+5RKPpfB6n X-Gm-Gg: AZuq6aKwwWU/T7RoTsS3E0C7eHe5YhZKnAFgEZkMNAhzKH0qtLNQMk9Waqxxe97BIk8 RWm1KuAeqdfQ4mvz/+D8pBpdmu2LN+2nkupll0z3rocgChDvXYNn6ohya3XQ+OPsBjb5bj2cLGc vWs1sgsIzttwTN44UaCiyUf3NuD+vI8nUypgxHzRX5e6dccYPeHGuUb+u+G2bD/IRYwrsuSHWhE aaCO3lOkbu3hVeJ+pmGtnFW1ABuybiBVTdupQYgjv80uNAx5Xbbu5p6IAa4gXhHx97zAuxCCmhw tAOa+GztzQRkucbxw2o22wm1wAd45zM/PxfIUGcSqN4p6we6bw8yzFu+uS6UVfSiVA3xzW61p1N vejGdYlZ/G5oOcrkoT/hHG4SDov52In6DnbzC7Iyio/vEcQY7GV8eviWVE4Tvl337jxFGjZXpdK P9Rm+FrOu4v07x/j+pIu75ybxWVfHVe9hCWLWgJB/2UVNEDDASMaBfmDEd0w== X-Received: by 2002:a17:903:2289:b0:2aa:d1e1:29cb with SMTP id d9443c01a7336-2aad1e12bfcmr99923275ad.61.1770731261491; Tue, 10 Feb 2026 05:47:41 -0800 (PST) Received: from DESKTOP-MOQC9AF.mioffice.cn ([43.224.245.239]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2a952222611sm138207265ad.90.2026.02.10.05.47.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Feb 2026 05:47:41 -0800 (PST) From: Zhan Xusheng X-Google-Original-From: Zhan Xusheng To: Peter Zijlstra Cc: linux-kernel@vger.kernel.org, Zhan Xusheng Subject: [PATCH] sched/rt: Add proper error handling and resource cleanup in alloc_rt_sched_group() Date: Tue, 10 Feb 2026 21:47:24 +0800 Message-ID: <20260210134724.213288-1-zhanxusheng@xiaomi.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In the alloc_rt_sched_group() function, refactor the error handling paths to ensure proper cleanup of per-CPU resources and task group-level structures on failure. - Introduced the 'allocated_cpus' variable to track successful per-CPU allocations. - Added rollback logic to free allocated resources (rt_rq, rt_se) if any step fails. - Improved resource deallocation by ensuring that rt_rq and rt_se arrays are freed in reverse order of allocation. - Prevent dangling pointers by explicitly nullifying tg->rt_rq and tg->rt_se. This ensures that in case of an error, all previously allocated resources are cleaned up properly, preventing memory leaks. Signed-off-by: Zhan Xusheng --- kernel/sched/rt.c | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/kernel/sched/rt.c b/kernel/sched/rt.c index f1867fe8e5c5..14f0945e8eb2 100644 --- a/kernel/sched/rt.c +++ b/kernel/sched/rt.c @@ -255,6 +255,7 @@ int alloc_rt_sched_group(struct task_group *tg, struct = task_group *parent) struct rt_rq *rt_rq; struct sched_rt_entity *rt_se; int i; + int allocated_cpus =3D -1; =20 if (!rt_group_sched_enabled()) return 1; @@ -264,7 +265,7 @@ int alloc_rt_sched_group(struct task_group *tg, struct = task_group *parent) goto err; tg->rt_se =3D kcalloc(nr_cpu_ids, sizeof(rt_se), GFP_KERNEL); if (!tg->rt_se) - goto err; + goto err_free_rt_rq; =20 init_rt_bandwidth(&tg->rt_bandwidth, ktime_to_ns(global_rt_period()), 0); =20 @@ -272,22 +273,39 @@ int alloc_rt_sched_group(struct task_group *tg, struc= t task_group *parent) rt_rq =3D kzalloc_node(sizeof(struct rt_rq), GFP_KERNEL, cpu_to_node(i)); if (!rt_rq) - goto err; + goto err_free_per_cpu; =20 rt_se =3D kzalloc_node(sizeof(struct sched_rt_entity), GFP_KERNEL, cpu_to_node(i)); - if (!rt_se) - goto err_free_rq; + if (!rt_se) { + kfree(rt_rq); + goto err_free_per_cpu; + } =20 init_rt_rq(rt_rq); rt_rq->rt_runtime =3D tg->rt_bandwidth.rt_runtime; init_tg_rt_entry(tg, rt_rq, rt_se, i, parent->rt_se[i]); + allocated_cpus =3D i; } =20 return 1; - -err_free_rq: - kfree(rt_rq); +err_free_per_cpu: + /* Rollback and free per-CPU rt_rq/rt_se resources */ + for (i =3D 0; i <=3D allocated_cpus; i++) { + if (cpu_possible(i)) { + kfree(tg->rt_rq[i]); + kfree(tg->rt_se[i]); + } + } + /* Free tg-level rt_se array */ + kfree(tg->rt_se); + /* Prevent dangling pointer */ + tg->rt_se =3D NULL; +err_free_rt_rq: + /* Free tg-level rt_rq array */ + kfree(tg->rt_rq); + /* Prevent dangling pointer */ + tg->rt_rq =3D NULL; err: return 0; } --=20 2.43.0