From nobody Thu Apr 2 01:15:56 2026 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (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 165AE39B94C for ; Wed, 1 Apr 2026 21:47:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775080066; cv=none; b=kmmbBznvcMnaJSA1Hb1cVezxm8uBMg2XRZAW+RKd9hD3+tIBXKMF8fxW0AOrtJOTP0O1vPN98JmlaXFqyDzJKsxK/g4pCss+UCX0/Py1k6vcKYGWmcPd9akDbo/irbGWgaTApqHUXrqUoWYWCyBCliBHzq6Z9ktoaBXx4q2sG3g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775080066; c=relaxed/simple; bh=ZOD32CPLt9QK3aMBProd892mYoMeBAWfZ1nsyKYtZeU=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=Q0ACIzkDujCvoau9liUpcIks/pARcqpBiwqA3ZELMzrhjoKlJZLam/Dbm7IKaOFYL5Kl5wv570Gbp2PIeYG7mgeRBiJ0+pdnd70icnEAVeJzo99fy1Ej3FftC0ew9+XYTWokn6zyS2yssjS6mebz7lo88maKYAqIOpIQz2e+lME= 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=pU/EYg0k; arc=none smtp.client-ip=209.85.214.175 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="pU/EYg0k" Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-2b0ba3bfe16so10933275ad.1 for ; Wed, 01 Apr 2026 14:47:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775080064; x=1775684864; 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=iGt1a4sLfrhxByIJ29j9DQkf7gSkmMPtiPiOxDIoUcA=; b=pU/EYg0kUPUWlpO4fh8GHmk3QM1FrO+JuYtA9VDMZC2Gtx5GKXRzbEXikPJ3SJs53Z 0fMG1cbT1BjkwHLIWOt5Y9w0fBnrwp0j7tKvnv+Yt4H3E/N6KzrBMi4Le0StwGMjxkOW hKlx/8c9uFIJOocDiTXWkAzC2n3Gy70ryFtHvut7QY4l4Nu4q5U985acrp2Ar2/k7kqO Zo5onLx281aXWs4gWpOu3k6uPlwMXAtOuouNj2rLhDq3zGu9fcfGSbz1OdoxVBBlCuUm rFkS3cpAbvUVn0471pUlQhz1c94IhVWBI2+sHUK5/nS0hsk0eR02IEudD2fQnjwo76Zh CQ8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775080064; x=1775684864; 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=iGt1a4sLfrhxByIJ29j9DQkf7gSkmMPtiPiOxDIoUcA=; b=nN0XqWoo8keXo9+uczIQoC5Wf7s5dnzClbNbFMcouM15vDNdPGb6NL7dnrCx5a3VjS qysy7ndynhKEL+A4lEIZWdNBYlFJ2Pn/zWOVozkJanHCCQlqkrjCkL84/BnN7pPtZgqo 32jqsWnZ9Tvc/R8/DycILDIrhuLfi5q94eYqIdHorCJaPSyPtSRuwSzbSK1Yq7zzqBN3 H819Rgd1Je19cZVrtNm31h9Co3ZO/ZNeFk0xgRUYk77crUZdNa6Te2RXNqtOED2CJgtj mKW4SmgQSXxRLsJMri5wiRCs2RlhJSHOuApNc8U2XsfD0qvcv+wkXer53HIL+/CKHzb5 Hi+Q== X-Forwarded-Encrypted: i=1; AJvYcCVdMt6aNwEwRnjGzL8Xb7tC7hXK2CGeQZxM7it9ONZi/tMJSyT+nwZJSUgHmEtTZM1OX+BsvZzaPV1sQlA=@vger.kernel.org X-Gm-Message-State: AOJu0YwXZVuEHoi1spLrtWJdvOZL31eMsukwsYyv11AeksJFMcxG96r6 KJ8xs9QvLjLoOmkn2lXJ/XtRz8wq3buRTwEn6tLjUKqUiIsmh+cKTb3F X-Gm-Gg: ATEYQzxWNBMrniYpysX7SkQcafdJulE4A0jWZSZcY6qDfH933Wss0xQsw49ZlhOAbSX Fb9p+x71WIG74XvLRPBEXtlR3OxSsIyUIp60LC24p6VFBsY0dyG2jTMxPo3XGAbrFNDDIxxj+Hm jqfMCW03pyZpDNNrUG9FeQ7OMRaWeLIcXkAj/oOjfLQ4CC13sBmcQJDDq5afn/0IJlwNlwoCQDV mGNkQaL/WpyAjtPwMMFGGzN7T73S64dRW6i3Kh1xgudt5OwwGwYFfzcJJNh+et00ZRIFSbYYfE5 Svt670Uh0LuGW+dCVvYtY02tgU9ynpF8yfOJVGMImqGu61om7wj8vjIxmFf+7kIpEB5JtrWRYSU 9ZC3pIqeKotFcrgkGn0Iu69wmBgyhdbrVPWqaFiY6jNHfS69+jb3oI/t4axZMq/7nonJ78/eorl Gz2Dvppo3Kr7x8DppWohp8+cUY9JhXRwnQc7WgADh55ilgoeILM/njcn4= X-Received: by 2002:a17:903:24e:b0:2b0:6ba2:3d78 with SMTP id d9443c01a7336-2b277d62983mr342125ad.3.1775080064337; Wed, 01 Apr 2026 14:47:44 -0700 (PDT) Received: from ryzen ([2601:644:8000:5b5d::8bd]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b27478cb4fsm7777625ad.29.2026.04.01.14.47.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Apr 2026 14:47:43 -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: tunnel: simplify allocation Date: Wed, 1 Apr 2026 14:47:26 -0700 Message-ID: <20260401214726.3911-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" Use a flexible array member and kzalloc_flex to combine allocations. Add __counted_by for extra runtime analysis. Move counting variable assignment after allocation. kzalloc_flex with GCC >=3D 15 does this automatically. Signed-off-by: Rosen Penev --- v2: move kernel-doc and reword slightly. drivers/thunderbolt/tunnel.c | 10 ++-------- drivers/thunderbolt/tunnel.h | 5 +++-- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/drivers/thunderbolt/tunnel.c b/drivers/thunderbolt/tunnel.c index 89676acf1290..f38f7753b6e4 100644 --- a/drivers/thunderbolt/tunnel.c +++ b/drivers/thunderbolt/tunnel.c @@ -180,19 +180,14 @@ static struct tb_tunnel *tb_tunnel_alloc(struct tb *t= b, size_t npaths, { struct tb_tunnel *tunnel; - tunnel =3D kzalloc_obj(*tunnel); + tunnel =3D kzalloc_flex(*tunnel, paths, npaths); if (!tunnel) return NULL; - tunnel->paths =3D kzalloc_objs(tunnel->paths[0], npaths); - if (!tunnel->paths) { - kfree(tunnel); - return NULL; - } + tunnel->npaths =3D npaths; INIT_LIST_HEAD(&tunnel->list); tunnel->tb =3D tb; - tunnel->npaths =3D npaths; tunnel->type =3D type; kref_init(&tunnel->kref); @@ -219,7 +214,6 @@ static void tb_tunnel_destroy(struct kref *kref) tb_path_free(tunnel->paths[i]); } - kfree(tunnel->paths); kfree(tunnel); } diff --git a/drivers/thunderbolt/tunnel.h b/drivers/thunderbolt/tunnel.h index 2c44fc8a10bc..4878763a82b3 100644 --- a/drivers/thunderbolt/tunnel.h +++ b/drivers/thunderbolt/tunnel.h @@ -37,7 +37,6 @@ enum tb_tunnel_state { * @src_port: Source port of the tunnel * @dst_port: Destination port of the tunnel. For discovered incomplete * tunnels may be %NULL or null adapter port instead. - * @paths: All paths required by the tunnel * @npaths: Number of paths in @paths * @pre_activate: Optional tunnel specific initialization called before * activation. Can touch hardware. @@ -69,13 +68,13 @@ enum tb_tunnel_state { * @dprx_work: Worker that is scheduled to poll completion of DPRX capabil= ities read * @callback: Optional callback called when DP tunnel is fully activated * @callback_data: Optional data for @callback + * @paths: All paths required by the tunnel */ struct tb_tunnel { struct kref kref; struct tb *tb; struct tb_port *src_port; struct tb_port *dst_port; - struct tb_path **paths; size_t npaths; int (*pre_activate)(struct tb_tunnel *tunnel); int (*activate)(struct tb_tunnel *tunnel, bool activate); @@ -107,6 +106,8 @@ struct tb_tunnel { struct delayed_work dprx_work; void (*callback)(struct tb_tunnel *tunnel, void *data); void *callback_data; + + struct tb_path *paths[] __counted_by(npaths); }; struct tb_tunnel *tb_tunnel_discover_pci(struct tb *tb, struct tb_port *do= wn, -- 2.53.0