From patchwork Tue Feb 21 15:18:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tero Kristo X-Patchwork-Id: 60131 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp74037wrd; Tue, 21 Feb 2023 07:33:46 -0800 (PST) X-Google-Smtp-Source: AK7set9FHiqtPG1F1UvGt4me04b9UlCsDrlWj1dGc5QOfC9B8aybrkIZFxrCzHcBQ4KwPzO3M5Df X-Received: by 2002:a17:902:d2d2:b0:199:564a:9936 with SMTP id n18-20020a170902d2d200b00199564a9936mr8081158plc.25.1676993626238; Tue, 21 Feb 2023 07:33:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676993626; cv=none; d=google.com; s=arc-20160816; b=ZlP2gclt7HhbvW/dJbNi67xpcmlwl27oc8U4Vs0+j9aTDRYNbKaFbeYn05Duw2DqK2 g4w52OS6/47GB2AcH7M/bN5+bgNehJD6TvBsev3cX6y9CKxwkHR6F4kY49B+EwXgizlv UVQeegcFPU/XRyyDYQDmi8lOGoBLA8g645PE3Z3esk8FF3zjdRuPZptV9+zn7nuxKSuQ Min/0fzjV7OkuC8TaDD+FjOXr3qkuXh8ecffVi7+XXF/NaSm65GrBpmOiI7pX274ArcD N/RvmUoMS3/adU3Yai9DtOTzX93t1grqegPcnu8oDqbjMuQVK5CMDzB0OVAx2ecLrMqM 5/bA== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=ga2w0tYcyIDo7+oQQHTpQafnGVaxICL+5ikfBx5i/y0=; b=B9wybJ4Q6FawnuyoQ2ZErsaPu46QjszLb7peEeGk9//B8tt/imp7o9+jGk6lzUVRlP 2adeh8pegNr1DCQ91ESwjbX5+XHBY+Bgi5rw9uonOICWnN6moGWTIEonal4yxC6KnYI7 mcgEQu8wFauw+HZrKM9MMtT0rdBHH1HVo1jdapiTofMd0jRaMoqJ5EGAHFo4MsUJLp8Z NQ5r+TtQNYa1TWOfiVjChGEfG5RJ8Yb5cv3YdqiV5jnmwaqnzmHYXKgQtX5k4fs68oI3 W52hhI2nY4w6aDs3Vdji11/4QxoYdJKjvcox4Kbwzbq1eGhjIA+SVnZNovVKPDaLTXXK hOIw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="dxLcN/23"; 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 w8-20020a1709026f0800b0019c9fa30c04si628078plk.204.2023.02.21.07.33.33; Tue, 21 Feb 2023 07:33:46 -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="dxLcN/23"; 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 S233771AbjBUPVN (ORCPT + 99 others); Tue, 21 Feb 2023 10:21:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42932 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232317AbjBUPVM (ORCPT ); Tue, 21 Feb 2023 10:21:12 -0500 Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE81B25BB2; Tue, 21 Feb 2023 07:21:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676992867; x=1708528867; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=ywd+JZNDR2jmJtryHqa+ea9UJUbbLZv/MDZRRFcw54g=; b=dxLcN/234cAl+auZPbD4F53/zzKQqX2RwvzGH3XjBoHnxHIuV/ZJD+G9 pV3XSU6zIdKn4NGP+QWZ3ilP2rVWOj0hXC5nvdEgQuzTuEGlWZdV8V9nu vvH4nsxR0COVEFoV/YvyMuMs2bjDNO3ACPDoa/iMaYbo8q/BlpGyUicx5 nYDuyvVwxLi/iwB/PdoEMeXvHA2R++UBuCwVOjtop9r3P0zhNlrCI6OCI yfDPpgGBAnWLfXJW250sDFbaEOeTnSDX+oOJrDaW9Epza6nN5TB7bbSHR HOdssc62h76YD/wc+JJSHy3rfob3NJLGsgSXaGydWiv0IMi3vmyhVgFub A==; X-IronPort-AV: E=McAfee;i="6500,9779,10628"; a="334020034" X-IronPort-AV: E=Sophos;i="5.97,315,1669104000"; d="scan'208";a="334020034" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Feb 2023 07:18:51 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10628"; a="814529284" X-IronPort-AV: E=Sophos;i="5.97,315,1669104000"; d="scan'208";a="814529284" Received: from tveit-mobl.ger.corp.intel.com (HELO tkristo-desk.bb.dnainternet.fi) ([10.249.39.132]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Feb 2023 07:18:48 -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: [RESEND PATCH] bpf: Add support for absolute value BPF timers Date: Tue, 21 Feb 2023 17:18:46 +0200 Message-Id: <20230221151846.2218217-1-tero.kristo@linux.intel.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,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?1758455268780924791?= X-GMAIL-MSGID: =?utf-8?q?1758455268780924791?= 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 and jitter induced by both the BPF and timer subsystems. This is useful e.g. in certain time sensitive profiling cases, where we need a timer to expire at an exact point in time. Signed-off-by: Tero Kristo --- include/uapi/linux/bpf.h | 15 +++++++++++++++ kernel/bpf/helpers.c | 11 +++++++++-- 2 files changed, 24 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;