Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp3644497pxb; Mon, 9 Nov 2020 17:25:09 -0800 (PST) X-Google-Smtp-Source: ABdhPJx1XUZtldp9AD/KK/yUI/H3sLT9UuMPBPCQ3zwwFlAfd+sUVdd1itwd2Sxe2DEMDqXqNn/u X-Received: by 2002:aa7:c68d:: with SMTP id n13mr18850971edq.350.1604971508838; Mon, 09 Nov 2020 17:25:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604971508; cv=none; d=google.com; s=arc-20160816; b=h30GZ2GLcS2ZTwZm7JrTtGNRurvLhAsJW1BTROfu5hqzu4SP0w87njiN1th6Cdvpq7 VYKgz6eB8e7QxrppcYXRu+OXpaaGvG7T9+i5qhLxfMkalKPhcgsrpeJgbgUBWyl76b6e DhTmcpvuVLmQWsYm7/Zvhnq0XsViKPPA38euivY4NUtQIcSHKuHLSWBgm2BdMRfCG1Q+ COdLcN97ESRcoL7yE9PekC7B7h/eYMyUCU1jDfdkSaqSrDqb/RAjcOy29rdujXA7rnEt ZOYHAjLSqIUNOGppoCZdp92rrvFnekF8si7CuAQXFg7XxcpGaN9MRwMbUgESJPRC+V8d uQpg== 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; bh=hNQe375Mkv0uKeZ0zJUXaxlTJQ/Zk2ID7BDl30JcxBY=; b=OfB2UaducwbISAmy66HgMEiOqBVjAaHU1s3vOcuTAOD2XnjXg5p+F/xCRyNakeM2IL 8t1v4i3P4icYi8oOuP2v3Rb4YFQvGXEKOK3xhl3xRNzP7/VutBVwveLo3ElogJU+TBsu UxOQe1rxJBqPvxQAPlP0/SJa9FLJv+dPo23nWHbS2gWkoBzk4a70NTLmDRwRovDOIIIU OvBgJBKoxNdQv6pnUKpTtfgAS6WkBeuXPiulhLwYQEg6UsM5Amnf4uotKOk9pWPTSD6H D+wYA1FTRxp18cMC6Pxn8SVr5iS4Kt2UPLZSPiRC2BXxGDM22g3qnDdest0dscfB2NSR oEoA== ARC-Authentication-Results: i=1; mx.google.com; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o5si2392293edz.444.2020.11.09.17.24.45; Mon, 09 Nov 2020 17:25:08 -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; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730460AbgKJBWk convert rfc822-to-8bit (ORCPT + 99 others); Mon, 9 Nov 2020 20:22:40 -0500 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:14452 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1729968AbgKJBWj (ORCPT ); Mon, 9 Nov 2020 20:22:39 -0500 Received: from pps.filterd (m0089730.ppops.net [127.0.0.1]) by m0089730.ppops.net (8.16.0.42/8.16.0.42) with SMTP id 0AA1KKbt007884 for ; Mon, 9 Nov 2020 17:22:38 -0800 Received: from mail.thefacebook.com ([163.114.132.120]) by m0089730.ppops.net with ESMTP id 34nqy2b3n3-6 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Mon, 09 Nov 2020 17:22:38 -0800 Received: from intmgw002.08.frc2.facebook.com (2620:10d:c085:108::8) by mail.thefacebook.com (2620:10d:c085:21d::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1979.3; Mon, 9 Nov 2020 17:22:36 -0800 Received: by devbig012.ftw2.facebook.com (Postfix, from userid 137359) id 5DBD52EC9241; Mon, 9 Nov 2020 17:19:38 -0800 (PST) From: Andrii Nakryiko To: , , , CC: , , , , Andrii Nakryiko , Arnaldo Carvalho de Melo , Greg Kroah-Hartman Subject: [PATCH v4 bpf-next 2/5] bpf: assign ID to vmlinux BTF and return extra info for BTF in GET_OBJ_INFO Date: Mon, 9 Nov 2020 17:19:29 -0800 Message-ID: <20201110011932.3201430-3-andrii@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20201110011932.3201430-1-andrii@kernel.org> References: <20201110011932.3201430-1-andrii@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8BIT X-FB-Internal: Safe Content-Type: text/plain X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.312,18.0.737 definitions=2020-11-09_15:2020-11-05,2020-11-09 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 priorityscore=1501 mlxlogscore=678 phishscore=0 mlxscore=0 malwarescore=0 spamscore=0 bulkscore=0 impostorscore=0 suspectscore=75 clxscore=1015 adultscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2011100008 X-FB-Internal: deliver Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Allocate ID for vmlinux BTF. This makes it visible when iterating over all BTF objects in the system. To allow distinguishing vmlinux BTF (and later kernel module BTF) from user-provided BTFs, expose extra kernel_btf flag, as well as BTF name ("vmlinux" for vmlinux BTF, will equal to module's name for module BTF). We might want to later allow specifying BTF name for user-provided BTFs as well, if that makes sense. But currently this is reserved only for in-kernel BTFs. Having in-kernel BTFs exposed IDs will allow to extend BPF APIs that require in-kernel BTF type with ability to specify BTF types from kernel modules, not just vmlinux BTF. This will be implemented in a follow up patch set for fentry/fexit/fmod_ret/lsm/etc. Signed-off-by: Andrii Nakryiko --- include/uapi/linux/bpf.h | 3 +++ kernel/bpf/btf.c | 43 +++++++++++++++++++++++++++++++--- tools/include/uapi/linux/bpf.h | 3 +++ 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 9879d6793e90..162999b12790 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -4466,6 +4466,9 @@ struct bpf_btf_info { __aligned_u64 btf; __u32 btf_size; __u32 id; + __aligned_u64 name; + __u32 name_len; + __u32 kernel_btf; } __attribute__((aligned(8))); struct bpf_link_info { diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 727c1c27053f..856585db7aa7 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -214,6 +214,8 @@ struct btf { struct btf *base_btf; u32 start_id; /* first type ID in this BTF (0 for base BTF) */ u32 start_str_off; /* first string offset (0 for base BTF) */ + char name[MODULE_NAME_LEN]; + bool kernel_btf; }; enum verifier_phase { @@ -4429,6 +4431,8 @@ struct btf *btf_parse_vmlinux(void) btf->data = __start_BTF; btf->data_size = __stop_BTF - __start_BTF; + btf->kernel_btf = true; + snprintf(btf->name, sizeof(btf->name), "vmlinux"); err = btf_parse_hdr(env); if (err) @@ -4454,8 +4458,13 @@ struct btf *btf_parse_vmlinux(void) bpf_struct_ops_init(btf, log); - btf_verifier_env_free(env); refcount_set(&btf->refcnt, 1); + + err = btf_alloc_id(btf); + if (err) + goto errout; + + btf_verifier_env_free(env); return btf; errout: @@ -5553,7 +5562,9 @@ int btf_get_info_by_fd(const struct btf *btf, struct bpf_btf_info info; u32 info_copy, btf_copy; void __user *ubtf; - u32 uinfo_len; + char __user *uname; + u32 uinfo_len, uname_len, name_len; + int ret = 0; uinfo = u64_to_user_ptr(attr->info.info); uinfo_len = attr->info.info_len; @@ -5570,11 +5581,37 @@ int btf_get_info_by_fd(const struct btf *btf, return -EFAULT; info.btf_size = btf->data_size; + info.kernel_btf = btf->kernel_btf; + + uname = u64_to_user_ptr(info.name); + uname_len = info.name_len; + if (!uname ^ !uname_len) + return -EINVAL; + + name_len = strlen(btf->name); + info.name_len = name_len; + + if (uname) { + if (uname_len >= name_len + 1) { + if (copy_to_user(uname, btf->name, name_len + 1)) + return -EFAULT; + } else { + char zero = '\0'; + + if (copy_to_user(uname, btf->name, uname_len - 1)) + return -EFAULT; + if (put_user(zero, uname + uname_len - 1)) + return -EFAULT; + /* let user-space know about too short buffer */ + ret = -ENOSPC; + } + } + if (copy_to_user(uinfo, &info, info_copy) || put_user(info_copy, &uattr->info.info_len)) return -EFAULT; - return 0; + return ret; } int btf_get_fd_by_id(u32 id) diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 9879d6793e90..162999b12790 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -4466,6 +4466,9 @@ struct bpf_btf_info { __aligned_u64 btf; __u32 btf_size; __u32 id; + __aligned_u64 name; + __u32 name_len; + __u32 kernel_btf; } __attribute__((aligned(8))); struct bpf_link_info { -- 2.24.1