6565#define MAX_SEND_MSG_COUNT 25
6666#define DLM_SHUTDOWN_WAIT_TIMEOUT msecs_to_jiffies(10000)
6767
68- struct cbuf {
69- unsigned int base ;
70- unsigned int len ;
71- unsigned int mask ;
72- };
73-
74- static void cbuf_add (struct cbuf * cb , int n )
75- {
76- cb -> len += n ;
77- }
78-
79- static int cbuf_data (struct cbuf * cb )
80- {
81- return ((cb -> base + cb -> len ) & cb -> mask );
82- }
83-
84- static void cbuf_init (struct cbuf * cb , int size )
85- {
86- cb -> base = cb -> len = 0 ;
87- cb -> mask = size - 1 ;
88- }
89-
90- static void cbuf_eat (struct cbuf * cb , int n )
91- {
92- cb -> len -= n ;
93- cb -> base += n ;
94- cb -> base &= cb -> mask ;
95- }
96-
97- static bool cbuf_empty (struct cbuf * cb )
98- {
99- return cb -> len == 0 ;
100- }
101-
10268struct connection {
10369 struct socket * sock ; /* NULL if not connected */
10470 uint32_t nodeid ; /* So we know who we are in the list */
@@ -117,15 +83,16 @@ struct connection {
11783 int (* rx_action ) (struct connection * ); /* What to do when active */
11884 void (* connect_action ) (struct connection * ); /* What to do to connect */
11985 void (* shutdown_action )(struct connection * con ); /* What to do to shutdown */
120- struct page * rx_page ;
121- struct cbuf cb ;
12286 int retries ;
12387#define MAX_CONNECT_RETRIES 3
12488 struct hlist_node list ;
12589 struct connection * othercon ;
12690 struct work_struct rwork ; /* Receive workqueue */
12791 struct work_struct swork ; /* Send workqueue */
12892 wait_queue_head_t shutdown_wait ; /* wait for graceful shutdown */
93+ unsigned char * rx_buf ;
94+ int rx_buflen ;
95+ int rx_leftover ;
12996 struct rcu_head rcu ;
13097};
13198#define sock2con (x ) ((struct connection *)(x)->sk_user_data)
@@ -219,6 +186,13 @@ static struct connection *nodeid2con(int nodeid, gfp_t alloc)
219186 if (!con )
220187 return NULL ;
221188
189+ con -> rx_buflen = dlm_config .ci_buffer_size ;
190+ con -> rx_buf = kmalloc (con -> rx_buflen , GFP_NOFS );
191+ if (!con -> rx_buf ) {
192+ kfree (con );
193+ return NULL ;
194+ }
195+
222196 con -> nodeid = nodeid ;
223197 mutex_init (& con -> sock_mutex );
224198 INIT_LIST_HEAD (& con -> writequeue );
@@ -613,11 +587,8 @@ static void close_connection(struct connection *con, bool and_other,
613587 /* Will only re-enter once. */
614588 close_connection (con -> othercon , false, true, true);
615589 }
616- if (con -> rx_page ) {
617- __free_page (con -> rx_page );
618- con -> rx_page = NULL ;
619- }
620590
591+ con -> rx_leftover = 0 ;
621592 con -> retries = 0 ;
622593 mutex_unlock (& con -> sock_mutex );
623594 clear_bit (CF_CLOSING , & con -> flags );
@@ -671,88 +642,89 @@ static void dlm_tcp_shutdown(struct connection *con)
671642 shutdown_connection (con );
672643}
673644
645+ static int con_realloc_receive_buf (struct connection * con , int newlen )
646+ {
647+ unsigned char * newbuf ;
648+
649+ newbuf = kmalloc (newlen , GFP_NOFS );
650+ if (!newbuf )
651+ return - ENOMEM ;
652+
653+ /* copy any leftover from last receive */
654+ if (con -> rx_leftover )
655+ memmove (newbuf , con -> rx_buf , con -> rx_leftover );
656+
657+ /* swap to new buffer space */
658+ kfree (con -> rx_buf );
659+ con -> rx_buflen = newlen ;
660+ con -> rx_buf = newbuf ;
661+
662+ return 0 ;
663+ }
664+
674665/* Data received from remote end */
675666static int receive_from_sock (struct connection * con )
676667{
677- int ret = 0 ;
678- struct msghdr msg = {};
679- struct kvec iov [2 ];
680- unsigned len ;
681- int r ;
682668 int call_again_soon = 0 ;
683- int nvec ;
669+ struct msghdr msg ;
670+ struct kvec iov ;
671+ int ret , buflen ;
684672
685673 mutex_lock (& con -> sock_mutex );
686674
687675 if (con -> sock == NULL ) {
688676 ret = - EAGAIN ;
689677 goto out_close ;
690678 }
679+
691680 if (con -> nodeid == 0 ) {
692681 ret = - EINVAL ;
693682 goto out_close ;
694683 }
695684
696- if (con -> rx_page == NULL ) {
697- /*
698- * This doesn't need to be atomic, but I think it should
699- * improve performance if it is.
700- */
701- con -> rx_page = alloc_page (GFP_ATOMIC );
702- if (con -> rx_page == NULL )
685+ /* realloc if we get new buffer size to read out */
686+ buflen = dlm_config .ci_buffer_size ;
687+ if (con -> rx_buflen != buflen && con -> rx_leftover <= buflen ) {
688+ ret = con_realloc_receive_buf (con , buflen );
689+ if (ret < 0 )
703690 goto out_resched ;
704- cbuf_init (& con -> cb , PAGE_SIZE );
705691 }
706692
707- /*
708- * iov[0] is the bit of the circular buffer between the current end
709- * point (cb.base + cb.len) and the end of the buffer.
693+ /* calculate new buffer parameter regarding last receive and
694+ * possible leftover bytes
710695 */
711- iov [0 ].iov_len = con -> cb .base - cbuf_data (& con -> cb );
712- iov [0 ].iov_base = page_address (con -> rx_page ) + cbuf_data (& con -> cb );
713- iov [1 ].iov_len = 0 ;
714- nvec = 1 ;
696+ iov .iov_base = con -> rx_buf + con -> rx_leftover ;
697+ iov .iov_len = con -> rx_buflen - con -> rx_leftover ;
715698
716- /*
717- * iov[1] is the bit of the circular buffer between the start of the
718- * buffer and the start of the currently used section (cb.base)
719- */
720- if (cbuf_data (& con -> cb ) >= con -> cb .base ) {
721- iov [0 ].iov_len = PAGE_SIZE - cbuf_data (& con -> cb );
722- iov [1 ].iov_len = con -> cb .base ;
723- iov [1 ].iov_base = page_address (con -> rx_page );
724- nvec = 2 ;
725- }
726- len = iov [0 ].iov_len + iov [1 ].iov_len ;
727- iov_iter_kvec (& msg .msg_iter , READ , iov , nvec , len );
728-
729- r = ret = sock_recvmsg (con -> sock , & msg , MSG_DONTWAIT | MSG_NOSIGNAL );
699+ memset (& msg , 0 , sizeof (msg ));
700+ msg .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL ;
701+ ret = kernel_recvmsg (con -> sock , & msg , & iov , 1 , iov .iov_len ,
702+ msg .msg_flags );
730703 if (ret <= 0 )
731704 goto out_close ;
732- else if (ret == len )
705+ else if (ret == iov . iov_len )
733706 call_again_soon = 1 ;
734707
735- cbuf_add (& con -> cb , ret );
736- ret = dlm_process_incoming_buffer (con -> nodeid ,
737- page_address (con -> rx_page ),
738- con -> cb .base , con -> cb .len ,
739- PAGE_SIZE );
740- if (ret < 0 ) {
741- log_print ("lowcomms err %d: addr=%p, base=%u, len=%u, read=%d" ,
742- ret , page_address (con -> rx_page ), con -> cb .base ,
743- con -> cb .len , r );
744- cbuf_eat (& con -> cb , r );
745- } else {
746- cbuf_eat (& con -> cb , ret );
747- }
708+ /* new buflen according readed bytes and leftover from last receive */
709+ buflen = ret + con -> rx_leftover ;
710+ ret = dlm_process_incoming_buffer (con -> nodeid , con -> rx_buf , buflen );
711+ if (ret < 0 )
712+ goto out_close ;
748713
749- if (cbuf_empty (& con -> cb ) && !call_again_soon ) {
750- __free_page (con -> rx_page );
751- con -> rx_page = NULL ;
714+ /* calculate leftover bytes from process and put it into begin of
715+ * the receive buffer, so next receive we have the full message
716+ * at the start address of the receive buffer.
717+ */
718+ con -> rx_leftover = buflen - ret ;
719+ if (con -> rx_leftover ) {
720+ memmove (con -> rx_buf , con -> rx_buf + ret ,
721+ con -> rx_leftover );
722+ call_again_soon = true;
752723 }
753724
754725 if (call_again_soon )
755726 goto out_resched ;
727+
756728 mutex_unlock (& con -> sock_mutex );
757729 return 0 ;
758730
@@ -854,6 +826,17 @@ static int accept_from_sock(struct connection *con)
854826 result = - ENOMEM ;
855827 goto accept_err ;
856828 }
829+
830+ othercon -> rx_buflen = dlm_config .ci_buffer_size ;
831+ othercon -> rx_buf = kmalloc (othercon -> rx_buflen , GFP_NOFS );
832+ if (!othercon -> rx_buf ) {
833+ mutex_unlock (& newcon -> sock_mutex );
834+ kfree (othercon );
835+ log_print ("failed to allocate incoming socket receive buffer" );
836+ result = - ENOMEM ;
837+ goto accept_err ;
838+ }
839+
857840 othercon -> nodeid = nodeid ;
858841 othercon -> rx_action = receive_from_sock ;
859842 mutex_init (& othercon -> sock_mutex );
@@ -1603,6 +1586,14 @@ static void shutdown_conn(struct connection *con)
16031586 con -> shutdown_action (con );
16041587}
16051588
1589+ static void connection_release (struct rcu_head * rcu )
1590+ {
1591+ struct connection * con = container_of (rcu , struct connection , rcu );
1592+
1593+ kfree (con -> rx_buf );
1594+ kfree (con );
1595+ }
1596+
16061597static void free_conn (struct connection * con )
16071598{
16081599 close_connection (con , true, true, true);
@@ -1611,10 +1602,10 @@ static void free_conn(struct connection *con)
16111602 spin_unlock (& connections_lock );
16121603 if (con -> othercon ) {
16131604 clean_one_writequeue (con -> othercon );
1614- kfree_rcu ( con -> othercon , rcu );
1605+ call_rcu ( & con -> othercon -> rcu , connection_release );
16151606 }
16161607 clean_one_writequeue (con );
1617- kfree_rcu ( con , rcu );
1608+ call_rcu ( & con -> rcu , connection_release );
16181609}
16191610
16201611static void work_flush (void )
0 commit comments