From patchwork Wed Nov 6 19:38:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13865499 Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05olkn2107.outbound.protection.outlook.com [40.92.89.107]) (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 0C2DD209F5F; Wed, 6 Nov 2024 19:39:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.89.107 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730921974; cv=fail; b=ZVaKs7w+RHQ0AVhVWiy6qoSjmUPqRtpoz/ZAnvV/t47Ej/8QIa9G2pJ+AaT8ID46rMEXfTYscz3jr32Ex4d65OeInI5CTI7veM01b65kEgAQYhTvYgVps6mlDl3aFBnyRqLcGM5kQO+WabpbRhGERma4O2q551EMEK+4GrOCl84= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730921974; c=relaxed/simple; bh=imCKjTt+FjZs71vA+1DWP4iRwmafPtQ7z6q2v3riIQE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=HlBEds+6pQ3pPgvx3RSwbKaZpThEiyU79Jj2HMM9gYp4mOS23AzHh7nI2p+wyowej+A/oL8AIF1l9rvMr0iyjb8+nspyPki3AosdYkx9Bcm/JhycJ7sRapkvcFmhleqB6IYGJv9gQuA5PNjsXrfJoZB4Y0hSxOgOyz65LBCmC/0= 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=EzGwyc0N; arc=fail smtp.client-ip=40.92.89.107 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="EzGwyc0N" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Jk+38LfN0uHu6852d6eFrETgibUgKRii2N+ZqYrsNKY1DqVwiVyxqk2GKFTsCZE9lvV+PWrV+keWQ3eBY9LxCB5VeoytoKZatL5x3uOXCZKb4HT2Lqv3XRB27BFg5DFJjMyoZW+NR3X0ROoNZMJBmz/QiwpE/lkLiD2Waub9nc+TEer15ah0cTtQi1PRyzuED9po3oAat9MzyrFM9a/qRNlt51Moi29c3GrEF/0uOjV3/3Ip2c5FOfW4CIvKAtSG6NJw17IjTOqTMxLOO3zat9NMM1Ac5UK8S4vxIGLLeSnZ6ym3qSBhj3reCTCVCZ92UoXe2lRy2AdGPFNPYGw8Qw== 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=P96QpylSM/F4xNLnWHy7URkJkQ9HGs/KE5ZWx90fjjQ=; b=nFs7f7SNRKt4kFwyk6h7o6hfr/6l+o5GabOQQrhpoe2ZiX8vNbote4y6dGMG/JROi3T+btUsomojeQZdV/iBEPq9gc1WQNfnoTrpZ+2nMm+JZ7aXujrFivFyOrQ+PRoq9z1KfduDXw4+SFKdUeO6ExTIyt0vQaNijFmDBoYTj0HYPjMPF64o+el5fybQ1XINy1GYp1fkkWxO9YPmCPUJ//GH4JUpXZmpuRooB1hKEZdA5Q9XUDJhPuTY4RI7BoVJrvph8mSkHz7pwtqm/e47ANMZTTMCDXtYSVwantwEZpE7hi8Ykgn42luiPM1s1w+9yygGlBgfzPCLTY9K3gQNfA== 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=P96QpylSM/F4xNLnWHy7URkJkQ9HGs/KE5ZWx90fjjQ=; b=EzGwyc0NYPIq7MvfhOHwN8Ht8/jrNyRl+u4K8Qy6AabSHTVxXTAand9efKg5oSqCXsG3cQ+tq7mNKxYfOr0CyNh6RY9SdD+z8ql/4GaOrrHLl11GBVI/zO9ApUSFaIkurZp53x+AIUiIUzy0jwqQiHn+qapK4U61wQX28y4u/OwWZb3eFHUFdB87PVng5wucPC0dShOutVkJG/IlEuu0iArKDplGfpZJSnkSR8T8eeqZD3KKoOmRNiNz6U+4lYNGPWSGAaHm+ZZ3pd9F08GvEz4MN0ejXT8qgRl2am5iUy+DN77C5ZEzRlUlQFAzebU/YaHu3wBL2MZW5OUmeHaXDw== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by AS2PR03MB9792.eurprd03.prod.outlook.com (2603:10a6:20b:608::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8137.18; Wed, 6 Nov 2024 19:39:28 +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, 6 Nov 2024 19:39:28 +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, brauner@kernel.org Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH bpf-next v3 1/4] bpf/crib: Introduce task_file open-coded iterator kfuncs Date: Wed, 6 Nov 2024 19:38:45 +0000 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO4P265CA0280.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:37a::17) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20241106193848.160447-1-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: linux-fsdevel@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_|AS2PR03MB9792:EE_ X-MS-Office365-Filtering-Correlation-Id: cb6fa12a-530c-48c8-2e90-08dcfe9aba1a X-Microsoft-Antispam: BCL:0;ARA:14566002|461199028|15080799006|19110799003|5072599009|8060799006|3412199025|440099028; X-Microsoft-Antispam-Message-Info: es8Kqm/eKjHZ6UQAoQoE9FZcNxAbXaRL6V+U2sZ1FEVRnhWhxiL7Bx0sS3xoauVL3ZNAo/dgwgCTFi9xdR56iStz9DRjaMAYQiJKxfIy2nHYLBbkJi2YimSmX/N/90WemFQu9Wp+2Ouqdu0LNJ2ZIZthQKP/zGRwPfcYOuuFZx4BYgZpTPD9mh6jhWXATE/MBjm0J2h6YUTKvru7mcZ2jr4EfhZJOs8P5LgDAwzKtVOl/vr+7q5X0UUES6rk4ogarw3wEkRa61YbbTbyXpUQTrboWCFIDr2mfEj5p4NuVmzfPMTw2pVEvcTPI+N3EjB0A6Zt1UP91jZfyGkROZem68+lZ13qW3hByIKLDG4WqzDt6W0dCiFB4qxZJWbLTCKxhU0/omxF6MI5gQ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cVFUZ7rwlTVqgAP1eFCwHaHMTNpGC/MVHxicu3BMkOUQ4SJGdK+GTVOiGKTYAvTZo6IsfXFUC2pInNCVK7uvI9J8pcHKdjrdMqJ1Nw+7OilO1inBjldKi2zc6DhLQMxamhRAezUWoUhtP6FjpAnZuzmq4zGzWol9CQ5Yo30d6AK27RPo5CuVPjkKgh3Mrx/I2rqoiEHaOvsAlmFzMl354i2KmobUdfOeVAq8a3tNnyidu/dRKOoCgrjmJVsrKH055sQaAfRxu3HzE/irzrbCq25VUsElIK1phWq7TUzCbGQx36YLd6WhhgxbcTiqYFKxXbmd10shJR7GyGF+GT/JK1Sim8eW8FN5C+LBVcnf/mupnvrKjaZ0vzfnWwksvoduC1MCFnKewEZ/83Q+YtrAejyd5t2VbPQlrF4aQUTLzsyx4wiJzWQAWQNWahCaW6Wr3Pcy0mQesA5rwhCOriwB3ezN+DUZLBcwbnS02bUkNELAQBhiWErp0aofqIsTJh8d8eo6GF/hI8OXpiVZk/5WAQ0NY/FtsETQ6rflKJW4T7sA24z/jU8ljPY64NhoxZtte1d2mm2P8O7eTzJHfME0nsQP36DsPa0f3vm3bW4sZapebPdszwdfhL8c7lDI3bSmFFQHCwFf/toNZ5J+WijwBr7f56Wcx/KZh1N4lF3Y+PEpbd4MgBPoX8pO0QiFIyrGvxfL6oBHQ9xcHckBRi4+wbkwbxVHEzuDb7Yre34MQL4UblWvKvrgV6ZNl8iIinCZgDhbUW8EDSt7WkToRx7uJ5i1nUGcuDLUOOcQEynDf1eC6cimYkunGi5WuhPtS9wqL4ui42lbixM3nuRnFHrBg4YbbfnyWW/8eDWPD3g5AcCvh+ad1J5ps+CF42NNs9vK9WqYs9o X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: cb6fa12a-530c-48c8-2e90-08dcfe9aba1a X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Nov 2024 19:39:28.2714 (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: AS2PR03MB9792 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/helpers.c | 4 ++ kernel/bpf/task_iter.c | 96 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 395221e53832..1f0f7ca1c47a 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -3096,6 +3096,10 @@ BTF_ID_FLAGS(func, bpf_iter_css_destroy, KF_ITER_DESTROY) BTF_ID_FLAGS(func, bpf_iter_task_new, KF_ITER_NEW | KF_TRUSTED_ARGS | KF_RCU_PROTECTED) BTF_ID_FLAGS(func, bpf_iter_task_next, KF_ITER_NEXT | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_iter_task_destroy, KF_ITER_DESTROY) +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_ID_FLAGS(func, bpf_dynptr_adjust) BTF_ID_FLAGS(func, bpf_dynptr_is_null) BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly) diff --git a/kernel/bpf/task_iter.c b/kernel/bpf/task_iter.c index 5af9e130e500..32e15403a5a6 100644 --- a/kernel/bpf/task_iter.c +++ b/kernel/bpf/task_iter.c @@ -1031,6 +1031,102 @@ __bpf_kfunc void bpf_iter_task_destroy(struct bpf_iter_task *it) { } +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_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. If -1 is returned, it means the iteration + * has not started yet. + */ +__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(); DEFINE_PER_CPU(struct mmap_unlock_irq_work, mmap_unlock_work);