From nobody Tue Apr 7 02:34:19 2026 Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) (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 57D0C3DCDA1 for ; Mon, 16 Mar 2026 18:21:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773685300; cv=none; b=RVTY9F0Iywdn7G/ld4O+e6Cu47XjnHRCT/ApsUhEHfCAw59ruBHfG49VQZmX2ssnJ0VGFL1YUJuOnft4lx/2QxtJl1+hkMLMyYR/POLpY0Irmo3FgMftrp2SLKfgbZn5mVESkKSL3rlgyG66j5hU8A7dmkpn3m4XhPn3p+Dvl8k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773685300; c=relaxed/simple; bh=wyoEhbH5vEmoe/FvNNWLblsq3Yj00zp3Hzhb8lN1S9w=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=Dv1Lem1YhE1iXMQXS7+r9ve21/hugLbDcbZWjqeCOZtxkxZ/g89IqMGxrUz/t0fPnuBIzuqLsdHL9PrTjcw1BLyjm996t7mbDKFi6e9ucrcH+WZGeH9+reknu2pwvwQRKRk/aV3yHoHbXiRqxS/mSUk+ZX4tatFd822JlslBznM= 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=gML1drS1; arc=none smtp.client-ip=209.85.210.170 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="gML1drS1" Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-8299f1ca894so2509847b3a.2 for ; Mon, 16 Mar 2026 11:21:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1773685290; x=1774290090; 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=ffmkfhN6IZVcQX8QYVniegQEwjGmXttuPdYtjponOMI=; b=gML1drS1cxujHW6+jEH+RoV9YKcV/WIyxim42MQEKud+pZQnCnvJexH9YEg7lQjfaM H9fNz9abjHJ+iSdftB3tr8q6yLQwH4+f3VXbLyXTkO87KmhXoenLFLiQ37wheXwRlQZA t3sLHKuuSypWuWpdn7XXuDK+dCpLEmi+KbdnhrH8tTgGNtJWb57lfLmVMgZiqFeAi+mj 1l+OYPNfhoFcBUMbs5MUhtrGnV76e2zZ/RH5hrt3iHMTOlrJ6TFSVqkoZMa+ercvRb7L HBIcW5tkXBhEO5L2TMf01wgG8wB4jpKeeYI2haMYKojc9RdzFFlkXSiecOXxREitVSDu Kf/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773685290; x=1774290090; 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=ffmkfhN6IZVcQX8QYVniegQEwjGmXttuPdYtjponOMI=; b=ZfcD5TLS6CPU/jK8G9AbJ8z5KO35nwkU+Z0PI94BI6QUqX5o6aPM2vo3xUfuUG9/g0 J3dSP2CHW6AnmWSNUd9HH6Aqrgw8kXKZS0/9TPsbwGlQShfd8M+Qim298etObk93BhIx uh3/PaBgIGZEbxCVtUVtstDjH+lJtFJfTArNSZik7cXVjyi1v6t1P8Xk9hbhwbRBfo5c d9sozaOkwaGAryh2/wydieY0Vegy123Uv/4f/rGAH2MC5tiKYW+ek6o4ylpPfZPRZRnZ nAK1sQqG4atRY/MWvjYpLTvAzYIZgi8KsHHqcI/enYo7qEnHdt8rACUhoOdGd/HOBNOB /+Lg== X-Forwarded-Encrypted: i=1; AJvYcCXVdPx2gyx22Jpm4OcvsvrO8hEhDL5Wbjw8XFchjYp+Sze75J0/oWmde9Vu+ySEAECAINuP87J9fP37+2s=@vger.kernel.org X-Gm-Message-State: AOJu0Yz5ifNKHERkIBsHcemK2N+veVYIn+lvMbhO5NkNrxI/3ee6dVu8 r8i85Drqm2sbO6pWpgppUJ4+s4VhAzkLkzuOWoSgoKq3YOn2237uK9aBAEyCvFq/ X-Gm-Gg: ATEYQzyalW9FW7Xmc5C106VE3ebGiJ+IpI/zcAifQRh/rutFzufxhfQYlO3/GNIusxd VyTKHAnpLvffxGTDVjzvXps3VdMj9+RrUgNPZNg4fbSJixv6V8M64yt9fFkIuJPG/TtmtsI3uqB /+ErIL1K13hAffD30W0dYIp/wD0krUToi1dVwKmELeOnVq0obcxhzacK5dYP60lxlIx9b4hstp4 Q4dIG8hW40cN7iGrSVEbi0yIy+tWJn1mdCm9oV9UcjVcVIHb2FhPN02yLQzP5EtdJiAxNLvHrBl BbDmuPbGz+6ZVYNhQPst36HYo17oNCnaD0Av1/bsxI0kviGnb8VHm0v+OHCeH4QwmdLNyNiVNye CBNDjWEGzZO0531d93YKA3kzG84Cgj8//R25EhBRVaepRgtag2qi5HAo/EPLPE5jyvRYTjEdMOB lBYgbLZQAvxEIThAEspMbVQ8dAWqiMMFJRmDdKwsLFr3NeDgYMchLdZlg= X-Received: by 2002:a05:6a00:aa0a:b0:81f:394a:4897 with SMTP id d2e1a72fcca58-82a198fd351mr12387566b3a.44.1773685290281; Mon, 16 Mar 2026 11:21:30 -0700 (PDT) Received: from ryzen ([2601:644:8000:56f5::8bd]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-82a07341ed1sm13602575b3a.33.2026.03.16.11.21.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Mar 2026 11:21:29 -0700 (PDT) From: Rosen Penev To: linux-usb@vger.kernel.org Cc: Andreas Noever , Mika Westerberg , Yehezkel Bernat , Kees Cook , "Gustavo A. R. Silva" , linux-kernel@vger.kernel.org (open list), linux-hardening@vger.kernel.org (open list:KERNEL HARDENING (not covered by other areas):Keyword:\b__counted_by(_le|_be)?\b) Subject: [PATCHv2] thunderbolt: use kzalloc_flex Date: Mon, 16 Mar 2026 11:21:12 -0700 Message-ID: <20260316182112.8682-1-rosenp@gmail.com> X-Mailer: git-send-email 2.53.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" Simplifies allocation by using a flexible arraay member. Added __counted_by for extra runtime analysis. Signed-off-by: Rosen Penev --- v2: remove extra kfree to fix kernel test bot. drivers/thunderbolt/path.c | 28 +++++++--------------------- drivers/thunderbolt/tb.h | 3 ++- 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/drivers/thunderbolt/path.c b/drivers/thunderbolt/path.c index 22fb4a1e1acd..8713ea0f47c1 100644 --- a/drivers/thunderbolt/path.c +++ b/drivers/thunderbolt/path.c @@ -150,22 +150,17 @@ struct tb_path *tb_path_discover(struct tb_port *src,= int src_hopid, num_hops++; } - path =3D kzalloc_obj(*path); + path =3D kzalloc_flex(*path, hops, num_hops); if (!path) return NULL; + path->path_length =3D num_hops; + path->name =3D name; path->tb =3D src->sw->tb; - path->path_length =3D num_hops; path->activated =3D true; path->alloc_hopid =3D alloc_hopid; - path->hops =3D kzalloc_objs(*path->hops, num_hops); - if (!path->hops) { - kfree(path); - return NULL; - } - tb_dbg(path->tb, "discovering %s path starting from %llx:%u\n", path->name, tb_route(src->sw), src->port); @@ -245,10 +240,6 @@ struct tb_path *tb_path_alloc(struct tb *tb, struct tb= _port *src, int src_hopid, size_t num_hops; int i, ret; - path =3D kzalloc_obj(*path); - if (!path) - return NULL; - first_port =3D last_port =3D NULL; i =3D 0; tb_for_each_port_on_path(src, dst, in_port) { @@ -259,20 +250,17 @@ struct tb_path *tb_path_alloc(struct tb *tb, struct t= b_port *src, int src_hopid, } /* Check that src and dst are reachable */ - if (first_port !=3D src || last_port !=3D dst) { - kfree(path); + if (first_port !=3D src || last_port !=3D dst) return NULL; - } /* Each hop takes two ports */ num_hops =3D i / 2; - path->hops =3D kzalloc_objs(*path->hops, num_hops); - if (!path->hops) { - kfree(path); + path =3D kzalloc_flex(*path, hops, num_hops); + if (!path) return NULL; - } + path->path_length =3D num_hops; path->alloc_hopid =3D true; in_hopid =3D src_hopid; @@ -339,7 +327,6 @@ struct tb_path *tb_path_alloc(struct tb *tb, struct tb_= port *src, int src_hopid, } path->tb =3D tb; - path->path_length =3D num_hops; path->name =3D name; return path; @@ -372,7 +359,6 @@ void tb_path_free(struct tb_path *path) } } - kfree(path->hops); kfree(path); } diff --git a/drivers/thunderbolt/tb.h b/drivers/thunderbolt/tb.h index e96474f17067..4e66b53860dd 100644 --- a/drivers/thunderbolt/tb.h +++ b/drivers/thunderbolt/tb.h @@ -440,9 +440,10 @@ struct tb_path { bool drop_packages; bool activated; bool clear_fc; - struct tb_path_hop *hops; int path_length; bool alloc_hopid; + + struct tb_path_hop hops[] __counted_by(path_length); }; /* HopIDs 0-7 are reserved by the Thunderbolt protocol */ -- 2.53.0