From patchwork Mon Aug 31 04:03:29 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Khalid Elmously X-Patchwork-Id: 1354103 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=canonical.com Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4BfxQv649nz9sTh; Mon, 31 Aug 2020 14:04:07 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1kCb32-0005Fg-7B; Mon, 31 Aug 2020 04:04:00 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1kCb2w-0005B6-0E for kernel-team@lists.ubuntu.com; Mon, 31 Aug 2020 04:03:54 +0000 Received: from mail-qk1-f197.google.com ([209.85.222.197]) by youngberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1kCb2v-0006Jr-Ju for kernel-team@lists.ubuntu.com; Mon, 31 Aug 2020 04:03:53 +0000 Received: by mail-qk1-f197.google.com with SMTP id c67so5097653qkd.0 for ; Sun, 30 Aug 2020 21:03:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=dX0ABxEI5LhaE5JDp7JJ6IExrk2gf561OuvJhPSHIc0=; b=PuaOBXEcTDCaBXhc8r6A1d/48cFfT8Zc3WZOTzh9lJpJbxX6Z8fenz0YfAIQL+7qGS N9UVEfm2zySunbnfES3QTd8PWTf8MEuuK+R0ZdaZHxBuA5YIJamXTCTmsrJCagROB6NS r/gb6cJ2LiSWHoxhCndMWPlcRCWFOMvTuH1drctbkVE3+OJUFHTOXc0Ljqd+lYdeTQBi cRaZgRVmxWH8bqUhqKwAABRfZ7XBsLS/3YbCW+lHEhKR2eCjtvLxUf2JQNuvrPonjbHu h2ksodbBqS6/bIJlwVkSdyPHTq2Au9YEp5JSkfn/VZHp8j95vzeWMfog6r4FxYZJvexa VW5A== X-Gm-Message-State: AOAM531UNFGh66CuyrK7Mix3OJldzs9+58lXMDbd0ilaHH1NG7vmmsyK GDlJFL+ysHddh5Bz436a5BLnSC7xex38l2LaLO+sIVs/211mHY7x2DhS9HKxZfRB0D/R1+aiIAN 0nU3H8TqWVanlwyV8MqOPtME3GJ9mNrfzNHoZ2rU00Q== X-Received: by 2002:a37:92c7:: with SMTP id u190mr9355318qkd.110.1598846630861; Sun, 30 Aug 2020 21:03:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw/eK50/Hqcz40Cyn82gIEenzwbmhV0Ytxc98aaHNzMnljpjqwVTM0Wl5hfhmzRVHdH5ZNq8A== X-Received: by 2002:a37:92c7:: with SMTP id u190mr9355297qkd.110.1598846630484; Sun, 30 Aug 2020 21:03:50 -0700 (PDT) Received: from kbuntu2.fuzzbuzz.org (dhcp-24-53-242-104.cable.user.start.ca. [24.53.242.104]) by smtp.gmail.com with ESMTPSA id d9sm7779648qkj.83.2020.08.30.21.03.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Aug 2020 21:03:50 -0700 (PDT) From: Khalid Elmously To: kernel-team@lists.ubuntu.com Subject: [PATCH 09/13] bpf: Add BPF_FUNC_jiffies64 Date: Mon, 31 Aug 2020 00:03:29 -0400 Message-Id: <20200831040333.6058-10-khalid.elmously@canonical.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200831040333.6058-1-khalid.elmously@canonical.com> References: <20200831040333.6058-1-khalid.elmously@canonical.com> X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Martin KaFai Lau BugLink: https://bugs.launchpad.net/bugs/1887740 [ upstream commit 5576b991e9c1a11d2cc21c4b94fc75ec27603896 ] This patch adds a helper to read the 64bit jiffies. It will be used in a later patch to implement the bpf_cubic.c. The helper is inlined for jit_requested and 64 BITS_PER_LONG as the map_gen_lookup(). Other cases could be considered together with map_gen_lookup() if needed. Signed-off-by: Martin KaFai Lau Signed-off-by: Alexei Starovoitov Link: https://lore.kernel.org/bpf/20200122233646.903260-1-kafai@fb.com Signed-off-by: Daniel Borkmann Signed-off-by: Khalid Elmously --- include/linux/bpf.h | 1 + kernel/bpf/core.c | 1 + kernel/bpf/helpers.c | 12 ++++++++++++ kernel/bpf/verifier.c | 24 ++++++++++++++++++++++++ net/core/filter.c | 2 ++ 5 files changed, 40 insertions(+) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 3703853ffd2e..0f49e4ba476f 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1056,6 +1056,7 @@ extern const struct bpf_func_proto bpf_get_local_storage_proto; extern const struct bpf_func_proto bpf_strtol_proto; extern const struct bpf_func_proto bpf_strtoul_proto; extern const struct bpf_func_proto bpf_tcp_sock_proto; +extern const struct bpf_func_proto bpf_jiffies64_proto; /* Shared helpers among cBPF and eBPF. */ void bpf_user_rnd_init_once(void); diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index ef0e1e3e66f4..3cb8772883f7 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -2031,6 +2031,7 @@ const struct bpf_func_proto bpf_map_pop_elem_proto __weak; const struct bpf_func_proto bpf_map_peek_elem_proto __weak; const struct bpf_func_proto bpf_spin_lock_proto __weak; const struct bpf_func_proto bpf_spin_unlock_proto __weak; +const struct bpf_func_proto bpf_jiffies64_proto __weak; const struct bpf_func_proto bpf_get_prandom_u32_proto __weak; const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak; diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 5e28718928ca..72bcc13d0e3d 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -11,6 +11,7 @@ #include #include #include +#include #include "../../lib/kstrtox.h" @@ -312,6 +313,17 @@ void copy_map_value_locked(struct bpf_map *map, void *dst, void *src, preempt_enable(); } +BPF_CALL_0(bpf_jiffies64) +{ + return get_jiffies_64(); +} + +const struct bpf_func_proto bpf_jiffies64_proto = { + .func = bpf_jiffies64, + .gpl_only = false, + .ret_type = RET_INTEGER, +}; + #ifdef CONFIG_CGROUPS BPF_CALL_0(bpf_get_current_cgroup_id) { diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 181126aeef7a..53a453d42b5e 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -9227,6 +9227,30 @@ static int fixup_bpf_calls(struct bpf_verifier_env *env) goto patch_call_imm; } + if (prog->jit_requested && BITS_PER_LONG == 64 && + insn->imm == BPF_FUNC_jiffies64) { + struct bpf_insn ld_jiffies_addr[2] = { + BPF_LD_IMM64(BPF_REG_0, + (unsigned long)&jiffies), + }; + + insn_buf[0] = ld_jiffies_addr[0]; + insn_buf[1] = ld_jiffies_addr[1]; + insn_buf[2] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, + BPF_REG_0, 0); + cnt = 3; + + new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, + cnt); + if (!new_prog) + return -ENOMEM; + + delta += cnt - 1; + env->prog = prog = new_prog; + insn = new_prog->insnsi + i + delta; + continue; + } + patch_call_imm: fn = env->ops->get_func_proto(insn->imm, env->prog); /* all functions that have prototype and verifier allowed diff --git a/net/core/filter.c b/net/core/filter.c index d1373a8ce6ed..be220ee91d1f 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -6061,6 +6061,8 @@ bpf_base_func_proto(enum bpf_func_id func_id) return &bpf_spin_unlock_proto; case BPF_FUNC_trace_printk: return bpf_get_trace_printk_proto(); + case BPF_FUNC_jiffies64: + return &bpf_jiffies64_proto; default: return NULL; }