From nobody Mon Apr 6 16:46:04 2026 Received: from mail-yw1-f179.google.com (mail-yw1-f179.google.com [209.85.128.179]) (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 E05662EC08C for ; Wed, 18 Mar 2026 18:52:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773859978; cv=none; b=EhVH15AE9aReURsrcYYhVxwTPN2evGXoppneZxqt/ThEXEuza+REElvcmi4Aj8EdmXd1AAC/3CpLxx0+SkO2EjNx10X6Mcb0VP6GqzZJ0mUPc8bR0a/miuGQn40I/nLg4tUpHFgA/M9ZwgEcWzI3FveiaqJ7AbGuF7Dgw487Cdw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773859978; c=relaxed/simple; bh=UCngcFeX5vh68hnFGhGjT/S5NFDy3cR2mGBh4g9ppOI=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=sPKKxnglr5IiBFqYcIYu8GpBLbr8kn37aSW1zdB2cTIpUw4hbL//gOeUJp+OToJu3VCcVRn+pqL/Pdu571kAnF2BXowtEHCJdLi60ZPOGHYdplWS0M8QuWsijPAdkwKlMDLi6wqCwhtlecZPq6Zr+8ElEFZ1HlPzaGYxD5kAUD0= 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=FbSuBvpc; arc=none smtp.client-ip=209.85.128.179 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="FbSuBvpc" Received: by mail-yw1-f179.google.com with SMTP id 00721157ae682-79853c0f5b9so2727347b3.0 for ; Wed, 18 Mar 2026 11:52:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1773859976; x=1774464776; 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=EaAhVFjEj7aLMwcXOusWyl6mHSsBkmDOc0v8egWd334=; b=FbSuBvpcS1cHE8n6UItqpDFLCI0ZRp1S7V9Anf/zkZ867hMYVOAQPIfKmcGzgNNlIZ MxUBytZj4SnvNsCuaPPQqzG5BnFt6/v2urCOaN7RXN4+J1q5jw4geTN2NjffA1f+BsG1 eJSebyNfWsoyU3gSQbfTuDmrGvohUx87pb+X3pBVxaKKS027nUhxTL433vwzC2uyFZgN klhLiZKEKqrcV6G5g+FO1+g/GmqjK63fcG0EK/pSh3rSBLNaXa3rSZYAdQ4Js8PBRO2j BYQ1Qojhesdc2GCL476C6phtigLW7WjPGoWzt9ycYql11hv3n67nd+MHQaWNGosObyx3 s3og== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773859976; x=1774464776; 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=EaAhVFjEj7aLMwcXOusWyl6mHSsBkmDOc0v8egWd334=; b=RH/gms1xjqdcHqNZ25DFxQ68UCdu3kNgktMPf1IwWd2zJSV6cM8Qzw0Fl7PZH/q/wb EV1rhTZqLdd2DXPYST5YQ3P9AM7yAaBDcFs5i9zhshrp2NSUhPLYqZ9lBMD80y56hqnT uq/q0F/U6nTzwYyU8cmhhU0Mks5zbp5OnCTesS+SmA0yl5XrA1ZJWQFBxGHLczmzQlS3 f1czgTPY+JEA9uBACHDkgtTBJtnkwVzn4cqtpb7YNzw5+ySYmRb/3X11OnCGR/nlojVQ UBB2pgKLyg9ETlaA7953qcWhYyEdJ7zejzFoP85ZOSofWQnL2gJ61XyJeSFiOd5Kwrw0 ci8g== X-Forwarded-Encrypted: i=1; AJvYcCWHzgJ4VHstUqj2jNN8ybt9wa2yAvQH+C1W/BDn5yT/rcXwctObpMcHYj+ztOGcqwEHHlC55BT0JzMH6OQ=@vger.kernel.org X-Gm-Message-State: AOJu0YxJKsy4Pl3q+0jhtGUhEtCAmUAmBHKT4ynhO1B9CGDMA2Fot42a TGoFwU94WfucJdU/nsxIjCWF/vpiNj/yKkIO/3ShgAf3iBQBbGSNX6za X-Gm-Gg: ATEYQzxnWRHkqFIPNilw1QcOmewYozeQ6cqmu8gjt+I6yTEORLqD9FJcoH/PyLbiUux kNZ/C0WWyfeFTelYkLhMUK/LDFt0Wpnkfr+TgCfYMetINsEObSbSwNpvYh/7sEY7Z2IdMFjBkjU cN7rfeXzQYDtr+D/Y/eqNNY4ccpjpfF8+vAJ8IxrPxMOzzPLg+YsEucZynR5GFS5vTTiz1l8fzn JnbsnbZcn6OaKkNOnT+fKd2ikuzyckt4LEAyWxX+TiPU8Mv7/YGsHV9YjAlXy3IZswwJVAyEDQD osRzR/s7W4KYJb+bw7B6Fx3nXvZh7cGaK0TmIRJBkFkgYJIRC2lFiUptLNRKvNWBFeHMFq9+zom 0xcmNwidObVDAp80XIml35BxoJd1XP6GLuVeTApcthxfbze9+DVuSknsvmTZXNM2uznsefkjBD9 Oja2e4QcaXE3sTnbXw/QhMt5MBX7ODrzM4J11r5iUTMcrNWnyqv8u0Ulc= X-Received: by 2002:a05:690c:c4e3:b0:79a:7cf4:1a60 with SMTP id 00721157ae682-79a7cf420d2mr20355937b3.48.1773859975837; Wed, 18 Mar 2026 11:52:55 -0700 (PDT) Received: from ryzen ([2601:644:8000:56f5::8bd]) by smtp.gmail.com with ESMTPSA id 00721157ae682-79a71593c08sm21689017b3.24.2026.03.18.11.52.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Mar 2026 11:52:55 -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: [PATCHv3] thunderbolt: use kzalloc_flex Date: Wed, 18 Mar 2026 11:52:37 -0700 Message-ID: <20260318185237.4742-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 Reviewed-by: Kees Cook --- v3: fix kdoc v2: remove extra kfree to fix kernel test bot. drivers/thunderbolt/path.c | 28 +++++++--------------------- drivers/thunderbolt/tb.h | 5 +++-- 2 files changed, 10 insertions(+), 23 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..217c3114bec8 100644 --- a/drivers/thunderbolt/tb.h +++ b/drivers/thunderbolt/tb.h @@ -419,9 +419,9 @@ enum tb_path_port { * @activated: Is the path active * @clear_fc: Clear all flow control from the path config space entries * when deactivating this path - * @hops: Path hops * @path_length: How many hops the path uses * @alloc_hopid: Does this path consume port HopID + * @hops: Path hops * * A path consists of a number of hops (see &struct tb_path_hop). To * establish a PCIe tunnel two paths have to be created between the two @@ -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