Skip to content

Commit f9b8eaf

Browse files
author
Yonghong Song
committed
[BPF] Handle DW_TAG_atomic_type properly
Make change in BTFDebug.cpp to handle DW_TAG_atomic_type properly. Otherwise, a type like _Atomic int i; // global the dwarf type chain atomic->int Since DW_TAG_atomic_type is not processed BTF generation will stop at atomic modifier and BTF will encode 'i' as void type. Similar for type like volatile _Atomic int *p; the dwarf type chain ptr->volatile->atomic->int Since atomic type is not processed and BTF generation will stop at atomic type, the eventual BTF type will be ptr->volatile->void which is incorrect. This patch fixed the following cases including the above two patterns by skipping DW_TAG_atomic_type: - global variable with _Atomic type. - function parameter and return type with _Atomic type. - struct member with _Atomic type. - ptr,const,volatile,restrict pointing to a _Atomic type. - btf_type_tag where ptr pointing to _Atomic type and btf_type_tag. With changed llvm, in kernel selftest arena_atomics.c ([1]), the new bpf code looks like ``` _Atomic __u64 __arena_global and64_value = (0x110ull << 32); _Atomic __u32 __arena_global and32_value = 0x110; SEC("raw_tp/sys_enter") int and(const void *ctx) { ... __c11_atomic_fetch_and(&and64_value, 0x011ull << 32, memory_order_relaxed); __c11_atomic_fetch_and(&and32_value, 0x011, memory_order_relaxed); ... return 0; } ``` and compilation is successful. The skel file arena_atomics.skel.h will be ``` struct arena_atomics__arena { ... __u64 and64_value; __u32 and32_value; ... } *arena; ``` [1] https://lore.kernel.org/r/[email protected]
1 parent 844342c commit f9b8eaf

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1249,8 +1249,12 @@ llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
12491249
CGM.getTarget().getDWARFAddressSpace(
12501250
CGM.getTypes().getTargetAddressSpace(PointeeTy));
12511251

1252+
const BTFTagAttributedType *BTFAttrTy;
1253+
if (auto *Atomic = PointeeTy->getAs<AtomicType>())
1254+
BTFAttrTy = dyn_cast<BTFTagAttributedType>(Atomic->getValueType());
1255+
else
1256+
BTFAttrTy = dyn_cast<BTFTagAttributedType>(PointeeTy);
12521257
SmallVector<llvm::Metadata *, 4> Annots;
1253-
auto *BTFAttrTy = dyn_cast<BTFTagAttributedType>(PointeeTy);
12541258
while (BTFAttrTy) {
12551259
StringRef Tag = BTFAttrTy->getAttr()->getBTFTypeTag();
12561260
if (!Tag.empty()) {

llvm/lib/Target/BPF/BTFDebug.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ static const char *BTFKindStr[] = {
3535
#include "llvm/DebugInfo/BTF/BTF.def"
3636
};
3737

38+
static const DIType *tryRemoveAtomicType(const DIType *Ty) {
39+
if (!Ty)
40+
return Ty;
41+
auto DerivedTy = dyn_cast<DIDerivedType>(Ty);
42+
if (DerivedTy && DerivedTy->getTag() == dwarf::DW_TAG_atomic_type)
43+
return DerivedTy->getBaseType();
44+
return Ty;
45+
}
46+
3847
/// Emit a BTF common type.
3948
void BTFTypeBase::emitType(MCStreamer &OS) {
4049
OS.AddComment(std::string(BTFKindStr[Kind]) + "(id = " + std::to_string(Id) +
@@ -90,7 +99,7 @@ void BTFTypeDerived::completeType(BTFDebug &BDebug) {
9099
return;
91100

92101
// The base type for PTR/CONST/VOLATILE could be void.
93-
const DIType *ResolvedType = DTy->getBaseType();
102+
const DIType *ResolvedType = tryRemoveAtomicType(DTy->getBaseType());
94103
if (!ResolvedType) {
95104
assert((Kind == BTF::BTF_KIND_PTR || Kind == BTF::BTF_KIND_CONST ||
96105
Kind == BTF::BTF_KIND_VOLATILE) &&
@@ -305,7 +314,7 @@ void BTFTypeStruct::completeType(BTFDebug &BDebug) {
305314
} else {
306315
BTFMember.Offset = DDTy->getOffsetInBits();
307316
}
308-
const auto *BaseTy = DDTy->getBaseType();
317+
const auto *BaseTy = tryRemoveAtomicType(DDTy->getBaseType());
309318
BTFMember.Type = BDebug.getTypeId(BaseTy);
310319
Members.push_back(BTFMember);
311320
}
@@ -342,15 +351,15 @@ void BTFTypeFuncProto::completeType(BTFDebug &BDebug) {
342351
IsCompleted = true;
343352

344353
DITypeRefArray Elements = STy->getTypeArray();
345-
auto RetType = Elements[0];
354+
auto RetType = tryRemoveAtomicType(Elements[0]);
346355
BTFType.Type = RetType ? BDebug.getTypeId(RetType) : 0;
347356
BTFType.NameOff = 0;
348357

349358
// For null parameter which is typically the last one
350359
// to represent the vararg, encode the NameOff/Type to be 0.
351360
for (unsigned I = 1, N = Elements.size(); I < N; ++I) {
352361
struct BTF::BTFParam Param;
353-
auto Element = Elements[I];
362+
auto Element = tryRemoveAtomicType(Elements[I]);
354363
if (Element) {
355364
Param.NameOff = BDebug.addString(FuncArgNames[I]);
356365
Param.Type = BDebug.getTypeId(Element);
@@ -483,7 +492,7 @@ void BTFTypeTypeTag::completeType(BTFDebug &BDebug) {
483492
IsCompleted = true;
484493
BTFType.NameOff = BDebug.addString(Tag);
485494
if (DTy) {
486-
const DIType *ResolvedType = DTy->getBaseType();
495+
const DIType *ResolvedType = tryRemoveAtomicType(DTy->getBaseType());
487496
if (!ResolvedType)
488497
BTFType.Type = 0;
489498
else
@@ -800,6 +809,10 @@ void BTFDebug::visitDerivedType(const DIDerivedType *DTy, uint32_t &TypeId,
800809
bool CheckPointer, bool SeenPointer) {
801810
unsigned Tag = DTy->getTag();
802811

812+
if (Tag == dwarf::DW_TAG_atomic_type)
813+
return visitTypeEntry(DTy->getBaseType(), TypeId, CheckPointer,
814+
SeenPointer);
815+
803816
/// Try to avoid chasing pointees, esp. structure pointees which may
804817
/// unnecessary bring in a lot of types.
805818
if (CheckPointer && !SeenPointer) {
@@ -1444,8 +1457,10 @@ void BTFDebug::processGlobals(bool ProcessingMapDef) {
14441457
DIGlobal = GVE->getVariable();
14451458
if (SecName.starts_with(".maps"))
14461459
visitMapDefType(DIGlobal->getType(), GVTypeId);
1447-
else
1448-
visitTypeEntry(DIGlobal->getType(), GVTypeId, false, false);
1460+
else {
1461+
const DIType *Ty = tryRemoveAtomicType(DIGlobal->getType());
1462+
visitTypeEntry(Ty, GVTypeId, false, false);
1463+
}
14491464
break;
14501465
}
14511466

0 commit comments

Comments
 (0)