From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) (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 3DE4C383333 for ; Mon, 8 Jun 2026 18:06:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780942001; cv=none; b=OBfDg9TWclWuaN1wmEqJfLWapuuP2aNTZTw64MQrIuUiDCjCQqbsHlzK5pSXZRqSJot5wUc79K/MH2f67yahoC/wY4lzrZRpXUWOHF1UZog/ZHBXw9SuwkLTXFKQgHpSx7wH/1ETSZY9a5/Rjv7OmVLXfaGWGaJ3i6lU9kdqU8I= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780942001; c=relaxed/simple; bh=9j1zwOiqYR/j62a4SmkndR3I/fBKjx3NMWjiae/q3ic=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=LbU9dMQMKFxkxHuRNMDyI/1jKnw2hv08PVQYOJ/DrWoUB8EWhv/PxSyfj7PV7HBViP5iVa4OQ/8V+RdWZXC0fVbZCl9OZpfi6ruMmbxeIZtwKvUMW1afvkJJUFM5MWayCxiDAg6HcuQBa0wH6PujtMTwjXAXoxsUTQI3iAyiY40= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=D/CmtwqX; arc=none smtp.client-ip=209.85.221.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="D/CmtwqX" Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-4602e2a0372so3277667f8f.3 for ; Mon, 08 Jun 2026 11:06:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1780941998; x=1781546798; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=ruO8SnBumLZRLs1kOpD88cW4Qstol4o2xlBdcC+hpIQ=; b=D/CmtwqXNyGX0yfK977KF9gElPvNCIInU2NrzbQmhyWE0xKKWUEcjUNzDODyE5jo4H sBnXO5I/0mY3/8ps/lM9mP4IK2FwzXYuwTOflph70TpmF3rnTN6gqDaNYNwD6gSU/A2i 4Sj93K9kZ8snRreaiYJrDglKHandL+bfm1LmLR+8JXL+aAaHdBBqOzcRtKSyAuSLosNx aYPfG8IU6BbSM/T4+q37xRRpQjqlIpRzvMZHBH73AK/lA6wU26fM1UNDAxO6ysSSwxuk bZQ/xe4x3cNYqeM95kcG6dE+ZG0/FhSehLhpAG9lcHIYS1nzGJlGPeXUl9dFkvc3bHbO 82Rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780941998; x=1781546798; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ruO8SnBumLZRLs1kOpD88cW4Qstol4o2xlBdcC+hpIQ=; b=G47/bC/qsKGnSTEVriQGT4lgoBzVNQaYVTnW48193JWd0M0hqgt38BcpHn7A7YQVGd gOpndVXvZKZTpu7U1bWHlQHRfVZOhgvJd9ojZaiKIZzIaHP9usQ5CG5P+GkUkVhloZjG 9RMg5St2+oHrjRVz7PLOmY5Gt1+ZaQTB3Z9nMRBM80WQngTA1xhfTzsaiYr7EkaYPBSB sR26rIbJoOSDt36wk/TQcLTL+EpvskQwPDlUGkH1svHGULD724Db8fxKJoZ4UxwjwtVA qG0mLx+jpuwBCP+TLp4z7f3YRhJc6hgjLfzjfOEKdqgG5K5dZJFpZNmc7KbyTfoXzY+w N6wA== X-Forwarded-Encrypted: i=1; AFNElJ86QABYlO52+GbXRGr4lMvwI9VFYAFcWAQOuc/lGWmVGP3aAnrx9yArFUKbO4r7BS2CFuo+Bp1VZWfY6g4=@vger.kernel.org X-Gm-Message-State: AOJu0YyVxF7QmSk9y2+7lpZo0EbCGDw9xaW/McWe3V9ETiw20U8YFn1D n8MM+3sf11MqXk7J5ZizdswBDmsEjjSSGtvKu3W+ttpEP/odopqN9WBkBMX5btuPnfA= X-Gm-Gg: Acq92OEgEQribqf1V6UCZ/U2jOof56Yf045n0UeqJJCl4ny0Jei9YAMq7J3apMaVT+k x6EOkcg4QDUpnGwTly02AzjFlYgJNkQf/3utNQEsgiQpTN23/k2+D4/Hmhvg9erXGrO+4KlrYVF RFOl7dLgZqopaeuy6xb2UWnGAm02pb9yi843vvBbsXq1NruCR+652Y5mX6O9x2beqiNinWVh7iN vtymUKFCw8ecYJrF/BnTRlZiJAomU5ELikXLe6vmjN9Z3nOJOF0toiAJMTYY1n7HyZzkTa9KuAR G8RmMXcA8o+/BtUp0hVdTag3mChpNHTHS84nK2C7x3qmklw0LnkLane+WXKoW4Al4bsypgvLA3y mAIeId52rw67DKJB5ndDShElbjgBl4H3CVog8gXDh8muy5cdWsOfh7ZWzxw7Hahz/8LCukbas3W KfVlRs+gTJEs5SJhyQxB+1lyycvVpLmVhjHeNqGaE= X-Received: by 2002:a05:6000:468d:b0:446:db72:e8ec with SMTP id ffacd0b85a97d-46030643609mr17198053f8f.23.1780941997560; Mon, 08 Jun 2026 11:06:37 -0700 (PDT) Received: from [192.168.1.3] ([185.48.77.170]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4601f2dcb13sm54421372f8f.2.2026.06.08.11.06.35 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 08 Jun 2026 11:06:36 -0700 (PDT) Message-ID: <48f06bc5-0283-4720-a5e5-843af16cb9dd@linaro.org> Date: Mon, 8 Jun 2026 19:06:35 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v4 2/4] perf: Use a union to clear branch entry bitfields To: Puranjay Mohan , bpf@vger.kernel.org Cc: Puranjay Mohan , Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , Will Deacon , Mark Rutland , Catalin Marinas , Leo Yan , Rob Herring , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Ian Rogers , Adrian Hunter , Shuah Khan , Breno Leitao , Ravi Bangoria , Stephane Eranian , Kumar Kartikeya Dwivedi , Usama Arif , linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@meta.com References: <20260527121207.2312181-1-puranjay@kernel.org> <20260527121207.2312181-3-puranjay@kernel.org> Content-Language: en-US From: James Clark In-Reply-To: <20260527121207.2312181-3-puranjay@kernel.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 27/05/2026 1:11 pm, Puranjay Mohan wrote: > perf_clear_branch_entry_bitfields() zeroes individual bitfields of struct > perf_branch_entry but has repeatedly fallen out of sync when new fields > were added (new_type and priv were missed). > > Wrap the bitfields in an anonymous struct inside a union with a u64 > bitfields member, and clear them all with a single assignment. This > avoids having to update the clearing function every time a new bitfield > is added. > > Fixes: bfe4daf850f4 ("perf/core: Add perf_clear_branch_entry_bitfields() helper") > Signed-off-by: Puranjay Mohan > --- > include/linux/perf_event.h | 9 +-------- > include/uapi/linux/perf_event.h | 25 +++++++++++++++---------- > tools/include/uapi/linux/perf_event.h | 25 +++++++++++++++---------- > 3 files changed, 31 insertions(+), 28 deletions(-) > > diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h > index 48d851fbd8ea..f7360c43f902 100644 > --- a/include/linux/perf_event.h > +++ b/include/linux/perf_event.h > @@ -1474,14 +1474,7 @@ static inline u32 perf_sample_data_size(struct perf_sample_data *data, > */ > static inline void perf_clear_branch_entry_bitfields(struct perf_branch_entry *br) > { > - br->mispred = 0; > - br->predicted = 0; > - br->in_tx = 0; > - br->abort = 0; > - br->cycles = 0; > - br->type = 0; > - br->spec = PERF_BR_SPEC_NA; > - br->reserved = 0; > + br->bitfields = 0; The comment above here says: * The to and from fields are not cleared because they are * systematically modified by caller. But this is an inline function and the to/from fields are always assigned right after calling it. Surely the compiler can work that out and this was a premature optimization. If we rename it to perf_clear_branch_entry() and do: *br = (struct perf_branch_entry) {0}; Then we can delete a big comment, remove a potential source of bugs if even more members are added, not have to update the uapi headers and simplify it instead of working around the original issue. > } > > extern void perf_output_sample(struct perf_output_handle *handle, > diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h > index fd10aa8d697f..c2e7b1b1c4fa 100644 > --- a/include/uapi/linux/perf_event.h > +++ b/include/uapi/linux/perf_event.h > @@ -1491,16 +1491,21 @@ union perf_mem_data_src { > struct perf_branch_entry { > __u64 from; > __u64 to; > - __u64 mispred : 1, /* target mispredicted */ > - predicted : 1, /* target predicted */ > - in_tx : 1, /* in transaction */ > - abort : 1, /* transaction abort */ > - cycles : 16, /* cycle count to last branch */ > - type : 4, /* branch type */ > - spec : 2, /* branch speculation info */ > - new_type : 4, /* additional branch type */ > - priv : 3, /* privilege level */ > - reserved : 31; > + union { > + struct { > + __u64 mispred : 1, /* target mispredicted */ > + predicted : 1, /* target predicted */ > + in_tx : 1, /* in transaction */ > + abort : 1, /* transaction abort */ > + cycles : 16, /* cycle count to last branch */ > + type : 4, /* branch type */ > + spec : 2, /* branch speculation info */ > + new_type : 4, /* additional branch type */ > + priv : 3, /* privilege level */ > + reserved : 31; > + }; > + __u64 bitfields; > + }; > }; > > /* Size of used info bits in struct perf_branch_entry */ > diff --git a/tools/include/uapi/linux/perf_event.h b/tools/include/uapi/linux/perf_event.h > index fd10aa8d697f..c2e7b1b1c4fa 100644 > --- a/tools/include/uapi/linux/perf_event.h > +++ b/tools/include/uapi/linux/perf_event.h > @@ -1491,16 +1491,21 @@ union perf_mem_data_src { > struct perf_branch_entry { > __u64 from; > __u64 to; > - __u64 mispred : 1, /* target mispredicted */ > - predicted : 1, /* target predicted */ > - in_tx : 1, /* in transaction */ > - abort : 1, /* transaction abort */ > - cycles : 16, /* cycle count to last branch */ > - type : 4, /* branch type */ > - spec : 2, /* branch speculation info */ > - new_type : 4, /* additional branch type */ > - priv : 3, /* privilege level */ > - reserved : 31; > + union { > + struct { > + __u64 mispred : 1, /* target mispredicted */ > + predicted : 1, /* target predicted */ > + in_tx : 1, /* in transaction */ > + abort : 1, /* transaction abort */ > + cycles : 16, /* cycle count to last branch */ > + type : 4, /* branch type */ > + spec : 2, /* branch speculation info */ > + new_type : 4, /* additional branch type */ > + priv : 3, /* privilege level */ > + reserved : 31; > + }; > + __u64 bitfields; > + }; > }; > > /* Size of used info bits in struct perf_branch_entry */