Netdev List
 help / color / mirror / Atom feed
From: Rosen Penev <rosenp@gmail.com>
To: netdev@vger.kernel.org
Cc: Jiri Pirko <jiri@resnulli.us>,
	Andrew Morton <akpm@linux-foundation.org>,
	Kees Cook <kees@kernel.org>,
	"Gustavo A. R. Silva" <gustavoars@kernel.org>,
	linux-kernel@vger.kernel.org (open list:LIBRARY CODE),
	linux-hardening@vger.kernel.org (open list:KERNEL HARDENING (not
	covered by other areas):Keyword:\b__counted_by(_le|_be|_ptr)?\b)
Subject: [PATCH] lib: objagg: Use flexible arrays for object storage
Date: Mon, 18 May 2026 18:58:33 -0700	[thread overview]
Message-ID: <20260519015833.634602-1-rosenp@gmail.com> (raw)

Store object payloads and temporary graph nodes in their parent
allocations instead of open-coding trailing storage or allocating the
node array separately.

Use byte-sized flexible arrays for object payloads so kzalloc_flex() is
passed the same byte count as the previous allocation sizes.

Assisted-by: Codex:GPT-5.5
Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 lib/objagg.c | 19 ++++++-------------
 1 file changed, 6 insertions(+), 13 deletions(-)

diff --git a/lib/objagg.c b/lib/objagg.c
index 23c7105a3f9f..995ec67fbc2f 100644
--- a/lib/objagg.c
+++ b/lib/objagg.c
@@ -28,7 +28,7 @@ struct objagg_hints_node {
 	struct objagg_hints_node *parent;
 	unsigned int root_id;
 	struct objagg_obj_stats_info stats_info;
-	unsigned long obj[];
+	u8 obj[];
 };
 
 static struct objagg_hints_node *
@@ -66,7 +66,7 @@ struct objagg_obj {
 				* including nested objects
 				*/
 	struct objagg_obj_stats stats;
-	unsigned long obj[];
+	u8 obj[];
 };
 
 static unsigned int objagg_obj_ref_inc(struct objagg_obj *objagg_obj)
@@ -369,8 +369,7 @@ static struct objagg_obj *objagg_obj_create(struct objagg *objagg, void *obj)
 	struct objagg_obj *objagg_obj;
 	int err;
 
-	objagg_obj = kzalloc(sizeof(*objagg_obj) + objagg->ops->obj_size,
-			     GFP_KERNEL);
+	objagg_obj = kzalloc_flex(*objagg_obj, obj, objagg->ops->obj_size);
 	if (!objagg_obj)
 		return ERR_PTR(-ENOMEM);
 	objagg_obj_ref_inc(objagg_obj);
@@ -657,7 +656,7 @@ objagg_hints_node_create(struct objagg_hints *objagg_hints,
 	struct objagg_hints_node *hnode;
 	int err;
 
-	hnode = kzalloc(sizeof(*hnode) + obj_size, GFP_KERNEL);
+	hnode = kzalloc_flex(*hnode, obj, obj_size);
 	if (!hnode)
 		return ERR_PTR(-ENOMEM);
 	memcpy(hnode->obj, &objagg_obj->obj, obj_size);
@@ -705,9 +704,9 @@ struct objagg_tmp_node {
 };
 
 struct objagg_tmp_graph {
-	struct objagg_tmp_node *nodes;
 	unsigned long nodes_count;
 	unsigned long *edges;
+	struct objagg_tmp_node nodes[] __counted_by(nodes_count);
 };
 
 static int objagg_tmp_graph_edge_index(struct objagg_tmp_graph *graph,
@@ -786,13 +785,10 @@ static struct objagg_tmp_graph *objagg_tmp_graph_create(struct objagg *objagg)
 	struct objagg_obj *objagg_obj;
 	int i, j;
 
-	graph = kzalloc_obj(*graph);
+	graph = kzalloc_flex(*graph, nodes, nodes_count);
 	if (!graph)
 		return NULL;
 
-	graph->nodes = kzalloc_objs(*graph->nodes, nodes_count);
-	if (!graph->nodes)
-		goto err_nodes_alloc;
 	graph->nodes_count = nodes_count;
 
 	graph->edges = bitmap_zalloc(nodes_count * nodes_count, GFP_KERNEL);
@@ -825,8 +821,6 @@ static struct objagg_tmp_graph *objagg_tmp_graph_create(struct objagg *objagg)
 	return graph;
 
 err_edges_alloc:
-	kfree(graph->nodes);
-err_nodes_alloc:
 	kfree(graph);
 	return NULL;
 }
@@ -834,7 +828,6 @@ static struct objagg_tmp_graph *objagg_tmp_graph_create(struct objagg *objagg)
 static void objagg_tmp_graph_destroy(struct objagg_tmp_graph *graph)
 {
 	bitmap_free(graph->edges);
-	kfree(graph->nodes);
 	kfree(graph);
 }
 
-- 
2.54.0


                 reply	other threads:[~2026-05-19  1:58 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260519015833.634602-1-rosenp@gmail.com \
    --to=rosenp@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=gustavoars@kernel.org \
    --cc=jiri@resnulli.us \
    --cc=kees@kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox