From patchwork Wed Feb 14 17:18:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 201077 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp1385700dyb; Wed, 14 Feb 2024 09:46:10 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXGO6jR5mvv5wRkjiAYcX1tDVJ6x303M4/E51WgqjQzV0AfxQPXevexHbhcDC9Dh3OHOF6Zp7xyKcEpKYREph6GtQRGcg== X-Google-Smtp-Source: AGHT+IGC8kGdxPN597d1AAigKxzuHYfiQb6olVq01i9gxyTgiLhiUuS9HdUrPXWmPVFeSOAwcKyZ X-Received: by 2002:a17:90b:182:b0:298:d0ed:e13a with SMTP id t2-20020a17090b018200b00298d0ede13amr3922144pjs.15.1707932770471; Wed, 14 Feb 2024 09:46:10 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707932770; cv=pass; d=google.com; s=arc-20160816; b=uWTDfHAmQYr5yxIZ1k/PZJSFa5DBwdrcpxGTwClAUG/WeeQIUFGx5RE9AxZRPvjX2U aaLAFtYzx8Tt024Ow7KYKMvGf8mkuXHDhcgKIZTZuysswtNS8GmrBlV+anWxSyOHfsKk tWwSj3Gvegg6Rh5mfFGA++g77RwzKEKtcuYPPzrFi+Xyz2lWp6uTE1Oo1nmJZmwwBx5t lJcQLZtaCIhdx3KtBEwjUiSjTgcS+BIdiTzHS7RBUx7SK3Z85y8q1D3QeEAGppzXq/t7 5rwilu8zDrb3bFQBIF8wwaUJtA7jy6NKCIz4QRSY8ESUX+wAR8phsWeOhvv2BUigUMkg VclA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=4e6lpCETxZwarx81SfIibXdAqv2Wu15cBOAxu6blfa8=; fh=hc4Y6F7QBSV+XcCZzl4ZOgjmnikSFu9Au/ny4/yLvAg=; b=jBxWF7BgssgZIawQvZ71y0vLJtGS6KzqtZbFx4ugpmzn5BhXMk+Z0aKPnykn6kcbCv cgazpBIRyPe8VptAA3FH7Y2mVpTFItHEWFISZElYpcKBqcaZO3iVZCIbzRYi7F+BnIGZ akAeG3CZzW4cRhTwJDMmDuVCvaXtjvAnmz73gkFa+1vxWPNVwc3Ha3gw7PMqPy85C+0J kaeaOzY7dhscer46SIXe0k2VGz8RMSR5yTnqMuEbzAfbkaHvrtXiQDckKsV5e2FaicBc sqJFfgWtC2xtgH5go2F0OR068yAyvDhVYkHj0F4SiiZ5+g7n2xkGYy5bU/oQwgyT8pqk fJvQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=QC12kdg4; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-65639-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65639-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id ep3-20020a17090ae64300b00296a72882c4si1484571pjb.74.2024.02.14.09.46.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 09:46:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-65639-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=QC12kdg4; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-65639-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65639-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id A51DAB2D164 for ; Wed, 14 Feb 2024 17:20:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A5B0084FD1; Wed, 14 Feb 2024 17:19:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="QC12kdg4" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 005358405C; Wed, 14 Feb 2024 17:19:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707931145; cv=none; b=dSRUj3zt1xxvCCiKewZ6coO/IaLts2iB7nzKY4VEcZCnIesYWaFF5G781+g0Fyb3JknyprhVHpRAfoROSQtDYZ/S7yS8Snp6/4pBH0D8ccd1o4tnmaW2d54d3X8LFFIGv2kKJDFUPAPqWGy+C/c6kedXaeqwcbvLaVDUraL665Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707931145; c=relaxed/simple; bh=ZuS+7v5Dfl5IafHw0MgYCYKa9AW++myTFw0GViRRpYM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=AO2OJJ+ZogPCs4yRP6c+vyzwdUcu6l1w4wKITYa9+GqlF1sbiw8HZx12wP6mdAiMAkXsOe6/Z07QZipNTFNuJw8eLTmFpC6xiLoYl9EMlrMmyPtoe7denNng0pVNIG2Ws8YV2TxdBSymiLNSPvYErUAG6BZHT5ocr+MjmXAXXKE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=QC12kdg4; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 74061C43601; Wed, 14 Feb 2024 17:19:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707931144; bh=ZuS+7v5Dfl5IafHw0MgYCYKa9AW++myTFw0GViRRpYM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=QC12kdg4Aib5AcPh49e2/LK8jJLkgUAruYKDOwNQaopd+pga/EDbxFgh3ojIhfczu HXg6GquX+hye+mvEnKHqn1lMPHgJUrbdk7h3TSO3+RVznexX/Wwj7SuYd2FKyGeFLM sRgVVFHhveH5Tr2z1J//ugH1iSil5213Fyh16blTxF0YnL1yjNTganARWQjG2HPPmq xklI4xCwY80G7dw1cjP2FWdDRIPKtbiZ0zmRFhtQrUJgNcCKbn8H95/F7TDXil2YDf p98Qxk3Oe4tF1Mju5PRRQr192/xJ6xEO0M+LjhkLpxJ9uNcYEGx93PaJtXQvMq1Veq SXQqZxyh5u7+A== From: Benjamin Tissoires Date: Wed, 14 Feb 2024 18:18:30 +0100 Subject: [PATCH RFC bpf-next v2 01/10] bpf/verifier: introduce in_sleepable() helper Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240214-hid-bpf-sleepable-v2-1-5756b054724d@kernel.org> References: <20240214-hid-bpf-sleepable-v2-0-5756b054724d@kernel.org> In-Reply-To: <20240214-hid-bpf-sleepable-v2-0-5756b054724d@kernel.org> To: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Jiri Kosina , Benjamin Tissoires , Jonathan Corbet , Shuah Khan Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1707931135; l=3259; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=ZuS+7v5Dfl5IafHw0MgYCYKa9AW++myTFw0GViRRpYM=; b=37Ow3DoMrOPm0yj/WYnsJcZ0R+oyH5zub8WYF+eRcYwPZ/8PMJA2pgnCX/cG5KdlJnzEDvfZi cd3R+/0JvARDrWnnyoK+ZqKtuRsCmOHN5BmrAb/Ol6jjgL5wMgD7s2q X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790897312557943067 X-GMAIL-MSGID: 1790897312557943067 No code change, but it'll allow to have only one place to change everything when we add in_sleepable in cur_state. Signed-off-by: Benjamin Tissoires --- changes in v2 (compared to the one attaches to v1 0/9): - dropped the cur_state flag, so it can be put first --- kernel/bpf/verifier.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 64fa188d00ad..7831adba9abf 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5255,6 +5255,11 @@ static int map_kptr_match_type(struct bpf_verifier_env *env, return -EINVAL; } +static bool in_sleepable(struct bpf_verifier_env *env) +{ + return env->prog->aux->sleepable; +} + /* The non-sleepable programs and sleepable programs with explicit bpf_rcu_read_lock() * can dereference RCU protected pointers and result is PTR_TRUSTED. */ @@ -5262,7 +5267,7 @@ static bool in_rcu_cs(struct bpf_verifier_env *env) { return env->cur_state->active_rcu_lock || env->cur_state->active_lock.ptr || - !env->prog->aux->sleepable; + !in_sleepable(env); } /* Once GCC supports btf_type_tag the following mechanism will be replaced with tag check */ @@ -10149,7 +10154,7 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn return -EINVAL; } - if (!env->prog->aux->sleepable && fn->might_sleep) { + if (!in_sleepable(env) && fn->might_sleep) { verbose(env, "helper call might sleep in a non-sleepable prog\n"); return -EINVAL; } @@ -10179,7 +10184,7 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn return -EINVAL; } - if (env->prog->aux->sleepable && is_storage_get_function(func_id)) + if (in_sleepable(env) && is_storage_get_function(func_id)) env->insn_aux_data[insn_idx].storage_get_func_atomic = true; } @@ -11538,7 +11543,7 @@ static bool check_css_task_iter_allowlist(struct bpf_verifier_env *env) return true; fallthrough; default: - return env->prog->aux->sleepable; + return in_sleepable(env); } } @@ -12059,7 +12064,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, } sleepable = is_kfunc_sleepable(&meta); - if (sleepable && !env->prog->aux->sleepable) { + if (sleepable && !in_sleepable(env)) { verbose(env, "program must be sleepable to call sleepable kfunc %s\n", func_name); return -EACCES; } @@ -18197,7 +18202,7 @@ static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env) return -E2BIG; } - if (env->prog->aux->sleepable) + if (in_sleepable(env)) atomic64_inc(&map->sleepable_refcnt); /* hold the map. If the program is rejected by verifier, * the map will be released by release_maps() or it @@ -19673,7 +19678,7 @@ static int do_misc_fixups(struct bpf_verifier_env *env) } if (is_storage_get_function(insn->imm)) { - if (!env->prog->aux->sleepable || + if (!in_sleepable(env) || env->insn_aux_data[i + delta].storage_get_func_atomic) insn_buf[0] = BPF_MOV64_IMM(BPF_REG_5, (__force __s32)GFP_ATOMIC); else From patchwork Wed Feb 14 17:18:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 201056 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp1371865dyb; Wed, 14 Feb 2024 09:23:08 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWZTG5VNQyuyJcPnd+cDrnvRVNcK4YN95HYf2bSnLm4p9kFC6k43vdqKv4bgjt8nEqVej2tkANtnB61KNWXO6GoAs5yFQ== X-Google-Smtp-Source: AGHT+IHryQPyx8cCf8tDgyh6gM15yFjF0mgorJUHJPe4EruDHtjTon/uMxADkseHawmaly0Czpbf X-Received: by 2002:a17:902:f54d:b0:1da:22d9:e7fd with SMTP id h13-20020a170902f54d00b001da22d9e7fdmr3553408plf.20.1707931387990; Wed, 14 Feb 2024 09:23:07 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707931387; cv=pass; d=google.com; s=arc-20160816; b=D/wZAd6OLqV7kLBKCA2GEDZnIaNO096beHf5fOSamWL1mFE5SLjL4yj05pr53DkqLz ummSHyJ9yih+HeU97Nc+xifKZci9VLkgqT/9OufCaoXldr1MU0/eI+ekpvxzH3k1Awfj 9w/zPctt6H1XHfQIP6BnBb/406lGvVuB+O1s9kU6KcOJOIMU+EVeK4tfbwcIIQzpu7ga I30GBDujF+SEr0Uf8WadkleElxAZQCKcusytCqX8HtCeRIay7hfOW3STbpnigGM8gtbr RxeYC8PQ7Af/qEUkKlXEk9/XY395OJsV7hwEA2r/FOhwWATHXWZ8TLN4P1tCL73KZufL 7RMA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=gjQwIrZFp9zrt2L6xLYisnqFYiG99y8iMrl9Uzt/gTE=; fh=YG+E0m5hdCPoTlW0EgKeOqhLxzmON+nj+Hnb2HDyUhI=; b=Nh1Os6QW6HtLjlqhwH+fPul7lEMbvDlFKbm9gVDc2yzspH3/RYXw6tQPYnxvCKhm6f l408NxSeMAB6jkxdqABAmJXmbMdsjDs7WVo5fexeLIUysIk0gokNTYYFy3V2MM2jH2ia T0A8DTgWDXb+Lv9qaxYxymJDB8XpMACQXocm5VpHYsf28Uw15+klDa2/WzzLnsJRXz7Z a2ngOHoE1jrjjth+soLYlhpPtdCTd17XHBnnKq1qF3Yiohbz6xOySSvz8Q2LWl3j1DU+ utUND0VujXKfr32p/z8EVJoNBU/F9v58PTbx24oNPJOOgKlFAqla/tMpvTCpO6s2+2CD exjw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=pKaugZ7p; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-65640-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65640-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=2; AJvYcCXyxFeoLndq1Ct7yN49bGGfZwxCzzTpxPjYVt80RipPrYRKgOxXgs2qRmMeADxdKDb14OCYn/NXa8oJJ1LFkyPVQ2LKkw== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id i17-20020a17090332d100b001d9607d5cc5si3974863plr.244.2024.02.14.09.23.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 09:23:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-65640-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=pKaugZ7p; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-65640-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65640-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id A8B9528EA05 for ; Wed, 14 Feb 2024 17:20:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 95CF882C6B; Wed, 14 Feb 2024 17:19:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="pKaugZ7p" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6466584FCD; Wed, 14 Feb 2024 17:19:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707931150; cv=none; b=vGu4n4QheMzZ4PnBUguxfFr6rPNMsHSohwU/o4JK/UR3mAb3vpgYdEGmZ35JelWJVegi+KehwpRGmKOHsREVnaJgPiCX3RjnHWbBGH26mFa7EDqCriQqBXpRh4Q63MaVpioTCuTcFEc6oUCL2ut4QePsx3mN9d4gS02F7eT5wig= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707931150; c=relaxed/simple; bh=LjjQ9QpSVv5Grn2hLf1mKwXw2hs0FHcfxkNuAvSb4nw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=dc25uUJStu5gKetLufO+BPbjJp+Old1e3uWbPjq8zk6DwzsvLABHDg2JdjttgWkJ708nyq7ovPNvamS/co2KgNKc55swNF9wB+bKSkSH0Ak7qCSHx/5c/MHs6ptclEp6RpRZ83M+5Zbnd+f1/3nGEL9htvCzCIfmjiwuNdFui6Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=pKaugZ7p; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id CEAABC433F1; Wed, 14 Feb 2024 17:19:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707931148; bh=LjjQ9QpSVv5Grn2hLf1mKwXw2hs0FHcfxkNuAvSb4nw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=pKaugZ7pOKT3AXUGpDiBALEHVHKk31W1ZF5lxnAs8bpRPXThUvfypRHsTKly1OEXM Gkdp94EIfgrzGZO9R9HaE3xaw7Dcx73E4ORMo5fnCmG2JZleiQuX/4viDfmj+SgyPT KmvTE4hpYIDuyv8GST6ImG6ZU7WafSNvIkHWGwhB985QGNd0UaHpHw69bFTW3KmanR DAUECWoWnVRhz8HyZb3TZw7oGOESilPn3qXaOSrbOxJcmaF10VVRETDgLK2KZTLqH+ 9cQsbh7TZbw9OmgBrHfIbaOjEzJ/qEIojur1DYd7hLZ15D1UVzceZjViCY3dsNj94h 90eNnzrkUz72Q== From: Benjamin Tissoires Date: Wed, 14 Feb 2024 18:18:31 +0100 Subject: [PATCH RFC bpf-next v2 02/10] bpf/helpers: introduce sleepable timers Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240214-hid-bpf-sleepable-v2-2-5756b054724d@kernel.org> References: <20240214-hid-bpf-sleepable-v2-0-5756b054724d@kernel.org> In-Reply-To: <20240214-hid-bpf-sleepable-v2-0-5756b054724d@kernel.org> To: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Jiri Kosina , Benjamin Tissoires , Jonathan Corbet , Shuah Khan Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1707931135; l=17650; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=LjjQ9QpSVv5Grn2hLf1mKwXw2hs0FHcfxkNuAvSb4nw=; b=2rv+f3Xy4jyPKp1W0A/osjfNN0RmsCvKLgqQp42biFek1oFMpExgV+oZOzKmTuDLNqV+Klarq ttBZi0TscXmAIIRABCNWt8wZkJZLApAV6PTR8vrvS6F3tHm1GzboKgp X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790895863062106753 X-GMAIL-MSGID: 1790895863062106753 They are implemented as a kfunc, which means a little bit of tweaks in the verifier. Signed-off-by: Benjamin Tissoires --- changes in v2 (compared to the one attaches to v1 0/9): - make use of a kfunc - add a (non-used) BPF_F_TIMER_SLEEPABLE - the callback is *not* called, it makes the kernel crashes --- include/linux/bpf_verifier.h | 2 + include/uapi/linux/bpf.h | 12 +++++ kernel/bpf/helpers.c | 105 ++++++++++++++++++++++++++++++++++++++++--- kernel/bpf/verifier.c | 72 ++++++++++++++++++++++++++--- 4 files changed, 180 insertions(+), 11 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 84365e6dd85d..789ef5fec547 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -426,6 +426,7 @@ struct bpf_verifier_state { * while they are still in use. */ bool used_as_loop_entry; + bool in_sleepable; /* first and last insn idx of this verifier state */ u32 first_insn_idx; @@ -626,6 +627,7 @@ struct bpf_subprog_info { bool is_async_cb: 1; bool is_exception_cb: 1; bool args_cached: 1; + bool is_sleepable: 1; u8 arg_cnt; struct bpf_subprog_arg_info args[MAX_BPF_FUNC_REG_ARGS]; diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index d96708380e52..0838597028a9 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -7427,6 +7427,18 @@ enum { BPF_F_TIMER_CPU_PIN = (1ULL << 1), }; +/* Extra flags to control bpf_timer_init() behaviour, in addition to CLOCK_*. + * - BPF_F_TIMER_SLEEPABLE: Timer will run in a workqueue in a sleepable + * context. + */ +enum { + /* CLOCK_* are using bits 0 to 3 */ + BPF_F_TIMER_SLEEPABLE = (1ULL << 4), + __MAX_BPF_F_TIMER_INIT, +}; + +#define MAX_BPF_F_TIMER_INIT __MAX_BPF_F_TIMER_INIT + /* BPF numbers iterator state */ struct bpf_iter_num { /* opaque iterator state; having __u64 here allows to preserve correct diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 4db1c658254c..2dbc09ce841a 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -1097,9 +1097,11 @@ const struct bpf_func_proto bpf_snprintf_proto = { */ struct bpf_hrtimer { struct hrtimer timer; + struct work_struct work; struct bpf_map *map; struct bpf_prog *prog; void __rcu *callback_fn; + void __rcu *sleepable_cb_fn; void *value; }; @@ -1113,18 +1115,59 @@ struct bpf_timer_kern { struct bpf_spin_lock lock; } __attribute__((aligned(8))); +static void bpf_timer_work_cb(struct work_struct *work) +{ + struct bpf_hrtimer *t = container_of(work, struct bpf_hrtimer, work); + struct bpf_map *map = t->map; + void *value = t->value; + bpf_callback_t callback_fn; + void *key; + u32 idx; + + BTF_TYPE_EMIT(struct bpf_timer); + + rcu_read_lock(); + callback_fn = rcu_dereference(t->sleepable_cb_fn); + rcu_read_unlock(); + if (!callback_fn) + return; + + /* FIXME: do we need any locking? */ + if (map->map_type == BPF_MAP_TYPE_ARRAY) { + struct bpf_array *array = container_of(map, struct bpf_array, map); + + /* compute the key */ + idx = ((char *)value - array->value) / array->elem_size; + key = &idx; + } else { /* hash or lru */ + key = value - round_up(map->key_size, 8); + } + + /* FIXME: this crashes the system with + * BUG: kernel NULL pointer dereference, address: 000000000000000b + */ + /* callback_fn((u64)(long)map, (u64)(long)key, (u64)(long)value, 0, 0); */ + /* The verifier checked that return value is zero. */ +} + static DEFINE_PER_CPU(struct bpf_hrtimer *, hrtimer_running); static enum hrtimer_restart bpf_timer_cb(struct hrtimer *hrtimer) { struct bpf_hrtimer *t = container_of(hrtimer, struct bpf_hrtimer, timer); + bpf_callback_t callback_fn, sleepable_cb_fn; struct bpf_map *map = t->map; void *value = t->value; - bpf_callback_t callback_fn; void *key; u32 idx; BTF_TYPE_EMIT(struct bpf_timer); + sleepable_cb_fn = rcu_dereference_check(t->sleepable_cb_fn, rcu_read_lock_bh_held()); + if (sleepable_cb_fn) { + schedule_work(&t->work); + goto out; + } + callback_fn = rcu_dereference_check(t->callback_fn, rcu_read_lock_bh_held()); if (!callback_fn) goto out; @@ -1154,10 +1197,14 @@ static enum hrtimer_restart bpf_timer_cb(struct hrtimer *hrtimer) return HRTIMER_NORESTART; } +#define BPF_TIMER_CLOCK_MASK (MAX_CLOCKS - 1) /* 0xf */ +#define BPF_TIMER_FLAGS_MASK GENMASK_ULL(63, 4) + BPF_CALL_3(bpf_timer_init, struct bpf_timer_kern *, timer, struct bpf_map *, map, u64, flags) { - clockid_t clockid = flags & (MAX_CLOCKS - 1); + clockid_t clockid = flags & BPF_TIMER_CLOCK_MASK; + u64 bpf_timer_flags = flags & BPF_TIMER_FLAGS_MASK; struct bpf_hrtimer *t; int ret = 0; @@ -1168,7 +1215,7 @@ BPF_CALL_3(bpf_timer_init, struct bpf_timer_kern *, timer, struct bpf_map *, map if (in_nmi()) return -EOPNOTSUPP; - if (flags >= MAX_CLOCKS || + if (bpf_timer_flags & ~(BPF_F_TIMER_SLEEPABLE) || /* similar to timerfd except _ALARM variants are not supported */ (clockid != CLOCK_MONOTONIC && clockid != CLOCK_REALTIME && @@ -1190,7 +1237,10 @@ BPF_CALL_3(bpf_timer_init, struct bpf_timer_kern *, timer, struct bpf_map *, map t->map = map; t->prog = NULL; rcu_assign_pointer(t->callback_fn, NULL); + rcu_assign_pointer(t->sleepable_cb_fn, NULL); + /* FIXME: probably do something about the SLEEPABLE flag */ hrtimer_init(&t->timer, clockid, HRTIMER_MODE_REL_SOFT); + INIT_WORK(&t->work, bpf_timer_work_cb); t->timer.function = bpf_timer_cb; WRITE_ONCE(timer->timer, t); /* Guarantee the order between timer->timer and map->usercnt. So @@ -1221,8 +1271,8 @@ static const struct bpf_func_proto bpf_timer_init_proto = { .arg3_type = ARG_ANYTHING, }; -BPF_CALL_3(bpf_timer_set_callback, struct bpf_timer_kern *, timer, void *, callback_fn, - struct bpf_prog_aux *, aux) +static int __bpf_timer_set_callback(struct bpf_timer_kern *timer, void *callback_fn, + struct bpf_prog_aux *aux, bool is_sleepable) { struct bpf_prog *prev, *prog = aux->prog; struct bpf_hrtimer *t; @@ -1260,12 +1310,24 @@ BPF_CALL_3(bpf_timer_set_callback, struct bpf_timer_kern *, timer, void *, callb bpf_prog_put(prev); t->prog = prog; } - rcu_assign_pointer(t->callback_fn, callback_fn); + if (is_sleepable) { + rcu_assign_pointer(t->sleepable_cb_fn, callback_fn); + rcu_assign_pointer(t->callback_fn, NULL); + } else { + rcu_assign_pointer(t->callback_fn, callback_fn); + rcu_assign_pointer(t->sleepable_cb_fn, NULL); + } out: __bpf_spin_unlock_irqrestore(&timer->lock); return ret; } +BPF_CALL_3(bpf_timer_set_callback, struct bpf_timer_kern *, timer, void *, callback_fn, + struct bpf_prog_aux *, aux) +{ + return __bpf_timer_set_callback(timer, callback_fn, aux, false); +} + static const struct bpf_func_proto bpf_timer_set_callback_proto = { .func = bpf_timer_set_callback, .gpl_only = true, @@ -1353,6 +1415,7 @@ BPF_CALL_1(bpf_timer_cancel, struct bpf_timer_kern *, timer) * if it was running. */ ret = ret ?: hrtimer_cancel(&t->timer); + ret = ret ?: cancel_work_sync(&t->work); return ret; } @@ -1407,6 +1470,8 @@ void bpf_timer_cancel_and_free(void *val) */ if (this_cpu_read(hrtimer_running) != t) hrtimer_cancel(&t->timer); + + cancel_work_sync(&t->work); kfree(t); } @@ -2542,6 +2607,33 @@ __bpf_kfunc void bpf_throw(u64 cookie) WARN(1, "A call to BPF exception callback should never return\n"); } +/* FIXME: use kernel doc style */ +/* Description + * Configure the timer to call *callback_fn* static function in a + * sleepable context. + * Return + * 0 on success. + * **-EINVAL** if *timer* was not initialized with bpf_timer_init() earlier. + * **-EPERM** if *timer* is in a map that doesn't have any user references. + * The user space should either hold a file descriptor to a map with timers + * or pin such map in bpffs. When map is unpinned or file descriptor is + * closed all timers in the map will be cancelled and freed. + */ +__bpf_kfunc int bpf_timer_set_sleepable_cb(struct bpf_timer_kern *timer, + int (callback_fn)(void *map, int *key, struct bpf_timer *timer)) +{ + struct bpf_throw_ctx ctx = {}; + + /* FIXME: definietely not sure this is OK */ + arch_bpf_stack_walk(bpf_stack_walker, &ctx); + WARN_ON_ONCE(!ctx.aux); + + if (!ctx.aux) + return -EINVAL; + + return __bpf_timer_set_callback(timer, (void *)callback_fn, ctx.aux, true); +} + __bpf_kfunc_end_defs(); BTF_KFUNCS_START(generic_btf_ids) @@ -2573,6 +2665,7 @@ BTF_ID_FLAGS(func, bpf_task_get_cgroup1, KF_ACQUIRE | KF_RCU | KF_RET_NULL) #endif BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_throw) +BTF_ID_FLAGS(func, bpf_timer_set_sleepable_cb) BTF_KFUNCS_END(generic_btf_ids) static const struct btf_kfunc_id_set generic_kfunc_set = { diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 7831adba9abf..67da3f7bddb5 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -503,6 +503,8 @@ static bool is_dynptr_ref_function(enum bpf_func_id func_id) static bool is_sync_callback_calling_kfunc(u32 btf_id); static bool is_bpf_throw_kfunc(struct bpf_insn *insn); +static bool is_bpf_timer_set_sleepable_cb_kfunc(u32 btf_id); + static bool is_sync_callback_calling_function(enum bpf_func_id func_id) { return func_id == BPF_FUNC_for_each_map_elem || @@ -513,7 +515,8 @@ static bool is_sync_callback_calling_function(enum bpf_func_id func_id) static bool is_async_callback_calling_function(enum bpf_func_id func_id) { - return func_id == BPF_FUNC_timer_set_callback; + return func_id == BPF_FUNC_timer_set_callback || + is_bpf_timer_set_sleepable_cb_kfunc(func_id); } static bool is_callback_calling_function(enum bpf_func_id func_id) @@ -1414,6 +1417,7 @@ static int copy_verifier_state(struct bpf_verifier_state *dst_state, } dst_state->speculative = src->speculative; dst_state->active_rcu_lock = src->active_rcu_lock; + dst_state->in_sleepable = src->in_sleepable; dst_state->curframe = src->curframe; dst_state->active_lock.ptr = src->active_lock.ptr; dst_state->active_lock.id = src->active_lock.id; @@ -2413,6 +2417,7 @@ static struct bpf_verifier_state *push_async_cb(struct bpf_verifier_env *env, * Initialize it similar to do_check_common(). */ elem->st.branches = 1; + elem->st.in_sleepable = env->subprog_info[subprog].is_sleepable; frame = kzalloc(sizeof(*frame), GFP_KERNEL); if (!frame) goto err; @@ -5257,7 +5262,8 @@ static int map_kptr_match_type(struct bpf_verifier_env *env, static bool in_sleepable(struct bpf_verifier_env *env) { - return env->prog->aux->sleepable; + return env->prog->aux->sleepable || + (env->cur_state && env->cur_state->in_sleepable); } /* The non-sleepable programs and sleepable programs with explicit bpf_rcu_read_lock() @@ -5439,6 +5445,26 @@ static int check_map_access(struct bpf_verifier_env *env, u32 regno, return -EACCES; } break; + case BPF_TIMER: + /* FIXME: kptr does the above, should we use the same? */ + if (src != ACCESS_DIRECT) { + verbose(env, "bpf_timer cannot be accessed indirectly by helper\n"); + return -EACCES; + } + if (!tnum_is_const(reg->var_off)) { + verbose(env, "bpf_timer access cannot have variable offset\n"); + return -EACCES; + } + if (p != off + reg->var_off.value) { + verbose(env, "bpf_timer access misaligned expected=%u off=%llu\n", + p, off + reg->var_off.value); + return -EACCES; + } + if (size != bpf_size_to_bytes(BPF_DW)) { + verbose(env, "bpf_timer access size must be BPF_DW\n"); + return -EACCES; + } + break; default: verbose(env, "%s cannot be accessed directly by load/store\n", btf_field_type_name(field->type)); @@ -9439,11 +9465,13 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins if (insn->code == (BPF_JMP | BPF_CALL) && insn->src_reg == 0 && - insn->imm == BPF_FUNC_timer_set_callback) { + (insn->imm == BPF_FUNC_timer_set_callback || + is_bpf_timer_set_sleepable_cb_kfunc(insn->imm))) { struct bpf_verifier_state *async_cb; /* there is no real recursion here. timer callbacks are async */ env->subprog_info[subprog].is_async_cb = true; + env->subprog_info[subprog].is_sleepable = is_bpf_timer_set_sleepable_cb_kfunc(insn->imm); async_cb = push_async_cb(env, env->subprog_info[subprog].start, insn_idx, subprog); if (!async_cb) @@ -10412,6 +10440,10 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn break; } + if (is_bpf_timer_set_sleepable_cb_kfunc(func_id)) + err = push_callback_call(env, insn, insn_idx, meta.subprogno, + set_timer_callback_state); + if (err) return err; @@ -10789,6 +10821,7 @@ enum { KF_ARG_LIST_NODE_ID, KF_ARG_RB_ROOT_ID, KF_ARG_RB_NODE_ID, + KF_ARG_TIMER_ID, }; BTF_ID_LIST(kf_arg_btf_ids) @@ -10797,6 +10830,7 @@ BTF_ID(struct, bpf_list_head) BTF_ID(struct, bpf_list_node) BTF_ID(struct, bpf_rb_root) BTF_ID(struct, bpf_rb_node) +BTF_ID(struct, bpf_timer_kern) static bool __is_kfunc_ptr_arg_type(const struct btf *btf, const struct btf_param *arg, int type) @@ -10840,6 +10874,12 @@ static bool is_kfunc_arg_rbtree_node(const struct btf *btf, const struct btf_par return __is_kfunc_ptr_arg_type(btf, arg, KF_ARG_RB_NODE_ID); } +static bool is_kfunc_arg_timer(const struct btf *btf, const struct btf_param *arg) +{ + bool ret = __is_kfunc_ptr_arg_type(btf, arg, KF_ARG_TIMER_ID); + return ret; +} + static bool is_kfunc_arg_callback(struct bpf_verifier_env *env, const struct btf *btf, const struct btf_param *arg) { @@ -10908,6 +10948,7 @@ enum kfunc_ptr_arg_type { KF_ARG_PTR_TO_RB_NODE, KF_ARG_PTR_TO_NULL, KF_ARG_PTR_TO_CONST_STR, + KF_ARG_PTR_TO_TIMER, }; enum special_kfunc_type { @@ -10934,6 +10975,7 @@ enum special_kfunc_type { KF_bpf_percpu_obj_drop_impl, KF_bpf_throw, KF_bpf_iter_css_task_new, + KF_bpf_timer_set_sleepable_cb, }; BTF_SET_START(special_kfunc_set) @@ -10960,6 +11002,7 @@ BTF_ID(func, bpf_throw) #ifdef CONFIG_CGROUPS BTF_ID(func, bpf_iter_css_task_new) #endif +BTF_ID(func, bpf_timer_set_sleepable_cb) BTF_SET_END(special_kfunc_set) BTF_ID_LIST(special_kfunc_list) @@ -10990,6 +11033,7 @@ BTF_ID(func, bpf_iter_css_task_new) #else BTF_ID_UNUSED #endif +BTF_ID(func, bpf_timer_set_sleepable_cb) static bool is_kfunc_ret_null(struct bpf_kfunc_call_arg_meta *meta) { @@ -11061,6 +11105,9 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env, if (is_kfunc_arg_const_str(meta->btf, &args[argno])) return KF_ARG_PTR_TO_CONST_STR; + if (is_kfunc_arg_timer(meta->btf, &args[argno])) + return KF_ARG_PTR_TO_TIMER; + if ((base_type(reg->type) == PTR_TO_BTF_ID || reg2btf_ids[base_type(reg->type)])) { if (!btf_type_is_struct(ref_t)) { verbose(env, "kernel function %s args#%d pointer type %s %s is not supported\n", @@ -11318,6 +11365,11 @@ static bool is_bpf_throw_kfunc(struct bpf_insn *insn) insn->imm == special_kfunc_list[KF_bpf_throw]; } +static bool is_bpf_timer_set_sleepable_cb_kfunc(u32 btf_id) +{ + return btf_id == special_kfunc_list[KF_bpf_timer_set_sleepable_cb]; +} + static bool is_rbtree_lock_required_kfunc(u32 btf_id) { return is_bpf_rbtree_api_kfunc(btf_id); @@ -11693,6 +11745,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ case KF_ARG_PTR_TO_CALLBACK: case KF_ARG_PTR_TO_REFCOUNTED_KPTR: case KF_ARG_PTR_TO_CONST_STR: + case KF_ARG_PTR_TO_TIMER: /* Trusted by default */ break; default: @@ -11973,6 +12026,9 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ if (ret) return ret; break; + case KF_ARG_PTR_TO_TIMER: + /* FIXME: should we do anything here? */ + break; } } @@ -15591,7 +15647,9 @@ static int visit_insn(int t, struct bpf_verifier_env *env) return DONE_EXPLORING; case BPF_CALL: - if (insn->src_reg == 0 && insn->imm == BPF_FUNC_timer_set_callback) + if (insn->src_reg == 0 && + (insn->imm == BPF_FUNC_timer_set_callback || + is_bpf_timer_set_sleepable_cb_kfunc(insn->imm))) /* Mark this call insn as a prune point to trigger * is_state_visited() check before call itself is * processed by __check_func_call(). Otherwise new @@ -16767,6 +16825,9 @@ static bool states_equal(struct bpf_verifier_env *env, if (old->active_rcu_lock != cur->active_rcu_lock) return false; + if (old->in_sleepable != cur->in_sleepable) + return false; + /* for states to be equal callsites have to be the same * and all frame states need to be equivalent */ @@ -19644,7 +19705,8 @@ static int do_misc_fixups(struct bpf_verifier_env *env) continue; } - if (insn->imm == BPF_FUNC_timer_set_callback) { + if (insn->imm == BPF_FUNC_timer_set_callback || + is_bpf_timer_set_sleepable_cb_kfunc(insn->imm)) { /* The verifier will process callback_fn as many times as necessary * with different maps and the register states prepared by * set_timer_callback_state will be accurate. From patchwork Wed Feb 14 17:18:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 201079 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp1386308dyb; Wed, 14 Feb 2024 09:47:16 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWc+l75TbGPtH7W/lDwKIK+GIxZlYyC2gtNwwfBTVeDt/W+G9XAZTgWWIxHpuzMjw8VCBLvgvHZDIfTdgqNU/i5dLorwg== X-Google-Smtp-Source: AGHT+IG4LLR7MVLJzWLbaWUXQLJbmYRXvPk1FGmgnVkIpYDeYPsgG+dvoVXEB8Eato5RUCXGtGap X-Received: by 2002:a05:6a20:c88d:b0:19e:a873:9253 with SMTP id hb13-20020a056a20c88d00b0019ea8739253mr3941701pzb.10.1707932836073; Wed, 14 Feb 2024 09:47:16 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707932836; cv=pass; d=google.com; s=arc-20160816; b=ROjzra97oiQiolpwgMxcd50gswqkVqHyZAmhWqkISlUVMWe/XTuCsqcX/wUq36HILb b1FCA6ULNtH6iUcGiHXCk2KA9fQ0rLVAdTfJQcFSPglXh/TCv7cSG4ZYHNdskEkYnXF0 Kc/rXzSmxOLi4ZcTNDJvZoIkXSPaaVesA5PM1Inw8UCcb1/RX480fAy1wsA9VSgLuPHl uXUJW2N0JerpJt/75IjAnOsx2oqQ4A1yj5+wEh0c8Poqf5IiMIqCMtilDzGH1qUKBzg5 lV4gGfpSj+kIplHNjSIgqFAO0d/7G1rmveDhRsXksvROUY70thK5wpayOUdVItST9jFT PmKQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=puiVmzL5J+PBnyPVVnJ11DTHB1EqCRuXQt+Sznv0bMo=; fh=+8yhlBdOzB5xZr35xwE9tYyKeOEOclQPNEAvxhFx/mY=; b=vhEbyO//LmqEfO+TraJFdhhEvVtVqwlZndxfrulXyysE+cI54Otic+ZoxJBWNSiLCd VXWc/7URz7fCQCPuGpipXscAhzBsjq6QuILjL73L4K08SzRmzLqTX2szI1ABzTiZHuCQ H/RB1Z4X4ul0ZguE7wWGobb4VpFE/lGk/UqwY8uKIt73XFKrVkd9P5jVCkXOiONgYcQD 9hF46SIOpqgam99tGfF9SeCNQZKrZEqioAmjTAQQNVSUM/aFeLkoCbXtF3TKJjPWyeST 0gvnlCGL7rG92ZA8mF+c1NqjITZ59PK4ZaOQhKoZ/q4V3VlgV/dgUPdZmmpBe/BsZJdr WJCA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=gSMJnCBh; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-65641-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65641-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=2; AJvYcCWqLbd7hd/thHVRl6e0MNnBvhpc5CuYXo/j1EJbm8WVwx73DrbbuZ85YDKkG/DzMwuy9u4P50f2eKihcwGa3RBuodW5kQ== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id k12-20020aa792cc000000b006e034ecf899si8644480pfa.390.2024.02.14.09.47.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 09:47:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-65641-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=gSMJnCBh; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-65641-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65641-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 05048B2D54C for ; Wed, 14 Feb 2024 17:20:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4F2938529F; Wed, 14 Feb 2024 17:19:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="gSMJnCBh" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B45B085268; Wed, 14 Feb 2024 17:19:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707931153; cv=none; b=Ce5JUzlmeQU+vgRiAJ83fLpL8rhA3A1/7LMd6TPwGDEILyKG5QtnLNeJRrDDdsvbk4DnIl189wwQ8/VT0nu7Pgt6+OB1MT8tSYiFyXLEl5kdheGSsZZL3ptci9sS5JjqL0HgRUHG2IQVJ/ZFAyZCSdJWItBP27+PYwwXeUC31WU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707931153; c=relaxed/simple; bh=4OdGUm9HFmtkn3vsq7JGFZKzQ/ITmuIGwj3G26a/QyQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=qXKlWwhG5pILVnym8bJIdZ+rYP6s1mVMaOeTLAlOVGqPeyt00l4v9lmKKaY1ofoEbFuuNiTSA8PcaXNwOwvtnM1/LUxmF7B+Qf+HQrpqKZqXDKfWOCgf3fuww5ziisoqlT8JCeBzKHujnc7OLSPWsvMuhjHs3MMHkSZtAb8YkQo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gSMJnCBh; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5D826C43609; Wed, 14 Feb 2024 17:19:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707931153; bh=4OdGUm9HFmtkn3vsq7JGFZKzQ/ITmuIGwj3G26a/QyQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=gSMJnCBho8MLq6HzBArgTrzpXHh3JvSoM1We081wZJHl7DcWe/piB3tfHYotLdPrL Lc1Tr/q0BQMqwX44lHF27ViFqWkv3bEeJV9em6Sy4MF2w0m29s1XPPeroi6rmJ+/BW kEimX8BLv/6EAgVcn74JbpLEvlcPipp+OR5BiBL95d2Z9FSnn1FM++LpGHi3//Txw8 dVbzOCnJe1JCbYHIVJsFKn1Xj6WMmqj/18Pnh7YxK8ncjkkHW9fuU6kSDuF7weCxZa v5w7OAcbxXR9PYiz+W4nJIx0aeiZu8+XE+5FtJWFud+Ln5QFvTS1JRE498kew1isXw 0VVNHr5xM0aug== From: Benjamin Tissoires Date: Wed, 14 Feb 2024 18:18:32 +0100 Subject: [PATCH RFC bpf-next v2 03/10] bpf/verifier: allow more maps in sleepable bpf programs Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240214-hid-bpf-sleepable-v2-3-5756b054724d@kernel.org> References: <20240214-hid-bpf-sleepable-v2-0-5756b054724d@kernel.org> In-Reply-To: <20240214-hid-bpf-sleepable-v2-0-5756b054724d@kernel.org> To: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Jiri Kosina , Benjamin Tissoires , Jonathan Corbet , Shuah Khan Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1707931135; l=1041; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=4OdGUm9HFmtkn3vsq7JGFZKzQ/ITmuIGwj3G26a/QyQ=; b=dqAI9nNqZJJSqL1WxlTEU41LIngJOXojPXunu816A7LhAL2wmgZqVLf71TIxKkD5v/TcP71l1 hyjLr7LL//CCJVyUI9mpcxpa840MSYCFWcS5Hpa7K/HzuzIprCpUJ0u X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790897381576352042 X-GMAIL-MSGID: 1790897381576352042 These 2 maps types are required for HID-BPF when a user wants to do IO with a device from a sleepable tracing point. Allowing BPF_MAP_TYPE_QUEUE (and therefore BPF_MAP_TYPE_STACK) allows for a BPF program to prepare from an IRQ the list of HID commands to send back to the device and then these commands can be retrieved from the sleepable trace point. Signed-off-by: Benjamin Tissoires --- changes in v2: - dropped BPF_MAP_TYPE_PROG_ARRAY from the list --- kernel/bpf/verifier.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 67da3f7bddb5..cb1266566b69 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -18094,6 +18094,8 @@ static int check_map_prog_compatibility(struct bpf_verifier_env *env, case BPF_MAP_TYPE_SK_STORAGE: case BPF_MAP_TYPE_TASK_STORAGE: case BPF_MAP_TYPE_CGRP_STORAGE: + case BPF_MAP_TYPE_QUEUE: + case BPF_MAP_TYPE_STACK: break; default: verbose(env, From patchwork Wed Feb 14 17:18:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 201057 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp1372098dyb; Wed, 14 Feb 2024 09:23:28 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUOrv9sXRZXUcQK4G3Dr9CDxLbEzRrvWc86zKYk28sfuXzBt6CV/buKFxr/nSgAqfQHhrB3P+ekqjfubMN/J9qH7muHjQ== X-Google-Smtp-Source: AGHT+IEOeMxUdmK5nEsBWTDezHY8FT2pdgSvjOskRTW9QrkvbWyUzWSOChpDJfPL4s1+DEMQF/VR X-Received: by 2002:a17:902:ec87:b0:1d9:f62b:237b with SMTP id x7-20020a170902ec8700b001d9f62b237bmr3749742plg.39.1707931408421; Wed, 14 Feb 2024 09:23:28 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707931408; cv=pass; d=google.com; s=arc-20160816; b=ZxkfCyVj3aznp5VBtvk9OyI6w4HHMjHn04Z2HIZM+znXKbKNnvXKTP4OyjOePRWndg 1rCJN1e6U0uJJ73EIsHGNPnKMN7IJARB1NchHclrmbuGYVBSvcKH6IFWy9B156KhMY0E rX+SCGKruLDlIzv0NUe/enBnm2LLDmWMtDeuxEs0H4exAj5U7Jt3eEmr9d5mIj4BJK+z Zpfr0SPgSJ/QH9HE5oHziCWg61yk7Hsv+uDOnUkh80z/HTHw5O4cwgGbDpFkrhqaa7z3 ewi26yQmQn0QOXSCRUn3Wo4+X+FpfjW+UOBrxNFXjzS9GvTUIBsOnrrKNzrigGmIpnX3 AcMA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=+WyySI7JlLqYT9S3JFYbHHS9lgu8Wp97AKLYMGJEVrM=; fh=NVUjwRExrvF+Nyxm9thuoaLRmlOeoQZy0zDMpv0+PgA=; b=wigImwZzwsWm56t8EEQBqR+uDs8a74nPtqQTAqwfjv1mLl+tR+lI53EjJB4D7pQrj3 lj5k3GQW+HQt04eiO5jv8vpjeg14jMq6orQ8AVN9oc+UqqV7o7vPS+3iD1TPoSI4v0kc AXywUBp5OZUEZTMy501XPPWcHWCh0a0pcazwEoKbc15988jHIdGg/DVtsIjlOzRxllCK x2ITQKaDjkYRXpuU9otJXI20Wxct4k1YbA9ALHN2eYOk8BNyvjs2db0LfOjm5aI866sM hTFRHlGvIXFXEY9ilzbGf9vqdUy7PMp8u3/rSOCsNJ0Doh9tujdbFn6HWRGpizmIJYWA PDjA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=DTfhx2e5; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-65642-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65642-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=2; AJvYcCXP3GgZ5k2/ch3XLITh9coEUVfGgnWEV/cSHQIHNIHCkH4fGqd37UCJR8pLtW+r681mOUWXYCBeLQ84C9LL8ASnfgX07w== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id i17-20020a17090332d100b001d9607d5cc5si3974863plr.244.2024.02.14.09.23.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 09:23:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-65642-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=DTfhx2e5; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-65642-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65642-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id E9D8F28F6DC for ; Wed, 14 Feb 2024 17:20:57 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D0C1D8592C; Wed, 14 Feb 2024 17:19:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="DTfhx2e5" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 215F08562E; Wed, 14 Feb 2024 17:19:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707931158; cv=none; b=kqCOZnCwfVhm3++fPV0UOzynlNyq7V2kUfV85MCFchMuvLvD9R1Gq20rqhLjFyONvuX1f8dc6mkWxM1+7+FrKgfL7f7ZYQEEvFlemckh/oTnCwxDNyF1dFqJbaZFK21y+OmBLPUVCkq5RQM00QkeRDW2XngKZ0192mucRtfpS6I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707931158; c=relaxed/simple; bh=O3ltY7Q/B4DHgoZ6KVi/nCnoo/71fgQwKeIFzP741cE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=IrF8jwKZH05X7Dh08LE03ueMGqwliewHkpfr5+iK0DhLwITPX9/g8aU2xrlu/Q1ZM+RdPafvU0TKliNKuuLx7YiC4mjWA2n5aFo9wFnFRpAF80sedyAKkL+ZmzFsRUMxlKYD4kodPko51LLoh48lYandgLfXLw9BZIWj+OfEyxg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DTfhx2e5; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id B4ED9C433C7; Wed, 14 Feb 2024 17:19:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707931157; bh=O3ltY7Q/B4DHgoZ6KVi/nCnoo/71fgQwKeIFzP741cE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=DTfhx2e5SylS0zcehe7DG3OP1vWu1FiIlI09I5Egawofm8PXxPbGHhRIilyCI43Ub AywulE4EDGZ0MSydfiA+A85xsVka4HNvrbh/XfWiWWBkKnYi4vR+IPyiCtAk9L1yho JUot14G4Yfk78kwe52caoghZ09aw1ibg3wvE5ptHo+FSoxvKjvMnEur3nni9iDFLJB 32fNAvw9OY/z0iS0NgmIzAdRA/pyYL+DLKh1PCZQzSHnNObSVS87n6HmbfUfgjWdYU q+vkH3wC15oHSgxeM/02IHmoQnidAVMzC0GBXxQsCN0x5F9eRqGI+Tf6xiv97HEgza 7UJbf6X1OMhcQ== From: Benjamin Tissoires Date: Wed, 14 Feb 2024 18:18:33 +0100 Subject: [PATCH RFC bpf-next v2 04/10] HID: bpf/dispatch: regroup kfuncs definitions Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240214-hid-bpf-sleepable-v2-4-5756b054724d@kernel.org> References: <20240214-hid-bpf-sleepable-v2-0-5756b054724d@kernel.org> In-Reply-To: <20240214-hid-bpf-sleepable-v2-0-5756b054724d@kernel.org> To: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Jiri Kosina , Benjamin Tissoires , Jonathan Corbet , Shuah Khan Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1707931135; l=3501; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=O3ltY7Q/B4DHgoZ6KVi/nCnoo/71fgQwKeIFzP741cE=; b=NPpI7dM5KQkR28VCTECGY5MMIIqX7Y9C4cVcFYhMh9k/WSPwTKQ583KLqY7yEaV7RMS/qyIbG oqbMUAZjuU0CQbcErX+jigEUVbrEilBiT4/FQtUqkKp0qrVIfqBt1zv X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790895884768808610 X-GMAIL-MSGID: 1790895884768808610 No code change, just move down the hid_bpf_get_data() kfunc definition so we have only one block of __bpf_kfunc_start/end_defs() Signed-off-by: Benjamin Tissoires --- no changes in v2 --- drivers/hid/bpf/hid_bpf_dispatch.c | 80 ++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 42 deletions(-) diff --git a/drivers/hid/bpf/hid_bpf_dispatch.c b/drivers/hid/bpf/hid_bpf_dispatch.c index e630caf644e8..52abb27426f4 100644 --- a/drivers/hid/bpf/hid_bpf_dispatch.c +++ b/drivers/hid/bpf/hid_bpf_dispatch.c @@ -143,48 +143,6 @@ u8 *call_hid_bpf_rdesc_fixup(struct hid_device *hdev, u8 *rdesc, unsigned int *s } EXPORT_SYMBOL_GPL(call_hid_bpf_rdesc_fixup); -/* Disables missing prototype warnings */ -__bpf_kfunc_start_defs(); - -/** - * hid_bpf_get_data - Get the kernel memory pointer associated with the context @ctx - * - * @ctx: The HID-BPF context - * @offset: The offset within the memory - * @rdwr_buf_size: the const size of the buffer - * - * @returns %NULL on error, an %__u8 memory pointer on success - */ -__bpf_kfunc __u8 * -hid_bpf_get_data(struct hid_bpf_ctx *ctx, unsigned int offset, const size_t rdwr_buf_size) -{ - struct hid_bpf_ctx_kern *ctx_kern; - - if (!ctx) - return NULL; - - ctx_kern = container_of(ctx, struct hid_bpf_ctx_kern, ctx); - - if (rdwr_buf_size + offset > ctx->allocated_size) - return NULL; - - return ctx_kern->data + offset; -} -__bpf_kfunc_end_defs(); - -/* - * The following set contains all functions we agree BPF programs - * can use. - */ -BTF_KFUNCS_START(hid_bpf_kfunc_ids) -BTF_ID_FLAGS(func, hid_bpf_get_data, KF_RET_NULL) -BTF_KFUNCS_END(hid_bpf_kfunc_ids) - -static const struct btf_kfunc_id_set hid_bpf_kfunc_set = { - .owner = THIS_MODULE, - .set = &hid_bpf_kfunc_ids, -}; - static int device_match_id(struct device *dev, const void *id) { struct hid_device *hdev = to_hid_device(dev); @@ -281,6 +239,31 @@ static int do_hid_bpf_attach_prog(struct hid_device *hdev, int prog_fd, struct b /* Disables missing prototype warnings */ __bpf_kfunc_start_defs(); +/** + * hid_bpf_get_data - Get the kernel memory pointer associated with the context @ctx + * + * @ctx: The HID-BPF context + * @offset: The offset within the memory + * @rdwr_buf_size: the const size of the buffer + * + * @returns %NULL on error, an %__u8 memory pointer on success + */ +__bpf_kfunc __u8 * +hid_bpf_get_data(struct hid_bpf_ctx *ctx, unsigned int offset, const size_t rdwr_buf_size) +{ + struct hid_bpf_ctx_kern *ctx_kern; + + if (!ctx) + return NULL; + + ctx_kern = container_of(ctx, struct hid_bpf_ctx_kern, ctx); + + if (rdwr_buf_size + offset > ctx->allocated_size) + return NULL; + + return ctx_kern->data + offset; +} + /** * hid_bpf_attach_prog - Attach the given @prog_fd to the given HID device * @@ -474,6 +457,19 @@ hid_bpf_hw_request(struct hid_bpf_ctx *ctx, __u8 *buf, size_t buf__sz, } __bpf_kfunc_end_defs(); +/* + * The following set contains all functions we agree BPF programs + * can use. + */ +BTF_KFUNCS_START(hid_bpf_kfunc_ids) +BTF_ID_FLAGS(func, hid_bpf_get_data, KF_RET_NULL) +BTF_KFUNCS_END(hid_bpf_kfunc_ids) + +static const struct btf_kfunc_id_set hid_bpf_kfunc_set = { + .owner = THIS_MODULE, + .set = &hid_bpf_kfunc_ids, +}; + /* our HID-BPF entrypoints */ BTF_SET8_START(hid_bpf_fmodret_ids) BTF_ID_FLAGS(func, hid_bpf_device_event) From patchwork Wed Feb 14 17:18:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 201058 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp1372288dyb; Wed, 14 Feb 2024 09:23:44 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWW1iludaABDRMg8/ldKbU0PirgCpgRWQzLEbptCQ63MnZn7iO9+Y0eCzo+0FNonutgONixiSZptHWzHhayeDszTG+X3Q== X-Google-Smtp-Source: AGHT+IG+JSK5id1vnaLnVQmBV7Ng/HXjBeAQ2JtFyuS64+zC0XkAcwrosEZra+mXYcSUR8cxs8hw X-Received: by 2002:aa7:d1cb:0:b0:561:2f3b:7218 with SMTP id g11-20020aa7d1cb000000b005612f3b7218mr2660571edp.35.1707931424541; Wed, 14 Feb 2024 09:23:44 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707931424; cv=pass; d=google.com; s=arc-20160816; b=RYRR71oFDtBxOPFyCylhs4IdUE/KI9yQdB93bq6vN39V4BCXrBZIcwQRxQCBIOCF+n jp0bF6nKCWxAXFgqivWHJ3dp1C2FWqYxmWQdmGMqymAhm7Mv/xjHkGb8/eQSSbr7k4UD xFsKtChz6kmQou/0dJPUR2SRx5Jg9O4nkA0gCrVJaUdGKzj+B1qJaHA/rTUpaHZ4Dqq+ 0cm+oRoORthLKzJanZh5gdGQSUMhwDSV+AAFEXaivJZ8hlH/FKGFzDyY9zSu3klNO+iB Mc3XH7UOVGDI5M5FbkokIujjqZaLz85rbxOzJjkxqVJqOVrh/7EnSZPKZVY8UfbNkbeW QU4w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=jnzHUxBGTijomsbaPlpsPMvnv89UndhUUh1UUVobuTA=; fh=0MPJ94euNF4Fma7x3S5ct4PKg0/xx7neHcpNb7DRqiA=; b=kdooW4gRpNQVUSW5g96VyfupQ43e+Ay4cmeEPLseOV2VMlHs+SsT+PvzXitF2MnCR8 YNfA+0dBNZKcy4IliuHCtoLrYoYyHM6GGlDWSQy0XqhuBAx5d15MNqkaT/RYaAp5adg5 kqrHuXA9E5VWwg069mAIbc7qV5fzPzkKGBAL4N1E0r7Y62yo7pr5IV9srGf4+MK5rHfu ZGFPXjTJW3vT8G8F6QDQEhyBBISwUS3PT87KxfdpgIpNF+8tiyeO3dugI3SZrOrQKLLb D8wHwQdHWYXmxbI2igsCRyEZbgFwKbg6JzNCMKPvOUd4CvD2haBzYEj97y1VYFsgGvi8 usbQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rWwirUt1; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-65643-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65643-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=2; AJvYcCWo+txQDpIYwkrtUkw5+aSyXMw56SWyIQlauXPF35tQ51TZWMNT7vR4NAGbAw4yZV6aJaZeQpXiPcB5pQvVOgml2b+I/Q== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id e8-20020a056402190800b00561e7d87078si2278673edz.559.2024.02.14.09.23.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 09:23:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-65643-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rWwirUt1; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-65643-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65643-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id A52FE1F21ABA for ; Wed, 14 Feb 2024 17:21:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B798285955; Wed, 14 Feb 2024 17:19:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="rWwirUt1" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7ABEC839FD; Wed, 14 Feb 2024 17:19:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707931162; cv=none; b=dUrnqtDrOyzuggarR8Qp7ynL/il+H8hsWFanyvsyIfXZMtpGb2B00y110PWBwWwC4KscGrFlt9fqX31sH5seyOQo8YxrsWvBHsLGsS4J1at08v9RwvMI/bhPk34A6uhpPsHxLK50KEdEAlenpaHxIXvrBVnF21V0O6vFIhTB7jc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707931162; c=relaxed/simple; bh=F43OnIh2rY49uodQcvLEcVEao1kg8gYHNVa0IY0wmEg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=WSrL6VmE1p4jkStHEDHmzA8RG0X618rkuSjwIU8ucVZQBH+SK0YbTfihZZXtF4k1F/ReyfxnKtDZw3JXSy+GdN+KwCH58L/S9IF4hY4CrxqPWgZf9jD0jV4fGjVJVbNqWNFYn2ij6FcLnMHoSmmLDb9YAICx8gjOWXrv3kLDwOg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rWwirUt1; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 19019C43390; Wed, 14 Feb 2024 17:19:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707931162; bh=F43OnIh2rY49uodQcvLEcVEao1kg8gYHNVa0IY0wmEg=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=rWwirUt1cgtXJksBNvsqHTO6dZFKiuox4F+JS6fr+OTAPntJpdztWShNuDVef4D8+ 1yFUjDGNwq35RlXuNEkV2abFuETi0oq5krfRnhYCcJX2LT0EYA662SPefgFbOL2FoM oDwboprdH9wgbpgdotLjlQYTdUSRoKED9DkE1Lqi6M4x0HKo9vZ1sQbvhLr/hIE878 lTrlmTWPDEubq/rBaQ+VbLZyJBE/kN5l+ZNKws68Sf1DJxogTIAOI9gxV+Gi4ye2bq u0uSkm7TX7VEAdGipIHhSSLgaY2x5spvNF8OA+fIKlApHFbm29OLVcbG0oettZ7y5b MacsE45b1BcQQ== From: Benjamin Tissoires Date: Wed, 14 Feb 2024 18:18:34 +0100 Subject: [PATCH RFC bpf-next v2 05/10] HID: bpf: export hid_hw_output_report as a BPF kfunc Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240214-hid-bpf-sleepable-v2-5-5756b054724d@kernel.org> References: <20240214-hid-bpf-sleepable-v2-0-5756b054724d@kernel.org> In-Reply-To: <20240214-hid-bpf-sleepable-v2-0-5756b054724d@kernel.org> To: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Jiri Kosina , Benjamin Tissoires , Jonathan Corbet , Shuah Khan Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1707931135; l=6507; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=F43OnIh2rY49uodQcvLEcVEao1kg8gYHNVa0IY0wmEg=; b=NOYZNeGqrOaGbIV6Bzq3s0T6oiR949jv9BwmZE0/hNIWzYdcaL9xWjE9oAPWANsttqQkkn/GW dX6WWCUj9LfAVanQCmkdoc3RKi8e3QY88gVDrAKAWlkguB1chwHqSee X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790895901560047285 X-GMAIL-MSGID: 1790895901560047285 We currently only export hid_hw_raw_request() as a BPF kfunc. However, some devices require an explicit write on the Output Report instead of the use of the control channel. So also export hid_hw_output_report to BPF Signed-off-by: Benjamin Tissoires --- no changes in v2 --- Documentation/hid/hid-bpf.rst | 2 +- drivers/hid/bpf/hid_bpf_dispatch.c | 112 +++++++++++++++++++++++++++---------- drivers/hid/hid-core.c | 1 + include/linux/hid_bpf.h | 1 + 4 files changed, 86 insertions(+), 30 deletions(-) diff --git a/Documentation/hid/hid-bpf.rst b/Documentation/hid/hid-bpf.rst index 4fad83a6ebc3..a575004d9025 100644 --- a/Documentation/hid/hid-bpf.rst +++ b/Documentation/hid/hid-bpf.rst @@ -179,7 +179,7 @@ Available API that can be used in syscall HID-BPF programs: ----------------------------------------------------------- .. kernel-doc:: drivers/hid/bpf/hid_bpf_dispatch.c - :functions: hid_bpf_attach_prog hid_bpf_hw_request hid_bpf_allocate_context hid_bpf_release_context + :functions: hid_bpf_attach_prog hid_bpf_hw_request hid_bpf_hw_output_report hid_bpf_allocate_context hid_bpf_release_context General overview of a HID-BPF program ===================================== diff --git a/drivers/hid/bpf/hid_bpf_dispatch.c b/drivers/hid/bpf/hid_bpf_dispatch.c index 52abb27426f4..a5b88b491b80 100644 --- a/drivers/hid/bpf/hid_bpf_dispatch.c +++ b/drivers/hid/bpf/hid_bpf_dispatch.c @@ -376,6 +376,46 @@ hid_bpf_release_context(struct hid_bpf_ctx *ctx) put_device(&hid->dev); } +static int +__hid_bpf_hw_check_params(struct hid_bpf_ctx *ctx, __u8 *buf, size_t *buf__sz, + enum hid_report_type rtype) +{ + struct hid_report_enum *report_enum; + struct hid_report *report; + struct hid_device *hdev; + u32 report_len; + + /* check arguments */ + if (!ctx || !hid_bpf_ops || !buf) + return -EINVAL; + + switch (rtype) { + case HID_INPUT_REPORT: + case HID_OUTPUT_REPORT: + case HID_FEATURE_REPORT: + break; + default: + return -EINVAL; + } + + if (*buf__sz < 1) + return -EINVAL; + + hdev = (struct hid_device *)ctx->hid; /* discard const */ + + report_enum = hdev->report_enum + rtype; + report = hid_bpf_ops->hid_get_report(report_enum, buf); + if (!report) + return -EINVAL; + + report_len = hid_report_len(report); + + if (*buf__sz > report_len) + *buf__sz = report_len; + + return 0; +} + /** * hid_bpf_hw_request - Communicate with a HID device * @@ -392,24 +432,14 @@ hid_bpf_hw_request(struct hid_bpf_ctx *ctx, __u8 *buf, size_t buf__sz, enum hid_report_type rtype, enum hid_class_request reqtype) { struct hid_device *hdev; - struct hid_report *report; - struct hid_report_enum *report_enum; + size_t size = buf__sz; u8 *dma_data; - u32 report_len; int ret; /* check arguments */ - if (!ctx || !hid_bpf_ops || !buf) - return -EINVAL; - - switch (rtype) { - case HID_INPUT_REPORT: - case HID_OUTPUT_REPORT: - case HID_FEATURE_REPORT: - break; - default: - return -EINVAL; - } + ret = __hid_bpf_hw_check_params(ctx, buf, &size, rtype); + if (ret) + return ret; switch (reqtype) { case HID_REQ_GET_REPORT: @@ -423,29 +453,16 @@ hid_bpf_hw_request(struct hid_bpf_ctx *ctx, __u8 *buf, size_t buf__sz, return -EINVAL; } - if (buf__sz < 1) - return -EINVAL; - hdev = (struct hid_device *)ctx->hid; /* discard const */ - report_enum = hdev->report_enum + rtype; - report = hid_bpf_ops->hid_get_report(report_enum, buf); - if (!report) - return -EINVAL; - - report_len = hid_report_len(report); - - if (buf__sz > report_len) - buf__sz = report_len; - - dma_data = kmemdup(buf, buf__sz, GFP_KERNEL); + dma_data = kmemdup(buf, size, GFP_KERNEL); if (!dma_data) return -ENOMEM; ret = hid_bpf_ops->hid_hw_raw_request(hdev, dma_data[0], dma_data, - buf__sz, + size, rtype, reqtype); @@ -455,6 +472,42 @@ hid_bpf_hw_request(struct hid_bpf_ctx *ctx, __u8 *buf, size_t buf__sz, kfree(dma_data); return ret; } + +/** + * hid_bpf_hw_output_report - Send an output report to a HID device + * + * @ctx: the HID-BPF context previously allocated in hid_bpf_allocate_context() + * @buf: a %PTR_TO_MEM buffer + * @buf__sz: the size of the data to transfer + * + * @returns the number of bytes transferred on success, a negative error code otherwise. + */ +__bpf_kfunc int +hid_bpf_hw_output_report(struct hid_bpf_ctx *ctx, __u8 *buf, size_t buf__sz) +{ + struct hid_device *hdev; + size_t size = buf__sz; + u8 *dma_data; + int ret; + + /* check arguments */ + ret = __hid_bpf_hw_check_params(ctx, buf, &size, HID_OUTPUT_REPORT); + if (ret) + return ret; + + hdev = (struct hid_device *)ctx->hid; /* discard const */ + + dma_data = kmemdup(buf, size, GFP_KERNEL); + if (!dma_data) + return -ENOMEM; + + ret = hid_bpf_ops->hid_hw_output_report(hdev, + dma_data, + size); + + kfree(dma_data); + return ret; +} __bpf_kfunc_end_defs(); /* @@ -488,6 +541,7 @@ BTF_ID_FLAGS(func, hid_bpf_attach_prog) BTF_ID_FLAGS(func, hid_bpf_allocate_context, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, hid_bpf_release_context, KF_RELEASE) BTF_ID_FLAGS(func, hid_bpf_hw_request) +BTF_ID_FLAGS(func, hid_bpf_hw_output_report) BTF_KFUNCS_END(hid_bpf_syscall_kfunc_ids) static const struct btf_kfunc_id_set hid_bpf_syscall_kfunc_set = { diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c index de7a477d6665..1243595890ba 100644 --- a/drivers/hid/hid-core.c +++ b/drivers/hid/hid-core.c @@ -2974,6 +2974,7 @@ EXPORT_SYMBOL_GPL(hid_check_keys_pressed); static struct hid_bpf_ops hid_ops = { .hid_get_report = hid_get_report, .hid_hw_raw_request = hid_hw_raw_request, + .hid_hw_output_report = hid_hw_output_report, .owner = THIS_MODULE, .bus_type = &hid_bus_type, }; diff --git a/include/linux/hid_bpf.h b/include/linux/hid_bpf.h index 7118ac28d468..5c7ff93dc73e 100644 --- a/include/linux/hid_bpf.h +++ b/include/linux/hid_bpf.h @@ -103,6 +103,7 @@ struct hid_bpf_ops { unsigned char reportnum, __u8 *buf, size_t len, enum hid_report_type rtype, enum hid_class_request reqtype); + int (*hid_hw_output_report)(struct hid_device *hdev, __u8 *buf, size_t len); struct module *owner; const struct bus_type *bus_type; }; From patchwork Wed Feb 14 17:18:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 201059 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp1372394dyb; Wed, 14 Feb 2024 09:23:56 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWv0dYiV4j1Rudd2g5BtKNBiLRHGcAzs9Tpy4YZ92siOljeK3nN4WBVZ+s80eXE4CE4mqEDiR6PxOa7B0RzEj+caQptzA== X-Google-Smtp-Source: AGHT+IELUDyRjokFARVjf4sRS4epFZLkUDcaC9wpZ84XYW9lqSv44bhrNhBkhGr98No5D0H9gXyy X-Received: by 2002:a17:902:e742:b0:1db:6aa9:fadd with SMTP id p2-20020a170902e74200b001db6aa9faddmr1634693plf.65.1707931436348; Wed, 14 Feb 2024 09:23:56 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707931436; cv=pass; d=google.com; s=arc-20160816; b=THZ4/U8atAZuRBOVPrGCdJUhsit+AK87pz8fkbk9fZ+ne1k6guhjTQQQcvJKYMvitL 4iwS01YywrQ1OjcF7LE+acRC3bUxzuEu7hjJy44Ey+kwYLaKCBkZMvMK2LpeMwASBOo8 5B3SB/e2yDAwfX4RrZjSfxZpve2wst/w42C+r8tg8kcMELRu6srSFuarWRjnBikGao2T SIOKC5F2z5RvbjbvfgGUKnFg5SoKdhpoHXdg/FhDY78isAHzYl6+dvnfFyff4GfN5/+h J7Tr3UUfeKs4LSpQly9QPO5U0gXm90VTcf8EKhovuAtFnZWi3YtpfQ83UuRoWKPwfguJ UnZw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=wmRip7rbuASIEiCJw6fma2NUD9A4u8Lk8VNf00DXtKo=; fh=yd/ErpmtOMXxrjqiw0JtZUCaw/422bY+3gmpxjc4Ku0=; b=ongdrydzT97XWS72jZisWXoqRCYDMdt8ymT4dCtL7BFY91oX4gg6XnLG9nnFRCv8sq NOXV9EMbXSPRUOQzP7a7Er/8iaOJUp09tJrfQfDiQ1UWt9CB4izj3ADlNubWSGrcFJAh Mkm/94EmbETbrYBSz6qV+Xd0XC/keQ9dLrUvJqH0KXkAgsiEmGEQsTTTF3Z2BB4TyqI1 WD4aBisqBnftEi8PIC1PpL/NOXhj3BpJiIDgYSlJucj3hXdVfBBqe2GyEQ0gwI9wxAwj q63jkQbrb8j+covbKKOrRchQKk09gAiDiei60QIauEnHvvD5lZHjeWC6qp9wjtWCvcGu tdhA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=H2zabjis; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-65644-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65644-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=2; AJvYcCVaIoK0Pr19O46sO2JpnaiGx9i6jzHqHIO+S5zcaXknQMccu1NI05xEai0PgmVfMzbC74kF9wvutVgcm008dsi8KX0MPQ== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id ma15-20020a170903094f00b001db2ac74e8fsi3695534plb.381.2024.02.14.09.23.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 09:23:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-65644-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=H2zabjis; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-65644-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65644-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id BF06B290453 for ; Wed, 14 Feb 2024 17:21:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 09CF383A15; Wed, 14 Feb 2024 17:19:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="H2zabjis" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E10EC83A0D; Wed, 14 Feb 2024 17:19:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707931167; cv=none; b=Fel07LpRkIvN+qR2c8KXIbnqYTQG7ixLeRXJsSgETNX7EcQMe9c+CXZIqn/NZpetbne5523JorV7/sc55isP9CLgqpFeVWLl2EF9GGEiomc3k3YQ9udr1pzHYDAKpWNvrjWbuwouJMOw5Aq5nhM7U7JeSoiE/xvDTnaIJ4aV4Kw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707931167; c=relaxed/simple; bh=u4iXnzdoA4mM6lNoRPFZIX10P/cAGd0s0q48JL6pfuo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=no83B1vawPKCDMI4pTgouGLb5jsI5qgdsacs3sQBYeFfvZ8R6giOAx3MmuNI95rkma+wEZ7IK+7dTgMTQ7Q2zdMf6EpJCFqTdBQgnGXZD31CCqzrji9LNq6LtRrkVU+l+4Xub7QMG9OIXIauWrK8sKULaHuMj80c73Y7ETx6uGg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=H2zabjis; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6FAF9C433C7; Wed, 14 Feb 2024 17:19:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707931166; bh=u4iXnzdoA4mM6lNoRPFZIX10P/cAGd0s0q48JL6pfuo=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=H2zabjisaWGRVZESJnRayQZrBXL4OYIm1FeDg8Kl3f3ZNvmQoUxWRp31ewazAudXR x1IZI97KaV9FYL8A6kUuYmRjdLAnYqQbndv0e2JJzNRLjnmPK1sulxf2aYrqcuHPSK /H9BOEEtc3RZJUqS4//rYGU1mS8SrGU/JcXIvOyayj9V5E4G7M+CqM4jHBy4r2TCLy 7NFeKF9dYxkGutyhh/lFuUjWyc9cAFmaC0rxWTn8p6e7aSLT3rwyG5wrLZlSLzuhYY ELKD0aLiILzvCZ2X0/i7O1UCARIQ3ABPjtZf7lu4npFgHErBTZyRNb1i80YwCZMa73 8Hu3jN17Qc5Hg== From: Benjamin Tissoires Date: Wed, 14 Feb 2024 18:18:35 +0100 Subject: [PATCH RFC bpf-next v2 06/10] selftests/hid: Add test for hid_bpf_hw_output_report Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240214-hid-bpf-sleepable-v2-6-5756b054724d@kernel.org> References: <20240214-hid-bpf-sleepable-v2-0-5756b054724d@kernel.org> In-Reply-To: <20240214-hid-bpf-sleepable-v2-0-5756b054724d@kernel.org> To: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Jiri Kosina , Benjamin Tissoires , Jonathan Corbet , Shuah Khan Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1707931135; l=5125; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=u4iXnzdoA4mM6lNoRPFZIX10P/cAGd0s0q48JL6pfuo=; b=t3jtm7WgNcD5AFL7lQvZQ6FGh87fIV2KVForxhXJo9RVtvnzXFEKCRagnTBVfDNMQhLsfHMIR 7u5OoUszUXjDpODiB5ppJm3/oPLJzSk/N9DEYw4aaVdIH946+gFUjMC X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790895913565563669 X-GMAIL-MSGID: 1790895913565563669 This time we need to ensure uhid receives it, thus the new mutex and condition. Signed-off-by: Benjamin Tissoires --- no changes in v2 --- tools/testing/selftests/hid/hid_bpf.c | 63 ++++++++++++++++++++++ tools/testing/selftests/hid/progs/hid.c | 24 +++++++++ .../testing/selftests/hid/progs/hid_bpf_helpers.h | 2 + 3 files changed, 89 insertions(+) diff --git a/tools/testing/selftests/hid/hid_bpf.c b/tools/testing/selftests/hid/hid_bpf.c index 2cf96f818f25..8332014838b0 100644 --- a/tools/testing/selftests/hid/hid_bpf.c +++ b/tools/testing/selftests/hid/hid_bpf.c @@ -16,6 +16,11 @@ #define SHOW_UHID_DEBUG 0 +#define min(a, b) \ + ({ __typeof__(a) _a = (a); \ + __typeof__(b) _b = (b); \ + _a < _b ? _a : _b; }) + static unsigned char rdesc[] = { 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */ 0x09, 0x21, /* Usage (Vendor Usage 0x21) */ @@ -111,6 +116,10 @@ struct hid_hw_request_syscall_args { static pthread_mutex_t uhid_started_mtx = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t uhid_started = PTHREAD_COND_INITIALIZER; +static pthread_mutex_t uhid_output_mtx = PTHREAD_MUTEX_INITIALIZER; +static pthread_cond_t uhid_output_cond = PTHREAD_COND_INITIALIZER; +static unsigned char output_report[10]; + /* no need to protect uhid_stopped, only one thread accesses it */ static bool uhid_stopped; @@ -205,6 +214,13 @@ static int uhid_event(struct __test_metadata *_metadata, int fd) break; case UHID_OUTPUT: UHID_LOG("UHID_OUTPUT from uhid-dev"); + + pthread_mutex_lock(&uhid_output_mtx); + memcpy(output_report, + ev.u.output.data, + min(ev.u.output.size, sizeof(output_report))); + pthread_cond_signal(&uhid_output_cond); + pthread_mutex_unlock(&uhid_output_mtx); break; case UHID_GET_REPORT: UHID_LOG("UHID_GET_REPORT from uhid-dev"); @@ -733,6 +749,53 @@ TEST_F(hid_bpf, test_hid_change_report) ASSERT_EQ(buf[2], 0) TH_LOG("leftovers_from_previous_test"); } +/* + * Call hid_bpf_hw_output_report against the given uhid device, + * check that the program is called and does the expected. + */ +TEST_F(hid_bpf, test_hid_user_output_report_call) +{ + struct hid_hw_request_syscall_args args = { + .retval = -1, + .size = 10, + }; + DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattrs, + .ctx_in = &args, + .ctx_size_in = sizeof(args), + ); + int err, cond_err, prog_fd; + struct timespec time_to_wait; + + LOAD_BPF; + + args.hid = self->hid_id; + args.data[0] = 1; /* report ID */ + args.data[1] = 2; /* report ID */ + args.data[2] = 42; /* report ID */ + + prog_fd = bpf_program__fd(self->skel->progs.hid_user_output_report); + + pthread_mutex_lock(&uhid_output_mtx); + + memset(output_report, 0, sizeof(output_report)); + clock_gettime(CLOCK_REALTIME, &time_to_wait); + time_to_wait.tv_sec += 2; + + err = bpf_prog_test_run_opts(prog_fd, &tattrs); + cond_err = pthread_cond_timedwait(&uhid_output_cond, &uhid_output_mtx, &time_to_wait); + + ASSERT_OK(err) TH_LOG("error while calling bpf_prog_test_run_opts"); + ASSERT_OK(cond_err) TH_LOG("error while calling waiting for the condition"); + + ASSERT_EQ(args.retval, 3); + + ASSERT_EQ(output_report[0], 1); + ASSERT_EQ(output_report[1], 2); + ASSERT_EQ(output_report[2], 42); + + pthread_mutex_unlock(&uhid_output_mtx); +} + /* * Attach hid_user_raw_request to the given uhid device, * call the bpf program from userspace diff --git a/tools/testing/selftests/hid/progs/hid.c b/tools/testing/selftests/hid/progs/hid.c index 1e558826b809..2c2b679a83b1 100644 --- a/tools/testing/selftests/hid/progs/hid.c +++ b/tools/testing/selftests/hid/progs/hid.c @@ -101,6 +101,30 @@ int hid_user_raw_request(struct hid_hw_request_syscall_args *args) return 0; } +SEC("syscall") +int hid_user_output_report(struct hid_hw_request_syscall_args *args) +{ + struct hid_bpf_ctx *ctx; + const size_t size = args->size; + int i, ret = 0; + + if (size > sizeof(args->data)) + return -7; /* -E2BIG */ + + ctx = hid_bpf_allocate_context(args->hid); + if (!ctx) + return -1; /* EPERM check */ + + ret = hid_bpf_hw_output_report(ctx, + args->data, + size); + args->retval = ret; + + hid_bpf_release_context(ctx); + + return 0; +} + static const __u8 rdesc[] = { 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ 0x09, 0x32, /* USAGE (Z) */ diff --git a/tools/testing/selftests/hid/progs/hid_bpf_helpers.h b/tools/testing/selftests/hid/progs/hid_bpf_helpers.h index 65e657ac1198..50c6a0d5765e 100644 --- a/tools/testing/selftests/hid/progs/hid_bpf_helpers.h +++ b/tools/testing/selftests/hid/progs/hid_bpf_helpers.h @@ -94,5 +94,7 @@ extern int hid_bpf_hw_request(struct hid_bpf_ctx *ctx, size_t buf__sz, enum hid_report_type type, enum hid_class_request reqtype) __ksym; +extern int hid_bpf_hw_output_report(struct hid_bpf_ctx *ctx, + __u8 *buf, size_t buf__sz) __ksym; #endif /* __HID_BPF_HELPERS_H */ From patchwork Wed Feb 14 17:18:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 201061 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp1372650dyb; Wed, 14 Feb 2024 09:24:21 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWqstLxq3y8BeXt9LCPU6feXMt6J+rZpeH+iNxhuwipS+/5EoQuy6rNdS/XwrpbT57+kc0eFCfjNphOCo0XRp0+4EHqiw== X-Google-Smtp-Source: AGHT+IHzVk1jfHdYuoK0mkoPM0t6CvFwtexGi3wVJ2rWNMBS4hTA8Faqt6dDxo51B7WCBNgsHVCP X-Received: by 2002:a05:6a00:10c4:b0:6e0:4b8a:7ab7 with SMTP id d4-20020a056a0010c400b006e04b8a7ab7mr3402820pfu.21.1707931461158; Wed, 14 Feb 2024 09:24:21 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707931461; cv=pass; d=google.com; s=arc-20160816; b=bghKA+kCCzpdr86AjsEkQX0CUDFfQO4ZuJbftlqJj935iuIYMIS8LkLWvbekDL81Kn 0eWwLdqQHd4r2YH4SSKDS72eWnGGODOG8wppOCyMEf3Y13NzgEms1WY5HS6ezxZ8zBr2 IbZJNuDAkVNwG0YAYvH4teYEqWr642Xea2Iw57N3R1RoddOQzBLbEe87xEghdUwmjm7/ LAZrgDJmZ2Dj7OOzacyuyDHjOQmuAvMdVaSaHjTHDXX7nHocpaHxHgxc/GAzRMv3h1em njGdNeMigXUcUg0A5h8Zg9K0pIWf3Mx0iQgH1bqjj4k0dAAs4Sku3pwGxGHI/sOkV4KM Pb/w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=O+DKMbL72Lr/atMAqah+aNJPLbCQ5KsP8qCtCuX8JQQ=; fh=PWWiX/ypFtRpBJmIB+F+kg0+f+0YwqmgG96dIadx2ho=; b=mBhfrwdwdcOijdKpthe0UKQgIw4V2G8axcQC1oz75GKNTEO+Vd1IOc6yFx83CxGNPo rtbD39/AsX8P4QQcvmRUZR4Ji76mO8eGZSWpVqZaRMkTeYj1KckDEDLjSbKTlPLRcImh q2/lqAaQz50RM74ihSb5jjcDkMPqHzAbfcPA+UVz9r21XhxQxeRPHYgUaE+BFVHzHrI2 5KvNVuI9AIxl+Yv0r+Vt8B8kbwMxQCtDChLbtTG44cF9VRrXqynnKwcoGY4VmorKdEyX TuGC/PzuO3eFUoviDWRESJKAd19XwDcw/YvXGz8JXcCbsGjh1iReViWRNxaJ6fEeJy3M l0Yg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=tV2C0zJw; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-65645-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65645-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=2; AJvYcCUzc+k0X5mgjweqM6jDF8QXrdoeJXEEXBaEZEwn4hNTN9j9/Xjd4qOZAV/gNAHjRPZ30bETuIsK+fnjasWXS4oePCy1+w== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id x7-20020a656aa7000000b005dc962cfb59si1058645pgu.350.2024.02.14.09.24.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 09:24:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-65645-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=tV2C0zJw; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-65645-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65645-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 269C1290A14 for ; Wed, 14 Feb 2024 17:22:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 38763126F06; Wed, 14 Feb 2024 17:19:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="tV2C0zJw" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4EE957C6C9; Wed, 14 Feb 2024 17:19:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707931172; cv=none; b=ZSOQqQa+qaRzBepbepLi/rMt05TULqC+wRON8HCJSIYqNtgpkaDBGRqBWwGZgX/WguN74q52H0u5K49hCHPfjjuNev1ExEoq8Qkc4tzKjsXNUnc+VKHEdV+MbocfGJf19U+3YJflGB57+AGhZZdW0utw96lWOmcRvqp31m6vJXw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707931172; c=relaxed/simple; bh=115krBgQOqzJZjZdyNNKcB5U5M5YB6JFsKKfrcL2Tvk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=YfIjtd26p2DcINGL7Nn+YDqGf86UbIp0KfCA5JqOaQKQJ4FiAahq88wOh40jxupMaNOJ+/TB5BeafMsb8dJgpGf2zTA3af+kxhuGakuOAWkbQmSwt9/SLlR8Muw8m2ihx4dx9fvOeTYGnuLQQIsDMxvwW+PDmNFj/h5lH0GLeZs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=tV2C0zJw; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id C93A6C433B2; Wed, 14 Feb 2024 17:19:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707931170; bh=115krBgQOqzJZjZdyNNKcB5U5M5YB6JFsKKfrcL2Tvk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=tV2C0zJwTuTSLaaG+ibFTQEDbUerBwpdbMCakLK4kbhIKY9skMvvrh8rMYRE28X/g /TLbNt+/B5WuExuxiest5LVVW55VcISiGfB/MlJkxTbd7yce1NSXYfrVmPOsF2GHx0 AuwSNdEyKDbYtpbHYBpXmlfz/ZudSiTrTrFbAcvuw2OiBm5SzYF7ugrNRmW9F9ztkP LgwCphHsFwUeePAxMvBLFeYb7K4yF6ZvBZLrFHhF9l/h/MH3VwDmKjuPVUbhuT3N71 vgW/NdOpf+UJ+OOiLZvLG3KpVWlZL9TtNWxsvjfeOgu9P15wc726409Evv8opz15N+ 0ONM4l8iMKB/A== From: Benjamin Tissoires Date: Wed, 14 Feb 2024 18:18:36 +0100 Subject: [PATCH RFC bpf-next v2 07/10] HID: bpf: allow to inject HID event from BPF Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240214-hid-bpf-sleepable-v2-7-5756b054724d@kernel.org> References: <20240214-hid-bpf-sleepable-v2-0-5756b054724d@kernel.org> In-Reply-To: <20240214-hid-bpf-sleepable-v2-0-5756b054724d@kernel.org> To: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Jiri Kosina , Benjamin Tissoires , Jonathan Corbet , Shuah Khan Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1707931135; l=4122; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=115krBgQOqzJZjZdyNNKcB5U5M5YB6JFsKKfrcL2Tvk=; b=HVw4zRUildvxD5pMQxd9fTMjiDfEZoxXyNhUOD6ObQFqlvkdTsNZ/7ssEzYTAyrIovRtJtIlS XmdTpzLivhHAl+AFVwkn15Dy3zT92gNxx4JgV95moDIY7d2x49M6Vhd X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790895939951683349 X-GMAIL-MSGID: 1790895939951683349 It can be interesting to inject events from BPF as if the event were to come from the device. For example, some multitouch devices do not all the time send a proximity out event, and we might want to send it for the physical device. Compared to uhid, we can now inject events on any physical device, not just uhid virtual ones. Signed-off-by: Benjamin Tissoires --- no changes in v2 --- Documentation/hid/hid-bpf.rst | 2 +- drivers/hid/bpf/hid_bpf_dispatch.c | 29 +++++++++++++++++++++++++++++ drivers/hid/hid-core.c | 1 + include/linux/hid_bpf.h | 2 ++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Documentation/hid/hid-bpf.rst b/Documentation/hid/hid-bpf.rst index a575004d9025..0765b3298ecf 100644 --- a/Documentation/hid/hid-bpf.rst +++ b/Documentation/hid/hid-bpf.rst @@ -179,7 +179,7 @@ Available API that can be used in syscall HID-BPF programs: ----------------------------------------------------------- .. kernel-doc:: drivers/hid/bpf/hid_bpf_dispatch.c - :functions: hid_bpf_attach_prog hid_bpf_hw_request hid_bpf_hw_output_report hid_bpf_allocate_context hid_bpf_release_context + :functions: hid_bpf_attach_prog hid_bpf_hw_request hid_bpf_hw_output_report hid_bpf_input_report hid_bpf_allocate_context hid_bpf_release_context General overview of a HID-BPF program ===================================== diff --git a/drivers/hid/bpf/hid_bpf_dispatch.c b/drivers/hid/bpf/hid_bpf_dispatch.c index a5b88b491b80..e1a650f4a626 100644 --- a/drivers/hid/bpf/hid_bpf_dispatch.c +++ b/drivers/hid/bpf/hid_bpf_dispatch.c @@ -508,6 +508,34 @@ hid_bpf_hw_output_report(struct hid_bpf_ctx *ctx, __u8 *buf, size_t buf__sz) kfree(dma_data); return ret; } + +/** + * hid_bpf_input_report - Inject a HID report in the kernel from a HID device + * + * @ctx: the HID-BPF context previously allocated in hid_bpf_allocate_context() + * @type: the type of the report (%HID_INPUT_REPORT, %HID_FEATURE_REPORT, %HID_OUTPUT_REPORT) + * @buf: a %PTR_TO_MEM buffer + * @buf__sz: the size of the data to transfer + * + * @returns %0 on success, a negative error code otherwise. + */ +__bpf_kfunc int +hid_bpf_input_report(struct hid_bpf_ctx *ctx, enum hid_report_type type, u8 *buf, + const size_t buf__sz) +{ + struct hid_device *hdev; + size_t size = buf__sz; + int ret; + + /* check arguments */ + ret = __hid_bpf_hw_check_params(ctx, buf, &size, type); + if (ret) + return ret; + + hdev = (struct hid_device *)ctx->hid; /* discard const */ + + return hid_input_report(hdev, type, buf, size, 0); +} __bpf_kfunc_end_defs(); /* @@ -542,6 +570,7 @@ BTF_ID_FLAGS(func, hid_bpf_allocate_context, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, hid_bpf_release_context, KF_RELEASE) BTF_ID_FLAGS(func, hid_bpf_hw_request) BTF_ID_FLAGS(func, hid_bpf_hw_output_report) +BTF_ID_FLAGS(func, hid_bpf_input_report) BTF_KFUNCS_END(hid_bpf_syscall_kfunc_ids) static const struct btf_kfunc_id_set hid_bpf_syscall_kfunc_set = { diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c index 1243595890ba..b1fa0378e8f4 100644 --- a/drivers/hid/hid-core.c +++ b/drivers/hid/hid-core.c @@ -2975,6 +2975,7 @@ static struct hid_bpf_ops hid_ops = { .hid_get_report = hid_get_report, .hid_hw_raw_request = hid_hw_raw_request, .hid_hw_output_report = hid_hw_output_report, + .hid_input_report = hid_input_report, .owner = THIS_MODULE, .bus_type = &hid_bus_type, }; diff --git a/include/linux/hid_bpf.h b/include/linux/hid_bpf.h index 5c7ff93dc73e..17b08f500098 100644 --- a/include/linux/hid_bpf.h +++ b/include/linux/hid_bpf.h @@ -104,6 +104,8 @@ struct hid_bpf_ops { size_t len, enum hid_report_type rtype, enum hid_class_request reqtype); int (*hid_hw_output_report)(struct hid_device *hdev, __u8 *buf, size_t len); + int (*hid_input_report)(struct hid_device *hid, enum hid_report_type type, + u8 *data, u32 size, int interrupt); struct module *owner; const struct bus_type *bus_type; }; From patchwork Wed Feb 14 17:18:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 201062 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp1372809dyb; Wed, 14 Feb 2024 09:24:38 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXLsMxABX2YuTNYXXQJheoIje7j25MdcmjRMqHYFJ50gULw6/L8705b3vi5eYMB/5bVX7UQNNgQZKY+faP9SeZC5w84mA== X-Google-Smtp-Source: AGHT+IGOintDo2F7ZDR6k8UwyOcEhfLo/ANUI5SwJy/Oz7xvbA9pr6yPPQgIRVbjbDprfbQkbZtg X-Received: by 2002:a05:6a21:3a87:b0:1a0:70db:43e5 with SMTP id zv7-20020a056a213a8700b001a070db43e5mr1657461pzb.5.1707931478028; Wed, 14 Feb 2024 09:24:38 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707931478; cv=pass; d=google.com; s=arc-20160816; b=TyySuKdoogS2LhT4/6P9jMjpuM8UFwomS+fdPyUp2SsdILQ+LUZHL97lvwggvIGA69 gIGWtTyuqaGBqvS51H2ttfk2xPRrt5kEJKlbmzRWYzubgeXvEvbp2tOqWnLfCZAiKXJj FA7ShS5/MYWkqfkE8XjdVruTpCVbIZoV3OvZFxARz1emjffV4EF+YgTdR3s4VuKkN2fj Tyx1ME5bzKr3Lav4SDCNrobqhB1JhOkXwN/z4zqeTPG3kSlv2b0Y5DikKAvrAkCCByFS LDjjvAI1wbVs2Ddu4TBRTQ+vZEALFHuce8m4CRpzbKaRfg2/nScyTlsCXRLp+DhLmVI+ jiBA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=WAUzfG6hYgBcOOGAYlUUPkzDYccZxvYPsLBr1ZdP/hs=; fh=698MDQ+r6VOgyBxcKOw8SdWV5XKXov7ABtfDpXTRaro=; b=RVs+3HEKct7VGfL7M3ZqwNldZfRJk+lrCrxuqqCvGjfY0KOpC1QoOH85neJL+u8SX6 ZSj5MofVUUFlv0R8e30Lt/eToMpl1wychLuhOFCzfOZynFzI1FWpZR1+z6mWSguqDxt8 W9Th28+U38P/dqIOxnNmhPdSj6VV8x4paPmTIqyxoXzDxAd9TNRh9xFxoSRB5XfqDPGq w8upbFYWNEpvLLaTEEbjfbbom/5QfJJO5/vMqpTvXvJxO7aX+vP77eXCtNlu+xtLdtWG f1x7WQNDQiddaKLYBkzWntp2mIhU6UME3hP4Wh3cb4acKFp5mNVCBqcAkUBeFJid4EZJ aurw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=hjHMlKL5; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-65647-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65647-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=2; AJvYcCWYnC0DTtrMz4we7vHqdSuyFz3q3Yo4/Rnfe5y9Aq+ig28zrWMJ4tCWPZODBnFGetsEYt6rNmdaH6CYWauSctI74UJz7A== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id c21-20020a056a000ad500b006e03381d1ddsi8932921pfl.343.2024.02.14.09.24.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 09:24:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-65647-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=hjHMlKL5; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-65647-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65647-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 926372866B1 for ; Wed, 14 Feb 2024 17:22:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 97EC91272B9; Wed, 14 Feb 2024 17:19:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="hjHMlKL5" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A291286635; Wed, 14 Feb 2024 17:19:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707931175; cv=none; b=Yx1QaHOQyCfyyLwC7pHzA5CCwv8vyOJwjuvIPqDO+rKfNQulnWR4L60pWH9B7jKktNVocZ+zPk68hcPqQyX4IoMZeVdjvZrQSrRBefTjXHpJYS4dKQ2SEIIR7/e/0p6NtURD1/7ofH7q7/fVAJ5WCH1L++NxijsXUpwlP1kZXw4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707931175; c=relaxed/simple; bh=/pTARDtbfM9ZCY8QYIJ5HFNo8IvYrXNQ2OhOc+QCWlE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=lx2UwsnVsquWwCWKq5LJROL2OOZENR9DOVVmdvH9lmfe+l/Ej9tDdKv7xIWNMvfWnsJFLfV7/ZsVy7NkVLO2f/meTGuLFYUgKpA5g6nweJFdlC2qlZLF6h8da3SA8PzB0yr6Y5nnWCtz8UnYOSxPyieboYqJtq94hr/FrSnm5L4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hjHMlKL5; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2CCA7C433F1; Wed, 14 Feb 2024 17:19:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707931175; bh=/pTARDtbfM9ZCY8QYIJ5HFNo8IvYrXNQ2OhOc+QCWlE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=hjHMlKL5v533+lxXsepilERKuJP0vI5wZnKexrHsEk16eWjwAGAh3HKGr8uK0y5Av vRQkk1ZcIYw+ZGEZEOWaYwTq0b/YDnUjOsyl3e/KfineoEYC983Yu7GvhVmSIBLzbx Z7oTG2PYY3UDHg8FQ+e2NkjSd/FJ92Rc5VYAiP9pjeplXWKFE+mzPKYcmksULLJ6uT c+14mu5FOqIuzPLqLlncT5+gcpy054dgL4wLVnI+Df6TWcjoDDU38+cG8cdffZ9hNY 5dlZWSTpGKboRxki1CPdEk5SLvW0v9huIkQ+0siIm68k+gsw+Pju3SkEVNxUptlXb3 qktflmntflJEw== From: Benjamin Tissoires Date: Wed, 14 Feb 2024 18:18:37 +0100 Subject: [PATCH RFC bpf-next v2 08/10] selftests/hid: add tests for hid_bpf_input_report Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240214-hid-bpf-sleepable-v2-8-5756b054724d@kernel.org> References: <20240214-hid-bpf-sleepable-v2-0-5756b054724d@kernel.org> In-Reply-To: <20240214-hid-bpf-sleepable-v2-0-5756b054724d@kernel.org> To: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Jiri Kosina , Benjamin Tissoires , Jonathan Corbet , Shuah Khan Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1707931135; l=4206; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=/pTARDtbfM9ZCY8QYIJ5HFNo8IvYrXNQ2OhOc+QCWlE=; b=0e+T6btGJ2ogRaR6qMnKUh8WvJ9REB9kjkC3JsahqEIX/AkTon8LzNinPzSMManOE0T/+3O82 EcoHAveRee2Dd3pj5VtCyjmz8X3KQ0g51uFOgF76N96kKls0hpYYLzA X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790895957853126224 X-GMAIL-MSGID: 1790895957853126224 Usual way of testing, we call the function and ensures we receive the event Signed-off-by: Benjamin Tissoires --- no changes in v2 --- tools/testing/selftests/hid/hid_bpf.c | 49 +++++++++++++++++++++- tools/testing/selftests/hid/progs/hid.c | 22 ++++++++++ .../testing/selftests/hid/progs/hid_bpf_helpers.h | 4 ++ 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/hid/hid_bpf.c b/tools/testing/selftests/hid/hid_bpf.c index 8332014838b0..f825623e3edc 100644 --- a/tools/testing/selftests/hid/hid_bpf.c +++ b/tools/testing/selftests/hid/hid_bpf.c @@ -749,6 +749,52 @@ TEST_F(hid_bpf, test_hid_change_report) ASSERT_EQ(buf[2], 0) TH_LOG("leftovers_from_previous_test"); } +/* + * Call hid_bpf_input_report against the given uhid device, + * check that the program is called and does the expected. + */ +TEST_F(hid_bpf, test_hid_user_input_report_call) +{ + struct hid_hw_request_syscall_args args = { + .retval = -1, + .size = 10, + }; + DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattrs, + .ctx_in = &args, + .ctx_size_in = sizeof(args), + ); + __u8 buf[10] = {0}; + int err, prog_fd; + + LOAD_BPF; + + args.hid = self->hid_id; + args.data[0] = 1; /* report ID */ + args.data[1] = 2; /* report ID */ + args.data[2] = 42; /* report ID */ + + prog_fd = bpf_program__fd(self->skel->progs.hid_user_input_report); + + /* check that there is no data to read from hidraw */ + memset(buf, 0, sizeof(buf)); + err = read(self->hidraw_fd, buf, sizeof(buf)); + ASSERT_EQ(err, -1) TH_LOG("read_hidraw"); + + err = bpf_prog_test_run_opts(prog_fd, &tattrs); + + ASSERT_OK(err) TH_LOG("error while calling bpf_prog_test_run_opts"); + + ASSERT_EQ(args.retval, 0); + + /* read the data from hidraw */ + memset(buf, 0, sizeof(buf)); + err = read(self->hidraw_fd, buf, sizeof(buf)); + ASSERT_EQ(err, 6) TH_LOG("read_hidraw"); + ASSERT_EQ(buf[0], 1); + ASSERT_EQ(buf[1], 2); + ASSERT_EQ(buf[2], 42); +} + /* * Call hid_bpf_hw_output_report against the given uhid device, * check that the program is called and does the expected. @@ -797,8 +843,7 @@ TEST_F(hid_bpf, test_hid_user_output_report_call) } /* - * Attach hid_user_raw_request to the given uhid device, - * call the bpf program from userspace + * Call hid_hw_raw_request against the given uhid device, * check that the program is called and does the expected. */ TEST_F(hid_bpf, test_hid_user_raw_request_call) diff --git a/tools/testing/selftests/hid/progs/hid.c b/tools/testing/selftests/hid/progs/hid.c index 2c2b679a83b1..f67d35def142 100644 --- a/tools/testing/selftests/hid/progs/hid.c +++ b/tools/testing/selftests/hid/progs/hid.c @@ -125,6 +125,28 @@ int hid_user_output_report(struct hid_hw_request_syscall_args *args) return 0; } +SEC("syscall") +int hid_user_input_report(struct hid_hw_request_syscall_args *args) +{ + struct hid_bpf_ctx *ctx; + const size_t size = args->size; + int i, ret = 0; + + if (size > sizeof(args->data)) + return -7; /* -E2BIG */ + + ctx = hid_bpf_allocate_context(args->hid); + if (!ctx) + return -1; /* EPERM check */ + + ret = hid_bpf_input_report(ctx, HID_INPUT_REPORT, args->data, size); + args->retval = ret; + + hid_bpf_release_context(ctx); + + return 0; +} + static const __u8 rdesc[] = { 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ 0x09, 0x32, /* USAGE (Z) */ diff --git a/tools/testing/selftests/hid/progs/hid_bpf_helpers.h b/tools/testing/selftests/hid/progs/hid_bpf_helpers.h index 50c6a0d5765e..9cd56821d0f1 100644 --- a/tools/testing/selftests/hid/progs/hid_bpf_helpers.h +++ b/tools/testing/selftests/hid/progs/hid_bpf_helpers.h @@ -96,5 +96,9 @@ extern int hid_bpf_hw_request(struct hid_bpf_ctx *ctx, enum hid_class_request reqtype) __ksym; extern int hid_bpf_hw_output_report(struct hid_bpf_ctx *ctx, __u8 *buf, size_t buf__sz) __ksym; +extern int hid_bpf_input_report(struct hid_bpf_ctx *ctx, + enum hid_report_type type, + __u8 *data, + size_t buf__sz) __ksym; #endif /* __HID_BPF_HELPERS_H */ From patchwork Wed Feb 14 17:18:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 201060 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp1372660dyb; Wed, 14 Feb 2024 09:24:22 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVEbL7pnoei2fMEAfM0u0WGmdxIs0ZpsyAPwYZCpT9R5ydpQDHtT7lJMFvofQ2gSs6VflkEkSAk6MnGoIdhAPD0DKxHgQ== X-Google-Smtp-Source: AGHT+IGImXVfLRBiHTiDrtgn6ym5WiYYUfwY+er88Dhc295i6bjJuk2bWyNj4KPd8Y62BM4SvMxd X-Received: by 2002:a17:906:ae09:b0:a3b:7216:3091 with SMTP id le9-20020a170906ae0900b00a3b72163091mr2120622ejb.20.1707931461978; Wed, 14 Feb 2024 09:24:21 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707931461; cv=pass; d=google.com; s=arc-20160816; b=WvAHJ4i6acbKjIYA7dK7XBL2gBBYhfB4KhyFUzpNXux2EirsJzyQqCFb5yeMzAn+Oy vHHB98l+Y1XhkO9iXdrkw+GQKyKU2MWGae3lugNHyOzbLo+bH2KQopYDHLeMzauKGqwz +72/D1IJ13JgNc8+I/4sGoMvfIKgLetT72viqCrSPoLzs2j9GC/ocIJt/VNW09dZ9Say DDygAq39k71USkPW87tDXWtt1MMLu1gYyDIMdvqKs1hKbISragkq+QIUyBjXoxEkE1+j k4ylS2hoO3ZcwMT+SqtzIhtLSQpWdbSxW+yI6TLflBRSdEXeoO05Pm9N8Z737CmyTpsQ fYzw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=gjGY5geS4OiEaSZiUGSjAM0fkHIbgTUcyt/EXdNKreE=; fh=j9lW6cJnrEuwV7ywyQ506azVQ1uipiaLSKr4dPqAxlM=; b=A+eekFUbBti411KlX2dhqqcQ1N1HaKK0Nnhw1UJh7siBWaih+QDDAw6Kyt8sofxqnk ol9JzLJEIa4eNFlfBQhDdmVSI3zKilSZhbPTeEL4s1zNBXJc7ExKLUeceDdPHZ5SkxFE mTo/NI7RyOn+/yMB7XFSitD+Nk7hEkkQavKLCct5GdWl9EYyPJdxV1FTSTUnrXv8aNsY GBSvWBHOcaGXF7WbT4dsCl5+XRIXl9kMs+UUj44jbwIQX1GzZjNjLxke5/59nmmNr3+w Ho4lDcMdgbo1VnldV0zLTAtprBX6xp6cNXcDDVEk2pAEAPL8LptdGG61cvfW6UtGyKbF apZA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WwuavARF; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-65648-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65648-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=2; AJvYcCUzpIQdH/o4ZItRuA0ksLIzZvTvsLSNvA7M1tHh4Jqhcrc7ACyhCDv3z+XKNkxhB/ZAsM6XgqC9U50eJdlJ8DJwLC6o3A== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id x19-20020a170906299300b00a3d7651b979si73497eje.155.2024.02.14.09.24.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 09:24:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-65648-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WwuavARF; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-65648-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65648-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 622AF1F2B884 for ; Wed, 14 Feb 2024 17:22:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 97C59127B43; Wed, 14 Feb 2024 17:19:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="WwuavARF" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 035271272BC; Wed, 14 Feb 2024 17:19:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707931180; cv=none; b=qbsjaG8HbPpqGEqQpyPOg0f1/f0iF0FG5dou+g4mxPO4OovAl37Xl4zrfQKJ2M6WELZexCEYpOBV+qfmiHSFolJLkNx/MYBhGHaq+cPMoKnlzQlNaY4Djc2Op1zZ3upj1vnHrRAkiqrwPMTTOAXY+5ti/vUBvPxoU53jZU23i0A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707931180; c=relaxed/simple; bh=DWfwQbRXjVrD14SVINGcgMqWHxYUtxveD0dIZGefNe4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=LQ/J66KsTazjDVy37hiloOGdOcW5hM2f2Iz1UYwQlqc685YSDsQ7HFVazlRC0tsq4I4QSsH1WJrFMtB+iz4QGsaIfTNXiOKZpfh8ID45gFdS29ts5ANazUzbyogBKbhdotdDpQHzBF1vC0fl7ZsSXm5BNFROfmGRoioTzq0cfw0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=WwuavARF; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 85F5EC433C7; Wed, 14 Feb 2024 17:19:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707931179; bh=DWfwQbRXjVrD14SVINGcgMqWHxYUtxveD0dIZGefNe4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=WwuavARF9I4Kdb+4gH2EMa1VpbiqZ7jEt5VqMfxhR415jxzUl69jx8RzI7Zj/sJpv iVtRtXzlQjrLr8w4Cjp5DAdY+RrgW5xtBg4JfWrrkkddtTLvdQXm1uMeGHhNhxW6KR FgN+LrCZjHJcm4P9CNrW2jnKK7b4yM8ijgGc+YM9OHhXPkqS54W/bL5gZ9G7g36dlS P2rDXwWaNxF2pJ8TfaZ9Y+kQMJqm2WG0+fWzK9q5ZC9InbIZzTSYQI+hwIVo/0gOZd qYRPDQwjyImkvN0l/dAvDa9EvIOi2VDYLHMabLLzLceoi/48pQIRnkb+SOdAPYUFBa OrhogM1VX11CQ== From: Benjamin Tissoires Date: Wed, 14 Feb 2024 18:18:38 +0100 Subject: [PATCH RFC bpf-next v2 09/10] HID: bpf: allow to use bpf_timer_set_sleepable_cb() in tracing callbacks. Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240214-hid-bpf-sleepable-v2-9-5756b054724d@kernel.org> References: <20240214-hid-bpf-sleepable-v2-0-5756b054724d@kernel.org> In-Reply-To: <20240214-hid-bpf-sleepable-v2-0-5756b054724d@kernel.org> To: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Jiri Kosina , Benjamin Tissoires , Jonathan Corbet , Shuah Khan Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1707931135; l=2187; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=DWfwQbRXjVrD14SVINGcgMqWHxYUtxveD0dIZGefNe4=; b=SWDa0TnPBLVOiw7xrbgzrI8gf6Q6gEt3YUt/IUl1nisfdqR0LGrlAAsx9WOkI9wfLB5WdB3Ei sUQwYt8/cYdC/He9YxXi7bmTJb/e+Yrfb3r3OEFYB1DjGlTehEb7Wk0 X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790895940354693470 X-GMAIL-MSGID: 1790895940354693470 Export the sleepable kfuncs we have on HID-BPF in tracing bpf programs, but with the condition of being used in a sleepable context. This allows to use the bpf_timer when used in a sleepable context through bpf_timer_set_sleepable_cb() and initiate work from a device event. Signed-off-by: Benjamin Tissoires --- new in v2 --- drivers/hid/bpf/hid_bpf_dispatch.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/drivers/hid/bpf/hid_bpf_dispatch.c b/drivers/hid/bpf/hid_bpf_dispatch.c index e1a650f4a626..275f2057c48d 100644 --- a/drivers/hid/bpf/hid_bpf_dispatch.c +++ b/drivers/hid/bpf/hid_bpf_dispatch.c @@ -544,6 +544,11 @@ __bpf_kfunc_end_defs(); */ BTF_KFUNCS_START(hid_bpf_kfunc_ids) BTF_ID_FLAGS(func, hid_bpf_get_data, KF_RET_NULL) +BTF_ID_FLAGS(func, hid_bpf_allocate_context, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE) +BTF_ID_FLAGS(func, hid_bpf_release_context, KF_RELEASE | KF_SLEEPABLE) +BTF_ID_FLAGS(func, hid_bpf_hw_request, KF_SLEEPABLE) +BTF_ID_FLAGS(func, hid_bpf_hw_output_report, KF_SLEEPABLE) +BTF_ID_FLAGS(func, hid_bpf_input_report, KF_SLEEPABLE) BTF_KFUNCS_END(hid_bpf_kfunc_ids) static const struct btf_kfunc_id_set hid_bpf_kfunc_set = { @@ -566,11 +571,11 @@ static const struct btf_kfunc_id_set hid_bpf_fmodret_set = { /* for syscall HID-BPF */ BTF_KFUNCS_START(hid_bpf_syscall_kfunc_ids) BTF_ID_FLAGS(func, hid_bpf_attach_prog) -BTF_ID_FLAGS(func, hid_bpf_allocate_context, KF_ACQUIRE | KF_RET_NULL) -BTF_ID_FLAGS(func, hid_bpf_release_context, KF_RELEASE) -BTF_ID_FLAGS(func, hid_bpf_hw_request) -BTF_ID_FLAGS(func, hid_bpf_hw_output_report) -BTF_ID_FLAGS(func, hid_bpf_input_report) +BTF_ID_FLAGS(func, hid_bpf_allocate_context, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE) +BTF_ID_FLAGS(func, hid_bpf_release_context, KF_RELEASE | KF_SLEEPABLE) +BTF_ID_FLAGS(func, hid_bpf_hw_request, KF_SLEEPABLE) +BTF_ID_FLAGS(func, hid_bpf_hw_output_report, KF_SLEEPABLE) +BTF_ID_FLAGS(func, hid_bpf_input_report, KF_SLEEPABLE) BTF_KFUNCS_END(hid_bpf_syscall_kfunc_ids) static const struct btf_kfunc_id_set hid_bpf_syscall_kfunc_set = { From patchwork Wed Feb 14 17:18:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 201063 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp1373081dyb; Wed, 14 Feb 2024 09:25:06 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXNveEZsmOtfKU0xD+dnlmRoYTGmDDcb6lpGVVLY2u8F4bFEZ/H0Qtip9ihKUxjlFcNi9l22Qtl57lXdsCprfnidsFcgg== X-Google-Smtp-Source: AGHT+IHLsLiUepwlcVSoxQBYp4D94FEKywnEAtdOKQViMxqeEj8uLKGOAO7iXLT0ClCgwJCqhRSq X-Received: by 2002:a05:6a21:918b:b0:19e:8b31:f1ae with SMTP id tp11-20020a056a21918b00b0019e8b31f1aemr3993764pzb.29.1707931506496; Wed, 14 Feb 2024 09:25:06 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707931506; cv=pass; d=google.com; s=arc-20160816; b=jv9YAJa+S5pcpMQEHduIS6VIqURavqQJR6ICeW7jfziYGuRC4KX+uXpOf1M2IHNbhZ AQamNPr9Zk9+pszchAN7whtxB+gXZyTYETarD0ZA6M7W5wi17JKvRcffDtAcsy128PnO gZWCCyBRi9Fp/IX4DUC4NYRXPryfLAJAZobTPIhT1mXRqPT1bEQspNzvSGoBFbgPnd/Z pAf4tDlAGQm/kDR9PAlKueDTQMaUpX3rNprxT2Pqtev9cajjc+XnFbxnMkgPHvBFelQV 3Snf3BT//me0qvgVF7Mzm21ng6bqrtCytjm+v5KlSLeEzqley3c7t5edBv2OUubnLpO7 x8LA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=KrCwtNUHFNZx5w+V8qor5YI+HinYFJu0HOXmb3iOIGw=; fh=u4sMCkJ2HKxCtGH2DX2TcTV68vh/O/lPNKraU+ry9VU=; b=lyCE37qRKHmCfLNDh/5lE3gEpa5xUgi1FoHNBQfzUjTFp5IvSGjysGFDIkYMaM4vAb HFuSBHOm+PNxGbxyTWbQ/X4yH9nhqhj6xlttVe4PnPeq5s7wQKUJJalIbSYDeeFcLaE7 8P1GuEp18AA4v16SzB+2Ce1UpERz/3yWlOiy/aMkXBQU3dyFTiVwprHHU1E4hqfAxK7U aCkVQsctFRX5OiBypEP38JkDvzT/h2HcH4TL712Bn+rShTchGKweWsTMSmg3+VvL//nR rD/jihDthLZukBbMfgEX11j+02bJxxRwrYNLGvzpizfUClH9fBGN9tMSzkFiEfMs/VdL bHxg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=cDd1cxWi; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-65649-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65649-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=2; AJvYcCVpWROT5sNeyEzbKCN/Lgl3QHQMqxk13Q7HB4DQIQE1BFGFV1RgdQWGpaBtXx3aGCd3e2YEuYzEAV2wuGuxd4gD6OOZrg== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id s27-20020a63925b000000b005dc80f256ccsi3450119pgn.849.2024.02.14.09.25.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 09:25:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-65649-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=cDd1cxWi; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-65649-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65649-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 4558A28E2DF for ; Wed, 14 Feb 2024 17:23:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C4E70128363; Wed, 14 Feb 2024 17:19:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="cDd1cxWi" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4CF2C84A37; Wed, 14 Feb 2024 17:19:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707931184; cv=none; b=uzFCJB6fAKdegnj0pkNjPe+jl5yWfdi7f00nP6iZ4zY7++uDO3SCTkqWwjvvjJT9BwuyFKS92RO0SnzhJC9d6S1eGGX1YwzTL+RFkCfvK7MrehIrMzAoImKa+z/NpcPd6vujKPG6E+BEd2A1+N/GJT1/+6urjKj8F0eGU6KrYl8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707931184; c=relaxed/simple; bh=PU+I3eS6cHBmwm4SgiKJZyWi+Zr3RNPwKsVpYhPf5k4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=uCTIRNRPM36j9FqflKzn728hf9MwyzWoquREdhI0SNz1NWMbEUDym7UE13Mp64pbSurma8N15buQNeLyvOGeTfio95GIHMd7f7a+lHoA+uwzXBmLfxNBKydxcPoZlTBkPhrjl6z8c+RNCfswXCLYAJMjAUoOCmmPkRfCBsR0HOo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=cDd1cxWi; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id DE346C43390; Wed, 14 Feb 2024 17:19:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707931183; bh=PU+I3eS6cHBmwm4SgiKJZyWi+Zr3RNPwKsVpYhPf5k4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=cDd1cxWiYwELvwUBnoMX0vHoQZR/ez6dv80xmCyueLX0/x2GazDUS9oA/UL3mjeiE pFyVGBbkheN5coonqeYSCY7u1sO4qXIghbMg0Q2LhSKs+STmpUCu9uxJz4B+ACP27w rcTxQe6Mmw8sjG1tAj5PSjFm9dctljIESY2yPOIpI7EruAT20W1JG0BznO2MWMWLbD CjiE+9X5U2VBFc9TgJdf/LMiV6UxRdABdIXedXphD6DVKlV/K64gXzkkljHIU5RRP/ m1wM0EE40cw89CLZ/KloG3HGR/JNf5yqFPiNot7mAkXLb5NQMrOuRdnZWhVUSHXuUj GG2/vjkUzDo9Q== From: Benjamin Tissoires Date: Wed, 14 Feb 2024 18:18:39 +0100 Subject: [PATCH RFC bpf-next v2 10/10] selftests/hid: add test for bpf_timer Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240214-hid-bpf-sleepable-v2-10-5756b054724d@kernel.org> References: <20240214-hid-bpf-sleepable-v2-0-5756b054724d@kernel.org> In-Reply-To: <20240214-hid-bpf-sleepable-v2-0-5756b054724d@kernel.org> To: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Jiri Kosina , Benjamin Tissoires , Jonathan Corbet , Shuah Khan Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1707931135; l=8194; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=PU+I3eS6cHBmwm4SgiKJZyWi+Zr3RNPwKsVpYhPf5k4=; b=g91iULn/RIdO4qMTzjZNWDRByiGpz0YQYg4DYiv1FZen6lFdP319S6f1vChI7eDlxK9Rs/gvi mBIT7wy7dsNAKNt8g2Xt7D2TTYegHj2BneC8hl9vLSSXIowq/TDi0MX X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790895987481052879 X-GMAIL-MSGID: 1790895987481052879 This test checks that we can actually delay a workload in a sleepable context through bpf_timer. When an event is injected, we push it on a map of type queue and schedule a work. When that work kicks in, it pulls the event from the queue, and wakes up userspace through a ring buffer. The use of the ring buffer is there to not have sleeps in userspace because we have no guarantees of the timing of when those jobs will be called. Signed-off-by: Benjamin Tissoires --- new in v2 --- tools/testing/selftests/hid/hid_bpf.c | 83 +++++++++++ tools/testing/selftests/hid/progs/hid.c | 152 +++++++++++++++++++++ .../testing/selftests/hid/progs/hid_bpf_helpers.h | 2 + 3 files changed, 237 insertions(+) diff --git a/tools/testing/selftests/hid/hid_bpf.c b/tools/testing/selftests/hid/hid_bpf.c index f825623e3edc..c16efb43dd91 100644 --- a/tools/testing/selftests/hid/hid_bpf.c +++ b/tools/testing/selftests/hid/hid_bpf.c @@ -875,6 +875,89 @@ TEST_F(hid_bpf, test_hid_user_raw_request_call) ASSERT_EQ(args.data[1], 2); } +static __u8 workload_data; + +static int handle_event(void *ctx, void *data, size_t data_sz) +{ + const __u8 *e = data; + + workload_data = *e; + + return 0; +} + +TEST_F(hid_bpf, test_hid_schedule_work_defer_events_2) +{ + struct hid_hw_request_syscall_args args = { + .retval = -1, + .size = 10, + }; + DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattrs, + .ctx_in = &args, + .ctx_size_in = sizeof(args), + ); + const struct test_program progs[] = { + { .name = "hid_defer_bpf_timer" }, + }; + struct ring_buffer *rb = NULL; + __u8 buf[10] = {0}; + int prog_fd, err; + + LOAD_PROGRAMS(progs); + + /* Set up ring buffer polling */ + rb = ring_buffer__new(bpf_map__fd(self->skel->maps.rb), handle_event, NULL, NULL); + ASSERT_OK_PTR(rb) TH_LOG("Failed to create ring buffer"); + ASSERT_EQ(workload_data, 0); + + args.hid = self->hid_id; + prog_fd = bpf_program__fd(self->skel->progs.hid_setup_timer); + + err = bpf_prog_test_run_opts(prog_fd, &tattrs); + + ASSERT_OK(err) TH_LOG("error while calling bpf_prog_test_run_opts"); + + /* check that there is no data to read from hidraw */ + memset(buf, 0, sizeof(buf)); + err = read(self->hidraw_fd, buf, sizeof(buf)); + ASSERT_EQ(err, -1) TH_LOG("read_hidraw"); + + /* inject one event */ + buf[0] = 1; + buf[1] = 47; + buf[2] = 50; + uhid_send_event(_metadata, self->uhid_fd, buf, 6); + + err = ring_buffer__poll(rb, 100 /* timeout, ms */); + ASSERT_EQ(err, 1) TH_LOG("error while calling ring_buffer__poll"); + ASSERT_EQ(workload_data, 3); + + /* read the data from hidraw */ + memset(buf, 0, sizeof(buf)); + err = read(self->hidraw_fd, buf, sizeof(buf)); + ASSERT_EQ(err, 6) TH_LOG("read_hidraw"); + ASSERT_EQ(buf[0], 2); + ASSERT_EQ(buf[1], 3); + ASSERT_EQ(buf[2], 4) TH_LOG("leftovers_from_previous_test"); + + err = ring_buffer__poll(rb, 100 /* timeout, ms */); + ASSERT_EQ(err, 1) TH_LOG("error while calling ring_buffer__poll"); + ASSERT_EQ(workload_data, 4); + + /* read the data from hidraw */ + memset(buf, 0, sizeof(buf)); + err = read(self->hidraw_fd, buf, sizeof(buf)); + ASSERT_EQ(err, 6) TH_LOG("read_hidraw"); + ASSERT_EQ(buf[0], 2); + ASSERT_EQ(buf[1], 4); + ASSERT_EQ(buf[2], 6); + + /* read the data from hidraw */ + memset(buf, 0, sizeof(buf)); + err = read(self->hidraw_fd, buf, sizeof(buf)); + ASSERT_EQ(err, -1) TH_LOG("read_hidraw"); +} + /* * Attach hid_insert{0,1,2} to the given uhid device, * retrieve and open the matching hidraw node, diff --git a/tools/testing/selftests/hid/progs/hid.c b/tools/testing/selftests/hid/progs/hid.c index f67d35def142..05afa056167e 100644 --- a/tools/testing/selftests/hid/progs/hid.c +++ b/tools/testing/selftests/hid/progs/hid.c @@ -250,3 +250,155 @@ int BPF_PROG(hid_test_insert3, struct hid_bpf_ctx *hid_ctx) return 0; } + +struct test_report { + __u8 data[6]; +}; + +struct { + __uint(type, BPF_MAP_TYPE_QUEUE); + __uint(max_entries, 8); + __type(value, struct test_report); +} queue SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_RINGBUF); + __uint(max_entries, 8); +} rb SEC(".maps"); + +struct elem { + struct bpf_timer t; +}; + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(max_entries, 1024); + __type(key, u32); + __type(value, struct elem); +} timer_map SEC(".maps"); + +/* callback for timer_map timers */ + +static int timer_cb1(void *map, int *key, struct bpf_timer *timer) +{ + struct hid_bpf_ctx *hid_ctx; + struct test_report buf; + __u8 *rb_elem; + int err; + int i, ret = 0; + + /* do not pop the event, it'll be done in hid_offload_test() when + * notifying user space, this also allows to retry sending it + * if hid_bpf_input_report fails + */ + if (bpf_map_peek_elem(&queue, &buf)) + return 0; + + hid_ctx = hid_bpf_allocate_context(*key); + if (!hid_ctx) + return 0; /* EPERM check */ + + buf.data[0] = 2; + + /* re-inject the modified event into the HID stack */ + err = hid_bpf_input_report(hid_ctx, HID_INPUT_REPORT, buf.data, sizeof(buf.data)); + if (err == -16 /* -EBUSY */) { + /* + * This happens when we schedule the work with a 0 delay: + * the thread immediately starts but the current input + * processing hasn't finished yet. So the semaphore is + * already taken, and hid_input_report returns -EBUSY + */ + /* schedule another attempt */ + bpf_timer_start(timer, 5 * 1000, 0); + + goto out; + } + + if (bpf_map_pop_elem(&queue, &buf)) + goto out; + + rb_elem = bpf_ringbuf_reserve(&rb, sizeof(*rb_elem), 0); + if (!rb_elem) + goto out; + + *rb_elem = buf.data[1]; + + bpf_ringbuf_submit(rb_elem, 0); + + /* call ourself once again until there is no more events in the queue */ + bpf_timer_start(timer, 10 * 1000 * 1000, 0); + + out: + hid_bpf_release_context(hid_ctx); + return 0; +} + +#define CLOCK_MONOTONIC 1 + +SEC("?fmod_ret/hid_bpf_device_event") +int BPF_PROG(hid_defer_bpf_timer, struct hid_bpf_ctx *hctx) +{ + __u8 *data = hid_bpf_get_data(hctx, 0 /* offset */, 4 /* size */); + struct test_report buf = { + .data = {2, 3, 4, 5, 6, 7}, + }; + struct bpf_timer *timer; + int key = hctx->hid->id; + struct elem init = {}; + + if (!data) + return 0; /* EPERM check */ + + /* Only schedule a delayed work when reportID is 1, otherwise + * simply forward it to hidraw + */ + if (data[0] != 1) + return 0; + + bpf_map_push_elem(&queue, &buf, BPF_ANY); + buf.data[0] = 2; + buf.data[1] = 4; + buf.data[2] = 6; + bpf_map_push_elem(&queue, &buf, BPF_ANY); + + timer = bpf_map_lookup_elem(&timer_map, &key); + if (!timer) + return 3; + + bpf_timer_set_sleepable_cb(timer, timer_cb1); + + if (bpf_timer_start(timer, 5 * 1000 * 1000, 0) != 0) + return 2; + + return -1; /* discard the event */ +} + +SEC("syscall") +int hid_setup_timer(struct hid_hw_request_syscall_args *args) +{ + struct hid_bpf_ctx *ctx; + struct bpf_timer *timer; + struct elem init = {}; + int key = args->hid; + int i, ret = 0; + + ctx = hid_bpf_allocate_context(args->hid); + if (!ctx) + return -1; /* EPERM check */ + + bpf_map_update_elem(&timer_map, &key, &init, 0); + + timer = bpf_map_lookup_elem(&timer_map, &key); + if (!timer) { + hid_bpf_release_context(ctx); + return 1; + } + + bpf_timer_init(timer, &timer_map, CLOCK_MONOTONIC | BPF_F_TIMER_SLEEPABLE); + + hid_bpf_release_context(ctx); + + return 0; +} + diff --git a/tools/testing/selftests/hid/progs/hid_bpf_helpers.h b/tools/testing/selftests/hid/progs/hid_bpf_helpers.h index 9cd56821d0f1..8235a28e7dee 100644 --- a/tools/testing/selftests/hid/progs/hid_bpf_helpers.h +++ b/tools/testing/selftests/hid/progs/hid_bpf_helpers.h @@ -100,5 +100,7 @@ extern int hid_bpf_input_report(struct hid_bpf_ctx *ctx, enum hid_report_type type, __u8 *data, size_t buf__sz) __ksym; +extern int bpf_timer_set_sleepable_cb(struct bpf_timer *timer, + int (callback_fn)(void *map, int *key, struct bpf_timer *timer)) __ksym; #endif /* __HID_BPF_HELPERS_H */