Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp852843pxb; Thu, 5 Nov 2020 15:02:57 -0800 (PST) X-Google-Smtp-Source: ABdhPJw1CEnYC62bMf6g3VTdVsEG6UGR6hXDTkdfWgFxo418l3a96RDjMwkcWL7JmROZhPQqdIs1 X-Received: by 2002:a05:6402:1c99:: with SMTP id cy25mr5141696edb.283.1604617377268; Thu, 05 Nov 2020 15:02:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604617377; cv=none; d=google.com; s=arc-20160816; b=AsFd3WXgx9JSw7eLjQFabHdFJ59AEamWgn7GWZTwckB9/z3nCNEODcIy/vGVK7+ABD zZPJjcgwY5tmWTg+39hwgj4lSjyuFfkrlf7UcCiF55jNR1uI9p2gm/MZrWNreIx1GQs8 SCZs37uC2KaCzl4oiEzIemX21xhOj1+n4UM9T3K5fDxISAoSCbLQfKs+7OqjGKE8lnxC nHuv73rHx//1LH0lvfjqDDrb3CKF0GNM58u7yau07+Ap+cBXOdrKDFTX9X3OYj4/ZIwY Y34fcr1l0dBg/MYgZodgWXq65j36UrF+BRk7Z/ETMl2f450ObhEdtOWvKvtXAlDIx2tf /hEA== 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=1iVPFC7uARpOdaXSU1nG+jmudGdOT2sbjxX8qhhLhxc=; b=zzkxr7fdYduMveNG7DEcfRaK4Pn/Dw8v7tWI/JSsldacoaAxCNlbg4Ic9HHSu+rIf3 2HujiWXPkohCl7l85/tybgla+ht0NMl8+ScdV3zDZ+tekEskyepaRJaOM8tkDzIoCcMc ChHdSJuQh0kbEgX+WZC5lzFtO1ae9rXIKVichN1x7wWtD+Kesha5GKs5ZQ5g3IM7VZa9 9yW6T7PnM4kjK9Br2S+0rTipyB1QUYTN/RduxP9YrlM4g8Gz5mBVzePRRyCMCCeSHbuY ag1S22om1gPUrTLepOfzq+aGR6JTENogC646DbA931ZdHHsZIC9ymzacIn7VRkFT4YMM 2ntQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="Wl/b0xFP"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 27si2262345edw.379.2020.11.05.15.02.11; Thu, 05 Nov 2020 15:02:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="Wl/b0xFP"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732482AbgKEW6n (ORCPT + 99 others); Thu, 5 Nov 2020 17:58:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39698 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732488AbgKEW6h (ORCPT ); Thu, 5 Nov 2020 17:58:37 -0500 Received: from mail-wr1-x442.google.com (mail-wr1-x442.google.com [IPv6:2a00:1450:4864:20::442]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5408CC0613D6 for ; Thu, 5 Nov 2020 14:58:36 -0800 (PST) Received: by mail-wr1-x442.google.com with SMTP id x7so3621657wrl.3 for ; Thu, 05 Nov 2020 14:58:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=1iVPFC7uARpOdaXSU1nG+jmudGdOT2sbjxX8qhhLhxc=; b=Wl/b0xFPlui9JcRoOmSBNQ9Cuge3i0nH+L+5vsJ2IxnC/X7WBzXOJQ19jMoZRLsAaA Zw0WeT6xOkDdjN4NwfUo3oNCywZAd5/Ngv2D1O+EMVrfifFm2LOaZkcjkJJI7u91yc4P 47k5rTy8xf+p1zQP1zuFQ05sTDpoz3s3u3xgQ= 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:mime-version:content-transfer-encoding; bh=1iVPFC7uARpOdaXSU1nG+jmudGdOT2sbjxX8qhhLhxc=; b=IGUTvc4nVoMBQhG1v10pzFDU7jXgTA6vIDab3b4KB4U+axy2bLZAEt+SZqh82tR1mA 19WIUc0nTcOxLAwwU7QyMQRATHsyrTACM8DMuzt9ESFNv9MAl2VPIG8D1H1yp2hRmkUL 8SfpC2K2QAsCWYrS9aUCzOxDGvuA1SKBeiWNBJM7lILli7c5LDVlIEGp2iTNtJWj3xN3 wUoTA0L+SMGU9Oq30JMRXUrPreBQH7nqOfVNaUvzCUZYR9R/TDw2BzcUFgNPyBGK2drM ax2mAhiMAi0HcxoPF5XbmJa7RoVvyYf5+p7/PHrXnuQ8/IfopjmTQfzQyey14z6vEDC0 nfOw== X-Gm-Message-State: AOAM530m6VoprpCIQ5fJKqW+k4lVwbOciGJd2YSyDRrwwGQY+i58ndN+ W+xPfWk4L8polsW01ENVIjCs5U/9dPdX+dbd X-Received: by 2002:adf:9d81:: with SMTP id p1mr5250773wre.13.1604617114852; Thu, 05 Nov 2020 14:58:34 -0800 (PST) Received: from kpsingh.c.googlers.com.com (203.75.199.104.bc.googleusercontent.com. [104.199.75.203]) by smtp.gmail.com with ESMTPSA id f19sm3977366wml.21.2020.11.05.14.58.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Nov 2020 14:58:34 -0800 (PST) From: KP Singh To: linux-kernel@vger.kernel.org, bpf@vger.kernel.org Cc: Song Liu , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Paul Turner , Jann Horn , Hao Luo Subject: [PATCH bpf-next v5 5/9] bpf: Implement get_current_task_btf and RET_PTR_TO_BTF_ID Date: Thu, 5 Nov 2020 22:58:23 +0000 Message-Id: <20201105225827.2619773-6-kpsingh@chromium.org> X-Mailer: git-send-email 2.29.1.341.ge80a0c044ae-goog In-Reply-To: <20201105225827.2619773-1-kpsingh@chromium.org> References: <20201105225827.2619773-1-kpsingh@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: KP Singh The currently available bpf_get_current_task returns an unsigned integer which can be used along with BPF_CORE_READ to read data from the task_struct but still cannot be used as an input argument to a helper that accepts an ARG_PTR_TO_BTF_ID of type task_struct. In order to implement this helper a new return type, RET_PTR_TO_BTF_ID, is added. This is similar to RET_PTR_TO_BTF_ID_OR_NULL but does not require checking the nullness of returned pointer. Acked-by: Song Liu Signed-off-by: KP Singh --- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 9 +++++++++ kernel/bpf/verifier.c | 7 +++++-- kernel/trace/bpf_trace.c | 16 ++++++++++++++++ tools/include/uapi/linux/bpf.h | 9 +++++++++ 5 files changed, 40 insertions(+), 2 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 2fffd30e13ac..73d5381a5d5c 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -310,6 +310,7 @@ enum bpf_return_type { RET_PTR_TO_BTF_ID_OR_NULL, /* returns a pointer to a btf_id or NULL */ RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL, /* returns a pointer to a valid memory or a btf_id or NULL */ RET_PTR_TO_MEM_OR_BTF_ID, /* returns a pointer to a valid memory or a btf_id */ + RET_PTR_TO_BTF_ID, /* returns a pointer to a btf_id */ }; /* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index f4037b2161a6..9879d6793e90 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -3779,6 +3779,14 @@ union bpf_attr { * 0 on success. * * **-ENOENT** if the bpf_local_storage cannot be found. + * + * struct task_struct *bpf_get_current_task_btf(void) + * Description + * Return a BTF pointer to the "current" task. + * This pointer can also be used in helpers that accept an + * *ARG_PTR_TO_BTF_ID* of type *task_struct*. + * Return + * Pointer to the current task. */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -3939,6 +3947,7 @@ union bpf_attr { FN(redirect_peer), \ FN(task_storage_get), \ FN(task_storage_delete), \ + FN(get_current_task_btf), \ /* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 00960f6a83ec..10da26e55130 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5186,11 +5186,14 @@ static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn PTR_TO_BTF_ID : PTR_TO_BTF_ID_OR_NULL; regs[BPF_REG_0].btf_id = meta.ret_btf_id; } - } else if (fn->ret_type == RET_PTR_TO_BTF_ID_OR_NULL) { + } else if (fn->ret_type == RET_PTR_TO_BTF_ID_OR_NULL || + fn->ret_type == RET_PTR_TO_BTF_ID) { int ret_btf_id; mark_reg_known_zero(env, regs, BPF_REG_0); - regs[BPF_REG_0].type = PTR_TO_BTF_ID_OR_NULL; + regs[BPF_REG_0].type = fn->ret_type == RET_PTR_TO_BTF_ID ? + PTR_TO_BTF_ID : + PTR_TO_BTF_ID_OR_NULL; ret_btf_id = *fn->ret_btf_id; if (ret_btf_id == 0) { verbose(env, "invalid return type %d of func %s#%d\n", diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 4517c8b66518..e4515b0f62a8 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -1022,6 +1022,20 @@ const struct bpf_func_proto bpf_get_current_task_proto = { .ret_type = RET_INTEGER, }; +BPF_CALL_0(bpf_get_current_task_btf) +{ + return (unsigned long) current; +} + +BTF_ID_LIST_SINGLE(bpf_get_current_btf_ids, struct, task_struct) + +static const struct bpf_func_proto bpf_get_current_task_btf_proto = { + .func = bpf_get_current_task_btf, + .gpl_only = true, + .ret_type = RET_PTR_TO_BTF_ID, + .ret_btf_id = &bpf_get_current_btf_ids[0], +}; + BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx) { struct bpf_array *array = container_of(map, struct bpf_array, map); @@ -1265,6 +1279,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) return &bpf_get_current_pid_tgid_proto; case BPF_FUNC_get_current_task: return &bpf_get_current_task_proto; + case BPF_FUNC_get_current_task_btf: + return &bpf_get_current_task_btf_proto; case BPF_FUNC_get_current_uid_gid: return &bpf_get_current_uid_gid_proto; case BPF_FUNC_get_current_comm: diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index f4037b2161a6..9879d6793e90 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -3779,6 +3779,14 @@ union bpf_attr { * 0 on success. * * **-ENOENT** if the bpf_local_storage cannot be found. + * + * struct task_struct *bpf_get_current_task_btf(void) + * Description + * Return a BTF pointer to the "current" task. + * This pointer can also be used in helpers that accept an + * *ARG_PTR_TO_BTF_ID* of type *task_struct*. + * Return + * Pointer to the current task. */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -3939,6 +3947,7 @@ union bpf_attr { FN(redirect_peer), \ FN(task_storage_get), \ FN(task_storage_delete), \ + FN(get_current_task_btf), \ /* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper -- 2.29.1.341.ge80a0c044ae-goog