From patchwork Mon Jan 23 23:22:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Vernet X-Patchwork-Id: 47469 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1871650wrn; Mon, 23 Jan 2023 15:41:17 -0800 (PST) X-Google-Smtp-Source: AMrXdXvceXCdiEAzle846cqbWQRe9vH1MMJC5hg9FEXQlqGQXEWzZjDh4Aihff09ml6rsGkB6Xlb X-Received: by 2002:a17:902:e5c9:b0:194:d0ad:53f0 with SMTP id u9-20020a170902e5c900b00194d0ad53f0mr22864135plf.9.1674517277303; Mon, 23 Jan 2023 15:41:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674517277; cv=none; d=google.com; s=arc-20160816; b=ejCHZiuGDmMJxBur36RAFBzvRHt/TD8EzOZPEsO4e3CKGG/fkD/BA2+pZUD2SZBZ8c mALMGOzfskUEatAQqjD9evlVmLLeM79bag4nbmnEbBLjoSN3H/bYOtBzM8VkPh/pwAU3 /MROz1UOjox/zeAiSmEsgCeuM9oNLmiUuhd1vr5iCISqiROGY13s+8Y+9ww7xz3XWKxA r3HfVqkemPSRMohvzJo/cM7On6szlI0m0PgQ0ISCp+79PAH+EPCleLxnh2bPHWqg3Ynx i5DBClnUnz/wbOGegyVtWEljd6BA+bfEQ5FzrTrkP79FpjNIYAMn1bJngUkymFi748FR NyhQ== 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=fb0Dqesrjqwtr/7go0R3yUjkHNdnkEwFdWq9cAVf5Uk=; b=d03VYdCeNXNtZ1FGOtFRV+YAEnMncsPfeiC8Fx4R1otvISUYbi0TiAiL8+QZPbzkQH L2EqWdf/RtYEnNejQNrVg+OriMNqjVZC2/Y5wuTiHftbjsmMV31/GxI1S2vkf/2g9m+b BClQHV97cbdrq/0NH+zBMXWgAPqqKUuMkkvDy1B70//ct2fk3cht7HuJaiYZGKAnP9Pz jyJHpQszqf+gD6LOS5K4/72mgTqXyx/AiTKAvKiOIMUwERC5HYQbSfL4+a33B1ncdQam sbbuUhf/OJvqGz1bXbNB0aCSCFEExdQ4HCxagLoAjcR4lM828m9UjqwqjvQnC5/1mNJk Ri3Q== 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 m21-20020a170902d19500b001898141f0edsi647522plb.159.2023.01.23.15.41.04; Mon, 23 Jan 2023 15:41:17 -0800 (PST) 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 S231822AbjAWXWj (ORCPT + 99 others); Mon, 23 Jan 2023 18:22:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48516 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231788AbjAWXWi (ORCPT ); Mon, 23 Jan 2023 18:22:38 -0500 Received: from mail-qv1-f41.google.com (mail-qv1-f41.google.com [209.85.219.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D27917CF7; Mon, 23 Jan 2023 15:22:37 -0800 (PST) Received: by mail-qv1-f41.google.com with SMTP id g10so10453138qvo.6; Mon, 23 Jan 2023 15:22:37 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=fb0Dqesrjqwtr/7go0R3yUjkHNdnkEwFdWq9cAVf5Uk=; b=DM68hg6h1fvo+mCt78/6PKBa0zRsAhHJzN1cbxTnrVDUoP838v7PDiUwJurObGK/+/ qt71BfyO0kyUYQeViphQKllao3f+B1aOy/ScOCsCXwbsVZieUffI0fPC5WOnTw3y0DB/ ANS8dLfMq5OtoAmAKJKDCWlsNqb3CMUBAbdBVa253xSm7bpSS5EEnKoxMcJtJsP2VlJc 6pkqWCLbBwstmqMOL44M/6g4a3IsQOmHHVyHIHuLBBqgWP+I/Wo3fLjcrk5+Mf8eK22z /sfvTvPLU8y/08B+Sq9bzoanyy9kcBlzcWdE4+DBte58A9bqgfhJg7S9SkzhjGrqyWOI VSHg== X-Gm-Message-State: AFqh2koS9GOChDbex0AHmoBHPP/YAutXl0cj6dCjAjJ0wfvPIwRzXZih qYLz+o5w8fsPD3V38rKekyMiLWlGmA0cYS53 X-Received: by 2002:ad4:4d4d:0:b0:532:1d4b:b93c with SMTP id m13-20020ad44d4d000000b005321d4bb93cmr61073908qvm.31.1674516155757; Mon, 23 Jan 2023 15:22:35 -0800 (PST) Received: from localhost ([2620:10d:c091:480::1:93a0]) by smtp.gmail.com with ESMTPSA id j10-20020a05620a0a4a00b00706a452c074sm342086qka.104.2023.01.23.15.22.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Jan 2023 15:22:35 -0800 (PST) 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@meta.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, tj@kernel.org, memxor@gmail.com Subject: [PATCH bpf-next 1/3] bpf: Allow BPF_PROG_TYPE_STRUCT_OPS programs to be sleepable Date: Mon, 23 Jan 2023 17:22:26 -0600 Message-Id: <20230123232228.646563-2-void@manifault.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230123232228.646563-1-void@manifault.com> References: <20230123232228.646563-1-void@manifault.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.5 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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755858628444499618?= X-GMAIL-MSGID: =?utf-8?q?1755858628444499618?= BPF struct_ops programs currently cannot be marked as sleepable. This need not be the case -- struct_ops programs could be sleepable, and e.g. invoke kfuncs that export the KF_SLEEPABLE flag. So as to allow future struct_ops programs to invoke such kfuncs, this patch updates the verifier to allow struct_ops programs to be sleepable. A follow-on patch will update the dummy_st_ops selftest suite to also validate sleepable struct_ops behavior. Signed-off-by: David Vernet --- kernel/bpf/verifier.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index ecf7fed7881c..6b8dcd542950 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -17030,7 +17030,8 @@ static bool can_be_sleepable(struct bpf_prog *prog) } } return prog->type == BPF_PROG_TYPE_LSM || - prog->type == BPF_PROG_TYPE_KPROBE; /* only for uprobes */ + prog->type == BPF_PROG_TYPE_KPROBE /* only for uprobes */ || + prog->type == BPF_PROG_TYPE_STRUCT_OPS; } static int check_attach_btf_id(struct bpf_verifier_env *env) @@ -17052,7 +17053,7 @@ static int check_attach_btf_id(struct bpf_verifier_env *env) } if (prog->aux->sleepable && !can_be_sleepable(prog)) { - verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter and uprobe programs can be sleepable\n"); + verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, and struct_ops programs can be sleepable\n"); return -EINVAL; } From patchwork Mon Jan 23 23:22:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Vernet X-Patchwork-Id: 47468 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1871627wrn; Mon, 23 Jan 2023 15:41:13 -0800 (PST) X-Google-Smtp-Source: AK7set+zttXAHA40zKhbxvSGJRwlaM1kOiDE3+5oApQSIecRJA/MJWrQ2Fgb/d7tDLPp2tuq7YUT X-Received: by 2002:a17:903:2344:b0:196:11a8:ebb5 with SMTP id c4-20020a170903234400b0019611a8ebb5mr1091320plh.2.1674517272794; Mon, 23 Jan 2023 15:41:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674517272; cv=none; d=google.com; s=arc-20160816; b=E7ooO0j3tUj5JctA7LZdm8/t5LOBCgIJfAqC/vemVi1eBLnigKM1ycx3tt0kiOnZmH IO9onnTiLnZ4lq6+WLeCuwqN88o7R9mZ0dlZ6/IpgW/cKrql6saRjELTpxEFAgqw7ws7 E8fBDXxyresnIeN3yiE6oFWBr/90/XlrQpDutfLMeXhkqo6qk3yw1E8xE48HE/TQm/bj lFJ4n2fis77ESH5a1D2fTTlfCb/xIkYpLy2vuRVZLKY7bXetkBBCLghri9/LqUMInq6N CrQJQGkrb+RDLTfBMXcTHsc2ln9u0SC2ODsTUokgSNys4KiJQ6wsvU/YkLLfH6gfyHJ3 Ri5A== 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=m39MU2Z/G62daCdjUzCBvxJkprRH1i3gxUN8pCdsiIk=; b=NPw9nAL03r1Gu++epi8p1bX7Z8qB1zxJbxHHcobgv8GVkoCD83Iu5hGZ8SuLyJD9uL rkRClZy92Uy3H40w5/Z1bjW9AugIS3ZvrG+eF6rTtgDXYfHVpBvJ/xEl0yAPWHM3WPVt exjMtu0hKvZmfH7eXT43W9I37IX5MpsfGoXiXD+RYK7AZNKrCaq5xR8d1M4zsTlFZ9tR a+G9OvysH7CvexmSB0SjBXfDhQjC3y3QhoKezubSezK4kcde0Lr1TK7Lfl89Tw3hYx8p LnywM7x5XigGDjsrsa+1tHFB2vEVlUAnDuCFkXTKCxj/1UJU8W/uZBQXhZqzRN3Ab9d4 eZ3w== 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 l12-20020a170903120c00b00186b1bb14a0si704810plh.559.2023.01.23.15.41.00; Mon, 23 Jan 2023 15:41:12 -0800 (PST) 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 S232101AbjAWXWo (ORCPT + 99 others); Mon, 23 Jan 2023 18:22:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48582 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231970AbjAWXWk (ORCPT ); Mon, 23 Jan 2023 18:22:40 -0500 Received: from mail-qt1-f179.google.com (mail-qt1-f179.google.com [209.85.160.179]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3AAA517CF7; Mon, 23 Jan 2023 15:22:39 -0800 (PST) Received: by mail-qt1-f179.google.com with SMTP id o5so11718330qtr.11; Mon, 23 Jan 2023 15:22:39 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=m39MU2Z/G62daCdjUzCBvxJkprRH1i3gxUN8pCdsiIk=; b=P0thrspY03Ru6G4iI8LWMinFTDRlfAD7JglDVSCqO4f7TK+KgK2zeuHSUmggHhmXgm 8u9oJMCvSb8uEOfzKdpazJQTo5qjyW7ZFDk517D3EWbggBhRk9T/uUDVoZTU2sc4Vtzv x4EPwU1PEcpadT8ZvK47ug4YbSiX5EXw3pCOfmu5VZ4nZ/jF+7YQUM4+V4cLd2acsMjF BYjgckt1aLV/q23ylf+00yAmrc5DBQwDPz/jwQGwTAD0Ey++ffHWZYpYqiGKGamUWWfG ggQkaJoT5u0LgjpW3tx3k2/lX9S0NtDQxJCZhcg7eyE3+k6rzXSl18BBMQ8F0JgHE5c+ tr4Q== X-Gm-Message-State: AFqh2kqcdEdiOtvOewexKzb4aAULVqJIca72ZivMHWNSiQqQkMdMXuKS XDIn6JUgrHONWMi8vxAwqs48dpShP2aj9g/H X-Received: by 2002:ac8:5490:0:b0:3a7:242:4f3 with SMTP id h16-20020ac85490000000b003a7024204f3mr37770983qtq.66.1674516157823; Mon, 23 Jan 2023 15:22:37 -0800 (PST) Received: from localhost ([2620:10d:c091:480::1:93a0]) by smtp.gmail.com with ESMTPSA id e16-20020ac84910000000b003972790deb9sm151439qtq.84.2023.01.23.15.22.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Jan 2023 15:22:37 -0800 (PST) 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@meta.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, tj@kernel.org, memxor@gmail.com Subject: [PATCH bpf-next 2/3] bpf: Pass const struct bpf_prog * to .check_member Date: Mon, 23 Jan 2023 17:22:27 -0600 Message-Id: <20230123232228.646563-3-void@manifault.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230123232228.646563-1-void@manifault.com> References: <20230123232228.646563-1-void@manifault.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755858623912427984?= X-GMAIL-MSGID: =?utf-8?q?1755858623912427984?= The .check_member field of struct bpf_struct_ops is currently passed the member's btf_type via const struct btf_type *t, and a const struct btf_member *member. This allows the struct_ops implementation to check whether e.g. an ops is supported, but it would be useful to also allow it to enforce that the struct_ops prog being loaded for that member has other properties, like being sleepable (or not). This patch therefore updates the .check_member() callback to also take a const struct bpf_prog *prog argument. Signed-off-by: David Vernet --- include/linux/bpf.h | 3 ++- kernel/bpf/verifier.c | 2 +- net/ipv4/bpf_tcp_ca.c | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index ae7771c7d750..b30739634947 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1421,7 +1421,8 @@ struct bpf_struct_ops { const struct bpf_verifier_ops *verifier_ops; int (*init)(struct btf *btf); int (*check_member)(const struct btf_type *t, - const struct btf_member *member); + const struct btf_member *member, + const struct bpf_prog *prog); int (*init_member)(const struct btf_type *t, const struct btf_member *member, void *kdata, const void *udata); diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 6b8dcd542950..eaa0b9acb555 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -16714,7 +16714,7 @@ static int check_struct_ops_btf_id(struct bpf_verifier_env *env) } if (st_ops->check_member) { - int err = st_ops->check_member(t, member); + int err = st_ops->check_member(t, member, prog); if (err) { verbose(env, "attach to unsupported member %s of struct %s\n", diff --git a/net/ipv4/bpf_tcp_ca.c b/net/ipv4/bpf_tcp_ca.c index 4517d2bd186a..13fc0c185cd9 100644 --- a/net/ipv4/bpf_tcp_ca.c +++ b/net/ipv4/bpf_tcp_ca.c @@ -248,7 +248,8 @@ static int bpf_tcp_ca_init_member(const struct btf_type *t, } static int bpf_tcp_ca_check_member(const struct btf_type *t, - const struct btf_member *member) + const struct btf_member *member, + const struct bpf_prog *prog) { if (is_unsupported(__btf_member_bit_offset(t, member) / 8)) return -ENOTSUPP; From patchwork Mon Jan 23 23:22:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Vernet X-Patchwork-Id: 47470 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1873008wrn; Mon, 23 Jan 2023 15:45:30 -0800 (PST) X-Google-Smtp-Source: AK7set/sSj/udh8Xq2DY2ckjKK+gRAjnilM0u2xEbtqVyAlNOqMULg5gsejaQsxPZNPoGplL0N4A X-Received: by 2002:a17:902:ea02:b0:196:e68:2e1d with SMTP id s2-20020a170902ea0200b001960e682e1dmr2169130plg.59.1674517530487; Mon, 23 Jan 2023 15:45:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674517530; cv=none; d=google.com; s=arc-20160816; b=eJiWbKj4QZB//Yexbsp2qHJ2rzpn3V8354/cgkjoFRmwno8GcG/CPZ8O3XEurlQscc mqqK1GcGs0YRWOxo5yoGQ/znETC++tgJiII8hl6g2rzpAK568LhtdXOjo0xIWvw2XZoM 2Nnw0uJiP13Szql1rRbzbjhZWVj7NEhMSqdvoEVJf8/7aVIZB9S3E2uCuT7xpKZduKZR WbY8D3OtcgU/3HSPz97WVKaIKoSmBUX/XRw4aI+CHngzuQ1Lkpsk50KuWO6Pc6bCIyVW LJw+Ns5HyBOSPotFc+yfWIkTTBt6+gQXUuHZLpiz3LQwzaJxNWcl4mclvZRmjJDV/TQF GCNw== 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=6XmmT9Nohm3bkrZY1xyNULiy3EwUIG1ep2ItPBLBkAw=; b=RrxBcD7uWTjvX8Z1SK1Bo8dk1tKzAek8bRj46l1qQ+OWDG6xVBSbbziB5l6fy/DB0j y9972y3Pdzd2DDFoygXZ+7HQJiU7icogQQz9volChCbBYRumQFbnyuMg4Ztt4BZPTdCf AkAXuwQa+W/J+cTk8Bc6eT3sRNzHEzBHDZZ5Un7X6iFpk0o+nCIMIam95q/x4Taz+JAB IjepVJVGOmJzIZaovvRhuuS5oMBOxnpsl8En1sfOIPsg4GJufCXL2ZPf35LMXYduWiYm qgRk9AFnsc89MaEFCuWfoxbx7B5MZ47RfDDKeXS4yEnh40/XjQUppbPxa3poJFTpDktc ighA== 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 q5-20020a170902bd8500b00186abd0c784si722284pls.217.2023.01.23.15.45.18; Mon, 23 Jan 2023 15:45:30 -0800 (PST) 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 S232204AbjAWXWr (ORCPT + 99 others); Mon, 23 Jan 2023 18:22:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48814 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232086AbjAWXWn (ORCPT ); Mon, 23 Jan 2023 18:22:43 -0500 Received: from mail-qt1-f182.google.com (mail-qt1-f182.google.com [209.85.160.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5D29338647; Mon, 23 Jan 2023 15:22:41 -0800 (PST) Received: by mail-qt1-f182.google.com with SMTP id m26so2045075qtp.9; Mon, 23 Jan 2023 15:22:41 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=6XmmT9Nohm3bkrZY1xyNULiy3EwUIG1ep2ItPBLBkAw=; b=7OBFj2kKlYp6egM4Q6DpauYoYIlA8Lec+eZTczCpfMCduLzLmKYpAARd4Kp8m39IlO 6kpHqcwD+THouhX/bxqSgxZfpLgVs5yUOBoriLuHfhD8p6WR15upL5DxdyhDaabGo7WW qs2EjCLWmBHypwjgsKG1x3ULA2bnMMQDQFqH6ThOrNlqfR6aeHDeDagp62TT7vcuZH47 2dgzcHkIwVgNcZQmK65jvkKur030SuvbNwwY9Q7bvIQ8iFg5PabDbfLgEiwyrvHAaP27 9Roxg2tUQE7AFMAT9G3jE4WV7un2kQuE5NCcYmEPVNCZyIz2WNbnldHKC9hqweDbROiv +/kA== X-Gm-Message-State: AFqh2kr1LnuzmaLShK7XgmC9wRLJ7+lJ3IuXQy3Pm+sMRwc/Xo4LcA0C eKxkmJapz3FWJ7CqyNJC911Y/skNcWnm+32I X-Received: by 2002:ac8:46c4:0:b0:3b6:980e:87e7 with SMTP id h4-20020ac846c4000000b003b6980e87e7mr20935182qto.24.1674516160063; Mon, 23 Jan 2023 15:22:40 -0800 (PST) Received: from localhost ([2620:10d:c091:480::1:93a0]) by smtp.gmail.com with ESMTPSA id z10-20020ac8430a000000b003b6325dfc4esm158936qtm.67.2023.01.23.15.22.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Jan 2023 15:22:39 -0800 (PST) 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@meta.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, tj@kernel.org, memxor@gmail.com Subject: [PATCH bpf-next 3/3] bpf/selftests: Verify struct_ops prog sleepable behavior Date: Mon, 23 Jan 2023 17:22:28 -0600 Message-Id: <20230123232228.646563-4-void@manifault.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230123232228.646563-1-void@manifault.com> References: <20230123232228.646563-1-void@manifault.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755858894011068581?= X-GMAIL-MSGID: =?utf-8?q?1755858894011068581?= In a set of prior changes, we added the ability for struct_ops programs to be sleepable. This patch enhances the dummy_st_ops selftest suite to validate this behavior by adding a new struct_ops entry to dummy_st_ops which calls a KF_SLEEPABLE kfunc. Signed-off-by: David Vernet --- include/linux/bpf.h | 1 + net/bpf/bpf_dummy_struct_ops.c | 18 ++++ net/bpf/test_run.c | 6 ++ .../selftests/bpf/prog_tests/dummy_st_ops.c | 85 +++++++++++++++++-- .../selftests/bpf/progs/dummy_st_ops.c | 11 +++ 5 files changed, 113 insertions(+), 8 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index b30739634947..34b90bda3eed 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1473,6 +1473,7 @@ struct bpf_dummy_ops { int (*test_1)(struct bpf_dummy_ops_state *cb); int (*test_2)(struct bpf_dummy_ops_state *cb, int a1, unsigned short a2, char a3, unsigned long a4); + int (*test_3)(struct bpf_dummy_ops_state *cb); }; int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr, diff --git a/net/bpf/bpf_dummy_struct_ops.c b/net/bpf/bpf_dummy_struct_ops.c index 1ac4467928a9..46099737d1da 100644 --- a/net/bpf/bpf_dummy_struct_ops.c +++ b/net/bpf/bpf_dummy_struct_ops.c @@ -154,6 +154,23 @@ static bool bpf_dummy_ops_is_valid_access(int off, int size, return bpf_tracing_btf_ctx_access(off, size, type, prog, info); } +static int bpf_dummy_ops_check_member(const struct btf_type *t, + const struct btf_member *member, + const struct bpf_prog *prog) +{ + u32 moff = __btf_member_bit_offset(t, member) / 8; + + switch (moff) { + case offsetof(struct bpf_dummy_ops, test_3): + break; + default: + if (prog->aux->sleepable) + return -EINVAL; + } + + return 0; +} + static int bpf_dummy_ops_btf_struct_access(struct bpf_verifier_log *log, const struct bpf_reg_state *reg, int off, int size, enum bpf_access_type atype, @@ -208,6 +225,7 @@ static void bpf_dummy_unreg(void *kdata) struct bpf_struct_ops bpf_bpf_dummy_ops = { .verifier_ops = &bpf_dummy_verifier_ops, .init = bpf_dummy_init, + .check_member = bpf_dummy_ops_check_member, .init_member = bpf_dummy_init_member, .reg = bpf_dummy_reg, .unreg = bpf_dummy_unreg, diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c index 2723623429ac..cce1be49a3b7 100644 --- a/net/bpf/test_run.c +++ b/net/bpf/test_run.c @@ -730,6 +730,10 @@ noinline void bpf_kfunc_call_test_destructive(void) { } +noinline void bpf_kfunc_call_test_sleepable(void) +{ +} + __diag_pop(); BTF_SET8_START(bpf_test_modify_return_ids) @@ -767,6 +771,7 @@ BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_fail1) BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_fail2) BTF_ID_FLAGS(func, bpf_kfunc_call_test_ref, KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_kfunc_call_test_destructive, KF_DESTRUCTIVE) +BTF_ID_FLAGS(func, bpf_kfunc_call_test_sleepable, KF_SLEEPABLE) BTF_SET8_END(test_sk_check_kfunc_ids) static void *bpf_test_init(const union bpf_attr *kattr, u32 user_size, @@ -1677,6 +1682,7 @@ static int __init bpf_prog_test_run_init(void) ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_prog_test_kfunc_set); ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_prog_test_kfunc_set); ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &bpf_prog_test_kfunc_set); + ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &bpf_prog_test_kfunc_set); return ret ?: register_btf_id_dtor_kfuncs(bpf_prog_test_dtor_kfunc, ARRAY_SIZE(bpf_prog_test_dtor_kfunc), THIS_MODULE); diff --git a/tools/testing/selftests/bpf/prog_tests/dummy_st_ops.c b/tools/testing/selftests/bpf/prog_tests/dummy_st_ops.c index c11832657d2b..fd496ef8f905 100644 --- a/tools/testing/selftests/bpf/prog_tests/dummy_st_ops.c +++ b/tools/testing/selftests/bpf/prog_tests/dummy_st_ops.c @@ -9,13 +9,37 @@ struct bpf_dummy_ops_state { int val; }; +static struct dummy_st_ops *open_load_skel(void) +{ + int err; + struct dummy_st_ops *skel; + + skel = dummy_st_ops__open(); + if (!ASSERT_OK_PTR(skel, "dummy_st_ops_open")) + return NULL; + + err = bpf_program__set_flags(skel->progs.test_3, BPF_F_SLEEPABLE); + if (!ASSERT_OK(err, "set_sleepable")) { + dummy_st_ops__destroy(skel); + return NULL; + } + + err = dummy_st_ops__load(skel); + if (!ASSERT_OK(err, "dummy_st_ops_load")) { + dummy_st_ops__destroy(skel); + return NULL; + } + + return skel; +} + static void test_dummy_st_ops_attach(void) { struct dummy_st_ops *skel; struct bpf_link *link; - skel = dummy_st_ops__open_and_load(); - if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load")) + skel = open_load_skel(); + if (!skel) return; link = bpf_map__attach_struct_ops(skel->maps.dummy_1); @@ -34,8 +58,8 @@ static void test_dummy_init_ret_value(void) struct dummy_st_ops *skel; int fd, err; - skel = dummy_st_ops__open_and_load(); - if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load")) + skel = open_load_skel(); + if (!skel) return; fd = bpf_program__fd(skel->progs.test_1); @@ -61,8 +85,8 @@ static void test_dummy_init_ptr_arg(void) struct dummy_st_ops *skel; int fd, err; - skel = dummy_st_ops__open_and_load(); - if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load")) + skel = open_load_skel(); + if (!skel) return; fd = bpf_program__fd(skel->progs.test_1); @@ -107,8 +131,8 @@ static void test_dummy_multiple_args(void) size_t i; char name[8]; - skel = dummy_st_ops__open_and_load(); - if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load")) + skel = open_load_skel(); + if (!skel) return; fd = bpf_program__fd(skel->progs.test_2); @@ -122,6 +146,47 @@ static void test_dummy_multiple_args(void) dummy_st_ops__destroy(skel); } +static void test_dummy_sleepable(void) +{ + __u64 args[1] = {0}; + LIBBPF_OPTS(bpf_test_run_opts, attr, + .ctx_in = args, + .ctx_size_in = sizeof(args), + ); + struct dummy_st_ops *skel; + int fd, err; + + skel = open_load_skel(); + if (!skel) + return; + + fd = bpf_program__fd(skel->progs.test_3); + err = bpf_prog_test_run_opts(fd, &attr); + ASSERT_OK(err, "test_run"); + + dummy_st_ops__destroy(skel); +} + +static void test_dummy_sleepable_disallowed(void) +{ + struct dummy_st_ops *skel; + int err; + + skel = dummy_st_ops__open(); + if (!ASSERT_OK_PTR(skel, "dummy_st_ops_open")) + goto out; + + err = bpf_program__set_flags(skel->progs.test_1, BPF_F_SLEEPABLE); + if (!ASSERT_OK(err, "set_sleepable")) + goto out; + + err = dummy_st_ops__load(skel); + ASSERT_ERR(err, "dummy_st_ops_load"); + +out: + dummy_st_ops__destroy(skel); +} + void test_dummy_st_ops(void) { if (test__start_subtest("dummy_st_ops_attach")) @@ -132,4 +197,8 @@ void test_dummy_st_ops(void) test_dummy_init_ptr_arg(); if (test__start_subtest("dummy_multiple_args")) test_dummy_multiple_args(); + if (test__start_subtest("dummy_sleepable")) + test_dummy_sleepable(); + if (test__start_subtest("dummy_not_sleepable")) + test_dummy_sleepable_disallowed(); } diff --git a/tools/testing/selftests/bpf/progs/dummy_st_ops.c b/tools/testing/selftests/bpf/progs/dummy_st_ops.c index ead87edb75e2..721886867efb 100644 --- a/tools/testing/selftests/bpf/progs/dummy_st_ops.c +++ b/tools/testing/selftests/bpf/progs/dummy_st_ops.c @@ -12,10 +12,13 @@ struct bpf_dummy_ops { int (*test_1)(struct bpf_dummy_ops_state *state); int (*test_2)(struct bpf_dummy_ops_state *state, int a1, unsigned short a2, char a3, unsigned long a4); + int (*test_3)(struct bpf_dummy_ops_state *state); }; char _license[] SEC("license") = "GPL"; +void bpf_kfunc_call_test_sleepable(void) __ksym; + SEC("struct_ops/test_1") int BPF_PROG(test_1, struct bpf_dummy_ops_state *state) { @@ -43,8 +46,16 @@ int BPF_PROG(test_2, struct bpf_dummy_ops_state *state, int a1, unsigned short a return 0; } +SEC("struct_ops/test_3") +int BPF_PROG(test_3, struct bpf_dummy_ops_state *state) +{ + bpf_kfunc_call_test_sleepable(); + return 0; +} + SEC(".struct_ops") struct bpf_dummy_ops dummy_1 = { .test_1 = (void *)test_1, .test_2 = (void *)test_2, + .test_3 = (void *)test_3, };