Message ID | 20221014202852.2491657-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 y7csp367400wrs; Fri, 14 Oct 2022 13:38:26 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6vIjuCn6JXIgjQQ7XwMXM01j9dDMXp4a3cjvxT2yGz4WSoLyjO8qAHwJi3xCQRK6i15FIJ X-Received: by 2002:a65:6d1b:0:b0:46a:f245:8825 with SMTP id bf27-20020a656d1b000000b0046af2458825mr6067287pgb.224.1665779906129; Fri, 14 Oct 2022 13:38:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665779906; cv=none; d=google.com; s=arc-20160816; b=c1K22D9RLxTx22TXNULo1RgXzNBYzFt+bjxKHkVwG7SQtuU+PtiTYtZo4fsHVdQFqD BKSk0SZ/a4hD0D86dhkAuULUZtt0w9e8YwMPEabQ+Ub4iOr4G+Q0fZ4cdZMy1+VXGMMh DcvB7q58RXYhjCR5DJrBuWYc77FEI7dldrDqpdKnCMHKn+FOStzjhW7MN6uABhEo7wjN nup7qA9r3j424T8bbK22D9KtBZIIVcT6hzRwX3IKZO4G+XSJ5GWZUn8vY0f7u/esqRtr bueBJ+itGGjRwUGb6Nr/xs0Nq6I7ffoa9ZNYUOiOmKc3fxAtuyX46/klMweXnpXR1vq8 r+mQ== 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=PW8mHRQgVN29JVQfa80ZbsvQpYu3G3VerTPmn74QPd8=; b=S66vH7aLOlf8qauXJRxa/R+EuQL3BxGOdFFei9cc4DRR431DvTu76nVq8W1Db83oNr YkBE2ZhTME//TzUMYhM0LqWsy3VLfJijIqYiXRFog1CK1xDylSxfB+pV4UMWB5wHFqvZ 20oh4beV/6UQA1Q90692rZeQwppu6J976jxn/YTH8D9RytTKCIrNtctVpwEdAY6kAZ15 kNTGMrxedu7i7JdHVxh1cVVu4cVvFzb3JCH9BQiq6y2EwZTX1ry17rI4xPhE2izFkLHt tAAbZCpCfeJND0KIsMK3bD8Ii7Un/YRUB1FAsO8N8F0+kTLSgb196CddvZzhfpHXKZrX u4lQ== 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 bj20-20020a17090b089400b0020d4b43d8b5si9062242pjb.103.2022.10.14.13.38.13; Fri, 14 Oct 2022 13:38:26 -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 S231382AbiJNU3Z (ORCPT <rfc822;ouuuleilei@gmail.com> + 99 others); Fri, 14 Oct 2022 16:29:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231350AbiJNU3X (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 14 Oct 2022 16:29:23 -0400 Received: from mail-qk1-f181.google.com (mail-qk1-f181.google.com [209.85.222.181]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 712765D702; Fri, 14 Oct 2022 13:29:22 -0700 (PDT) Received: by mail-qk1-f181.google.com with SMTP id a5so3237016qkl.6; Fri, 14 Oct 2022 13:29:22 -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=PW8mHRQgVN29JVQfa80ZbsvQpYu3G3VerTPmn74QPd8=; b=am7tH3CzjQIU8nlbO6zX5a6dn9UsrO1EhiSpqqcMm4k0HO0x19k4cHQcp3fJwhekwh ul5qF5Zp2/8VgHoBt+FfXrMXl1EvYixZZG6a8R+Pd1eceWSYXqhiINlFq3YhF36qAbxA D1kqvbNAzFoZRBBE23h1Lqds1w/k1t5R+vnte6lg9CWayy390jy9Jra54jRu7sGJUJfD rbOGJccRM2kncRaBntvXgLTZ6mA4PZ6EcRmekCyIOZlEiCqB0ji6N1q5W7HcJRkD4MnQ nMSgnpb2YNMv2bO/zfEG0K5NuWX8aAY4Anxgy07iu6mLt0K0LWhIUH8iYKlalSkXubI5 LWWg== X-Gm-Message-State: ACrzQf0Nuhs6r1E83JcTK/9KDR8Hx8oU1sjUNMn4ARABCwoeKUoW9GOk ycHKPu+dqZ0ZympNo+cvn3yw6cGGuMM6uA== X-Received: by 2002:ae9:c318:0:b0:6ee:245a:a4d8 with SMTP id n24-20020ae9c318000000b006ee245aa4d8mr5180091qkg.346.1665779361325; Fri, 14 Oct 2022 13:29:21 -0700 (PDT) Received: from localhost ([2620:10d:c091:480::6918]) by smtp.gmail.com with ESMTPSA id u8-20020a37ab08000000b006cbc6e1478csm3047832qke.57.2022.10.14.13.29.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Oct 2022 13:29:21 -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 v4 0/3] Support storing struct task_struct objects as kptrs Date: Fri, 14 Oct 2022 15:28:49 -0500 Message-Id: <20221014202852.2491657-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?1746696831122669686?= |
Series |
Support storing struct task_struct objects as kptrs
|
|
Message
David Vernet
Oct. 14, 2022, 8:28 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 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. 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