From patchwork Tue Oct 29 22:39:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13855591 X-Patchwork-Delegate: bpf@iogearbox.net Received: from EUR03-DBA-obe.outbound.protection.outlook.com (mail-dbaeur03olkn2104.outbound.protection.outlook.com [40.92.58.104]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A15A4199FBB; Tue, 29 Oct 2024 22:41:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.58.104 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730241677; cv=fail; b=lrfX8/7N043L30JqR+j6JI02BTNenzaHQ0L+dGc06aztDIPhV9DO3UnuKOk1BQryMQerHc+MLNwTQl1cMCXDndesHh160f3risz669AOU+BhnCWAECpkZTT/vyIFML+171at5/j8LS2Oio5krp40tQRGNrTuB66Yv15ojAszqx8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730241677; c=relaxed/simple; bh=GwOMTBxL7EBlQbeUT5o3SQTv1nbDs02IDXR055rBeOM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=cNoL7gjm3/NIOu8QYTuFR7btRrlI6NBOKYeYrgJFDC1jyt3nFnYgcCZBg2Ms4ndtk7ZtkZnkXiaMfrhTnIAglvDYVHZ3Cg+NZfhjww1ygoMRSawRzMyIFvbJe5uDd5kZ69+DAZEtA0GQSeX7xN0gDxkf5CX3VU6qraj7yxwfmhk= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=T65IZc/s; arc=fail smtp.client-ip=40.92.58.104 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="T65IZc/s" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=xIbVcVlHDmY0uJ92JEOeU0sRBhx6bhSTdJNJiBEnHdlJObBQvQgs0g0U6KyYmKizYChZIHTsvav3v7mx4Wctk/tWt/DkqNiZIlP5aG7ZFMpYbedPirqQKqG2PqLrxxL027tmmvPepkJZrl7g7yZYicyl+BcI15+EKvTxxG4JTBiHrWjdsc2I9/NvSFVQvTdN+a4w7JnO2y/nvG9OjPoXAmAVsxJr4O2bo7eoal+YqA7hkwR9Ns648SEQVsQcfPZ1LJXFAV/EDYUCXshA/l52wUA2V3uqB9Jdoa+Lp0LnlOROVZHoBmMYMnae4dvcqKFcOloAqQfPz9P/4p85glM/7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=qOk1EtiAZ+PvJ1ok7hOJuKLuBgQ3Eoj1iPXa2vAg57c=; b=kcRShQtL7ltW/sG8Q72g7R6XsM8ZkrDTDgUasALasLSJ28jCSZxCDtI9EMricsfUU+sN3H7DDuilZLrvjESVLtWi4lCdIHQ2fgxD1Zy9LxiiYbBVJ4ubYLe+clMxiCdZXy4pVeIfH8O5LmU34LUZBwo+dEsxa2te0MlWdcbz0ZIQuuGKt0dHE0gE6Aca4Gk+2g25J49qJ6Ov6EMxMXmGgHP3JR1np4zU8tdOr/KSA8UCYHs6BTGo9ZfvzYO4q//mX1GsYTaAAr+5y2O17DuERQuSEUNjIDzqNtwub7RPz8VxeihvuOGllHUsDs+vavKjNDehdBxO72IGg2r8iltiyw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=qOk1EtiAZ+PvJ1ok7hOJuKLuBgQ3Eoj1iPXa2vAg57c=; b=T65IZc/sh/+d196vZucTR0oyx3AaIfTcbcOKx885D9SEE/jrjDFBA9Hp2Tcp691TdzqwzlBqv6MjKAdMSIttwG+dNhdH3uC1PmVgC2YvxE1V0/Ib+O5U9MjvlfSRHRekrkPg0Dq46Xz+uEOtf1OhD7n4q5MvlpckWY2PubXu68h7Y749bZJUIXWT1iKqDcqVWxC7j6DgvESm/XjnI7lYt91zrSLKc/EPu+SBi1Ar4RUJpP6MLpbo9qFcWKHMaH3dwGHJjRCw58gZWhDg8TRZZ9Hwytyw7cMF/RUjHVzE9Hf/ITzP197KbSPWmwJRAz2HLaMnpkUWaBPhjgpp2syLUA== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by AM0PR03MB6130.eurprd03.prod.outlook.com (2603:10a6:20b:151::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8093.32; Tue, 29 Oct 2024 22:41:11 +0000 Received: from AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7]) by AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7%4]) with mapi id 15.20.8093.027; Tue, 29 Oct 2024 22:41:11 +0000 From: Juntong Deng To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, memxor@gmail.com, snorcht@gmail.com Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH bpf-next 1/4] bpf/crib: Introduce task_file open-coded iterator kfuncs Date: Tue, 29 Oct 2024 22:39:40 +0000 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO4P123CA0476.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:1a8::13) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20241029223943.119979-1-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM6PR03MB5848:EE_|AM0PR03MB6130:EE_ X-MS-Office365-Filtering-Correlation-Id: 53fd812f-0a9d-47fd-66a8-08dcf86ac9b7 X-Microsoft-Antispam: BCL:0;ARA:14566002|461199028|19110799003|5072599009|15080799006|8060799006|3412199025|440099028; X-Microsoft-Antispam-Message-Info: rqv9xgBARMUwoVgkaQIU7/H+xmNOiwPKJ5sUN/IItvekja0AHci0a97Vx6kNIxOh1YSMUswJUZdHKLD+ovjgeun3Txaqp8DmBLRy1Ub+HRjnMgIRjZZOdZ98ZyFEub0mci3U5tACPtqH4KWOk5YXMI+IIhkIw3VX6bo8GRauRtH63L0wwel2ARBN/y96dMpkDE+9G3YW4ejJEtbMTABccwrdETg0D7yWSdGSTQ+moaPBxtQakBlY0kkqKDZmsQYzWC6mu0ESth6Iwk6auhhtlmnKvBCKEP+a+s6Qi53nc7o08qPU6y8yyHR68EyvEI+gsvVwUkOvg0B0Giu9+HnwQIASe6tEQc4eUxXMLb1I8bK36pdphurpChkN2+BEbYsFO0cOnaPp2xG86kjEPqTWnG3gXGImE0k3x1HnwIiY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dyK1htX//6uxn2hv/hoD4ykCHNTFUX6s6JXnVxMuzOxrhsg8DFag3/KdvpQcuDlGlnvPg/ukAKiweXkIpEpSMRNN4KBEKAj1/Q1supiPDZf3VkhjsfPDbVHng/0MmVGROm5ZkBh6aMrl99KmE6C1s4vWOcPI+hLakNq2scQSEpd/ikOGTmePn+61TYbsbOcoX7tiAfjG/4rsv88zShYz4qaNif2gQrboNaMl07KiXBFPyZlydbfdR/si/OOSB5NDdrb17JwXC8ZOsVs42G+enJ0TtZ2z/6d3sVwnAudyimfHJ0y7hapDqDnaatVioh0LKdqs7PcJ8xioRMJOgoaIYk7GP4twuMdOMVsM69l8Y0M/PeapPQp7SUsc1sRTltG+bWApS5zIcmSgOEcrv07+7Yrwzr+yLolHGN0QXFmM1TvPZXYc4ZGnbZ+R7CGPQd6RRwdTihpv+vPB9vGhIpj5XZNzHPruvIHqegBXhcE49auHym2Sd5c7ipXbuLh+aJsZWBEq9LrM+bmdkaZmTL1sOVmZSzyeaxX8pVXYGoClbzFhoS8k3PHX6PVYTEgmw4aWEvLbUydbHzP9y59oc1KxwJmOHO/VyvG9Zl+G14FKUWVPWIGuVvcxuWkfDBMpxgLHWnESigq+A97ny0nGoYq1BGI7/uGfhDMwLpv+EENv7Bt4welR7Knf/KMElL4XHTHFCXv4fNR5XsGelLW/NrwKYwYdwbYvsc94wMvvriQQx+aHSW+OFVLU9iKylGnhKi55wSLQog1JuNh5tNGijYtSGZag1VttxDTXuwwEhkAM6Il+pDmLFCz0zLxzDH3GK9ZSrUyxWp4mfsFByDv6rucdqldqsaD+iqSLoyIswDwDbC9/fxDav5FPxtYwkiE X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 53fd812f-0a9d-47fd-66a8-08dcf86ac9b7 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Oct 2024 22:41:11.6249 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM0PR03MB6130 X-Patchwork-Delegate: bpf@iogearbox.net This patch adds the open-coded iterator style process file iterator kfuncs bpf_iter_task_file_{new,next,destroy} that iterates over all files opened by the specified process. In addition, this patch adds bpf_iter_task_file_get_fd() getter to get the file descriptor corresponding to the file in the current iteration. The reference to struct file acquired by the previous bpf_iter_task_file_next() is released in the next bpf_iter_task_file_next(), and the last reference is released in the last bpf_iter_task_file_next() that returns NULL. In the bpf_iter_task_file_destroy(), if the iterator does not iterate to the end, then the last struct file reference is released at this time. Signed-off-by: Juntong Deng --- kernel/bpf/Makefile | 1 + kernel/bpf/crib/Makefile | 3 ++ kernel/bpf/crib/crib.c | 29 +++++++++++ kernel/bpf/crib/files.c | 105 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 138 insertions(+) create mode 100644 kernel/bpf/crib/Makefile create mode 100644 kernel/bpf/crib/crib.c create mode 100644 kernel/bpf/crib/files.c diff --git a/kernel/bpf/Makefile b/kernel/bpf/Makefile index 105328f0b9c0..933d36264e5e 100644 --- a/kernel/bpf/Makefile +++ b/kernel/bpf/Makefile @@ -53,3 +53,4 @@ obj-$(CONFIG_BPF_SYSCALL) += relo_core.o obj-$(CONFIG_BPF_SYSCALL) += btf_iter.o obj-$(CONFIG_BPF_SYSCALL) += btf_relocate.o obj-$(CONFIG_BPF_SYSCALL) += kmem_cache_iter.o +obj-$(CONFIG_BPF_SYSCALL) += crib/ diff --git a/kernel/bpf/crib/Makefile b/kernel/bpf/crib/Makefile new file mode 100644 index 000000000000..4e1bae1972dd --- /dev/null +++ b/kernel/bpf/crib/Makefile @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-$(CONFIG_BPF_SYSCALL) += crib.o files.o diff --git a/kernel/bpf/crib/crib.c b/kernel/bpf/crib/crib.c new file mode 100644 index 000000000000..e6536ee9a845 --- /dev/null +++ b/kernel/bpf/crib/crib.c @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Checkpoint/Restore In eBPF (CRIB) + */ + +#include +#include +#include + +BTF_KFUNCS_START(bpf_crib_kfuncs) + +BTF_ID_FLAGS(func, bpf_iter_task_file_new, KF_ITER_NEW | KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_iter_task_file_next, KF_ITER_NEXT | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_iter_task_file_get_fd) +BTF_ID_FLAGS(func, bpf_iter_task_file_destroy, KF_ITER_DESTROY) + +BTF_KFUNCS_END(bpf_crib_kfuncs) + +static const struct btf_kfunc_id_set bpf_crib_kfunc_set = { + .owner = THIS_MODULE, + .set = &bpf_crib_kfuncs, +}; + +static int __init bpf_crib_init(void) +{ + return register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &bpf_crib_kfunc_set); +} + +late_initcall(bpf_crib_init); diff --git a/kernel/bpf/crib/files.c b/kernel/bpf/crib/files.c new file mode 100644 index 000000000000..ececf150303f --- /dev/null +++ b/kernel/bpf/crib/files.c @@ -0,0 +1,105 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include + +struct bpf_iter_task_file { + __u64 __opaque[3]; +} __aligned(8); + +struct bpf_iter_task_file_kern { + struct task_struct *task; + struct file *file; + int fd; +} __aligned(8); + +__bpf_kfunc_start_defs(); + +/** + * bpf_iter_task_file_new() - Initialize a new task file iterator for a task, + * used to iterate over all files opened by a specified task + * + * @it: the new bpf_iter_task_file to be created + * @task: a pointer pointing to a task to be iterated over + */ +__bpf_kfunc int bpf_iter_task_file_new(struct bpf_iter_task_file *it, + struct task_struct *task) +{ + struct bpf_iter_task_file_kern *kit = (void *)it; + + BUILD_BUG_ON(sizeof(struct bpf_iter_task_file_kern) > sizeof(struct bpf_iter_task_file)); + BUILD_BUG_ON(__alignof__(struct bpf_iter_task_file_kern) != + __alignof__(struct bpf_iter_task_file)); + + kit->task = task; + kit->fd = -1; + kit->file = NULL; + + return 0; +} + +/** + * bpf_iter_task_file_next() - Get the next file in bpf_iter_task_file + * + * bpf_iter_task_file_next acquires a reference to the returned struct file. + * + * The reference to struct file acquired by the previous + * bpf_iter_task_file_next() is released in the next bpf_iter_task_file_next(), + * and the last reference is released in the last bpf_iter_task_file_next() + * that returns NULL. + * + * @it: the bpf_iter_task_file to be checked + * + * @returns a pointer to the struct file of the next file if further files + * are available, otherwise returns NULL + */ +__bpf_kfunc struct file *bpf_iter_task_file_next(struct bpf_iter_task_file *it) +{ + struct bpf_iter_task_file_kern *kit = (void *)it; + + if (kit->file) + fput(kit->file); + + kit->fd++; + + rcu_read_lock(); + kit->file = task_lookup_next_fdget_rcu(kit->task, &kit->fd); + rcu_read_unlock(); + + return kit->file; +} + +/** + * bpf_iter_task_file_get_fd() - Get the file descriptor corresponding to + * the file in the current iteration + * + * @it: the bpf_iter_task_file to be checked + * + * @returns the file descriptor + */ +__bpf_kfunc int bpf_iter_task_file_get_fd(struct bpf_iter_task_file *it__iter) +{ + struct bpf_iter_task_file_kern *kit = (void *)it__iter; + + return kit->fd; +} + +/** + * bpf_iter_task_file_destroy() - Destroy a bpf_iter_task_file + * + * If the iterator does not iterate to the end, then the last + * struct file reference is released at this time. + * + * @it: the bpf_iter_task_file to be destroyed + */ +__bpf_kfunc void bpf_iter_task_file_destroy(struct bpf_iter_task_file *it) +{ + struct bpf_iter_task_file_kern *kit = (void *)it; + + if (kit->file) + fput(kit->file); +} + +__bpf_kfunc_end_defs(); From patchwork Tue Oct 29 22:39:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13855592 X-Patchwork-Delegate: bpf@iogearbox.net Received: from EUR03-DBA-obe.outbound.protection.outlook.com (mail-dbaeur03olkn2044.outbound.protection.outlook.com [40.92.58.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DC0FE199FBB; Tue, 29 Oct 2024 22:42:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.58.44 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730241748; cv=fail; b=HRUtFLltgT5ErQFtcCxq0Te21LKQC5IbnxZ+F5hs6kc7z4EMJHTFmJnLlRxrh54mynGk+brMPGUMr+9dzHEC2MzX8y2t2r0jxvgSR+hvfK2rV94+GeRAiKjXfNRpSo7oc9CgmQLIzpSqW/803cFqBCRa3x5PmEiOjGSOWoEpS74= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730241748; c=relaxed/simple; bh=TmVbmywAbjQE9qy324iDqjWn/KJz3A/Q109kDadncfw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=qHYQmFZQ7xZ8Xq9ol62BMASORXpshnSodWhcV5uNMPPBn9FLgUhxgXXpUXAOtZ/HtwJK2GRqNoC2hwIQZhJA1EdC2/H6JHpmUUfDClPPDJfyoEK8AqUjBzm6J3M94VwhnV5T//o5ElreKfjtNXffTw/kPVvzPRWXLYYAB4GTYDE= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=ipthFgUk; arc=fail smtp.client-ip=40.92.58.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="ipthFgUk" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=PgGiuI/3XZY9PY+DvLKv7baDHGsrkdVovE1KNHlvRylA4v9Za1qacybOjINaW60hZKgTaqvAROfjrDquvqmsTI+rVFOvWyV2nBKI4A4MCWGDDVB1Kf7LLcOR/Keog8j8ZWR+H8VeEoOoEha9ovRH3gsGoE+GwYbzq6BJg1Iwn57zCEyfPl3a9KYmwsxGGPE79+jIn8o53dkekO9ZB5L0vjlwrytZnRCfxVSHufPWg7Wm0BmJnveNqkGOkyxg9mpaYoC4eH49lIRuweIbgEIMMTRZygv/VH82CIjkZwOh7EvshYRcynOzOJbvWadyjAlXMmj5/agozmwIrf8gmBLJXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=zCT168cnmImpgJ4I7ENlgp10y5Mp8vE5Je9OmnjFKxc=; b=a8lp7cM7xrBd46bz2vVsB2sAQ8oopPDFzKasPq/mixT3ImJpfedrYxZazF6f/aknw0H2dByJCAdAp/oZUXvRGxk15r2NC4BgPKaO9L9iHmf2sgEA7liszFhUC49NnufdM92eKeRn0iygPOoAucu0LNmE/oFXqp1HTh5/BT9kQ8NDo+mGq3isfY9eLgLU4/851q3CLQ/WC0wvrZnPNo+xzRAWu9/9p8NB0TCf4DKtPrD+e/fXcIU5E3GmWekPDjeO1DHLBx0+AlQbOcUdw+IrsdtrgwxU5VEfXcizCC0+judyd9RkTJCtpCOhNArT6E251Z170iSn+KbKW7iSfmUwgg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=zCT168cnmImpgJ4I7ENlgp10y5Mp8vE5Je9OmnjFKxc=; b=ipthFgUkbYnW/bsj7RZXjZLWBXajrHJu9R0wr9hGs2R9XjBpmbbyvymZFCYxMOjdUgn/55NW/B/I3uAbSM9oQHbzqtkQPy/Pqdo1pKTRDDtNVBXJKeUHcclx0eMoloKL5w+b782DW67HJ2kTy/kYGQaukHcqXzhEt9ypLonLoc8YIfHxi/kdPSe7nYLs3aFU0nV2GrhPJmlAcAF1hpENfkzuCuKYa1whTpv4xfImVvH7T8RV8dLdNRU/gLk8GmYNnVcBI5Nv5tJPZ/FqpBKeqBKv20mRp4QVdCR06c2YrCVgujiKBJ3J38bkTLH4c+DLE/63+6goxSfeI/8l1Y5GNw== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by AM0PR03MB6130.eurprd03.prod.outlook.com (2603:10a6:20b:151::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8093.32; Tue, 29 Oct 2024 22:42:23 +0000 Received: from AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7]) by AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7%4]) with mapi id 15.20.8093.027; Tue, 29 Oct 2024 22:42:23 +0000 From: Juntong Deng To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, memxor@gmail.com, snorcht@gmail.com Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH bpf-next 2/4] selftests/bpf: Add tests for open-coded style process file iterator Date: Tue, 29 Oct 2024 22:39:41 +0000 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO4P123CA0476.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:1a8::13) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20241029223943.119979-2-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM6PR03MB5848:EE_|AM0PR03MB6130:EE_ X-MS-Office365-Filtering-Correlation-Id: 54ade31d-561f-48e0-2799-08dcf86af489 X-Microsoft-Antispam: BCL:0;ARA:14566002|461199028|19110799003|5072599009|15080799006|8060799006|3412199025|440099028; X-Microsoft-Antispam-Message-Info: 9dL+b2SOQWHimagi6Xxzu4qOOhU5atQRfLY/b6REOXlgdh0Z7RlyBtkKqkIRIXyt70YP0GKl+UyLntQuW70zuxRh0X5So8UVWDvEMSheDLwKMLymmxEs0VCKqOvYkoQGv0onL+Po4k+jifQZUsc5HdCdiLoECFGZ8vL8zVddcmBXs9wfLYR4bTTnR4NlLIHQVgMH/aGdakLcxdSFoe6NI6TpCgrYgv2WNVoYIkUjPF+rRSiTcCsgrsWhcVetSm1zgq5kkTGUM/7dErAan/pHCtsZSe+Yi0K0VeH6X3tlzNqWRZQNCPNjqob8xCyrPjQt6ivr0ONXxtUgIh+HlUMJ4ssixw8tXcAsZeF2YHA1dgpN+pFthRg0quIaAx+cV7dIa2vEo6B5ssr+g+1ej5nB6H6F/axOS2eumPLRDdBnVIBqp49DIH2Y9Vtprhi6rlUHpp4BtWHyBQBhKCBwGH/h/ONFgBJ/Ra9zFX/g/SWI6Pw3VVtSqKmkNhcoyiNbcyXUxmnekFfWGapFaFXpZrIGBh3uEz21Kv6KnuHaVovK9fwEXLdDOWnat0XTiYONmJB7nra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VfWOLDBdhAuvRiIFOZIVE9QgvwbqaalIY3uDX0StcwAIDbIhMmKjb7fkIAUc3bIagKppvDgAwLWURIoig5gvXzJK3ipMOc66dqcyTczJCHe+1CSWBLNiRemrTG7unk1BOyHTsgyLFT6xThpoZ8fPyHNgfn3LRkTrLmPmKXakeuB9MXu17Qtm5SVGSdRgwMQc39uNNn7KmrqC9Hsxzj26D7QLon438v4O9Om/HXTwWj+aeYz72Dsg0zew505pnWIbdyMneY6LNunGmLeRo2Hoz0UAA0bX1WWNrCSbw2Za2GZlhStQ/fl1BkQQELvHxr60TPKEhlQpr+iEhlMDoG4b0PBwlDji2AAdbRktAtwnj1h3IVAkR25n4nQ5d93DK5d48BJLPhynLSTMSFTTE/W6Ra/p2wqoha85A7D0YtQ4mPwYovQJS3bNkyipKa7etNAKXOxHQUFcI0Zqb9tgjAjkSBhoDCuo2fG5/xv2CO01xVppJ8ZVohvHKe5vsImCrZ5tJwi4s+M0MJowpApS4UpUon9FwGHkEG4VssNffAl0MNR4M9AZlpFgUbiGneWKoo3duko2OI62OE702M2jWSECIIHQubzr4IxJOfu2DP2YwnGZGdEZ5PHYbxDKSX+QBKcw9x+kcdFqNhex/uc3T+d0IN3+GKT4qrUKlG7VHMbBOdAPQGxev9vEIfcutiI3B7gAEtF8j0lOGzDt9YhkaJm+VfkB58yoKHKAj6 X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 54ade31d-561f-48e0-2799-08dcf86af489 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Oct 2024 22:42:23.4663 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM0PR03MB6130 X-Patchwork-Delegate: bpf@iogearbox.net This patch adds test cases for open-coded style process file iterator. Test cases related to process files are run in the newly created child process. Close all opened files inherited from the parent process in the child process to avoid the files opened by the parent process affecting the test results. In addition, this patch adds failure test cases where bpf programs cannot pass the verifier due to uninitialized or untrusted arguments, etc. Signed-off-by: Juntong Deng --- tools/testing/selftests/bpf/prog_tests/crib.c | 125 ++++++++++++++++++ .../testing/selftests/bpf/progs/crib_common.h | 21 +++ .../selftests/bpf/progs/crib_files_failure.c | 86 ++++++++++++ .../selftests/bpf/progs/crib_files_success.c | 73 ++++++++++ 4 files changed, 305 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/crib.c create mode 100644 tools/testing/selftests/bpf/progs/crib_common.h create mode 100644 tools/testing/selftests/bpf/progs/crib_files_failure.c create mode 100644 tools/testing/selftests/bpf/progs/crib_files_success.c diff --git a/tools/testing/selftests/bpf/prog_tests/crib.c b/tools/testing/selftests/bpf/prog_tests/crib.c new file mode 100644 index 000000000000..48c5156504ad --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/crib.c @@ -0,0 +1,125 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define _GNU_SOURCE +#include +#include +#include +#include +#include "crib_files_failure.skel.h" +#include "crib_files_success.skel.h" + +struct files_test_args { + bool *setup_end; + bool *cr_end; +}; + +static int files_test_process(void *args) +{ + struct files_test_args *test_args = (struct files_test_args *)args; + int pipefd[2], sockfd, err = 0; + + /* Create a clean file descriptor table for the test process */ + close_range(0, ~0U, 0); + + if (pipe(pipefd) < 0) + return 1; + + sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (sockfd < 0) { + err = 2; + goto cleanup_pipe; + } + + *test_args->setup_end = true; + + while (!*test_args->cr_end) + ; + + close(sockfd); +cleanup_pipe: + close(pipefd[0]); + close(pipefd[1]); + return err; +} + +static void run_files_success_test(const char *prog_name) +{ + int prog_fd, child_pid, wstatus, err = 0; + const int stack_size = 1024 * 1024; + struct crib_files_success *skel; + struct files_test_args args; + struct bpf_program *prog; + bool setup_end, cr_end; + char *stack; + + skel = crib_files_success__open_and_load(); + if (!ASSERT_OK_PTR(skel, "open_and_load")) + return; + + if (!ASSERT_OK(skel->bss->err, "pre_test_err")) + goto cleanup_skel; + + prog = bpf_object__find_program_by_name(skel->obj, prog_name); + if (!ASSERT_OK_PTR(prog, "find_program_by_name")) + goto cleanup_skel; + + prog_fd = bpf_program__fd(prog); + if (!ASSERT_GT(prog_fd, -1, "bpf_program__fd")) + goto cleanup_skel; + + stack = (char *)malloc(stack_size); + if (!ASSERT_OK_PTR(stack, "clone_stack")) + return; + + setup_end = false; + cr_end = false; + + args.setup_end = &setup_end; + args.cr_end = &cr_end; + + /* Note that there is no CLONE_FILES */ + child_pid = clone(files_test_process, stack + stack_size, CLONE_VM | SIGCHLD, &args); + if (!ASSERT_GT(child_pid, -1, "child_pid")) + goto cleanup_stack; + + while (!setup_end) + ; + + skel->bss->pid = child_pid; + + err = bpf_prog_test_run_opts(prog_fd, NULL); + if (!ASSERT_OK(err, "prog_test_run")) + goto cleanup_stack; + + cr_end = true; + + if (!ASSERT_GT(waitpid(child_pid, &wstatus, 0), -1, "waitpid")) + goto cleanup_stack; + + if (!ASSERT_OK(WEXITSTATUS(wstatus), "run_files_test_err")) + goto cleanup_stack; + + ASSERT_OK(skel->bss->err, "run_files_test_failure"); +cleanup_stack: + free(stack); +cleanup_skel: + crib_files_success__destroy(skel); +} + +static const char * const files_success_tests[] = { + "test_bpf_iter_task_file", +}; + +void test_crib(void) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(files_success_tests); i++) { + if (!test__start_subtest(files_success_tests[i])) + continue; + + run_files_success_test(files_success_tests[i]); + } + + RUN_TESTS(crib_files_failure); +} diff --git a/tools/testing/selftests/bpf/progs/crib_common.h b/tools/testing/selftests/bpf/progs/crib_common.h new file mode 100644 index 000000000000..93b8f9b1bdf8 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/crib_common.h @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef __CRIB_COMMON_H +#define __CRIB_COMMON_H + +#include +#include +#include +#include + +extern struct task_struct *bpf_task_from_vpid(s32 vpid) __ksym; +extern void bpf_task_release(struct task_struct *p) __ksym; + +struct bpf_iter_task_file; +extern int bpf_iter_task_file_new(struct bpf_iter_task_file *it, + struct task_struct *task) __ksym; +extern struct file *bpf_iter_task_file_next(struct bpf_iter_task_file *it) __ksym; +extern int bpf_iter_task_file_get_fd(struct bpf_iter_task_file *it__iter) __ksym; +extern void bpf_iter_task_file_destroy(struct bpf_iter_task_file *it) __ksym; + +#endif /* __CRIB_COMMON_H */ diff --git a/tools/testing/selftests/bpf/progs/crib_files_failure.c b/tools/testing/selftests/bpf/progs/crib_files_failure.c new file mode 100644 index 000000000000..ebae01d87ff9 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/crib_files_failure.c @@ -0,0 +1,86 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" +#include +#include +#include "bpf_misc.h" +#include "crib_common.h" + +char _license[] SEC("license") = "GPL"; + +SEC("syscall") +__failure __msg("expected uninitialized iter_task_file as arg #1") +int bpf_iter_task_file_new_inited_iter(void *ctx) +{ + struct bpf_iter_task_file task_file_it; + struct task_struct *task; + + task = bpf_get_current_task_btf(); + + bpf_iter_task_file_new(&task_file_it, task); + + bpf_iter_task_file_new(&task_file_it, task); + + bpf_iter_task_file_destroy(&task_file_it); + return 0; +} + +SEC("syscall") +__failure __msg("Possibly NULL pointer passed to trusted arg1") +int bpf_iter_task_file_new_untrusted_task(void *ctx) +{ + struct bpf_iter_task_file task_file_it; + struct task_struct *task = NULL; + + bpf_iter_task_file_new(&task_file_it, task); + + bpf_iter_task_file_destroy(&task_file_it); + return 0; +} + +SEC("syscall") +__failure __msg("Unreleased reference") +int bpf_iter_task_file_no_destory(void *ctx) +{ + struct bpf_iter_task_file task_file_it; + struct task_struct *task; + + task = bpf_get_current_task_btf(); + + bpf_iter_task_file_new(&task_file_it, task); + + return 0; +} + +SEC("syscall") +__failure __msg("expected an initialized iter_task_file as arg #1") +int bpf_iter_task_file_next_uninit_iter(void *ctx) +{ + struct bpf_iter_task_file task_file_it; + + bpf_iter_task_file_next(&task_file_it); + + return 0; +} + +SEC("syscall") +__failure __msg("expected an initialized iter_task_file as arg #1") +int bpf_iter_task_file_get_fd_uninit_iter(void *ctx) +{ + struct bpf_iter_task_file task_file_it; + + bpf_iter_task_file_get_fd(&task_file_it); + + return 0; +} + +SEC("syscall") +__failure __msg("expected an initialized iter_task_file as arg #1") +int bpf_iter_task_file_destroy_uninit_iter(void *ctx) +{ + struct bpf_iter_task_file task_file_it; + + bpf_iter_task_file_destroy(&task_file_it); + + return 0; +} diff --git a/tools/testing/selftests/bpf/progs/crib_files_success.c b/tools/testing/selftests/bpf/progs/crib_files_success.c new file mode 100644 index 000000000000..92ca7d9d44c3 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/crib_files_success.c @@ -0,0 +1,73 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" +#include +#include +#include "bpf_misc.h" +#include "crib_common.h" + +char _license[] SEC("license") = "GPL"; + +int err, pid; + +SEC("syscall") +int test_bpf_iter_task_file(void *ctx) +{ + struct bpf_iter_task_file task_file_it; + struct task_struct *task; + struct file *file; + int fd; + + task = bpf_task_from_vpid(pid); + if (task == NULL) { + err = 1; + return 0; + } + + bpf_iter_task_file_new(&task_file_it, task); + + file = bpf_iter_task_file_next(&task_file_it); + if (file == NULL) { + err = 2; + goto cleanup; + } + + fd = bpf_iter_task_file_get_fd(&task_file_it); + if (fd != 0) { + err = 3; + goto cleanup; + } + + file = bpf_iter_task_file_next(&task_file_it); + if (file == NULL) { + err = 4; + goto cleanup; + } + + fd = bpf_iter_task_file_get_fd(&task_file_it); + if (fd != 1) { + err = 5; + goto cleanup; + } + + file = bpf_iter_task_file_next(&task_file_it); + if (file == NULL) { + err = 6; + goto cleanup; + } + + fd = bpf_iter_task_file_get_fd(&task_file_it); + if (fd != 2) { + err = 7; + goto cleanup; + } + + file = bpf_iter_task_file_next(&task_file_it); + if (file != NULL) + err = 7; + +cleanup: + bpf_iter_task_file_destroy(&task_file_it); + bpf_task_release(task); + return 0; +} From patchwork Tue Oct 29 22:39:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13855593 X-Patchwork-Delegate: bpf@iogearbox.net Received: from EUR03-DBA-obe.outbound.protection.outlook.com (mail-dbaeur03olkn2063.outbound.protection.outlook.com [40.92.58.63]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5F6BA20B1EA; Tue, 29 Oct 2024 22:43:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.58.63 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730241824; cv=fail; b=EAIDYif7s/EbSCIxYTPKP8zKJ1TLYf2qD07rFyflTqS4MweMEl+stN3FoSuuEGgU5BvsJ40lLJfv1ohVCFpTbWBFvuXVLFMxLYi3bapuQNYCD+YBDiT/AQQgxTU0X7nzmX4CWzDYkppJ8SGENpBUSIa2rd+HTXjfcJnzjSfnvOY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730241824; c=relaxed/simple; bh=dx0ybxqUrlzgqwmQ0ztItlfqUEB1SRgoqUxNidJDbjw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=PFwspKd38ewPI5FJn+OiSjEUKyBUcpmk9pwLy3oz9BxKba2ZXicGvi72oDnQp6X5H9PRNhTxtb5vGqBajcu7iD42C1eZkNsgH4DS/pKkaEopRpLpss5VqdQ+ecDnmIM6ygXZaxb2xgKMYoXtcJe4Okikk4zP936sl7+6LEHF/n8= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=f/HxgFnX; arc=fail smtp.client-ip=40.92.58.63 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="f/HxgFnX" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=uNkj3OO275E52hCaMII1mtFiwVdA7mtWYcFtu1/hFwxSP6b6ABm/v7vmh3QI4e9gZjt7in/FTSPvHKhUXUexIH6oIR0nsvx3cr4ZzHcc2jA9HL/W5laO83CKu7TCec15ITWEy+SBIQ1smFFNWdrsCmyRQgEpF/QJictlcYEpAqzGI406eKWZ7LckK1jNgBX7xMU63pCQYVHNJ1vRxcaqPs5/9s9c21m958+dyp3bF/9rh+Om0EPMxPliDkJ+U7Esf9xYfaGv4+pJkTpXeEsFYdwfDBI0bMjk/1wYZIDKFqXiOyXfsA0u1+5KbbEU4HX+Wj8qHJTTiH9IOO1qIffoTA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=V9pxNH/v7o/ozsnWjwtaN8YP8vTDpkqyZeoksCF/+G8=; b=qVy694ZjHb99PgFZPnKfxLlERAR82AgcEkrgknK9CXHQSmCPl3jG+HvFZ4oYQZq0N7OY7TolkKom18vZ4XvsQ0xM1YIMyTY/y1q2XD1EE8em5ctKqkypQP9qcjzrTqpK9KUx2RhljS7R3QRc9/lyDrUOVDZU4rj3ckR0HSobn6nhk6qhiCOCPVHG03McImEWL+nPCWaZqa9dwF9BgneIGSP7+OqGi062jYrdl8cWOKCktsqn4wI3cdmjGL9RZsUfU11lZelTH/sY46s/N0VU5FAmVR/wj+mxXiXvcZiBgWLXQcLplEnpQLsOYnc7Na3rzDofgVZXkOgcyoTGSVZl+A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=V9pxNH/v7o/ozsnWjwtaN8YP8vTDpkqyZeoksCF/+G8=; b=f/HxgFnXb5OtpV1+HxqFvV3Ui3r/vkzb8KdsP3oRl0l8JyfsMYqugxvEbp2sw9SmTUE1MNAFQt/1e050IS9FnS2+6oD0p+BSHdI5qK65AVSwSIg6RKIARrxn6HApVoRJEjJKq6+M/FtDBmIJg5gkTCVGJaehwf5H6I0of2iUO9PgzrGVKKwkzbDXP0tdpTA5l4RwKMXtSMsWtXLOsq9DzMgxahfvOglUejtSZFmHEYk6dP/RS/swTiYtE6LeB4awJFVxWEfO16ZePcmTDwphIHt16ugNB8XFqmd4eqSWMX3tnJQGoZvsTjWv5yD7ZLB/fGEfh7tcUe1ErG7rEZokiw== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by AM0PR03MB6130.eurprd03.prod.outlook.com (2603:10a6:20b:151::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8093.32; Tue, 29 Oct 2024 22:43:39 +0000 Received: from AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7]) by AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7%4]) with mapi id 15.20.8093.027; Tue, 29 Oct 2024 22:43:38 +0000 From: Juntong Deng To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, memxor@gmail.com, snorcht@gmail.com Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH bpf-next 3/4] bpf/crib: Add struct file related CRIB kfuncs Date: Tue, 29 Oct 2024 22:39:42 +0000 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO4P123CA0476.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:1a8::13) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20241029223943.119979-3-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM6PR03MB5848:EE_|AM0PR03MB6130:EE_ X-MS-Office365-Filtering-Correlation-Id: 3c575fe1-6237-42a9-7e70-08dcf86b2170 X-Microsoft-Antispam: BCL:0;ARA:14566002|461199028|19110799003|5072599009|15080799006|8060799006|3412199025|440099028; X-Microsoft-Antispam-Message-Info: LTat2fHjVh4uQ5c6Znr8Vbdx6j0nZBze1hFlFzTswHd9mPNhgRLdBFediVCtcZVTJ0vA8aOAftJFaqMRkXgQMc0kMlDstLrC4txDR5lpj4ExKM2ZFfnZOzg2aAF+MPUDT22560nPO1UwrE99NJREdV65gPX8AzAJ1piQhYTv9ho6829/jFpS0Pk0dQ8FOe71Kv52Vp2C2B4zgNHRGXVQvwThnNZnbUeneqf33/G2irZzIaftSVLqspwZmwCf9jXkZ2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Id: 3c575fe1-6237-42a9-7e70-08dcf86b2170 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Oct 2024 22:43:38.8358 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM0PR03MB6130 X-Patchwork-Delegate: bpf@iogearbox.net This patch adds struct file related CRIB kfuncs. bpf_fget_task() is used to get a pointer to the struct file corresponding to the task file descriptor. Note that this function acquires a reference to struct file. bpf_get_file_ops_type() is used to determine what exactly this file is based on the file operations, such as socket, eventfd, timerfd, pipe, etc, in order to perform different checkpoint/restore processing for different file types. This function currently has only one return value, FILE_OPS_UNKNOWN, but will increase with the file types that CRIB supports for checkpoint/restore. Signed-off-by: Juntong Deng --- kernel/bpf/crib/crib.c | 4 ++++ kernel/bpf/crib/files.c | 44 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/kernel/bpf/crib/crib.c b/kernel/bpf/crib/crib.c index e6536ee9a845..78ddd19d5693 100644 --- a/kernel/bpf/crib/crib.c +++ b/kernel/bpf/crib/crib.c @@ -14,6 +14,10 @@ BTF_ID_FLAGS(func, bpf_iter_task_file_next, KF_ITER_NEXT | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_iter_task_file_get_fd) BTF_ID_FLAGS(func, bpf_iter_task_file_destroy, KF_ITER_DESTROY) +BTF_ID_FLAGS(func, bpf_fget_task, KF_ACQUIRE | KF_TRUSTED_ARGS | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_get_file_ops_type, KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_put_file, KF_RELEASE) + BTF_KFUNCS_END(bpf_crib_kfuncs) static const struct btf_kfunc_id_set bpf_crib_kfunc_set = { diff --git a/kernel/bpf/crib/files.c b/kernel/bpf/crib/files.c index ececf150303f..8e0e29877359 100644 --- a/kernel/bpf/crib/files.c +++ b/kernel/bpf/crib/files.c @@ -5,6 +5,14 @@ #include #include +/** + * This enum will grow with the file types that CRIB supports for + * checkpoint/restore. + */ +enum { + FILE_OPS_UNKNOWN = 0 +}; + struct bpf_iter_task_file { __u64 __opaque[3]; } __aligned(8); @@ -102,4 +110,40 @@ __bpf_kfunc void bpf_iter_task_file_destroy(struct bpf_iter_task_file *it) fput(kit->file); } +/** + * bpf_fget_task() - Get a pointer to the struct file corresponding to + * the task file descriptor + * + * Note that this function acquires a reference to struct file. + * + * @task: the specified struct task_struct + * @fd: the file descriptor + * + * @returns the corresponding struct file pointer if found, + * otherwise returns NULL + */ +__bpf_kfunc struct file *bpf_fget_task(struct task_struct *task, unsigned int fd) +{ + struct file *file; + + file = fget_task(task, fd); + return file; +} + +/** + * bpf_get_file_ops_type() - Determine what exactly this file is based on + * the file operations, such as socket, eventfd, timerfd, pipe, etc + * + * This function will grow with the file types that CRIB supports for + * checkpoint/restore. + * + * @file: a pointer to the struct file + * + * @returns the file operations type + */ +__bpf_kfunc unsigned int bpf_get_file_ops_type(struct file *file) +{ + return FILE_OPS_UNKNOWN; +} + __bpf_kfunc_end_defs(); From patchwork Tue Oct 29 22:39:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13855594 X-Patchwork-Delegate: bpf@iogearbox.net Received: from EUR03-VI1-obe.outbound.protection.outlook.com (mail-vi1eur03olkn2092.outbound.protection.outlook.com [40.92.57.92]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3AA9520B1E1; Tue, 29 Oct 2024 22:44:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.57.92 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730241883; cv=fail; b=kuuTwzDVQJqKKCcOP2Mb1cRxYj89e5ZblW2X9ROJK6Hc6Lrfem7f7FZIOXAdGIk2Cex6lvKr57DXMzJd5y1vl/LbOcCUwCvTNhzJaWdEaLx1qprJqj3hkLnmhijXhByzmjkndHegoZSSxaVAvBM/hsR9uO3TsX+AV5VEC2BFsMc= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730241883; c=relaxed/simple; bh=vSGbKIF2sRp0vn+4IiqEk+mTnGBFk1oiOkzE23fOWyU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=Ha3je6xJBuRUZTeAyi0Ul7IRiMoxjGDygbP8Dez89PIu83b4yBKuKr/T9aBY4/BPDKATHvn6pukEew8LNuroJF8AK9kB3IMrB+VM8LSwqgl5KShbONv10Suqfpz7s7zftTtdNMR0n/JD9CzvJ/LW44a+nyrz8cbPyDhWbTvjhLU= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=Kom1IbAN; arc=fail smtp.client-ip=40.92.57.92 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="Kom1IbAN" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=lbWU5PbV/yMusd9LK3egT3taYZquxTkH2gWJATh4pRlq4yGpE7BByDEFUe7H8Q/8A778wGf2jZr/7DFJORGNbL/1S31sTDSPGV3UitK49gUMnhJxBU1rHCWuyE82qnfpAsCs0Zo+eQ8sZWZquBEk+0QaIJM8txw8sXEVRAoH/f8626eQ8AFGxWbfKsLKCSCg7vzhQ6+Cl5d2MVhSgG70fJGNg/PzOunmth5oVX8swCoYu4tnQRwESnqT7+n1GDP099gYLkmzbMronCqYtZ8CNkYu293zXTshfYFCAkrbu77DuCuzPYL3wfwJKSboOEJo8q5aMydaCSKNemYVubFSdg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=R8tUpDDVleEdlfs21asoU69Z0Pd+WVxTF9CVlBoq5E8=; b=GOZcVPuWrmk26nPuGtFe0CdKMMoVyN261ZuojvSMuO7TjoIlCv/EVwy00NPvb89nCfEZg+oGvfEL+laY/L4dBD6UU8q3wmuSYxXs9gBbwZS/LCVmKLTM+B0XuNlYmziGwA34tw79nz512Dm+p/z5LHN0PnCvrN+UV23K+fsjQRAFayST9jfu5com1/5188O0b/4ChFRm5nxZpxMcinXmo/w+k7V48JypYSrsOeOoHMNKoM9/eNnLrYK7bOEha5W3yAEE4utXkF8x/QNtPYQLpbF5dj55tkwV7NIOEocMK/j3sk4g7PFd6GI7TFc5Hkcj3Yb1cLGVZBAoqPP4PEQ8aQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=R8tUpDDVleEdlfs21asoU69Z0Pd+WVxTF9CVlBoq5E8=; b=Kom1IbANPdMFdmwFUo3A8SvTXuMx0L2s0BwTZMvHDwGQFfm53EzRr76L1yP89ZpDvTks9jT+Ly41fj0oYEdDvlUb+Pd6WRYG+ARSxBXLRRNSG489WATomfFT/QLg1SFKVy+Qc6I9O9tEHemadR4OY+eocs30fYNKbYUIiIF/WXgCglvWgVqwusABkkKEbDklvy9fduUpLVdHHORFzqN8GI7KBdjnKhpKuboqzj1q02qpbQ5EjQGWpSQxcUDGzInIgocICn61ouF9VooKABhwBBDrGgQzZi6aqnGfAjCCcv7uXS6KX20TdbhMznjJcs9LyDegkNX6fgBFvD6U/rTpTQ== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by AM0PR03MB6130.eurprd03.prod.outlook.com (2603:10a6:20b:151::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8093.32; Tue, 29 Oct 2024 22:44:38 +0000 Received: from AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7]) by AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7%4]) with mapi id 15.20.8093.027; Tue, 29 Oct 2024 22:44:38 +0000 From: Juntong Deng To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, memxor@gmail.com, snorcht@gmail.com Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH bpf-next 4/4] selftests/bpf: Add tests for struct file related CRIB kfuncs. Date: Tue, 29 Oct 2024 22:39:43 +0000 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO4P123CA0476.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:1a8::13) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20241029223943.119979-4-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM6PR03MB5848:EE_|AM0PR03MB6130:EE_ X-MS-Office365-Filtering-Correlation-Id: dc5878f6-4a63-481d-b6b9-08dcf86b44e6 X-Microsoft-Antispam: BCL:0;ARA:14566002|461199028|19110799003|5072599009|15080799006|8060799006|3412199025|440099028; X-Microsoft-Antispam-Message-Info: lY/6nX5xg5lObs9b/FaHoNUQUeJmru/BpSGYsrtCWraqa5KlXNCFnRu2QWcVgj/XwvOL4nmTJ5pMOYRxQzvy1GeQbiEZqCHvjJy2EGh/ETV4aYUU21b0nhOtwoVU5Myri7u9aAwMhon9pncqw9PMp/13/F58eY6VgU3lF8jPNuEzXK8Z4XbCfkYC8UWDF8nBwj+YyyUjd1U+n5xOKADxWcbwDsNZiP1XQx9mMyau2cPHGBeebVCqCaUhq5EZ7NmeRS74EpiuCzBBtoCknutk87R+iPCPOnAY54UIeQVWFB33JDLpmShhS5K8vMgB6tYRUyxYlJm1OZETbA/dIjnDmKbJORiLjbJOXZ8LC3S6rwQjlYyNjc7qM5f5HUq5sUyFsn2o5TVMEBajMQUkgQPbDCZCMhL5cA/uiNznFZhQaAmPeKWhzNEzZJhSB5Qfsr2Kk1Yiqr9vejr73tkXmnQAULcNc/dRw8BancUcE9mxfQ1juXhNqr+n0VfN9MWdm+JhNReW/wid42jvZewxP+m6fbiul2GTeEBhpPkTKfOJBUUSLwVtDJV+P4BzbAj1GcfJqz2+1LDZSj5mdtic8SRHYT8CJSgWxTZ69TqRq+ehxVx/Y/VraZff4HbDRAs0a0UK6gOqJ5cObSBXuRAXVI6a+VEKttFLdBbYnSNkgZ1uKvZiWI4u5mqrfqCkCJ/Rz1YpraUHrIoKqxJXQC4mZR2FKtPOux6eH1PDTyaxkK45HNJUC7TB8KN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ka4JeU3iud2ZsW/kEDen4XFMl1BPYiv98eZjYMuroCNfihov0SWkmv5DAziE+e/q6U9RQ6WoMCl+8qxdLq89SRUq6HBy4FYB7ZKKVYLJACRuY7R+TguEtHfg0sLVBjY+nFerROpico1GOLHHccFeUGUukddeKBkHuzQCMx7ynsY4QGyVa7u/3e9dnE1NwvBakxFizQp2Li2/r/EHkX41VK31X6oDbq5zY5d9KZhW3e5K4MjECXQfN2dA4Cb175MLbStQKzh7kOOZlDde+4pBb9COQYHRyzaiA8d/QoGhx+dF/QMRWriULfZxYsndWeTWTUryXJlsVBk0mT6eiP0vE7+6Zf4zANocB2JwR4iC25ouTzXv4MZSKkutdEKVdCJSrMJsdbk2jviAW9Fre8oKuM1eyKiaHHB/Q0eXA+GueHLmGm4dlYLKwr1OIvlqG1ZtahikW1Qg69hLRuw2qOOOI2yOgAbVvTujB61idQhu3lVDwWdD1mtKEtzr2g6RU7Qy12jTVSo2z5q911CA5e9/NRZVq87bLatxdK X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: dc5878f6-4a63-481d-b6b9-08dcf86b44e6 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Oct 2024 22:44:38.3008 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM0PR03MB6130 X-Patchwork-Delegate: bpf@iogearbox.net This patch adds test cases for struct file related CRIB kfuncs. The test case for bpf_fget_task() is written based on files_test_process(), so there should only be 3 opened files, corresponding to file descriptors 0, 1, 2. bpf_get_file_ops_type() currently only returns FILE_OPS_UNKNOWN, so no test cases are needed for now. In addition, this patch adds failure test cases where bpf programs cannot pass the verifier due to untrusted pointer arguments. Signed-off-by: Juntong Deng --- tools/testing/selftests/bpf/prog_tests/crib.c | 1 + .../testing/selftests/bpf/progs/crib_common.h | 4 ++ .../selftests/bpf/progs/crib_files_failure.c | 22 +++++++++ .../selftests/bpf/progs/crib_files_success.c | 46 +++++++++++++++++++ 4 files changed, 73 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/crib.c b/tools/testing/selftests/bpf/prog_tests/crib.c index 48c5156504ad..5ef887e43170 100644 --- a/tools/testing/selftests/bpf/prog_tests/crib.c +++ b/tools/testing/selftests/bpf/prog_tests/crib.c @@ -108,6 +108,7 @@ static void run_files_success_test(const char *prog_name) static const char * const files_success_tests[] = { "test_bpf_iter_task_file", + "test_bpf_fget_task", }; void test_crib(void) diff --git a/tools/testing/selftests/bpf/progs/crib_common.h b/tools/testing/selftests/bpf/progs/crib_common.h index 93b8f9b1bdf8..7b6f72ca6a6f 100644 --- a/tools/testing/selftests/bpf/progs/crib_common.h +++ b/tools/testing/selftests/bpf/progs/crib_common.h @@ -18,4 +18,8 @@ extern struct file *bpf_iter_task_file_next(struct bpf_iter_task_file *it) __ksy extern int bpf_iter_task_file_get_fd(struct bpf_iter_task_file *it__iter) __ksym; extern void bpf_iter_task_file_destroy(struct bpf_iter_task_file *it) __ksym; +extern struct file *bpf_fget_task(struct task_struct *task, int fd) __ksym; +extern unsigned int bpf_get_file_ops_type(struct file *file) __ksym; +extern void bpf_put_file(struct file *file) __ksym; + #endif /* __CRIB_COMMON_H */ diff --git a/tools/testing/selftests/bpf/progs/crib_files_failure.c b/tools/testing/selftests/bpf/progs/crib_files_failure.c index ebae01d87ff9..9360aad50c15 100644 --- a/tools/testing/selftests/bpf/progs/crib_files_failure.c +++ b/tools/testing/selftests/bpf/progs/crib_files_failure.c @@ -84,3 +84,25 @@ int bpf_iter_task_file_destroy_uninit_iter(void *ctx) return 0; } + +SEC("syscall") +__failure __msg("Possibly NULL pointer passed to trusted arg0") +int bpf_fget_task_untrusted_file(void *ctx) +{ + struct task_struct *task = NULL; + + bpf_fget_task(task, 1); + + return 0; +} + +SEC("syscall") +__failure __msg("Possibly NULL pointer passed to trusted arg0") +int bpf_get_file_ops_type_untrusted_file(void *ctx) +{ + struct file *file = NULL; + + bpf_get_file_ops_type(file); + + return 0; +} diff --git a/tools/testing/selftests/bpf/progs/crib_files_success.c b/tools/testing/selftests/bpf/progs/crib_files_success.c index 92ca7d9d44c3..8dddac6a7378 100644 --- a/tools/testing/selftests/bpf/progs/crib_files_success.c +++ b/tools/testing/selftests/bpf/progs/crib_files_success.c @@ -71,3 +71,49 @@ int test_bpf_iter_task_file(void *ctx) bpf_task_release(task); return 0; } + +SEC("syscall") +int test_bpf_fget_task(void *ctx) +{ + struct task_struct *task; + struct file *file; + + task = bpf_task_from_vpid(pid); + if (task == NULL) { + err = 1; + return 0; + } + + file = bpf_fget_task(task, 0); + if (file == NULL) { + err = 2; + goto cleanup; + } + + bpf_put_file(file); + + file = bpf_fget_task(task, 1); + if (file == NULL) { + err = 3; + goto cleanup; + } + + bpf_put_file(file); + + file = bpf_fget_task(task, 2); + if (file == NULL) { + err = 4; + goto cleanup; + } + + bpf_put_file(file); + + file = bpf_fget_task(task, 3); + if (file != NULL) { + err = 5; + bpf_put_file(file); + } +cleanup: + bpf_task_release(task); + return 0; +}