| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Copyright (c) 2015, Sony Mobile Communications Inc. |
|---|
| 3 | 4 | * Copyright (c) 2013, The Linux Foundation. All rights reserved. |
|---|
| 4 | | - * |
|---|
| 5 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 6 | | - * it under the terms of the GNU General Public License version 2 and |
|---|
| 7 | | - * only version 2 as published by the Free Software Foundation. |
|---|
| 8 | | - * |
|---|
| 9 | | - * This program is distributed in the hope that it will be useful, |
|---|
| 10 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 | | - * GNU General Public License for more details. |
|---|
| 13 | 5 | */ |
|---|
| 14 | 6 | #include <linux/module.h> |
|---|
| 15 | 7 | #include <linux/netlink.h> |
|---|
| 16 | 8 | #include <linux/qrtr.h> |
|---|
| 17 | 9 | #include <linux/termios.h> /* For TIOCINQ/OUTQ */ |
|---|
| 10 | +#include <linux/spinlock.h> |
|---|
| 11 | +#include <linux/wait.h> |
|---|
| 18 | 12 | |
|---|
| 19 | 13 | #include <net/sock.h> |
|---|
| 20 | 14 | |
|---|
| .. | .. |
|---|
| 26 | 20 | /* auto-bind range */ |
|---|
| 27 | 21 | #define QRTR_MIN_EPH_SOCKET 0x4000 |
|---|
| 28 | 22 | #define QRTR_MAX_EPH_SOCKET 0x7fff |
|---|
| 23 | +#define QRTR_EPH_PORT_RANGE \ |
|---|
| 24 | + XA_LIMIT(QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET) |
|---|
| 29 | 25 | |
|---|
| 30 | 26 | /** |
|---|
| 31 | 27 | * struct qrtr_hdr_v1 - (I|R)PCrouter packet header version 1 |
|---|
| .. | .. |
|---|
| 101 | 97 | return container_of(sk, struct qrtr_sock, sk); |
|---|
| 102 | 98 | } |
|---|
| 103 | 99 | |
|---|
| 104 | | -static unsigned int qrtr_local_nid = -1; |
|---|
| 100 | +static unsigned int qrtr_local_nid = 1; |
|---|
| 105 | 101 | |
|---|
| 106 | 102 | /* for node ids */ |
|---|
| 107 | | -static RADIX_TREE(qrtr_nodes, GFP_KERNEL); |
|---|
| 103 | +static RADIX_TREE(qrtr_nodes, GFP_ATOMIC); |
|---|
| 104 | +static DEFINE_SPINLOCK(qrtr_nodes_lock); |
|---|
| 108 | 105 | /* broadcast list */ |
|---|
| 109 | 106 | static LIST_HEAD(qrtr_all_nodes); |
|---|
| 110 | | -/* lock for qrtr_nodes, qrtr_all_nodes and node reference */ |
|---|
| 107 | +/* lock for qrtr_all_nodes and node reference */ |
|---|
| 111 | 108 | static DEFINE_MUTEX(qrtr_node_lock); |
|---|
| 112 | 109 | |
|---|
| 113 | 110 | /* local port allocation management */ |
|---|
| 114 | | -static DEFINE_IDR(qrtr_ports); |
|---|
| 115 | | -static DEFINE_MUTEX(qrtr_port_lock); |
|---|
| 111 | +static DEFINE_XARRAY_ALLOC(qrtr_ports); |
|---|
| 116 | 112 | |
|---|
| 117 | 113 | /** |
|---|
| 118 | 114 | * struct qrtr_node - endpoint node |
|---|
| .. | .. |
|---|
| 120 | 116 | * @ep: endpoint |
|---|
| 121 | 117 | * @ref: reference count for node |
|---|
| 122 | 118 | * @nid: node id |
|---|
| 119 | + * @qrtr_tx_flow: tree of qrtr_tx_flow, keyed by node << 32 | port |
|---|
| 120 | + * @qrtr_tx_lock: lock for qrtr_tx_flow inserts |
|---|
| 123 | 121 | * @rx_queue: receive queue |
|---|
| 124 | | - * @work: scheduled work struct for recv work |
|---|
| 125 | 122 | * @item: list item for broadcast list |
|---|
| 126 | 123 | */ |
|---|
| 127 | 124 | struct qrtr_node { |
|---|
| .. | .. |
|---|
| 130 | 127 | struct kref ref; |
|---|
| 131 | 128 | unsigned int nid; |
|---|
| 132 | 129 | |
|---|
| 130 | + struct radix_tree_root qrtr_tx_flow; |
|---|
| 131 | + struct mutex qrtr_tx_lock; /* for qrtr_tx_flow */ |
|---|
| 132 | + |
|---|
| 133 | 133 | struct sk_buff_head rx_queue; |
|---|
| 134 | | - struct work_struct work; |
|---|
| 135 | 134 | struct list_head item; |
|---|
| 136 | 135 | }; |
|---|
| 136 | + |
|---|
| 137 | +/** |
|---|
| 138 | + * struct qrtr_tx_flow - tx flow control |
|---|
| 139 | + * @resume_tx: waiters for a resume tx from the remote |
|---|
| 140 | + * @pending: number of waiting senders |
|---|
| 141 | + * @tx_failed: indicates that a message with confirm_rx flag was lost |
|---|
| 142 | + */ |
|---|
| 143 | +struct qrtr_tx_flow { |
|---|
| 144 | + struct wait_queue_head resume_tx; |
|---|
| 145 | + int pending; |
|---|
| 146 | + int tx_failed; |
|---|
| 147 | +}; |
|---|
| 148 | + |
|---|
| 149 | +#define QRTR_TX_FLOW_HIGH 10 |
|---|
| 150 | +#define QRTR_TX_FLOW_LOW 5 |
|---|
| 137 | 151 | |
|---|
| 138 | 152 | static int qrtr_local_enqueue(struct qrtr_node *node, struct sk_buff *skb, |
|---|
| 139 | 153 | int type, struct sockaddr_qrtr *from, |
|---|
| .. | .. |
|---|
| 141 | 155 | static int qrtr_bcast_enqueue(struct qrtr_node *node, struct sk_buff *skb, |
|---|
| 142 | 156 | int type, struct sockaddr_qrtr *from, |
|---|
| 143 | 157 | struct sockaddr_qrtr *to); |
|---|
| 158 | +static struct qrtr_sock *qrtr_port_lookup(int port); |
|---|
| 159 | +static void qrtr_port_put(struct qrtr_sock *ipc); |
|---|
| 144 | 160 | |
|---|
| 145 | 161 | /* Release node resources and free the node. |
|---|
| 146 | 162 | * |
|---|
| .. | .. |
|---|
| 150 | 166 | static void __qrtr_node_release(struct kref *kref) |
|---|
| 151 | 167 | { |
|---|
| 152 | 168 | struct qrtr_node *node = container_of(kref, struct qrtr_node, ref); |
|---|
| 169 | + struct radix_tree_iter iter; |
|---|
| 170 | + struct qrtr_tx_flow *flow; |
|---|
| 171 | + unsigned long flags; |
|---|
| 172 | + void __rcu **slot; |
|---|
| 153 | 173 | |
|---|
| 174 | + spin_lock_irqsave(&qrtr_nodes_lock, flags); |
|---|
| 154 | 175 | if (node->nid != QRTR_EP_NID_AUTO) |
|---|
| 155 | 176 | radix_tree_delete(&qrtr_nodes, node->nid); |
|---|
| 177 | + spin_unlock_irqrestore(&qrtr_nodes_lock, flags); |
|---|
| 156 | 178 | |
|---|
| 157 | 179 | list_del(&node->item); |
|---|
| 158 | 180 | mutex_unlock(&qrtr_node_lock); |
|---|
| 159 | 181 | |
|---|
| 160 | | - cancel_work_sync(&node->work); |
|---|
| 161 | 182 | skb_queue_purge(&node->rx_queue); |
|---|
| 183 | + |
|---|
| 184 | + /* Free tx flow counters */ |
|---|
| 185 | + radix_tree_for_each_slot(slot, &node->qrtr_tx_flow, &iter, 0) { |
|---|
| 186 | + flow = *slot; |
|---|
| 187 | + radix_tree_iter_delete(&node->qrtr_tx_flow, &iter, slot); |
|---|
| 188 | + kfree(flow); |
|---|
| 189 | + } |
|---|
| 162 | 190 | kfree(node); |
|---|
| 163 | 191 | } |
|---|
| 164 | 192 | |
|---|
| .. | .. |
|---|
| 178 | 206 | kref_put_mutex(&node->ref, __qrtr_node_release, &qrtr_node_lock); |
|---|
| 179 | 207 | } |
|---|
| 180 | 208 | |
|---|
| 209 | +/** |
|---|
| 210 | + * qrtr_tx_resume() - reset flow control counter |
|---|
| 211 | + * @node: qrtr_node that the QRTR_TYPE_RESUME_TX packet arrived on |
|---|
| 212 | + * @skb: resume_tx packet |
|---|
| 213 | + */ |
|---|
| 214 | +static void qrtr_tx_resume(struct qrtr_node *node, struct sk_buff *skb) |
|---|
| 215 | +{ |
|---|
| 216 | + struct qrtr_ctrl_pkt *pkt = (struct qrtr_ctrl_pkt *)skb->data; |
|---|
| 217 | + u64 remote_node = le32_to_cpu(pkt->client.node); |
|---|
| 218 | + u32 remote_port = le32_to_cpu(pkt->client.port); |
|---|
| 219 | + struct qrtr_tx_flow *flow; |
|---|
| 220 | + unsigned long key; |
|---|
| 221 | + |
|---|
| 222 | + key = remote_node << 32 | remote_port; |
|---|
| 223 | + |
|---|
| 224 | + rcu_read_lock(); |
|---|
| 225 | + flow = radix_tree_lookup(&node->qrtr_tx_flow, key); |
|---|
| 226 | + rcu_read_unlock(); |
|---|
| 227 | + if (flow) { |
|---|
| 228 | + spin_lock(&flow->resume_tx.lock); |
|---|
| 229 | + flow->pending = 0; |
|---|
| 230 | + spin_unlock(&flow->resume_tx.lock); |
|---|
| 231 | + wake_up_interruptible_all(&flow->resume_tx); |
|---|
| 232 | + } |
|---|
| 233 | + |
|---|
| 234 | + consume_skb(skb); |
|---|
| 235 | +} |
|---|
| 236 | + |
|---|
| 237 | +/** |
|---|
| 238 | + * qrtr_tx_wait() - flow control for outgoing packets |
|---|
| 239 | + * @node: qrtr_node that the packet is to be send to |
|---|
| 240 | + * @dest_node: node id of the destination |
|---|
| 241 | + * @dest_port: port number of the destination |
|---|
| 242 | + * @type: type of message |
|---|
| 243 | + * |
|---|
| 244 | + * The flow control scheme is based around the low and high "watermarks". When |
|---|
| 245 | + * the low watermark is passed the confirm_rx flag is set on the outgoing |
|---|
| 246 | + * message, which will trigger the remote to send a control message of the type |
|---|
| 247 | + * QRTR_TYPE_RESUME_TX to reset the counter. If the high watermark is hit |
|---|
| 248 | + * further transmision should be paused. |
|---|
| 249 | + * |
|---|
| 250 | + * Return: 1 if confirm_rx should be set, 0 otherwise or errno failure |
|---|
| 251 | + */ |
|---|
| 252 | +static int qrtr_tx_wait(struct qrtr_node *node, int dest_node, int dest_port, |
|---|
| 253 | + int type) |
|---|
| 254 | +{ |
|---|
| 255 | + unsigned long key = (u64)dest_node << 32 | dest_port; |
|---|
| 256 | + struct qrtr_tx_flow *flow; |
|---|
| 257 | + int confirm_rx = 0; |
|---|
| 258 | + int ret; |
|---|
| 259 | + |
|---|
| 260 | + /* Never set confirm_rx on non-data packets */ |
|---|
| 261 | + if (type != QRTR_TYPE_DATA) |
|---|
| 262 | + return 0; |
|---|
| 263 | + |
|---|
| 264 | + mutex_lock(&node->qrtr_tx_lock); |
|---|
| 265 | + flow = radix_tree_lookup(&node->qrtr_tx_flow, key); |
|---|
| 266 | + if (!flow) { |
|---|
| 267 | + flow = kzalloc(sizeof(*flow), GFP_KERNEL); |
|---|
| 268 | + if (flow) { |
|---|
| 269 | + init_waitqueue_head(&flow->resume_tx); |
|---|
| 270 | + if (radix_tree_insert(&node->qrtr_tx_flow, key, flow)) { |
|---|
| 271 | + kfree(flow); |
|---|
| 272 | + flow = NULL; |
|---|
| 273 | + } |
|---|
| 274 | + } |
|---|
| 275 | + } |
|---|
| 276 | + mutex_unlock(&node->qrtr_tx_lock); |
|---|
| 277 | + |
|---|
| 278 | + /* Set confirm_rx if we where unable to find and allocate a flow */ |
|---|
| 279 | + if (!flow) |
|---|
| 280 | + return 1; |
|---|
| 281 | + |
|---|
| 282 | + spin_lock_irq(&flow->resume_tx.lock); |
|---|
| 283 | + ret = wait_event_interruptible_locked_irq(flow->resume_tx, |
|---|
| 284 | + flow->pending < QRTR_TX_FLOW_HIGH || |
|---|
| 285 | + flow->tx_failed || |
|---|
| 286 | + !node->ep); |
|---|
| 287 | + if (ret < 0) { |
|---|
| 288 | + confirm_rx = ret; |
|---|
| 289 | + } else if (!node->ep) { |
|---|
| 290 | + confirm_rx = -EPIPE; |
|---|
| 291 | + } else if (flow->tx_failed) { |
|---|
| 292 | + flow->tx_failed = 0; |
|---|
| 293 | + confirm_rx = 1; |
|---|
| 294 | + } else { |
|---|
| 295 | + flow->pending++; |
|---|
| 296 | + confirm_rx = flow->pending == QRTR_TX_FLOW_LOW; |
|---|
| 297 | + } |
|---|
| 298 | + spin_unlock_irq(&flow->resume_tx.lock); |
|---|
| 299 | + |
|---|
| 300 | + return confirm_rx; |
|---|
| 301 | +} |
|---|
| 302 | + |
|---|
| 303 | +/** |
|---|
| 304 | + * qrtr_tx_flow_failed() - flag that tx of confirm_rx flagged messages failed |
|---|
| 305 | + * @node: qrtr_node that the packet is to be send to |
|---|
| 306 | + * @dest_node: node id of the destination |
|---|
| 307 | + * @dest_port: port number of the destination |
|---|
| 308 | + * |
|---|
| 309 | + * Signal that the transmission of a message with confirm_rx flag failed. The |
|---|
| 310 | + * flow's "pending" counter will keep incrementing towards QRTR_TX_FLOW_HIGH, |
|---|
| 311 | + * at which point transmission would stall forever waiting for the resume TX |
|---|
| 312 | + * message associated with the dropped confirm_rx message. |
|---|
| 313 | + * Work around this by marking the flow as having a failed transmission and |
|---|
| 314 | + * cause the next transmission attempt to be sent with the confirm_rx. |
|---|
| 315 | + */ |
|---|
| 316 | +static void qrtr_tx_flow_failed(struct qrtr_node *node, int dest_node, |
|---|
| 317 | + int dest_port) |
|---|
| 318 | +{ |
|---|
| 319 | + unsigned long key = (u64)dest_node << 32 | dest_port; |
|---|
| 320 | + struct qrtr_tx_flow *flow; |
|---|
| 321 | + |
|---|
| 322 | + rcu_read_lock(); |
|---|
| 323 | + flow = radix_tree_lookup(&node->qrtr_tx_flow, key); |
|---|
| 324 | + rcu_read_unlock(); |
|---|
| 325 | + if (flow) { |
|---|
| 326 | + spin_lock_irq(&flow->resume_tx.lock); |
|---|
| 327 | + flow->tx_failed = 1; |
|---|
| 328 | + spin_unlock_irq(&flow->resume_tx.lock); |
|---|
| 329 | + } |
|---|
| 330 | +} |
|---|
| 331 | + |
|---|
| 181 | 332 | /* Pass an outgoing packet socket buffer to the endpoint driver. */ |
|---|
| 182 | 333 | static int qrtr_node_enqueue(struct qrtr_node *node, struct sk_buff *skb, |
|---|
| 183 | 334 | int type, struct sockaddr_qrtr *from, |
|---|
| .. | .. |
|---|
| 185 | 336 | { |
|---|
| 186 | 337 | struct qrtr_hdr_v1 *hdr; |
|---|
| 187 | 338 | size_t len = skb->len; |
|---|
| 188 | | - int rc; |
|---|
| 339 | + int rc, confirm_rx; |
|---|
| 340 | + |
|---|
| 341 | + confirm_rx = qrtr_tx_wait(node, to->sq_node, to->sq_port, type); |
|---|
| 342 | + if (confirm_rx < 0) { |
|---|
| 343 | + kfree_skb(skb); |
|---|
| 344 | + return confirm_rx; |
|---|
| 345 | + } |
|---|
| 189 | 346 | |
|---|
| 190 | 347 | hdr = skb_push(skb, sizeof(*hdr)); |
|---|
| 191 | 348 | hdr->version = cpu_to_le32(QRTR_PROTO_VER_1); |
|---|
| .. | .. |
|---|
| 201 | 358 | } |
|---|
| 202 | 359 | |
|---|
| 203 | 360 | hdr->size = cpu_to_le32(len); |
|---|
| 204 | | - hdr->confirm_rx = 0; |
|---|
| 361 | + hdr->confirm_rx = !!confirm_rx; |
|---|
| 205 | 362 | |
|---|
| 206 | 363 | rc = skb_put_padto(skb, ALIGN(len, 4) + sizeof(*hdr)); |
|---|
| 207 | 364 | |
|---|
| .. | .. |
|---|
| 214 | 371 | kfree_skb(skb); |
|---|
| 215 | 372 | mutex_unlock(&node->ep_lock); |
|---|
| 216 | 373 | } |
|---|
| 374 | + /* Need to ensure that a subsequent message carries the otherwise lost |
|---|
| 375 | + * confirm_rx flag if we dropped this one */ |
|---|
| 376 | + if (rc && confirm_rx) |
|---|
| 377 | + qrtr_tx_flow_failed(node, to->sq_node, to->sq_port); |
|---|
| 378 | + |
|---|
| 217 | 379 | return rc; |
|---|
| 218 | 380 | } |
|---|
| 219 | 381 | |
|---|
| .. | .. |
|---|
| 224 | 386 | static struct qrtr_node *qrtr_node_lookup(unsigned int nid) |
|---|
| 225 | 387 | { |
|---|
| 226 | 388 | struct qrtr_node *node; |
|---|
| 389 | + unsigned long flags; |
|---|
| 227 | 390 | |
|---|
| 228 | | - mutex_lock(&qrtr_node_lock); |
|---|
| 391 | + spin_lock_irqsave(&qrtr_nodes_lock, flags); |
|---|
| 229 | 392 | node = radix_tree_lookup(&qrtr_nodes, nid); |
|---|
| 230 | 393 | node = qrtr_node_acquire(node); |
|---|
| 231 | | - mutex_unlock(&qrtr_node_lock); |
|---|
| 394 | + spin_unlock_irqrestore(&qrtr_nodes_lock, flags); |
|---|
| 232 | 395 | |
|---|
| 233 | 396 | return node; |
|---|
| 234 | 397 | } |
|---|
| .. | .. |
|---|
| 240 | 403 | */ |
|---|
| 241 | 404 | static void qrtr_node_assign(struct qrtr_node *node, unsigned int nid) |
|---|
| 242 | 405 | { |
|---|
| 406 | + unsigned long flags; |
|---|
| 407 | + |
|---|
| 243 | 408 | if (node->nid != QRTR_EP_NID_AUTO || nid == QRTR_EP_NID_AUTO) |
|---|
| 244 | 409 | return; |
|---|
| 245 | 410 | |
|---|
| 246 | | - mutex_lock(&qrtr_node_lock); |
|---|
| 411 | + spin_lock_irqsave(&qrtr_nodes_lock, flags); |
|---|
| 247 | 412 | radix_tree_insert(&qrtr_nodes, nid, node); |
|---|
| 248 | 413 | node->nid = nid; |
|---|
| 249 | | - mutex_unlock(&qrtr_node_lock); |
|---|
| 414 | + spin_unlock_irqrestore(&qrtr_nodes_lock, flags); |
|---|
| 250 | 415 | } |
|---|
| 251 | 416 | |
|---|
| 252 | 417 | /** |
|---|
| .. | .. |
|---|
| 262 | 427 | struct qrtr_node *node = ep->node; |
|---|
| 263 | 428 | const struct qrtr_hdr_v1 *v1; |
|---|
| 264 | 429 | const struct qrtr_hdr_v2 *v2; |
|---|
| 430 | + struct qrtr_sock *ipc; |
|---|
| 265 | 431 | struct sk_buff *skb; |
|---|
| 266 | 432 | struct qrtr_cb *cb; |
|---|
| 267 | 433 | size_t size; |
|---|
| .. | .. |
|---|
| 324 | 490 | if (!size || len != ALIGN(size, 4) + hdrlen) |
|---|
| 325 | 491 | goto err; |
|---|
| 326 | 492 | |
|---|
| 327 | | - if (cb->dst_port != QRTR_PORT_CTRL && cb->type != QRTR_TYPE_DATA) |
|---|
| 493 | + if (cb->dst_port != QRTR_PORT_CTRL && cb->type != QRTR_TYPE_DATA && |
|---|
| 494 | + cb->type != QRTR_TYPE_RESUME_TX) |
|---|
| 328 | 495 | goto err; |
|---|
| 329 | 496 | |
|---|
| 330 | 497 | skb_put_data(skb, data + hdrlen, size); |
|---|
| 331 | 498 | |
|---|
| 332 | | - skb_queue_tail(&node->rx_queue, skb); |
|---|
| 333 | | - schedule_work(&node->work); |
|---|
| 499 | + qrtr_node_assign(node, cb->src_node); |
|---|
| 500 | + |
|---|
| 501 | + if (cb->type == QRTR_TYPE_RESUME_TX) { |
|---|
| 502 | + qrtr_tx_resume(node, skb); |
|---|
| 503 | + } else { |
|---|
| 504 | + ipc = qrtr_port_lookup(cb->dst_port); |
|---|
| 505 | + if (!ipc) |
|---|
| 506 | + goto err; |
|---|
| 507 | + |
|---|
| 508 | + if (sock_queue_rcv_skb(&ipc->sk, skb)) { |
|---|
| 509 | + qrtr_port_put(ipc); |
|---|
| 510 | + goto err; |
|---|
| 511 | + } |
|---|
| 512 | + |
|---|
| 513 | + qrtr_port_put(ipc); |
|---|
| 514 | + } |
|---|
| 334 | 515 | |
|---|
| 335 | 516 | return 0; |
|---|
| 336 | 517 | |
|---|
| .. | .. |
|---|
| 365 | 546 | return skb; |
|---|
| 366 | 547 | } |
|---|
| 367 | 548 | |
|---|
| 368 | | -static struct qrtr_sock *qrtr_port_lookup(int port); |
|---|
| 369 | | -static void qrtr_port_put(struct qrtr_sock *ipc); |
|---|
| 370 | | - |
|---|
| 371 | | -/* Handle and route a received packet. |
|---|
| 372 | | - * |
|---|
| 373 | | - * This will auto-reply with resume-tx packet as necessary. |
|---|
| 374 | | - */ |
|---|
| 375 | | -static void qrtr_node_rx_work(struct work_struct *work) |
|---|
| 376 | | -{ |
|---|
| 377 | | - struct qrtr_node *node = container_of(work, struct qrtr_node, work); |
|---|
| 378 | | - struct qrtr_ctrl_pkt *pkt; |
|---|
| 379 | | - struct sockaddr_qrtr dst; |
|---|
| 380 | | - struct sockaddr_qrtr src; |
|---|
| 381 | | - struct sk_buff *skb; |
|---|
| 382 | | - |
|---|
| 383 | | - while ((skb = skb_dequeue(&node->rx_queue)) != NULL) { |
|---|
| 384 | | - struct qrtr_sock *ipc; |
|---|
| 385 | | - struct qrtr_cb *cb; |
|---|
| 386 | | - int confirm; |
|---|
| 387 | | - |
|---|
| 388 | | - cb = (struct qrtr_cb *)skb->cb; |
|---|
| 389 | | - src.sq_node = cb->src_node; |
|---|
| 390 | | - src.sq_port = cb->src_port; |
|---|
| 391 | | - dst.sq_node = cb->dst_node; |
|---|
| 392 | | - dst.sq_port = cb->dst_port; |
|---|
| 393 | | - confirm = !!cb->confirm_rx; |
|---|
| 394 | | - |
|---|
| 395 | | - qrtr_node_assign(node, cb->src_node); |
|---|
| 396 | | - |
|---|
| 397 | | - ipc = qrtr_port_lookup(cb->dst_port); |
|---|
| 398 | | - if (!ipc) { |
|---|
| 399 | | - kfree_skb(skb); |
|---|
| 400 | | - } else { |
|---|
| 401 | | - if (sock_queue_rcv_skb(&ipc->sk, skb)) |
|---|
| 402 | | - kfree_skb(skb); |
|---|
| 403 | | - |
|---|
| 404 | | - qrtr_port_put(ipc); |
|---|
| 405 | | - } |
|---|
| 406 | | - |
|---|
| 407 | | - if (confirm) { |
|---|
| 408 | | - skb = qrtr_alloc_ctrl_packet(&pkt); |
|---|
| 409 | | - if (!skb) |
|---|
| 410 | | - break; |
|---|
| 411 | | - |
|---|
| 412 | | - pkt->cmd = cpu_to_le32(QRTR_TYPE_RESUME_TX); |
|---|
| 413 | | - pkt->client.node = cpu_to_le32(dst.sq_node); |
|---|
| 414 | | - pkt->client.port = cpu_to_le32(dst.sq_port); |
|---|
| 415 | | - |
|---|
| 416 | | - if (qrtr_node_enqueue(node, skb, QRTR_TYPE_RESUME_TX, |
|---|
| 417 | | - &dst, &src)) |
|---|
| 418 | | - break; |
|---|
| 419 | | - } |
|---|
| 420 | | - } |
|---|
| 421 | | -} |
|---|
| 422 | | - |
|---|
| 423 | 549 | /** |
|---|
| 424 | 550 | * qrtr_endpoint_register() - register a new endpoint |
|---|
| 425 | 551 | * @ep: endpoint to register |
|---|
| .. | .. |
|---|
| 439 | 565 | if (!node) |
|---|
| 440 | 566 | return -ENOMEM; |
|---|
| 441 | 567 | |
|---|
| 442 | | - INIT_WORK(&node->work, qrtr_node_rx_work); |
|---|
| 443 | 568 | kref_init(&node->ref); |
|---|
| 444 | 569 | mutex_init(&node->ep_lock); |
|---|
| 445 | 570 | skb_queue_head_init(&node->rx_queue); |
|---|
| 446 | 571 | node->nid = QRTR_EP_NID_AUTO; |
|---|
| 447 | 572 | node->ep = ep; |
|---|
| 573 | + |
|---|
| 574 | + INIT_RADIX_TREE(&node->qrtr_tx_flow, GFP_KERNEL); |
|---|
| 575 | + mutex_init(&node->qrtr_tx_lock); |
|---|
| 448 | 576 | |
|---|
| 449 | 577 | qrtr_node_assign(node, nid); |
|---|
| 450 | 578 | |
|---|
| .. | .. |
|---|
| 466 | 594 | struct qrtr_node *node = ep->node; |
|---|
| 467 | 595 | struct sockaddr_qrtr src = {AF_QIPCRTR, node->nid, QRTR_PORT_CTRL}; |
|---|
| 468 | 596 | struct sockaddr_qrtr dst = {AF_QIPCRTR, qrtr_local_nid, QRTR_PORT_CTRL}; |
|---|
| 597 | + struct radix_tree_iter iter; |
|---|
| 469 | 598 | struct qrtr_ctrl_pkt *pkt; |
|---|
| 599 | + struct qrtr_tx_flow *flow; |
|---|
| 470 | 600 | struct sk_buff *skb; |
|---|
| 601 | + void __rcu **slot; |
|---|
| 471 | 602 | |
|---|
| 472 | 603 | mutex_lock(&node->ep_lock); |
|---|
| 473 | 604 | node->ep = NULL; |
|---|
| .. | .. |
|---|
| 479 | 610 | pkt->cmd = cpu_to_le32(QRTR_TYPE_BYE); |
|---|
| 480 | 611 | qrtr_local_enqueue(NULL, skb, QRTR_TYPE_BYE, &src, &dst); |
|---|
| 481 | 612 | } |
|---|
| 613 | + |
|---|
| 614 | + /* Wake up any transmitters waiting for resume-tx from the node */ |
|---|
| 615 | + mutex_lock(&node->qrtr_tx_lock); |
|---|
| 616 | + radix_tree_for_each_slot(slot, &node->qrtr_tx_flow, &iter, 0) { |
|---|
| 617 | + flow = *slot; |
|---|
| 618 | + wake_up_interruptible_all(&flow->resume_tx); |
|---|
| 619 | + } |
|---|
| 620 | + mutex_unlock(&node->qrtr_tx_lock); |
|---|
| 482 | 621 | |
|---|
| 483 | 622 | qrtr_node_release(node); |
|---|
| 484 | 623 | ep->node = NULL; |
|---|
| .. | .. |
|---|
| 496 | 635 | if (port == QRTR_PORT_CTRL) |
|---|
| 497 | 636 | port = 0; |
|---|
| 498 | 637 | |
|---|
| 499 | | - mutex_lock(&qrtr_port_lock); |
|---|
| 500 | | - ipc = idr_find(&qrtr_ports, port); |
|---|
| 638 | + rcu_read_lock(); |
|---|
| 639 | + ipc = xa_load(&qrtr_ports, port); |
|---|
| 501 | 640 | if (ipc) |
|---|
| 502 | 641 | sock_hold(&ipc->sk); |
|---|
| 503 | | - mutex_unlock(&qrtr_port_lock); |
|---|
| 642 | + rcu_read_unlock(); |
|---|
| 504 | 643 | |
|---|
| 505 | 644 | return ipc; |
|---|
| 506 | 645 | } |
|---|
| .. | .. |
|---|
| 539 | 678 | |
|---|
| 540 | 679 | __sock_put(&ipc->sk); |
|---|
| 541 | 680 | |
|---|
| 542 | | - mutex_lock(&qrtr_port_lock); |
|---|
| 543 | | - idr_remove(&qrtr_ports, port); |
|---|
| 544 | | - mutex_unlock(&qrtr_port_lock); |
|---|
| 681 | + xa_erase(&qrtr_ports, port); |
|---|
| 682 | + |
|---|
| 683 | + /* Ensure that if qrtr_port_lookup() did enter the RCU read section we |
|---|
| 684 | + * wait for it to up increment the refcount */ |
|---|
| 685 | + synchronize_rcu(); |
|---|
| 545 | 686 | } |
|---|
| 546 | 687 | |
|---|
| 547 | 688 | /* Assign port number to socket. |
|---|
| .. | .. |
|---|
| 556 | 697 | */ |
|---|
| 557 | 698 | static int qrtr_port_assign(struct qrtr_sock *ipc, int *port) |
|---|
| 558 | 699 | { |
|---|
| 559 | | - u32 min_port; |
|---|
| 560 | 700 | int rc; |
|---|
| 561 | 701 | |
|---|
| 562 | | - mutex_lock(&qrtr_port_lock); |
|---|
| 563 | 702 | if (!*port) { |
|---|
| 564 | | - min_port = QRTR_MIN_EPH_SOCKET; |
|---|
| 565 | | - rc = idr_alloc_u32(&qrtr_ports, ipc, &min_port, QRTR_MAX_EPH_SOCKET, GFP_ATOMIC); |
|---|
| 566 | | - if (!rc) |
|---|
| 567 | | - *port = min_port; |
|---|
| 703 | + rc = xa_alloc(&qrtr_ports, port, ipc, QRTR_EPH_PORT_RANGE, |
|---|
| 704 | + GFP_KERNEL); |
|---|
| 568 | 705 | } else if (*port < QRTR_MIN_EPH_SOCKET && !capable(CAP_NET_ADMIN)) { |
|---|
| 569 | 706 | rc = -EACCES; |
|---|
| 570 | 707 | } else if (*port == QRTR_PORT_CTRL) { |
|---|
| 571 | | - min_port = 0; |
|---|
| 572 | | - rc = idr_alloc_u32(&qrtr_ports, ipc, &min_port, 0, GFP_ATOMIC); |
|---|
| 708 | + rc = xa_insert(&qrtr_ports, 0, ipc, GFP_KERNEL); |
|---|
| 573 | 709 | } else { |
|---|
| 574 | | - min_port = *port; |
|---|
| 575 | | - rc = idr_alloc_u32(&qrtr_ports, ipc, &min_port, *port, GFP_ATOMIC); |
|---|
| 576 | | - if (!rc) |
|---|
| 577 | | - *port = min_port; |
|---|
| 710 | + rc = xa_insert(&qrtr_ports, *port, ipc, GFP_KERNEL); |
|---|
| 578 | 711 | } |
|---|
| 579 | | - mutex_unlock(&qrtr_port_lock); |
|---|
| 580 | 712 | |
|---|
| 581 | | - if (rc == -ENOSPC) |
|---|
| 713 | + if (rc == -EBUSY) |
|---|
| 582 | 714 | return -EADDRINUSE; |
|---|
| 583 | 715 | else if (rc < 0) |
|---|
| 584 | 716 | return rc; |
|---|
| .. | .. |
|---|
| 592 | 724 | static void qrtr_reset_ports(void) |
|---|
| 593 | 725 | { |
|---|
| 594 | 726 | struct qrtr_sock *ipc; |
|---|
| 595 | | - int id; |
|---|
| 727 | + unsigned long index; |
|---|
| 596 | 728 | |
|---|
| 597 | | - mutex_lock(&qrtr_port_lock); |
|---|
| 598 | | - idr_for_each_entry(&qrtr_ports, ipc, id) { |
|---|
| 599 | | - /* Don't reset control port */ |
|---|
| 600 | | - if (id == 0) |
|---|
| 601 | | - continue; |
|---|
| 602 | | - |
|---|
| 729 | + rcu_read_lock(); |
|---|
| 730 | + xa_for_each_start(&qrtr_ports, index, ipc, 1) { |
|---|
| 603 | 731 | sock_hold(&ipc->sk); |
|---|
| 604 | 732 | ipc->sk.sk_err = ENETRESET; |
|---|
| 605 | 733 | ipc->sk.sk_error_report(&ipc->sk); |
|---|
| 606 | 734 | sock_put(&ipc->sk); |
|---|
| 607 | 735 | } |
|---|
| 608 | | - mutex_unlock(&qrtr_port_lock); |
|---|
| 736 | + rcu_read_unlock(); |
|---|
| 609 | 737 | } |
|---|
| 610 | 738 | |
|---|
| 611 | 739 | /* Bind socket to address. |
|---|
| .. | .. |
|---|
| 690 | 818 | |
|---|
| 691 | 819 | ipc = qrtr_port_lookup(to->sq_port); |
|---|
| 692 | 820 | if (!ipc || &ipc->sk == skb->sk) { /* do not send to self */ |
|---|
| 821 | + if (ipc) |
|---|
| 822 | + qrtr_port_put(ipc); |
|---|
| 693 | 823 | kfree_skb(skb); |
|---|
| 694 | 824 | return -ENODEV; |
|---|
| 695 | 825 | } |
|---|
| .. | .. |
|---|
| 736 | 866 | DECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, msg->msg_name); |
|---|
| 737 | 867 | int (*enqueue_fn)(struct qrtr_node *, struct sk_buff *, int, |
|---|
| 738 | 868 | struct sockaddr_qrtr *, struct sockaddr_qrtr *); |
|---|
| 869 | + __le32 qrtr_type = cpu_to_le32(QRTR_TYPE_DATA); |
|---|
| 739 | 870 | struct qrtr_sock *ipc = qrtr_sk(sock->sk); |
|---|
| 740 | 871 | struct sock *sk = sock->sk; |
|---|
| 741 | 872 | struct qrtr_node *node; |
|---|
| 742 | 873 | struct sk_buff *skb; |
|---|
| 743 | 874 | size_t plen; |
|---|
| 744 | | - u32 type = QRTR_TYPE_DATA; |
|---|
| 875 | + u32 type; |
|---|
| 745 | 876 | int rc; |
|---|
| 746 | 877 | |
|---|
| 747 | 878 | if (msg->msg_flags & ~(MSG_DONTWAIT)) |
|---|
| .. | .. |
|---|
| 818 | 949 | } |
|---|
| 819 | 950 | |
|---|
| 820 | 951 | /* control messages already require the type as 'command' */ |
|---|
| 821 | | - skb_copy_bits(skb, 0, &type, 4); |
|---|
| 822 | | - type = le32_to_cpu(type); |
|---|
| 952 | + skb_copy_bits(skb, 0, &qrtr_type, 4); |
|---|
| 823 | 953 | } |
|---|
| 824 | 954 | |
|---|
| 955 | + type = le32_to_cpu(qrtr_type); |
|---|
| 825 | 956 | rc = enqueue_fn(node, skb, type, &ipc->us, addr); |
|---|
| 826 | 957 | if (rc >= 0) |
|---|
| 827 | 958 | rc = len; |
|---|
| .. | .. |
|---|
| 831 | 962 | release_sock(sk); |
|---|
| 832 | 963 | |
|---|
| 833 | 964 | return rc; |
|---|
| 965 | +} |
|---|
| 966 | + |
|---|
| 967 | +static int qrtr_send_resume_tx(struct qrtr_cb *cb) |
|---|
| 968 | +{ |
|---|
| 969 | + struct sockaddr_qrtr remote = { AF_QIPCRTR, cb->src_node, cb->src_port }; |
|---|
| 970 | + struct sockaddr_qrtr local = { AF_QIPCRTR, cb->dst_node, cb->dst_port }; |
|---|
| 971 | + struct qrtr_ctrl_pkt *pkt; |
|---|
| 972 | + struct qrtr_node *node; |
|---|
| 973 | + struct sk_buff *skb; |
|---|
| 974 | + int ret; |
|---|
| 975 | + |
|---|
| 976 | + node = qrtr_node_lookup(remote.sq_node); |
|---|
| 977 | + if (!node) |
|---|
| 978 | + return -EINVAL; |
|---|
| 979 | + |
|---|
| 980 | + skb = qrtr_alloc_ctrl_packet(&pkt); |
|---|
| 981 | + if (!skb) |
|---|
| 982 | + return -ENOMEM; |
|---|
| 983 | + |
|---|
| 984 | + pkt->cmd = cpu_to_le32(QRTR_TYPE_RESUME_TX); |
|---|
| 985 | + pkt->client.node = cpu_to_le32(cb->dst_node); |
|---|
| 986 | + pkt->client.port = cpu_to_le32(cb->dst_port); |
|---|
| 987 | + |
|---|
| 988 | + ret = qrtr_node_enqueue(node, skb, QRTR_TYPE_RESUME_TX, &local, &remote); |
|---|
| 989 | + |
|---|
| 990 | + qrtr_node_release(node); |
|---|
| 991 | + |
|---|
| 992 | + return ret; |
|---|
| 834 | 993 | } |
|---|
| 835 | 994 | |
|---|
| 836 | 995 | static int qrtr_recvmsg(struct socket *sock, struct msghdr *msg, |
|---|
| .. | .. |
|---|
| 855 | 1014 | release_sock(sk); |
|---|
| 856 | 1015 | return rc; |
|---|
| 857 | 1016 | } |
|---|
| 1017 | + cb = (struct qrtr_cb *)skb->cb; |
|---|
| 858 | 1018 | |
|---|
| 859 | 1019 | copied = skb->len; |
|---|
| 860 | 1020 | if (copied > size) { |
|---|
| .. | .. |
|---|
| 873 | 1033 | */ |
|---|
| 874 | 1034 | memset(addr, 0, sizeof(*addr)); |
|---|
| 875 | 1035 | |
|---|
| 876 | | - cb = (struct qrtr_cb *)skb->cb; |
|---|
| 877 | 1036 | addr->sq_family = AF_QIPCRTR; |
|---|
| 878 | 1037 | addr->sq_node = cb->src_node; |
|---|
| 879 | 1038 | addr->sq_port = cb->src_port; |
|---|
| .. | .. |
|---|
| 881 | 1040 | } |
|---|
| 882 | 1041 | |
|---|
| 883 | 1042 | out: |
|---|
| 1043 | + if (cb->confirm_rx) |
|---|
| 1044 | + qrtr_send_resume_tx(cb); |
|---|
| 1045 | + |
|---|
| 884 | 1046 | skb_free_datagram(sk, skb); |
|---|
| 885 | 1047 | release_sock(sk); |
|---|
| 886 | 1048 | |
|---|
| .. | .. |
|---|
| 984 | 1146 | break; |
|---|
| 985 | 1147 | } |
|---|
| 986 | 1148 | break; |
|---|
| 987 | | - case SIOCGSTAMP: |
|---|
| 988 | | - rc = sock_get_timestamp(sk, argp); |
|---|
| 989 | | - break; |
|---|
| 990 | 1149 | case SIOCADDRT: |
|---|
| 991 | 1150 | case SIOCDELRT: |
|---|
| 992 | 1151 | case SIOCSIFADDR: |
|---|
| .. | .. |
|---|
| 1050 | 1209 | .recvmsg = qrtr_recvmsg, |
|---|
| 1051 | 1210 | .getname = qrtr_getname, |
|---|
| 1052 | 1211 | .ioctl = qrtr_ioctl, |
|---|
| 1212 | + .gettstamp = sock_gettstamp, |
|---|
| 1053 | 1213 | .poll = datagram_poll, |
|---|
| 1054 | 1214 | .shutdown = sock_no_shutdown, |
|---|
| 1055 | | - .setsockopt = sock_no_setsockopt, |
|---|
| 1056 | | - .getsockopt = sock_no_getsockopt, |
|---|
| 1057 | 1215 | .release = qrtr_release, |
|---|
| 1058 | 1216 | .mmap = sock_no_mmap, |
|---|
| 1059 | 1217 | .sendpage = sock_no_sendpage, |
|---|
| .. | .. |
|---|
| 1091 | 1249 | return 0; |
|---|
| 1092 | 1250 | } |
|---|
| 1093 | 1251 | |
|---|
| 1094 | | -static const struct nla_policy qrtr_policy[IFA_MAX + 1] = { |
|---|
| 1095 | | - [IFA_LOCAL] = { .type = NLA_U32 }, |
|---|
| 1096 | | -}; |
|---|
| 1097 | | - |
|---|
| 1098 | | -static int qrtr_addr_doit(struct sk_buff *skb, struct nlmsghdr *nlh, |
|---|
| 1099 | | - struct netlink_ext_ack *extack) |
|---|
| 1100 | | -{ |
|---|
| 1101 | | - struct nlattr *tb[IFA_MAX + 1]; |
|---|
| 1102 | | - struct ifaddrmsg *ifm; |
|---|
| 1103 | | - int rc; |
|---|
| 1104 | | - |
|---|
| 1105 | | - if (!netlink_capable(skb, CAP_NET_ADMIN)) |
|---|
| 1106 | | - return -EPERM; |
|---|
| 1107 | | - |
|---|
| 1108 | | - if (!netlink_capable(skb, CAP_SYS_ADMIN)) |
|---|
| 1109 | | - return -EPERM; |
|---|
| 1110 | | - |
|---|
| 1111 | | - ASSERT_RTNL(); |
|---|
| 1112 | | - |
|---|
| 1113 | | - rc = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, qrtr_policy, extack); |
|---|
| 1114 | | - if (rc < 0) |
|---|
| 1115 | | - return rc; |
|---|
| 1116 | | - |
|---|
| 1117 | | - ifm = nlmsg_data(nlh); |
|---|
| 1118 | | - if (!tb[IFA_LOCAL]) |
|---|
| 1119 | | - return -EINVAL; |
|---|
| 1120 | | - |
|---|
| 1121 | | - qrtr_local_nid = nla_get_u32(tb[IFA_LOCAL]); |
|---|
| 1122 | | - return 0; |
|---|
| 1123 | | -} |
|---|
| 1124 | | - |
|---|
| 1125 | 1252 | static const struct net_proto_family qrtr_family = { |
|---|
| 1126 | 1253 | .owner = THIS_MODULE, |
|---|
| 1127 | 1254 | .family = AF_QIPCRTR, |
|---|
| .. | .. |
|---|
| 1142 | 1269 | return rc; |
|---|
| 1143 | 1270 | } |
|---|
| 1144 | 1271 | |
|---|
| 1145 | | - rc = rtnl_register_module(THIS_MODULE, PF_QIPCRTR, RTM_NEWADDR, qrtr_addr_doit, NULL, 0); |
|---|
| 1146 | | - if (rc) { |
|---|
| 1147 | | - sock_unregister(qrtr_family.family); |
|---|
| 1148 | | - proto_unregister(&qrtr_proto); |
|---|
| 1149 | | - } |
|---|
| 1272 | + qrtr_ns_init(); |
|---|
| 1150 | 1273 | |
|---|
| 1151 | 1274 | return rc; |
|---|
| 1152 | 1275 | } |
|---|
| .. | .. |
|---|
| 1154 | 1277 | |
|---|
| 1155 | 1278 | static void __exit qrtr_proto_fini(void) |
|---|
| 1156 | 1279 | { |
|---|
| 1157 | | - rtnl_unregister(PF_QIPCRTR, RTM_NEWADDR); |
|---|
| 1280 | + qrtr_ns_remove(); |
|---|
| 1158 | 1281 | sock_unregister(qrtr_family.family); |
|---|
| 1159 | 1282 | proto_unregister(&qrtr_proto); |
|---|
| 1160 | 1283 | } |
|---|