162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci#include <linux/net.h>
462306a36Sopenharmony_ci#include <linux/uio.h>
562306a36Sopenharmony_ci#include <net/sock.h>
662306a36Sopenharmony_ci#include <linux/nospec.h>
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include "rsrc.h"
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#define IO_NOTIF_UBUF_FLAGS	(SKBFL_ZEROCOPY_FRAG | SKBFL_DONT_ORPHAN)
1162306a36Sopenharmony_ci#define IO_NOTIF_SPLICE_BATCH	32
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_cistruct io_notif_data {
1462306a36Sopenharmony_ci	struct file		*file;
1562306a36Sopenharmony_ci	struct ubuf_info	uarg;
1662306a36Sopenharmony_ci	unsigned long		account_pages;
1762306a36Sopenharmony_ci	bool			zc_report;
1862306a36Sopenharmony_ci	bool			zc_used;
1962306a36Sopenharmony_ci	bool			zc_copied;
2062306a36Sopenharmony_ci};
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_cistruct io_kiocb *io_alloc_notif(struct io_ring_ctx *ctx);
2362306a36Sopenharmony_civoid io_notif_set_extended(struct io_kiocb *notif);
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_cistatic inline struct io_notif_data *io_notif_to_data(struct io_kiocb *notif)
2662306a36Sopenharmony_ci{
2762306a36Sopenharmony_ci	return io_kiocb_to_cmd(notif, struct io_notif_data);
2862306a36Sopenharmony_ci}
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_cistatic inline void io_notif_flush(struct io_kiocb *notif)
3162306a36Sopenharmony_ci	__must_hold(&notif->ctx->uring_lock)
3262306a36Sopenharmony_ci{
3362306a36Sopenharmony_ci	struct io_notif_data *nd = io_notif_to_data(notif);
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci	/* drop slot's master ref */
3662306a36Sopenharmony_ci	if (refcount_dec_and_test(&nd->uarg.refcnt))
3762306a36Sopenharmony_ci		__io_req_task_work_add(notif, IOU_F_TWQ_LAZY_WAKE);
3862306a36Sopenharmony_ci}
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_cistatic inline int io_notif_account_mem(struct io_kiocb *notif, unsigned len)
4162306a36Sopenharmony_ci{
4262306a36Sopenharmony_ci	struct io_ring_ctx *ctx = notif->ctx;
4362306a36Sopenharmony_ci	struct io_notif_data *nd = io_notif_to_data(notif);
4462306a36Sopenharmony_ci	unsigned nr_pages = (len >> PAGE_SHIFT) + 2;
4562306a36Sopenharmony_ci	int ret;
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci	if (ctx->user) {
4862306a36Sopenharmony_ci		ret = __io_account_mem(ctx->user, nr_pages);
4962306a36Sopenharmony_ci		if (ret)
5062306a36Sopenharmony_ci			return ret;
5162306a36Sopenharmony_ci		nd->account_pages += nr_pages;
5262306a36Sopenharmony_ci	}
5362306a36Sopenharmony_ci	return 0;
5462306a36Sopenharmony_ci}
55