From patchwork Wed Feb 21 16:25:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 204300 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp1145620dyc; Wed, 21 Feb 2024 08:26:02 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCV9bAk8jr/YHwUPq1hay6dokZLw5P12PZ3G6xA6uAGncoQFBv8hQXTTpjQxWTviTdufHUmK11ieV61KPl9v+Y9RX3aJWA== X-Google-Smtp-Source: AGHT+IEZv+QLbVGuK7xeHnBlVcfJMzlqU8LeERTjVag+4LqwT8wpwAGbkTZk9DOLetXqKwByAMjO X-Received: by 2002:a05:622a:1007:b0:42c:5d43:7e77 with SMTP id d7-20020a05622a100700b0042c5d437e77mr23818936qte.7.1708532761989; Wed, 21 Feb 2024 08:26:01 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708532761; cv=pass; d=google.com; s=arc-20160816; b=HGpQ5ndVQV9ENRji6L3seeooynELyH2FyWGj1qIsiZKO2d6Hg2W1m0aZvjPjvz301K q9NoRuUKBrsofCzAVp6P9NMSGr1SSRBSu8eCG32qGjie4AhDTbnB/UMSVZ2tuWL1RQIs JQtbhX/udsQN07NIHBARZ4XxOB4iiuz02pzJjMr5sqm8D1UZa3HR+Txgt51MepuHSsHI sNXGGYPc2zX29J++8GvGvmOPZF15N27Jd8ylT4jUlUyZcvRU3FVd3hGb8RnGsmSaPaYP 2fR2/gz9HQuZnU0gr0e6weg5xYZMqTKDkmfdwYJ9bir8WrctloO4Meh56zNZv82BTlAP ZoQg== 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=RpWV9faDa50F3MHYh31AkxBgCSGb3L/CNxUOfZiezwk=; fh=hc4Y6F7QBSV+XcCZzl4ZOgjmnikSFu9Au/ny4/yLvAg=; b=XJgtELxtObfrnIFPmVwJ5Zcl/PW7eciqgn9L7ZDV5UH6Ruqfqt0c+NNqQOP145rlPW XllOSohb25Koh/Gz/sUwxi1PgzJ2YkvJ/igKMBfXiA1Q1Cd8HVcdcX9H3N/R+qJ5HLhX j7U9ufdP1UTYLmF+MZwwLyH1Vke3xDL9iOiLLPvaUQVt14Qh8+Uwx17Dk/kDg7GbwZU+ MbADZu6sgmo1QX7kpZP8XDsbStw3KM5AeBccW/pBRWDH+ANYcEtYWCvXk/ImqFNJ/vqF sqXQKohCkrmvn7xrfRu6APErDc1IUIn+PwkYhocXlJ8hJ22opZ/r5PW+GyvojFy0HFai p+UA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=nYcRtqq0; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75098-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75098-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id h25-20020a05620a13f900b007876f7e5bffsi5809982qkl.612.2024.02.21.08.26.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 08:26:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-75098-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=nYcRtqq0; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75098-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75098-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id B82291C2108D for ; Wed, 21 Feb 2024 16:26:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DA3B882890; Wed, 21 Feb 2024 16:25:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="nYcRtqq0" 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 1D7F181731; Wed, 21 Feb 2024 16:25:28 +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=1708532729; cv=none; b=kXNjRZV9UnFldf5vEVqUg5mxZHww7si2xBv0qub3WW2W37TzCpxZA4M5aZCYsUJ7Ic9LPEjk0chheEm0i+GJ0N0iraPiVpIS//Gch55iZeK7kHTjWYqp2Lm+WAg/9MJ7HIQX+TREbU4Kd6tcS7pMSB4iSb64yLzsQIzn+ehLyCw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708532729; c=relaxed/simple; bh=R3G29V1nYqPfReSpGBjXHcB6eUKOdQPJgoZsK67W94g=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=gMG3r/gfXoh1Wb/j555WRckKzEguhTU4NpKkhdIO3JVDIAciF586RYDbZKrUn9CxqvRCREr7l+MKFiOmpQ9nEWUhF7mUO94E6Jb/UzgZBy3KUvu6sQxyCCyZCcK35ZIxQfexT6tmcFe0rsVVe3P4sg8LpaK0gP6haQk8Ie41AiU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=nYcRtqq0; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7080CC433B2; Wed, 21 Feb 2024 16:25:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708532728; bh=R3G29V1nYqPfReSpGBjXHcB6eUKOdQPJgoZsK67W94g=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=nYcRtqq0BYleLMWVme4nWxcvJm//247pjMWOUnYH8ciU/DCq/xWp9PU1gmDacqs6N vg6+GFYqpPyR3fKPLyU5KzPuzlB6gMrkfvmyZS7fswB52lucDTMBh8dGt1ZwniUtbl RriSFP9Ru2NKl4gSiMPQSRSixwhXs6y+HxX5ZuueaLlj3EXO6i++mFHkjF0xB3BWZI IvK5GkwjKWms4gdC3nz5uKoM1wBmNz8ngPdxQrQqTilwOnmLO6tpZreVlUmv1Ppeik qzI23UglSDKXsXIyHCcL5F6hXHfVoYy+OTT42qX/Y/avQD/uNCW/ILNUiHG0b42RZC 1gwrofyKYw0fA== From: Benjamin Tissoires Date: Wed, 21 Feb 2024 17:25:17 +0100 Subject: [PATCH RFC bpf-next v3 01/16] 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: <20240221-hid-bpf-sleepable-v3-1-1fb378ca6301@kernel.org> References: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@kernel.org> In-Reply-To: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@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=1708532719; l=1061; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=R3G29V1nYqPfReSpGBjXHcB6eUKOdQPJgoZsK67W94g=; b=Sj7ipK1o5q96wXkl7PmldN0CmFbJdrMyNtyHoJiZuX1EPDYejRzCG4iBxZ+OSDYpD5UptXfDe Y71xE1UhYqMC9qmgMnDcBIxYAtpJT7wWm68sYByi26265cLilYCfRIg X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791526449462521271 X-GMAIL-MSGID: 1791526449462521271 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 --- no changes in v3 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 011d54a1dc53..88e9d2e4c29f 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -18022,6 +18022,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 21 16:25:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 204301 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp1145786dyc; Wed, 21 Feb 2024 08:26:19 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUbMXTS6/FvtGExjbb1KbvuAK3bYbg7F3fjtXJfSlroB6moWyAyYobhJzNUik5cau9vk8SsizpHzZ6ZE+1QTRxCjxAvGA== X-Google-Smtp-Source: AGHT+IHzws5PygBfiKwhBy3EMtIYb6iKb+wr/9mNTLuZao2I+Dng513Nlg7okulQ6Ome7HczR2E6 X-Received: by 2002:aa7:9849:0:b0:6e4:8961:5c2b with SMTP id n9-20020aa79849000000b006e489615c2bmr3230016pfq.14.1708532779410; Wed, 21 Feb 2024 08:26:19 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708532779; cv=pass; d=google.com; s=arc-20160816; b=kfj9dlQm+vDc4w2mi7EjXuE6NwUy7RITre4pG7nZHodOw52NBPmc1fVD0YtCwEhgET Jkz2mKaO8HFkKK/wHZK7r4ZpW+AooYGzrchMtvw8L5GCPS7YWatfwV6LktEqvOmOC2av KxoBYvODJSMY+idnpW8Zip4n7lFfNleb1kbOMuoU5EMfxyDrm6Jp4PMj07fujCykF3Jm Kej+dW6wT1Bb+Bxuo4eSM4CCS2l5qVCxaokNZsmUvCOMveVvyYtsw1Z4zfd4BTiQG0w4 nUXkcZzrb0in57DBs5ijQ8MDQe8B5cK0m0kcu9yKPqALu9dUt6fphZhXdkljYdCqNiMH KrHg== 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=RDRlMAJOdlwYvOPJ+hpC4Dt0G+qyQqlMPwWvJMkqEA4=; fh=hc4Y6F7QBSV+XcCZzl4ZOgjmnikSFu9Au/ny4/yLvAg=; b=B8Ng7S+AhO+XQI3R/EOceeuSSq7dENwp0mM6mKPQdOqBlTQ+NnQb3d8q2au6sbB1BH 4CFKsLtv7xCU7w4r+1q1IprHScZMtgaBijZsSxQ7+Otw1nlN+PPMlZKjLkrBYRPBJ3hl IVt7Uie+NDHIkaAT9pTE3GfYbqouLkBTd+BcLWw+/EsuxSXJ9ab0QBiZ75MwN9NnXq7J SbADLVNhOEChdR5gDTVjzJP1VCtzBoZTExpg3uyeUnjMPENCW1fOOgrf7yAO+MS3ETA9 k4MLobgYrhwrRebLxKmJWi1R/wD+4MccbZXqDgkbm6SpBezZ8KpAYU0BzQyGvimDd0fy 3BUQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=BzgkSWxx; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75099-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75099-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id fj36-20020a056a003a2400b006d9bb8e9e5esi8457680pfb.356.2024.02.21.08.26.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 08:26:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-75099-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=BzgkSWxx; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75099-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75099-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 38B89287295 for ; Wed, 21 Feb 2024 16:26:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2A9AC8173B; Wed, 21 Feb 2024 16:25:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="BzgkSWxx" 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 58AB38175F; Wed, 21 Feb 2024 16:25:33 +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=1708532733; cv=none; b=FE3tUlZHFnFFkkTYddBpnSmZpoXdKNp8RB6t+1Bn/AkPmqpfPptW19VfaKpHA0IX9ngHVOp7MIwapL+U5nO8ZoNIQcB6g2qmY7nC4BCZbPWN9CRAdn+ZSmopAV5nBqJDiUsHmq1M/vNSIIyJK2wPmUo9J5PWzn5h00nQxtU95Zc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708532733; c=relaxed/simple; bh=Mms2ysexNAwTlu0ixVh3mQvJfR7XVQbK1/6OpHHyNTk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=QmUlzTYsvtNRf2T2e0GPVJVUWv0CqVf1jPqGYqe1oSEABMFpN6TsnGFUkIoLJ30yg1Lgrc6s2fIHB79n+HWwQ1yVWl/LnxpNm9KherVyhFJ5NydeIsQub+Ih7uGhCETxdUzjXd+m0JivRimMxTN8Rj2mu4rhtBbFWyNNmP/N2M4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BzgkSWxx; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id E1AE3C43399; Wed, 21 Feb 2024 16:25:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708532732; bh=Mms2ysexNAwTlu0ixVh3mQvJfR7XVQbK1/6OpHHyNTk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=BzgkSWxxL1yVpA3AP40UL1iNJt7dhFRqcnWwkIr9MhcBPn87wDNPRJnBCO/sMFhuO B6DsLTtCrQvFeP2jMEm94SzdbUK6wIM9gu1zajvLf2RkqYsFIAgWwWzGrn7BvwABer hhELTbOnO72g9IDIM8LRdYl+ojzy/l4B7Up77tcxToMoKVxAFmZCv3V1Tf8QdRjWF8 Ptc+V8oI0gAnaaWlOL5dr2tuPABx+Sf9U1vYxhRNKCHYmUS8FurcVf1aV2TU8Wbs36 x8BV/kfrthAlqGX0OqI5BIFSZ8ilAk5VqLniB0nW6DKV9meDGnDUZVcZnksdc9r+66 RHUoEN+lQKB/g== From: Benjamin Tissoires Date: Wed, 21 Feb 2024 17:25:18 +0100 Subject: [PATCH RFC bpf-next v3 02/16] 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: <20240221-hid-bpf-sleepable-v3-2-1fb378ca6301@kernel.org> References: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@kernel.org> In-Reply-To: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@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=1708532719; l=3279; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=Mms2ysexNAwTlu0ixVh3mQvJfR7XVQbK1/6OpHHyNTk=; b=ubFCIPswBRFz/abxhmIcawSwTVPZa0JGnBDjJfGIMEOXWj8HHjtDgLFSPl40V+M9vub1naeZk 2KcPMrYv1mfB+/yBHNtNvw7/HK9cipJeTmWu+mfNNkZwmPmF3NuzTNN X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791526467733218861 X-GMAIL-MSGID: 1791526467733218861 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 --- no changes in v3 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 88e9d2e4c29f..7a4b19bea2ac 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 */ @@ -10164,7 +10169,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; } @@ -10194,7 +10199,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; } @@ -11535,7 +11540,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); } } @@ -12056,7 +12061,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; } @@ -18193,7 +18198,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 @@ -19669,7 +19674,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 21 16:25:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 204302 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp1145921dyc; Wed, 21 Feb 2024 08:26:36 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUkU0bhNBJgSh31e2HQyMyHy7/eNrgkUYXU3uww/vOE4ubVDiztzvCw+dY4VZzbtUKS/NV60hQBDtdAugFJaX+uqP6KfQ== X-Google-Smtp-Source: AGHT+IGdLHPKE/ocC48E4zMRES3I7ZBUYUCCMugFUZ2/gSfe9frqFKH9a/G64eNNdIzmELWcLvyJ X-Received: by 2002:a05:620a:127a:b0:787:903c:b2b2 with SMTP id b26-20020a05620a127a00b00787903cb2b2mr1141885qkl.69.1708532796440; Wed, 21 Feb 2024 08:26:36 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708532796; cv=pass; d=google.com; s=arc-20160816; b=IT5XnlVM4jV1vF95jRKKjPw0Yg08th8faPCWgUh/4lr40dAl4uptRIAXutt6c21tZU MsXo6jGRufG2R+mNkfHwYrYZ5dUsY5uRj+koTN6vzCZzQWC5E5V+cDNXUYRroBjJ8jjN LtVlM4e+shGgZ5pVdap2PJR9NSC+OO589gpHb6JqKCH2mDG8E6xvLG3Tpo3eNOFu9zl1 zpFrMqtG2miapvWQ68IpFjU3X4x03bPM6lcHei/SizgEgXvo0IJ6u4ML6c7CsN5xcr0d yExHibumE34yhnRHkgKe5DdGnX6jOYNhT6Xis1SipuvVbgUmjbzpPCGE3EyKJPqzi2O8 ewJA== 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=CCwRAZuN0reHDp0M5bkAJew7ynmBrN6bD5stFcTkPZQ=; fh=hc4Y6F7QBSV+XcCZzl4ZOgjmnikSFu9Au/ny4/yLvAg=; b=uJ/tu2LOM6FZItSm9kOFq9E9uoF6JG5L6YhhdXZRq2RhPuqYMjWG4UVeXqiUKtfsJ1 ix2PRbmD1RU4KjFqnMDBOb1Tkh0pOXbMkA/QSpKPwDI6Dh3vkDDu8EX3RUTpskDCQmJX QvrUx5aD1PocUjvQxelzR4UAdXeU9S+v9EcrzeKBA1hTpvvr3bv7eRqP79jhIWW7CLiO gZqpIU0UD+4C3vuGEQGHxa83KL6oVua/UEmn0s3Y9yQKBxmXhdUEOj9ed2U5zg88zJoI wRvkLjHD0JWliOIsqJzFBBFV+RDkRoEZOGJ4m632cRBuCGYtgX0zaLIvlNo1wTDsGFP4 iRzw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="knrL/Igp"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75100-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75100-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id h25-20020a05620a13f900b007876f7e5bffsi5809982qkl.612.2024.02.21.08.26.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 08:26:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-75100-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="knrL/Igp"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75100-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75100-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 3CF3A1C23433 for ; Wed, 21 Feb 2024 16:26:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7704C82D88; Wed, 21 Feb 2024 16:25:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="knrL/Igp" 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 D13F08062A; Wed, 21 Feb 2024 16:25:37 +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=1708532737; cv=none; b=sveNfKGLBCoDdsZN7MOCJek89HmuYNBON7keO+g2A0P/R9Rl5wYsij17+9iMarpkmg9eqcaJ9ruBTcpIRkom63wCiBzc+ntVc7meUNjSu6Fevl9gAoALNKD3BGW1Su9WaVF2QmidNFGpSZy7DU+VHfeNrNf04KaPyCY1dRmZISI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708532737; c=relaxed/simple; bh=ifZFLSAYIFkAKgWtCvrWc7FBsIzi3tPzHayMgfZ0rSU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ImKMnDd6/rCXEQgqGjU6gkAqXdIoqccuZAdHgN6i9pqOttUrmc0uo6g0umEORTnkjTn0ltE1V+IQIvUUx7+4CFt76fvovPtySCbtkArL6lVWdtVQ1TRfgGOdIg1FqAyHGjAzdETLJI9902pSNaOb89TUrpyqgHBayVAxumpKJxk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=knrL/Igp; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5CED0C433F1; Wed, 21 Feb 2024 16:25:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708532737; bh=ifZFLSAYIFkAKgWtCvrWc7FBsIzi3tPzHayMgfZ0rSU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=knrL/IgpI0fD6He4VSAVpXLKpfzHdDdIIfp3hDSxmOe7ZkDiqhvcQ3gzHmDQU3n6P vMcXB41uLn/QxeNf3PWpMnZDCjn8BA51UTWFzHLOCh3fFBa7ZTFFXRpWkDDIvKuGfM 75mCSiVsn0lu2nmM+mUwKmkgUyodZanxAgnPo6AgmmLfnsGHMqWrNf/rVHPJaF9/FT L1Cojcf35a/0mqeDkj4BVW537izWdQzIZb9DMSQ26SB4K5ETzqn9yr8ESAaVC0finh rIc//xvetK6+2TNoSMp1cSBIrcvvpQpgkIxAm/JEQKb+wyTKU9f+ugxEGUsREHn2xh SzQuF0qLG8f/w== From: Benjamin Tissoires Date: Wed, 21 Feb 2024 17:25:19 +0100 Subject: [PATCH RFC bpf-next v3 03/16] bpf/verifier: add is_async_callback_calling_insn() helper Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240221-hid-bpf-sleepable-v3-3-1fb378ca6301@kernel.org> References: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@kernel.org> In-Reply-To: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@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=1708532719; l=1797; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=ifZFLSAYIFkAKgWtCvrWc7FBsIzi3tPzHayMgfZ0rSU=; b=JIpXu3g0QunAN4Krqvk/yZ3ph5YehKnA1pxkpJWLimDr+7xLD4HesIWBl5fi/gxLdz+IaKjQS 6HWbJS3t48jANwXHML823DEwFy3apcxCIjwCzfSeuTPrRLWd5Q92NG/ X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791526485970372584 X-GMAIL-MSGID: 1791526485970372584 Currently we have a special case for BPF_FUNC_timer_set_callback, let's introduce a helper we can extend for the kfunc that will come in a later patch Signed-off-by: Benjamin Tissoires --- new in v3 --- kernel/bpf/verifier.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 7a4b19bea2ac..f81c799b2c80 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -528,6 +528,11 @@ static bool is_sync_callback_calling_insn(struct bpf_insn *insn) (bpf_pseudo_kfunc_call(insn) && is_sync_callback_calling_kfunc(insn->imm)); } +static bool is_async_callback_calling_insn(struct bpf_insn *insn) +{ + return (bpf_helper_call(insn) && is_async_callback_calling_function(insn->imm)); +} + static bool is_storage_get_function(enum bpf_func_id func_id) { return func_id == BPF_FUNC_sk_storage_get || @@ -9445,9 +9450,7 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins return -EFAULT; } - if (insn->code == (BPF_JMP | BPF_CALL) && - insn->src_reg == 0 && - insn->imm == BPF_FUNC_timer_set_callback) { + if (is_async_callback_calling_insn(insn)) { struct bpf_verifier_state *async_cb; /* there is no real recursion here. timer callbacks are async */ @@ -15588,7 +15591,7 @@ 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 (is_async_callback_calling_insn(insn)) /* 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 From patchwork Wed Feb 21 16:25:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 204303 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp1146049dyc; Wed, 21 Feb 2024 08:26:52 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUKc4RaGY9mvdHVbbmi2Q95xIqlxbSSFU9H9KAOtZVHF8PxXKa/2FNLq8QK7kE0JRYCN0gAGjCOv/3haajFUKmmT2iC+g== X-Google-Smtp-Source: AGHT+IHxUCGnEUjY2ayI2S6yvZx614r991DxZN07yk6+s6Id3QAIA7DTUInjwjdmYSmB8H9ovTJi X-Received: by 2002:a05:6870:169c:b0:21e:6961:9683 with SMTP id j28-20020a056870169c00b0021e69619683mr18007989oae.0.1708532812193; Wed, 21 Feb 2024 08:26:52 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708532812; cv=pass; d=google.com; s=arc-20160816; b=dO5sQYcNmXg3ySEa09VHs4Mt2V/TF8tmQFn5myXWbEBSZ7GPgE2wMZS5w8xUc5Gi2L Z/4RCxsZp4t92Ecr6l1WLkNHXhTaFxusHp7ZyBLeT4vOZfvYzy7XhNKCDmKkoc1hSK3X y2APTtRqHE/8q/+M5U6AHNw2c5v+ZUL3Pk6z70obijX/ScWlZJVuH5NL9+JeJlxoMtHr ewRUBj950qaKrQ+5Was5sTQtmEg/2maRS6sH81dujZ3OEuEJ3qzPI61Z55/2zDiPJbz+ M6M9DeGMTWc4PM36feCIfbyPCrXVvOJzpkmMB1G3xEpspDoKVYGTyjn9j3bM8+wJcon5 ryRQ== 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=X9AMFo4C5peqqK88+p0TXC1r8wPzp6sRf+eanPmIzfI=; fh=hc4Y6F7QBSV+XcCZzl4ZOgjmnikSFu9Au/ny4/yLvAg=; b=TYkW2KVg6msBVokDH/j9/MgLkRoUYXZ8ZQQXg64CSWvoO61Fgek+JzP9lQVXuxFCiS AQuJkUEcDCaonA6QqI34I8EBzEWs8Te4QFEKKw2XW/xIpRtN4032Kd4R76wr/rXNMJE3 crSCI6PfR8C7ngOInqx7cDLOTIaf1VrQd5r0skdL7YorN+E52a6YCYlc5DasrJFtKA0y OYjnS8D+wvFSqWv+yrXPYyN82vRcn2z5ApPKQd/KqNGl9A1pbNZ/fxbEdFT7K0EeUBfz 43Dsbmd2gwUbNxtzyvFEe9xnrPaQiufXHiBbSzZK+rIMgCW6qzYonUSDx678FPwgxe6x C2VQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=m2iBXTEW; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75101-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75101-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id p16-20020ac84610000000b0042c501aa0ddsi10974877qtn.778.2024.02.21.08.26.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 08:26:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-75101-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=m2iBXTEW; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75101-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75101-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id DEE511C2456F for ; Wed, 21 Feb 2024 16:26:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D249F839F3; Wed, 21 Feb 2024 16:25:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="m2iBXTEW" 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 44CEF81AB3; Wed, 21 Feb 2024 16:25:41 +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=1708532742; cv=none; b=TZlESJiQS17q4CqWphqNT1A8CJ8vk4Lp7ssmj8KEj+7MZbdeux5cj9W+94hNjO7Fv2fiXUAlM+bOCz/O9EG86dnhxW8hVfIgMEnCQdgVFh4q2lCtzkvxeokt+pBOEXa/mazvrowitEVErV4L1e8iQK9cfjcJRpXIpFzwhGPDARo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708532742; c=relaxed/simple; bh=ZYNT6AaQhkuGvT8Z9dQUoI41EykqVHPK+5CqRnyQs3A=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=mUm7JDUMx+5T+A+orbYjhug/xNh54d6zUcQr2uri3pi1+0jskGb7rbb/XYZoxlwuP1mRGP/3bW7CvLoWNCKxm401jOgUkqslHC6fUvycPgMEXMZ6U0EQGp8w1O7cc0Fsku1ZCPyNdOntzXcSz3ye5zWg2y50tGyM6CRV4Qmmb2I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=m2iBXTEW; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id CE27EC433B2; Wed, 21 Feb 2024 16:25:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708532741; bh=ZYNT6AaQhkuGvT8Z9dQUoI41EykqVHPK+5CqRnyQs3A=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=m2iBXTEWXgULxh3hvLUrKiSzHgnKmHSkFNNoAVbtLUy+WhGa3JBGLTRuKCYOUiLa0 3lyIlTI3YmfYd96dg6z3iHx35L7ZwgYPkqIdBPLqgWl39A8SRKupmQZ4CteLYppLyp rUvitXx4/Zh2Io8PPd/8AXkk93J6mCHdmDwmYerxAKtEh5r+R82u5LjBJc9wzsIvRQ tyhtvKu3lx5dka/z7gZZVQb0FuEcEEp+NGZn/N8NlQk6h4AXZkdjlBAZhpCJP4v3w4 kWxO4WYE2rqA72mC4S7QBDy5XNtCEGZyBNeBzkOPJmH/2d3Vabotg6cip4x66VlL7c 2jw6dkrAyHOzA== From: Benjamin Tissoires Date: Wed, 21 Feb 2024 17:25:20 +0100 Subject: [PATCH RFC bpf-next v3 04/16] bpf/helpers: introduce sleepable bpf_timers Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240221-hid-bpf-sleepable-v3-4-1fb378ca6301@kernel.org> References: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@kernel.org> In-Reply-To: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@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=1708532719; l=7906; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=ZYNT6AaQhkuGvT8Z9dQUoI41EykqVHPK+5CqRnyQs3A=; b=NhATyVXLBVmiUhI3zNu7Frdb/5dMuW4X4D3dXM/NaBaAy0JRXEJNO0fjK1D7kD/h0+fckzBvg 7iaiB1bNfDqDlBCwS7fo235/Sn9HYXfGYGh3WXOQ3gRFFBRjXGYA+yy X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791526502096412879 X-GMAIL-MSGID: 1791526502096412879 They are implemented as a workqueue, which means that there are no guarantees of timing nor ordering. Signed-off-by: Benjamin Tissoires --- changes in v3: - extracted the implementation in bpf_timer only, without bpf_timer_set_sleepable_cb() - rely on schedule_work() only, from bpf_timer_start() - add semaphore to ensure bpf_timer_work_cb() is accessing consistent data 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/uapi/linux/bpf.h | 4 +++ kernel/bpf/helpers.c | 92 ++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 82 insertions(+), 14 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index d96708380e52..1fc7ecbd9d33 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -7421,10 +7421,14 @@ struct bpf_core_relo { * - BPF_F_TIMER_ABS: Timeout passed is absolute time, by default it is * relative to current time. * - BPF_F_TIMER_CPU_PIN: Timer will be pinned to the CPU of the caller. + * - BPF_F_TIMER_SLEEPABLE: Timer will run in a sleepable context, with + * no guarantees of ordering nor timing (consider this as being just + * offloaded immediately). */ enum { BPF_F_TIMER_ABS = (1ULL << 0), BPF_F_TIMER_CPU_PIN = (1ULL << 1), + BPF_F_TIMER_SLEEPABLE = (1ULL << 2), }; /* BPF numbers iterator state */ diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 93edf730d288..f9add0abe40a 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "../../lib/kstrtox.h" @@ -1094,13 +1095,19 @@ const struct bpf_func_proto bpf_snprintf_proto = { * bpf_timer_cancel() cancels the timer and decrements prog's refcnt. * Inner maps can contain bpf timers as well. ops->map_release_uref is * freeing the timers when inner map is replaced or deleted by user space. + * + * sleepable_lock protects only the setup of the workqueue, not the callback + * itself. This is done to ensure we don't run concurrently a free of the + * callback or the associated program. */ struct bpf_hrtimer { struct hrtimer timer; + struct work_struct work; struct bpf_map *map; struct bpf_prog *prog; void __rcu *callback_fn; void *value; + struct semaphore sleepable_lock; }; /* the actual struct hidden inside uapi struct bpf_timer */ @@ -1113,6 +1120,55 @@ struct bpf_timer_kern { struct bpf_spin_lock lock; } __attribute__((aligned(8))); +static u32 __bpf_timer_compute_key(struct bpf_hrtimer *timer) +{ + struct bpf_map *map = timer->map; + void *value = timer->value; + + if (map->map_type == BPF_MAP_TYPE_ARRAY) { + struct bpf_array *array = container_of(map, struct bpf_array, map); + + /* compute the key */ + return ((char *)value - array->value) / array->elem_size; + } + + /* hash or lru */ + return *(u32 *)(value - round_up(map->key_size, 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; + u32 key; + + BTF_TYPE_EMIT(struct bpf_timer); + + down(&t->sleepable_lock); + + callback_fn = READ_ONCE(t->callback_fn); + if (!callback_fn) { + up(&t->sleepable_lock); + return; + } + + key = __bpf_timer_compute_key(t); + + /* prevent the callback to be freed by bpf_timer_cancel() while running + * so we can release the semaphore + */ + bpf_prog_inc(t->prog); + + up(&t->sleepable_lock); + + callback_fn((u64)(long)map, (u64)(long)&key, (u64)(long)value, 0, 0); + /* The verifier checked that return value is zero. */ + + bpf_prog_put(t->prog); +} + static DEFINE_PER_CPU(struct bpf_hrtimer *, hrtimer_running); static enum hrtimer_restart bpf_timer_cb(struct hrtimer *hrtimer) @@ -1121,8 +1177,7 @@ static enum hrtimer_restart bpf_timer_cb(struct hrtimer *hrtimer) struct bpf_map *map = t->map; void *value = t->value; bpf_callback_t callback_fn; - void *key; - u32 idx; + u32 key; BTF_TYPE_EMIT(struct bpf_timer); callback_fn = rcu_dereference_check(t->callback_fn, rcu_read_lock_bh_held()); @@ -1136,17 +1191,9 @@ static enum hrtimer_restart bpf_timer_cb(struct hrtimer *hrtimer) * bpf_map_delete_elem() on the same timer. */ this_cpu_write(hrtimer_running, t); - 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); - } + key = __bpf_timer_compute_key(t); - callback_fn((u64)(long)map, (u64)(long)key, (u64)(long)value, 0, 0); + callback_fn((u64)(long)map, (u64)(long)&key, (u64)(long)value, 0, 0); /* The verifier checked that return value is zero. */ this_cpu_write(hrtimer_running, NULL); @@ -1191,6 +1238,8 @@ BPF_CALL_3(bpf_timer_init, struct bpf_timer_kern *, timer, struct bpf_map *, map t->prog = NULL; rcu_assign_pointer(t->callback_fn, NULL); hrtimer_init(&t->timer, clockid, HRTIMER_MODE_REL_SOFT); + INIT_WORK(&t->work, bpf_timer_work_cb); + sema_init(&t->sleepable_lock, 1); t->timer.function = bpf_timer_cb; WRITE_ONCE(timer->timer, t); /* Guarantee the order between timer->timer and map->usercnt. So @@ -1245,6 +1294,7 @@ BPF_CALL_3(bpf_timer_set_callback, struct bpf_timer_kern *, timer, void *, callb ret = -EPERM; goto out; } + down(&t->sleepable_lock); prev = t->prog; if (prev != prog) { /* Bump prog refcnt once. Every bpf_timer_set_callback() @@ -1261,6 +1311,7 @@ BPF_CALL_3(bpf_timer_set_callback, struct bpf_timer_kern *, timer, void *, callb t->prog = prog; } rcu_assign_pointer(t->callback_fn, callback_fn); + up(&t->sleepable_lock); out: __bpf_spin_unlock_irqrestore(&timer->lock); return ret; @@ -1282,7 +1333,7 @@ BPF_CALL_3(bpf_timer_start, struct bpf_timer_kern *, timer, u64, nsecs, u64, fla if (in_nmi()) return -EOPNOTSUPP; - if (flags & ~(BPF_F_TIMER_ABS | BPF_F_TIMER_CPU_PIN)) + if (flags & ~(BPF_F_TIMER_ABS | BPF_F_TIMER_CPU_PIN | BPF_F_TIMER_SLEEPABLE)) return -EINVAL; __bpf_spin_lock_irqsave(&timer->lock); t = timer->timer; @@ -1299,7 +1350,10 @@ BPF_CALL_3(bpf_timer_start, struct bpf_timer_kern *, timer, u64, nsecs, u64, fla if (flags & BPF_F_TIMER_CPU_PIN) mode |= HRTIMER_MODE_PINNED; - hrtimer_start(&t->timer, ns_to_ktime(nsecs), mode); + if (flags & BPF_F_TIMER_SLEEPABLE) + schedule_work(&t->work); + else + hrtimer_start(&t->timer, ns_to_ktime(nsecs), mode); out: __bpf_spin_unlock_irqrestore(&timer->lock); return ret; @@ -1346,13 +1400,21 @@ BPF_CALL_1(bpf_timer_cancel, struct bpf_timer_kern *, timer) ret = -EDEADLK; goto out; } + down(&t->sleepable_lock); drop_prog_refcnt(t); + up(&t->sleepable_lock); out: __bpf_spin_unlock_irqrestore(&timer->lock); /* Cancel the timer and wait for associated callback to finish * if it was running. */ ret = ret ?: hrtimer_cancel(&t->timer); + + /* also cancel the sleepable work, but *do not* wait for + * it to finish if it was running as we might not be in a + * sleepable context + */ + ret = ret ?: cancel_work(&t->work); return ret; } @@ -1407,6 +1469,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); } From patchwork Wed Feb 21 16:25:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 204304 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp1146234dyc; Wed, 21 Feb 2024 08:27:07 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWQDkVVUvP6IHBJNa3s/NQbrWOxO1TtAVmNhQgs8KkfR4cfAmQM+z6XtHadrjsouCCA9XtRfgP/wfrdKL9Fkbm7BFI7Jg== X-Google-Smtp-Source: AGHT+IFuJ5v/IzxxJpmLT/ZbzJz54sS6qW81rBu/4gc8QBzG7JCRBkwptaojl7uyPvHk72P7dFRP X-Received: by 2002:a05:6358:4c44:b0:17b:59ef:bc72 with SMTP id lc4-20020a0563584c4400b0017b59efbc72mr2722151rwc.9.1708532827085; Wed, 21 Feb 2024 08:27:07 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708532827; cv=pass; d=google.com; s=arc-20160816; b=S40/u0j5ky+5QjnRUTHopxeQBXZ7NW2iA4/eZftvSMs89nH9mNXMRK5geqk5UXa8X+ b265CN0OBdUrqTGXm9g6V2lTY5pcSw6x4xv3NHl61lRA7tI8xdfZsm/XZ4S+Pmog7URw v+pl0zYN+vgqfH3Uvr3695uVHLit8s6K+0rNkMkzyZiFLbmD5w/QkXxf1No4XDPgAK/m yAjBoKjQZDjKgTpjWjZNs02kIhZezKL4TaDhVEu+m7pJwbS2mSE5l4PohaAV4krO+GC6 l8SJvHzUQa2xNYLGwrwjj6rAbaZ1gdW/k3pOMFRQDKSFTYgBoVAuDuYUFJDsb0EjJcdh B1WQ== 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=/LO5zV+EpCgiiDVI+gf9fOd7UPl0+r37NnzZNpaQtFk=; fh=hc4Y6F7QBSV+XcCZzl4ZOgjmnikSFu9Au/ny4/yLvAg=; b=GauToq2cgcI+CHdDXQRXCMr0BSXtc8IMB+bVHZ6PsKtgEXv4F+D9qaeNXTMzOWO/H7 vXwgEZf2bLFCPfBztigiReh+jT6h/9FhACWVcHf0G1VgO7WqHDY13M9+HjQE1TgGX9A7 HgCgZMOEtzDLsNxAYJgSdlid6TQqnDaMwVeO9LHPlxDpKnxSgLRAEZoOp9eBpiu9fSWc wdyLS2EPI8YUNPlmtW0fD64crjBC4L47zs+D8rh+v3rFWTgktV/j63Hom4q6ZyF71G3H epDuzPKFZ0l7VcTec5A0MpxEyMK9rvtROffl8qkoJ0lQqOxuyTclAIDgIS+v+tWjK8co GYMw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=lJdBG51D; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75102-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75102-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id 36-20020a631464000000b005dc50f61543si8228189pgu.427.2024.02.21.08.27.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 08:27:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-75102-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=lJdBG51D; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75102-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75102-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 70CDB2875EF for ; Wed, 21 Feb 2024 16:27:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7042283CA1; Wed, 21 Feb 2024 16:25:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="lJdBG51D" 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 AD7D4823B2; Wed, 21 Feb 2024 16:25:46 +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=1708532746; cv=none; b=rtL1Q1vLNcX/ocpD7bmeHH5LJSFKWfH6HGPFJHDvH8ZxXjYDC03GD2K3o2qWIUmdmFue0yGWfq8N3AOTv8cs/aHd9t+9wut94oaqOsihv6h9g4fa7Gomyg4B4oXYwwRLlQPRpV1JFjZQR1sbytFK2vja/O+J7MNW2qX4uXE+neI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708532746; c=relaxed/simple; bh=QnRTVuBH9fYcBUFkZSJeCI484dX0GF8bieROarhOafM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=uUNBsg1tOZ/Q6cTVwKRo/KYTnYPRlyt7FreV4b33t2ONVvMYpwfTt7o7StgAhwWckhWGQmzF6h+QH4WOTduNdMsteQdWyBwgPEyAgVB179rboAR6aqqGEizR0B0pLa+Ap4Ama2/vhY08f32O/IKyM7PVzI84srh7pauo5CTu4HY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lJdBG51D; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4D016C43390; Wed, 21 Feb 2024 16:25:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708532746; bh=QnRTVuBH9fYcBUFkZSJeCI484dX0GF8bieROarhOafM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=lJdBG51DCpUie8ePVFvoGaE3y8zRG2cUteRahz4qVeIZlfbVEu5HDP9OKBJg50YTk JyM6OBtDRsYDeCiEFwb1YTBGxUhfGeQ1CI1Da3D/SWplPThCKrwRb+t1mUjJ3pk2FZ ve4VWW+gNbt3msHgUHwm2wG2KTV7BPa1+7w4j/mumCfcSno4Pcc2mj6oi7Xkmob9A4 MnM7qVcnRuPEXxtVVMX21jnElxRcaAzI0vsS6wZpq6+eHOwGpktlo5p/Bhkm2+8lZm tk20tXWSonVppxcmc6MkJvxJaH0syYi6MScTFjf29cBQjY4GH09JZdP8tDq4KLgen0 hguBaRW+bo4BQ== From: Benjamin Tissoires Date: Wed, 21 Feb 2024 17:25:21 +0100 Subject: [PATCH RFC bpf-next v3 05/16] bpf/verifier: add bpf_timer as a kfunc capable type Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240221-hid-bpf-sleepable-v3-5-1fb378ca6301@kernel.org> References: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@kernel.org> In-Reply-To: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@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=1708532719; l=3633; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=QnRTVuBH9fYcBUFkZSJeCI484dX0GF8bieROarhOafM=; b=63OFTbVppf+nuiSypXyyhrLP+N831rna1g38d4aUQHOSUnTCmQMddwzJmy4lN5gY4TACu0F9D osn2uDFkjmcBslibu4c58TGHphbUDZT9e5lbDXqpJXSsfvr1opd2nj6 X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791526517564306790 X-GMAIL-MSGID: 1791526517564306790 We need to extend the bpf_timer API, but the way forward relies on kfuncs. So make bpf_timer known for kfuncs from the verifier PoV Signed-off-by: Benjamin Tissoires --- new in v3 (split from v2 02/10) --- kernel/bpf/verifier.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index f81c799b2c80..2b11687063ff 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5444,6 +5444,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)); @@ -10789,6 +10809,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 +10818,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 +10862,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 +10936,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 { @@ -11061,6 +11090,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", @@ -11693,6 +11725,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 +12006,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; } } From patchwork Wed Feb 21 16:25:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 204305 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp1146402dyc; Wed, 21 Feb 2024 08:27:24 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCW1bu/olZBiSBrRQgv68Wlvt7CLoqaQa6jxsNQBzyCU3xfKF3xnkFAcHy45168TzxDS2YoDyQQl9l/usY97AUU+tHblsQ== X-Google-Smtp-Source: AGHT+IHZn3IryS2TqiwwxIZ6U2PyvqSkgdglSCfB8GUaODL8ei3MoxwQF3aDPsUu52knVb7Krh1C X-Received: by 2002:a05:622a:1d1:b0:42e:4521:44dd with SMTP id t17-20020a05622a01d100b0042e452144ddmr116448qtw.6.1708532844140; Wed, 21 Feb 2024 08:27:24 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708532844; cv=pass; d=google.com; s=arc-20160816; b=TLh/i9JqI9psmlzylbfCAojXJi/kpYjxbal8tomkckOCV27lWCimz2e7yDTkFd/Urb YaQFM4l2+NNiBDo56LW2AejCtu9uFa1Bkq3ELtjdjohzwUohjK9PgxRE9lMa6MI4NdFk Oyvo7JSD4xShVkGzfnP1CuX/YDrA6v8xlRitSmIDYeCBGwGai/T+/cROdQLBvBOnPIie wc2F4ZdrtgiFq8GtlKBjfZx7d4effx/k9u5mOlf37ueudVtBFzMVnvFiEsi8grpOSVfz 0fExwKt901+FcJ4nFUYjZ6LmP0tn71/clATispd7iMvUZTrkd9kUKPv5botJzGGyHWr5 9wrQ== 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=9KfiP4/eWuozowRVGCL1spkvUaNxQ1udDKmAPlgGuPs=; fh=hc4Y6F7QBSV+XcCZzl4ZOgjmnikSFu9Au/ny4/yLvAg=; b=i/0MQawi0D+m23nKGHDG3rD0FIVd/c/DreVYtSPHaz5dw7OH1jopTXptnuQZOk1ZsB zOwI8s+dGToDEgS1kJEVw/XDsYvSXCXEhmVcsgfTT7woIO95bhHUwhM5j3/LVMJN34K8 ZQx3r9Glog37J46YqDqhblYkog8yLp9wQkKzVC0vSfXODEz2lYujI8XstJrWSbu9E3of PANRrFP4RZB4ijQv9h4fADNfuwghGxogXwM04f27MrAGE8XF2hab25WtRhjdcwV4sSgj 5K2GaHJwU/S9T+Yibz0tFBODuTfBC5fFaSWKKbEw3vCtY2oHgxXYInJdmwrE2m/D1Xgl J4OA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="lK9/4QRA"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75103-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75103-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id d13-20020ac8544d000000b0042e2497f61csi4527460qtq.29.2024.02.21.08.27.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 08:27:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-75103-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="lK9/4QRA"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75103-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75103-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id D2FCB1C244EB for ; Wed, 21 Feb 2024 16:27:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 785FE8286A; Wed, 21 Feb 2024 16:25:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="lK9/4QRA" 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 E3E3C823DF; Wed, 21 Feb 2024 16:25:50 +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=1708532751; cv=none; b=DnYMNey9BtyIPLggB1IvJ0Xsw6I5QTW+NFfLc5cFXeWolxGrtSsavYc07By90J9GnVUkHa3XPPr8xHdjfmPZfG5fhrvDtDRQSA8V3qOwB3xsVpxBUjMfzdI6spJE/jTQ9ETNO8d7FqiQc3h+LLMQ4WEByi4DkAZzJswJ2cdFz2E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708532751; c=relaxed/simple; bh=mPB13lwDyMmrcL43OZduPdt7XlyPzMPfxJk3wSQi6i0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Oftbbu/8ox7DEXFoBloQm8ubXDIOE9aq63qNTHGQcwz3KUlVMJZFnjxtiJJm2rmwBDx7dZu8NeSV8xJTtYsH+7AP0jET/C/bKLkMSdt2nBfX9NkJWAtVSPH3RdQdUZmGy/z0i7UsHS1NGlFxUsuA9Z2FxFTlvwEk+SOOlgr1Onc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lK9/4QRA; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id BC97FC43394; Wed, 21 Feb 2024 16:25:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708532750; bh=mPB13lwDyMmrcL43OZduPdt7XlyPzMPfxJk3wSQi6i0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=lK9/4QRANbD1ikbby/u0uvG3pn30oMZMO7T/xeFCDV9WRQwCRm6rEZJuhX4fJ0UNb ew6N0AztOnXVz38LOtF+7SfdmVhNNND6BTPvevlw4CBuPrpcDYcloh0u7v/Fa1CpPS 89yR3b5t2PUloP30P851IFD3Yq5L2mO9Ovw/ez1wz3i/4hVoYarm1rsjkXeAMEvnNQ UVv6Y2CH1JNETFNXrS9n/3lvDvS2Bjep51D9D7OJwCJDPUXHW3UeTUflHY5Axm2YDG UZSjDEeLJFR8S5IEwCs7d72NRVWHPFJS+x0lB94Wvr1Ium5WbO/Yir0RZS9edBV57Q PQ2ETCkOqFDlw== From: Benjamin Tissoires Date: Wed, 21 Feb 2024 17:25:22 +0100 Subject: [PATCH RFC bpf-next v3 06/16] bpf/helpers: introduce bpf_timer_set_sleepable_cb() kfunc Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240221-hid-bpf-sleepable-v3-6-1fb378ca6301@kernel.org> References: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@kernel.org> In-Reply-To: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@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=1708532719; l=7632; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=mPB13lwDyMmrcL43OZduPdt7XlyPzMPfxJk3wSQi6i0=; b=B+nS9++S8JmQb74JEXeGhA+6cIAUKNJ+sqzNSBI47ZNlqXnNUN21KpkprCmTmKXWhLg9LpMLu 1MZFXPpPuMTDvZlZhGhDZb2jV20Ba+XsRdmHsoNJRxySklYsp7IIIbC X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791526535913068412 X-GMAIL-MSGID: 1791526535913068412 In this patch, bpf_timer_set_sleepable_cb() is functionally equivalent to bpf_timer_set_callback(), to the exception that it enforces the timer to be started with BPF_F_TIMER_SLEEPABLE. But given that bpf_timer_set_callback() is a helper when bpf_timer_set_sleepable_cb() is a kfunc, we need to teach the verifier about its attached callback. Marking that callback as sleepable will be done in a separate patch Signed-off-by: Benjamin Tissoires --- new in v3 (split from v2 02/10) --- kernel/bpf/helpers.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- kernel/bpf/verifier.c | 31 +++++++++++++++++++++++++++++-- 2 files changed, 75 insertions(+), 4 deletions(-) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index f9add0abe40a..2c6dc3d0ffff 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -1108,6 +1108,7 @@ struct bpf_hrtimer { void __rcu *callback_fn; void *value; struct semaphore sleepable_lock; + bool is_sleepable; }; /* the actual struct hidden inside uapi struct bpf_timer */ @@ -1270,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; @@ -1311,12 +1312,19 @@ BPF_CALL_3(bpf_timer_set_callback, struct bpf_timer_kern *, timer, void *, callb t->prog = prog; } rcu_assign_pointer(t->callback_fn, callback_fn); + t->is_sleepable = is_sleepable; up(&t->sleepable_lock); 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, @@ -1342,6 +1350,11 @@ BPF_CALL_3(bpf_timer_start, struct bpf_timer_kern *, timer, u64, nsecs, u64, fla goto out; } + if (t->is_sleepable && !(flags & BPF_F_TIMER_SLEEPABLE)) { + ret = -EINVAL; + goto out; + } + if (flags & BPF_F_TIMER_ABS) mode = HRTIMER_MODE_ABS_SOFT; else @@ -2606,6 +2619,36 @@ __bpf_kfunc void bpf_throw(u64 cookie) WARN(1, "A call to BPF exception callback should never return\n"); } +/** + * bpf_timer_set_sleepable_cb() - Configure the timer to call %callback_fn + * static function in a sleepable context. + * @timer: The bpf_timer that needs to be configured + * @callback_fn: a static bpf function + * + * @returns %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. + * + * This kfunc is equivalent to %bpf_timer_set_callback except that it tells + * the verifier that the target callback is run in a sleepable context. + */ +__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 = {}; + + 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) @@ -2682,6 +2725,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_is_null) BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly) BTF_ID_FLAGS(func, bpf_dynptr_size) BTF_ID_FLAGS(func, bpf_dynptr_clone) +BTF_ID_FLAGS(func, bpf_timer_set_sleepable_cb) BTF_KFUNCS_END(common_btf_ids) static const struct btf_kfunc_id_set common_kfunc_set = { diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 2b11687063ff..91e583c6feba 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -501,6 +501,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_async_callback_calling_kfunc(u32 btf_id); +static bool is_callback_calling_kfunc(u32 btf_id); static bool is_bpf_throw_kfunc(struct bpf_insn *insn); static bool is_sync_callback_calling_function(enum bpf_func_id func_id) @@ -530,7 +532,8 @@ static bool is_sync_callback_calling_insn(struct bpf_insn *insn) static bool is_async_callback_calling_insn(struct bpf_insn *insn) { - return (bpf_helper_call(insn) && is_async_callback_calling_function(insn->imm)); + return (bpf_helper_call(insn) && is_async_callback_calling_function(insn->imm)) || + (bpf_pseudo_kfunc_call(insn) && is_async_callback_calling_kfunc(insn->imm)); } static bool is_storage_get_function(enum bpf_func_id func_id) @@ -9459,7 +9462,7 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins */ env->subprog_info[subprog].is_cb = true; if (bpf_pseudo_kfunc_call(insn) && - !is_sync_callback_calling_kfunc(insn->imm)) { + !is_callback_calling_kfunc(insn->imm)) { verbose(env, "verifier bug: kfunc %s#%d not marked as callback-calling\n", func_id_name(insn->imm), insn->imm); return -EFAULT; @@ -10963,6 +10966,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) @@ -10989,6 +10993,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) @@ -11019,6 +11024,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) { @@ -11344,12 +11350,23 @@ static bool is_sync_callback_calling_kfunc(u32 btf_id) return btf_id == special_kfunc_list[KF_bpf_rbtree_add_impl]; } +static bool is_async_callback_calling_kfunc(u32 btf_id) +{ + return btf_id == special_kfunc_list[KF_bpf_timer_set_sleepable_cb]; +} + static bool is_bpf_throw_kfunc(struct bpf_insn *insn) { return bpf_pseudo_kfunc_call(insn) && insn->off == 0 && insn->imm == special_kfunc_list[KF_bpf_throw]; } +static bool is_callback_calling_kfunc(u32 btf_id) +{ + return is_sync_callback_calling_kfunc(btf_id) || + is_async_callback_calling_kfunc(btf_id); +} + static bool is_rbtree_lock_required_kfunc(u32 btf_id) { return is_bpf_rbtree_api_kfunc(btf_id); @@ -12120,6 +12137,16 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, } } + if (is_async_callback_calling_kfunc(meta.func_id)) { + err = push_callback_call(env, insn, insn_idx, meta.subprogno, + set_timer_callback_state); + if (err) { + verbose(env, "kfunc %s#%d failed callback verification\n", + func_name, meta.func_id); + return err; + } + } + rcu_lock = is_kfunc_bpf_rcu_read_lock(&meta); rcu_unlock = is_kfunc_bpf_rcu_read_unlock(&meta); From patchwork Wed Feb 21 16:25:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 204306 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp1146553dyc; Wed, 21 Feb 2024 08:27:40 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWRDQipSvBOfVFYqkOb7Was0mTuoFXcRlkqrhq31ru5YzCsHRqButKJkwX9wznDEM5zDA5T36jA31ic2cTl/msIwuNCvA== X-Google-Smtp-Source: AGHT+IEgDEHX1HDMgv/GsNQ/FCFE83Ar4etwFKteTv0dcAFJmfbwuHXY7FitlTUQ+dn8XqWsIZIb X-Received: by 2002:a17:907:9857:b0:a3e:6bd4:9ac3 with SMTP id jj23-20020a170907985700b00a3e6bd49ac3mr8480701ejc.11.1708532860679; Wed, 21 Feb 2024 08:27:40 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708532860; cv=pass; d=google.com; s=arc-20160816; b=xd1ONa5HVCLH0sbJGAGam7SyDKR637JRqwM8TIjByYFXOsrtZdqmSaJt1YIJu7zebS 2wmmjZYsaRxlusTQoDMrfMQUDfCqLQGD65HidtvnGFbt+MgNRYuFTNxIw5YlmHLZDPBJ KtZkNYYUba8GlmlZ5zE5mUDpJByW77np5ZNwAh9Kc4jNWWLTtHXDgO6ydhNBPT040D4R 072j3Z9NrEyobxgPyhVbSxGUM0XAAdreOLGS8hY9QRsKNCrmMZz2mdHzkxdo5mK9Bukp nA3bUFkaT5nHwx1CfIG3mSEKU/T9OnWif4E7xWCcmMu4KvVCMdZRZh49oEJC5aTKLiVB e4cw== 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=+bgBgPHAB/M1hO7RwEWUaWR+//cgVPhyd4e3//G93Jk=; fh=hc4Y6F7QBSV+XcCZzl4ZOgjmnikSFu9Au/ny4/yLvAg=; b=XT/oaTTHvCxhk6bYsuG8h9IA51uKCQ6um2DS5CICZqrApg5Q3JAe2JKMBoPvrvQN3V AoskFv6RF0lEvQZIqCkIwXAVz0yUWWSaXeVPtNmKyARvhy/BClVChHbUhlPCYwHvYGlO f8OludW8oapl0CS5yXtpYTdDIfiWLcnFB7bxkcYAQlcPEofWyLC9EC3rbKs7si2/RHaJ 9wrW2I6KiutnSKf/DYfVtnTMH1IZ085HyLXkInnUDrb41ZcEKG3MhxpnRBOg3AvKTRuZ 8NByM8h5bKUYZhHDDuAGDslzzFYW2UcvKdwI9ERiEN424dJmQNsVYPIQ/OEbUMJPw0TD bJUA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=r7oMkj0B; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75104-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75104-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id u20-20020a170906409400b00a3e98876098si2909531ejj.471.2024.02.21.08.27.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 08:27:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-75104-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=r7oMkj0B; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75104-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75104-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 4D0241F22C14 for ; Wed, 21 Feb 2024 16:27:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3EF7084052; Wed, 21 Feb 2024 16:25:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="r7oMkj0B" 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 62BF78286E; Wed, 21 Feb 2024 16:25:55 +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=1708532755; cv=none; b=bftGGeuByj2BOMsewJJLl6ZCN5R/pOVFBiBISXzBD8GAkfjnLBLsbvXjGkJrsu+i7GqztrZcIWVDE3M1Fn+WkxiWy+bmavR43KcYxpaBggs4PVJT14FaKSc7iOKn9UnvJE0h3ggz9v6vzaeS8fzXsSityd8LMibwga8moAPi+y8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708532755; c=relaxed/simple; bh=BfdDE26WfSDQtzFiSKilP3VZNPmAkXOrXBWoKEcfTV8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Cf0X30+fhQPXXYzDUW6hgDfJ5f22VgK8PDwMarSimnOb73qgaU0UKtpWCitNhMMSz0QwhNs++8iy7Sr7hzFEFu1ChcKmJ4igH+xcRnWGIu0Qis71scgMeAGCWhvUSt5V74l6oyTcbUkx20xcXq4MGkuQYFSeX6AMCbUV6rIrAaQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=r7oMkj0B; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3A502C43394; Wed, 21 Feb 2024 16:25:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708532755; bh=BfdDE26WfSDQtzFiSKilP3VZNPmAkXOrXBWoKEcfTV8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=r7oMkj0B7d78kBRvozcUBQAjhj7uiAGqH+NshKHkhZtbzsD8VNfiGohqCy4+CanWK HySRrxjQM9TVw7KJbfxV8HvGO75sZBgYb+hybxq03jRsKOcRXL+Nsb81yspMqo9hsc sWafiP3BhwKUAsRV7cOJ0mcHVN4Vh4KHAZmtIogOW0uzm7pfcXGwHBGZZBi5AVB0z9 nZTMg3jIaCczXwIoUMB3ObF8AOWthxBR0b372g4xJPXWd8u7/KjCWB77suf2phisAm DJ4uM+GCuMzOxKQ71+agabp8aMRZnxlUfBzGvVn6RvW4o17uGtsYRv6YDKELbIXdZR onb5ef6mr94oA== From: Benjamin Tissoires Date: Wed, 21 Feb 2024 17:25:23 +0100 Subject: [PATCH RFC bpf-next v3 07/16] bpf/helpers: mark the callback of bpf_timer_set_sleepable_cb() as sleepable Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240221-hid-bpf-sleepable-v3-7-1fb378ca6301@kernel.org> References: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@kernel.org> In-Reply-To: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@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=1708532719; l=3906; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=BfdDE26WfSDQtzFiSKilP3VZNPmAkXOrXBWoKEcfTV8=; b=pNiiAx214IycXAS4yumeCfPJPdLGXqFyYpJxw2eFqF8jcawBNeTNAESSwrDtQ0URfqs5N5V9y 5q+MHYlzNH8C3XH0pOPRP5TKIb+/ou5EOG4+l+bmXU0NQNev6+E+GZD X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791526552932645545 X-GMAIL-MSGID: 1791526552932645545 Now that we have bpf_timer_set_sleepable_cb() available and working, we can tag the attached callback as sleepable, and let the verifier check in the correct context the calls and kfuncs. Signed-off-by: Benjamin Tissoires --- new in v3 (split from v2 02/10) --- include/linux/bpf_verifier.h | 2 ++ kernel/bpf/verifier.c | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) 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/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 91e583c6feba..4766c43606c4 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -505,6 +505,8 @@ static bool is_async_callback_calling_kfunc(u32 btf_id); static bool is_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 || @@ -1422,6 +1424,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; @@ -2421,6 +2424,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; @@ -5265,7 +5269,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() @@ -9478,6 +9483,7 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins /* 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) @@ -11361,6 +11367,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_callback_calling_kfunc(u32 btf_id) { return is_sync_callback_calling_kfunc(btf_id) || @@ -16830,6 +16841,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 */ From patchwork Wed Feb 21 16:25:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 204307 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp1146660dyc; Wed, 21 Feb 2024 08:27:54 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWlkf0Q4xxGNYjooEXWFlaTRquZcU3ABlxeh621JvZJDMKsnL2TQrOKXMgXEVedp2Dlv6VH4koobmuz+y15aCUWZFsmdA== X-Google-Smtp-Source: AGHT+IFeNry0W1GeHa2apiK0rwvgHknYgoAiq5FHftj9BB1Z/gdgCoq4vGJwxzih6k8jXqK62yUo X-Received: by 2002:a25:f622:0:b0:dcc:6e60:7025 with SMTP id t34-20020a25f622000000b00dcc6e607025mr18984210ybd.37.1708532874419; Wed, 21 Feb 2024 08:27:54 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708532874; cv=pass; d=google.com; s=arc-20160816; b=Djj92T/Nt4PlL90HJW2ql5Ndv9HsogIIl+/djUUlM5RLSXvPVSG9gY28nK3PPNx0XO yWpqLw+KN1ITehrKGxnA4Q46SbLJAH4eIS0deYyon+/2OtHcbsG4+pAHuncHwNlbS2K/ Og1IA3IWYvQIKIamINTe4O1VFAGEhQvqYTtAX3uIaIU2Kkxy+HBUnEAYSe2d2NJp8KHo +U3lmqxfxAUycld7pOsco/DBshvR99Cc+DQ3xrUkZtvn2T+fnN67p07UsYrdbunfY/Qf WWBqCLBdQVlUaebWBxKl7Gz6KQnDRsYJ7rW2BVCUk4RhX8hEZSU4997SHSAsyfHWyFNZ o2mA== 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=TJKw+AsLlnEjjlOKsgrZe41IFOqxNYjqahfqLxS9e5A=; fh=hc4Y6F7QBSV+XcCZzl4ZOgjmnikSFu9Au/ny4/yLvAg=; b=ed68G8St5yZ4qee3MY+QR9L59ZjyL7OW4kUjrCTMzinmLAwWfdXXRKh2qKzZy81ULt buoyLALSds4Fq1NhMuxBOmQ1BoaJb+tykvJaJ/pY3JsvEuXFSn5VpDQ5Vsnqkvvk0Ukl ktalGBatc+QrRiYz68tEJVvZV/ip3bIwo2GB/HxzLUTKGfwHZr/OPJig/dSpx2G+WrSA l+Vo+PWkLsPLireVpDnZ4YX3ZHiJcLEmrB3gdP769a7J0uPM3fsoW2vWLVy7rGqLpXDQ a8+jzuUf1KSFYnUG64ldZ4W08eMAoLIULfg5a2+M0QsjSj6bQdDY7Gc0tUjCADS9RstD +BiQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=GwbLSn6x; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75105-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75105-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id p16-20020ac84610000000b0042c501aa0ddsi10974877qtn.778.2024.02.21.08.27.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 08:27:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-75105-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=GwbLSn6x; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75105-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75105-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 29ABC1C24750 for ; Wed, 21 Feb 2024 16:27:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8422884A45; Wed, 21 Feb 2024 16:26:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="GwbLSn6x" 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 D4ED68286E; Wed, 21 Feb 2024 16:25:59 +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=1708532760; cv=none; b=HmJ65YMwo1tAy9o0DGzdWmi7P03+OmMnLVXooy/Tu4xrAIiAaLBRQ0L4QSvMFjXXZPmD0WBk/MrN76LZdP/J7EjughRxWDXZioX2s8WCcUCZ/cb8MQ+yWIGYnwD46aDN41/1L5n31cDe8Mx+Appa16yegqgv++ZMRlxAxoqtMpE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708532760; c=relaxed/simple; bh=lWc/Co5ySMroMa/eudRddSJ5CcdAiLdAB0D5VhBoAvg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ShwC4ssvoRxrDWmEDucjyyqMudCLg3DOetm7tXbr9rUp3QoeUKsMDqOEHOqIJgj65Tu+xF7X0clga9UsVm9wiSAL1JgrQTjcXdf8Hfpfi/0U/w5030Ur1uT8EcWjhs8I2WQDntcBE+/udH/MTHqy8LUiij3MTwQRhAlXHlbbBYQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=GwbLSn6x; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id AE607C43390; Wed, 21 Feb 2024 16:25:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708532759; bh=lWc/Co5ySMroMa/eudRddSJ5CcdAiLdAB0D5VhBoAvg=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=GwbLSn6xfNvIGSAxA1xlvN0b77mdW4g4P25/ipPqgH2WNHi+62OtpMBDANQyXnSim i7ki2gEQlsABylfFaHex6cdNqqkcNT6iK+Arhphll+f+Kv8AUX4oV+Y0dPdUJa2dN8 i+1ch7jrLH5E7aobE7omru6fHItFPwFpGEyZwruunze5APqYzEUfI0hS9wV/qH2rZq AhmX7rgXN7xDivI2idAs7nkVppZz86ircA+ZvmWjrfh0ZvdI88UERhKWvV2aQSAGYM +SdvDgsvD9nNrrAEQ8HMpwE77Y55+NJ56EaBDOzfPjplahzraWk4UKUVqOsSMXnt3H /Ca0U1ekgf3Uw== From: Benjamin Tissoires Date: Wed, 21 Feb 2024 17:25:24 +0100 Subject: [PATCH RFC bpf-next v3 08/16] bpf/verifier: do_misc_fixups for is_bpf_timer_set_sleepable_cb_kfunc Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240221-hid-bpf-sleepable-v3-8-1fb378ca6301@kernel.org> References: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@kernel.org> In-Reply-To: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@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=1708532719; l=894; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=lWc/Co5ySMroMa/eudRddSJ5CcdAiLdAB0D5VhBoAvg=; b=ma84XtgROEN9tdF4Dzdxs29bXjzrN6p+Au89dZQv99gHRtEVfXxzOusC8V/KtrG053LncKsqR IKtRuMr0yyuAKjr2zIVovJWW31GKuD6siie2Jkmepj5X7klAxLQuLpx X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791526567182943582 X-GMAIL-MSGID: 1791526567182943582 This is still a WIP, but I think this can be dropped as we never get to this instruction. So what should we do here? Signed-off-by: Benjamin Tissoires --- kernel/bpf/verifier.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 4766c43606c4..8a9f268c4ee2 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -19720,7 +19720,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 21 16:25:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 204313 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp1150771dyc; Wed, 21 Feb 2024 08:34:21 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUddg1VCh6ln9aitJ5sCmAwKGl97R1/p8JsJvRYX86FSnUZSLgOP2+dZToQbemZN62MmjkIpOcehWjaL4WQrrmA+hVOoA== X-Google-Smtp-Source: AGHT+IFduDM0IwtWIFsdeKhtPY+YUL3dzG/l152aoBvp+tDoFVCC9+VVU0KV9BuZNSfizT4M9pmO X-Received: by 2002:a17:90a:ce04:b0:299:34b4:2ea7 with SMTP id f4-20020a17090ace0400b0029934b42ea7mr10321748pju.47.1708533261413; Wed, 21 Feb 2024 08:34:21 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708533261; cv=pass; d=google.com; s=arc-20160816; b=RVBkx+qxT5VUR9kGuRvqzxCmsGobKProVMNlnAYsm6ohvyQccoBJbLMNhwvKbNBYSH vEI4LeLEa80dD9yxLz1gdX1uiWfvSLu4aoCxMo8+ClLTUH6iVQikz0/28+kDf4W9s8ou QwrER5dMJUhkIsAR1Jz+eEMrimNpGX2NPp/ifrSTKNYHAD9T6wo6MQ77L099rcPNXHOC BgQwoK3KKYb71Re2w/2swWJpziLtmi/aKdV8OolfNIGILnsy37Q4/f46GWibKC06C0Kz /pPDpt04X2sb5Dw/x+VIldwbR0+DPPKpx+Z3ukVzhd10c0DjtQf3aqCrKpHBKZjid9tb j64w== 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=y6IeKtLMXpVi0lqKw3sXmdW12OBr2MyJuC4Yw7a1CEg=; fh=hc4Y6F7QBSV+XcCZzl4ZOgjmnikSFu9Au/ny4/yLvAg=; b=hIymaXXAwfazd4mcPWWCoSJG2uMiBP1MG6lE6L987DvpKaNsKok/kKOW0KnHowmk2i AyZzuTyzyGR43dL5yfrBIV4zRe/J+dVskGJCA9BTEcvPFceDp0/td3qLkAg72VSWHgc5 ODBKJ/dfzxv0oryVNT9skpZGLPAQSk9liU4Qq2pusJTNkNe6O39yB00F3lNQiZ3cqeNt PSyuTsBPsvhDX9usEfyiIjDHsyIU17MQyVQ/BSvKM9Ayl9VV/+qURMovcl656j9HZKBn v3vMDXo9iUkfV3fezj+DFQSQ0plx9tk9fxdyes9QItz7/HlM9IHwVCFWNIxh/KnTEBfu Oc7Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=J4NLUfJm; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75106-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75106-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. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id ls16-20020a17090b351000b0029a39ecc6efsi260083pjb.56.2024.02.21.08.34.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 08:34:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-75106-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=J4NLUfJm; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75106-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75106-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 8A813B20FD7 for ; Wed, 21 Feb 2024 16:28:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5370D8287D; Wed, 21 Feb 2024 16:26:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="J4NLUfJm" 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 996D081AA3; Wed, 21 Feb 2024 16:26: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=1708532764; cv=none; b=KtcJuhsb2YpAQ/OEBTDH8KYhVwAcG3jYn6geiS5/nw84HixPLyCyVVxG9PzSM7P4CoWceP2xBlb4tuRn3OUNc6IyL85kwS+nM6aNQmbhggoEwu7qjqCvxDF85oLVnVR5Pn4Bw+x3NjybNgpzDTWURD9yz/wQhk3v5PDTypa2MzY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708532764; c=relaxed/simple; bh=TtpobMMu80H5N4HlAwS872e0X6zIMx4ZXZINEqECFG0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ogU8edUS8ngyk3AVK8gMEZVeMprQlXr6q3TRX0BwKfnOyr7Q2tssIgs9WQe53IBTql9tKvU3hiTxZkM9RqPXQYpjYZlYeSkG+6JF6bqnRagbmLxysBsSjYBR0gtd/aMbNlisVmSFtPgoxG8M1BA7jLJNkeax54s2XQUo+sofTqc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=J4NLUfJm; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2AE7EC43601; Wed, 21 Feb 2024 16:25:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708532764; bh=TtpobMMu80H5N4HlAwS872e0X6zIMx4ZXZINEqECFG0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=J4NLUfJmXHd/29pu0o91PmvPaBM7350lH59pHtHwEvmFthvtFmklHRcfBK9d36ggv kcmtcWf2dwCVQlexKW3Too2HIvL24/kkbeCLWgkxq2F8z7ThNSYX6wINhPQUy1OVPj ZFftLnvxlg2z7K6PYoHDbY/MxDVXZEwHx7EU20gvgYFmuA5iWQh4Hmf7Qq42s4tUs1 kbKv70zo9sdLbub67NwaPz8Jl0Fsq5fjquMf5rXATC69ZbrkBY2QusxZMapR6YaCmn mhQYm/7qOzVCdeNMq4qTDpILesI6ks96BHGKkVcf9rbbIEv8tj2jbdVF4SbK1/moGH cAPEJnK6wFMog== From: Benjamin Tissoires Date: Wed, 21 Feb 2024 17:25:25 +0100 Subject: [PATCH RFC bpf-next v3 09/16] 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: <20240221-hid-bpf-sleepable-v3-9-1fb378ca6301@kernel.org> References: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@kernel.org> In-Reply-To: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@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=1708532719; l=3521; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=TtpobMMu80H5N4HlAwS872e0X6zIMx4ZXZINEqECFG0=; b=T33LfFftYVbRgn8ui1NAZaKDxcIcF6nO+CzP83MtS0HpUbPOCpJ/D50R/OgWDqPW35i3EGMvT noF8L43G4s4ByXqsa9Tnmx6vjw6FZXDr6fjECinHtfnfXGAC6mB5TUs X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791526973107374028 X-GMAIL-MSGID: 1791526973107374028 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 v3 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 21 16:25:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 204308 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp1146932dyc; Wed, 21 Feb 2024 08:28:25 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVf9HUsDk4gM1FYh7U1ZhI1s2r+mfSFx+gwlYikRMR0YCr2IDUiNwpZdECmD+CBmJjdx0C4WkFGOJfsuSm/TvigWVKFWg== X-Google-Smtp-Source: AGHT+IEqIdqkRUnU6zcMwRQ7j7RHWw7/8eTdyQSjxwGFZ+l2ofd0MLOdU1OAB4qsJD0NG0cQNVPY X-Received: by 2002:a05:6402:40c4:b0:563:b7b4:a30e with SMTP id z4-20020a05640240c400b00563b7b4a30emr14863968edb.3.1708532905495; Wed, 21 Feb 2024 08:28:25 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708532905; cv=pass; d=google.com; s=arc-20160816; b=QSWL7pxfoD/91/kChn81us9Dm5V8VfOzvlLcS+OS29RIUTcLHsC9cq4PwkhbyKXETn Zou51MdMLmoPd3X83jRWmbUBPrCHFtAbmFXEBmHgdSaEiFh4juI9dnHIGXnIBb2YyCTK akuvSk/A/Dp/aUiiq3Q7X8vdo7Tr975hw/ViK6geTifzcqz23iXgxhqHsbONccvWDnsi NYnxyL0Tk07TnhLWriWL9B6JSLWhb65zlbvagZq7zQPGoV3AFtBwreUN3ZIyVOXWTDwX OwhSn9jIGwNuBaU2F4eUYLI+htJ0v8OfzIhSMQgub4KK44tOB4+Tpj87u8BrKnsmYOyg gagw== 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=UIjn76LCc6XLLSZFGLDYFS7glu78t58B9jNwOQhu+5U=; fh=hc4Y6F7QBSV+XcCZzl4ZOgjmnikSFu9Au/ny4/yLvAg=; b=qQpjWtzObwSueKoctBA7vABZBMzglNb3/OZ4QC/zX5Ox7Ppk2nTO9Ftqv2mJTfk14/ 0z2hRsnHiAug4fV0B+1vZrR3NdfedGZvmyZiCBvnw9TpfxprYMWaz2JaN879+iMW/7LU TShBGmK3pkJKhaSEszQKYLsESxfujVIuDWcyiPqacdpUMonv4CHIUu484nJKeygunFgR NkG0kEICswXp5YiQ/GcCvVZ9ZgKDjyO+8+GXaNP1zyot+dLTxQ7XXsrCo0zB4gssiTsm C3b9vzr2UJKr/eH6qGLkEIHwv0+8vzfdwblFDmXUqJ8CClV9RTVUqSuwwjwyAR/SjnNh P5Xw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=r1MB2yXy; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75107-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75107-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id g10-20020a0564021eca00b005640b40a448si3852809edg.657.2024.02.21.08.28.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 08:28:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-75107-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=r1MB2yXy; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75107-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75107-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 EA9781F231EF for ; Wed, 21 Feb 2024 16:28:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 14C2C82C63; Wed, 21 Feb 2024 16:26:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="r1MB2yXy" 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 C41B382884; Wed, 21 Feb 2024 16:26:08 +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=1708532768; cv=none; b=DjkA3/Lh+/qJIEnsSmSZhgDO8I1muzbEoOskhczaliYhEpU2aRwUXAHLiHNAg9gwY3kXXmmDh3EmDhV5eK1X2wNkv2FyuhCwYAuWxt5najQVuQqbNeAspuOBHKwhgXLR8iyfdOnPAhllkt/FYoCXOkI0/Lonk2kukDpvjZpf5Ts= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708532768; c=relaxed/simple; bh=rXcQTMVhTNdxc8wKJLIFnLsU2bjegWsmd/e5VtQdixo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=n68pKXud4dJVekb2MViJEliZkcqql5pTNizTG4w8JKTfuvVNtS3uf82fh9aDz4R3jkBm+I5lS2MuKwMmuzqMW12vcXa7jTejO68LbaKztdHa6++sZTJhzhoYBFr9SfmHTyLlj6v8sFAzy9JwXutnSDoCIxSI2SOaRxG2Lx84ojc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=r1MB2yXy; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9D147C433B2; Wed, 21 Feb 2024 16:26:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708532768; bh=rXcQTMVhTNdxc8wKJLIFnLsU2bjegWsmd/e5VtQdixo=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=r1MB2yXynh44q3o6ebNOQ4y2geXc6R/PiPP1GWtuHZ3JkGk+UcqpbqSxiU2LZaisj uoYkP9QxmjnZceKNrhsbwPiwcJ0s4BSgoTuAT/qna15ch23wHIvxYPKchUhPJIxGyX 06krh8Tq5SbtYlYtFyWoHyy6ggDBS+3YwlO8LuvrKm6uFUel9u6gaQMzVPKmqV7Nxc 8rSAzIZ4riwI1WEMzbKc9AxMXdsLRs3ofTzOyfGCPcqYkjkpzEYm6wH5loYGTHrVqG gizaD1/d1fqk//cC65znHDpJ1dEwv9QURqasYefbK9P2eR22uS/y8djVpB1ngdoIyI A7YUIpbo33Q5w== From: Benjamin Tissoires Date: Wed, 21 Feb 2024 17:25:26 +0100 Subject: [PATCH RFC bpf-next v3 10/16] 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: <20240221-hid-bpf-sleepable-v3-10-1fb378ca6301@kernel.org> References: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@kernel.org> In-Reply-To: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@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=1708532719; l=6527; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=rXcQTMVhTNdxc8wKJLIFnLsU2bjegWsmd/e5VtQdixo=; b=kdcNuUs4EPSE3drlI7x8oddYw1EdN/GSJZEJv877xlaHMyALXspXUKw/yinqS2oTtgoC+3G31 SYk+dhuu/WmAx1cVM9YVUSVeS4k1e2bVNm3lXlsqwe4tSRGeIWY41Ql X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791526600127749357 X-GMAIL-MSGID: 1791526600127749357 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 v3 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 21 16:25:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 204309 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp1147062dyc; Wed, 21 Feb 2024 08:28:40 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX7OE5I7trn1SBImvbJ9aiCbOy1M3vEBVCSnYkfqh+1NLnCZ1tykZLXOPt6KdW9ICSU/cf0BLCfJxkI4MDhrSdLVcozsQ== X-Google-Smtp-Source: AGHT+IEDc10W93jXxYo85IzH5AWEMZaV182fZhdPOabdby4SdCtWVA1SKInjXzNModvbH13N+ktf X-Received: by 2002:ac8:4e4a:0:b0:42e:15d0:c75e with SMTP id e10-20020ac84e4a000000b0042e15d0c75emr9348870qtw.30.1708532920668; Wed, 21 Feb 2024 08:28:40 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708532920; cv=pass; d=google.com; s=arc-20160816; b=LrrKpCEkgxRkP8lEUKqfIn+usZDmRSgC4AQrDb1mtGLNn7jZ/jSFXEkhq+IZtD65jN qIRlWJYxpIBkxj0p9KUr/9KOhf42qecP9jqv7Bqc822gP/3ncBqP9nrOsknhgKLo1RlT 4SdRDsNI/qkkA2dvv19Je0k0wl2uDRRF/DGtu3mzaMId4nUPPKIT/fqxi74jGBzBsaIu xFtZ2SDEIRBTj340p3j5ndPcvv6FpI4Cy2Bt5imwsO0/u5zMvOg5xygUTr9mDkUnfR6M z076Pyz0ht3jQlwuQ9bKs7DNOUXqVDdGNeumGh1D7k9QZ3xhQ8VzpJjdwqVIbZsXfXQS Pq4w== 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=JfDd2azw3CT4MRLGjD3i65pDyqj9Wy+upIDFQbl2JB4=; fh=hc4Y6F7QBSV+XcCZzl4ZOgjmnikSFu9Au/ny4/yLvAg=; b=jWYO3eiz22auA60ZJsC3zCuelFUKjAYGdfTDOqE7yWXbKTvEa8fqikxHnPJqJ0uMMW DP4MkBogCU7j63Zb/esSiJRFEVOfqtO9kIASWA7JObZJ+jhMq6TNla5yctFBYgSpuyoR 6irFCZVvxHcoN+MBF/NmSL+msJNgsFCyVKHh8tu35neDa8HOuWgM4qPjyt6SNyJrFK5V f802Jpenl4uFYAGIVEcFYlpgSn7LkQ5XjH7QhmQF3PuwuUp0a8HpkAjrNCC6rPCU52eL GCAai0xHyS+XdLtatkTyWFTk6S/9fwNtf/dbUYS7Tg4hboVfu4jxxRJyHOQRap1oTODi EdAQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=a534VnN2; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75108-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75108-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id j17-20020ac85511000000b0042c3325e600si10304562qtq.745.2024.02.21.08.28.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 08:28:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-75108-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=a534VnN2; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75108-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75108-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 550F81C24582 for ; Wed, 21 Feb 2024 16:28:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A0A4185652; Wed, 21 Feb 2024 16:26:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="a534VnN2" 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 92F5182C71; Wed, 21 Feb 2024 16:26: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=1708532773; cv=none; b=HzNFrCHgQ3rZKIyBTVJ49XcC4wAEhHpBX5SXVEpsfUXjmbPStfVw97Giz5ol3LCyIXh+xPnlalhKbgX2M2dshgK6zuizR2eBsHH/WIc/qXegyMpU93zvk6HJQu26a9XZQCiLhWenzLMbkN8jETtBtE1JTrXHOW9/L2Qui7cr5m0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708532773; c=relaxed/simple; bh=n/qD1PdO7BQm/8CDbkc4TE5R/ZFI6LEuZyfg0DJrltg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=B+0m+IYef1rlp8hN44YX0W+7hxGFI50kRVX8WNhKmn15eaQGZfNftu9GdG4CW72/zLYizh25QoyzvmKpfCJObS3cfgcJx9ZryIMNbhJfPrS4X3XWtbR6a5kQcUCJqZwuxmzu18aP4+PNB78QPVjJKqabcQyLV1OtEZ7X0aoW7c8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=a534VnN2; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 19F76C43394; Wed, 21 Feb 2024 16:26:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708532773; bh=n/qD1PdO7BQm/8CDbkc4TE5R/ZFI6LEuZyfg0DJrltg=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=a534VnN2cGSeFyzIcpebXICpan9hM8BisFiN8Ia4R6qcDOpuI8hWiXFnGuh15mhIU v6YJ3ZeRpzN014bN/T49uchEN9LFhfVMFAiLFnakh2WBJHvm0xPv8Tb7h8v2xrNspW 8ovrEsiWZY0z3spVITTJagkodApH2PdybEIB2ZOF1ye04D2sohxKrU2sV0VMlXEPdj GxlHhEzoQPAcfVz/Jsoxl63PjfuWfQlVPvv4+Pd0ITwaboWI/hrqez/ptrJhcMp3F5 ihNpBR0dRfG/xzOdaOo7l8JVNqipf90mBgmDwqySc4xC4nNapFZKYXt4jQfzijjGa0 3AoWzog+4TTnw== From: Benjamin Tissoires Date: Wed, 21 Feb 2024 17:25:27 +0100 Subject: [PATCH RFC bpf-next v3 11/16] 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: <20240221-hid-bpf-sleepable-v3-11-1fb378ca6301@kernel.org> References: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@kernel.org> In-Reply-To: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@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=1708532719; l=5145; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=n/qD1PdO7BQm/8CDbkc4TE5R/ZFI6LEuZyfg0DJrltg=; b=uTCXux7/N75Sc+Frx3otA1/fe9LmjEsIOavqXLV3OMI6U7KGkj3LyXgOs88x3SdDjw/HhGEyQ UkoVASZtZvKAkzP928AWaaJ4HroRpVImDKaTaGgD9Ypa9EM7wGx5K6M X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791526615696811655 X-GMAIL-MSGID: 1791526615696811655 This time we need to ensure uhid receives it, thus the new mutex and condition. Signed-off-by: Benjamin Tissoires --- no changes in v3 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 21 16:25:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 204310 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp1147218dyc; Wed, 21 Feb 2024 08:28:56 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUwtT0TmmZMc1UvhSqVjA0mhxwFztR85pMI4SDrErNlbfQWbjO+L0c2xF9/jGXba+fCgpUBDaD+m8HVh7Hyy73bBrJy0g== X-Google-Smtp-Source: AGHT+IGLYO65t6vImcqnOyhtbXc/qe41Q3gFK82pqUsJEEjKgUMALs6JqTEatKkhj84eSc8FnaYF X-Received: by 2002:a05:6a00:9386:b0:6e4:6793:5c6a with SMTP id ka6-20020a056a00938600b006e467935c6amr9082989pfb.7.1708532936535; Wed, 21 Feb 2024 08:28:56 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708532936; cv=pass; d=google.com; s=arc-20160816; b=SvLepwsuUUF5lfPelq0uwFGux1nVGo+tuQ2P5jxeHzUo0KLBruOFZcV64GUHWJHbhE nIp5uY1+0VvWXAI39u6uzVJxcmo9N4SvZsTZ5gfnJEtxd2uAngG0Wdp5R+1Fw8oeYL3G k/FwfeWzZ17/SVU7kohRzAarJsTWCpzskDW6cg31L6dMUFHJjpj8D2p0zQLZ3Mi6Ue6A 7nhd8v3LAAnDCk49MrTLNLgIxOIYbL4QCrOBqJdXunQoZMzvhlJThk8fhnWCYBHaaU82 SNUI+HDx0kqfzkw06ktyQjz3gj1G8FTGiK3UmXtc1/ZmaX5xKvrtLOzd2DhmxgKaNkl0 u/rw== 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=3ifGiCIeS+Es8jzLu3+Y+sD0raTcR41fBM5d9jl5uv4=; fh=hc4Y6F7QBSV+XcCZzl4ZOgjmnikSFu9Au/ny4/yLvAg=; b=kLt4NXEY8u1ySkwqJOBjK4CXN3tTzEtAEAZGEFgC25x/17YM7tq19qlvjdx2f2LBqK YfL/sBli2Ooz/edw5TptqctRSqyrxlPudob0eNOQRRGXar+/MlGK2XWx0/SR3GkjuP9N lvizhIq3osrc90mQI5cNjHzZentic5O9VmHN8mNln3IDYq86Nqnd4EtVMx1em2T1TCqq R4wJtHG44PwYy3mjsAk8kV0Q63EJbADmLp4PAiVM7uVJeYSL853kEUJ5P8KOdeAadYUP 7Yaj3XEqj5Ff3Ex01YHH0DJdVJtgu7edr+0LNMlnCGANgyVIhEhotSx3jciMBZC/kgGL 7cdw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=k+7u+Vmc; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75109-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75109-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id q11-20020a056a00088b00b006e0379f3935si8641750pfj.261.2024.02.21.08.28.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 08:28:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-75109-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=k+7u+Vmc; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75109-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75109-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 4B618287966 for ; Wed, 21 Feb 2024 16:28:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C5B2081AAB; Wed, 21 Feb 2024 16:26:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="k+7u+Vmc" 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 ECC7882C71; Wed, 21 Feb 2024 16:26: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=1708532778; cv=none; b=qvzp98fBbH0HWIt4uwcoz0yGqhwhwST1i49HM0zd8zhLSSEopUMLVpLFzU7B+jTN3w5/4Ut/Ygwbdezmzu1TouY7xNJQJeowW2NydTfUcWxG+8HrQMVX+iBNnAZSyl1C4yvse+q7mBvTODJiv8UPwyJIirj9TyvYE6XE6N7yGAY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708532778; c=relaxed/simple; bh=iKXM6sl1EhTTyHX8QELSkIyjx+AwhteZbGwXvEk/F2w=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=hJ4xkuQXAqCjDx8CrQJl9jQj33yqt842CjVycce4eCT0bJ6NzDv2NnwdrWW3S42Ldbp1AoNYoOKFkLbw6PIMd2Hr+whDEAM7Eem3K0EFF0Lmqydwg53sN85qCaDdAbEUFZPyOa2bxKYR5QPLKoz5Thwx/lkHlTT737GBichuMHw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=k+7u+Vmc; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8C3D0C433B2; Wed, 21 Feb 2024 16:26:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708532777; bh=iKXM6sl1EhTTyHX8QELSkIyjx+AwhteZbGwXvEk/F2w=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=k+7u+Vmc1aSkO1N/zyEpFPuR4z/WHiPvoD9MtjntyyayqqyhWl5BZ/VsPlHsW3cmO aLz5KpVCBLueqisOi8/V03xgcwm5Rdv1xxNgiNuCO1ZdaANOwggMsFNx0GqjCGT5JQ /FKCTq/7BJ+BCCrhXKslR2YoSctu+WZoa5W4pdixhFed9QGY2MEV8g0DJBSCdWG8wm UqhsUq4iQmXO1SW9qTeJ6Hkq5wIRThCIRfCaewL/B8NQ/uRuGGMLtCNOqtFV0szfwE lCOc/eIRy5lYyFiADZJyAcFNzOwNrhIww0AOfVWGoCDg8nfiKqLsrMEmLxd1tVkkn3 SFi769BRKnY0g== From: Benjamin Tissoires Date: Wed, 21 Feb 2024 17:25:28 +0100 Subject: [PATCH RFC bpf-next v3 12/16] 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: <20240221-hid-bpf-sleepable-v3-12-1fb378ca6301@kernel.org> References: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@kernel.org> In-Reply-To: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@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=1708532719; l=4142; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=iKXM6sl1EhTTyHX8QELSkIyjx+AwhteZbGwXvEk/F2w=; b=k1H5/z0nr1x3VI9kFiBv0l9fBzI8F+Rp+KTKDdncd+7ilb3GGj8OujU3e10101TdFMnJtZ0Au F1ogARjyn+VCQ1eDT0Q94aiWoc0FaDpf9aaxH9mDMvyZlXxTMTO5K4K X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791526632206603434 X-GMAIL-MSGID: 1791526632206603434 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 v3 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 21 16:25:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 204314 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp1151304dyc; Wed, 21 Feb 2024 08:35:17 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVroYHoUhrbyHHw0VxP9z8PBtoLmdP7kBcM2YHB9T0RbQ811gvL/N0scOqL0V8FBiDNEV73DbcVltLBemO1OkYl/e2PXA== X-Google-Smtp-Source: AGHT+IE0gOoYAifPnbc6FqTQqHLCAmtdsPwX5lDfc3r9AewwnEQXn3I7j9/FMrJ/ccalmcKpgmmu X-Received: by 2002:a05:6a21:3a47:b0:19e:ad09:a571 with SMTP id zu7-20020a056a213a4700b0019ead09a571mr25988804pzb.29.1708533317193; Wed, 21 Feb 2024 08:35:17 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708533317; cv=pass; d=google.com; s=arc-20160816; b=PyWCAvAMyW15FhUDB0LTZ5SDeroGdxRcfE+pLKyIrAsVLQFVjlNedSBXYw908gWYyY kcbndlu5xvIsTd/c7Y5rcJSH+5NoQoYYLctTwpdFiyiHVwR7Sk3KyPNgBN52dJkOLBHE ShIZu/ZuAUnAZzPMs1pFMvoUFeH+PQRgLwChpOLbv8Mozk71TLmo7NQjv1GYDdNYomMZ R/vsjpw2pfZsR2nk2bHJ1l+yziw36lBLGg99b85iCtQzt3a6YKOo7x5ZgSSw1wgfgVWk FfYwW4SVDv5KOwd4PDTY+gmPIt00onD33Q/2vQOVxu1SpAaXADV6xBV+PuKgTKq9vTPZ F5jQ== 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=eXvOK/vC6KXbI/u7wRnH9tYG3w9E4CDHP8e5yG7WFFY=; fh=hc4Y6F7QBSV+XcCZzl4ZOgjmnikSFu9Au/ny4/yLvAg=; b=c+rflE6wzuHe0FYmzlGSdhDam8WEHDpOeDby8rTKpeP4BQxDj3EbkfsJ7fkFX+qTK/ pSoe7IS13+FKP9Y5e5eejx8ly87MwUbyeAsBmfHiKd0vjhs5EuRZB2Xl0xu98q8r5xQB omO/bW8Jl4ij/EA6/syGzESO4WEyhrAR/FCwJCe+LNU7L0FZLFIZV5rZN/YBCCPa7ZWo Fjd3ECp1pvlsExhPYy7PATqjIynYBiQanOfiJxTJWXood+mPikOUsHbmw1ki0SE4kkBZ cKmMUYEqmOfUfvQpwMJhsnjf+V9NPAxQ3aIHA/4ZUvs20pKdTw7Z50oGwf796juJRy+K SjVA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=C9NRW3Pe; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75110-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75110-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 cm13-20020a056a020a0d00b005be03f0da68si8829505pgb.13.2024.02.21.08.35.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 08:35:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-75110-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=C9NRW3Pe; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75110-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75110-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 C9A9BB2295E for ; Wed, 21 Feb 2024 16:29:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 587908595A; Wed, 21 Feb 2024 16:26:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="C9NRW3Pe" 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 6F69D81AAA; Wed, 21 Feb 2024 16:26: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=1708532782; cv=none; b=HjEIRNV0V50um2Xkr8HLNtBH2Hu7kZf0QNkfvVeI+4BBamm4uComMPIk5TzYy0wkUbIwkjF3xjxG1jCmOJklTACgzJ3WiIPUnTkT/+vGNRs/qn+Jkp5j2wfzKIBsf7H+DDoPl+ktaLcau6Adl+qx7IQEiRbKMe+k4wpS2ymGYt8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708532782; c=relaxed/simple; bh=+rMCilTz6LtxI50aPniz+3/TXiw2f8owICLVx5G6iGI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=dK9N+Dk6JshmijLBTqxZokC+wcBuOW6Kf/YM62iqdq7VdMcSuXzINk1r3be4ZzI7P+VBNTp5uOEE19KbMoHMiz6jbr5jKK/e9YQt7juqPSJQr9xU0RrkbwJf/5oPR2lGLYlAhYX2KYrecXMf8YStW+kiXhF6nYyyGaCEOdoql40= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=C9NRW3Pe; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 09698C43394; Wed, 21 Feb 2024 16:26:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708532782; bh=+rMCilTz6LtxI50aPniz+3/TXiw2f8owICLVx5G6iGI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=C9NRW3PeX1w+SeD9HHF8O1LkDNYGHkkLapUGtyUB9ZUzkBzEtme4dRJQF4+qRz6ZX hAu5tUJbDeTlpEfPuw0SIWJkTca4QmRb7IyOjs15yCRMF36r4l158UJ59+hBfqDAKG N3GkND8wtgaKLKk6tu8BXniBrHuO9yoP8pRo3d+dBgQ5fSskDDUTpVI6mDiMpIvRIF IAnDVk2vcy5JIlhq+1qzG6gU0Om1U9m1KS79yrzi6b375aAZaU4mPXVZD4VqmFRDXI Ysjy3op6azJhIE1uz7vuJXdEUCoKYji3Z82r5z/OVU+bTk8lP8Hh9ZX/C/F0cTHzG7 1AXPiYtubkyTw== From: Benjamin Tissoires Date: Wed, 21 Feb 2024 17:25:29 +0100 Subject: [PATCH RFC bpf-next v3 13/16] 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: <20240221-hid-bpf-sleepable-v3-13-1fb378ca6301@kernel.org> References: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@kernel.org> In-Reply-To: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@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=1708532719; l=4226; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=+rMCilTz6LtxI50aPniz+3/TXiw2f8owICLVx5G6iGI=; b=LYROSoJi4yNche7QagjMXbKZa7M8HPI9SmacfOXgiTAgyJB85n1lNL7I6QyiyuXsjBU32S9QJ B2JZ21bgLeBDWmTz3ymzFsDfmU62sQai7VfNgkoA6/dNT2DOWVv7vvp X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791527031741729201 X-GMAIL-MSGID: 1791527031741729201 Usual way of testing, we call the function and ensures we receive the event Signed-off-by: Benjamin Tissoires --- no changes in v3 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 21 16:25: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: 204315 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp1151596dyc; Wed, 21 Feb 2024 08:35:44 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCV3M69vRRE7wTNGrdXpfQ/8U3r4wOsc6z3xei4OgtXkkS9XbVxdGym/VpVd6SK4nJ6mmR2MAeLmH/72b8gs040cww6VgQ== X-Google-Smtp-Source: AGHT+IFl/A5u+Nwd6UQYWKW2APRM9/XjOEyZhLjRXRggGQUrbdipMjdHDVSz+Xcpp95p9EekfKv/ X-Received: by 2002:a05:6a00:80d4:b0:6e2:de02:598e with SMTP id ei20-20020a056a0080d400b006e2de02598emr10432895pfb.33.1708533344018; Wed, 21 Feb 2024 08:35:44 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708533344; cv=pass; d=google.com; s=arc-20160816; b=aYh6tG/qwmd/ZUiVcTVZvFe+uwZXH4YDGpFRsiGzDiOK9jsv0qgrAH4lJxehoGFH1u 5g9An9UhJ2KVmrALdBAYFaI9akdqJsdWO33chELhlODnJ+LG2twGRZFMMDGZydSGHr+p BU7jH446hGuJOg5IF8IIxpRjL/UUI74xztMZCQFN8XgLE68C2JOmc9F8BvRpBlSpbyPh FqbJzeppHXfq4COBfdpylqA5iStO3nfRTYv064YeHzZLzyGiyUM6noHZwyDK9tCnawAj tYVVY+AMBlJOLR7qd1feF0ThhHuqofP/OLxswk5nyn7DfRM1tlsPtxqwgqNzLu2YMLbW KyDg== 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=i2ofzzXR66lmJsG2e2Gkjtpj+cQplTDfpB2Jsno6/LQ=; fh=hc4Y6F7QBSV+XcCZzl4ZOgjmnikSFu9Au/ny4/yLvAg=; b=qXD+eSelWCc88FOLSis4Q8/dJbO/N/A3dixIE7po27Xlej6/le5J+nCy0SZu4QXf++ 7pNUS9Uvc2CFgB+VvUvcW3S5zy8EUIiZmz4cHQlZ9T3s/kST05Qf6jxXHDBTrZcWpDpo J99fZCVJP1uz/GwI5fP/mbqg0wE97BPdu4b+ml68KHKIc9all92h5WWoRAL0cYZh8tXH G7Xqli2Bqd+JiKPIERni5uAb3QLn+CcSfSpBjNEi4mgl9ebVg6kvVcjgSs/jPkHjRMHf FXdYjYL+9gfoc7NcxvNMwNod98jU95uMK08mkLavkyKzznDbMunSPAaQ7qUv5oG8MSym deig==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=CRuFjLOz; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75111-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75111-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. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id z18-20020a056a001d9200b006e466191f79si5265024pfw.150.2024.02.21.08.35.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 08:35:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-75111-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=CRuFjLOz; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75111-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75111-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 8312AB24CFB for ; Wed, 21 Feb 2024 16:29:29 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A030582D75; Wed, 21 Feb 2024 16:26:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="CRuFjLOz" 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 0975782D6B; Wed, 21 Feb 2024 16:26: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=1708532787; cv=none; b=lp6r3Mla9YeMsxmbE3As/eSfO9S9tF25oE0ScHlBQcYqiygio/DwXMTrA0cC+VZLu/ywGRX+bHmQMhnp0Cft5n5ZiD+Wa8z7lk/d45S8NTDGTMbQN9Ee/51hutCtki/XXcVfCdRxILFgOLiwUTHgzDc0Vvyfw35S2lkRBWsufCo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708532787; c=relaxed/simple; bh=aOeYWbBo6BBRgng3mWTrVA3C2orudmt4XB516sHxv3U=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=WtqziC5hk67sj954vTOTgIxR1U9j6g2EaN7OPhIE5pyNCK+A5IxGvp5mB+Fzfwcr9hw+rpnUQV2cS3FZsAxjjCFMBaMpqV+vGb8np5gGz/lnIJ1iWZSv3+8rt2DBWdQBj/Z0abbgEcLaccQCtK6dwJeNhG4NH4KwVB2VZqr6kFE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=CRuFjLOz; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 78DEEC43609; Wed, 21 Feb 2024 16:26:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708532786; bh=aOeYWbBo6BBRgng3mWTrVA3C2orudmt4XB516sHxv3U=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=CRuFjLOz/oP9LGG+DScB65m5dNy1A6QCZ1/OvkXlvdRUIXEZV6/gwerU/2EOSHT4l p0q77VyVFVPTMftWeWMr/mW0huGodOw35WDnRdeLbfS69eL8ke1nfNk23OvUJmPZFz LZdECWQBTZedf8QDjGziZQz2Wnp5D6G7N7/R0Gipm7WvKYSxfnW4RmVn6QzF7FRJPm YLc/C2cR21IaHUjGiYpGVvRIOI5eYxfGu6+hl35cutkdk8E0eGhTsuhpNQ99NO9F2z vKI1odmAjBN56jZ6z+vpV2xp21JGNW2hYxltVoTWgDmH3wng3VUJO98k0sFfs86ymb A3vX4C/JmUJXw== From: Benjamin Tissoires Date: Wed, 21 Feb 2024 17:25:30 +0100 Subject: [PATCH RFC bpf-next v3 14/16] 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: <20240221-hid-bpf-sleepable-v3-14-1fb378ca6301@kernel.org> References: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@kernel.org> In-Reply-To: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@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=1708532719; l=2207; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=aOeYWbBo6BBRgng3mWTrVA3C2orudmt4XB516sHxv3U=; b=5c6iFAL0ghfRa0CQ+I5WiS8W3VZHjDLOLVCvDikeugMAA26k2NejVsty4Ye8iMtnIQGyGpWAe kxEB5meSPzqCYxWaCyomP6nK9/TqKeiFZNYnSvFjr5lFPycq+ARmIK5 X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791527059885969302 X-GMAIL-MSGID: 1791527059885969302 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 --- no changes in v3 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 21 16:25: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: 204311 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp1147666dyc; Wed, 21 Feb 2024 08:29:44 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUyteYTUSNQBAfbIoDumfl7OAqodBIOmZG5W7D6ODx1rG0TCuxkoWYqBUs5zg41dX65yw513MM/bbedgI+vbT6fEykFvw== X-Google-Smtp-Source: AGHT+IEJECVPMAd/O9PaccUK6SKMf90Uz63EA600+dIvKgU6+k1nZMFun3ZqtZMyQY8VXrM8cC4r X-Received: by 2002:a17:903:1209:b0:1d9:907f:636d with SMTP id l9-20020a170903120900b001d9907f636dmr34061984plh.10.1708532984444; Wed, 21 Feb 2024 08:29:44 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708532984; cv=pass; d=google.com; s=arc-20160816; b=ZOjq0JnP2hcWHP1OzAk6zWlcMLaETUrUrzX5Y22IWyN16W3GPlk4Mn43vDk6DIDOar CKUhRtmVbqjJg92bMuY1uB9I5j9wAs4ilcAWQxxYOuxdUKSjmyxpAVQTBDEwhjJHpvTL DLUSLFrlAf3b4lCPkO87gnhdea8z6IlAF1IZv0nov/ky2Rhf9DEe/2EVKws4DsBTqYBN Ey8lJNsfHohdLNtnNh+nqvx2To8hDCEvc7lYbvEikaqYRrkY6PnRLNNqIUNHcbR6oclQ htQS9Of3m7+HCFkqxtCUZ1nqbGKRMkGwRmdu/b0ry+ibroJUi4K10gEgbmYM02qL9t5X Gegg== 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=z+bMpHbwNxmX3BTGn+pJubRmV3F0QMjG5AuVV4SsC1Y=; fh=hc4Y6F7QBSV+XcCZzl4ZOgjmnikSFu9Au/ny4/yLvAg=; b=LmQDacm27IJ5+7OG6RJk+A9hY5tm8a1CwoH/wdjBUbrBOWSm511feGnoMKK1fNAhru DXzPztvL+tSTVLdSoRcHJ/fX0V/mwc4B3Aol1KqUz7EuwK+HhB0wwp3mfotKS3zBo8JK dOjqnsLEMns6kEk6loZ4x8/jve42ZUincp+Sek9ys3Ky2555CbcPHQFYKVzyjz/tkTGo n+41gHPuGxF86w9bVFdT1OYqA+Sw2PFbaQyGJEzdrQgUMmJnQfbm1twB8Cs1f2/89e06 bWNX7lKyjemmEV1o0QZSHr0w+GERRxQ440iAa/0D8SLz6R6jd3coYg0fIsmeBOdShaP3 Ck6Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jvPtQ9yi; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75112-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75112-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id im9-20020a170902bb0900b001d9624c714esi8396512plb.512.2024.02.21.08.29.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 08:29:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-75112-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=jvPtQ9yi; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75112-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75112-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 4151B2876D3 for ; Wed, 21 Feb 2024 16:29:44 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F1C9F8614C; Wed, 21 Feb 2024 16:26:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="jvPtQ9yi" 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 612A182D77; Wed, 21 Feb 2024 16:26:31 +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=1708532791; cv=none; b=RE1I8DxTseXsP/bQVGeTICsxqcAqJKPtEF3AkUA+5R+QI+A3JoFHrAQ32qLO1eQT5XXhTgPBVJP8OMijmeekqy5YJJuKWPVDGr93w8FoxJNyxQssgy9CQCgNrMkpRkCSHDhx0V7XHO+YwJtsCgv43hB2SJa5RjJ/C5ZVFYGDCDc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708532791; c=relaxed/simple; bh=uFqa1SLCaUViBoH/OYHQ0iYiqRPFBCZ/HsiN0L6yk2I=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=MbIb0c0+JdTwisa6ridjGodRGIGF+6WCYJjoEKxP8NHjslGUElbaTPb4wIko8aaoUJ26LQ0NiggwFKe/Vk/d7pcvrMDDelANcn8i0bHRJepznuv7LfIBFW+3dFSLzGpgTVLhm9nSSse1t9HsATBwCpBaJC+ZYduuePRQCyTnOBI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jvPtQ9yi; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id EA99EC43390; Wed, 21 Feb 2024 16:26:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708532791; bh=uFqa1SLCaUViBoH/OYHQ0iYiqRPFBCZ/HsiN0L6yk2I=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=jvPtQ9yihE49v9Ew2xh8ToPyc0J3cU08IGWFxiG/ShjpNq0IAxlXPBm6dEdcMWaTL UcK6NNUytrAwC0X5YcsuowlOPfVWTf9x0ivdf4q/OrUqh6nk8OyhMwTP4utovKeGrT AOfpMI1NusxYGDCAA7Vin4TGHVrdkbv17T3rV5hO2qF/QNAGmls3Js58qEzMb5avHT ydNJk2VHsRR322Fs3NOukc6wrPeMvLI8Q/5djTwGs3Q5CuaWq/QfRPN4n/xQU9woMM JrKw3Q02vrpuVtA/DHDU6NKvsI8SoXMhFzwic+J5WAoKZruNoYvVfB4bq6bnM3QRaZ +fsS9sdGUsbOQ== From: Benjamin Tissoires Date: Wed, 21 Feb 2024 17:25:31 +0100 Subject: [PATCH RFC bpf-next v3 15/16] 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: <20240221-hid-bpf-sleepable-v3-15-1fb378ca6301@kernel.org> References: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@kernel.org> In-Reply-To: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@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=1708532719; l=8253; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=uFqa1SLCaUViBoH/OYHQ0iYiqRPFBCZ/HsiN0L6yk2I=; b=YZnsdAt39+fJdwZpL7xIMeZqz3RIqrOxdy/X4PpDsrYWgIcjWRBvXTozHTdzNsiuNiepZidlv 2LjSk1qtpVCCUel10ER3sTxlTX51sN0AhAJISIB5YscPOTVBQyPCNX4 X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791526682763316065 X-GMAIL-MSGID: 1791526682763316065 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 --- changes in v3: - amended for the new API changes in v3 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..7afcc77bcc29 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, 0, BPF_F_TIMER_SLEEPABLE); + + 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, 0, BPF_F_TIMER_SLEEPABLE); + + 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, 0, BPF_F_TIMER_SLEEPABLE) != 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); + + 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 */ From patchwork Wed Feb 21 16:25: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: 204312 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp1147824dyc; Wed, 21 Feb 2024 08:30:00 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVOo+E7FUA8kCkd1/1Iv8W+vKKIAx/MsHB4mMLBP+WwnTaUajglDaJdBY56tFYXwIi2RpYcw9jKcfzo2t4nwDWwz6/YeA== X-Google-Smtp-Source: AGHT+IEUw1yYhCAiXXqV1boO6GwzoXAW3Sg6NHA5YYzXWaIrqC7NAfg2Pa6mBcrme1k+RE9tdnuT X-Received: by 2002:ac8:5e4b:0:b0:42e:2b80:72ca with SMTP id i11-20020ac85e4b000000b0042e2b8072camr5124525qtx.45.1708533000083; Wed, 21 Feb 2024 08:30:00 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708533000; cv=pass; d=google.com; s=arc-20160816; b=s7v/5vBM5q4oJ9L9STnxX0w4AuJ0/vWWpVnw043858xGci8pmMwq9ZXE4ALgv4bc1X sI3Kkqfv5XShbdjTsjYk9TnppClmg4eqLB0hCu2WbewcOz8TOecs7R5hJ/leDFNs7OMF GFm9BhEk5XFMeVV3nceB0e2H++BflbmPzzxruWiUcHtdPjEUMI7KbU/WLs+b0EkmRexd g6OA6vnlxqFZf/HPo/3i6WOTH8g6arnIvJARp67cYi+Ne/Vwr4Zutqx04F8uaLe1/R0Y xXzvD4thS23A7v2liI0nQ815TwYjEUGR/uCIabODGsvpe7aAQ5bYyIkBIlgQfjojB8b1 5Mlg== 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=hxzCxPQqPUfSgTpJYfpOymT5n/egnFRwF+d+PX7enns=; fh=hc4Y6F7QBSV+XcCZzl4ZOgjmnikSFu9Au/ny4/yLvAg=; b=cDoZgnpIvgezZ0qBLQ8d6L5iz2z5SaJQ3XPmtgTrqJMV+Wx405qIrmahVcGHzUQ/YT 1xtN5imJ6otkseWO2Rq7m7Tit9bmnzrNbd2NG044UxX1gV80Yfm8/rRhgD4A+345sZ5b 4y9DMkIinAgtXeZDSC+0qmW4uhaziDYwq+SCmiK+sEHDcW4uuxhX3wZBWCOE4kK0maXY uahi/iJchBWaWMqVdtzIsf5QRwrMeHxGIkl1I6RlZ4G1rbFZVAdKIvR8dcNqA/YB1bCD qce0G2kRSXQ86te7rEQWKfR+5VJDZakPEiAY49/yj3PEHGfVwvOOK6gTmSL2KvdFMahI D1ew==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=tW2owCt+; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75113-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75113-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id y12-20020a05622a004c00b0042c5ff0e29asi12144414qtw.740.2024.02.21.08.29.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 08:30:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-75113-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=tW2owCt+; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-75113-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75113-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id A7EFA1C247B4 for ; Wed, 21 Feb 2024 16:29:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3085D86640; Wed, 21 Feb 2024 16:26:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="tW2owCt+" 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 8E1898613F; Wed, 21 Feb 2024 16:26: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=1708532795; cv=none; b=e0RoCZMjFXdoxQZNMS6Ebop4eR7quDJ+/YzB1bpD40O6CGjVZojkzLPoy+v5tXzYljuTzXRE8Z8wyNegSAfZDQSdpAPsw4zWYPmiv3+SDEV9vBt7diGD0CKp6+YFDpHKm3G1KhNzo4lZQmqZvbz8EiqJRxiEdATuEEn5b3+jGrg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708532795; c=relaxed/simple; bh=FSFPqOox8jy+6us2erHCBIcxyNaaYOx+aOuItwb5naM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=RwVSu1vfjQ3e0WV/cnjFGUqo/RQ2VHP7qvnNb3kdjKuUqrSyBjp3UeR4qrp2juYjKSsLcEMp7BWOJ36k0oH/k9Vw9N7UTdFzr+lUMV/iqYkAql5ES8vaooHo/wt3NAAVqZ2eYPKHSdGQvBmVRcshLRagwW08TJHLymbmoWu0keI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=tW2owCt+; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 65EB5C43601; Wed, 21 Feb 2024 16:26:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708532795; bh=FSFPqOox8jy+6us2erHCBIcxyNaaYOx+aOuItwb5naM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=tW2owCt+eidfjnZErOd6UZdf80iVKOoRCZ/dpzQwWJy1xOJOcoqzQ5M2+U03abqLS 2eYJR3BZOBKr9VzTln1c6qZwaWnNwrBvqXeSY1XkIkcnc0q5unKP2EUNZZN+zBOvPB 0LcbyYIfW7tD1JAFx3zAuyVej64OwUfsa71rjTLyDilvbFCF2ZHexpayVZuzw7lc2R 1eZcFuTqJDTYH060SXuE0kkhtyvnMivAglis0vt+AHEAFzY3QKQ+QjJjwyoffReJ1j 41aAHQu7U7zrmJ0mdBDUpcUm5LCUPw19sNQD1y2VApexlpnG0okzvqDGBVx4BpzLiY mz++cAnGIipCA== From: Benjamin Tissoires Date: Wed, 21 Feb 2024 17:25:32 +0100 Subject: [PATCH RFC bpf-next v3 16/16] selftests/hid: add KASAN to the VM tests Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240221-hid-bpf-sleepable-v3-16-1fb378ca6301@kernel.org> References: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@kernel.org> In-Reply-To: <20240221-hid-bpf-sleepable-v3-0-1fb378ca6301@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=1708532719; l=600; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=FSFPqOox8jy+6us2erHCBIcxyNaaYOx+aOuItwb5naM=; b=os7H6Nw/gEfbbLdcePDV54UogZsbsOUmDrFyLvScf7QTRZCgWAui2Sw4Yz5yjwtlweqqb1KN/ hafTD3+SoIdA8qt+8z4AB9K40VwVO0llfpGdqp5IY19Ibm1gvzmaQMQ X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791526699016538885 X-GMAIL-MSGID: 1791526699016538885 It's always a good idea to have KASAN in tests. Signed-off-by: Benjamin Tissoires --- new in v3 --- tools/testing/selftests/hid/config.common | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/testing/selftests/hid/config.common b/tools/testing/selftests/hid/config.common index 0f456dbab62f..45b5570441ce 100644 --- a/tools/testing/selftests/hid/config.common +++ b/tools/testing/selftests/hid/config.common @@ -238,3 +238,4 @@ CONFIG_VLAN_8021Q=y CONFIG_XFRM_SUB_POLICY=y CONFIG_XFRM_USER=y CONFIG_ZEROPLUS_FF=y +CONFIG_KASAN=y