From nobody Mon Feb 9 05:38:44 2026 Received: from out-184.mta0.migadu.com (out-184.mta0.migadu.com [91.218.175.184]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ED6DF217659 for ; Mon, 28 Apr 2025 21:07:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.184 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745874476; cv=none; b=EIrhhyeqxXeFIeyLkHQwHxbqdJ91tKDyO/03fCT+XWZ4AXyznijR7WdFoUHtDJ6owg8ikUPxeNndrMJwPY6IuaMJR7w8+5Lx7ttNslWs+s22QhfLlsjaunNneA+IJJx22tNIJrCkSRloL9yLeRzbo9ahlkQ2onccsPmwYA0c+EE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745874476; c=relaxed/simple; bh=f6Ivb5X5pkMHM4s201C1kB1phD/8VjqDKvpUccGBZXo=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=YwqGtG245OTJIckY/1hcXMODhZR1hqXVEPz9nOmkXb/wJPFuqRxUw03ZK4QY3JCEaAOx2OCTByrDP3QmiFo83WJ5ndjGDhl+BKbFpB/6kfZivdLf7o5hQDJCsu3NJtvds4Z9QDASUHaY2LBCKupaaRF7mIUU0bgTAC/xzR1jbk8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=U1T3vtwf; arc=none smtp.client-ip=91.218.175.184 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="U1T3vtwf" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1745874470; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=jUS0s/tdQHjXzAtC64/fbqpJmBqxqUB4naODkeZs7IA=; b=U1T3vtwfTe5bOElYIfF/krvWpK5HgvnWcdnXVLyZarrrKWxfFXLo9xJmXvcmeMnH2AFG2I kpTxztQwONsX8FRRY2Yvhm8qR/ywGZQi7Ih5eDLUiJIEg1+pKROu4XNCWkU3e/KW2/8+0Q /cmhYKq/mB8Qi73n6JKhrjJEvwsGvYo= From: Thorsten Blum To: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa Cc: Thorsten Blum , bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH bpf-next] bpf: Replace offsetof() with struct_size() Date: Mon, 28 Apr 2025 23:06:39 +0200 Message-ID: <20250428210638.30219-2-thorsten.blum@linux.dev> 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 X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Compared to offsetof(), struct_size() provides additional compile-time checks for structs with flexible arrays (e.g., __must_be_array()). No functional changes intended. Signed-off-by: Thorsten Blum --- kernel/bpf/syscall.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 9794446bc8c6..d7287f3e260b 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -36,6 +36,7 @@ #include #include #include +#include =20 #include #include @@ -693,7 +694,7 @@ struct btf_record *btf_record_dup(const struct btf_reco= rd *rec) =20 if (IS_ERR_OR_NULL(rec)) return NULL; - size =3D offsetof(struct btf_record, fields[rec->cnt]); + size =3D struct_size(rec, fields, rec->cnt); new_rec =3D kmemdup(rec, size, GFP_KERNEL | __GFP_NOWARN); if (!new_rec) return ERR_PTR(-ENOMEM); @@ -748,7 +749,7 @@ bool btf_record_equal(const struct btf_record *rec_a, c= onst struct btf_record *r return false; if (rec_a->cnt !=3D rec_b->cnt) return false; - size =3D offsetof(struct btf_record, fields[rec_a->cnt]); + size =3D struct_size(rec_a, fields, rec_a->cnt); /* btf_parse_fields uses kzalloc to allocate a btf_record, so unused * members are zeroed out. So memcmp is safe to do without worrying * about padding/unused fields. --=20 2.49.0