Merge branch 'mailbox-for-next' of git://git.linaro.org/landing-teams/working/fujitsu...
[cascardo/linux.git] / drivers / staging / lustre / lnet / klnds / socklnd / socklnd_lib.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2012, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #include "socklnd.h"
34
35 int
36 ksocknal_lib_get_conn_addrs(struct ksock_conn *conn)
37 {
38         int rc = lnet_sock_getaddr(conn->ksnc_sock, 1, &conn->ksnc_ipaddr,
39                                    &conn->ksnc_port);
40
41         /* Didn't need the {get,put}connsock dance to deref ksnc_sock... */
42         LASSERT(!conn->ksnc_closing);
43
44         if (rc) {
45                 CERROR("Error %d getting sock peer IP\n", rc);
46                 return rc;
47         }
48
49         rc = lnet_sock_getaddr(conn->ksnc_sock, 0, &conn->ksnc_myipaddr, NULL);
50         if (rc) {
51                 CERROR("Error %d getting sock local IP\n", rc);
52                 return rc;
53         }
54
55         return 0;
56 }
57
58 int
59 ksocknal_lib_zc_capable(struct ksock_conn *conn)
60 {
61         int caps = conn->ksnc_sock->sk->sk_route_caps;
62
63         if (conn->ksnc_proto == &ksocknal_protocol_v1x)
64                 return 0;
65
66         /*
67          * ZC if the socket supports scatter/gather and doesn't need software
68          * checksums
69          */
70         return ((caps & NETIF_F_SG) && (caps & NETIF_F_CSUM_MASK));
71 }
72
73 int
74 ksocknal_lib_send_iov(struct ksock_conn *conn, struct ksock_tx *tx)
75 {
76         struct socket *sock = conn->ksnc_sock;
77         int nob;
78         int rc;
79
80         if (*ksocknal_tunables.ksnd_enable_csum && /* checksum enabled */
81             conn->ksnc_proto == &ksocknal_protocol_v2x && /* V2.x connection  */
82             tx->tx_nob == tx->tx_resid           && /* frist sending    */
83             !tx->tx_msg.ksm_csum)                    /* not checksummed  */
84                 ksocknal_lib_csum_tx(tx);
85
86         /*
87          * NB we can't trust socket ops to either consume our iovs
88          * or leave them alone.
89          */
90         {
91 #if SOCKNAL_SINGLE_FRAG_TX
92                 struct kvec scratch;
93                 struct kvec *scratchiov = &scratch;
94                 unsigned int niov = 1;
95 #else
96                 struct kvec *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
97                 unsigned int niov = tx->tx_niov;
98 #endif
99                 struct msghdr msg = {.msg_flags = MSG_DONTWAIT};
100                 int i;
101
102                 for (nob = i = 0; i < niov; i++) {
103                         scratchiov[i] = tx->tx_iov[i];
104                         nob += scratchiov[i].iov_len;
105                 }
106
107                 if (!list_empty(&conn->ksnc_tx_queue) ||
108                     nob < tx->tx_resid)
109                         msg.msg_flags |= MSG_MORE;
110
111                 rc = kernel_sendmsg(sock, &msg, scratchiov, niov, nob);
112         }
113         return rc;
114 }
115
116 int
117 ksocknal_lib_send_kiov(struct ksock_conn *conn, struct ksock_tx *tx)
118 {
119         struct socket *sock = conn->ksnc_sock;
120         lnet_kiov_t *kiov = tx->tx_kiov;
121         int rc;
122         int nob;
123
124         /* Not NOOP message */
125         LASSERT(tx->tx_lnetmsg);
126
127         /*
128          * NB we can't trust socket ops to either consume our iovs
129          * or leave them alone.
130          */
131         if (tx->tx_msg.ksm_zc_cookies[0]) {
132                 /* Zero copy is enabled */
133                 struct sock *sk = sock->sk;
134                 struct page *page = kiov->kiov_page;
135                 int offset = kiov->kiov_offset;
136                 int fragsize = kiov->kiov_len;
137                 int msgflg = MSG_DONTWAIT;
138
139                 CDEBUG(D_NET, "page %p + offset %x for %d\n",
140                        page, offset, kiov->kiov_len);
141
142                 if (!list_empty(&conn->ksnc_tx_queue) ||
143                     fragsize < tx->tx_resid)
144                         msgflg |= MSG_MORE;
145
146                 if (sk->sk_prot->sendpage) {
147                         rc = sk->sk_prot->sendpage(sk, page,
148                                                    offset, fragsize, msgflg);
149                 } else {
150                         rc = tcp_sendpage(sk, page, offset, fragsize, msgflg);
151                 }
152         } else {
153 #if SOCKNAL_SINGLE_FRAG_TX || !SOCKNAL_RISK_KMAP_DEADLOCK
154                 struct kvec scratch;
155                 struct kvec *scratchiov = &scratch;
156                 unsigned int niov = 1;
157 #else
158 #ifdef CONFIG_HIGHMEM
159 #warning "XXX risk of kmap deadlock on multiple frags..."
160 #endif
161                 struct kvec *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
162                 unsigned int niov = tx->tx_nkiov;
163 #endif
164                 struct msghdr msg = {.msg_flags = MSG_DONTWAIT};
165                 int i;
166
167                 for (nob = i = 0; i < niov; i++) {
168                         scratchiov[i].iov_base = kmap(kiov[i].kiov_page) +
169                                                  kiov[i].kiov_offset;
170                         nob += scratchiov[i].iov_len = kiov[i].kiov_len;
171                 }
172
173                 if (!list_empty(&conn->ksnc_tx_queue) ||
174                     nob < tx->tx_resid)
175                         msg.msg_flags |= MSG_MORE;
176
177                 rc = kernel_sendmsg(sock, &msg, (struct kvec *)scratchiov, niov, nob);
178
179                 for (i = 0; i < niov; i++)
180                         kunmap(kiov[i].kiov_page);
181         }
182         return rc;
183 }
184
185 void
186 ksocknal_lib_eager_ack(struct ksock_conn *conn)
187 {
188         int opt = 1;
189         struct socket *sock = conn->ksnc_sock;
190
191         /*
192          * Remind the socket to ACK eagerly.  If I don't, the socket might
193          * think I'm about to send something it could piggy-back the ACK
194          * on, introducing delay in completing zero-copy sends in my
195          * peer.
196          */
197         kernel_setsockopt(sock, SOL_TCP, TCP_QUICKACK, (char *)&opt,
198                           sizeof(opt));
199 }
200
201 int
202 ksocknal_lib_recv_iov(struct ksock_conn *conn)
203 {
204 #if SOCKNAL_SINGLE_FRAG_RX
205         struct kvec scratch;
206         struct kvec *scratchiov = &scratch;
207         unsigned int niov = 1;
208 #else
209         struct kvec *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
210         unsigned int niov = conn->ksnc_rx_niov;
211 #endif
212         struct kvec *iov = conn->ksnc_rx_iov;
213         struct msghdr msg = {
214                 .msg_flags = 0
215         };
216         int nob;
217         int i;
218         int rc;
219         int fragnob;
220         int sum;
221         __u32 saved_csum;
222
223         /*
224          * NB we can't trust socket ops to either consume our iovs
225          * or leave them alone.
226          */
227         LASSERT(niov > 0);
228
229         for (nob = i = 0; i < niov; i++) {
230                 scratchiov[i] = iov[i];
231                 nob += scratchiov[i].iov_len;
232         }
233         LASSERT(nob <= conn->ksnc_rx_nob_wanted);
234
235         rc = kernel_recvmsg(conn->ksnc_sock, &msg, scratchiov, niov, nob,
236                             MSG_DONTWAIT);
237
238         saved_csum = 0;
239         if (conn->ksnc_proto == &ksocknal_protocol_v2x) {
240                 saved_csum = conn->ksnc_msg.ksm_csum;
241                 conn->ksnc_msg.ksm_csum = 0;
242         }
243
244         if (saved_csum) {
245                 /* accumulate checksum */
246                 for (i = 0, sum = rc; sum > 0; i++, sum -= fragnob) {
247                         LASSERT(i < niov);
248
249                         fragnob = iov[i].iov_len;
250                         if (fragnob > sum)
251                                 fragnob = sum;
252
253                         conn->ksnc_rx_csum = ksocknal_csum(conn->ksnc_rx_csum,
254                                                            iov[i].iov_base, fragnob);
255                 }
256                 conn->ksnc_msg.ksm_csum = saved_csum;
257         }
258
259         return rc;
260 }
261
262 static void
263 ksocknal_lib_kiov_vunmap(void *addr)
264 {
265         if (!addr)
266                 return;
267
268         vunmap(addr);
269 }
270
271 static void *
272 ksocknal_lib_kiov_vmap(lnet_kiov_t *kiov, int niov,
273                        struct kvec *iov, struct page **pages)
274 {
275         void *addr;
276         int nob;
277         int i;
278
279         if (!*ksocknal_tunables.ksnd_zc_recv || !pages)
280                 return NULL;
281
282         LASSERT(niov <= LNET_MAX_IOV);
283
284         if (niov < 2 ||
285             niov < *ksocknal_tunables.ksnd_zc_recv_min_nfrags)
286                 return NULL;
287
288         for (nob = i = 0; i < niov; i++) {
289                 if ((kiov[i].kiov_offset && i > 0) ||
290                     (kiov[i].kiov_offset + kiov[i].kiov_len != PAGE_SIZE && i < niov - 1))
291                         return NULL;
292
293                 pages[i] = kiov[i].kiov_page;
294                 nob += kiov[i].kiov_len;
295         }
296
297         addr = vmap(pages, niov, VM_MAP, PAGE_KERNEL);
298         if (!addr)
299                 return NULL;
300
301         iov->iov_base = addr + kiov[0].kiov_offset;
302         iov->iov_len = nob;
303
304         return addr;
305 }
306
307 int
308 ksocknal_lib_recv_kiov(struct ksock_conn *conn)
309 {
310 #if SOCKNAL_SINGLE_FRAG_RX || !SOCKNAL_RISK_KMAP_DEADLOCK
311         struct kvec scratch;
312         struct kvec *scratchiov = &scratch;
313         struct page **pages = NULL;
314         unsigned int niov = 1;
315 #else
316 #ifdef CONFIG_HIGHMEM
317 #warning "XXX risk of kmap deadlock on multiple frags..."
318 #endif
319         struct kvec *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
320         struct page **pages = conn->ksnc_scheduler->kss_rx_scratch_pgs;
321         unsigned int niov = conn->ksnc_rx_nkiov;
322 #endif
323         lnet_kiov_t   *kiov = conn->ksnc_rx_kiov;
324         struct msghdr msg = {
325                 .msg_flags = 0
326         };
327         int nob;
328         int i;
329         int rc;
330         void *base;
331         void *addr;
332         int sum;
333         int fragnob;
334         int n;
335
336         /*
337          * NB we can't trust socket ops to either consume our iovs
338          * or leave them alone.
339          */
340         addr = ksocknal_lib_kiov_vmap(kiov, niov, scratchiov, pages);
341         if (addr) {
342                 nob = scratchiov[0].iov_len;
343                 n = 1;
344
345         } else {
346                 for (nob = i = 0; i < niov; i++) {
347                         nob += scratchiov[i].iov_len = kiov[i].kiov_len;
348                         scratchiov[i].iov_base = kmap(kiov[i].kiov_page) +
349                                                  kiov[i].kiov_offset;
350                 }
351                 n = niov;
352         }
353
354         LASSERT(nob <= conn->ksnc_rx_nob_wanted);
355
356         rc = kernel_recvmsg(conn->ksnc_sock, &msg, (struct kvec *)scratchiov,
357                             n, nob, MSG_DONTWAIT);
358
359         if (conn->ksnc_msg.ksm_csum) {
360                 for (i = 0, sum = rc; sum > 0; i++, sum -= fragnob) {
361                         LASSERT(i < niov);
362
363                         /*
364                          * Dang! have to kmap again because I have nowhere to
365                          * stash the mapped address.  But by doing it while the
366                          * page is still mapped, the kernel just bumps the map
367                          * count and returns me the address it stashed.
368                          */
369                         base = kmap(kiov[i].kiov_page) + kiov[i].kiov_offset;
370                         fragnob = kiov[i].kiov_len;
371                         if (fragnob > sum)
372                                 fragnob = sum;
373
374                         conn->ksnc_rx_csum = ksocknal_csum(conn->ksnc_rx_csum,
375                                                            base, fragnob);
376
377                         kunmap(kiov[i].kiov_page);
378                 }
379         }
380
381         if (addr) {
382                 ksocknal_lib_kiov_vunmap(addr);
383         } else {
384                 for (i = 0; i < niov; i++)
385                         kunmap(kiov[i].kiov_page);
386         }
387
388         return rc;
389 }
390
391 void
392 ksocknal_lib_csum_tx(struct ksock_tx *tx)
393 {
394         int i;
395         __u32 csum;
396         void *base;
397
398         LASSERT(tx->tx_iov[0].iov_base == &tx->tx_msg);
399         LASSERT(tx->tx_conn);
400         LASSERT(tx->tx_conn->ksnc_proto == &ksocknal_protocol_v2x);
401
402         tx->tx_msg.ksm_csum = 0;
403
404         csum = ksocknal_csum(~0, tx->tx_iov[0].iov_base,
405                              tx->tx_iov[0].iov_len);
406
407         if (tx->tx_kiov) {
408                 for (i = 0; i < tx->tx_nkiov; i++) {
409                         base = kmap(tx->tx_kiov[i].kiov_page) +
410                                tx->tx_kiov[i].kiov_offset;
411
412                         csum = ksocknal_csum(csum, base, tx->tx_kiov[i].kiov_len);
413
414                         kunmap(tx->tx_kiov[i].kiov_page);
415                 }
416         } else {
417                 for (i = 1; i < tx->tx_niov; i++)
418                         csum = ksocknal_csum(csum, tx->tx_iov[i].iov_base,
419                                              tx->tx_iov[i].iov_len);
420         }
421
422         if (*ksocknal_tunables.ksnd_inject_csum_error) {
423                 csum++;
424                 *ksocknal_tunables.ksnd_inject_csum_error = 0;
425         }
426
427         tx->tx_msg.ksm_csum = csum;
428 }
429
430 int
431 ksocknal_lib_get_conn_tunables(struct ksock_conn *conn, int *txmem, int *rxmem, int *nagle)
432 {
433         struct socket *sock = conn->ksnc_sock;
434         int len;
435         int rc;
436
437         rc = ksocknal_connsock_addref(conn);
438         if (rc) {
439                 LASSERT(conn->ksnc_closing);
440                 *txmem = *rxmem = *nagle = 0;
441                 return -ESHUTDOWN;
442         }
443
444         rc = lnet_sock_getbuf(sock, txmem, rxmem);
445         if (!rc) {
446                 len = sizeof(*nagle);
447                 rc = kernel_getsockopt(sock, SOL_TCP, TCP_NODELAY,
448                                        (char *)nagle, &len);
449         }
450
451         ksocknal_connsock_decref(conn);
452
453         if (!rc)
454                 *nagle = !*nagle;
455         else
456                 *txmem = *rxmem = *nagle = 0;
457
458         return rc;
459 }
460
461 int
462 ksocknal_lib_setup_sock(struct socket *sock)
463 {
464         int rc;
465         int option;
466         int keep_idle;
467         int keep_intvl;
468         int keep_count;
469         int do_keepalive;
470         struct linger linger;
471
472         sock->sk->sk_allocation = GFP_NOFS;
473
474         /*
475          * Ensure this socket aborts active sends immediately when we close
476          * it.
477          */
478         linger.l_onoff = 0;
479         linger.l_linger = 0;
480
481         rc = kernel_setsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger,
482                                sizeof(linger));
483         if (rc) {
484                 CERROR("Can't set SO_LINGER: %d\n", rc);
485                 return rc;
486         }
487
488         option = -1;
489         rc = kernel_setsockopt(sock, SOL_TCP, TCP_LINGER2, (char *)&option,
490                                sizeof(option));
491         if (rc) {
492                 CERROR("Can't set SO_LINGER2: %d\n", rc);
493                 return rc;
494         }
495
496         if (!*ksocknal_tunables.ksnd_nagle) {
497                 option = 1;
498
499                 rc = kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY,
500                                        (char *)&option, sizeof(option));
501                 if (rc) {
502                         CERROR("Can't disable nagle: %d\n", rc);
503                         return rc;
504                 }
505         }
506
507         rc = lnet_sock_setbuf(sock, *ksocknal_tunables.ksnd_tx_buffer_size,
508                               *ksocknal_tunables.ksnd_rx_buffer_size);
509         if (rc) {
510                 CERROR("Can't set buffer tx %d, rx %d buffers: %d\n",
511                        *ksocknal_tunables.ksnd_tx_buffer_size,
512                        *ksocknal_tunables.ksnd_rx_buffer_size, rc);
513                 return rc;
514         }
515
516 /* TCP_BACKOFF_* sockopt tunables unsupported in stock kernels */
517
518         /* snapshot tunables */
519         keep_idle  = *ksocknal_tunables.ksnd_keepalive_idle;
520         keep_count = *ksocknal_tunables.ksnd_keepalive_count;
521         keep_intvl = *ksocknal_tunables.ksnd_keepalive_intvl;
522
523         do_keepalive = (keep_idle > 0 && keep_count > 0 && keep_intvl > 0);
524
525         option = (do_keepalive ? 1 : 0);
526         rc = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&option,
527                                sizeof(option));
528         if (rc) {
529                 CERROR("Can't set SO_KEEPALIVE: %d\n", rc);
530                 return rc;
531         }
532
533         if (!do_keepalive)
534                 return 0;
535
536         rc = kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, (char *)&keep_idle,
537                                sizeof(keep_idle));
538         if (rc) {
539                 CERROR("Can't set TCP_KEEPIDLE: %d\n", rc);
540                 return rc;
541         }
542
543         rc = kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
544                                (char *)&keep_intvl, sizeof(keep_intvl));
545         if (rc) {
546                 CERROR("Can't set TCP_KEEPINTVL: %d\n", rc);
547                 return rc;
548         }
549
550         rc = kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT, (char *)&keep_count,
551                                sizeof(keep_count));
552         if (rc) {
553                 CERROR("Can't set TCP_KEEPCNT: %d\n", rc);
554                 return rc;
555         }
556
557         return 0;
558 }
559
560 void
561 ksocknal_lib_push_conn(struct ksock_conn *conn)
562 {
563         struct sock *sk;
564         struct tcp_sock *tp;
565         int nonagle;
566         int val = 1;
567         int rc;
568
569         rc = ksocknal_connsock_addref(conn);
570         if (rc)                     /* being shut down */
571                 return;
572
573         sk = conn->ksnc_sock->sk;
574         tp = tcp_sk(sk);
575
576         lock_sock(sk);
577         nonagle = tp->nonagle;
578         tp->nonagle = 1;
579         release_sock(sk);
580
581         rc = kernel_setsockopt(conn->ksnc_sock, SOL_TCP, TCP_NODELAY,
582                                (char *)&val, sizeof(val));
583         LASSERT(!rc);
584
585         lock_sock(sk);
586         tp->nonagle = nonagle;
587         release_sock(sk);
588
589         ksocknal_connsock_decref(conn);
590 }
591
592 /*
593  * socket call back in Linux
594  */
595 static void
596 ksocknal_data_ready(struct sock *sk)
597 {
598         struct ksock_conn *conn;
599
600         /* interleave correctly with closing sockets... */
601         LASSERT(!in_irq());
602         read_lock(&ksocknal_data.ksnd_global_lock);
603
604         conn = sk->sk_user_data;
605         if (!conn) {         /* raced with ksocknal_terminate_conn */
606                 LASSERT(sk->sk_data_ready != &ksocknal_data_ready);
607                 sk->sk_data_ready(sk);
608         } else {
609                 ksocknal_read_callback(conn);
610         }
611
612         read_unlock(&ksocknal_data.ksnd_global_lock);
613 }
614
615 static void
616 ksocknal_write_space(struct sock *sk)
617 {
618         struct ksock_conn *conn;
619         int wspace;
620         int min_wpace;
621
622         /* interleave correctly with closing sockets... */
623         LASSERT(!in_irq());
624         read_lock(&ksocknal_data.ksnd_global_lock);
625
626         conn = sk->sk_user_data;
627         wspace = sk_stream_wspace(sk);
628         min_wpace = sk_stream_min_wspace(sk);
629
630         CDEBUG(D_NET, "sk %p wspace %d low water %d conn %p%s%s%s\n",
631                sk, wspace, min_wpace, conn,
632                !conn ? "" : (conn->ksnc_tx_ready ?
633                                       " ready" : " blocked"),
634                !conn ? "" : (conn->ksnc_tx_scheduled ?
635                                       " scheduled" : " idle"),
636                !conn ? "" : (list_empty(&conn->ksnc_tx_queue) ?
637                                       " empty" : " queued"));
638
639         if (!conn) {         /* raced with ksocknal_terminate_conn */
640                 LASSERT(sk->sk_write_space != &ksocknal_write_space);
641                 sk->sk_write_space(sk);
642
643                 read_unlock(&ksocknal_data.ksnd_global_lock);
644                 return;
645         }
646
647         if (wspace >= min_wpace) {            /* got enough space */
648                 ksocknal_write_callback(conn);
649
650                 /*
651                  * Clear SOCK_NOSPACE _after_ ksocknal_write_callback so the
652                  * ENOMEM check in ksocknal_transmit is race-free (think about
653                  * it).
654                  */
655                 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
656         }
657
658         read_unlock(&ksocknal_data.ksnd_global_lock);
659 }
660
661 void
662 ksocknal_lib_save_callback(struct socket *sock, struct ksock_conn *conn)
663 {
664         conn->ksnc_saved_data_ready = sock->sk->sk_data_ready;
665         conn->ksnc_saved_write_space = sock->sk->sk_write_space;
666 }
667
668 void
669 ksocknal_lib_set_callback(struct socket *sock,  struct ksock_conn *conn)
670 {
671         sock->sk->sk_user_data = conn;
672         sock->sk->sk_data_ready = ksocknal_data_ready;
673         sock->sk->sk_write_space = ksocknal_write_space;
674 }
675
676 void
677 ksocknal_lib_reset_callback(struct socket *sock, struct ksock_conn *conn)
678 {
679         /*
680          * Remove conn's network callbacks.
681          * NB I _have_ to restore the callback, rather than storing a noop,
682          * since the socket could survive past this module being unloaded!!
683          */
684         sock->sk->sk_data_ready = conn->ksnc_saved_data_ready;
685         sock->sk->sk_write_space = conn->ksnc_saved_write_space;
686
687         /*
688          * A callback could be in progress already; they hold a read lock
689          * on ksnd_global_lock (to serialise with me) and NOOP if
690          * sk_user_data is NULL.
691          */
692         sock->sk->sk_user_data = NULL;
693 }
694
695 int
696 ksocknal_lib_memory_pressure(struct ksock_conn *conn)
697 {
698         int rc = 0;
699         struct ksock_sched *sched;
700
701         sched = conn->ksnc_scheduler;
702         spin_lock_bh(&sched->kss_lock);
703
704         if (!test_bit(SOCK_NOSPACE, &conn->ksnc_sock->flags) &&
705             !conn->ksnc_tx_ready) {
706                 /*
707                  * SOCK_NOSPACE is set when the socket fills
708                  * and cleared in the write_space callback
709                  * (which also sets ksnc_tx_ready).  If
710                  * SOCK_NOSPACE and ksnc_tx_ready are BOTH
711                  * zero, I didn't fill the socket and
712                  * write_space won't reschedule me, so I
713                  * return -ENOMEM to get my caller to retry
714                  * after a timeout
715                  */
716                 rc = -ENOMEM;
717         }
718
719         spin_unlock_bh(&sched->kss_lock);
720
721         return rc;
722 }