Skip to contentSkip to conversation

timers/migration: turn tmigr_hierarchy level_list into a flexible array

PATCH Applied Rosen Penev 2 participants 2 messages May 22 – Jun 2 High-resolution timers, timer wheel, clockevents

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 timers/migration: turn tmigr_hierarchy level_list into a flexible array

level_list was kzalloc_objs()'d separately right after the parent struct, with the count (tmigr_hierarchy_levels, a module-static set during init) trivially available before the parent allocation. Move level_list to the struct tail as a flexible array member and fold the two allocations into a single kzalloc_flex().

The cpumask sub-allocation has variable size from cpumask_size() and stays separate; with level_list gone the err label collapses into a short inline cleanup.

Assisted-by Claude:Opus-4.7
---
 kernel/time/timer_migration.c | 16 +++++-----------
 kernel/time/timer_migration.h |  5 ++---
 2 files changed, 7 insertions(+), 14 deletions(-)
kernel/time/timer_migration.c +5 -11
index 25e3c563eb74..daaf6c91eead 100644
@@ -1925,17 +1925,15 @@ static struct tmigr_hierarchy *tmigr_get_hierarchy(unsigned int capacity)
 	if (hier)
 		return hier;
 
-	hier = kzalloc(sizeof(*hier), GFP_KERNEL);
+	hier = kzalloc_flex(*hier, level_list, tmigr_hierarchy_levels);
 	if (!hier)
 		return ERR_PTR(-ENOMEM);
 
 	hier->cpumask = kzalloc(cpumask_size(), GFP_KERNEL);
-	if (!hier->cpumask)
-		goto err;
-
-	hier->level_list = kzalloc_objs(struct list_head, tmigr_hierarchy_levels);
-	if (!hier->level_list)
-		goto err;
+	if (!hier->cpumask) {
+		kfree(hier);
+		return ERR_PTR(-ENOMEM);
+	}
 
 	for (int i = 0; i < tmigr_hierarchy_levels; i++)
 		INIT_LIST_HEAD(&hier->level_list[i]);
@@ -1944,10 +1942,6 @@ static struct tmigr_hierarchy *tmigr_get_hierarchy(unsigned int capacity)
 	list_add_tail(&hier->node, &tmigr_hierarchy_list);
 
 	return hier;
-err:
-	kfree(hier->cpumask);
-	kfree(hier);
-	return ERR_PTR(-ENOMEM);
 }
 
 static int tmigr_connect_old_root(struct tmigr_hierarchy *hier, int cpu,
kernel/time/timer_migration.h +2 -3
index 291bfb6adfc3..c75e31a644e1 100644
@@ -9,19 +9,18 @@
  * struct tmigr_hierarchy - a hierarchy associated to a given CPU capacity.
  *                          Homogeneous systems have only one hierarchy.
  *                          Heterogenous have one hierarchy per CPU capacity.
- * @level_list:	Per level lists of tmigr groups
  * @cpumask:	CPUs belonging to this hierarchy
  * @root:	The current root of the hierarchy
  * @capacity:	CPU capacity associated to this hierarchy
  * @node:	Node in the global hierarchy list
+ * @level_list:	Per level lists of tmigr groups
  */
 struct tmigr_hierarchy {
-	struct list_head	*level_list;
 	struct cpumask		*cpumask;
 	struct tmigr_group	*root;
 	unsigned long		capacity;
 	struct list_head	node;
-
+	struct list_head	level_list[];
 };
 
 /**
Signature · 1 line
2.54.0
merge bot Applied
#1 lore raw

[tip: timers/core] timers/migration: Turn tmigr_hierarchy level_list into a flexible array

The following commit has been merged into the timers/core branch of tip:

The following commit has been merged into the timers/core branch of tip:

Commit-ID:     45b49d7e3ab6490a9b957a4075344093c43d1f7e
Gitweb:        https://git.kernel.org/tip/45b49d7e3ab6490a9b957a4075344093c43d1f7e
Author:        Rosen Penev <[email protected]>
AuthorDate:    Fri, 22 May 2026 16:16:18 -07:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Tue, 02 Jun 2026 21:34:03 +02:00

timers/migration: Turn tmigr_hierarchy level_list into a flexible array

The level_list array is allocated separately right after the parent struct. The size of the array is already known.

Move level_list to the struct tail as a flexible array member and fold the two allocations into a single kzalloc_flex().

---
 kernel/time/timer_migration.c | 16 +++++-----------
 kernel/time/timer_migration.h |  5 ++---
 2 files changed, 7 insertions(+), 14 deletions(-)
kernel/time/timer_migration.c +5 -11
index 8ba53ad..548d849 100644
@@ -1963,17 +1963,15 @@ static struct tmigr_hierarchy *tmigr_get_hierarchy(int cpu)
 	if (hier)
 		return hier;
 
-	hier = kzalloc(sizeof(*hier), GFP_KERNEL);
+	hier = kzalloc_flex(*hier, level_list, tmigr_hierarchy_levels);
 	if (!hier)
 		return ERR_PTR(-ENOMEM);
 
 	hier->cpumask = kzalloc(cpumask_size(), GFP_KERNEL);
-	if (!hier->cpumask)
-		goto err;
-
-	hier->level_list = kzalloc_objs(struct list_head, tmigr_hierarchy_levels);
-	if (!hier->level_list)
-		goto err;
+	if (!hier->cpumask) {
+		kfree(hier);
+		return ERR_PTR(-ENOMEM);
+	}
 
 	for (int i = 0; i < tmigr_hierarchy_levels; i++)
 		INIT_LIST_HEAD(&hier->level_list[i]);
@@ -1982,10 +1980,6 @@ static struct tmigr_hierarchy *tmigr_get_hierarchy(int cpu)
 	list_add_tail(&hier->node, &tmigr_hierarchy_list);
 
 	return hier;
-err:
-	kfree(hier->cpumask);
-	kfree(hier);
-	return ERR_PTR(-ENOMEM);
 }
 
 static int tmigr_connect_old_root(struct tmigr_hierarchy *hier, int cpu,
kernel/time/timer_migration.h +2 -3
index ea8db95..31735dd 100644
@@ -9,19 +9,18 @@
  * struct tmigr_hierarchy - a hierarchy associated to a given CPU capacity.
  *                          Homogeneous systems have only one hierarchy.
  *                          Heterogenous have one hierarchy per CPU capacity.
- * @level_list:	Per level lists of tmigr groups
  * @cpumask:	CPUs belonging to this hierarchy
  * @root:	The current root of the hierarchy
  * @capacity:	CPU capacity associated to this hierarchy
  * @node:	Node in the global hierarchy list
+ * @level_list:	Per level lists of tmigr groups
  */
 struct tmigr_hierarchy {
-	struct list_head	*level_list;
 	struct cpumask		*cpumask;
 	struct tmigr_group	*root;
 	unsigned long		capacity;
 	struct list_head	node;
-
+	struct list_head	level_list[];
 };
 
 /**