Message ID | 20221014212133.2520531-1-void@manifault.com |
---|---|
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp382968wrs; Fri, 14 Oct 2022 14:26:07 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4w2qgHFyWw4m6dPo37EJWoLZFROb6kF41bE/2T7VvP0U0T0v9psvxxKQ38//gKhDm3VnjQ X-Received: by 2002:a17:907:75f1:b0:78a:f935:647d with SMTP id jz17-20020a17090775f100b0078af935647dmr4904042ejc.587.1665782767588; Fri, 14 Oct 2022 14:26:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665782767; cv=none; d=google.com; s=arc-20160816; b=QnWlpP2IPwMdFloVvDJFiD/WKCm/XEyisQypVJbtBMMWDYe1UM635ARBrsx4Plx8BL C1Klbdbg61OBQnJ+ukbSHH1YTr0UcQNp6XG1eXO6lWmV9JyJQQZlSMDGsMXT4Ov64H8z PVf0964WeEFTHr2NaJYoJxbzFQhN/RaCvZBN/EXFNNGdAjoUjVaUgPlQAuu1LW/mcM/M w8Tr8rYuiCpFWuKIuFsMCojMQfSyMILRXW7ko7tOrCga0bi4NMOyA1AmG7Dnf928EKrY 7dtWwY6ybHVALF1BjO84vJSuTqnp5HaS1UE068V0dt2oVWFy3UT2kbkm4fG/ZuCevydP 2U2A== 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 :message-id:date:subject:cc:to:from; bh=nF3cQdAKhaqUM7UI3J6yW3zKANsJ1fED/aKUWKtLaEA=; b=Y7jghxxMOgOU6pne4xtjihyduHYJ12cY809dOO9D6vF4xn5dGaYU337FCYM9zzaugh W07uNkoLldA49S5dFJV4SnjRcRVKSO11GyylSHRVLqgjxJYe2PDmXQTzPfy9eRoTO/16 Otrg6R/7tyMAnDHrzGKRF4OvHoPXgO1AeYkJRX2/yg5FEEmnsZFr3yvSfNY6mtGcJTnz QyZ0h0zZtNZFkA5NpDqSMqHOdVNzbZMoL34WszUVpIUZeSN40hmAPWIa4j/v8hj9uzV9 UvpVMMRrkkLLTJvKT0KH2KTqhrtngVBsXX5QpJBhEsZGpizHay8RtLQAS+CmGSIymb9N gvLQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hz3-20020a1709072ce300b0078db371355esi1101606ejc.987.2022.10.14.14.25.42; Fri, 14 Oct 2022 14:26:07 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229833AbiJNVVl (ORCPT <rfc822;ouuuleilei@gmail.com> + 99 others); Fri, 14 Oct 2022 17:21:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229818AbiJNVVk (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 14 Oct 2022 17:21:40 -0400 Received: from mail-qk1-f179.google.com (mail-qk1-f179.google.com [209.85.222.179]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 122051D3479; Fri, 14 Oct 2022 14:21:37 -0700 (PDT) Received: by mail-qk1-f179.google.com with SMTP id 8so3330629qka.1; Fri, 14 Oct 2022 14:21:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=nF3cQdAKhaqUM7UI3J6yW3zKANsJ1fED/aKUWKtLaEA=; b=fWxTiN9egTSdK3IQ3/pGP0x8S+gJa0PZhWq2sMdaYQkgrbeAkaGwRfFVFovUS+F8Gf S07klcnbi3jzssNl1+FqZs774J9O0Hhp6CjVtLYCqFmrnMoMoZawvlq1/Sq0IrjoyJfM krR7NL1DQ5agUlXYesKXDJQgTHxsKKceHvd07sSkOxA3wdpAhzu6nfnboVHmVCo30Fr9 v0127RFoUtBoPpzPpYlJ8lMBfgHpv4EDnU0wh0qo9aAm+MAowumh0QDpVC83Z0RPSe4r 2ZK212eh4Tqp2dzgx5Su0X+6inRE/WeaPP1gGPiiAo0TXipsD7BvJY0wq6Smk+84/OEY p8dA== X-Gm-Message-State: ACrzQf2IBHQQIiZFw5HrwTPYiodjk/nGvzko+sb/2CxbNSZVcM+/PgBG wAwOoTCPoYplkzDGSJ13u2FPh1PEh5dx/w== X-Received: by 2002:a05:620a:2887:b0:6cf:971e:104d with SMTP id j7-20020a05620a288700b006cf971e104dmr5074746qkp.441.1665782496608; Fri, 14 Oct 2022 14:21:36 -0700 (PDT) Received: from localhost ([2620:10d:c091:480::6918]) by smtp.gmail.com with ESMTPSA id o16-20020a05620a2a1000b006e6a7c2a269sm3454005qkp.22.2022.10.14.14.21.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Oct 2022 14:21:36 -0700 (PDT) From: David Vernet <void@manifault.com> To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, linux-kernel@vger.kernel.org, kernel-team@fb.com, tj@kernel.org, memxor@gmail.com Subject: [PATCH v5 0/3] Support storing struct task_struct objects as kptrs Date: Fri, 14 Oct 2022 16:21:30 -0500 Message-Id: <20221014212133.2520531-1-void@manifault.com> X-Mailer: git-send-email 2.38.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, TVD_PH_BODY_ACCOUNTS_PRE autolearn=no 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1746695976660614872?= X-GMAIL-MSGID: =?utf-8?q?1746699830986824773?= |
Series |
Support storing struct task_struct objects as kptrs
|
|
Message
David Vernet
Oct. 14, 2022, 9:21 p.m. UTC
Now that BPF supports adding new kernel functions with kfuncs, and storing kernel objects in maps with kptrs, we can add a set of kfuncs which allow struct task_struct objects to be stored in maps as referenced kptrs. The possible use cases for doing this are plentiful. During tracing, for example, it would be useful to be able to collect some tasks that performed a certain operation, and then periodically summarize who they are, which cgroup they're in, how much CPU time they've utilized, etc. Doing this now would require storing the task's pids along with some relevant data to be exported to user space, and later associating the pids to tasks in other event handlers where the data is recorded. Another useful by-product of this is that it allows a program to pin a task in a BPF program, and by proxy therefore also e.g. pin its task local storage. In order to support this, we'll need to expand KF_TRUSTED_ARGS to support receiving trusted, non-refcounted pointers. It currently only supports either PTR_TO_CTX pointers, or refcounted pointers . What this means in terms of implementation is that btf_check_func_arg_match() would have to add another condition to its logic for checking if a ptr needs a refcount to also require that the pointer has at least one type modifier, such as PTR_UNTRUSTED. PTR_UNTRUSTED does not cover all of the possible pointers we need to watch out for, though. For example, a pointer obtained from walking a struct is considered "trusted" (or at least, not PTR_UNTRUSTED). To account for this and enable us to expand KF_TRUSTED_ARGS, this patch set also introduces a new PTR_NESTED type flag modifier which records if a pointer was obtained from walking a struct. This patch set adds this new PTR_NESTED type flag, expands KF_TRUSTED_ARGS accordingly, adds the new set of kfuncs mentioned above, and then finally adds a new selftest suite to validate all of this new behavior. David Vernet (3): bpf: Allow trusted pointers to be passed to KF_TRUSTED_ARGS kfuncs bpf: Add kfuncs for storing struct task_struct * as a kptr bpf/selftests: Add selftests for new task kfuncs include/linux/bpf.h | 6 + kernel/bpf/btf.c | 11 +- kernel/bpf/helpers.c | 86 ++++- kernel/bpf/verifier.c | 12 +- tools/testing/selftests/bpf/DENYLIST.s390x | 1 + .../selftests/bpf/prog_tests/task_kfunc.c | 160 +++++++++ .../selftests/bpf/progs/task_kfunc_common.h | 83 +++++ .../selftests/bpf/progs/task_kfunc_failure.c | 315 ++++++++++++++++++ .../selftests/bpf/progs/task_kfunc_success.c | 132 ++++++++ tools/testing/selftests/bpf/verifier/calls.c | 4 +- 10 files changed, 801 insertions(+), 9 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/task_kfunc.c create mode 100644 tools/testing/selftests/bpf/progs/task_kfunc_common.h create mode 100644 tools/testing/selftests/bpf/progs/task_kfunc_failure.c create mode 100644 tools/testing/selftests/bpf/progs/task_kfunc_success.c