From patchwork Wed Oct 30 00:14:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13855790 X-Patchwork-Delegate: bpf@iogearbox.net Received: from EUR02-DB5-obe.outbound.protection.outlook.com (mail-db5eur02olkn2103.outbound.protection.outlook.com [40.92.50.103]) (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 27B8BBE65; Wed, 30 Oct 2024 00:15:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.50.103 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730247318; cv=fail; b=Eu8RaDRV2oKM+w1M8GOT/3kNAQKb8aLhpT3tojxCCad5OQC7xLofluUrTvRv4725sZ7Er8MZd6K+zOZHKNbmpQ7mjwdluEUDlxC2cbfNxUM2n76FaOUv4C2kTK+YCkWhaa5Iorle23zqf/1P0Igg4vNZfBYZzXg3Z9EQmyRPoBQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730247318; c=relaxed/simple; bh=GwOMTBxL7EBlQbeUT5o3SQTv1nbDs02IDXR055rBeOM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=cDAPH+CAp4CjO6Fin8E4YwPvJBT5r+jP5FsSEZrH9WnNUckBm98D5xtcahd6ES9kcygPTKapA7I9sJqIS1YCutpU0XOhW/ih4qXf7ZtYfmGPiP6bsk5lxv+K/hXfzb/8csLQVTeUXkAwZce7QPymrtywYzjOm52xDmJ+7wOejlw= 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=SqnrPC60; arc=fail smtp.client-ip=40.92.50.103 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="SqnrPC60" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=nb38FPPJtIkSmqJKoIdewSR/8cb3ZWd0RyzrJZcbmYALuoQj7/u6VG2uxRBuEEwC586YL4v9u26sfW2A7pFeUOXbyPvLun2IWFoie/be/0u3SL7JBc/5eP3IB5LJDG5ABcbrDTU8+FqOGxBu8fp42KzZVrdPNsk1AiDdiLpfqIfnOUk1oO/srn/EyFxqICbgq4T0yiY64jriQ3y0hDEJ0I2O4Dep1wCMBVTUVxZghLcgblZvLbAdhySpKIoQHrUv7+ag8/NR+X1imPXyQzGB0DElSPQTPf4mq1Z6VA2wnP8bFa7iau/IhKcaKaiaKXzIk8U15TVog7A67FAJ0W7mGA== 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=R3NsVhC+MZjCKsDPN3zY0YlceSofry2CNtzFV90tGEt+5zQllCgGs7mkPWh731Sjc4j5Qw5DdRud5WNI382rfNlFF4yK9Q+oC2ijUcOhr3NBc96qvfkjJKrI/kLTFBI9xY4Lq4X2f1dYDlZwu3DpFXV7Al45RoXqC0Lj7gB34+kiyWiVSZNMg2sOQ9H0z++snwJyxCD1v9HOnUqUd4Bu0hgXZqhguG2ofdowRLIIlnnQlirGQ7ykv24iUM9LzmHkdU4dBeGpbHLVLlg3Mbjpnaub5L3iwNEZuW63ET2oMaFsKp0pl54AweXt9ZabWv3GxKLqwa5DQQSpJQ5i5vpRnQ== 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=SqnrPC60/5qfumpuPx81lRT2beN/6mJbmAiGYVechxieZwjei99+3Kc6TkotVrlX5uhpekQptWRSVLJdACT8XnHta0B0emG1fPgX1gbDPC4xCLsVS7KfhYITNeuKjnhvJpAuKFOEiOV/fVAUTuLNrkx81mDXMuSTaiJgHECKn8qL/4Scu1oxjksrlLCjgUmDhbc6QoZzuf+grCLBbJOiWNn5BIzVggZdamg/GN8T8JaZaWq40yEMNCbL8B/Y8Vj/+5qFDo9NSA0c1L6EOl/hFA2lFbvCwoukMtO1tchmz4ERuEYscpMBkh/e64qe6UV8o8mj/lXlAWXqJW+ZDpXjMg== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by DB4PR03MB10105.eurprd03.prod.outlook.com (2603:10a6:10:3ff::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8093.27; Wed, 30 Oct 2024 00:15:13 +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; Wed, 30 Oct 2024 00:15:13 +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 v2 1/4] bpf/crib: Introduce task_file open-coded iterator kfuncs Date: Wed, 30 Oct 2024 00:14:54 +0000 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO4P265CA0238.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:350::11) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20241030001457.15593-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_|DB4PR03MB10105:EE_ X-MS-Office365-Filtering-Correlation-Id: 9f6151ab-0a31-495a-64f1-08dcf877ec35 X-Microsoft-Antispam: BCL:0;ARA:14566002|5072599009|19110799003|461199028|8060799006|15080799006|3412199025|440099028; X-Microsoft-Antispam-Message-Info: pHZCPUO2c3tzP3l+AFZQZ5H0Ney1/WXDPrsJe0zC3rlhVyBueGTkrbh3OKtG23C0s+xOh64PpgSR6NMF7nIdRdmLdP/t7TKHlFkvydulKopMmEwFLVpyvfNYJ5VJ4ntiYqCHE+MsdqOHY0kR+V9qXZ9Ink/i7ITKY+qrQjwO3LMcU52SYH/mY7CXUvWEpdeV73f3ZBFmedwnVnhykcd5NWVD06J5ydySx5fT6YJrBCvthnbwWS6bmTJvfuda6vKt0ayDKFtG58XK+buct/SiB0qOZ3/5Jp+SUMvLBBeQm8yedWC/0njZWmJGfPLwVDEemK4fWurIHbcIZJDApKLitpmRNRRspln/Iq105uJ7s8MTbECuF11h+QnLfbNrylJQQOw4wyxb2L3nSyvQ0sGbMP90ecTzH5WQxanAM3p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Sp3qPBKtCDGACMqek6EulA1ykrUN5UCqLhZw/e20EoAC0cATbT6sDtGcUOgueKmVzb9ZgAsFqajJ9xkhM3CfJU0Q/J/w5hBODAysZ9XK+c0Zwg/RmWBbXjA4xd2sonx4kbjAv0nbcuz+XeH58n9iDgtyzKwS52wPQNPjBdsTihz5BycwRt4p0kBKiy/Mi0+uujT11dZJ7SBHcj+o09KeYqw0nqqYVmv7QhLXwqou7tPo4aZ6+K0JCGMv4CICepZXzbYWyjMllwSbbK6utA2kh/2wnCZoJObQIIryHJvT+H7w/c5hZr9jD6wqXtZDGz0VbfLfkqRAGB2AjA7LqgQZZolp63/y2NOvI40QPONq9ZQ0oz6qpq/xYjGhgnpr0Q8OueUJyD0QrrovZK9cU+xJDR9dmALl/EH6WJnsZPyOK5gD5tWH2BEVMSoqK91eNWnu3woQPKB+a0cKA3AbCw4mBz9RSLXvp0c0E1iWbva/hKJDD1mo0P+cStbeagf42DL+5f/LoTasVqPUOF+AHASol2Jm7One5Q36YfBsk7VcgYkrUp8ZCDA72wbz2pSJ5p431PcmuZanTydKZBuSvCZv3SAI4TDcVbMyQK+y+GvULXDc2WJu9ZfJtoV32CuyVSVSZ2ckiHn4uPflDkPF9m20iUCmOh23H2xqWNBz/QP6J4QJrN7TJjb2iBhy58RVZLLIlYwdIGAHuad3Qx2VrDoXwhjhyhjx9RUiyGMc4imT/IOtYGzjWC8y5MxhbkI980u8jurZH0D8nITmrJ/wUZLVG5/pyd5dXfIdni3kP3pkjIuM4ER6b/3QoX3xRg4GtFVBtpbnFaaW1x+C6izL1HRZXtVCxWkvfaNoZiw9cZ0Ad++gt X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9f6151ab-0a31-495a-64f1-08dcf877ec35 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Oct 2024 00:15:12.9651 (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: DB4PR03MB10105 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 Wed Oct 30 00:14:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13855791 X-Patchwork-Delegate: bpf@iogearbox.net Received: from EUR02-VI1-obe.outbound.protection.outlook.com (mail-vi1eur02olkn2021.outbound.protection.outlook.com [40.92.48.21]) (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 79A40DDC3; Wed, 30 Oct 2024 00:16:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.48.21 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730247382; cv=fail; b=tXdP6Qyqdz/mPEJOJmAJDT87/RfW+H9oiRHH5DucsTzIUJ7Y+qOd3Z6sqjKWoFOedvH7AdQVjaaRuI/WXvujKCMSyZ5deWc63XCazx3zAEIdLH+xt17grCGYa+Fph1EtqVHUJ3C6Bq2l66V0y8+OZQR+WFZAxOTCpFDzAdq+PIk= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730247382; c=relaxed/simple; bh=TmVbmywAbjQE9qy324iDqjWn/KJz3A/Q109kDadncfw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=ZacCU8zkuoHIEh8Ys2G+9HsfQMOSWBVsL0w+Yy9I9H55Wnl8phJQTuyFLdrDQwgaF6bJdow/sm5hRE0VXR/usYUIdbmulyDNQMDzEJeuHEfaudmStgnzGWBskiTkV6RaCZd/bPJGqDmKLalibB16V0DSS5JRwgybOS+UGoD7wN4= 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=G1fiCqWa; arc=fail smtp.client-ip=40.92.48.21 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="G1fiCqWa" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=rulBd5rBRjWlzY8a/lXGrQvv7UjiXRh2zRxATRRYbeU5jbFuZT4vcBwrtu0Ylv491NN2/TPrbYnFT0DzHA63nSZ1bJqT8EjedeWMYiy8Q5ujWsF8+dPZfzo9fTyE+FKjnn5ks7ZPakHHmdNS1JDqoAwbHHF4NDKQOSj7nMjwiJ1MU2/M3CUDQe09NiAeBnpifUV7wuQu4TeisNnV68R9R1lm0R/7jRn7aek4WyRNMkwXoAxJowpzv2DEJ8twDWjd99YPuh6wIxpcARrqluZqecj6egPbNBHX7QdvrOXxDZGs/YLdd9RSNN+do6YX/Wi3J307UpmKneLRSmUR6sZuTg== 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=Gl/0AXJv0P1IHFM454sG2DzzbOkixvIxS1s+yxYV5cuL3MIt73XPcHH6Jbg1yHTDPTg2YvcAaV29G1hgCzJOqaisaA0Oz8V6FXx+J88zfff0edtsYYtppg9C/zJdUM/R7ybMlMwWfGF2SMtvJVyxZv/2HMwBYgjl283XR0sOj8JKULBbhSOwszO3R9J3FYDWYW79tg/fdWHrM+Jj/3qEbwCIDNmQb0ZqxC+bshtBgYiSsHCQqCTnzJPsr19pJS348PhfEGa652mqeqVbZAiob2AH/5kmtlhiFnSU0yLF4KdgxBA/aDTHvxGhvuYVTvREedaq5vG2B3KWkFeVrXe3cw== 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=G1fiCqWaUYgNK77lBpKnfzn5KxFNH7soZ9pqXGZZ9QEb+Q+1+orPSGrqaqjOl3rIvYZfILplc5pqLfPzBxWWACAMto/4DL9uqco+/IklUmDHWzuZ4OPmCt4Z0pPa85dmBxIPEX+RfzUni1LwBF5yG587E/CK3zit1k5SiV7ZYwGV3cng1gYBeL0lhP9OOr4o/yOTl6lUe5ktWMdl1iQPd0Xgz3Ku9gFoHTe2IMlgVl6WVvLrdObhIj1b8DOjSRk6dAntZtRp2D+FgUzurbEZWWcRqH23LcsU73U0mzrLa6Hn9nWNwZROVSalKQkjepxKI0RSTD7BVMlpFMHCXDABFw== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by DB4PR03MB10105.eurprd03.prod.outlook.com (2603:10a6:10:3ff::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8093.27; Wed, 30 Oct 2024 00:16:13 +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; Wed, 30 Oct 2024 00:16:13 +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 v2 2/4] selftests/bpf: Add tests for open-coded style process file iterator Date: Wed, 30 Oct 2024 00:14:55 +0000 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO4P265CA0238.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:350::11) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20241030001457.15593-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_|DB4PR03MB10105:EE_ X-MS-Office365-Filtering-Correlation-Id: f4b74f99-48d9-4292-2828-08dcf878106d X-Microsoft-Antispam: BCL:0;ARA:14566002|5072599009|19110799003|461199028|8060799006|15080799006|3412199025|440099028; X-Microsoft-Antispam-Message-Info: DYam3ZJBXWJmWtkBhcJwA0ANtxsMfcCJckpLWDoqGGuKw5ALSu57hQda8Xerr5ZQjDdZT+RbZwD473/UrTHERF5JF+UqC5rsQVckxDxXsCtHWVg+YF2vXWJuoIb9V/vYcK0Ej5+65l9WBjuciHhwrXuSthJYwVc9p9SatnYr7ceGpOkStD0AZGebtPD4r2GZUkW/yBTqwwRTU8SJsI0j7Xe9icPdumo10arm6ya7h9czTSN84LEZVroeYJm3mr9mODKkpbzGkh9d96CaKsrJ35nIIW0hPMaVdWGw6/Ra1rN9NlITpjlL8dtcg+uL/wPALt7pPspSXdoN/CCLym+303TvbC9YWlG0tvunadkwpIJUuOUOcgdJpnHcX5bDG8McUn/IuD15pQA/cwES8uJ6sJsejHQAusaBV6A3kQRKarFjiXVcZtFnouUvLFDM/2E3LhG7NGJk62vYEmQ0WTWxQqhLAiRvfnjDziWHj7m2o3ijp1NP2MIO5yI5WkfJMF94V0jL38daiPj+xg/hgv1XkVjo5V5PEa+4x2KP7iqfe/nQfslasa40PiBy8NVbGb0WUD5gaKc/hTzOcW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Zj2L62fTSvUPyEowVFAT2iBmnbmvKNnQGAXqJ2b6t26iwnacJceYpUlvbcdwPjMwZCKxYJEW2fjCtiDVa6iD8+k2AB580TZGEyUIym0I6eooA1K7uBgr8YeLlJyQsh84EXn36h4OK3Tx2wv8CZ3+unyWaag2A50yuKnV67BzEt9YqqVbjPYJB4MVlDM3CLaDHEdtbSQS/bt99YoNW3siRZjq7ybx7iYTFHWI+THmAhpj+5bE6tVhoSY0tQRq+5xX7+5NX4JIQdOl7c/0AF0rrxbxKpP+oaIL5TIHYj/gMqxPySpvVpusof71W7oTSSVbSTPuApIGS9jNUeNgA33ylP0X1k+BiNW9IjfzpsY5eS3wy1LJAJOeQyc7xPJEiQOTacet7UKlMjXa9iG9qc6UhFv6Krno0C47bWm26yUQj7VPJ2sxhCMWAM+cm8T5OHV8Q2svWVlTSUIgg39TclLntB/tI/7X+G5cWrQByp5J7MfhPAq1abZjhPactMpeOZGhotzbyXmGoyHKW4sMCztmVF6L4xh+52jEM0XsYOkQU6Mok1pCm9Cxxt98TBxpkDNhHbcmzbCe3pXQjS1c8+aSpyCWg5N/y3SNdDLpRLBOjroQzMN+T1mZKBCjwg7/rTwbCaOsLAE66Y0ZI56dKD8UjNM7L16s+nYtLQYPPsVzkI30IoJ5xGntDTNwtL1Ur+YhWT+xwpji X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: f4b74f99-48d9-4292-2828-08dcf878106d X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Oct 2024 00:16:13.7312 (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: DB4PR03MB10105 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 Wed Oct 30 00:14:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13855792 X-Patchwork-Delegate: bpf@iogearbox.net Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05olkn2011.outbound.protection.outlook.com [40.92.89.11]) (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 3E9B710940; Wed, 30 Oct 2024 00:17:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.89.11 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730247452; cv=fail; b=qyxN3YejDqYW0s6M6mrEmYkO/bOYAcgw92dpde6Kl4Kf6mOPU7JKHnUp6xIwR5MJuXvis+hlq86wdvnb+wthdZiiwRVQI7rQub3WPskambuHjOwQCMWglZE98jLupPHRVVOCNL9S54UDZg0MG3ViEOx6h0fJ7B5NM5DQVcEgUjQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730247452; c=relaxed/simple; bh=dx0ybxqUrlzgqwmQ0ztItlfqUEB1SRgoqUxNidJDbjw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=SqI4pwVUAGj2jsYmHDyDvOEqJaOcu5EG8Q7bhANKfpCveNFCiQDn3ygNvFQOiPBRHpB39SMLRMJUecTh3foUgyXgJ2B0L/K/ecF+vSsGA1gcIqmuPj/Oq4VJER+HN+19CA25uppvO6UMiC+eFb2w8zFyRWBF3XqMWl0PK5jWbig= 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=r/+xDNwz; arc=fail smtp.client-ip=40.92.89.11 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="r/+xDNwz" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=k1a4JXEjpK0sEb0rxiDzpVorAx4o6fJCH21ThKtE/BVEIXcWaQKtAigeOFjIK7/SJ3utn+GBp5hdrSBvuKm/jXs3jBzRIbT6WqqEj555jQsJmva+hE5/mRIFGI9hvnFg0tbdbPxS4wM+LPICS7wQibwxZzVtkO/JX8OkyZcAjLKErvSLI9gC9FHuUYcpjCjwR0U9mz8uITuNNJo3pKpXzqWDmKKGCDPN9mdmdzIYB7tVM9k93MPSh5vw2U3u3r+/TdpivmPU5av7+8Q95oJGKTXXOQg3sUl1PiZzSOe8T98/4NLNYx23ce32+SstOn4ceTNBCgTv2gEKq09brejqPQ== 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=lfOQVgooXnw/d8k2xM4aZv6XTNntg44DWfSndPieEARYnzF03MD6rb+i2LutNLtYj8MkGtASsT5vqTR2BjXyCudpj0cSguMZZ0wTA+/Pr15M4E9tAStZGFZLZcz8EXAvNwLfz1o/h5SE3w3kqA8do0/NJEW1A/cVu0OtfloyoITglI4IYkzLWpgkreWJc41s6NkxMIBc08a+74gxAlxbJCogdILvaDuHOIq5Jorj9okMMhEVOiVeExAbMKfvZ5GoBbv5m/ENLinTBTDdLtgRjcwZOfXFPWai+Rd84VT22/X08+pbtUoiZHmkKqTdi/BlWtMfXHcsGH/d+SOV8kIuNQ== 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=r/+xDNwzq/sULbYYrSE1XiuUbsv1E18J8H8jjLjTFIjBa6+eeUrIuwSBHgCBqth4zy5hk3EIi6G3aT2yUC/ZcT8xg10hAGYxx497xHufI6IjWjh6rV+Jz/576KxyOLkvlJombruynE60pG4WH+72jctBsKmsKEBfTGZZkvt1sv4zJEZYGikHmpoR1NtfJ5W+gQVcpYNNEB1+3kHfB49zi6TKnlT+nEinVMLbISlSi0MSICK45ZZojo76BGbtl0rqR/+zOF/InG4WBuNDu/9BqnUZSS0VGnvpLQlizs9xXMj/72GYboFzZjp4xrw+M8WJpjL3owjXOk4204BEyV1tTQ== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by DB4PR03MB10105.eurprd03.prod.outlook.com (2603:10a6:10:3ff::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8093.27; Wed, 30 Oct 2024 00:17:27 +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; Wed, 30 Oct 2024 00:17:27 +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 v2 3/4] bpf/crib: Add struct file related CRIB kfuncs Date: Wed, 30 Oct 2024 00:14:56 +0000 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO4P265CA0238.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:350::11) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20241030001457.15593-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_|DB4PR03MB10105:EE_ X-MS-Office365-Filtering-Correlation-Id: 83d77c21-a597-420f-2df3-08dcf8783c16 X-Microsoft-Antispam: BCL:0;ARA:14566002|5072599009|19110799003|461199028|8060799006|15080799006|3412199025|440099028; X-Microsoft-Antispam-Message-Info: otqvHMVD/i6h1VQnpGHVMqjMSgCXg56Tm8kd/oF8OugBCqbMAnw3hQ7nC0XXBbFMEWjLUV6mqng6IFHpfc0sYCrOxTpZfo1/RcYr1DUacjmOAllyrEvld+W4veFZRIVPw2DMZqCD0pj/HNgcs1AXOPx+mmR0StdbY9cIKQDe4sTolmptkowpk5W3co8uX2UgdBzySzstIhbeRkJlSytspfzKZDp0z6zeqzf0RAwVt4e0rGp5r4aYwowqZVCgdFdDnn9bDMqznznJIq4Vn+7pJtDGcT1k4KY3EBOkFfD3hMVhScqQzHO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ossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Oct 2024 00:17:27.4086 (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: DB4PR03MB10105 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 Wed Oct 30 00:14:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13855794 X-Patchwork-Delegate: bpf@iogearbox.net Received: from EUR02-AM0-obe.outbound.protection.outlook.com (mail-am0eur02olkn2020.outbound.protection.outlook.com [40.92.49.20]) (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 9A26264A; Wed, 30 Oct 2024 00:18:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.49.20 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730247486; cv=fail; b=iAwZW1OZpAkaDIXnMX/ivtsJ1DcoCCYgjVTe4YH2krtWqE7HOXNrGjPU++Fnm0hGnJtliNjAcr10apG91BjxLYc6fPjx0mCpAFITvVGolbiccc0z7lK0Fy31ZsR1ucGZExQafMVgNYpx8ge/qBYo3pXwXBCEtdli1RLmxIIIJVM= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730247486; c=relaxed/simple; bh=gZ4ZsfrXjLE/euxDXbRqSbrJN/oeswBzWeE0ZYDKq9c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=qN8AftBM7WS+012z6tE1wFVLVEJuMaWVt4CvzZ6dyeYnU3NSwHxf620Rs9S6IUnRh3/PdCoDwDDeuPBOa96zIl3p+R/ozKBwBp1PDxkn+anC93YwEakqwy7maq9W2mbToG+LNEQdKztIHKqZSD8mwjKty7jfQpa6+QQiZPbc6Ok= 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=esxPko0r; arc=fail smtp.client-ip=40.92.49.20 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="esxPko0r" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=eJsKc+uVQ+NdOt+xBVfEgEmURg8fNbVGiiHaokv3/sMZB/nGPL+/unb0ZP1RndQVcxPnRvpD/rtrkMdQTnEkB+GsKEgUWQkIOneeYRq/UAMSb+tfhxDaBIjHaju2rrcFF5+PStd7+HyiorYhaTpFXJ6Pyzp9Ii7txEYBWoHFvIbIFlSlSCAwivD7ll6faG0K0VMjq7cvc2HfDDLPciB9dcwBc/0xhXUjnnrvd3Wd7ceQO4gJdJpr6KTv3zikRQ337gd0PFZMljyoogKBE+hT+XSHEqezFbobm4JJhMK60PwTQEdARzDAuG+HBKuhUmbtR3P1ixX7BSeQrbNjdP9bJA== 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=fqMLYqY7WZmPsDuyp6cFamd6LLYvH1cd3WsR6pnyRM8=; b=X9pnpVo5XA+C3RPxD8es4VC+7nM3DIOZCHi3TL9x0D41QjpCTmYzm46gWu4ER0I6dGy695DdF2yie11h18TOhQXQG3jq9ebSAmkIRA6IYjXAaodNB41tdRk2JLJm0lVlJ4+H0UyEsDe4nqeaLa2x1S7qALMULNhaS7zNOk9/dvMmKbNW+v286+t3w1KjwcQpeN+1rcqdydHi+IUK/3uacpWHdKOuobF/O3EY1P555Fb6UdGFjPkBKxeQ6dQwaf7yidNi9I3xYiwS5DzU7Mu0B/TOk/vSxIOCudwPMedRQoXQ4PWHfZa15FOsF8FshYmqf3YykROlAN5pjNZuaUiHkw== 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=fqMLYqY7WZmPsDuyp6cFamd6LLYvH1cd3WsR6pnyRM8=; b=esxPko0rNyAPSaEUKAl0FRkpx1zTDKcFEs+QdvDvlX7h4WgCYBbDQYDC6I9gzccshg3L3JKOtZla7IXQ1uVTIx2xd3C0HPuYxDZ3sjoDyB3kc2ZWPGflAco7LQuvUE7cTOo7F0/UIShZ++fmDuPbY2rqRTc05jAV7w8XYmQ4Rjt+R/1o0+EBu+7VoUi7hE2AleIbPG+ybZ0aBzVjfv4eAvVKEt5Et/UD9i2nFu7hDeJjKhZvuesDLNdG0rf/RN1Id730TbzYCWIK3avNpqdcr6gKMfch4/Bv7Mc+O2a+Z8iUcZDtixMVXnUKkJhJ2QjAAOHTG4u8K3429QIuQo+OcQ== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by DB4PR03MB10105.eurprd03.prod.outlook.com (2603:10a6:10:3ff::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8093.27; Wed, 30 Oct 2024 00:18:01 +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; Wed, 30 Oct 2024 00:18:01 +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 v2 4/4] selftests/bpf: Add tests for struct file related CRIB kfuncs Date: Wed, 30 Oct 2024 00:14:57 +0000 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO4P265CA0238.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:350::11) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20241030001457.15593-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_|DB4PR03MB10105:EE_ X-MS-Office365-Filtering-Correlation-Id: e28593f2-66e3-488a-9c45-08dcf87850a8 X-Microsoft-Antispam: BCL:0;ARA:14566002|5072599009|19110799003|461199028|8060799006|15080799006|3412199025|440099028; X-Microsoft-Antispam-Message-Info: rE13zP6L6sZws2d2GCHrna2V7LtIP3obH71GLAxBSCmUmIDlLk2IYX1TMZIkaemIquGAuLT3a8FbMFo37s94Uhvkn/YJyQs8J12bbK348D71l4UixoC1vAljClzdV8VWtLRqreDT8QY1jwe57Y3uZA74XqMHC1MG5aqg6ITR8Z0OmMyURKF8/cKL9tBGJfUzncZjRsPeaqf3/xho6g3RnNSNM4dODPUrN/ecjHahxkRiwYPF8V04fc4CONvM6cR/Sz9+R//hn2xMgHBBA2F0slc9IJdL5RyKMfx54hXILQriRil5ey+rr/a8T1M2ghbChLXP4SDCaYDxhMGJ8hEwKY0JoET0f687UPr8pWmj0MI/Ge1whhcYI2e/4+L5HMcX5Q0f+ZVIgtfcwdRmxt+4hQ1ObFSdh9/e0xKL3NaNts+SOX4KSgQt5A3bMnFM6Hq2d06PU/gNOJFLy5ORuir/18Xx8+UySmHG0L7GcVm1+qj3vVRkMB4xxEwpQe9RuD07OG6c/Ddw7Jp4mZ+Mrm+zX8GkuZsRzGw21sySSfTyLqwAUM4yg3DMklku6vEgr6dxcAooY3UeUGbWR1EJ0jWfHgeQUeC8hMsfgfTFc8UIbnBA5hL4mSnfguMVHzOHtLsQiQMoLe7QrL9GnCKk0bPfzjjFrKi9vp1Vkj71gw3H1urVfY14kYO3GofOwxS8wEi83C4sjTbOFimaqWhwMzA7SWBcSuMxyWJr2J9TpcnFd5N/OEbjIjmd5Y3oK2gW6IOtjLizDQ6XLRFDrOF9x7Hp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vcyg8LPYw6Z4sTcQN2tNRodZkCwKekI6jMA/GzjSAQ5vZs8rmGATTk1Z9huGwHXX0p0qTt+50TaP5HDFsVRwWj/3pq+PQCAWyQlNtT4/WWDxWxhB8zsfcOJQEIbZkBRrH01+ba/TZyUYDd6MsxgKN7YvQHhpEq/BL0RRdRhBDM1dXZldMSJop6LySIu5ZcKicdIi9YCfQo+VQWvFnbegddphItU55sa6YRGfRAVzrTTkrIwHf/JmB8g4bLJ+iGC0BCkeCT1xV32jXJc0LWbHsJF1O5wpEv1H0U3hTXYw+FZY5f+V2ZCY7b213XP8OYS0i4LwzWEO2ueo2qaQLJONUf0/VeesUaSKFCfSLiNsDuBqTFpLM8Gl8BzCsRpqbyiPuIvI/bVPl1iYtze2RLDSe6wKLmggc0TxOz88w7sYgzVLhf7xohyl26cUi0CDuyxyQzwmwPgf8uwhaqi37Lg+s5a4F35Y2IR6eV5jsA8bL4VvAdsH/pezHrxYxjVpILDG9N7i0Gay7u X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: e28593f2-66e3-488a-9c45-08dcf87850a8 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Oct 2024 00:18:01.4801 (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: DB4PR03MB10105 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..0bc77d1b02b2 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, unsigned 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; +}