From patchwork Tue Jan 24 16:07:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Vernet X-Patchwork-Id: 47754 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2243604wrn; Tue, 24 Jan 2023 08:20:59 -0800 (PST) X-Google-Smtp-Source: AMrXdXsf6UpDH+4cwHdAZhmWgA8KW7tjjdvh/IKW5CAXfF21D2IownmqvPq6ZVdMMAIpj7j6g1GG X-Received: by 2002:a17:907:397:b0:844:5d1f:95cc with SMTP id ss23-20020a170907039700b008445d1f95ccmr26637097ejb.15.1674577259768; Tue, 24 Jan 2023 08:20:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674577259; cv=none; d=google.com; s=arc-20160816; b=d5b7HzfWwBRe/PL97QNySF+c+vVTBf15vgpHjdrzKTzxib8x8q2lftlNUT7N2wcYTv ao/ItprpUsrwudWgueT6udbrsLE8xa3zPOe5uiF/ZZuHHoA5Plc6XTGd9ODeQskqZpXA pLbB7e37UiaTNI+be0oTo9Dhv+4wKk5Pw6tdEjLwS8OHC7ICH4/1i730kGOSb8d7Ym4m EmKEg1YkdbaFB7L4qoPDVyeZW982l9hPinujx27cRZ2e49+K4rpYk/7puE2twfW2jqvp uMCoJWqru1sDTpwZ4N1+ZxqTEaA3V9Sbw+kq8xAMMiJaq/1ht1L00HAyguDOdIdbMg7S vEBg== 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=geJZ0n/Osgu93pxX9vL5o3+bzLvg1dQW9V/BZwV5zXs=; b=ufj1WlkB7P+9I2AQe4WvcbHTMz96FHqhzOCbcMUVEBr3mQCDQTpnlbKu7+UgA1xy6d mWFmByUpzgohowjkE+ywfPHjmBnV2we1lZiiHfUV01bGxNGTnvK1IlS84y7riUgfPwaz jbLhbC+bDjyf2fzStlOMTO3Vdd1cxDV36u4iArrG41JoGt5FQ+nS+K8Sg2tXSWzGkak/ ZyZGTk5iy7zpk06rPJLgC8lSiWcvkG7EbiRm7tPYHCJ6o+HG5N4lmu31jBrKBCrOyZFr jlYFhYNrwks+RIV1nEO2BzPJgOBXnPZbF4WcnHxja8BMKk6oVU1wGITgfgvZxTS9fwBT kaKQ== 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 g19-20020a056402115300b0049e19d3cc36si3620842edw.78.2023.01.24.08.20.36; Tue, 24 Jan 2023 08:20:59 -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 S233616AbjAXQJ3 (ORCPT + 99 others); Tue, 24 Jan 2023 11:09:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43234 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233386AbjAXQJ2 (ORCPT ); Tue, 24 Jan 2023 11:09:28 -0500 Received: from mail-qt1-f169.google.com (mail-qt1-f169.google.com [209.85.160.169]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A23071ABFC; Tue, 24 Jan 2023 08:09:27 -0800 (PST) Received: by mail-qt1-f169.google.com with SMTP id a25so13420667qto.10; Tue, 24 Jan 2023 08:09:27 -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=geJZ0n/Osgu93pxX9vL5o3+bzLvg1dQW9V/BZwV5zXs=; b=iExaW15UXGcH5X2l1dvf9i8ktI2IL00jf87Sn/fU8AWWy5Ctq3lhhjSK5dWFYRU0kP 8zQyYu9LlJ29UiPyLmsfCxC2QyOlN62dzdJpXf0Tf5iF3RWhWAnqmtdPs/EVqD5Bwj3A fxheHL+ov0pSUQjNt7eZzZqsbD2ZH0xVECuYby6RDP/nGMXd3OtBQjdUIt1ehCuuejJL 2ociJmShOKxfvIoN8PhE+f41aWxXQKqYB63TGr/SybXpPe3AM+ECeZPOr3PKMxpqwMxR 4XgH0f1eoMTcPbv/77nEYVhyve9FLDgzsuBkgNOpwAOsNaLrMRmI/nrTa9PntY8rXXOJ +hzQ== X-Gm-Message-State: AFqh2krQhaaIz1PKtZdj2yRpRpVW+UFZb2E/53MXSdz+8zr+1PUmTb75 mUFB4y7ei28hrlhnJTavntJioPtjqOTgb56N X-Received: by 2002:ac8:4d04:0:b0:3b6:97f0:42dc with SMTP id w4-20020ac84d04000000b003b697f042dcmr23010708qtv.23.1674576566256; Tue, 24 Jan 2023 08:09:26 -0800 (PST) Received: from localhost ([2620:10d:c091:480::1:93a0]) by smtp.gmail.com with ESMTPSA id cr18-20020a05622a429200b003a97a71c906sm1448722qtb.78.2023.01.24.08.09.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Jan 2023 08:09:25 -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 Subject: [PATCH bpf-next v2 1/4] bpf: Allow BPF_PROG_TYPE_STRUCT_OPS programs to be sleepable Date: Tue, 24 Jan 2023 10:07:59 -0600 Message-Id: <20230124160802.1122124-2-void@manifault.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230124160802.1122124-1-void@manifault.com> References: <20230124160802.1122124-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?1755921524873956841?= X-GMAIL-MSGID: =?utf-8?q?1755921524873956841?= BPF struct_ops programs currently cannot be marked as sleepable. This need not be the case -- struct_ops programs can 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 add support to libbpf for specifying struct_ops.s as a sleepable struct_ops program, and then another patch will add testcases to the dummy_st_ops selftest suite which test sleepable struct_ops behavior. Signed-off-by: David Vernet --- kernel/bpf/verifier.c | 5 +++-- tools/testing/selftests/bpf/verifier/sleepable.c | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 800488289297..2b8f0c0aa0cc 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -17054,7 +17054,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) @@ -17076,7 +17077,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; } diff --git a/tools/testing/selftests/bpf/verifier/sleepable.c b/tools/testing/selftests/bpf/verifier/sleepable.c index bea0daef908a..1f0d2bdc673f 100644 --- a/tools/testing/selftests/bpf/verifier/sleepable.c +++ b/tools/testing/selftests/bpf/verifier/sleepable.c @@ -85,7 +85,7 @@ .expected_attach_type = BPF_TRACE_RAW_TP, .kfunc = "sched_switch", .result = REJECT, - .errstr = "Only fentry/fexit/fmod_ret, lsm, iter and uprobe programs can be sleepable", + .errstr = "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, and struct_ops programs can be sleepable", .flags = BPF_F_SLEEPABLE, .runs = -1, }, From patchwork Tue Jan 24 16:08:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Vernet X-Patchwork-Id: 47755 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2243697wrn; Tue, 24 Jan 2023 08:21:11 -0800 (PST) X-Google-Smtp-Source: AMrXdXsaex0cAekVlQrzaHOEkgSjm25C1ZZZbi1wWjDVDzDufNa9cvfJe7Ny1KJCuzUeSNGgnvSD X-Received: by 2002:a17:906:94d0:b0:7c4:f0b5:fedc with SMTP id d16-20020a17090694d000b007c4f0b5fedcmr30257280ejy.65.1674577270942; Tue, 24 Jan 2023 08:21:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674577270; cv=none; d=google.com; s=arc-20160816; b=vhmwKZqNoN025whOaAYQFB5/swAwvAR2hBW2azHkll7bQBjk4T7Dc89B4TRRv63VzZ 904k5ArLF1Y6lqM4xMVxEA+v7MXhydisw9iF09BdiMGexqJzDAANed7Xv8qH1uHBhHBT 6BNiL/ASmQdCy5DGPnUfZthTIHLIVMtTHqwHsjG7LgbWxQ+I6axl8Z6NoEMFsAcgF3Qr fOmI0G7OCbfWl/gl2FZ7VG84Delf/f7l5+FabFv5R7pSShVndNofbnaAGv0C7jkI/tSh kkEbZT0ToihlMPmsClwoOX7mFbk8hEJAltiwbqn+2zM9DhQrTCIwCwl9bxlEP31hkiGb JtxA== 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=/fu5eVF7dMpmGGWY5c48C3xPPttDeaZkjkMTCdf0Gr0=; b=HToyJxaazfDpEX2HMd1QeUEGVjKL2vT/aVDPzD471MH/nAXpOmXeruR3rZlFZtSfMs gMDNk17Ievfi02jKfS8PWBCkd2P7CDM3SzfOUD2Xc+fQkiij8DqS/JgVon+lNpvZ48LX gBi6cx3/9mz1p70+IKzmXwOsG9OcmJpMm40U0hwzhGhxNdfPUWoiYyiHqOX5wAJ5u2XF IayeXSN3uPbRoY4XN9bi/oPlLZHhtfMp/OMbW3Ozv1/RWsaHtCFXKhKfiVhnRMjK3Iew sZTWsLX7E+tEW6FUBwDd09L+yfv2cWQErESz8i0bl8Gg3QA55fKYOZCFLGgVAHZqtl8X lkhA== 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 ft21-20020a170907801500b0084d44edc334si2432680ejc.204.2023.01.24.08.20.45; Tue, 24 Jan 2023 08:21:10 -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 S233868AbjAXQJe (ORCPT + 99 others); Tue, 24 Jan 2023 11:09:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43280 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233741AbjAXQJa (ORCPT ); Tue, 24 Jan 2023 11:09:30 -0500 Received: from mail-qt1-f181.google.com (mail-qt1-f181.google.com [209.85.160.181]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 17B8D4A1EB; Tue, 24 Jan 2023 08:09:29 -0800 (PST) Received: by mail-qt1-f181.google.com with SMTP id s4so13435257qtx.6; Tue, 24 Jan 2023 08:09:29 -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=/fu5eVF7dMpmGGWY5c48C3xPPttDeaZkjkMTCdf0Gr0=; b=1wI9tfI1qOh+StW0HIzmBcpQjt4X50Bwe8clhM5U0u8nmVfAdPnn9E2PBVeEiyYn5V BICYZwGISQaVg/IrkkkohvPSreQJ3uvYNFw4zA7sud5zFpMwbrwnO2oeb0grRsr6Z1Gd PKAV6IzWT1TyOs+cHPlVqm1oPNsFEsaUzbfsMIlEqHe4OP4pTKv7Z2aAKjMjmsf97PBO 5n4L0MepveEDjuprFgEvSIiIfedtwi9yVEgZxluibXfoAUZ23qhsCpY2f1TJL9opVvV3 le7e1K86UIb0OlN4L6nKOvxAs80NHBScyISAGzEeSd1VKMbLMIoUjEYEJT5ze+SzmTKN QbFg== X-Gm-Message-State: AFqh2kpQgjrBdMrQE2CgiiTJZLTrooguv4VHuMnRHXUkQTgHsKgtFOaW vOY9KQwLHIuU9ySfs+IZwW0SNsN0sPHGViAA X-Received: by 2002:ac8:4c84:0:b0:3b6:31e2:f537 with SMTP id j4-20020ac84c84000000b003b631e2f537mr37609803qtv.8.1674576567725; Tue, 24 Jan 2023 08:09:27 -0800 (PST) Received: from localhost ([2620:10d:c091:480::1:93a0]) by smtp.gmail.com with ESMTPSA id m15-20020aed27cf000000b003afbf704c7csm1492934qtg.24.2023.01.24.08.09.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Jan 2023 08:09:27 -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 Subject: [PATCH bpf-next v2 2/4] libbpf: Support sleepable struct_ops.s section Date: Tue, 24 Jan 2023 10:08:00 -0600 Message-Id: <20230124160802.1122124-3-void@manifault.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230124160802.1122124-1-void@manifault.com> References: <20230124160802.1122124-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?1755921536391076844?= X-GMAIL-MSGID: =?utf-8?q?1755921536391076844?= In a prior change, the verifier was updated to support sleepable BPF_PROG_TYPE_STRUCT_OPS programs. A caller could set the program as sleepable with bpf_program__set_flags(), but it would be more ergonomic and more in-line with other sleepable program types if we supported suffixing a struct_ops section name with .s to indicate that it's sleepable. Signed-off-by: David Vernet --- tools/lib/bpf/libbpf.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 27d9faa80471..eed5cec6f510 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -8605,6 +8605,7 @@ static const struct bpf_sec_def section_defs[] = { SEC_DEF("cgroup/setsockopt", CGROUP_SOCKOPT, BPF_CGROUP_SETSOCKOPT, SEC_ATTACHABLE), SEC_DEF("cgroup/dev", CGROUP_DEVICE, BPF_CGROUP_DEVICE, SEC_ATTACHABLE_OPT), SEC_DEF("struct_ops+", STRUCT_OPS, 0, SEC_NONE), + SEC_DEF("struct_ops.s+", STRUCT_OPS, 0, SEC_SLEEPABLE), SEC_DEF("sk_lookup", SK_LOOKUP, BPF_SK_LOOKUP, SEC_ATTACHABLE), }; From patchwork Tue Jan 24 16:08:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Vernet X-Patchwork-Id: 47756 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2243701wrn; Tue, 24 Jan 2023 08:21:11 -0800 (PST) X-Google-Smtp-Source: AMrXdXvI/2LpoMH2dmU5KgQUcUlVi+k4xNgAcC7aoi4kPb3vdMOXXy9mdXUJR3QMOZHJuF7ZlThT X-Received: by 2002:a17:906:b24c:b0:869:236c:ac41 with SMTP id ce12-20020a170906b24c00b00869236cac41mr33124697ejb.24.1674577271049; Tue, 24 Jan 2023 08:21:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674577271; cv=none; d=google.com; s=arc-20160816; b=dALKlxpTVoxWozIZOybIibj4hM0qEsuvLv284TEdjxLX19TTZsjHrclUrhj5HBz3yN DpdUvHuqt3qFq5YFVZXdMykTV0kpRGBWxvnFm4ylwethjRf7DX51M/+fEilwnFSe2k/d +c/eDZN9NxvXLEovIW2KIIcBFnf+NgXoMC7cnTnoAISwBCGzoFitfSvi6m/NyoJBLnkv 6DSl4Fyjvf7jXVkCrHsPqhz0ffdlXOhwX/QtkwD8n4flZ/uG+R4VM3r1Z9XKZnrqdGrv sf0qDD6V97zLDR8BgYME2TifECQi7RA6AMzxQOzJ2ufrdyOl0KWfcD6uWTtZw612q7R1 tR7w== 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=vPyaQjSFalprBy0AXNcos253eyU8hf3tSNTKaLeAtbU=; b=FS7KAU/Rz2ChEX7pEDOMy2dAOumglK3gX9dVkM+n10x/00o4BINL2j6bGaefJWbsQU p+EkNM+JxJriCThUZyaKy/R3uQCGF9+2hCkD7XAUHQafiIz76GwWqiOJZf/wRVYwrvCG 7UsyzJGMkfh9aeBQFmaZoMRB3zBCGBf9gClDsTdXZMSoEb35cPE778DA/RkaXJ81BXn1 Pi8fx+kehJTvoudcD/kt6aFghxJlmx01KYwie8JW7beiy7M4fgWwdcH1FPApVModz9bN GIAQUh8zhWn7WDjvHb8BY2n22pPp/AMOeB3ugPgxp5SwHZunTuzBUd77pDj54b+u6ABn 9Keg== 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 ex8-20020a170907954800b007c18ffbf16esi2585647ejc.433.2023.01.24.08.20.47; Tue, 24 Jan 2023 08:21:11 -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 S233580AbjAXQJn (ORCPT + 99 others); Tue, 24 Jan 2023 11:09:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43402 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233551AbjAXQJl (ORCPT ); Tue, 24 Jan 2023 11:09:41 -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 3F57F4ABEC; Tue, 24 Jan 2023 08:09:30 -0800 (PST) Received: by mail-qv1-f41.google.com with SMTP id n2so11979079qvo.1; Tue, 24 Jan 2023 08:09:30 -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=vPyaQjSFalprBy0AXNcos253eyU8hf3tSNTKaLeAtbU=; b=eXJI/NGrUz19C2WlxSZtJURJ2eHilrHrSosVRdtit+1WY6c0HXwV5jygEj6C5ZFhte 42NkGAgv0Q+dIGdtUsR0n8eMGijKV+S8BMtpC0CMHMGtVLKuzNfXRmwmX65SrvDpSSdX ptUeyLRgg0x7z/Wfpua82IG6Ig3VI3YA1KVXuSbgTsryJBmBibYBo/GMtbA0yRTNsmwp VqyjyuqmeSTzK/T2Ojd55XCJVvTPeHjC/ykpZA9kNSx1vwVQMyI26b94HdMbSw6zYbQM LUcUq87860YM7hz1Soj6qV4j52e+nOBgUyWXTz8oC8lxs/nbZXOPStn8X899aumP1GlV qBmw== X-Gm-Message-State: AFqh2kqsVvmYgzdAimFeWokc/w9LQxIySvEyAv1KEvHW6sHrM8K04oxm pch1y5QXvETYDvSDZ/lQJPZMeBM2xHXPf7VP X-Received: by 2002:a0c:8b12:0:b0:535:2264:7433 with SMTP id q18-20020a0c8b12000000b0053522647433mr38105343qva.6.1674576569229; Tue, 24 Jan 2023 08:09:29 -0800 (PST) Received: from localhost ([2620:10d:c091:480::1:93a0]) by smtp.gmail.com with ESMTPSA id e22-20020a05620a209600b006cbe3be300esm1676344qka.12.2023.01.24.08.09.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Jan 2023 08:09:28 -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 Subject: [PATCH bpf-next v2 3/4] bpf: Pass const struct bpf_prog * to .check_member Date: Tue, 24 Jan 2023 10:08:01 -0600 Message-Id: <20230124160802.1122124-4-void@manifault.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230124160802.1122124-1-void@manifault.com> References: <20230124160802.1122124-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?1755921536562356367?= X-GMAIL-MSGID: =?utf-8?q?1755921536562356367?= 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 enforce that the struct_ops prog being loaded for that member has other qualities, 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 ad4bb36d4c10..50123afab9bf 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1422,7 +1422,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 2b8f0c0aa0cc..20dd03498e56 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -16732,7 +16732,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 Tue Jan 24 16:08:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Vernet X-Patchwork-Id: 47757 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2243739wrn; Tue, 24 Jan 2023 08:21:15 -0800 (PST) X-Google-Smtp-Source: AMrXdXsYaQ/takFoWv5UWRrQMT0WWIYdxAsrLwvTGRPrY8AHAtLATT8TlsAqqTH57UlehpLPR2h8 X-Received: by 2002:a17:906:b305:b0:86e:8b7b:adb9 with SMTP id n5-20020a170906b30500b0086e8b7badb9mr26986524ejz.34.1674577275582; Tue, 24 Jan 2023 08:21:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674577275; cv=none; d=google.com; s=arc-20160816; b=TMgAdyvYWfpRTn2p/+rMeunepoGsZBG3xPZB1vV+f+IA8MurIlLUj30t9d2F7GOxRT cQ6KnpyFKzkxa4F8uQq9thcad8xyZXFb/REzXKAkUSjeHH/a5LP9CrZseAphTbkLzI3h hDCszDEo83+PWJEAAfq/W5701o33wjKowe4gb/h3qMzolNHevWy5icWpWlYVGmmMo24M 9QciN7ciNcgQx/s/IL1snEqeHd0eW2IwqOzCBTuaeQAgYQnLJdmuIUbOaq+gbLPjtTuG gv4lPYNJPyYIGLRiZYe5odvcxcsVMCJqWioJGxV76VCFEQ1DV6bdwnhZpsqu1eQ5GHzf 36YA== 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=nqp9aSOT9za2yKZpSIP55Z7jUl/+T4V00ezoEYQ+5+I=; b=yZg+sv1Ij+dFwxdMgLY7dJLLu67w3pD+NdKmhtMvWAW0BbG6Vrq2TqQNJdB9FjBpKS 5TPpk5Dh7kM3HilBQz86/E081R9rwJ/fKS6ojlMIeGJmXErWXA/wNAqdpD/vqGUJoZ/X ADvmAQievO79eeTXJJgK8jygQH4zDFOkpdwcKACRcMGuq4JOCysxrV5BqAZyKRmIyqG4 ExvozlIApDnyEIQgJCNmhoOrYdJRnNdqpIWxzGyoHFxoAs+aYzhk61mYby6XibZC2UrL y4Cajypxu2vTcB++ySrQcWG2tJoleLhwTDfLKQAReEoziGbxHdoRDxDK4migDOjcoF+d k5ZA== 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 fp34-20020a1709069e2200b0083bb359db7dsi3280328ejc.115.2023.01.24.08.20.51; Tue, 24 Jan 2023 08:21:15 -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 S234100AbjAXQKB (ORCPT + 99 others); Tue, 24 Jan 2023 11:10:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43644 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234179AbjAXQJy (ORCPT ); Tue, 24 Jan 2023 11:09:54 -0500 Received: from mail-yw1-f175.google.com (mail-yw1-f175.google.com [209.85.128.175]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2A3124ABD0; Tue, 24 Jan 2023 08:09:32 -0800 (PST) Received: by mail-yw1-f175.google.com with SMTP id 00721157ae682-4fda31c3351so195900877b3.11; Tue, 24 Jan 2023 08:09:32 -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=nqp9aSOT9za2yKZpSIP55Z7jUl/+T4V00ezoEYQ+5+I=; b=v6FTruwW8YFQaDOzbMgIpJ/OlMMJT5sbN2DzFW4X8LDIltjrU2TNVtPeVol+BHTQ14 4GrcMgA70V0HTERiv8VOUYiYGgLfLpS7PYOUVFR+KWe2uF3NUtvtQHx/uVk+DANKYQ9R GwGjvMtRdsIWKJmKimAzleocMSUbL2Cdxs4r61zm6wPw06bM0KGt0EZY+D15mM7nVkFk XErmcKlZMmDHsFCAEKXw620zzYj5xSV3d+NI0kj0ajiDd++6Qiuwacrmmk9m8u2Al1cj 0woW7Y+vCTYSrpAyyPWoOiPlI6EcjMM1e8uO0Bo23ig/KdI2XNzdvSUW3SLQcqw0dpn8 JdKA== X-Gm-Message-State: AFqh2kpRAOpoRe9exK2QUbk24xSUFqn/pqwyX/9ReL/TNR6doSTk1QjX tbxtrd/IwUJHsga6GUV3M6N86BpqQkX2jh3t X-Received: by 2002:a05:7500:631c:b0:f1:e53f:ed8f with SMTP id ib28-20020a057500631c00b000f1e53fed8fmr2253743gab.6.1674576570877; Tue, 24 Jan 2023 08:09:30 -0800 (PST) Received: from localhost ([2620:10d:c091:480::1:93a0]) by smtp.gmail.com with ESMTPSA id 71-20020a370c4a000000b006fc5a1d9cd4sm1660355qkm.34.2023.01.24.08.09.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Jan 2023 08:09:30 -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 Subject: [PATCH bpf-next v2 4/4] bpf/selftests: Verify struct_ops prog sleepable behavior Date: Tue, 24 Jan 2023 10:08:02 -0600 Message-Id: <20230124160802.1122124-5-void@manifault.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230124160802.1122124-1-void@manifault.com> References: <20230124160802.1122124-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?1755921541335624610?= X-GMAIL-MSGID: =?utf-8?q?1755921541335624610?= 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 | 56 ++++++++++++++----- .../selftests/bpf/progs/dummy_st_ops_common.h | 20 +++++++ .../selftests/bpf/progs/dummy_st_ops_fail.c | 41 ++++++++++++++ ...{dummy_st_ops.c => dummy_st_ops_success.c} | 18 +++--- 7 files changed, 136 insertions(+), 24 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/dummy_st_ops_common.h create mode 100644 tools/testing/selftests/bpf/progs/dummy_st_ops_fail.c rename tools/testing/selftests/bpf/progs/{dummy_st_ops.c => dummy_st_ops_success.c} (75%) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 50123afab9bf..64034311c5f7 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1474,6 +1474,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 8da0d73b368e..33ea57d34c0b 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, @@ -1680,6 +1685,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..dbf568e0ade2 100644 --- a/tools/testing/selftests/bpf/prog_tests/dummy_st_ops.c +++ b/tools/testing/selftests/bpf/prog_tests/dummy_st_ops.c @@ -1,7 +1,8 @@ // SPDX-License-Identifier: GPL-2.0 /* Copyright (C) 2021. Huawei Technologies Co., Ltd */ #include -#include "dummy_st_ops.skel.h" +#include "dummy_st_ops_success.skel.h" +#include "dummy_st_ops_fail.skel.h" #include "trace_dummy_st_ops.skel.h" /* Need to keep consistent with definition in include/linux/bpf.h */ @@ -11,17 +12,17 @@ struct bpf_dummy_ops_state { static void test_dummy_st_ops_attach(void) { - struct dummy_st_ops *skel; + struct dummy_st_ops_success *skel; struct bpf_link *link; - skel = dummy_st_ops__open_and_load(); + skel = dummy_st_ops_success__open_and_load(); if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load")) return; link = bpf_map__attach_struct_ops(skel->maps.dummy_1); ASSERT_EQ(libbpf_get_error(link), -EOPNOTSUPP, "dummy_st_ops_attach"); - dummy_st_ops__destroy(skel); + dummy_st_ops_success__destroy(skel); } static void test_dummy_init_ret_value(void) @@ -31,10 +32,10 @@ static void test_dummy_init_ret_value(void) .ctx_in = args, .ctx_size_in = sizeof(args), ); - struct dummy_st_ops *skel; + struct dummy_st_ops_success *skel; int fd, err; - skel = dummy_st_ops__open_and_load(); + skel = dummy_st_ops_success__open_and_load(); if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load")) return; @@ -43,7 +44,7 @@ static void test_dummy_init_ret_value(void) ASSERT_OK(err, "test_run"); ASSERT_EQ(attr.retval, 0xf2f3f4f5, "test_ret"); - dummy_st_ops__destroy(skel); + dummy_st_ops_success__destroy(skel); } static void test_dummy_init_ptr_arg(void) @@ -58,17 +59,17 @@ static void test_dummy_init_ptr_arg(void) .ctx_size_in = sizeof(args), ); struct trace_dummy_st_ops *trace_skel; - struct dummy_st_ops *skel; + struct dummy_st_ops_success *skel; int fd, err; - skel = dummy_st_ops__open_and_load(); + skel = dummy_st_ops_success__open_and_load(); if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load")) return; fd = bpf_program__fd(skel->progs.test_1); trace_skel = trace_dummy_st_ops__open(); - if (!ASSERT_OK_PTR(trace_skel, "trace_dummy_st_ops__open")) + if (!ASSERT_OK_PTR(trace_skel, "trace_dummy_st_ops_success__open")) goto done; err = bpf_program__set_attach_target(trace_skel->progs.fentry_test_1, @@ -91,7 +92,7 @@ static void test_dummy_init_ptr_arg(void) ASSERT_EQ(trace_skel->bss->val, exp_retval, "fentry_val"); done: - dummy_st_ops__destroy(skel); + dummy_st_ops_success__destroy(skel); trace_dummy_st_ops__destroy(trace_skel); } @@ -102,12 +103,12 @@ static void test_dummy_multiple_args(void) .ctx_in = args, .ctx_size_in = sizeof(args), ); - struct dummy_st_ops *skel; + struct dummy_st_ops_success *skel; int fd, err; size_t i; char name[8]; - skel = dummy_st_ops__open_and_load(); + skel = dummy_st_ops_success__open_and_load(); if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load")) return; @@ -119,10 +120,31 @@ static void test_dummy_multiple_args(void) ASSERT_EQ(skel->bss->test_2_args[i], args[i], name); } - dummy_st_ops__destroy(skel); + dummy_st_ops_success__destroy(skel); } -void test_dummy_st_ops(void) +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_success *skel; + int fd, err; + + skel = dummy_st_ops_success__open_and_load(); + if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load")) + 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_success__destroy(skel); +} + +void test_dummy_st_ops_success(void) { if (test__start_subtest("dummy_st_ops_attach")) test_dummy_st_ops_attach(); @@ -132,4 +154,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(); + + RUN_TESTS(dummy_st_ops_fail); } diff --git a/tools/testing/selftests/bpf/progs/dummy_st_ops_common.h b/tools/testing/selftests/bpf/progs/dummy_st_ops_common.h new file mode 100644 index 000000000000..7d0761594b69 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/dummy_st_ops_common.h @@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */ + +#ifndef _DUMMY_ST_OPS_COMMON_H +#define _DUMMY_ST_OPS_COMMON_H + +struct bpf_dummy_ops_state { + int val; +} __attribute__((preserve_access_index)); + +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); +}; + +void bpf_kfunc_call_test_sleepable(void) __ksym; + +#endif /* _DUMMY_ST_OPS_COMMON_H */ diff --git a/tools/testing/selftests/bpf/progs/dummy_st_ops_fail.c b/tools/testing/selftests/bpf/progs/dummy_st_ops_fail.c new file mode 100644 index 000000000000..e3f6c5445e0d --- /dev/null +++ b/tools/testing/selftests/bpf/progs/dummy_st_ops_fail.c @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */ + +#include +#include +#include + +#include "bpf_misc.h" +#include "dummy_st_ops_common.h" + +char _license[] SEC("license") = "GPL"; + +SEC("struct_ops.s/test_2") +__failure __msg("attach to unsupported member test_2 of struct bpf_dummy_ops") +int BPF_PROG(test_unsupported_field_sleepable, + struct bpf_dummy_ops_state *state, int a1, unsigned short a2, + char a3, unsigned long a4) +{ + /* Tries to mark an unsleepable field in struct bpf_dummy_ops as sleepable. */ + return 0; +} + +SEC("struct_ops/test_3") +__failure __msg("program must be sleepable to call sleepable kfunc bpf_kfunc_call_test_sleepable") +int BPF_PROG(test_supported_field_not_in_sleepable_section, struct bpf_dummy_ops_state *state) +{ + /* + * Calls a sleepable kfunc, but doesn't mark self as sleepable, even + * though test_3 is allowed to be a sleepable field in struct + * bpf_ops_dummy. + */ + bpf_kfunc_call_test_sleepable(); + return 0; +} + +SEC(".struct_ops") +struct bpf_dummy_ops dummy_1 = { + .test_1 = NULL, + .test_2 = (void *)test_unsupported_field_sleepable, + .test_3 = (void *)test_supported_field_not_in_sleepable_section, +}; diff --git a/tools/testing/selftests/bpf/progs/dummy_st_ops.c b/tools/testing/selftests/bpf/progs/dummy_st_ops_success.c similarity index 75% rename from tools/testing/selftests/bpf/progs/dummy_st_ops.c rename to tools/testing/selftests/bpf/progs/dummy_st_ops_success.c index ead87edb75e2..1e9fab99ac0d 100644 --- a/tools/testing/selftests/bpf/progs/dummy_st_ops.c +++ b/tools/testing/selftests/bpf/progs/dummy_st_ops_success.c @@ -4,15 +4,7 @@ #include #include -struct bpf_dummy_ops_state { - int val; -} __attribute__((preserve_access_index)); - -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); -}; +#include "dummy_st_ops_common.h" char _license[] SEC("license") = "GPL"; @@ -43,8 +35,16 @@ int BPF_PROG(test_2, struct bpf_dummy_ops_state *state, int a1, unsigned short a return 0; } +SEC("struct_ops.s/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, };