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();