From patchwork Mon Sep 16 09:17:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13805202 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (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 B29661422B8 for ; Mon, 16 Sep 2024 09:18:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726478300; cv=none; b=Ae5+C85zaELKnwaW65jCQFbv/p/O1jD6697cNi/vTYEg/EVIU7rB2xTwR1uZNt1PhAc+4tZxNZl7j3hoayFqSKHv2+M3ltulhhwAcUUDQszINv1a66yBrBFg8Wb3L0Cua2nB+LITrgwz+0MQjSC18QZNs3nGEloKxugsKJfQl+M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726478300; c=relaxed/simple; bh=Ehu+tQlV3Yn9+mb+64Att64L+pNpYJzRbY/H/0fZsUc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BE5RKDph2K0e/Ox+X+qdMQB8nXCWkjSpmBRd7CuzTUn4PGVwh8Iem5JFUrrBLXarXDtVz1YQbcKZHT2rdhNdJNmPEcDHscXzdtjegIjcBtxPRmntw3dl4UwmHVhi1+eueDUNNAzdvPcPikD3YmHe/4VITIfwo2lXthLARVrelwM= 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=KeHtyvnK; arc=none smtp.client-ip=209.85.214.180 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="KeHtyvnK" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-2068acc8a4fso26828015ad.1 for ; Mon, 16 Sep 2024 02:18:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726478298; x=1727083098; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=9tLwns4BgiXw9pLV109Ngds60h7ORpQpM9oAr5JWGfY=; b=KeHtyvnK7GUUjooDXXpi45neQo3uUdehKsuCxqDlLqiFcQ1iBG87qJ4x6e+u4QqIPq EJEHeS82r19WllBgZCEu+NhWd8jjVXYYjxkyCbDAKfYbx1TnM6ELHEUkuf07SP5AKXav FeQVACkWyBmsxmjMU4lrNNutVXk2Yy6YgAp9Cn4KFJu4aBygtHryNjB7FhaIQuVSrrao vautG+fKerBLXgw+EKJ5pWfZa0ZMHyFEm5INxhAUMzgBwguBhdz4MDfqxqqfeU8Im6EB EHqbn+tIvtzSNzqIKAOWeQQHGZQsnq1J6B8LoMj5mKeitEObonu9UQNptPIOBkDnWIUS kytQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726478298; x=1727083098; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9tLwns4BgiXw9pLV109Ngds60h7ORpQpM9oAr5JWGfY=; b=h9iwnDrb8JTTKTtbq/uzp9uTbFxaSJaEGocOYua1RkSPTkf13CIXymYDjuVFtz5Ok7 7K1moD5+LqvWTQ1INAPo0gMsJdyiYD9ragiWiwjonYg6thxcKQw8PVmNOHjFeHf1dIBA z5+SN3YtljTeNNglz7PvD/V1bukS2LJ861s/gVa4UUxlkMTky1M2gWQsat22Gv+xqm0W Oy9pfKrDo7FHf/2nT1Qnri4coubskQ/cfFGLU+BQvtZRJvLLKWbSQACqIyaN6rCn0X6A ZybpZ8jckNECiLDWGeTZzxi4zH+4AfA1t4HCIOeR1WhtLSGt4PgauFCUPhihjsZvLTwg y+/A== X-Gm-Message-State: AOJu0YzZOqm9Iy0xUcxSkttAfkafbg/f0Ku0l5WQjyLc6tTQ1hrAeLUE ++wfqGBjSZ9A3N52EN+WHXo7QwOdCth2BAMaKlhXTVjZpKroQUZ5YQ4oZw== X-Google-Smtp-Source: AGHT+IGWJWQj0zMrq/Ra1kxCXRdntcPH5pneCWsuYBHFZIrfsJLC4sChWsVlDmdq6/B+9AcsmN9ErA== X-Received: by 2002:a17:902:d2ca:b0:202:2f0:3bb2 with SMTP id d9443c01a7336-20782c201e0mr109652265ad.60.1726478297621; Mon, 16 Sep 2024 02:18:17 -0700 (PDT) Received: from honey-badger.. ([38.34.87.7]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-207945da63fsm32882195ad.38.2024.09.16.02.18.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Sep 2024 02:18:16 -0700 (PDT) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, kernel-team@fb.com, yonghong.song@linux.dev, arnaldo.melo@gmail.com, Eduard Zingerman Subject: [PATCH bpf-next v1 1/4] bpf: allow specifying bpf_fastcall attribute for BPF helpers Date: Mon, 16 Sep 2024 02:17:09 -0700 Message-ID: <20240916091712.2929279-2-eddyz87@gmail.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240916091712.2929279-1-eddyz87@gmail.com> References: <20240916091712.2929279-1-eddyz87@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Allow a new optional 'Attributes' section to be specified for helper functions description, e.g.: * u32 bpf_get_smp_processor_id(void) * ... * Return * ... * Attributes * __bpf_fastcall * Generated header for the example above: #ifndef __bpf_fastcall #if __has_attribute(__bpf_fastcall) #define __bpf_fastcall __attribute__((bpf_fastcall)) #else #define __bpf_fastcall #endif #endif ... __bpf_fastcall static __u32 (* const bpf_get_smp_processor_id)(void) = (void *) 8; The following rules apply: - when present, section must follow 'Return' section; - attribute names are specified on the line following 'Attribute' keyword; - attribute names are separated by spaces; - section ends with an "empty" line (" *\n"). Valid attribute names are recorded in the ATTRS map. ATTRS maps shortcut attribute name to correct C syntax. Signed-off-by: Eduard Zingerman --- scripts/bpf_doc.py | 50 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/scripts/bpf_doc.py b/scripts/bpf_doc.py index c55878bddfdd..db50c8d7d112 100755 --- a/scripts/bpf_doc.py +++ b/scripts/bpf_doc.py @@ -37,10 +37,11 @@ class APIElement(object): @desc: textual description of the symbol @ret: (optional) description of any associated return value """ - def __init__(self, proto='', desc='', ret=''): + def __init__(self, proto='', desc='', ret='', attrs=[]): self.proto = proto self.desc = desc self.ret = ret + self.attrs = attrs class Helper(APIElement): @@ -81,6 +82,11 @@ class Helper(APIElement): return res +ATTRS = { + '__bpf_fastcall': 'bpf_fastcall' +} + + class HeaderParser(object): """ An object used to parse a file in order to extract the documentation of a @@ -111,7 +117,8 @@ class HeaderParser(object): proto = self.parse_proto() desc = self.parse_desc(proto) ret = self.parse_ret(proto) - return Helper(proto=proto, desc=desc, ret=ret) + attrs = self.parse_attrs(proto) + return Helper(proto=proto, desc=desc, ret=ret, attrs=attrs) def parse_symbol(self): p = re.compile(r' \* ?(BPF\w+)$') @@ -192,6 +199,28 @@ class HeaderParser(object): raise Exception("No return found for " + proto) return ret + def parse_attrs(self, proto): + p = re.compile(r' \* ?(?:\t| {5,8})Attributes$') + capture = p.match(self.line) + if not capture: + return [] + # Expect a single line with mnemonics for attributes separated by spaces + self.line = self.reader.readline() + p = re.compile(r' \* ?(?:\t| {5,8})(?:\t| {8})(.*)') + capture = p.match(self.line) + if not capture: + raise Exception("Incomplete 'Attributes' section for " + proto) + attrs = capture.group(1).split(' ') + for attr in attrs: + if attr not in ATTRS: + raise Exception("Unexpected attribute '" + attr + "' specified for " + proto) + self.line = self.reader.readline() + if self.line != ' *\n': + raise Exception("Expecting empty line after 'Attributes' section for " + proto) + # Prepare a line for next self.parse_* to consume + self.line = self.reader.readline() + return attrs + def seek_to(self, target, help_message, discard_lines = 1): self.reader.seek(0) offset = self.reader.read().find(target) @@ -789,6 +818,21 @@ class PrinterHelpers(Printer): print('%s;' % fwd) print('') + used_attrs = set() + for helper in self.elements: + for attr in helper.attrs: + used_attrs.add(attr) + for attr in sorted(used_attrs): + print('#ifndef %s' % attr) + print('#if __has_attribute(%s)' % ATTRS[attr]) + print('#define %s __attribute__((%s))' % (attr, ATTRS[attr])) + print('#else') + print('#define %s' % attr) + print('#endif') + print('#endif') + if used_attrs: + print('') + def print_footer(self): footer = '' print(footer) @@ -827,6 +871,8 @@ class PrinterHelpers(Printer): print(' *{}{}'.format(' \t' if line else '', line)) print(' */') + if helper.attrs: + print(" ".join(helper.attrs)) print('static %s %s(* const %s)(' % (self.map_type(proto['ret_type']), proto['ret_star'], proto['name']), end='') comma = '' From patchwork Mon Sep 16 09:17:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13805203 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (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 EF1EF1494CF for ; Mon, 16 Sep 2024 09:18:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726478301; cv=none; b=WIz4PjsR4XhD6eFIUxBpckd3/u7TkbVlIZ22rUKtuK7iHSt8cHCKBYzjUmelDXYrY5fFjtWk3hRC3ArddgacRHP/CNK3GYGIL4rf1qPMNtV8AZWC6CeCIp3wxmTNvqaNV5Pq0GyyOnDFfb1w+gGyaEC5EMEc9Yhtmw0+5pIcn3o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726478301; c=relaxed/simple; bh=Ae9QwDopzJTnytsNNCMQdMvvJAR7HEi7/xjmZEOVzHI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uxUj/O33bubh9ZAz/OgzoPxtGEh52DF31Du/RaTaj6LapvJIwE6511uVJ/VMabbnsugXlj7cAXnZzmtDTv6B8mkb46yybZZi1h6O+dg2M7yTb8KzWN16QF/g7TTSo77Ny1qghZdwEeKcq/4hEGnyo7zbmTdzcJ5FmopCxT29zUI= 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=UsJuCXQ3; arc=none smtp.client-ip=209.85.214.176 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="UsJuCXQ3" Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-201d5af11a4so47033585ad.3 for ; Mon, 16 Sep 2024 02:18:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726478299; x=1727083099; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=wohpFGdgdGWZ71ddt6kBAYLO4PksGhnaccJ5XelkzB8=; b=UsJuCXQ3L4vZAdA78GAgcA4QeQCCwzF9er0krAQuSHzPD3O88j3GSdUsQ8Va/fUcs/ Za/bxvijelzzWS6+MjOUwLI1o4sXVNKDnLsxd3a/+Yc7GtHMNjiqHYUXutXDtAiIguBv hwXWYwJy6Av9lC5u4q99ZmSUoqoVRZKSmuWX80ND+SPjKm5WhmHlQNAuKRQjkl0vbwBR zgsSFA+crdgiuE8Km5Qq1j0ZtPYfdfaKFl8vXqDkapNH3m3TwNnINkmAZ/viNBG/P3tb U4J1lC5Y9pGrIYQ5g9yobim4hhLr+qb9sE4kt6hcJ908IzjNFaMFgJLo7UJyw0yHbElX FF3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726478299; x=1727083099; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wohpFGdgdGWZ71ddt6kBAYLO4PksGhnaccJ5XelkzB8=; b=V9aTxcbZbC1B4ImSX57fjfOVpchPVMs31zYIwYyBjo/D3bBylg0P9HO74aIUBzRviA DXYaKZ4agr6X28wqF94BaXgu4EbcgNVskKSd58iP5YRel+cAKizylpm0i/FISXcF2y8P yKYf3GL0AcfYrogE3EMV+/09UMMpiuUuxtLLcxdY1sSlwiZ1wxtiF5thZyVA7qUdk/Sp jiyjdCbN4gh6a8vBBBPs0tiipJWC0R1cmH9T+kg8/ouM8hsR61lyGynD2stIcbhcbX2I 0DE1cuNVEzXt92J8lFrgyqpB1dvBzLkZIPaM0mjr2G0zmmD3/HqwkgCu6RIsosTQveT9 XebA== X-Gm-Message-State: AOJu0Ywot7O40A2VhhCfMg3jZGGCXbQukzEvCelJO1HcuKj+6Waf8Tkm QhG7rea3zXkDvkdMGCVSrEYMegHqpxV+0OlyJtP1qOuFNT4Z5nK1V26Cxw== X-Google-Smtp-Source: AGHT+IGTYmjX01dC96m1VY9uAnIXrP4MVVn02LTpib0O/GsdsKtHkmh2u0uBV9ZN61w7uGKKvvE7kw== X-Received: by 2002:a17:902:d2ca:b0:206:c2f4:afb7 with SMTP id d9443c01a7336-2076e36db8cmr194234095ad.26.1726478298736; Mon, 16 Sep 2024 02:18:18 -0700 (PDT) Received: from honey-badger.. ([38.34.87.7]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-207945da63fsm32882195ad.38.2024.09.16.02.18.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Sep 2024 02:18:18 -0700 (PDT) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, kernel-team@fb.com, yonghong.song@linux.dev, arnaldo.melo@gmail.com, Eduard Zingerman Subject: [PATCH bpf-next v1 2/4] bpf: __bpf_fastcall for bpf_get_smp_processor_id in uapi Date: Mon, 16 Sep 2024 02:17:10 -0700 Message-ID: <20240916091712.2929279-3-eddyz87@gmail.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240916091712.2929279-1-eddyz87@gmail.com> References: <20240916091712.2929279-1-eddyz87@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Since [1] kernel supports __bpf_fastcall attribute for helper function bpf_get_smp_processor_id(). Update uapi definition for this helper in order to have this attribute in the generated bpf_helper_defs.h [1] commit 91b7fbf3936f ("bpf, x86, riscv, arm: no_caller_saved_registers for bpf_get_smp_processor_id()") Signed-off-by: Eduard Zingerman --- include/uapi/linux/bpf.h | 2 ++ tools/include/uapi/linux/bpf.h | 2 ++ 2 files changed, 4 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index c3a5728db115..fd1f59c6d1de 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -1970,6 +1970,8 @@ union bpf_attr { * program. * Return * The SMP id of the processor running the program. + * Attributes + * __bpf_fastcall * * long bpf_skb_store_bytes(struct sk_buff *skb, u32 offset, const void *from, u32 len, u64 flags) * Description diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index f329ee44627a..22b041c81276 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -1970,6 +1970,8 @@ union bpf_attr { * program. * Return * The SMP id of the processor running the program. + * Attributes + * __bpf_fastcall * * long bpf_skb_store_bytes(struct sk_buff *skb, u32 offset, const void *from, u32 len, u64 flags) * Description From patchwork Mon Sep 16 09:17:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13805204 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (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 90CC24AEF2 for ; Mon, 16 Sep 2024 09:18:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726478301; cv=none; b=oiRJ0x6Omt/WHkVKC4FJCRBlkBxgU3iP3FMG7upDwsYg9jANeJds7oUG2Qzu0+lAsdnz/Yij4beJ5KSXcyfeXq67qzVwOVbDyvhNEXo5rL5X5HcyjncW0+bbeUC4Ra3kj4wdN0UBs3KyMSI3AsAH99IoMihKsZXmQZcXDKeeoZ8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726478301; c=relaxed/simple; bh=vXB1ltdueuJTPO9Ur04wBgDjbhC/Js9Buiqj+gYXNOc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UMpaP22ldgX14rrjKa467icBEiLg0gISMO8LMB2hVOq2uZQiYq0xsyfKI4m084EYzTfZ2kU3rlWt/CRKBYXZsBh68aShpVPkruS49Gzw0TlpLLSAzm29JR+Rqw7p3V0SRKZ7Ny+E1oz3ZiX4Y3lYsc19ePp2Ak85IIkCSGXzd+o= 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=I/GlrFEI; arc=none smtp.client-ip=209.85.214.170 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="I/GlrFEI" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-2055a3f80a4so27222535ad.2 for ; Mon, 16 Sep 2024 02:18:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726478299; x=1727083099; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=PbxFz7dV+ZWVbIZIwQdM92e5BVqo4TzVLga0n5ZdWjQ=; b=I/GlrFEItO2GYoWsqJaQLAwrjRpWUoK3+6kNzcSorLLk/B4aDi7OmHNrcyhh5wLJSU pURzp8SxJiVwR5aixFZ0vwrcFM48vspQeh+0EDeAqAb4tBu9CfuSgTIAb2U2mInR7GGp TFA/23GhTV3Lz8Q0P205hIgOsVcBs90EAX1fWyQN0xGvHgAj4rbRnjvFc5wGx09Jj9G0 AxETUxO8Ur5RZM4r+atmZInaYbAWf3X91CiKhXMyc4xqsa0XOFaOxY2YkE5kODutZnNT 89rAPZhhPHkRiMqDp59RceSQhFu/sFN2a+K6iXNFCllGg7Elvyzr1IuGveMnMsVr4PFj nGbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726478299; x=1727083099; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=PbxFz7dV+ZWVbIZIwQdM92e5BVqo4TzVLga0n5ZdWjQ=; b=hw5o2BrJL32qK1H77W66t4tARRanvkLQhGYV7Xa7nd/Xj5hvkf1azz5UZqaWjhQZ2f x0vkCBBYJqiFRQ1wHc1j2f8IZd1p3MuR1XLWcs6RLd8xq+qlnLLumUDTfYu4tCOpQD7j A2hVVLsJk46cWvZM0qhmoD6l/PnuLHyolnX3pm6kde/lcityTYl1Qm1i4E/FjVaTv20R lkccAMnGA/YDSzfMdLxW6bigV4OPyNqfAbb2qxXmmpdtZ87MmEm8iwP9bnejpokEa2oO 3DZPbY9fCnKJ2x17c/WhvP4hQ5XE7uoZJdtEDziXFNVVHC4JTf62rRDZ831yf3HPHftW vjVg== X-Gm-Message-State: AOJu0YzyqF6qHmGPpSu6NQb0c8twHn9WLRPL4rZSH5LYUXHvtPRTBNBx z7afKIFoMuyjl2owWWmGV2J9YOyA8C3SGEcoBtaoRRxXY+dnO2w0lkTkfw== X-Google-Smtp-Source: AGHT+IHD/k9jIP+Hsb+wcBHraD2Ehqtais5TIxtz2CIjmmKPgunztZxyf8msaTpmQbuScVvXo7iRug== X-Received: by 2002:a17:902:c411:b0:206:b7b8:1f0e with SMTP id d9443c01a7336-2076e319544mr217856865ad.1.1726478299528; Mon, 16 Sep 2024 02:18:19 -0700 (PDT) Received: from honey-badger.. ([38.34.87.7]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-207945da63fsm32882195ad.38.2024.09.16.02.18.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Sep 2024 02:18:19 -0700 (PDT) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, kernel-team@fb.com, yonghong.song@linux.dev, arnaldo.melo@gmail.com, Eduard Zingerman Subject: [PATCH bpf-next v1 3/4] bpf: use KF_FASTCALL to mark kfuncs supporting fastcall contract Date: Mon, 16 Sep 2024 02:17:11 -0700 Message-ID: <20240916091712.2929279-4-eddyz87@gmail.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240916091712.2929279-1-eddyz87@gmail.com> References: <20240916091712.2929279-1-eddyz87@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net In order to allow pahole add btf_decl_tag("bpf_fastcall") for kfuncs supporting bpf_fastcall, mark such functions with KF_FASTCALL in id_set8 objects. Signed-off-by: Eduard Zingerman --- include/linux/btf.h | 1 + kernel/bpf/helpers.c | 4 ++-- kernel/bpf/verifier.c | 5 +---- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/include/linux/btf.h b/include/linux/btf.h index b8a583194c4a..631060e3ad14 100644 --- a/include/linux/btf.h +++ b/include/linux/btf.h @@ -75,6 +75,7 @@ #define KF_ITER_NEXT (1 << 9) /* kfunc implements BPF iter next method */ #define KF_ITER_DESTROY (1 << 10) /* kfunc implements BPF iter destructor */ #define KF_RCU_PROTECTED (1 << 11) /* kfunc should be protected by rcu cs when they are invoked */ +#define KF_FASTCALL (1 << 12) /* kfunc supports bpf_fastcall protocol */ /* * Tag marking a kernel function as a kfunc. This is meant to minimize the diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 3956be5d6440..d80632405148 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -3052,8 +3052,8 @@ BTF_ID(func, bpf_cgroup_release_dtor) #endif BTF_KFUNCS_START(common_btf_ids) -BTF_ID_FLAGS(func, bpf_cast_to_kern_ctx) -BTF_ID_FLAGS(func, bpf_rdonly_cast) +BTF_ID_FLAGS(func, bpf_cast_to_kern_ctx, KF_FASTCALL) +BTF_ID_FLAGS(func, bpf_rdonly_cast, KF_FASTCALL) BTF_ID_FLAGS(func, bpf_rcu_read_lock) BTF_ID_FLAGS(func, bpf_rcu_read_unlock) BTF_ID_FLAGS(func, bpf_dynptr_slice, KF_RET_NULL) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index f35b80c16cda..80a9c73137b8 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -16199,10 +16199,7 @@ static u32 kfunc_fastcall_clobber_mask(struct bpf_kfunc_call_arg_meta *meta) /* Same as verifier_inlines_helper_call() but for kfuncs, see comment above */ static bool is_fastcall_kfunc_call(struct bpf_kfunc_call_arg_meta *meta) { - if (meta->btf == btf_vmlinux) - return meta->func_id == special_kfunc_list[KF_bpf_cast_to_kern_ctx] || - meta->func_id == special_kfunc_list[KF_bpf_rdonly_cast]; - return false; + return meta->kfunc_flags & KF_FASTCALL; } /* LLVM define a bpf_fastcall function attribute. From patchwork Mon Sep 16 09:17:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13805205 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (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 BC9851494C3 for ; Mon, 16 Sep 2024 09:18:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726478303; cv=none; b=bYQZMcGcW8OiozK/g1TSamEGNydTl0hWXxgZCTFGphkA2Cj/mBj9V889ueePHrvIzvzN5vcLDNsQ7EfNE3UBxlo9jTjxWeVg8Rone7Xd3H2vfVadEWjQhcIftA4E0bxNVpkVSLRK63MFCqKFcqsynUG6/4Y3his8BYYdvvRibNw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726478303; c=relaxed/simple; bh=K80yxk0AV47JDXGFLo8wruR+a8ZyuuRvgzgspHU/G+M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GUDDCADdSzovD80G1//9y8dknsw8zb1mfWMZw+Y8/d3HX1R9DP+raUoP+d84LTFp1DXlH1fMKQK/uZ8bDwEugcUqOI9AoTVSwyD2APqREAEqPq7NIV8AF0K0nseji2v7WKB4pknSQ/55zFiLlJsUvx3xt8XlZ2M39aUgQaLJ2v0= 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=eys1glCE; arc=none smtp.client-ip=209.85.214.180 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="eys1glCE" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-1fc47abc040so32362755ad.0 for ; Mon, 16 Sep 2024 02:18:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726478301; x=1727083101; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=dwdTK5lWXY0gtZJbf5sNwJbD9nyZDZ8gEoQ29EROv9M=; b=eys1glCEq2UIBg6X7yv12zyAhMocXhySZ7TVK00sMAAWayCKDfr3TQ9FuZJyg/rBn/ 5C8y3IdvwOGKxEbbT11Y8argpsJVaxn1ztp4U8CvuHvFeI1ncs27Z4/dyj5SUe0KqUGm LA6/3vlihBMPWQpggzqsgU8DwnNPSufUgysQ/+aX85VMpjhqinoEym+/ZjF9eEiR14RF HGYpefDUFR6RNgCEqqa3nkZfFnB44NXaLvjXQHtEvMKIUc6IkTru4L2GpntBbQjKfThm pgERjBnt8eJMdZbLKochJT2BsQpSYz5HncYkaY+tG+H+ZYl3pmTdVq33ec/I0VYn2oD4 Ci6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726478301; x=1727083101; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dwdTK5lWXY0gtZJbf5sNwJbD9nyZDZ8gEoQ29EROv9M=; b=h00Jn3wnyEiWlZleFLPx3lhA1+IR8NZr3xk7/OkjOPRQ796WZ8qGtxNlljlbwgFLQ6 GjEZvLLyvnEr+9yWe8KeuygkFOqpJHGiVnCNcNmRVdkLgvPMRFQvN0v9aafXYahnfjnD rJz4uXdrFAeg9ERrmeqAV+/7nJIKcua06trTe+2Oi9Jzy1VM6nwUXG5iNMM7xyjzoNGK dvu0kgw/fMe+Xi14omjonoFbMozNmMJ+/u/BeX+qhWfeAmrLbj5HPseVpg4FG/AgWPzu +zyUos43OsCucKsS3ipe7E4NZ3+q29LYaEm81sjKYvTkRO2ivEUeAmS0XwVsihPGa+wJ Wu3w== X-Gm-Message-State: AOJu0YwY7A341XnEj0xRLABt/y8dZI/S7/Dnb7NskJX3KRmxsEX/YSuS AE1dN4G5v+pBA6AJyzcCoXFOlCbdm5206l70akW/N0vkZ3wz1hh3KEgibg== X-Google-Smtp-Source: AGHT+IHTCwta+fra8I1s98kINEFwUjQEJgAs+mJaGsv1CCYCPuvIp/AUBDIkBNU+xwLx81O29yFv8w== X-Received: by 2002:a17:902:ea0b:b0:206:ae0b:bfb6 with SMTP id d9443c01a7336-2076e412914mr199980555ad.40.1726478300455; Mon, 16 Sep 2024 02:18:20 -0700 (PDT) Received: from honey-badger.. ([38.34.87.7]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-207945da63fsm32882195ad.38.2024.09.16.02.18.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Sep 2024 02:18:20 -0700 (PDT) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, kernel-team@fb.com, yonghong.song@linux.dev, arnaldo.melo@gmail.com, Eduard Zingerman Subject: [PATCH bpf-next v1 4/4] bpftool: __bpf_fastcall for kfuncs marked with special decl_tag Date: Mon, 16 Sep 2024 02:17:12 -0700 Message-ID: <20240916091712.2929279-5-eddyz87@gmail.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240916091712.2929279-1-eddyz87@gmail.com> References: <20240916091712.2929279-1-eddyz87@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Generate __attribute__((bpf_fastcall)) for kfuncs marked with "bpf_fastcall" decl tag. E.g. for the following BTF: $ bpftool btf dump file vmlinux ... [A] FUNC 'bpf_rdonly_cast' type_id=... ... [B] DECL_TAG 'bpf_kfunc' type_id=A component_idx=-1 [C] DECL_TAG 'bpf_fastcall' type_id=A component_idx=-1 Generate the following vmlinux.h: #ifndef __VMLINUX_H__ #define __VMLINUX_H__ ... #ifndef __bpf_fastcall #if __has_attribute(bpf_fastcall) #define __bpf_fastcall __attribute__((bpf_fastcall)) #else #define __bpf_fastcall #endif #endif ... __bpf_fastcall extern void *bpf_rdonly_cast(...) ...; The "bpf_fastcall" / "bpf_kfunc" tags pair would generated by pahole when constructing vmlinux BTF. While at it, sort printed kfuncs by name for better vmlinux.h stability. Signed-off-by: Eduard Zingerman --- tools/bpf/bpftool/btf.c | 98 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 88 insertions(+), 10 deletions(-) diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c index 7d2af1ff3c8d..98810b83e976 100644 --- a/tools/bpf/bpftool/btf.c +++ b/tools/bpf/bpftool/btf.c @@ -1,11 +1,15 @@ // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) /* Copyright (C) 2019 Facebook */ +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif #include #include #include #include #include +#include #include #include #include @@ -21,6 +25,7 @@ #include "main.h" #define KFUNC_DECL_TAG "bpf_kfunc" +#define FASTCALL_DECL_TAG "bpf_fastcall" static const char * const btf_kind_str[NR_BTF_KINDS] = { [BTF_KIND_UNKN] = "UNKNOWN", @@ -464,19 +469,59 @@ static int dump_btf_raw(const struct btf *btf, return 0; } +struct ptr_array { + __u32 cnt; + __u32 cap; + const void **elems; +}; + +static int ptr_array_push(const void *ptr, struct ptr_array *arr) +{ + __u32 new_cap; + void *tmp; + + if (arr->cnt == arr->cap) { + new_cap = (arr->cap ?: 16) * 2; + tmp = realloc(arr->elems, sizeof(*arr->elems) * new_cap); + if (!tmp) + return -ENOMEM; + arr->elems = tmp; + arr->cap = new_cap; + } + arr->elems[arr->cnt++] = ptr; + return 0; +} + +static void ptr_array_free(struct ptr_array *arr) +{ + free(arr->elems); +} + +static int cmp_kfuncs(const void *pa, const void *pb, void *ctx) +{ + struct btf *btf = ctx; + const struct btf_type *a = *(void **)pa; + const struct btf_type *b = *(void **)pb; + + return strcmp(btf__str_by_offset(btf, a->name_off), + btf__str_by_offset(btf, b->name_off)); +} + static int dump_btf_kfuncs(struct btf_dump *d, const struct btf *btf) { LIBBPF_OPTS(btf_dump_emit_type_decl_opts, opts); - int cnt = btf__type_cnt(btf); - int i; + __u32 cnt = btf__type_cnt(btf), i, j; + struct ptr_array fastcalls = {}; + struct ptr_array kfuncs = {}; + int err = 0; printf("\n/* BPF kfuncs */\n"); printf("#ifndef BPF_NO_KFUNC_PROTOTYPES\n"); for (i = 1; i < cnt; i++) { const struct btf_type *t = btf__type_by_id(btf, i); + const struct btf_type *ft; const char *name; - int err; if (!btf_is_decl_tag(t)) continue; @@ -484,27 +529,53 @@ static int dump_btf_kfuncs(struct btf_dump *d, const struct btf *btf) if (btf_decl_tag(t)->component_idx != -1) continue; - name = btf__name_by_offset(btf, t->name_off); - if (strncmp(name, KFUNC_DECL_TAG, sizeof(KFUNC_DECL_TAG))) + ft = btf__type_by_id(btf, t->type); + if (!btf_is_func(ft)) continue; - t = btf__type_by_id(btf, t->type); - if (!btf_is_func(t)) - continue; + name = btf__name_by_offset(btf, t->name_off); + if (strncmp(name, KFUNC_DECL_TAG, sizeof(KFUNC_DECL_TAG)) == 0) { + err = ptr_array_push(ft, &kfuncs); + if (err) + goto out; + } + + if (strncmp(name, FASTCALL_DECL_TAG, sizeof(FASTCALL_DECL_TAG)) == 0) { + err = ptr_array_push(ft, &fastcalls); + if (err) + goto out; + } + } + + /* Sort kfuncs by name for improved vmlinux.h stability */ + qsort_r(kfuncs.elems, kfuncs.cnt, sizeof(*kfuncs.elems), cmp_kfuncs, (void *)btf); + for (i = 0; i < kfuncs.cnt; i++) { + const struct btf_type *t = kfuncs.elems[i]; + + /* Assume small amount of fastcall kfuncs */ + for (j = 0; j < fastcalls.cnt; j++) { + if (fastcalls.elems[j] == t) { + printf("__bpf_fastcall "); + break; + } + } printf("extern "); opts.field_name = btf__name_by_offset(btf, t->name_off); err = btf_dump__emit_type_decl(d, t->type, &opts); if (err) - return err; + goto out; printf(" __weak __ksym;\n"); } printf("#endif\n\n"); - return 0; +out: + ptr_array_free(&fastcalls); + ptr_array_free(&kfuncs); + return err; } static void __printf(2, 0) btf_dump_printf(void *ctx, @@ -718,6 +789,13 @@ static int dump_btf_c(const struct btf *btf, printf("#ifndef __weak\n"); printf("#define __weak __attribute__((weak))\n"); printf("#endif\n\n"); + printf("#ifndef __bpf_fastcall\n"); + printf("#if __has_attribute(bpf_fastcall)\n"); + printf("#define __bpf_fastcall __attribute__((bpf_fastcall))\n"); + printf("#else\n"); + printf("#define __bpf_fastcall\n"); + printf("#endif\n"); + printf("#endif\n\n"); if (root_type_cnt) { for (i = 0; i < root_type_cnt; i++) {