xref: /openbmc/linux/drivers/md/dm-log-userspace-transfer.c (revision 3bd940030752a33ff665eefdd74a1cdb74a4f9b0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2006-2009 Red Hat, Inc.
4  *
5  * This file is released under the LGPL.
6  */
7 
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <net/sock.h>
12 #include <linux/workqueue.h>
13 #include <linux/connector.h>
14 #include <linux/device-mapper.h>
15 #include <linux/dm-log-userspace.h>
16 
17 #include "dm-log-userspace-transfer.h"
18 
19 static uint32_t dm_ulog_seq;
20 
21 /*
22  * Netlink/Connector is an unreliable protocol.  How long should
23  * we wait for a response before assuming it was lost and retrying?
24  * (If we do receive a response after this time, it will be discarded
25  * and the response to the resent request will be waited for.
26  */
27 #define DM_ULOG_RETRY_TIMEOUT (15 * HZ)
28 
29 /*
30  * Pre-allocated space for speed
31  */
32 #define DM_ULOG_PREALLOCED_SIZE 512
33 static struct cn_msg *prealloced_cn_msg;
34 static struct dm_ulog_request *prealloced_ulog_tfr;
35 
36 static struct cb_id ulog_cn_id = {
37 	.idx = CN_IDX_DM,
38 	.val = CN_VAL_DM_USERSPACE_LOG
39 };
40 
41 static DEFINE_MUTEX(dm_ulog_lock);
42 
43 struct receiving_pkg {
44 	struct list_head list;
45 	struct completion complete;
46 
47 	uint32_t seq;
48 
49 	int error;
50 	size_t *data_size;
51 	char *data;
52 };
53 
54 static DEFINE_SPINLOCK(receiving_list_lock);
55 static struct list_head receiving_list;
56 
57 static int dm_ulog_sendto_server(struct dm_ulog_request *tfr)
58 {
59 	int r;
60 	struct cn_msg *msg = prealloced_cn_msg;
61 
62 	memset(msg, 0, sizeof(struct cn_msg));
63 
64 	msg->id.idx = ulog_cn_id.idx;
65 	msg->id.val = ulog_cn_id.val;
66 	msg->ack = 0;
67 	msg->seq = tfr->seq;
68 	msg->len = sizeof(struct dm_ulog_request) + tfr->data_size;
69 
70 	r = cn_netlink_send(msg, 0, 0, gfp_any());
71 
72 	return r;
73 }
74 
75 /*
76  * Parameters for this function can be either msg or tfr, but not
77  * both.  This function fills in the reply for a waiting request.
78  * If just msg is given, then the reply is simply an ACK from userspace
79  * that the request was received.
80  *
81  * Returns: 0 on success, -ENOENT on failure
82  */
83 static int fill_pkg(struct cn_msg *msg, struct dm_ulog_request *tfr)
84 {
85 	uint32_t rtn_seq = (msg) ? msg->seq : (tfr) ? tfr->seq : 0;
86 	struct receiving_pkg *pkg;
87 
88 	/*
89 	 * The 'receiving_pkg' entries in this list are statically
90 	 * allocated on the stack in 'dm_consult_userspace'.
91 	 * Each process that is waiting for a reply from the user
92 	 * space server will have an entry in this list.
93 	 *
94 	 * We are safe to do it this way because the stack space
95 	 * is unique to each process, but still addressable by
96 	 * other processes.
97 	 */
98 	list_for_each_entry(pkg, &receiving_list, list) {
99 		if (rtn_seq != pkg->seq)
100 			continue;
101 
102 		if (msg) {
103 			pkg->error = -msg->ack;
104 			/*
105 			 * If we are trying again, we will need to know our
106 			 * storage capacity.  Otherwise, along with the
107 			 * error code, we make explicit that we have no data.
108 			 */
109 			if (pkg->error != -EAGAIN)
110 				*(pkg->data_size) = 0;
111 		} else if (tfr->data_size > *(pkg->data_size)) {
112 			DMERR("Insufficient space to receive package [%u] "
113 			      "(%u vs %zu)", tfr->request_type,
114 			      tfr->data_size, *(pkg->data_size));
115 
116 			*(pkg->data_size) = 0;
117 			pkg->error = -ENOSPC;
118 		} else {
119 			pkg->error = tfr->error;
120 			memcpy(pkg->data, tfr->data, tfr->data_size);
121 			*(pkg->data_size) = tfr->data_size;
122 		}
123 		complete(&pkg->complete);
124 		return 0;
125 	}
126 
127 	return -ENOENT;
128 }
129 
130 /*
131  * This is the connector callback that delivers data
132  * that was sent from userspace.
133  */
134 static void cn_ulog_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
135 {
136 	struct dm_ulog_request *tfr = (struct dm_ulog_request *)(msg + 1);
137 
138 	if (!capable(CAP_SYS_ADMIN))
139 		return;
140 
141 	spin_lock(&receiving_list_lock);
142 	if (msg->len == 0)
143 		fill_pkg(msg, NULL);
144 	else if (msg->len < sizeof(*tfr))
145 		DMERR("Incomplete message received (expected %u, got %u): [%u]",
146 		      (unsigned)sizeof(*tfr), msg->len, msg->seq);
147 	else
148 		fill_pkg(NULL, tfr);
149 	spin_unlock(&receiving_list_lock);
150 }
151 
152 /**
153  * dm_consult_userspace
154  * @uuid: log's universal unique identifier (must be DM_UUID_LEN in size)
155  * @luid: log's local unique identifier
156  * @request_type:  found in include/linux/dm-log-userspace.h
157  * @data: data to tx to the server
158  * @data_size: size of data in bytes
159  * @rdata: place to put return data from server
160  * @rdata_size: value-result (amount of space given/amount of space used)
161  *
162  * rdata_size is undefined on failure.
163  *
164  * Memory used to communicate with userspace is zero'ed
165  * before populating to ensure that no unwanted bits leak
166  * from kernel space to user-space.  All userspace log communications
167  * between kernel and user space go through this function.
168  *
169  * Returns: 0 on success, -EXXX on failure
170  **/
171 int dm_consult_userspace(const char *uuid, uint64_t luid, int request_type,
172 			 char *data, size_t data_size,
173 			 char *rdata, size_t *rdata_size)
174 {
175 	int r = 0;
176 	unsigned long tmo;
177 	size_t dummy = 0;
178 	int overhead_size = sizeof(struct dm_ulog_request) + sizeof(struct cn_msg);
179 	struct dm_ulog_request *tfr = prealloced_ulog_tfr;
180 	struct receiving_pkg pkg;
181 
182 	/*
183 	 * Given the space needed to hold the 'struct cn_msg' and
184 	 * 'struct dm_ulog_request' - do we have enough payload
185 	 * space remaining?
186 	 */
187 	if (data_size > (DM_ULOG_PREALLOCED_SIZE - overhead_size)) {
188 		DMINFO("Size of tfr exceeds preallocated size");
189 		return -EINVAL;
190 	}
191 
192 	if (!rdata_size)
193 		rdata_size = &dummy;
194 resend:
195 	/*
196 	 * We serialize the sending of requests so we can
197 	 * use the preallocated space.
198 	 */
199 	mutex_lock(&dm_ulog_lock);
200 
201 	memset(tfr, 0, DM_ULOG_PREALLOCED_SIZE - sizeof(struct cn_msg));
202 	memcpy(tfr->uuid, uuid, DM_UUID_LEN);
203 	tfr->version = DM_ULOG_REQUEST_VERSION;
204 	tfr->luid = luid;
205 	tfr->seq = dm_ulog_seq++;
206 
207 	/*
208 	 * Must be valid request type (all other bits set to
209 	 * zero).  This reserves other bits for possible future
210 	 * use.
211 	 */
212 	tfr->request_type = request_type & DM_ULOG_REQUEST_MASK;
213 
214 	tfr->data_size = data_size;
215 	if (data && data_size)
216 		memcpy(tfr->data, data, data_size);
217 
218 	memset(&pkg, 0, sizeof(pkg));
219 	init_completion(&pkg.complete);
220 	pkg.seq = tfr->seq;
221 	pkg.data_size = rdata_size;
222 	pkg.data = rdata;
223 	spin_lock(&receiving_list_lock);
224 	list_add(&(pkg.list), &receiving_list);
225 	spin_unlock(&receiving_list_lock);
226 
227 	r = dm_ulog_sendto_server(tfr);
228 
229 	mutex_unlock(&dm_ulog_lock);
230 
231 	if (r) {
232 		DMERR("Unable to send log request [%u] to userspace: %d",
233 		      request_type, r);
234 		spin_lock(&receiving_list_lock);
235 		list_del_init(&(pkg.list));
236 		spin_unlock(&receiving_list_lock);
237 
238 		goto out;
239 	}
240 
241 	tmo = wait_for_completion_timeout(&(pkg.complete), DM_ULOG_RETRY_TIMEOUT);
242 	spin_lock(&receiving_list_lock);
243 	list_del_init(&(pkg.list));
244 	spin_unlock(&receiving_list_lock);
245 	if (!tmo) {
246 		DMWARN("[%s] Request timed out: [%u/%u] - retrying",
247 		       (strlen(uuid) > 8) ?
248 		       (uuid + (strlen(uuid) - 8)) : (uuid),
249 		       request_type, pkg.seq);
250 		goto resend;
251 	}
252 
253 	r = pkg.error;
254 	if (r == -EAGAIN)
255 		goto resend;
256 
257 out:
258 	return r;
259 }
260 
261 int dm_ulog_tfr_init(void)
262 {
263 	int r;
264 	void *prealloced;
265 
266 	INIT_LIST_HEAD(&receiving_list);
267 
268 	prealloced = kmalloc(DM_ULOG_PREALLOCED_SIZE, GFP_KERNEL);
269 	if (!prealloced)
270 		return -ENOMEM;
271 
272 	prealloced_cn_msg = prealloced;
273 	prealloced_ulog_tfr = prealloced + sizeof(struct cn_msg);
274 
275 	r = cn_add_callback(&ulog_cn_id, "dmlogusr", cn_ulog_callback);
276 	if (r) {
277 		kfree(prealloced_cn_msg);
278 		return r;
279 	}
280 
281 	return 0;
282 }
283 
284 void dm_ulog_tfr_exit(void)
285 {
286 	cn_del_callback(&ulog_cn_id);
287 	kfree(prealloced_cn_msg);
288 }
289