From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.16 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795457071; b=IitXWiqFNEcafp6mhVVGC/CDX1g9cPUyqe22yqU32fWEuvvH2zFhn08k0YcQklVNE2ZP LnA2a9MYAb2ao4m90h7TDDpb4QLXI97NnHNxyZu14mgzsDAuUp8dabvmWGbSIENW44drV L9kL1ju/zpdaDgyEV+apVc8cyF19DhbFugbqOWT65Z5d1QRq1ysXh7Og1+HjNxxM9GsSZ 9VfzkntG9CKS+ROIVZVLCo1iWnQ4mQEjt+ZOqL3Ac/fMUzCKp5mGrDzWCXZPQj066nj0s luL0MTH8cilNAAtazf7iYMdhjI070hKM7JVdkXc2VGmMQPDrGv5PUDeveF8EADulzog== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795457071; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:Message-ID:Date:MIME-Version:User-Agent: Subject:From:To:Cc:References:Content-Language:In-Reply-To: Content-Type:Content-Transfer-Encoding; bh=/MRrQ3r/FsSqfgiAVcoLx4c4RLhZEkDW2vzDgBjMFHY=; b=U2IBL7NsaFAtBvkAxG5pp6DhQhqEzh+WrMbpAIA6qoysFyBHXKXVFn9jPIn+iNoxZmwB z+d2gu9x/n/PNEO4lV3fmXd9q5Tn5CJxenDhZ4fPzFrrA6EyjLo6Xm0McDcmnxCugROwg D4Wba2GhsrhhfzueJdcd8gDCpZ85h/8TPDZyK5/epT5MUk5s9g+0L4HJi2/GenpjQlzsU 1JEKHdLFp+3HD2KYQ9VHGfpCVnCxSYVudWHesc+JEbXaAmQ1IPMlSpSZ5IzyO2S/yf+0g F5/l8W+FC31UkCm79XRtdYzc2IWBhzeqjZ/3bQPWO+yRrc3VTZq4iLQedtmvrYotkCQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=IgkY0jIl; arc=none smtp.remote-ip=91.218.175.16 Received: from mta0.migadu.com (out-16.mta0.migadu.com [91.218.175.16]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 97B9D606A31E for ; Mon, 23 Nov 2026 18:04:30 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 97B9D606A31E 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, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=IgkY0jIl X-Envelope-To: bpf@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=xV3BrWnrBSFsdnZPVdY3EKhS+zRaIDNGentZoexLv/0=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795457068; v=1; x=1796061868; b=IgkY0jIlaPMtxMg30S2bXBQ8BnmJlWwDq4r3//qAnyRsveMYxlSVmDJZCoflx3HhWG9MyD4X du8M1f2m0dojRDRsxeX8MGOqA8l5WJA02zydg/bzeZIwIA42+cxwHRgqzDI8FNzgg3t/+I+3Ns8 9VkPoFUxoVmEyb6ICrG6DoZI= X-Envelope-To: bpf@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id ec4d2de4b5deccbd; Mon, 23 Nov 2026 18:04:28 +0000 X-Mizu-Trace-ID: ec4d2de4b5deccbd X-Migadu-Flow: FLOW_OUT Message-ID: Date: Mon, 23 Nov 2026 10:04:22 -0800 X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH bpf-next v5 0/3] libbpf: Fix CO-RE relocations of unsigned 32-bit enum values From: Yonghong Song To: Eduard Zingerman , bpf@vger.kernel.org Cc: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , kernel-team@fb.com, vineet.gupta@linux.dev References: <20261120214957.868409-1-yonghong.song@linux.dev> <8fdf1b51-7368-46e0-80f9-744b15845ad0@linux.dev> Content-Language: en-GB In-Reply-To: <8fdf1b51-7368-46e0-80f9-744b15845ad0@linux.dev> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 11/23/26 10:01 AM, Yonghong Song wrote: > > > On 11/23/26 9:45 AM, Eduard Zingerman wrote: >> On Fri, 2026-11-20 at 13:49 -0800, Yonghong Song wrote: >>> For a BPF_CORE_ENUMVAL_VALUE relocation, libbpf sign-extends the value >>> of a 32-bit enumerator even if the enum is unsigned. A value in >>> [0x80000000, 0xffffffff], e.g., DCACHE_PRIVATE of enum dentry_flags, >>> then doesn't match the zero-extended value clang emits, and the whole >>> object is refused. And a target value in that range is patched >>> sign-extended. >>> >>> Patch 1 zero-extends the 32-bit values of unsigned enums, i.e., with >>> the kflag 0, which is the form clang emits. The kernel builds the same >>> relo_core.c, so it gets the fix too. >>> >>> GCC emits every 32-bit enum value sign-extended, since its >>> __builtin_preserve_enum_value() returns int. So with the change above >>> alone, GCC objects that load today would be refused. Patch 1 therefore >>> also accepts the sign-extended form of an unsigned value, and patches >>> the target value in the same form. >> Hi Yonghong, >> >> I checked LLVM and GCC definitions for __builtin_preserve_enum_value(). >> LLVM: >> >>    clang/include/clang/Basic/BuiltinsBPF.td: >> >>    def PreserveEnumValue : TargetBuiltin { >>      let Spellings = ["__builtin_preserve_enum_value"]; >>      let Attributes = [CustomTypeChecking]; >>      let Prototype = "long int(...)"; >>    } >> >>    --- >> >>    gcc/config/bpf/bpf.cc: >> >>      def_builtin ("__builtin_preserve_enum_value", >>                   BPF_BUILTIN_PRESERVE_ENUM_VALUE, >>                   build_function_type_list (integer_type_node, >> ptr_type_node, >>                                             integer_type_node, >> integer_type_node, >>                                             0)); >>    gcc/tree.cc: >> >>      integer_type_node = make_signed_type (INT_TYPE_SIZE); >> >> >> Meaning that the two indeed disagree. Given that GCC version can't >> express 64-bit values, it appears that GCC side definition needs an >> update. Would this allow to avoid GCC-specific workaround in libbpf? > > Yes, I am aware of this issue. See > https://lore.kernel.org/bpf/a4aec2b1-f754-48ed-a7d7-ef07bed43eb8@linux.dev/ > > I even suggested a solution: > > diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc index > 3ef93938222..45d5337bab6 100644 --- a/gcc/config/bpf/bpf.cc +++ > b/gcc/config/bpf/bpf.cc @@ -1048,7 +1048,7 @@ bpf_init_builtins (void) > integer_type_node, 0)); >     def_builtin ("__builtin_preserve_enum_value", >                 BPF_BUILTIN_PRESERVE_ENUM_VALUE, > - build_function_type_list (integer_type_node, ptr_type_node, + > build_function_type_list (ullt, ptr_type_node, integer_type_node, > integer_type_node, >                                           0)); > > diff --git a/gcc/config/bpf/core-builtins.cc > b/gcc/config/bpf/core-builtins.cc index a5c973cd385..289d3e67d78 > 100644 --- a/gcc/config/bpf/core-builtins.cc +++ > b/gcc/config/bpf/core-builtins.cc @@ -877,8 +877,12 @@ > pack_enum_value_fail:     else >       { >         ret.reloc_data.expr = tmp; > + /* Extend the enumerator to 64 bits according to its own signedness, > + as clang does, instead of expanding it as a sign-extended SImode + > CONST_INT. */         if (kind == BPF_RELO_ENUMVAL_VALUE) > - ret.reloc_data.default_value = enum_value; + > ret.reloc_data.default_value + = fold_convert > (long_long_unsigned_type_node, enum_value);         else >          ret.reloc_data.default_value = integer_one_node; >       } > > Vineet Gupta will take a look. > > Discussed with Alexei earlier, the suggestion is to proceed as is for > now. > It is not clear when gcc will implement this. Sorry, messed up the above diff. The below is a better one: diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc index 3ef93938222..45d5337bab6 100644 --- a/gcc/config/bpf/bpf.cc +++ b/gcc/config/bpf/bpf.cc @@ -1048,7 +1048,7 @@ bpf_init_builtins (void) integer_type_node, 0)); def_builtin ("__builtin_preserve_enum_value", BPF_BUILTIN_PRESERVE_ENUM_VALUE, - build_function_type_list (integer_type_node, ptr_type_node, + build_function_type_list (ullt, ptr_type_node, integer_type_node, integer_type_node, 0)); diff --git a/gcc/config/bpf/core-builtins.cc b/gcc/config/bpf/core-builtins.cc index a5c973cd385..289d3e67d78 100644 --- a/gcc/config/bpf/core-builtins.cc +++ b/gcc/config/bpf/core-builtins.cc @@ -877,8 +877,12 @@ pack_enum_value_fail: else { ret.reloc_data.expr = tmp; + /* Extend the enumerator to 64 bits according to its own signedness, + as clang does, instead of expanding it as a sign-extended SImode + CONST_INT. */ if (kind == BPF_RELO_ENUMVAL_VALUE) - ret.reloc_data.default_value = enum_value; + ret.reloc_data.default_value + = fold_convert (long_long_unsigned_type_node, enum_value); else ret.reloc_data.default_value = integer_one_node; }