From patchwork Mon Jan 27 23:46:50 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13951905 Received: from PA4PR04CU001.outbound.protection.outlook.com (mail-francecentralazolkn19013076.outbound.protection.outlook.com [52.103.46.76]) (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 045D41DED76; Mon, 27 Jan 2025 23:48:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.46.76 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738021693; cv=fail; b=dpfkAK0FYZ5FUAHmvDNwF3GI3EKg6uyOAYxHHeF9IRvmXy9EkVxpUfomS3ajIdTpKuogvIsHogWRteiKD6xpVGiYNy960miO6+UP4WF0JmWjLqpbUUk6bDCQVRDgIOIHeVkYBV9M/zAdIytCRcWfN4pq3LBNw5qHzlLnhBYqLrw= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738021693; c=relaxed/simple; bh=pugAuILa5hScos3sY9rnqD32OV7A5ZwmKpNU3EOmSf8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=fCaJOTapyh+CaQHjI03/3QsovzsnLpjbcsaxHihpdeNPYS50ZRLFAlyyAfU1EfaWqLRbRWyn7JvcM+cR8HjjFsr60F5VTMn1oIP7ImTKW2LoM0rTRniAgsagC62F05Kyj1Gey3D3LilvZ9PBtnykPvV0CNnqBSA+kCz5qodAdB0= 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=dHAHNWWE; arc=fail smtp.client-ip=52.103.46.76 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="dHAHNWWE" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=kaUAq9iNfP3obWHCN/3c0s1NOqI4bwQDJsvf+2oO0CBmFll4jJFYXOhiRye5WpgNcYaodWhgU1KaH0LCM2w91QW8EI3WO5uN/ynz+OmVjwrtXz9Uu5v8kyWth1dw2keyalVWKfNqT+J23vWj8gg7SAzMKFjnOJENNHKL4QmA60NZIYWqUr2YctP8lE5U9M+DQm7GV8MOFnKT/sXtFSKpJD+M+9yAjEPDBlVbA3My2DMQ884r+MVeyctyYDujaryEARCuWPk2iIF6u3dOrYXP0BBWujhTiHxqKiA2zKCqN5bbTTW9heUBm2d/YUdk8yifTABMQjvnsrcLinW+XT9bzg== 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=gAleI/7pahHtOzi4/CGD6R44dIvtsL6dRYEC4VIDUCs=; b=cEQpGMV0mJBpS6MMtjEcFNDdddVVSwqkrQpImteKBjMnpfssvPzb4D0VD9+lXRSKK4/Ygxjh7z4hnCsJL3mGr59Q9fCElwlnp/8r6d8r7aB6AHEEeC1sLHqNkXnQ5Tfx52weA3ZUH7TpiaJzKRGtTLO22hAEcVl/9DtAW8S7sIhAi/scwBmaKD5v/eJyKir42SjgIFK7Xug6NjzMp5hCJ1LHKCYUaMYIPZSQWaqa/i3zYZnFCbK/vHsK7sIbeE2z5ZYJePopjKjQqG2ad5SBa68EyC6E5YiRdWHpOcjLtgJzywT3KmdMhJVqQQyYDH6RWGHMxqdhpkDw0dbkkVhGGQ== 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=gAleI/7pahHtOzi4/CGD6R44dIvtsL6dRYEC4VIDUCs=; b=dHAHNWWEGPvjUfRBx8RBym/ABlnvgsTRHqsYF1UAJemXa1OyZC9JeMjbZfHsHZTa7R5MIyrqW1+3KXmj9ozAqxMPa6JQIFa5gzhfzlJdP6Ay9z0NSKa2/y2dVaSgg8PDJzBydZXiYVJnsYV5tyjNt6zQlU1RI3c7lEx+qWQPXAg6oiOe9xrCrsQqc/r5XqE5Yq6GlQgxEZcehMXlhdHt2L+gcHlsUE88n3xoxNHknloJo91zm79aIQ2NCZ3E2ATDj8r8YmSkyOWefqOdzqWYZ7TAXjiQkqzsTQ1yGQ5nmtA5kl6Ef/fCcOt15LBavfQa025/1m7DLUYMjBQJ6AJS7g== Received: from AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) by AM0PR03MB6209.eurprd03.prod.outlook.com (2603:10a6:20b:156::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8377.22; Mon, 27 Jan 2025 23:48:07 +0000 Received: from AM6PR03MB5080.eurprd03.prod.outlook.com ([fe80::a16:9eb8:6868:f6d8]) by AM6PR03MB5080.eurprd03.prod.outlook.com ([fe80::a16:9eb8:6868:f6d8%3]) with mapi id 15.20.8377.021; Mon, 27 Jan 2025 23:48:07 +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 v9 1/5] bpf: Introduce task_file open-coded iterator kfuncs Date: Mon, 27 Jan 2025 23:46:50 +0000 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO3P265CA0018.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:bb::23) To AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) X-Microsoft-Original-Message-ID: <20250127234654.89332-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: AM6PR03MB5080:EE_|AM0PR03MB6209:EE_ X-MS-Office365-Filtering-Correlation-Id: 23750658-87c6-4e04-5c9f-08dd3f2d0ca6 X-Microsoft-Antispam: BCL:0;ARA:14566002|8060799006|19110799003|5072599009|15080799006|461199028|440099028|3412199025|41001999003; X-Microsoft-Antispam-Message-Info: jhxSo9XNohlSb3Ao0cIJ4iugbC5hzL8kzmjSlSeox3Udn9Wt3GpHpsHdXfT/A8x6fuUXjRKgXlhca2xR0p2qzAR5LHDfFA+k02bWRXhdIBYmTPjFGJgP1xCHmC40TkQZbg/HQuTDR4yrnCQmDIC55iVersDZ6IJP8KSsw4zma2/u0EohFGRUMxY8uzfugx6rApA7esm6c0CLxVvhW6rD25+0qwNlJjPm0zOOFPtO/QumXgkMZdJgLhc23dQ0xtakgm+5D9UiYj6oaZJPDe0oFizD95Nse3pB2dm0IjvhKBzIWjpVVMxToL/ZFHEbo8Hpzm1wjb+nfWNYtexTi8mmjFCs9yYBcKoz5W9OdUBzriiGF2InyTUw9Ne/D20IoIXGY9gqYaO+gj71QE/TzZ32nnTbs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R03MB5080.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Jan 2025 23:48:07.6929 (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: AM0PR03MB6209 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. bpf_iter_task_file_next returns a pointer to bpf_iter_task_file_item, which currently contains *task, *file, fd. This is an extensible structure that enables compatibility with different versions through CO-RE. 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 | 3 ++ kernel/bpf/task_iter.c | 90 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index f27ce162427a..359c5bbf4814 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -3157,6 +3157,9 @@ 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_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 98d9b4c0daff..24a5af67e6c8 100644 --- a/kernel/bpf/task_iter.c +++ b/kernel/bpf/task_iter.c @@ -1027,6 +1027,96 @@ __bpf_kfunc void bpf_iter_task_destroy(struct bpf_iter_task *it) { } +struct bpf_iter_task_file_item { + struct task_struct *task; + struct file *file; + unsigned int fd; +}; + +struct bpf_iter_task_file { + __u64 __opaque[4]; +} __aligned(8); + +struct bpf_iter_task_file_kern { + struct bpf_iter_task_file_item item; + unsigned int next_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 the 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; + struct bpf_iter_task_file_item *item = &kit->item; + + 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)); + + item->task = get_task_struct(task); + item->file = NULL; + item->fd = 0; + kit->next_fd = 0; + + 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 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 bpf_iter_task_file_item + */ +__bpf_kfunc struct bpf_iter_task_file_item *bpf_iter_task_file_next(struct bpf_iter_task_file *it) +{ + struct bpf_iter_task_file_kern *kit = (void *)it; + struct bpf_iter_task_file_item *item = &kit->item; + + if (item->file) + fput(item->file); + + item->file = fget_task_next(item->task, &kit->next_fd); + if (!item->file) + return NULL; + + item->fd = kit->next_fd; + kit->next_fd++; + + return item; +} + +/** + * 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; + struct bpf_iter_task_file_item *item = &kit->item; + + if (item->file) + fput(item->file); + + put_task_struct(item->task); +} + __bpf_kfunc_end_defs(); DEFINE_PER_CPU(struct mmap_unlock_irq_work, mmap_unlock_work);