Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  * Copyright (c) 2006, 2020 Oracle and/or its affiliates.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * This software is available to you under a choice of one of two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * licenses.  You may choose to be licensed under the terms of the GNU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * General Public License (GPL) Version 2, available from the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * COPYING in the main directory of this source tree, or the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * OpenIB.org BSD license below:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *     Redistribution and use in source and binary forms, with or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *     without modification, are permitted provided that the following
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *     conditions are met:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  *      - Redistributions of source code must retain the above
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  *        copyright notice, this list of conditions and the following
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *        disclaimer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  *      - Redistributions in binary form must reproduce the above
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  *        copyright notice, this list of conditions and the following
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *        disclaimer in the documentation and/or other materials
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *        provided with the distribution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * SOFTWARE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #include <linux/errqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #include "rds.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) static unsigned int	rds_exthdr_size[__RDS_EXTHDR_MAX] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) [RDS_EXTHDR_NONE]	= 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) [RDS_EXTHDR_VERSION]	= sizeof(struct rds_ext_header_version),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) [RDS_EXTHDR_RDMA]	= sizeof(struct rds_ext_header_rdma),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) [RDS_EXTHDR_RDMA_DEST]	= sizeof(struct rds_ext_header_rdma_dest),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) [RDS_EXTHDR_NPATHS]	= sizeof(u16),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) [RDS_EXTHDR_GEN_NUM]	= sizeof(u32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) void rds_message_addref(struct rds_message *rm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	rdsdebug("addref rm %p ref %d\n", rm, refcount_read(&rm->m_refcount));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	refcount_inc(&rm->m_refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) EXPORT_SYMBOL_GPL(rds_message_addref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static inline bool rds_zcookie_add(struct rds_msg_zcopy_info *info, u32 cookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	struct rds_zcopy_cookies *ck = &info->zcookies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	int ncookies = ck->num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	if (ncookies == RDS_MAX_ZCOOKIES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	ck->cookies[ncookies] = cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	ck->num =  ++ncookies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) static struct rds_msg_zcopy_info *rds_info_from_znotifier(struct rds_znotifier *znotif)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	return container_of(znotif, struct rds_msg_zcopy_info, znotif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) void rds_notify_msg_zcopy_purge(struct rds_msg_zcopy_queue *q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	LIST_HEAD(copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	struct rds_msg_zcopy_info *info, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	spin_lock_irqsave(&q->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	list_splice(&q->zcookie_head, &copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	INIT_LIST_HEAD(&q->zcookie_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	spin_unlock_irqrestore(&q->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	list_for_each_entry_safe(info, tmp, &copy, rs_zcookie_next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		list_del(&info->rs_zcookie_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		kfree(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) static void rds_rm_zerocopy_callback(struct rds_sock *rs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 				     struct rds_znotifier *znotif)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	struct rds_msg_zcopy_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	struct rds_msg_zcopy_queue *q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	u32 cookie = znotif->z_cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	struct rds_zcopy_cookies *ck;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	struct list_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	mm_unaccount_pinned_pages(&znotif->z_mmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	q = &rs->rs_zcookie_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	spin_lock_irqsave(&q->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	head = &q->zcookie_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	if (!list_empty(head)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		info = list_entry(head, struct rds_msg_zcopy_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 				  rs_zcookie_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		if (info && rds_zcookie_add(info, cookie)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 			spin_unlock_irqrestore(&q->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 			kfree(rds_info_from_znotifier(znotif));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 			/* caller invokes rds_wake_sk_sleep() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	info = rds_info_from_znotifier(znotif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	ck = &info->zcookies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	memset(ck, 0, sizeof(*ck));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	WARN_ON(!rds_zcookie_add(info, cookie));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	list_add_tail(&q->zcookie_head, &info->rs_zcookie_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	spin_unlock_irqrestore(&q->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	/* caller invokes rds_wake_sk_sleep() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)  * This relies on dma_map_sg() not touching sg[].page during merging.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static void rds_message_purge(struct rds_message *rm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	unsigned long i, flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	bool zcopy = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	if (unlikely(test_bit(RDS_MSG_PAGEVEC, &rm->m_flags)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	spin_lock_irqsave(&rm->m_rs_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	if (rm->m_rs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		struct rds_sock *rs = rm->m_rs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		if (rm->data.op_mmp_znotifier) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			zcopy = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			rds_rm_zerocopy_callback(rs, rm->data.op_mmp_znotifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			rds_wake_sk_sleep(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			rm->data.op_mmp_znotifier = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		sock_put(rds_rs_to_sk(rs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		rm->m_rs = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	spin_unlock_irqrestore(&rm->m_rs_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	for (i = 0; i < rm->data.op_nents; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		/* XXX will have to put_page for page refs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		if (!zcopy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 			__free_page(sg_page(&rm->data.op_sg[i]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 			put_page(sg_page(&rm->data.op_sg[i]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	rm->data.op_nents = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	if (rm->rdma.op_active)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		rds_rdma_free_op(&rm->rdma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	if (rm->rdma.op_rdma_mr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		kref_put(&rm->rdma.op_rdma_mr->r_kref, __rds_put_mr_final);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	if (rm->atomic.op_active)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		rds_atomic_free_op(&rm->atomic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	if (rm->atomic.op_rdma_mr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		kref_put(&rm->atomic.op_rdma_mr->r_kref, __rds_put_mr_final);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) void rds_message_put(struct rds_message *rm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	rdsdebug("put rm %p ref %d\n", rm, refcount_read(&rm->m_refcount));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	WARN(!refcount_read(&rm->m_refcount), "danger refcount zero on %p\n", rm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	if (refcount_dec_and_test(&rm->m_refcount)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		BUG_ON(!list_empty(&rm->m_sock_item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		BUG_ON(!list_empty(&rm->m_conn_item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		rds_message_purge(rm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		kfree(rm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) EXPORT_SYMBOL_GPL(rds_message_put);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) void rds_message_populate_header(struct rds_header *hdr, __be16 sport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 				 __be16 dport, u64 seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	hdr->h_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	hdr->h_sport = sport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	hdr->h_dport = dport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	hdr->h_sequence = cpu_to_be64(seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	hdr->h_exthdr[0] = RDS_EXTHDR_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) EXPORT_SYMBOL_GPL(rds_message_populate_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) int rds_message_add_extension(struct rds_header *hdr, unsigned int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 			      const void *data, unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	unsigned int ext_len = sizeof(u8) + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	unsigned char *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	/* For now, refuse to add more than one extension header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	if (hdr->h_exthdr[0] != RDS_EXTHDR_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	if (type >= __RDS_EXTHDR_MAX || len != rds_exthdr_size[type])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	if (ext_len >= RDS_HEADER_EXT_SPACE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	dst = hdr->h_exthdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	*dst++ = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	memcpy(dst, data, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	dst[len] = RDS_EXTHDR_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) EXPORT_SYMBOL_GPL(rds_message_add_extension);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)  * If a message has extension headers, retrieve them here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)  * Call like this:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)  * unsigned int pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)  * while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)  *	buflen = sizeof(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)  *	type = rds_message_next_extension(hdr, &pos, buffer, &buflen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)  *	if (type == RDS_EXTHDR_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)  *		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)  *	...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)  * }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) int rds_message_next_extension(struct rds_header *hdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		unsigned int *pos, void *buf, unsigned int *buflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	unsigned int offset, ext_type, ext_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	u8 *src = hdr->h_exthdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	offset = *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	if (offset >= RDS_HEADER_EXT_SPACE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		goto none;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	/* Get the extension type and length. For now, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	 * length is implied by the extension type. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	ext_type = src[offset++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	if (ext_type == RDS_EXTHDR_NONE || ext_type >= __RDS_EXTHDR_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		goto none;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	ext_len = rds_exthdr_size[ext_type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	if (offset + ext_len > RDS_HEADER_EXT_SPACE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		goto none;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	*pos = offset + ext_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	if (ext_len < *buflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		*buflen = ext_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	memcpy(buf, src + offset, *buflen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	return ext_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) none:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	*pos = RDS_HEADER_EXT_SPACE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	*buflen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	return RDS_EXTHDR_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) int rds_message_add_rdma_dest_extension(struct rds_header *hdr, u32 r_key, u32 offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	struct rds_ext_header_rdma_dest ext_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	ext_hdr.h_rdma_rkey = cpu_to_be32(r_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	ext_hdr.h_rdma_offset = cpu_to_be32(offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	return rds_message_add_extension(hdr, RDS_EXTHDR_RDMA_DEST, &ext_hdr, sizeof(ext_hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) EXPORT_SYMBOL_GPL(rds_message_add_rdma_dest_extension);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)  * Each rds_message is allocated with extra space for the scatterlist entries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)  * rds ops will need. This is to minimize memory allocation count. Then, each rds op
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)  * can grab SGs when initializing its part of the rds_message.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) struct rds_message *rds_message_alloc(unsigned int extra_len, gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	struct rds_message *rm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	if (extra_len > KMALLOC_MAX_SIZE - sizeof(struct rds_message))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	rm = kzalloc(sizeof(struct rds_message) + extra_len, gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	if (!rm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	rm->m_used_sgs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	rm->m_total_sgs = extra_len / sizeof(struct scatterlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	refcount_set(&rm->m_refcount, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	INIT_LIST_HEAD(&rm->m_sock_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	INIT_LIST_HEAD(&rm->m_conn_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	spin_lock_init(&rm->m_rs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	init_waitqueue_head(&rm->m_flush_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	return rm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)  * RDS ops use this to grab SG entries from the rm's sg pool.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) struct scatterlist *rds_message_alloc_sgs(struct rds_message *rm, int nents)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	struct scatterlist *sg_first = (struct scatterlist *) &rm[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	struct scatterlist *sg_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	if (nents <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		pr_warn("rds: alloc sgs failed! nents <= 0\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	if (rm->m_used_sgs + nents > rm->m_total_sgs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		pr_warn("rds: alloc sgs failed! total %d used %d nents %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 			rm->m_total_sgs, rm->m_used_sgs, nents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	sg_ret = &sg_first[rm->m_used_sgs];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	sg_init_table(sg_ret, nents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	rm->m_used_sgs += nents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	return sg_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) struct rds_message *rds_message_map_pages(unsigned long *page_addrs, unsigned int total_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	struct rds_message *rm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	int num_sgs = DIV_ROUND_UP(total_len, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	int extra_bytes = num_sgs * sizeof(struct scatterlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	rm = rds_message_alloc(extra_bytes, GFP_NOWAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	if (!rm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	set_bit(RDS_MSG_PAGEVEC, &rm->m_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	rm->m_inc.i_hdr.h_len = cpu_to_be32(total_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	rm->data.op_nents = DIV_ROUND_UP(total_len, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	rm->data.op_sg = rds_message_alloc_sgs(rm, num_sgs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	if (IS_ERR(rm->data.op_sg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		void *err = ERR_CAST(rm->data.op_sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		rds_message_put(rm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	for (i = 0; i < rm->data.op_nents; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		sg_set_page(&rm->data.op_sg[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 				virt_to_page(page_addrs[i]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 				PAGE_SIZE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	return rm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) static int rds_message_zcopy_from_user(struct rds_message *rm, struct iov_iter *from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	struct scatterlist *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	int length = iov_iter_count(from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	int total_copied = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	struct rds_msg_zcopy_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	rm->m_inc.i_hdr.h_len = cpu_to_be32(iov_iter_count(from));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	 * now allocate and copy in the data payload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	sg = rm->data.op_sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	info = kzalloc(sizeof(*info), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	if (!info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	INIT_LIST_HEAD(&info->rs_zcookie_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	rm->data.op_mmp_znotifier = &info->znotif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	if (mm_account_pinned_pages(&rm->data.op_mmp_znotifier->z_mmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 				    length)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	while (iov_iter_count(from)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		struct page *pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		size_t start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		ssize_t copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		copied = iov_iter_get_pages(from, &pages, PAGE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 					    1, &start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		if (copied < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 			struct mmpin *mmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 			int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 			for (i = 0; i < rm->data.op_nents; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 				put_page(sg_page(&rm->data.op_sg[i]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 			mmp = &rm->data.op_mmp_znotifier->z_mmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 			mm_unaccount_pinned_pages(mmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 			ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		total_copied += copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		iov_iter_advance(from, copied);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		length -= copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		sg_set_page(sg, pages, copied, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		rm->data.op_nents++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		sg++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	WARN_ON_ONCE(length != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	kfree(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	rm->data.op_mmp_znotifier = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) int rds_message_copy_from_user(struct rds_message *rm, struct iov_iter *from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 			       bool zcopy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	unsigned long to_copy, nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	unsigned long sg_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	struct scatterlist *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	rm->m_inc.i_hdr.h_len = cpu_to_be32(iov_iter_count(from));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	/* now allocate and copy in the data payload.  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	sg = rm->data.op_sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	sg_off = 0; /* Dear gcc, sg->page will be null from kzalloc. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	if (zcopy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		return rds_message_zcopy_from_user(rm, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	while (iov_iter_count(from)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		if (!sg_page(sg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 			ret = rds_page_remainder_alloc(sg, iov_iter_count(from),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 						       GFP_HIGHUSER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 			rm->data.op_nents++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 			sg_off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		to_copy = min_t(unsigned long, iov_iter_count(from),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 				sg->length - sg_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		rds_stats_add(s_copy_from_user, to_copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		nbytes = copy_page_from_iter(sg_page(sg), sg->offset + sg_off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 					     to_copy, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		if (nbytes != to_copy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 		sg_off += to_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		if (sg_off == sg->length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 			sg++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) int rds_message_inc_copy_to_user(struct rds_incoming *inc, struct iov_iter *to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	struct rds_message *rm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	struct scatterlist *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	unsigned long to_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	unsigned long vec_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	int copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	u32 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	rm = container_of(inc, struct rds_message, m_inc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	sg = rm->data.op_sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	vec_off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	copied = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	while (iov_iter_count(to) && copied < len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		to_copy = min_t(unsigned long, iov_iter_count(to),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 				sg->length - vec_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 		to_copy = min_t(unsigned long, to_copy, len - copied);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		rds_stats_add(s_copy_to_user, to_copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		ret = copy_page_to_iter(sg_page(sg), sg->offset + vec_off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 					to_copy, to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		if (ret != to_copy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		vec_off += to_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		copied += to_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 		if (vec_off == sg->length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 			vec_off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 			sg++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	return copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)  * If the message is still on the send queue, wait until the transport
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)  * is done with it. This is particularly important for RDMA operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) void rds_message_wait(struct rds_message *rm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	wait_event_interruptible(rm->m_flush_wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 			!test_bit(RDS_MSG_MAPPED, &rm->m_flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) void rds_message_unmapped(struct rds_message *rm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	clear_bit(RDS_MSG_MAPPED, &rm->m_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	wake_up_interruptible(&rm->m_flush_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) EXPORT_SYMBOL_GPL(rds_message_unmapped);