From patchwork Thu Jul 27 07:36:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuyi Zhou X-Patchwork-Id: 126765 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a985:0:b0:3e4:2afc:c1 with SMTP id t5csp929710vqo; Thu, 27 Jul 2023 00:58:24 -0700 (PDT) X-Google-Smtp-Source: APBJJlEZyGODOLIUNSwo4P2v6J+YTJr7wtv3bafMFCo+5PSp5lxkjz6wMzsE78CCpjfY09dIERva X-Received: by 2002:a17:906:64c6:b0:99b:65fa:fc24 with SMTP id p6-20020a17090664c600b0099b65fafc24mr1425546ejn.36.1690444704588; Thu, 27 Jul 2023 00:58:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690444704; cv=none; d=google.com; s=arc-20160816; b=ZG/K9RRfB++wXMR5wdl0vRl5CBapq70mVKXlzHoiUzH4gS/8EMVzUv7BiQbfjSoC1l IJCJPvFtmilJSuvs9qdy5sRrAr0/WNIUWG/irdafLiw1iWoIwco+2ayMTWGDaPgBMdek 2C5M42wPmPUeBABm3ZZdP5PhlV6fCYaQjHfWmnipXuSeC+6Dp0QTFnOkY3yVPWonRQ5n TLgHg9LtPSha1ldWOU87/e0ALXujwg/rRy5Rm6ksg3IlOqS0PIzeKo1PE1+UkQTYCAvl 1l3TKwsxCZIqu5sAsSJ2OlXc0MNK/+7I3uMHqq2XNhqCZPzxwbIo15+9YZ1ChlK1dl8H mlSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Uv3OYCgolDRnDe8sjWn08GTjavdgRys1P8gpfRFXymU=; fh=hkVHq7oq/x6T47Z/W+8iW7aAYD01qDHXzBTbsOMDe4M=; b=H5dOjZGN4XjES0mTWHSqvoSKacQiQqOfJ4ICWKiFZkvf15JjA9YXbeX9RIvMwM8Pdn lzYfe+qwvQE57mc7KFDQA5rqqm2np9KqeD43o/brM2jpXnjE/f5eWB6cnj3umesV9PHn KPetiAibFqgYDKBVsewlihNdpeDiG+WQQw16w5xRlnlwwfIgg9azuc2r2rn4vMObf6Gi TDi3snAN8/j2bfpm973BTNmp2kwk3WQGQKv27VP2ZHRlADDadJAnAaUkS1ZCRolMX31v xbblDNgFTdBLCwJpAesvVvzt098MLOMrt3jNNnlQ6daii5Ur4yD/h6EXK/ayQKxMx/Kj WNLg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=GPxKU1wL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e19-20020a170906249300b0099bc2f8e22esi627239ejb.599.2023.07.27.00.58.00; Thu, 27 Jul 2023 00:58:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=GPxKU1wL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233437AbjG0Hos (ORCPT + 99 others); Thu, 27 Jul 2023 03:44:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50680 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233650AbjG0HnU (ORCPT ); Thu, 27 Jul 2023 03:43:20 -0400 Received: from mail-oi1-x22e.google.com (mail-oi1-x22e.google.com [IPv6:2607:f8b0:4864:20::22e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ABFBC4C0A for ; Thu, 27 Jul 2023 00:37:29 -0700 (PDT) Received: by mail-oi1-x22e.google.com with SMTP id 5614622812f47-3a38953c928so601645b6e.1 for ; Thu, 27 Jul 2023 00:37:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1690443447; x=1691048247; 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=Uv3OYCgolDRnDe8sjWn08GTjavdgRys1P8gpfRFXymU=; b=GPxKU1wLA/ico6qT1nEEu7gHQfgASi8rquE1AHhcXYJT1YIgQWCl0gYkO+q7Q4Ajd8 ZGhT1ge1OlP7dnRkKC9NzY2sUvhgM/h6rPXqBu3xK1S9r98sHCrdmoTvcTfANYWZBoLd xfVIwZRmn2yWUEfGaYPS4U465p+g49PadC9fcYd0hx5ojquQUhWVeEKTqP+pZ9xvinLc bNKbsys0ZIGHmQCTT0vWBHPadAdI12u2RXt8ygcYMQkFpLj8uIdMWG4xFoQS93HgAPsT PwYp3/fy/qrx1r2C2MV0kCEoJMDJandD7xo85GeTJRyvBJxZslJGlHE1vTxBwPd27657 T+pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690443447; x=1691048247; 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=Uv3OYCgolDRnDe8sjWn08GTjavdgRys1P8gpfRFXymU=; b=JHbQfvkrFEKjH0JaFaOeThVbgsnmlgFoggfq4LsnYZuLXcsBpVWyXCcJpndfk4FeWA m75WXzKZWLLTV2qV2NRaJxchqhcXSy2P2LTWB5fULMlODVG7PzY9rnOK0pZFj1jjg6t+ hnoggyealKlfmm9Q98FFlxo4mdZiOlbUVh+5RmYcx/07HdUzcKhLMLl8GduONm9YjRFB T3oOvKIiu0OGCXOr5jO5Yx2THT7kdClBuzjEyRu0FqipbUlDIO9Ah2Y/LI1sWv4fB8bQ vMdaYA5KYDdjwpoZoTzBu183/0kbwxDBopjeVNpvtd+eUbDHs2/ThTzHa/iZZy/hPFAu OoOw== X-Gm-Message-State: ABy/qLYtVz+cgjOVgqhypcxp+whiDyvH92GkheNsLhAl05pwtPe5cJsN 0OAe980VxKETBWan7HC7kXRuwSBJr2dZzA3CUqAsgQ== X-Received: by 2002:a05:6808:1393:b0:3a5:ca93:fb69 with SMTP id c19-20020a056808139300b003a5ca93fb69mr2618037oiw.55.1690443447035; Thu, 27 Jul 2023 00:37:27 -0700 (PDT) Received: from n37-019-243.byted.org ([180.184.51.134]) by smtp.gmail.com with ESMTPSA id s196-20020a6377cd000000b005638a70110bsm733919pgc.65.2023.07.27.00.37.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Jul 2023 00:37:26 -0700 (PDT) From: Chuyi Zhou To: hannes@cmpxchg.org, mhocko@kernel.org, roman.gushchin@linux.dev, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, wuyun.abel@bytedance.com, robin.lu@bytedance.com, Chuyi Zhou Subject: [RFC PATCH 4/5] bpf: Add a new bpf helper to get cgroup ino Date: Thu, 27 Jul 2023 15:36:31 +0800 Message-Id: <20230727073632.44983-5-zhouchuyi@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20230727073632.44983-1-zhouchuyi@bytedance.com> References: <20230727073632.44983-1-zhouchuyi@bytedance.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772559746475424124 X-GMAIL-MSGID: 1772559746475424124 This patch adds a new bpf helper bpf_get_ino_from_cgroup_id, so that we can get the inode number once we know the cgroup id. Cgroup_id is used to identify a cgroup in BPF prog. However we can't get the cgroup id directly in userspace applications. In userspace, we are used to identifying cgroups by their paths or their inodes. However, cgroup id is not always equal to the inode number, depending on the sizeof ino_t. For example, given some cgroup paths, we only care about the events related to those cgroups. We can only do this by updating these paths in a map and doing string comparison in BPF prog, which is not very convenient. However with this new helper, we just need to record the inode in a map and lookup a inode number in BPF prog. Signed-off-by: Chuyi Zhou --- include/uapi/linux/bpf.h | 7 +++++++ kernel/bpf/core.c | 1 + kernel/bpf/helpers.c | 17 +++++++++++++++++ tools/include/uapi/linux/bpf.h | 7 +++++++ 4 files changed, 32 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 9da0d61cf703..01efb289fa14 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -5575,6 +5575,12 @@ union bpf_attr { * 0 on success. * * **-ENOENT** if the bpf_local_storage cannot be found. + * + * u64 bpf_get_ino_from_cgroup_id(u64 id) + * Description + * Get inode number from a *cgroup id*. + * Return + * Inode number. */ #define ___BPF_FUNC_MAPPER(FN, ctx...) \ FN(unspec, 0, ##ctx) \ @@ -5789,6 +5795,7 @@ union bpf_attr { FN(user_ringbuf_drain, 209, ##ctx) \ FN(cgrp_storage_get, 210, ##ctx) \ FN(cgrp_storage_delete, 211, ##ctx) \ + FN(get_ino_from_cgroup_id, 212, ##ctx) \ /* */ /* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index dc85240a0134..49dfdb2dd336 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -2666,6 +2666,7 @@ const struct bpf_func_proto bpf_snprintf_btf_proto __weak; const struct bpf_func_proto bpf_seq_printf_btf_proto __weak; const struct bpf_func_proto bpf_set_retval_proto __weak; const struct bpf_func_proto bpf_get_retval_proto __weak; +const struct bpf_func_proto bpf_get_ino_from_cgroup_id_proto __weak; const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void) { diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 9e80efa59a5d..e87328b008d3 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -433,6 +433,21 @@ const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto = { .ret_type = RET_INTEGER, .arg1_type = ARG_ANYTHING, }; + +BPF_CALL_1(bpf_get_ino_from_cgroup_id, u64, id) +{ + u64 ino = kernfs_id_ino(id); + + return ino; +} + +const struct bpf_func_proto bpf_get_ino_from_cgroup_id_proto = { + .func = bpf_get_ino_from_cgroup_id, + .gpl_only = false, + .ret_type = RET_INTEGER, + .arg1_type = ARG_ANYTHING, +}; + #endif /* CONFIG_CGROUPS */ #define BPF_STRTOX_BASE_MASK 0x1F @@ -1767,6 +1782,8 @@ bpf_base_func_proto(enum bpf_func_id func_id) return &bpf_get_current_cgroup_id_proto; case BPF_FUNC_get_current_ancestor_cgroup_id: return &bpf_get_current_ancestor_cgroup_id_proto; + case BPF_FUNC_get_ino_from_cgroup_id: + return &bpf_get_ino_from_cgroup_id_proto; #endif default: break; diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 9da0d61cf703..661d97aacb85 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -5575,6 +5575,12 @@ union bpf_attr { * 0 on success. * * **-ENOENT** if the bpf_local_storage cannot be found. + * + * u64 bpf_get_ino_from_cgroup_id(u64 id) + * Description + * Get inode number from a *cgroup id*. + * Return + * Inode number. */ #define ___BPF_FUNC_MAPPER(FN, ctx...) \ FN(unspec, 0, ##ctx) \ @@ -5789,6 +5795,7 @@ union bpf_attr { FN(user_ringbuf_drain, 209, ##ctx) \ FN(cgrp_storage_get, 210, ##ctx) \ FN(cgrp_storage_delete, 211, ##ctx) \ + FN(get_ino_from_cgroup_id, 212, ##ctx) \ /* */ /* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't