From patchwork Thu Mar 2 11:46:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tero Kristo X-Patchwork-Id: 63375 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp4191967wrd; Thu, 2 Mar 2023 03:57:49 -0800 (PST) X-Google-Smtp-Source: AK7set+81Lfm6jBvwfw240cl7SGqykikw1VP+2V408D7zBRS4GEnijDlW6zhoST1/UfYTc/dkyRV X-Received: by 2002:a05:6a20:6a0e:b0:cb:a0e3:4598 with SMTP id p14-20020a056a206a0e00b000cba0e34598mr12727011pzk.43.1677758269366; Thu, 02 Mar 2023 03:57:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677758269; cv=none; d=google.com; s=arc-20160816; b=YImITzJCz6y9uXYovjR8cl4EwzpkUoUtednM2P+Slxovx0WzC8iccRKAk/VeLfQkJZ UdNMRdfWXwM9djMtlmDDEeV3k23XHXYuDFzYa2/4tynw+FAFVvtAXkYKAfQHyfuiZNHL zdj4BptOZIUc3jJ3bEj2SCsiT9bvJv5VPi/0+xYuRs8LzLEq9clprkC6E8hYchW2Cw5m 0CxyUALxmEZvEjslDBVRyHG8miqVUXR53p1THt0R7Y3M/e9Hea6zPTCvob2GZ6qlsmvM p+jvJAuS4fNgmR5lMZmAvKMhskMNiLSgrN9kNBZaDKF6vlLN2CdQtQv9VIc22lWgg9Pi xlGA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=/Ts7Y2oKVGO1ImSbGCEMUgxchYHyptCmYlwrAL59AD4=; b=BeyhIz08F5NVzJZl0XbtGSSl3Af8UpmpDsMSfof9dZkuXfkIJfZxadd49Aggw3ejCF Ysm0/VGZ32UejXQP5yhv4E1c00lWM6UV6eNNnSbBhI7CqNEm2nLBT972uh7MGNk6TM/6 M8G/higqryfP/OOssX8SxtkVaiPDqgn8RggTxRhGDGlyvwefRe0T1OsKRw+pRGdkr2uH L3BLdqQcSaNnvqaq1psduk5uG8hmPA6VunzedEk+ifkzCzJZov2E74m74Jvnvbss3ard LJQ6RwkoGWlYA5bGEe6KJxO4D8XRcBIvJbXBP8uSRAnTJ73yIeXdp7What9yaD0ncAde inXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=SKAqhzQI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t65-20020a628144000000b00574b46d3b26si14906159pfd.334.2023.03.02.03.57.36; Thu, 02 Mar 2023 03:57:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=SKAqhzQI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229895AbjCBLqf (ORCPT + 99 others); Thu, 2 Mar 2023 06:46:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43976 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229835AbjCBLqc (ORCPT ); Thu, 2 Mar 2023 06:46:32 -0500 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E6220231D8; Thu, 2 Mar 2023 03:46:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1677757591; x=1709293591; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=/uRUelVZU3Q6oGhZ0g8qpAC23v6y/WT8NF4OlN7AJZ4=; b=SKAqhzQIj2ZQupsdsptP8OySuBGuV3YQIy/QanlwBwEuprTR/Fq5k1MQ jYCatUZZpGoSkne6pob3y2+62UYciCxv9rTY2BMhQjwXU+3AYhTO/efbP BDt+cnopcm/Axj4B8u1bCwafVKvRc/I0hfURt4WXUUvoA/ehdYjzjVIuO RH8MxvOWYAmE7W57+p6kWWLWv7BanwX6NsNgYO4gaj+ckrB1ixX5PcIWj lusqcg5GiS9SGFueOYOJJbEHTMLClEUduovEv8nUp+mQvMracDz27Tw25 NUdeuaWaDRB454xBH9516MNRi6jV/NXX/xXeBqhsu51iB8jLSYwfSdGqT Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10636"; a="332180134" X-IronPort-AV: E=Sophos;i="5.98,227,1673942400"; d="scan'208";a="332180134" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Mar 2023 03:46:31 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10636"; a="705229331" X-IronPort-AV: E=Sophos;i="5.98,227,1673942400"; d="scan'208";a="705229331" Received: from pplank-mobl1.ger.corp.intel.com (HELO tkristo-desk.bb.dnainternet.fi) ([10.251.217.71]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Mar 2023 03:46:29 -0800 From: Tero Kristo To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, bpf@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Artem Bityutskiy Subject: [PATCHv2 1/2] bpf: Add support for absolute value BPF timers Date: Thu, 2 Mar 2023 13:46:13 +0200 Message-Id: <20230302114614.2985072-2-tero.kristo@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230302114614.2985072-1-tero.kristo@linux.intel.com> References: <20230302114614.2985072-1-tero.kristo@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759257054957847670?= X-GMAIL-MSGID: =?utf-8?q?1759257054957847670?= Add a new flag BPF_F_TIMER_ABS that can be passed to bpf_timer_start() to start an absolute value timer instead of the default relative value. This makes the timer expire at an exact point in time, instead of a time with latencies induced by both the BPF and timer subsystems. Suggested-by: Artem Bityutskiy Signed-off-by: Tero Kristo --- v2: - added suggested by from Artem - copied the change from include/uapi/linux/bpf.h to tools/include also include/uapi/linux/bpf.h | 15 +++++++++++++++ kernel/bpf/helpers.c | 11 +++++++++-- tools/include/uapi/linux/bpf.h | 15 +++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 464ca3f01fe7..7f5b71847984 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -4951,6 +4951,12 @@ union bpf_attr { * different maps if key/value layout matches across maps. * Every bpf_timer_set_callback() can have different callback_fn. * + * *flags* can be one of: + * + * **BPF_F_TIMER_ABS** + * Start the timer in absolute expire value instead of the + * default relative one. + * * Return * 0 on success. * **-EINVAL** if *timer* was not initialized with bpf_timer_init() earlier @@ -7050,4 +7056,13 @@ struct bpf_core_relo { enum bpf_core_relo_kind kind; }; +/* + * Flags to control bpf_timer_start() behaviour. + * - BPF_F_TIMER_ABS: Timeout passed is absolute time, by default it is + * relative to current time. + */ +enum { + BPF_F_TIMER_ABS = (1ULL << 0), +}; + #endif /* _UAPI__LINUX_BPF_H__ */ diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index af30c6cbd65d..924849d89828 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -1253,10 +1253,11 @@ BPF_CALL_3(bpf_timer_start, struct bpf_timer_kern *, timer, u64, nsecs, u64, fla { struct bpf_hrtimer *t; int ret = 0; + enum hrtimer_mode mode; if (in_nmi()) return -EOPNOTSUPP; - if (flags) + if (flags > BPF_F_TIMER_ABS) return -EINVAL; __bpf_spin_lock_irqsave(&timer->lock); t = timer->timer; @@ -1264,7 +1265,13 @@ BPF_CALL_3(bpf_timer_start, struct bpf_timer_kern *, timer, u64, nsecs, u64, fla ret = -EINVAL; goto out; } - hrtimer_start(&t->timer, ns_to_ktime(nsecs), HRTIMER_MODE_REL_SOFT); + + if (flags & BPF_F_TIMER_ABS) + mode = HRTIMER_MODE_ABS_SOFT; + else + mode = HRTIMER_MODE_REL_SOFT; + + hrtimer_start(&t->timer, ns_to_ktime(nsecs), mode); out: __bpf_spin_unlock_irqrestore(&timer->lock); return ret; diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 464ca3f01fe7..7f5b71847984 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -4951,6 +4951,12 @@ union bpf_attr { * different maps if key/value layout matches across maps. * Every bpf_timer_set_callback() can have different callback_fn. * + * *flags* can be one of: + * + * **BPF_F_TIMER_ABS** + * Start the timer in absolute expire value instead of the + * default relative one. + * * Return * 0 on success. * **-EINVAL** if *timer* was not initialized with bpf_timer_init() earlier @@ -7050,4 +7056,13 @@ struct bpf_core_relo { enum bpf_core_relo_kind kind; }; +/* + * Flags to control bpf_timer_start() behaviour. + * - BPF_F_TIMER_ABS: Timeout passed is absolute time, by default it is + * relative to current time. + */ +enum { + BPF_F_TIMER_ABS = (1ULL << 0), +}; + #endif /* _UAPI__LINUX_BPF_H__ */ From patchwork Thu Mar 2 11:46:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tero Kristo X-Patchwork-Id: 63376 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp4191984wrd; Thu, 2 Mar 2023 03:57:51 -0800 (PST) X-Google-Smtp-Source: AK7set9Ro5f8dsZ1Tu0a9cbVFcxx7g8o1LNb7lQBSxnUs+3JjmTZQ8QlCqJq8BRXNitXAjEly2wb X-Received: by 2002:a17:903:2447:b0:19c:c9da:a62e with SMTP id l7-20020a170903244700b0019cc9daa62emr11435749pls.54.1677758271239; Thu, 02 Mar 2023 03:57:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677758271; cv=none; d=google.com; s=arc-20160816; b=LeHVxQSvUiHuCC55MrrTsb6uKRwZTAw9fNgBa2S83sZIzDceY6KpXfP1JR5UkDIY1N k+abPF5CXDIQguZd7Z4LhW0zK54xYpTlnOKOQjvHHr10cqgK/x/PLHTCPtYwxCZnH4cM 5nQNEZq/Yim7xG2ZUhd7352HjBiiseFPs88PrUWST7qdp/v9axeZhELpdpTvllfcf0aF +/nGoxfkSfACMnPpAzY3zFn5l1ZySbBQ6mznCgTFx0CMZgmT24T+rqeJE67v8ZiQlJFS fskYqk1e69XQtRcAu8rZ/ZQ8uIpmbWuN57lsSN0ctFzyML1KtJWRVG52+JLISp3BfjkV J/MQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=NoylzIorCtCQAq36pO3LNbRPB7UXJeHrEAheWSsAVkY=; b=V78oZuD012Ri/3paiZF9QxWHRXZb68hy+9ckI+arj1Yrm7nGcLeCQFDsbsEeg6Clp5 /tbP8Dg5Sm5+si32zy7+HU6d46XVDjXOVgH3KQj/LDRKapnHHhyND2I4aSA5ZSw2hl9y VHeUWcGJ9Wl06kgBRB3yl0rnapvEv/nSHKsqiMmR2O/h93Rbak437raeaqOVPmQVwqES vIxl/gIem5e6E9HEfZcUx8GRORyWf7uF7YscOR4ZUaeqea8ehCM8QHM3YRjI3fHcEGht VkGvndgpOB2FJ8bLdF0WgXBaz0iyFoWpJB5E4K8VIoOOzsL2+2qtfD89ca25u6d7G5rT zrjA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Ue9s7B2e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c9-20020a170902c1c900b0019ca806fbcasi14122882plc.594.2023.03.02.03.57.38; Thu, 02 Mar 2023 03:57:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Ue9s7B2e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229959AbjCBLqh (ORCPT + 99 others); Thu, 2 Mar 2023 06:46:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229879AbjCBLqe (ORCPT ); Thu, 2 Mar 2023 06:46:34 -0500 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0F5842A98C; Thu, 2 Mar 2023 03:46:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1677757594; x=1709293594; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=MLx0q3kH81DEhARp7hQmNWOMxwl1xSU4b2mgfSyDf38=; b=Ue9s7B2efhoAFRCjrsBhVqPu/yond9O3mwzcIxSveLRiaHIObPdxKNnk IHC7E1AKMrOaB7qz3KcazJHk3ojvdxqwwQWb1Ibt1vIKqvVMx5QXBBH/4 AsrQim6B4/0zvxr1LwlT++Ny32DthYG/OnXR9F29SH6uNmn1Z5ZbLhdCD M/up4BTIACl1EyENdV3/02My+PqyxnnSgnthE7MJdH5pLFJHt/32HHXA4 9i6EmdxRWkQfTUSnw/FeiyyzpaCtspfOA5MLu0o2JNipydpkLdojUErRE ixGec6opCKjz9qeyWMojsaawnCKRThpHWpYjaZxI1sWb54yyASQ299yLK Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10636"; a="332180140" X-IronPort-AV: E=Sophos;i="5.98,227,1673942400"; d="scan'208";a="332180140" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Mar 2023 03:46:33 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10636"; a="705229333" X-IronPort-AV: E=Sophos;i="5.98,227,1673942400"; d="scan'208";a="705229333" Received: from pplank-mobl1.ger.corp.intel.com (HELO tkristo-desk.bb.dnainternet.fi) ([10.251.217.71]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Mar 2023 03:46:31 -0800 From: Tero Kristo To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, bpf@vger.kernel.org Cc: linux-kernel@vger.kernel.org Subject: [PATCHv2 2/2] selftests/bpf: Add absolute timer test Date: Thu, 2 Mar 2023 13:46:14 +0200 Message-Id: <20230302114614.2985072-3-tero.kristo@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230302114614.2985072-1-tero.kristo@linux.intel.com> References: <20230302114614.2985072-1-tero.kristo@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759257057051437129?= X-GMAIL-MSGID: =?utf-8?q?1759257057051437129?= Add test for the absolute BPF timer under the existing timer tests. This will run the timer two times with 1us expiration time, and then re-arm the timer at ~35s in the future. At the end, it is verified that the absolute timer expired exactly two times. Signed-off-by: Tero Kristo --- v2: new patch added .../testing/selftests/bpf/prog_tests/timer.c | 3 ++ tools/testing/selftests/bpf/progs/timer.c | 45 +++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/timer.c b/tools/testing/selftests/bpf/prog_tests/timer.c index 7eb049214859..290c21dbe65a 100644 --- a/tools/testing/selftests/bpf/prog_tests/timer.c +++ b/tools/testing/selftests/bpf/prog_tests/timer.c @@ -29,6 +29,9 @@ static int timer(struct timer *timer_skel) /* check that timer_cb2() was executed twice */ ASSERT_EQ(timer_skel->bss->bss_data, 10, "bss_data"); + /* check that timer_cb3() was executed twice */ + ASSERT_EQ(timer_skel->bss->abs_data, 12, "abs_data"); + /* check that there were no errors in timer execution */ ASSERT_EQ(timer_skel->bss->err, 0, "err"); diff --git a/tools/testing/selftests/bpf/progs/timer.c b/tools/testing/selftests/bpf/progs/timer.c index acda5c9cea93..9a16d95213e1 100644 --- a/tools/testing/selftests/bpf/progs/timer.c +++ b/tools/testing/selftests/bpf/progs/timer.c @@ -46,7 +46,15 @@ struct { __type(value, struct elem); } lru SEC(".maps"); +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, 1); + __type(key, int); + __type(value, struct elem); +} abs_timer SEC(".maps"); + __u64 bss_data; +__u64 abs_data; __u64 err; __u64 ok; __u64 callback_check = 52; @@ -284,3 +292,40 @@ int BPF_PROG2(test2, int, a, int, b) return bpf_timer_test(); } + +/* callback for absolute timer */ +static int timer_cb3(void *map, int *key, struct bpf_timer *timer) +{ + abs_data += 6; + + if (abs_data < 12) { + bpf_timer_start(timer, bpf_ktime_get_boot_ns() + 1000, + BPF_F_TIMER_ABS); + } else { + /* Re-arm timer ~35 seconds in future */ + bpf_timer_start(timer, bpf_ktime_get_boot_ns() + (1ull << 35), + BPF_F_TIMER_ABS); + } + + return 0; +} + +SEC("fentry/bpf_fentry_test3") +int BPF_PROG2(test3, int, a) +{ + int key = 0; + struct bpf_timer *timer; + + bpf_printk("test3"); + + timer = bpf_map_lookup_elem(&abs_timer, &key); + if (timer) { + if (bpf_timer_init(timer, &abs_timer, CLOCK_BOOTTIME) != 0) + err |= 2048; + bpf_timer_set_callback(timer, timer_cb3); + bpf_timer_start(timer, bpf_ktime_get_boot_ns() + 1000, + BPF_F_TIMER_ABS); + } + + return 0; +}