xref: /linux/net/rds/info.c (revision 24e4aff8983fe663a85b5b157476f87ae0819e2c)
1 /*
2  * Copyright (c) 2006 Oracle.  All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  *
32  */
33 #include <linux/percpu.h>
34 #include <linux/seq_file.h>
35 #include <linux/srcu.h>
36 #include <linux/slab.h>
37 #include <linux/proc_fs.h>
38 #include <linux/export.h>
39 #include <linux/uio.h>
40 
41 #include "rds.h"
42 
43 /*
44  * This file implements a getsockopt() call which copies a set of fixed
45  * sized structs into a user-specified buffer as a means of providing
46  * read-only information about RDS.
47  *
48  * For a given information source there are a given number of fixed sized
49  * structs at a given time.  The structs are only copied if the user-specified
50  * buffer is big enough.  The destination pages that make up the buffer
51  * are pinned for the duration of the copy.
52  *
53  * This gives us the following benefits:
54  *
55  * - simple implementation, no copy "position" across multiple calls
56  * - consistent snapshot of an info source
57  * - atomic copy works well with whatever locking info source has
58  * - one portable tool to get rds info across implementations
59  * - long-lived tool can get info without allocating
60  *
61  * at the following costs:
62  *
63  * - info source copy must be pinned, may be "large"
64  */
65 
66 struct rds_info_iterator {
67 	struct page **pages;
68 	void *addr;
69 	unsigned long offset;
70 };
71 
72 DEFINE_STATIC_SRCU(rds_info_srcu);
73 static DEFINE_SPINLOCK(rds_info_lock);
74 static rds_info_func rds_info_funcs[RDS_INFO_LAST - RDS_INFO_FIRST + 1];
75 
76 void rds_info_register_func(int optname, rds_info_func func)
77 {
78 	int offset = optname - RDS_INFO_FIRST;
79 
80 	BUG_ON(optname < RDS_INFO_FIRST || optname > RDS_INFO_LAST);
81 
82 	spin_lock(&rds_info_lock);
83 	if (WARN_ON_ONCE(rds_info_funcs[offset])) {
84 		spin_unlock(&rds_info_lock);
85 		return;
86 	}
87 	WRITE_ONCE(rds_info_funcs[offset], func);
88 	spin_unlock(&rds_info_lock);
89 }
90 EXPORT_SYMBOL_GPL(rds_info_register_func);
91 
92 void rds_info_deregister_func(int optname, rds_info_func func)
93 {
94 	int offset = optname - RDS_INFO_FIRST;
95 
96 	BUG_ON(optname < RDS_INFO_FIRST || optname > RDS_INFO_LAST);
97 
98 	spin_lock(&rds_info_lock);
99 	if (WARN_ON_ONCE(rds_info_funcs[offset] != func)) {
100 		spin_unlock(&rds_info_lock);
101 		return;
102 	}
103 	WRITE_ONCE(rds_info_funcs[offset], NULL);
104 	spin_unlock(&rds_info_lock);
105 	synchronize_srcu(&rds_info_srcu);
106 }
107 EXPORT_SYMBOL_GPL(rds_info_deregister_func);
108 
109 /*
110  * Typically we hold an atomic kmap across multiple rds_info_copy() calls
111  * because the kmap is so expensive.  This must be called before using blocking
112  * operations while holding the mapping and as the iterator is torn down.
113  */
114 void rds_info_iter_unmap(struct rds_info_iterator *iter)
115 {
116 	if (iter->addr) {
117 		kunmap_atomic(iter->addr);
118 		iter->addr = NULL;
119 	}
120 }
121 
122 /*
123  * get_user_pages() called flush_dcache_page() on the pages for us.
124  */
125 void rds_info_copy(struct rds_info_iterator *iter, void *data,
126 		   unsigned long bytes)
127 {
128 	unsigned long this;
129 
130 	while (bytes) {
131 		if (!iter->addr)
132 			iter->addr = kmap_atomic(*iter->pages);
133 
134 		this = min(bytes, PAGE_SIZE - iter->offset);
135 
136 		rdsdebug("page %p addr %p offset %lu this %lu data %p "
137 			  "bytes %lu\n", *iter->pages, iter->addr,
138 			  iter->offset, this, data, bytes);
139 
140 		memcpy(iter->addr + iter->offset, data, this);
141 
142 		data += this;
143 		bytes -= this;
144 		iter->offset += this;
145 
146 		if (iter->offset == PAGE_SIZE) {
147 			kunmap_atomic(iter->addr);
148 			iter->addr = NULL;
149 			iter->offset = 0;
150 			iter->pages++;
151 		}
152 	}
153 }
154 EXPORT_SYMBOL_GPL(rds_info_copy);
155 
156 /*
157  * @opt->iter_out describes the buffer that the information snapshot will be
158  * copied into, and @opt->optlen is the size of that buffer on input.  On
159  * output @opt->optlen is set to the size of the requested snapshot in bytes.
160  *
161  * This function returns -errno if there is a failure, particularly -ENOSPC
162  * if the given buffer was not large enough to fit the snapshot.  On success
163  * it returns the positive number of bytes of each array element in the
164  * snapshot.
165  */
166 int rds_info_getsockopt(struct socket *sock, int optname, sockopt_t *opt)
167 {
168 	struct rds_info_iterator iter;
169 	struct rds_info_lengths lens;
170 	unsigned long nr_pages = 0;
171 	rds_info_func func;
172 	struct page **pages = NULL;
173 	size_t offset0 = 0;
174 	int srcu_idx;
175 	int npages = 0;
176 	int ret;
177 	int len;
178 	int total;
179 
180 	len = opt->optlen;
181 
182 	/* check for all kinds of wrapping and the like */
183 	if (len < 0 || len > INT_MAX - PAGE_SIZE + 1) {
184 		ret = -EINVAL;
185 		goto out;
186 	}
187 
188 	/* The info producers write into the pages with kmap_atomic() while
189 	 * holding a spinlock, so they need a genuine page-backed user buffer.
190 	 */
191 	if (!user_backed_iter(&opt->iter_out)) {
192 		ret = -EOPNOTSUPP;
193 		goto out;
194 	}
195 
196 	/* a 0 len call is just trying to probe its length */
197 	if (len == 0)
198 		goto call_func;
199 
200 	/*
201 	 * Preallocate the page array and pass it in so that
202 	 * iov_iter_extract_pages() fills it in place rather than allocating
203 	 * one for us.  Handing it a non-NULL array keeps ownership of the
204 	 * array with us on every return path, instead of depending on the
205 	 * iterator code to allocate and hand it back.
206 	 */
207 	npages = iov_iter_npages(&opt->iter_out, INT_MAX);
208 	pages = kvmalloc_array(npages, sizeof(*pages), GFP_KERNEL);
209 	if (!pages) {
210 		ret = -ENOMEM;
211 		goto out;
212 	}
213 
214 	ret = iov_iter_extract_pages(&opt->iter_out, &pages, len, npages,
215 				     0, &offset0);
216 	if (ret < 0)
217 		goto out;
218 	nr_pages = DIV_ROUND_UP(offset0 + ret, PAGE_SIZE);
219 	if (ret != len) {
220 		ret = -EAGAIN; /* XXX ? */
221 		goto out;
222 	}
223 
224 	rdsdebug("len %d nr_pages %lu\n", len, nr_pages);
225 
226 call_func:
227 	srcu_idx = srcu_read_lock(&rds_info_srcu);
228 	func = READ_ONCE(rds_info_funcs[optname - RDS_INFO_FIRST]);
229 	if (!func) {
230 		srcu_read_unlock(&rds_info_srcu, srcu_idx);
231 		ret = -ENOPROTOOPT;
232 		goto out;
233 	}
234 
235 	iter.pages = pages;
236 	iter.addr = NULL;
237 	iter.offset = offset0;
238 
239 	func(sock, len, &iter, &lens);
240 	srcu_read_unlock(&rds_info_srcu, srcu_idx);
241 	BUG_ON(lens.each == 0);
242 
243 	total = lens.nr * lens.each;
244 
245 	rds_info_iter_unmap(&iter);
246 
247 	if (total > len) {
248 		len = total;
249 		ret = -ENOSPC;
250 	} else {
251 		len = total;
252 		ret = lens.each;
253 	}
254 
255 	opt->optlen = len;
256 
257 out:
258 	/*
259 	 * iov_iter_extract_pages() pins only user-backed (ubuf) iters;
260 	 * iov_iter_extract_will_pin() reports whether an unpin is owed here.
261 	 */
262 	if (pages && iov_iter_extract_will_pin(&opt->iter_out))
263 		unpin_user_pages_dirty_lock(pages, nr_pages, true);
264 	kvfree(pages);
265 
266 	return ret;
267 }
268