Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp1553203rwl; Fri, 31 Mar 2023 12:59:52 -0700 (PDT) X-Google-Smtp-Source: AKy350at7h21IivAGYTN0RCH5vD9Q7dOtF0v2HJN1Q7WRNjJXAUVNcKFHb86xSVol4cRZIAQZS/A X-Received: by 2002:a05:6a20:8e19:b0:dd:dfe4:f06a with SMTP id y25-20020a056a208e1900b000dddfe4f06amr7776984pzj.3.1680292791720; Fri, 31 Mar 2023 12:59:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680292791; cv=none; d=google.com; s=arc-20160816; b=f4h1pm9Rsymj6tO9X4GHueEmcczHTFHrIyMeOwscf6nH50tAudMCdk0DHdSfl0gUr7 faTxMB8d7D701fXnprcZykskwUxbwVCxKDE9aOyX3DWvVg4btPHYQMFCzmFZUC9+fDQP uC9TMIyV8q0z0A/4nEZT38u3CF9NwlRj7fy28xR9U3+Ejm5TmHzWwDVr9H+s3TEmXZbm qIMZ5NO9Szlezrw2bDNMeClTlPnRlnm2an97OF5OwTnGU+W5fPopjkZSYVBGQY+JqeqF ryEZAcUV8JjwWmIe1bE7jBixLYNxZd9IByUK1IGjwaJsgmiyyUh4nV6sKrEI6lrJwglQ yOqg== 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; bh=a2HAELiYxMwO28bC3+J9qfIMUXe3QPmftJ2DsQn4LZo=; b=pUzSTq36a+N72seNoMys24tYmWqXgRvmbX86/quJn842fX2zcZ3iBELaWb8oaXw8ZV m8+SeBBYE2+a3VSpEW5UYwH0gl0w3ut4+7PRCynefmVkd9torOONTvblky68b4S7xLiX YU8VY3K9r0Jqpg/LsfYDXln1op8Q5j9U/InxQxLtF0bwHu4t0A1VrTVQGawTRmJYylya PWY96jBl2KQS4ArjBq939wKO1+9toHr78BOI/weJkkYMaQ9BAmxcNzz6qwhWekO6KtUI M76s+SPPKf4TfnFJvMQ044drLg/lzy/JKvGn7n+gRsV7sV3aCW5rdIxB6kWtmfjHYIRd nHfw== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f26-20020aa79d9a000000b00627e9ad5918si3079739pfq.193.2023.03.31.12.59.40; Fri, 31 Mar 2023 12:59:51 -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 S231159AbjCaT5u (ORCPT + 99 others); Fri, 31 Mar 2023 15:57:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42308 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232270AbjCaT5n (ORCPT ); Fri, 31 Mar 2023 15:57:43 -0400 Received: from mail-qv1-f51.google.com (mail-qv1-f51.google.com [209.85.219.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 30AEC1FD1D; Fri, 31 Mar 2023 12:57:42 -0700 (PDT) Received: by mail-qv1-f51.google.com with SMTP id on15so3931690qvb.7; Fri, 31 Mar 2023 12:57:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680292661; x=1682884661; 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=a2HAELiYxMwO28bC3+J9qfIMUXe3QPmftJ2DsQn4LZo=; b=PQ7lVnPGcbNzZg0/6kU45CcmI6Aejhiaoej7UykV11sRDjOLuvrM9EC75OJPk/joyM pYdVGHWdhMheMhXsrlEUuVYWvJ8V4qLweDS3z6LeZEMikUDSe0quDrhNa+vQugeCK1g9 B95RqPPs22aFke5EEFPSOm/ucekTY3p7jYf4OAE1ubbObpINVgZZQ3/KmDcf7F92y/4A VxQZgaKkEHN61koT1KKwb2ce9ou4AIWqLjO/FPYY3SfJmkfjkthC9+h5Ny97uwWBf4k7 F5KL1VVYUyS/6bovP7kfiQHtWlth4ID9+VQdqhICSZ4Zn6f2oI4ZGtLDhHt4FaQUBXjw DJXQ== X-Gm-Message-State: AAQBX9fvpK9nkevXqMnYmnZutfVw/AYJJz5j9UXOPqXECibScSQo42Eo P7eu6mKhsVdHVofRpNQsN7NKu/8TBOquRnTS X-Received: by 2002:a05:6214:29e8:b0:5a3:79cd:8ef8 with SMTP id jv8-20020a05621429e800b005a379cd8ef8mr43581327qvb.32.1680292661266; Fri, 31 Mar 2023 12:57:41 -0700 (PDT) Received: from localhost ([2620:10d:c091:400::5:2101]) by smtp.gmail.com with ESMTPSA id mk14-20020a056214580e00b005dd8b9345e8sm806212qvb.128.2023.03.31.12.57.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Mar 2023 12:57:40 -0700 (PDT) From: David Vernet 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@meta.com Subject: [PATCH bpf-next v2 3/3] bpf,docs: Update documentation to reflect new task kfuncs Date: Fri, 31 Mar 2023 14:57:33 -0500 Message-Id: <20230331195733.699708-4-void@manifault.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230331195733.699708-1-void@manifault.com> References: <20230331195733.699708-1-void@manifault.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=0.5 required=5.0 tests=FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS 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: X-Mailing-List: linux-kernel@vger.kernel.org Now that struct task_struct objects are RCU safe, and bpf_task_acquire() can return NULL, we should update the BPF task kfunc documentation to reflect the current state of the API. Signed-off-by: David Vernet --- Documentation/bpf/kfuncs.rst | 49 +++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/Documentation/bpf/kfuncs.rst b/Documentation/bpf/kfuncs.rst index bf1b85941452..d8a16c4bef7f 100644 --- a/Documentation/bpf/kfuncs.rst +++ b/Documentation/bpf/kfuncs.rst @@ -471,13 +471,50 @@ struct_ops callback arg. For example: struct task_struct *acquired; acquired = bpf_task_acquire(task); + if (acquired) + /* + * In a typical program you'd do something like store + * the task in a map, and the map will automatically + * release it later. Here, we release it manually. + */ + bpf_task_release(acquired); + return 0; + } + + +References acquired on ``struct task_struct *`` objects are RCU protected. +Therefore, when in an RCU read region, you can obtain a pointer to a task +embedded in a map value without having to acquire a reference: + +.. code-block:: c + + #define private(name) SEC(".data." #name) __hidden __attribute__((aligned(8))) + private(TASK) static struct task_struct *global; + + /** + * A trivial example showing how to access a task stored + * in a map using RCU. + */ + SEC("tp_btf/task_newtask") + int BPF_PROG(task_rcu_read_example, struct task_struct *task, u64 clone_flags) + { + struct task_struct *local_copy; + + bpf_rcu_read_lock(); + local_copy = global; + if (local_copy) + /* + * We could also pass local_copy to kfuncs or helper functions here, + * as we're guaranteed that local_copy will be valid until we exit + * the RCU read region below. + */ + bpf_printk("Global task %s is valid", local_copy->comm); + else + bpf_printk("No global task found"); + bpf_rcu_read_unlock(); + + /* At this point we can no longer reference local_copy. */ - /* - * In a typical program you'd do something like store - * the task in a map, and the map will automatically - * release it later. Here, we release it manually. - */ - bpf_task_release(acquired); return 0; } -- 2.39.0