Skip to contentSkip to conversation

smb: smbdirect/client/server: moving to common functions and smbdirect.ko

PATCH Superseded Stefan Metzmacher 3 participants 134 messages Oct 29, 2025 – Nov 5, 2025 Page 2 of 2

Subsystems (3)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
author reviewer
#95 lore raw

Re: [PATCH v2 091/127] smb: client: make use of smbdirect.ko

In reply to kernel test robot

Hi test robot,

can you retest with this branch?
git fetch https://git.samba.org/ksmbd.git ksmbd-for-next-next

Thanks!
metze

Am 31.10.25 um 12:41 schrieb kernel test robot:

Quoted 382 lines

Hi Stefan,

kernel test robot noticed the following build errors:

[auto build test ERROR on linus/master]
[also build test ERROR on v6.18-rc3 next-20251030]
[cannot apply to cifs/for-next brauner-vfs/vfs.all v6.18-rc3 v6.18-rc2 v6.18-rc1] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Stefan-Metzmacher/smb-smbdirect-let-smbdirect-h-include-include-linux-types-h/20251030-015100
base:   linus/master
patch link:    https://lore.kernel.org/r/8ae722e2bc80de69e330baa49102011572eb100b.1761742839.git.metze%40samba.org
patch subject: [PATCH v2 091/127] smb: client: make use of smbdirect.ko
config: sparc-randconfig-001-20251030 (https://download.01.org/0day-ci/archive/20251031/[email protected]/config)
compiler: sparc-linux-gcc (GCC) 8.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251031/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from fs/smb/common/smbdirect/smbdirect_internal.h:17,
from fs/smb/common/smbdirect/smbdirect_connect.c:6:

Quoted 1 line
Quoted 1 line

fs/smb/common/smbdirect/smbdirect_socket.h:191:4: error: unknown type name 'mempool_t'

        mempool_t *pool;
        ^~~~~~~~~
    fs/smb/common/smbdirect/smbdirect_socket.h:246:4: error: unknown type name 'mempool_t'
        mempool_t *pool;
        ^~~~~~~~~

--
In file included from fs/smb/common/smbdirect/smbdirect_internal.h:17,
from fs/smb/common/smbdirect/smbdirect_connection.c:7:

Quoted 1 line
Quoted 1 line

fs/smb/common/smbdirect/smbdirect_socket.h:191:4: error: unknown type name 'mempool_t'

        mempool_t *pool;
        ^~~~~~~~~
    fs/smb/common/smbdirect/smbdirect_socket.h:246:4: error: unknown type name 'mempool_t'
        mempool_t *pool;
        ^~~~~~~~~
    fs/smb/common/smbdirect/smbdirect_connection.c: In function 'smbdirect_connection_create_mem_pools':
Quoted 1 line
Quoted 1 line

fs/smb/common/smbdirect/smbdirect_connection.c:702:25: error: implicit declaration of function 'mempool_create_slab_pool'; did you mean 'proc_create_seq_data'? [-Werror=implicit-function-declaration]

      sc->send_io.mem.pool = mempool_create_slab_pool(sp->send_credit_target,
                             ^~~~~~~~~~~~~~~~~~~~~~~~
                             proc_create_seq_data
    fs/smb/common/smbdirect/smbdirect_connection.c:702:23: warning: assignment to 'int *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
      sc->send_io.mem.pool = mempool_create_slab_pool(sp->send_credit_target,
                           ^
    fs/smb/common/smbdirect/smbdirect_connection.c:731:23: warning: assignment to 'int *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
      sc->recv_io.mem.pool = mempool_create_slab_pool(sp->recv_credit_max,
                           ^
Quoted 1 line
Quoted 1 line

fs/smb/common/smbdirect/smbdirect_connection.c:739:13: error: implicit declaration of function 'mempool_alloc'; did you mean 'mm_alloc'? [-Werror=implicit-function-declaration]

       recv_io = mempool_alloc(sc->recv_io.mem.pool,
                 ^~~~~~~~~~~~~
                 mm_alloc
    fs/smb/common/smbdirect/smbdirect_connection.c:739:11: warning: assignment to 'struct smbdirect_recv_io *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
       recv_io = mempool_alloc(sc->recv_io.mem.pool,
               ^
    fs/smb/common/smbdirect/smbdirect_connection.c: In function 'smbdirect_connection_destroy_mem_pools':
Quoted 1 line
Quoted 1 line

fs/smb/common/smbdirect/smbdirect_connection.c:761:3: error: implicit declaration of function 'mempool_free'; did you mean 'reqsk_free'? [-Werror=implicit-function-declaration]

       mempool_free(recv_io, sc->recv_io.mem.pool);
       ^~~~~~~~~~~~
       reqsk_free
Quoted 1 line
Quoted 1 line

fs/smb/common/smbdirect/smbdirect_connection.c:769:2: error: implicit declaration of function 'mempool_destroy'; did you mean 'ib_mr_pool_destroy'? [-Werror=implicit-function-declaration]

      mempool_destroy(sc->recv_io.mem.pool);
      ^~~~~~~~~~~~~~~
      ib_mr_pool_destroy
    fs/smb/common/smbdirect/smbdirect_connection.c: In function 'smbdirect_connection_alloc_send_io':
    fs/smb/common/smbdirect/smbdirect_connection.c:787:6: warning: assignment to 'struct smbdirect_send_io *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
      msg = mempool_alloc(sc->send_io.mem.pool, sc->send_io.mem.gfp_mask);
          ^
    cc1: some warnings being treated as errors

vim +/mempool_t +191 fs/smb/common/smbdirect/smbdirect_socket.h

89bae05f9df68f8 Stefan Metzmacher 2025-08-15   95
22234e37d7e9765 Stefan Metzmacher 2025-05-28   96  struct smbdirect_socket {
22234e37d7e9765 Stefan Metzmacher 2025-05-28   97  	enum smbdirect_socket_status status;
371d3ab51ce44c8 Stefan Metzmacher 2025-08-08   98  	wait_queue_head_t status_wait;
f5b893edde7bcd1 Stefan Metzmacher 2025-08-28   99  	int first_error;
22234e37d7e9765 Stefan Metzmacher 2025-05-28  100
6c5b0f9253d7f15 Stefan Metzmacher 2025-08-22  101  	/*
6c5b0f9253d7f15 Stefan Metzmacher 2025-08-22  102  	 * This points to the workqueue to
6c5b0f9253d7f15 Stefan Metzmacher 2025-08-22  103  	 * be used for this socket.
6c5b0f9253d7f15 Stefan Metzmacher 2025-08-22  104  	 * It can be per socket (on the client)
6c5b0f9253d7f15 Stefan Metzmacher 2025-08-22  105  	 * or point to a global workqueue (on the server)
6c5b0f9253d7f15 Stefan Metzmacher 2025-08-22  106  	 */
6c5b0f9253d7f15 Stefan Metzmacher 2025-08-22  107  	struct workqueue_struct *workqueue;
6c5b0f9253d7f15 Stefan Metzmacher 2025-08-22  108
de32f33c0365d06 Stefan Metzmacher 2025-08-08  109  	struct work_struct disconnect_work;
de32f33c0365d06 Stefan Metzmacher 2025-08-08  110
f23812679f6587e Stefan Metzmacher 2025-10-29  111  	/*
f23812679f6587e Stefan Metzmacher 2025-10-29  112  	 * The reference counts.
f23812679f6587e Stefan Metzmacher 2025-10-29  113  	 */
f23812679f6587e Stefan Metzmacher 2025-10-29  114  	struct {
f23812679f6587e Stefan Metzmacher 2025-10-29  115  		/*
f23812679f6587e Stefan Metzmacher 2025-10-29  116  		 * This holds the references by the
f23812679f6587e Stefan Metzmacher 2025-10-29  117  		 * frontend, typically the smb layer.
f23812679f6587e Stefan Metzmacher 2025-10-29  118  		 *
f23812679f6587e Stefan Metzmacher 2025-10-29  119  		 * It is typically 1 and a disconnect
f23812679f6587e Stefan Metzmacher 2025-10-29  120  		 * will happen if it reaches 0.
f23812679f6587e Stefan Metzmacher 2025-10-29  121  		 */
f23812679f6587e Stefan Metzmacher 2025-10-29  122  		struct kref disconnect;
f23812679f6587e Stefan Metzmacher 2025-10-29  123
f23812679f6587e Stefan Metzmacher 2025-10-29  124  		/*
f23812679f6587e Stefan Metzmacher 2025-10-29  125  		 * This holds the reference by the
f23812679f6587e Stefan Metzmacher 2025-10-29  126  		 * backend, the code that manages
f23812679f6587e Stefan Metzmacher 2025-10-29  127  		 * the lifetime of the whole
f23812679f6587e Stefan Metzmacher 2025-10-29  128  		 * struct smbdirect_socket,
f23812679f6587e Stefan Metzmacher 2025-10-29  129  		 * if this reaches 0 it can will
f23812679f6587e Stefan Metzmacher 2025-10-29  130  		 * be freed.
f23812679f6587e Stefan Metzmacher 2025-10-29  131  		 *
f23812679f6587e Stefan Metzmacher 2025-10-29  132  		 * Can be REFCOUNT_MAX is part
f23812679f6587e Stefan Metzmacher 2025-10-29  133  		 * of another structure.
f23812679f6587e Stefan Metzmacher 2025-10-29  134  		 *
f23812679f6587e Stefan Metzmacher 2025-10-29  135  		 * This is equal or higher than
f23812679f6587e Stefan Metzmacher 2025-10-29  136  		 * the disconnect refcount.
f23812679f6587e Stefan Metzmacher 2025-10-29  137  		 */
f23812679f6587e Stefan Metzmacher 2025-10-29  138  		struct kref destroy;
f23812679f6587e Stefan Metzmacher 2025-10-29  139  	} refs;
f23812679f6587e Stefan Metzmacher 2025-10-29  140
22234e37d7e9765 Stefan Metzmacher 2025-05-28  141  	/* RDMA related */
22234e37d7e9765 Stefan Metzmacher 2025-05-28  142  	struct {
22234e37d7e9765 Stefan Metzmacher 2025-05-28  143  		struct rdma_cm_id *cm_id;
beda261c142cc7b Stefan Metzmacher 2025-10-29  144  		/*
beda261c142cc7b Stefan Metzmacher 2025-10-29  145  		 * The expected event in our current
beda261c142cc7b Stefan Metzmacher 2025-10-29  146  		 * cm_id->event_handler, all other events
beda261c142cc7b Stefan Metzmacher 2025-10-29  147  		 * are treated as an error.
beda261c142cc7b Stefan Metzmacher 2025-10-29  148  		 */
beda261c142cc7b Stefan Metzmacher 2025-10-29  149  		enum rdma_cm_event_type expected_event;
2baedb2026a5c01 Stefan Metzmacher 2025-08-21  150  		/*
2baedb2026a5c01 Stefan Metzmacher 2025-08-21  151  		 * This is for iWarp MPA v1
2baedb2026a5c01 Stefan Metzmacher 2025-08-21  152  		 */
2baedb2026a5c01 Stefan Metzmacher 2025-08-21  153  		bool legacy_iwarp;
22234e37d7e9765 Stefan Metzmacher 2025-05-28  154  	} rdma;
22234e37d7e9765 Stefan Metzmacher 2025-05-28  155
22234e37d7e9765 Stefan Metzmacher 2025-05-28  156  	/* IB verbs related */
22234e37d7e9765 Stefan Metzmacher 2025-05-28  157  	struct {
22234e37d7e9765 Stefan Metzmacher 2025-05-28  158  		struct ib_pd *pd;
2a871f3e3b060fa Stefan Metzmacher 2025-10-29  159  		enum ib_poll_context poll_ctx;
22234e37d7e9765 Stefan Metzmacher 2025-05-28  160  		struct ib_cq *send_cq;
22234e37d7e9765 Stefan Metzmacher 2025-05-28  161  		struct ib_cq *recv_cq;
22234e37d7e9765 Stefan Metzmacher 2025-05-28  162
22234e37d7e9765 Stefan Metzmacher 2025-05-28  163  		/*
22234e37d7e9765 Stefan Metzmacher 2025-05-28  164  		 * shortcuts for rdma.cm_id->{qp,device};
22234e37d7e9765 Stefan Metzmacher 2025-05-28  165  		 */
22234e37d7e9765 Stefan Metzmacher 2025-05-28  166  		struct ib_qp *qp;
22234e37d7e9765 Stefan Metzmacher 2025-05-28  167  		struct ib_device *dev;
22234e37d7e9765 Stefan Metzmacher 2025-05-28  168  	} ib;
dce8047f4725d44 Stefan Metzmacher 2025-05-28  169
dce8047f4725d44 Stefan Metzmacher 2025-05-28  170  	struct smbdirect_socket_parameters parameters;
33dd53a90e3419e Stefan Metzmacher 2025-08-05  171
89bae05f9df68f8 Stefan Metzmacher 2025-08-15  172  	/*
89bae05f9df68f8 Stefan Metzmacher 2025-08-15  173  	 * The state for keepalive and timeout handling
89bae05f9df68f8 Stefan Metzmacher 2025-08-15  174  	 */
89bae05f9df68f8 Stefan Metzmacher 2025-08-15  175  	struct {
89bae05f9df68f8 Stefan Metzmacher 2025-08-15  176  		enum smbdirect_keepalive_status keepalive;
89bae05f9df68f8 Stefan Metzmacher 2025-08-15  177  		struct work_struct immediate_work;
89bae05f9df68f8 Stefan Metzmacher 2025-08-15  178  		struct delayed_work timer_work;
89bae05f9df68f8 Stefan Metzmacher 2025-08-15  179  	} idle;
89bae05f9df68f8 Stefan Metzmacher 2025-08-15  180
5ef8278e3734a88 Stefan Metzmacher 2025-08-06  181  	/*
5ef8278e3734a88 Stefan Metzmacher 2025-08-06  182  	 * The state for posted send buffers
5ef8278e3734a88 Stefan Metzmacher 2025-08-06  183  	 */
5ef8278e3734a88 Stefan Metzmacher 2025-08-06  184  	struct {
5ef8278e3734a88 Stefan Metzmacher 2025-08-06  185  		/*
5ef8278e3734a88 Stefan Metzmacher 2025-08-06  186  		 * Memory pools for preallocating
5ef8278e3734a88 Stefan Metzmacher 2025-08-06  187  		 * smbdirect_send_io buffers
5ef8278e3734a88 Stefan Metzmacher 2025-08-06  188  		 */
5ef8278e3734a88 Stefan Metzmacher 2025-08-06  189  		struct {
5ef8278e3734a88 Stefan Metzmacher 2025-08-06  190  			struct kmem_cache *cache;
5ef8278e3734a88 Stefan Metzmacher 2025-08-06 @191  			mempool_t *pool;
932070bf508d8ad Stefan Metzmacher 2025-10-29  192  			gfp_t gfp_mask;
5ef8278e3734a88 Stefan Metzmacher 2025-08-06  193  		} mem;
3b7be44a0eb18b2 Stefan Metzmacher 2025-08-11  194
2191b3471df53d8 Stefan Metzmacher 2025-08-11  195  		/*
68335cbcddcd586 Stefan Metzmacher 2025-10-20  196  		 * The local credit state for ib_post_send()
68335cbcddcd586 Stefan Metzmacher 2025-10-20  197  		 */
68335cbcddcd586 Stefan Metzmacher 2025-10-20  198  		struct {
68335cbcddcd586 Stefan Metzmacher 2025-10-20  199  			atomic_t count;
68335cbcddcd586 Stefan Metzmacher 2025-10-20  200  			wait_queue_head_t wait_queue;
68335cbcddcd586 Stefan Metzmacher 2025-10-20  201  		} lcredits;
68335cbcddcd586 Stefan Metzmacher 2025-10-20  202
68335cbcddcd586 Stefan Metzmacher 2025-10-20  203  		/*
68335cbcddcd586 Stefan Metzmacher 2025-10-20  204  		 * The remote credit state for the send side
2191b3471df53d8 Stefan Metzmacher 2025-08-11  205  		 */
2191b3471df53d8 Stefan Metzmacher 2025-08-11  206  		struct {
2191b3471df53d8 Stefan Metzmacher 2025-08-11  207  			atomic_t count;
2191b3471df53d8 Stefan Metzmacher 2025-08-11  208  			wait_queue_head_t wait_queue;
2191b3471df53d8 Stefan Metzmacher 2025-08-11  209  		} credits;
2191b3471df53d8 Stefan Metzmacher 2025-08-11  210
3b7be44a0eb18b2 Stefan Metzmacher 2025-08-11  211  		/*
3b7be44a0eb18b2 Stefan Metzmacher 2025-08-11  212  		 * The state about posted/pending sends
3b7be44a0eb18b2 Stefan Metzmacher 2025-08-11  213  		 */
3b7be44a0eb18b2 Stefan Metzmacher 2025-08-11  214  		struct {
3b7be44a0eb18b2 Stefan Metzmacher 2025-08-11  215  			atomic_t count;
3b7be44a0eb18b2 Stefan Metzmacher 2025-08-11  216  			/*
3b7be44a0eb18b2 Stefan Metzmacher 2025-08-11  217  			 * woken when count is decremented
3b7be44a0eb18b2 Stefan Metzmacher 2025-08-11  218  			 */
3b7be44a0eb18b2 Stefan Metzmacher 2025-08-11  219  			wait_queue_head_t dec_wait_queue;
3b7be44a0eb18b2 Stefan Metzmacher 2025-08-11  220  			/*
3b7be44a0eb18b2 Stefan Metzmacher 2025-08-11  221  			 * woken when count reached zero
3b7be44a0eb18b2 Stefan Metzmacher 2025-08-11  222  			 */
3b7be44a0eb18b2 Stefan Metzmacher 2025-08-11  223  			wait_queue_head_t zero_wait_queue;
3b7be44a0eb18b2 Stefan Metzmacher 2025-08-11  224  		} pending;
5ef8278e3734a88 Stefan Metzmacher 2025-08-06  225  	} send_io;
5ef8278e3734a88 Stefan Metzmacher 2025-08-06  226
33dd53a90e3419e Stefan Metzmacher 2025-08-05  227  	/*
33dd53a90e3419e Stefan Metzmacher 2025-08-05  228  	 * The state for posted receive buffers
33dd53a90e3419e Stefan Metzmacher 2025-08-05  229  	 */
33dd53a90e3419e Stefan Metzmacher 2025-08-05  230  	struct {
33dd53a90e3419e Stefan Metzmacher 2025-08-05  231  		/*
33dd53a90e3419e Stefan Metzmacher 2025-08-05  232  		 * The type of PDU we are expecting
33dd53a90e3419e Stefan Metzmacher 2025-08-05  233  		 */
33dd53a90e3419e Stefan Metzmacher 2025-08-05  234  		enum {
33dd53a90e3419e Stefan Metzmacher 2025-08-05  235  			SMBDIRECT_EXPECT_NEGOTIATE_REQ = 1,
33dd53a90e3419e Stefan Metzmacher 2025-08-05  236  			SMBDIRECT_EXPECT_NEGOTIATE_REP = 2,
33dd53a90e3419e Stefan Metzmacher 2025-08-05  237  			SMBDIRECT_EXPECT_DATA_TRANSFER = 3,
33dd53a90e3419e Stefan Metzmacher 2025-08-05  238  		} expected;
d0df32a3025c2de Stefan Metzmacher 2025-08-05  239
5ef8278e3734a88 Stefan Metzmacher 2025-08-06  240  		/*
5ef8278e3734a88 Stefan Metzmacher 2025-08-06  241  		 * Memory pools for preallocating
5ef8278e3734a88 Stefan Metzmacher 2025-08-06  242  		 * smbdirect_recv_io buffers
5ef8278e3734a88 Stefan Metzmacher 2025-08-06  243  		 */
5ef8278e3734a88 Stefan Metzmacher 2025-08-06  244  		struct {
5ef8278e3734a88 Stefan Metzmacher 2025-08-06  245  			struct kmem_cache *cache;
5ef8278e3734a88 Stefan Metzmacher 2025-08-06  246  			mempool_t *pool;
932070bf508d8ad Stefan Metzmacher 2025-10-29  247  			gfp_t gfp_mask;
5ef8278e3734a88 Stefan Metzmacher 2025-08-06  248  		} mem;
5ef8278e3734a88 Stefan Metzmacher 2025-08-06  249
d0df32a3025c2de Stefan Metzmacher 2025-08-05  250  		/*
d0df32a3025c2de Stefan Metzmacher 2025-08-05  251  		 * The list of free smbdirect_recv_io
d0df32a3025c2de Stefan Metzmacher 2025-08-05  252  		 * structures
d0df32a3025c2de Stefan Metzmacher 2025-08-05  253  		 */
d0df32a3025c2de Stefan Metzmacher 2025-08-05  254  		struct {
d0df32a3025c2de Stefan Metzmacher 2025-08-05  255  			struct list_head list;
d0df32a3025c2de Stefan Metzmacher 2025-08-05  256  			spinlock_t lock;
d0df32a3025c2de Stefan Metzmacher 2025-08-05  257  		} free;
b7ffb4d2a036004 Stefan Metzmacher 2025-08-05  258
ec76e3cc6bbcb17 Stefan Metzmacher 2025-08-11  259  		/*
ec76e3cc6bbcb17 Stefan Metzmacher 2025-08-11  260  		 * The state for posted recv_io messages
ec76e3cc6bbcb17 Stefan Metzmacher 2025-08-11  261  		 * and the refill work struct.
ec76e3cc6bbcb17 Stefan Metzmacher 2025-08-11  262  		 */
ec76e3cc6bbcb17 Stefan Metzmacher 2025-08-11  263  		struct {
ec76e3cc6bbcb17 Stefan Metzmacher 2025-08-11  264  			atomic_t count;
ec76e3cc6bbcb17 Stefan Metzmacher 2025-08-11  265  			struct work_struct refill_work;
ec76e3cc6bbcb17 Stefan Metzmacher 2025-08-11  266  		} posted;
ec76e3cc6bbcb17 Stefan Metzmacher 2025-08-11  267
ec76e3cc6bbcb17 Stefan Metzmacher 2025-08-11  268  		/*
ec76e3cc6bbcb17 Stefan Metzmacher 2025-08-11  269  		 * The credit state for the recv side
ec76e3cc6bbcb17 Stefan Metzmacher 2025-08-11  270  		 */
ec76e3cc6bbcb17 Stefan Metzmacher 2025-08-11  271  		struct {
ec76e3cc6bbcb17 Stefan Metzmacher 2025-08-11  272  			u16 target;
ec76e3cc6bbcb17 Stefan Metzmacher 2025-08-11  273  			atomic_t count;
ec76e3cc6bbcb17 Stefan Metzmacher 2025-08-11  274  		} credits;
ec76e3cc6bbcb17 Stefan Metzmacher 2025-08-11  275
b7ffb4d2a036004 Stefan Metzmacher 2025-08-05  276  		/*
b7ffb4d2a036004 Stefan Metzmacher 2025-08-05  277  		 * The list of arrived non-empty smbdirect_recv_io
b7ffb4d2a036004 Stefan Metzmacher 2025-08-05  278  		 * structures
b7ffb4d2a036004 Stefan Metzmacher 2025-08-05  279  		 *
b7ffb4d2a036004 Stefan Metzmacher 2025-08-05  280  		 * This represents the reassembly queue.
b7ffb4d2a036004 Stefan Metzmacher 2025-08-05  281  		 */
b7ffb4d2a036004 Stefan Metzmacher 2025-08-05  282  		struct {
b7ffb4d2a036004 Stefan Metzmacher 2025-08-05  283  			struct list_head list;
b7ffb4d2a036004 Stefan Metzmacher 2025-08-05  284  			spinlock_t lock;
b7ffb4d2a036004 Stefan Metzmacher 2025-08-05  285  			wait_queue_head_t wait_queue;
b7ffb4d2a036004 Stefan Metzmacher 2025-08-05  286  			/* total data length of reassembly queue */
b7ffb4d2a036004 Stefan Metzmacher 2025-08-05  287  			int data_length;
b7ffb4d2a036004 Stefan Metzmacher 2025-08-05  288  			int queue_length;
b7ffb4d2a036004 Stefan Metzmacher 2025-08-05  289  			/* the offset to first buffer in reassembly queue */
b7ffb4d2a036004 Stefan Metzmacher 2025-08-05  290  			int first_entry_offset;
b7ffb4d2a036004 Stefan Metzmacher 2025-08-05  291  			/*
b7ffb4d2a036004 Stefan Metzmacher 2025-08-05  292  			 * Indicate if we have received a full packet on the
b7ffb4d2a036004 Stefan Metzmacher 2025-08-05  293  			 * connection This is used to identify the first SMBD
b7ffb4d2a036004 Stefan Metzmacher 2025-08-05  294  			 * packet of a assembled payload (SMB packet) in
b7ffb4d2a036004 Stefan Metzmacher 2025-08-05  295  			 * reassembly queue so we can return a RFC1002 length to
b7ffb4d2a036004 Stefan Metzmacher 2025-08-05  296  			 * upper layer to indicate the length of the SMB packet
b7ffb4d2a036004 Stefan Metzmacher 2025-08-05  297  			 * received
b7ffb4d2a036004 Stefan Metzmacher 2025-08-05  298  			 */
b7ffb4d2a036004 Stefan Metzmacher 2025-08-05  299  			bool full_packet_received;
b7ffb4d2a036004 Stefan Metzmacher 2025-08-05  300  		} reassembly;
33dd53a90e3419e Stefan Metzmacher 2025-08-05  301  	} recv_io;
90255ed46774bda Stefan Metzmacher 2025-08-14  302
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  303  	/*
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  304  	 * The state for Memory registrations on the client
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  305  	 */
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  306  	struct {
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  307  		enum ib_mr_type type;
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  308
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  309  		/*
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  310  		 * The list of free smbdirect_mr_io
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  311  		 * structures
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  312  		 */
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  313  		struct {
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  314  			struct list_head list;
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  315  			spinlock_t lock;
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  316  		} all;
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  317
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  318  		/*
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  319  		 * The number of available MRs ready for memory registration
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  320  		 */
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  321  		struct {
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  322  			atomic_t count;
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  323  			wait_queue_head_t wait_queue;
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  324  		} ready;
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  325
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  326  		/*
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  327  		 * The number of used MRs
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  328  		 */
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  329  		struct {
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  330  			atomic_t count;
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  331  		} used;
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  332
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  333  		struct work_struct recovery_work;
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  334
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  335  		/* Used by transport to wait until all MRs are returned */
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  336  		struct {
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  337  			wait_queue_head_t wait_queue;
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  338  		} cleanup;
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  339  	} mr_io;
ed3350f09d2c023 Stefan Metzmacher 2025-08-19  340
90255ed46774bda Stefan Metzmacher 2025-08-14  341  	/*
90255ed46774bda Stefan Metzmacher 2025-08-14  342  	 * The state for RDMA read/write requests on the server
90255ed46774bda Stefan Metzmacher 2025-08-14  343  	 */
90255ed46774bda Stefan Metzmacher 2025-08-14  344  	struct {
dbd035a139e7e0a Stefan Metzmacher 2025-10-29  345  		/*
dbd035a139e7e0a Stefan Metzmacher 2025-10-29  346  		 * Memory hints for
dbd035a139e7e0a Stefan Metzmacher 2025-10-29  347  		 * smbdirect_rw_io structs
dbd035a139e7e0a Stefan Metzmacher 2025-10-29  348  		 */
dbd035a139e7e0a Stefan Metzmacher 2025-10-29  349  		struct {
dbd035a139e7e0a Stefan Metzmacher 2025-10-29  350  			gfp_t gfp_mask;
dbd035a139e7e0a Stefan Metzmacher 2025-10-29  351  		} mem;
dbd035a139e7e0a Stefan Metzmacher 2025-10-29  352
90255ed46774bda Stefan Metzmacher 2025-08-14  353  		/*
90255ed46774bda Stefan Metzmacher 2025-08-14  354  		 * The credit state for the send side
90255ed46774bda Stefan Metzmacher 2025-08-14  355  		 */
90255ed46774bda Stefan Metzmacher 2025-08-14  356  		struct {
90255ed46774bda Stefan Metzmacher 2025-08-14  357  			/*
90255ed46774bda Stefan Metzmacher 2025-08-14  358  			 * The maximum number of rw credits
90255ed46774bda Stefan Metzmacher 2025-08-14  359  			 */
90255ed46774bda Stefan Metzmacher 2025-08-14  360  			size_t max;
90255ed46774bda Stefan Metzmacher 2025-08-14  361  			/*
90255ed46774bda Stefan Metzmacher 2025-08-14  362  			 * The number of pages per credit
90255ed46774bda Stefan Metzmacher 2025-08-14  363  			 */
90255ed46774bda Stefan Metzmacher 2025-08-14  364  			size_t num_pages;
90255ed46774bda Stefan Metzmacher 2025-08-14  365  			atomic_t count;
90255ed46774bda Stefan Metzmacher 2025-08-14  366  			wait_queue_head_t wait_queue;
90255ed46774bda Stefan Metzmacher 2025-08-14  367  		} credits;
90255ed46774bda Stefan Metzmacher 2025-08-14  368  	} rw_io;
6889d2f5caa69ff Stefan Metzmacher 2025-08-19  369
6889d2f5caa69ff Stefan Metzmacher 2025-08-19  370  	/*
6889d2f5caa69ff Stefan Metzmacher 2025-08-19  371  	 * For debug purposes
6889d2f5caa69ff Stefan Metzmacher 2025-08-19  372  	 */
6889d2f5caa69ff Stefan Metzmacher 2025-08-19  373  	struct {
6889d2f5caa69ff Stefan Metzmacher 2025-08-19  374  		u64 get_receive_buffer;
6889d2f5caa69ff Stefan Metzmacher 2025-08-19  375  		u64 put_receive_buffer;
6889d2f5caa69ff Stefan Metzmacher 2025-08-19  376  		u64 enqueue_reassembly_queue;
6889d2f5caa69ff Stefan Metzmacher 2025-08-19  377  		u64 dequeue_reassembly_queue;
6889d2f5caa69ff Stefan Metzmacher 2025-08-19  378  		u64 send_empty;
6889d2f5caa69ff Stefan Metzmacher 2025-08-19  379  	} statistics;
8ddb25e3ca878f2 Stefan Metzmacher 2025-10-29  380
8ddb25e3ca878f2 Stefan Metzmacher 2025-10-29  381  	struct {
8ddb25e3ca878f2 Stefan Metzmacher 2025-10-29  382  		void *private_ptr;
8ddb25e3ca878f2 Stefan Metzmacher 2025-10-29  383  		bool (*needed)(struct smbdirect_socket *sc,
8ddb25e3ca878f2 Stefan Metzmacher 2025-10-29  384  			       void *private_ptr,
8ddb25e3ca878f2 Stefan Metzmacher 2025-10-29  385  			       unsigned int lvl,
8ddb25e3ca878f2 Stefan Metzmacher 2025-10-29  386  			       unsigned int cls);
8ddb25e3ca878f2 Stefan Metzmacher 2025-10-29  387  		void (*vaprintf)(struct smbdirect_socket *sc,
8ddb25e3ca878f2 Stefan Metzmacher 2025-10-29  388  				 const char *func,
8ddb25e3ca878f2 Stefan Metzmacher 2025-10-29  389  				 unsigned int line,
8ddb25e3ca878f2 Stefan Metzmacher 2025-10-29  390  				 void *private_ptr,
8ddb25e3ca878f2 Stefan Metzmacher 2025-10-29  391  				 unsigned int lvl,
8ddb25e3ca878f2 Stefan Metzmacher 2025-10-29  392  				 unsigned int cls,
8ddb25e3ca878f2 Stefan Metzmacher 2025-10-29  393  				 struct va_format *vaf);
8ddb25e3ca878f2 Stefan Metzmacher 2025-10-29  394  	} logging;
22234e37d7e9765 Stefan Metzmacher 2025-05-28  395  };
22234e37d7e9765 Stefan Metzmacher 2025-05-28  396
author reviewer
#96 lore raw

PATCH v2 92/127 smb: server: make use of smbdirect_socket_prepare_create()

In reply to Stefan Metzmacher

This prepares the use of functions from smbdirect_connection.c.

---
 fs/smb/server/transport_rdma.c | 28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)
fs/smb/server/transport_rdma.c +17 -11
index 9bf023b797ad..bcc584884f29 100644
@@ -397,20 +397,14 @@ static struct smb_direct_transport *alloc_transport(struct rdma_cm_id *cm_id)
 {
 	struct smb_direct_transport *t;
 	struct smbdirect_socket *sc;
+	struct smbdirect_socket_parameters init_params = {};
 	struct smbdirect_socket_parameters *sp;
 	struct ksmbd_conn *conn;
 
-	t = kzalloc(sizeof(*t), KSMBD_DEFAULT_GFP);
-	if (!t)
-		return NULL;
-	sc = &t->socket;
-	smbdirect_socket_init(sc);
-	sp = &sc->parameters;
-
-	sc->workqueue = smb_direct_wq;
-
-	INIT_WORK(&sc->disconnect_work, smb_direct_disconnect_rdma_work);
-
+	/*
+	 * Create the initial parameters
+	 */
+	sp = &init_params;
 	sp->negotiate_timeout_msec = SMB_DIRECT_NEGOTIATE_TIMEOUT * 1000;
 	sp->initiator_depth = SMB_DIRECT_CM_INITIATOR_DEPTH;
 	sp->responder_resources = 1;
@@ -423,6 +417,18 @@ static struct smb_direct_transport *alloc_transport(struct rdma_cm_id *cm_id)
 	sp->keepalive_interval_msec = SMB_DIRECT_KEEPALIVE_SEND_INTERVAL * 1000;
 	sp->keepalive_timeout_msec = SMB_DIRECT_KEEPALIVE_RECV_TIMEOUT * 1000;
 
+	t = kzalloc(sizeof(*t), KSMBD_DEFAULT_GFP);
+	if (!t)
+		return NULL;
+	sc = &t->socket;
+	smbdirect_socket_prepare_create(sc, sp, smb_direct_wq);
+	/*
+	 * from here we operate on the copy.
+	 */
+	sp = &sc->parameters;
+
+	INIT_WORK(&sc->disconnect_work, smb_direct_disconnect_rdma_work);
+
 	sc->rdma.cm_id = cm_id;
 	cm_id->context = sc;
 
Signature · 1 line
2.43.0
author reviewer
#97 lore raw

PATCH v2 93/127 smb: server: make use of smbdirect_socket_set_logging()

In reply to Stefan Metzmacher

This will allow the logging to keep working as before, when we move to common functions in the next commits.

---
 fs/smb/server/transport_rdma.c | 62 ++++++++++++++++++++++++++++++++++
 1 file changed, 62 insertions(+)
fs/smb/server/transport_rdma.c +62 -0
index bcc584884f29..fa6371ed0b76 100644
@@ -109,6 +109,65 @@ struct smb_direct_transport {
 	struct smbdirect_socket socket;
 };
 
+static bool smb_direct_logging_needed(struct smbdirect_socket *sc,
+				      void *private_ptr,
+				      unsigned int lvl,
+				      unsigned int cls)
+{
+	if (lvl <= SMBDIRECT_LOG_ERR)
+		return true;
+
+	if (lvl > SMBDIRECT_LOG_INFO)
+		return false;
+
+	switch (cls) {
+	/*
+	 * These were more or less also logged before
+	 * the move to common code.
+	 *
+	 * SMBDIRECT_LOG_RDMA_MR was not used, but
+	 * that's client only code and we should
+	 * notice if it's used on the server...
+	 */
+	case SMBDIRECT_LOG_RDMA_EVENT:
+	case SMBDIRECT_LOG_RDMA_SEND:
+	case SMBDIRECT_LOG_RDMA_RECV:
+	case SMBDIRECT_LOG_WRITE:
+	case SMBDIRECT_LOG_READ:
+	case SMBDIRECT_LOG_NEGOTIATE:
+	case SMBDIRECT_LOG_OUTGOING:
+	case SMBDIRECT_LOG_RDMA_RW:
+	case SMBDIRECT_LOG_RDMA_MR:
+		return true;
+	/*
+	 * These were not logged before the move
+	 * to common code.
+	 */
+	case SMBDIRECT_LOG_KEEP_ALIVE:
+	case SMBDIRECT_LOG_INCOMING:
+		return false;
+	}
+
+	/*
+	 * Log all unknown messages
+	 */
+	return true;
+}
+
+static void smb_direct_logging_vaprintf(struct smbdirect_socket *sc,
+					const char *func,
+					unsigned int line,
+					void *private_ptr,
+					unsigned int lvl,
+					unsigned int cls,
+					struct va_format *vaf)
+{
+	if (lvl <= SMBDIRECT_LOG_ERR)
+		pr_err("%pV", vaf);
+	else
+		ksmbd_debug(RDMA, "%pV", vaf);
+}
+
 #define KSMBD_TRANS(t) (&(t)->transport)
 #define SMBD_TRANS(t)	(container_of(t, \
 				struct smb_direct_transport, transport))
@@ -422,6 +481,9 @@ static struct smb_direct_transport *alloc_transport(struct rdma_cm_id *cm_id)
 		return NULL;
 	sc = &t->socket;
 	smbdirect_socket_prepare_create(sc, sp, smb_direct_wq);
+	smbdirect_socket_set_logging(sc, NULL,
+				     smb_direct_logging_needed,
+				     smb_direct_logging_vaprintf);
 	/*
 	 * from here we operate on the copy.
 	 */
Signature · 1 line
2.43.0
author reviewer
#98 lore raw

PATCH v2 94/127 smb: server: make use of smbdirect_connection_wake_up_all()

In reply to Stefan Metzmacher

This is a superset of smb_direct_disconnect_wake_up_all() and calling wake_up_all(&sc->mr_io.ready.wait_queue); and wake_up_all(&sc->mr_io.cleanup.wait_queue); in addition should not matter as it's not used on the server anyway.

---
 fs/smb/server/transport_rdma.c | 20 +++-----------------
 1 file changed, 3 insertions(+), 17 deletions(-)
fs/smb/server/transport_rdma.c +3 -17
index fa6371ed0b76..b78753801fe5 100644
@@ -281,20 +281,6 @@ static struct smbdirect_recv_io *get_first_reassembly(struct smbdirect_socket *s
 		return NULL;
 }
 
-static void smb_direct_disconnect_wake_up_all(struct smbdirect_socket *sc)
-{
-	/*
-	 * Wake up all waiters in all wait queues
-	 * in order to notice the broken connection.
-	 */
-	wake_up_all(&sc->status_wait);
-	wake_up_all(&sc->send_io.lcredits.wait_queue);
-	wake_up_all(&sc->send_io.credits.wait_queue);
-	wake_up_all(&sc->send_io.pending.zero_wait_queue);
-	wake_up_all(&sc->recv_io.reassembly.wait_queue);
-	wake_up_all(&sc->rw_io.credits.wait_queue);
-}
-
 static void smb_direct_disconnect_rdma_work(struct work_struct *work)
 {
 	struct smbdirect_socket *sc =
@@ -350,7 +336,7 @@ static void smb_direct_disconnect_rdma_work(struct work_struct *work)
 	 * Wake up all waiters in all wait queues
 	 * in order to notice the broken connection.
 	 */
-	smb_direct_disconnect_wake_up_all(sc);
+	smbdirect_connection_wake_up_all(sc);
 }
 
 static void
@@ -412,7 +398,7 @@ smb_direct_disconnect_rdma_connection(struct smbdirect_socket *sc)
 	 * Wake up all waiters in all wait queues
 	 * in order to notice the broken connection.
 	 */
-	smb_direct_disconnect_wake_up_all(sc);
+	smbdirect_connection_wake_up_all(sc);
 
 	queue_work(sc->workqueue, &sc->disconnect_work);
 }
@@ -538,7 +524,7 @@ static void free_transport(struct smb_direct_transport *t)
 	 * Most likely this was already called via
 	 * smb_direct_disconnect_rdma_work(), but call it again...
 	 */
-	smb_direct_disconnect_wake_up_all(sc);
+	smbdirect_connection_wake_up_all(sc);
 
 	disable_work_sync(&sc->recv_io.posted.refill_work);
 	disable_delayed_work_sync(&sc->idle.timer_work);
Signature · 1 line
2.43.0
author reviewer
#99 lore raw

PATCH v2 95/127 smb: server: make use of smbdirect_connection_disconnect_work()

In reply to Stefan Metzmacher

Note smbdirect_socket_prepare_create() already calls INIT_WORK() with smbdirect_connection_disconnect_work.

---
 fs/smb/server/transport_rdma.c | 70 +++-------------------------------
 1 file changed, 5 insertions(+), 65 deletions(-)
fs/smb/server/transport_rdma.c +5 -65
index b78753801fe5..a649194ab6c8 100644
@@ -281,64 +281,6 @@ static struct smbdirect_recv_io *get_first_reassembly(struct smbdirect_socket *s
 		return NULL;
 }
 
-static void smb_direct_disconnect_rdma_work(struct work_struct *work)
-{
-	struct smbdirect_socket *sc =
-		container_of(work, struct smbdirect_socket, disconnect_work);
-
-	/*
-	 * make sure this and other work is not queued again
-	 * but here we don't block and avoid
-	 * disable[_delayed]_work_sync()
-	 */
-	disable_work(&sc->disconnect_work);
-	disable_work(&sc->recv_io.posted.refill_work);
-	disable_delayed_work(&sc->idle.timer_work);
-	disable_work(&sc->idle.immediate_work);
-
-	if (sc->first_error == 0)
-		sc->first_error = -ECONNABORTED;
-
-	switch (sc->status) {
-	case SMBDIRECT_SOCKET_NEGOTIATE_NEEDED:
-	case SMBDIRECT_SOCKET_NEGOTIATE_RUNNING:
-	case SMBDIRECT_SOCKET_NEGOTIATE_FAILED:
-	case SMBDIRECT_SOCKET_CONNECTED:
-	case SMBDIRECT_SOCKET_ERROR:
-		sc->status = SMBDIRECT_SOCKET_DISCONNECTING;
-		rdma_disconnect(sc->rdma.cm_id);
-		break;
-
-	case SMBDIRECT_SOCKET_CREATED:
-	case SMBDIRECT_SOCKET_RESOLVE_ADDR_NEEDED:
-	case SMBDIRECT_SOCKET_RESOLVE_ADDR_RUNNING:
-	case SMBDIRECT_SOCKET_RESOLVE_ADDR_FAILED:
-	case SMBDIRECT_SOCKET_RESOLVE_ROUTE_NEEDED:
-	case SMBDIRECT_SOCKET_RESOLVE_ROUTE_RUNNING:
-	case SMBDIRECT_SOCKET_RESOLVE_ROUTE_FAILED:
-	case SMBDIRECT_SOCKET_RDMA_CONNECT_NEEDED:
-	case SMBDIRECT_SOCKET_RDMA_CONNECT_RUNNING:
-	case SMBDIRECT_SOCKET_RDMA_CONNECT_FAILED:
-		/*
-		 * rdma_accept() never reached
-		 * RDMA_CM_EVENT_ESTABLISHED
-		 */
-		sc->status = SMBDIRECT_SOCKET_DISCONNECTED;
-		break;
-
-	case SMBDIRECT_SOCKET_DISCONNECTING:
-	case SMBDIRECT_SOCKET_DISCONNECTED:
-	case SMBDIRECT_SOCKET_DESTROYED:
-		break;
-	}
-
-	/*
-	 * Wake up all waiters in all wait queues
-	 * in order to notice the broken connection.
-	 */
-	smbdirect_connection_wake_up_all(sc);
-}
-
 static void
 smb_direct_disconnect_rdma_connection(struct smbdirect_socket *sc)
 {
@@ -475,8 +417,6 @@ static struct smb_direct_transport *alloc_transport(struct rdma_cm_id *cm_id)
 	 */
 	sp = &sc->parameters;
 
-	INIT_WORK(&sc->disconnect_work, smb_direct_disconnect_rdma_work);
-
 	sc->rdma.cm_id = cm_id;
 	cm_id->context = sc;
 
@@ -513,7 +453,7 @@ static void free_transport(struct smb_direct_transport *t)
 
 	disable_work_sync(&sc->disconnect_work);
 	if (sc->status < SMBDIRECT_SOCKET_DISCONNECTING)
-		smb_direct_disconnect_rdma_work(&sc->disconnect_work);
+		smbdirect_connection_disconnect_work(&sc->disconnect_work);
 	if (sc->status < SMBDIRECT_SOCKET_DISCONNECTED)
 		wait_event(sc->status_wait, sc->status == SMBDIRECT_SOCKET_DISCONNECTED);
 
@@ -522,7 +462,7 @@ static void free_transport(struct smb_direct_transport *t)
 	 * in order to notice the broken connection.
 	 *
 	 * Most likely this was already called via
-	 * smb_direct_disconnect_rdma_work(), but call it again...
+	 * smbdirect_connection_disconnect_work(), but call it again...
 	 */
 	smbdirect_connection_wake_up_all(sc);
 
@@ -1773,7 +1713,7 @@ static void smb_direct_shutdown(struct ksmbd_transport *t)
 
 	ksmbd_debug(RDMA, "smb-direct shutdown cm_id=%p\n", sc->rdma.cm_id);
 
-	smb_direct_disconnect_rdma_work(&sc->disconnect_work);
+	smbdirect_connection_disconnect_work(&sc->disconnect_work);
 }
 
 static int smb_direct_cm_handler(struct rdma_cm_id *cm_id,
@@ -1794,14 +1734,14 @@ static int smb_direct_cm_handler(struct rdma_cm_id *cm_id,
 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
 	case RDMA_CM_EVENT_DISCONNECTED: {
 		sc->status = SMBDIRECT_SOCKET_DISCONNECTED;
-		smb_direct_disconnect_rdma_work(&sc->disconnect_work);
+		smbdirect_connection_disconnect_work(&sc->disconnect_work);
 		if (sc->ib.qp)
 			ib_drain_qp(sc->ib.qp);
 		break;
 	}
 	case RDMA_CM_EVENT_CONNECT_ERROR: {
 		sc->status = SMBDIRECT_SOCKET_DISCONNECTED;
-		smb_direct_disconnect_rdma_work(&sc->disconnect_work);
+		smbdirect_connection_disconnect_work(&sc->disconnect_work);
 		break;
 	}
 	default:
Signature · 1 line
2.43.0
author reviewer
#100 lore raw

PATCH v2 96/127 smb: server: make use of smbdirect_connection_schedule_disconnect()

In reply to Stefan Metzmacher

This removes smb_direct_disconnect_rdma_connection() which is basically the same as smbdirect_connection_schedule_disconnect().
And we pass more useful errors than -ECONNABORTED if we have them.

---
 fs/smb/server/transport_rdma.c | 88 +++++-----------------------------
 1 file changed, 12 insertions(+), 76 deletions(-)
fs/smb/server/transport_rdma.c +12 -76
index a649194ab6c8..89db1cc921d2 100644
@@ -281,70 +281,6 @@ static struct smbdirect_recv_io *get_first_reassembly(struct smbdirect_socket *s
 		return NULL;
 }
 
-static void
-smb_direct_disconnect_rdma_connection(struct smbdirect_socket *sc)
-{
-	/*
-	 * make sure other work (than disconnect_work) is
-	 * not queued again but here we don't block and avoid
-	 * disable[_delayed]_work_sync()
-	 */
-	disable_work(&sc->recv_io.posted.refill_work);
-	disable_work(&sc->idle.immediate_work);
-	disable_delayed_work(&sc->idle.timer_work);
-
-	if (sc->first_error == 0)
-		sc->first_error = -ECONNABORTED;
-
-	switch (sc->status) {
-	case SMBDIRECT_SOCKET_RESOLVE_ADDR_FAILED:
-	case SMBDIRECT_SOCKET_RESOLVE_ROUTE_FAILED:
-	case SMBDIRECT_SOCKET_RDMA_CONNECT_FAILED:
-	case SMBDIRECT_SOCKET_NEGOTIATE_FAILED:
-	case SMBDIRECT_SOCKET_ERROR:
-	case SMBDIRECT_SOCKET_DISCONNECTING:
-	case SMBDIRECT_SOCKET_DISCONNECTED:
-	case SMBDIRECT_SOCKET_DESTROYED:
-		/*
-		 * Keep the current error status
-		 */
-		break;
-
-	case SMBDIRECT_SOCKET_RESOLVE_ADDR_NEEDED:
-	case SMBDIRECT_SOCKET_RESOLVE_ADDR_RUNNING:
-		sc->status = SMBDIRECT_SOCKET_RESOLVE_ADDR_FAILED;
-		break;
-
-	case SMBDIRECT_SOCKET_RESOLVE_ROUTE_NEEDED:
-	case SMBDIRECT_SOCKET_RESOLVE_ROUTE_RUNNING:
-		sc->status = SMBDIRECT_SOCKET_RESOLVE_ROUTE_FAILED;
-		break;
-
-	case SMBDIRECT_SOCKET_RDMA_CONNECT_NEEDED:
-	case SMBDIRECT_SOCKET_RDMA_CONNECT_RUNNING:
-		sc->status = SMBDIRECT_SOCKET_RDMA_CONNECT_FAILED;
-		break;
-
-	case SMBDIRECT_SOCKET_NEGOTIATE_NEEDED:
-	case SMBDIRECT_SOCKET_NEGOTIATE_RUNNING:
-		sc->status = SMBDIRECT_SOCKET_NEGOTIATE_FAILED;
-		break;
-
-	case SMBDIRECT_SOCKET_CREATED:
-	case SMBDIRECT_SOCKET_CONNECTED:
-		sc->status = SMBDIRECT_SOCKET_ERROR;
-		break;
-	}
-
-	/*
-	 * Wake up all waiters in all wait queues
-	 * in order to notice the broken connection.
-	 */
-	smbdirect_connection_wake_up_all(sc);
-
-	queue_work(sc->workqueue, &sc->disconnect_work);
-}
-
 static void smb_direct_send_immediate_work(struct work_struct *work)
 {
 	struct smbdirect_socket *sc =
@@ -363,7 +299,7 @@ static void smb_direct_idle_connection_timer(struct work_struct *work)
 	struct smbdirect_socket_parameters *sp = &sc->parameters;
 
 	if (sc->idle.keepalive != SMBDIRECT_KEEPALIVE_NONE) {
-		smb_direct_disconnect_rdma_connection(sc);
+		smbdirect_connection_schedule_disconnect(sc, -ETIMEDOUT);
 		return;
 	}
 
@@ -612,7 +548,7 @@ static void recv_done(struct ib_cq *cq, struct ib_wc *wc)
 			pr_err("Recv error. status='%s (%d)' opcode=%d\n",
 			       ib_wc_status_msg(wc->status), wc->status,
 			       wc->opcode);
-			smb_direct_disconnect_rdma_connection(sc);
+			smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
 		}
 		return;
 	}
@@ -636,7 +572,7 @@ static void recv_done(struct ib_cq *cq, struct ib_wc *wc)
 	case SMBDIRECT_EXPECT_NEGOTIATE_REQ:
 		if (wc->byte_len < sizeof(struct smbdirect_negotiate_req)) {
 			put_recvmsg(sc, recvmsg);
-			smb_direct_disconnect_rdma_connection(sc);
+			smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
 			return;
 		}
 		sc->recv_io.reassembly.full_packet_received = true;
@@ -654,7 +590,7 @@ static void recv_done(struct ib_cq *cq, struct ib_wc *wc)
 		if (wc->byte_len <
 		    offsetof(struct smbdirect_data_transfer, padding)) {
 			put_recvmsg(sc, recvmsg);
-			smb_direct_disconnect_rdma_connection(sc);
+			smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
 			return;
 		}
 
@@ -664,7 +600,7 @@ static void recv_done(struct ib_cq *cq, struct ib_wc *wc)
 		if (wc->byte_len < data_offset ||
 		    wc->byte_len < (u64)data_offset + data_length) {
 			put_recvmsg(sc, recvmsg);
-			smb_direct_disconnect_rdma_connection(sc);
+			smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
 			return;
 		}
 		if (remaining_data_length > sp->max_fragmented_recv_size ||
@@ -672,7 +608,7 @@ static void recv_done(struct ib_cq *cq, struct ib_wc *wc)
 		    (u64)remaining_data_length + (u64)data_length >
 		    (u64)sp->max_fragmented_recv_size) {
 			put_recvmsg(sc, recvmsg);
-			smb_direct_disconnect_rdma_connection(sc);
+			smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
 			return;
 		}
 
@@ -727,7 +663,7 @@ static void recv_done(struct ib_cq *cq, struct ib_wc *wc)
 	 */
 	WARN_ON_ONCE(sc->recv_io.expected != SMBDIRECT_EXPECT_DATA_TRANSFER);
 	put_recvmsg(sc, recvmsg);
-	smb_direct_disconnect_rdma_connection(sc);
+	smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
 }
 
 static int smb_direct_post_recv(struct smbdirect_socket *sc,
@@ -760,7 +696,7 @@ static int smb_direct_post_recv(struct smbdirect_socket *sc,
 				    recvmsg->sge.addr, recvmsg->sge.length,
 				    DMA_FROM_DEVICE);
 		recvmsg->sge.length = 0;
-		smb_direct_disconnect_rdma_connection(sc);
+		smbdirect_connection_schedule_disconnect(sc, ret);
 		return ret;
 	}
 	return ret;
@@ -947,7 +883,7 @@ static void send_done(struct ib_cq *cq, struct ib_wc *wc)
 		pr_err("Send error. status='%s (%d)', opcode=%d\n",
 		       ib_wc_status_msg(wc->status), wc->status,
 		       wc->opcode);
-		smb_direct_disconnect_rdma_connection(sc);
+		smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
 		return;
 	}
 
@@ -1003,7 +939,7 @@ static int smb_direct_post_send(struct smbdirect_socket *sc,
 	ret = ib_post_send(sc->ib.qp, wr, NULL);
 	if (ret) {
 		pr_err("failed to post send: %d\n", ret);
-		smb_direct_disconnect_rdma_connection(sc);
+		smbdirect_connection_schedule_disconnect(sc, ret);
 	}
 	return ret;
 }
@@ -1530,7 +1466,7 @@ static void read_write_done(struct ib_cq *cq, struct ib_wc *wc,
 		pr_err("read/write error. opcode = %d, status = %s(%d)\n",
 		       wc->opcode, ib_wc_status_msg(wc->status), wc->status);
 		if (wc->status != IB_WC_WR_FLUSH_ERR)
-			smb_direct_disconnect_rdma_connection(sc);
+			smbdirect_connection_schedule_disconnect(sc, msg->error);
 	}
 
 	complete(msg->completion);
@@ -1763,7 +1699,7 @@ static void smb_direct_qpair_handler(struct ib_event *event, void *context)
 	switch (event->event) {
 	case IB_EVENT_CQ_ERR:
 	case IB_EVENT_QP_FATAL:
-		smb_direct_disconnect_rdma_connection(sc);
+		smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
 		break;
 	default:
 		break;
Signature · 1 line
2.43.0
author reviewer
#101 lore raw

PATCH v2 97/127 smb: server: make use of smbdirect_connection_{get,put}_recv_io()

In reply to Stefan Metzmacher

These are basically copies of {get,put}_receive_buffer() in the client. They are very similar to {get_free,put}_recvmsg() the only logical difference is the updating of the sc->statistics.*.

---
 fs/smb/server/transport_rdma.c | 67 ++++++++--------------------------
 1 file changed, 15 insertions(+), 52 deletions(-)
fs/smb/server/transport_rdma.c +15 -52
index 89db1cc921d2..cc64be846daf 100644
@@ -215,43 +215,6 @@ static inline void
 	return (void *)recvmsg->packet;
 }
 
-static struct
-smbdirect_recv_io *get_free_recvmsg(struct smbdirect_socket *sc)
-{
-	struct smbdirect_recv_io *recvmsg = NULL;
-	unsigned long flags;
-
-	spin_lock_irqsave(&sc->recv_io.free.lock, flags);
-	if (!list_empty(&sc->recv_io.free.list)) {
-		recvmsg = list_first_entry(&sc->recv_io.free.list,
-					   struct smbdirect_recv_io,
-					   list);
-		list_del(&recvmsg->list);
-	}
-	spin_unlock_irqrestore(&sc->recv_io.free.lock, flags);
-	return recvmsg;
-}
-
-static void put_recvmsg(struct smbdirect_socket *sc,
-			struct smbdirect_recv_io *recvmsg)
-{
-	unsigned long flags;
-
-	if (likely(recvmsg->sge.length != 0)) {
-		ib_dma_unmap_single(sc->ib.dev,
-				    recvmsg->sge.addr,
-				    recvmsg->sge.length,
-				    DMA_FROM_DEVICE);
-		recvmsg->sge.length = 0;
-	}
-
-	spin_lock_irqsave(&sc->recv_io.free.lock, flags);
-	list_add(&recvmsg->list, &sc->recv_io.free.list);
-	spin_unlock_irqrestore(&sc->recv_io.free.lock, flags);
-
-	queue_work(sc->workqueue, &sc->recv_io.posted.refill_work);
-}
-
 static void enqueue_reassembly(struct smbdirect_socket *sc,
 			       struct smbdirect_recv_io *recvmsg,
 			       int data_length)
@@ -424,7 +387,7 @@ static void free_transport(struct smb_direct_transport *t)
 		if (recvmsg) {
 			list_del(&recvmsg->list);
 			spin_unlock_irqrestore(&sc->recv_io.reassembly.lock, flags);
-			put_recvmsg(sc, recvmsg);
+			smbdirect_connection_put_recv_io(recvmsg);
 		} else {
 			spin_unlock_irqrestore(&sc->recv_io.reassembly.lock, flags);
 		}
@@ -543,7 +506,7 @@ static void recv_done(struct ib_cq *cq, struct ib_wc *wc)
 	sp = &sc->parameters;
 
 	if (wc->status != IB_WC_SUCCESS || wc->opcode != IB_WC_RECV) {
-		put_recvmsg(sc, recvmsg);
+		smbdirect_connection_put_recv_io(recvmsg);
 		if (wc->status != IB_WC_WR_FLUSH_ERR) {
 			pr_err("Recv error. status='%s (%d)' opcode=%d\n",
 			       ib_wc_status_msg(wc->status), wc->status,
@@ -571,7 +534,7 @@ static void recv_done(struct ib_cq *cq, struct ib_wc *wc)
 	switch (sc->recv_io.expected) {
 	case SMBDIRECT_EXPECT_NEGOTIATE_REQ:
 		if (wc->byte_len < sizeof(struct smbdirect_negotiate_req)) {
-			put_recvmsg(sc, recvmsg);
+			smbdirect_connection_put_recv_io(recvmsg);
 			smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
 			return;
 		}
@@ -589,7 +552,7 @@ static void recv_done(struct ib_cq *cq, struct ib_wc *wc)
 
 		if (wc->byte_len <
 		    offsetof(struct smbdirect_data_transfer, padding)) {
-			put_recvmsg(sc, recvmsg);
+			smbdirect_connection_put_recv_io(recvmsg);
 			smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
 			return;
 		}
@@ -599,7 +562,7 @@ static void recv_done(struct ib_cq *cq, struct ib_wc *wc)
 		data_offset = le32_to_cpu(data_transfer->data_offset);
 		if (wc->byte_len < data_offset ||
 		    wc->byte_len < (u64)data_offset + data_length) {
-			put_recvmsg(sc, recvmsg);
+			smbdirect_connection_put_recv_io(recvmsg);
 			smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
 			return;
 		}
@@ -607,7 +570,7 @@ static void recv_done(struct ib_cq *cq, struct ib_wc *wc)
 		    data_length > sp->max_fragmented_recv_size ||
 		    (u64)remaining_data_length + (u64)data_length >
 		    (u64)sp->max_fragmented_recv_size) {
-			put_recvmsg(sc, recvmsg);
+			smbdirect_connection_put_recv_io(recvmsg);
 			smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
 			return;
 		}
@@ -649,7 +612,7 @@ static void recv_done(struct ib_cq *cq, struct ib_wc *wc)
 			enqueue_reassembly(sc, recvmsg, (int)data_length);
 			wake_up(&sc->recv_io.reassembly.wait_queue);
 		} else
-			put_recvmsg(sc, recvmsg);
+			smbdirect_connection_put_recv_io(recvmsg);
 
 		return;
 	}
@@ -662,7 +625,7 @@ static void recv_done(struct ib_cq *cq, struct ib_wc *wc)
 	 * This is an internal error!
 	 */
 	WARN_ON_ONCE(sc->recv_io.expected != SMBDIRECT_EXPECT_DATA_TRANSFER);
-	put_recvmsg(sc, recvmsg);
+	smbdirect_connection_put_recv_io(recvmsg);
 	smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
 }
 
@@ -788,7 +751,7 @@ static int smb_direct_read(struct ksmbd_transport *t, char *buf,
 					spin_unlock_irqrestore(&sc->recv_io.reassembly.lock, flags);
 				}
 				queue_removed++;
-				put_recvmsg(sc, recvmsg);
+				smbdirect_connection_put_recv_io(recvmsg);
 				offset = 0;
 			} else {
 				offset += to_copy;
@@ -832,7 +795,7 @@ static void smb_direct_post_recv_credits(struct work_struct *work)
 
 	if (atomic_read(&sc->recv_io.credits.count) < sc->recv_io.credits.target) {
 		while (true) {
-			recvmsg = get_free_recvmsg(sc);
+			recvmsg = smbdirect_connection_get_recv_io(sc);
 			if (!recvmsg)
 				break;
 
@@ -841,7 +804,7 @@ static void smb_direct_post_recv_credits(struct work_struct *work)
 			ret = smb_direct_post_recv(sc, recvmsg);
 			if (ret) {
 				pr_err("Can't post recv: %d\n", ret);
-				put_recvmsg(sc, recvmsg);
+				smbdirect_connection_put_recv_io(recvmsg);
 				break;
 			}
 			credits++;
@@ -1830,7 +1793,7 @@ static int smb_direct_prepare_negotiation(struct smbdirect_socket *sc)
 
 	sc->recv_io.expected = SMBDIRECT_EXPECT_NEGOTIATE_REQ;
 
-	recvmsg = get_free_recvmsg(sc);
+	recvmsg = smbdirect_connection_get_recv_io(sc);
 	if (!recvmsg)
 		return -ENOMEM;
 
@@ -1848,7 +1811,7 @@ static int smb_direct_prepare_negotiation(struct smbdirect_socket *sc)
 
 	return 0;
 out_err:
-	put_recvmsg(sc, recvmsg);
+	smbdirect_connection_put_recv_io(recvmsg);
 	return ret;
 }
 
@@ -1888,7 +1851,7 @@ static void smb_direct_destroy_pools(struct smbdirect_socket *sc)
 {
 	struct smbdirect_recv_io *recvmsg;
 
-	while ((recvmsg = get_free_recvmsg(sc)))
+	while ((recvmsg = smbdirect_connection_get_recv_io(sc)))
 		mempool_free(recvmsg, sc->recv_io.mem.pool);
 
 	mempool_destroy(sc->recv_io.mem.pool);
@@ -2211,7 +2174,7 @@ static int smb_direct_prepare(struct ksmbd_transport *t)
 	sc->recv_io.reassembly.queue_length--;
 	list_del(&recvmsg->list);
 	spin_unlock_irqrestore(&sc->recv_io.reassembly.lock, flags);
-	put_recvmsg(sc, recvmsg);
+	smbdirect_connection_put_recv_io(recvmsg);
 
 	if (ret == -ECONNABORTED)
 		return ret;
Signature · 1 line
2.43.0
author reviewer
#102 lore raw

PATCH v2 98/127 smb: server: make use of smbdirect_connection_reassembly_{append,first}_recv_io()

In reply to Stefan Metzmacher

These are basically copies of enqueue_reassembly() and get_first_reassembly(). The only difference is that sc->statistics.enqueue_reassembly_queue now updated.

---
 fs/smb/server/transport_rdma.c | 41 +++++-----------------------------
 1 file changed, 6 insertions(+), 35 deletions(-)
fs/smb/server/transport_rdma.c +6 -35
index cc64be846daf..fdf8ac7d5d34 100644
@@ -215,35 +215,6 @@ static inline void
 	return (void *)recvmsg->packet;
 }
 
-static void enqueue_reassembly(struct smbdirect_socket *sc,
-			       struct smbdirect_recv_io *recvmsg,
-			       int data_length)
-{
-	unsigned long flags;
-
-	spin_lock_irqsave(&sc->recv_io.reassembly.lock, flags);
-	list_add_tail(&recvmsg->list, &sc->recv_io.reassembly.list);
-	sc->recv_io.reassembly.queue_length++;
-	/*
-	 * Make sure reassembly_data_length is updated after list and
-	 * reassembly_queue_length are updated. On the dequeue side
-	 * reassembly_data_length is checked without a lock to determine
-	 * if reassembly_queue_length and list is up to date
-	 */
-	virt_wmb();
-	sc->recv_io.reassembly.data_length += data_length;
-	spin_unlock_irqrestore(&sc->recv_io.reassembly.lock, flags);
-}
-
-static struct smbdirect_recv_io *get_first_reassembly(struct smbdirect_socket *sc)
-{
-	if (!list_empty(&sc->recv_io.reassembly.list))
-		return list_first_entry(&sc->recv_io.reassembly.list,
-				struct smbdirect_recv_io, list);
-	else
-		return NULL;
-}
-
 static void smb_direct_send_immediate_work(struct work_struct *work)
 {
 	struct smbdirect_socket *sc =
@@ -383,7 +354,7 @@ static void free_transport(struct smb_direct_transport *t)
 		unsigned long flags;
 
 		spin_lock_irqsave(&sc->recv_io.reassembly.lock, flags);
-		recvmsg = get_first_reassembly(sc);
+		recvmsg = smbdirect_connection_reassembly_first_recv_io(sc);
 		if (recvmsg) {
 			list_del(&recvmsg->list);
 			spin_unlock_irqrestore(&sc->recv_io.reassembly.lock, flags);
@@ -541,7 +512,7 @@ static void recv_done(struct ib_cq *cq, struct ib_wc *wc)
 		sc->recv_io.reassembly.full_packet_received = true;
 		WARN_ON_ONCE(sc->status != SMBDIRECT_SOCKET_NEGOTIATE_NEEDED);
 		sc->status = SMBDIRECT_SOCKET_NEGOTIATE_RUNNING;
-		enqueue_reassembly(sc, recvmsg, 0);
+		smbdirect_connection_reassembly_append_recv_io(sc, recvmsg, 0);
 		wake_up(&sc->status_wait);
 		return;
 	case SMBDIRECT_EXPECT_DATA_TRANSFER: {
@@ -609,7 +580,7 @@ static void recv_done(struct ib_cq *cq, struct ib_wc *wc)
 			if (sc->recv_io.credits.target > old_recv_credit_target)
 				queue_work(sc->workqueue, &sc->recv_io.posted.refill_work);
 
-			enqueue_reassembly(sc, recvmsg, (int)data_length);
+			smbdirect_connection_reassembly_append_recv_io(sc, recvmsg, data_length);
 			wake_up(&sc->recv_io.reassembly.wait_queue);
 		} else
 			smbdirect_connection_put_recv_io(recvmsg);
@@ -695,7 +666,7 @@ static int smb_direct_read(struct ksmbd_transport *t, char *buf,
 		/*
 		 * Need to make sure reassembly_data_length is read before
 		 * reading reassembly_queue_length and calling
-		 * get_first_reassembly. This call is lock free
+		 * smbdirect_connection_reassembly_first_recv_io. This call is lock free
 		 * as we never read at the end of the queue which are being
 		 * updated in SOFTIRQ as more data is received
 		 */
@@ -705,7 +676,7 @@ static int smb_direct_read(struct ksmbd_transport *t, char *buf,
 		to_read = size;
 		offset = sc->recv_io.reassembly.first_entry_offset;
 		while (data_read < size) {
-			recvmsg = get_first_reassembly(sc);
+			recvmsg = smbdirect_connection_reassembly_first_recv_io(sc);
 			data_transfer = smbdirect_recv_io_payload(recvmsg);
 			data_length = le32_to_cpu(data_transfer->data_length);
 			remaining_data_length =
@@ -2148,7 +2119,7 @@ static int smb_direct_prepare(struct ksmbd_transport *t)
 	if (ret <= 0 || sc->status != SMBDIRECT_SOCKET_NEGOTIATE_RUNNING)
 		return ret < 0 ? ret : -ETIMEDOUT;
 
-	recvmsg = get_first_reassembly(sc);
+	recvmsg = smbdirect_connection_reassembly_first_recv_io(sc);
 	if (!recvmsg)
 		return -ECONNABORTED;
 
Signature · 1 line
2.43.0
author reviewer
#103 lore raw

PATCH v2 99/127 smb: server: make use of smbdirect_connection_idle_timer_work()

In reply to Stefan Metzmacher

This is basically a copy of smb_direct_idle_connection_timer(). The only difference is that we had no logging before.

Note smbdirect_socket_prepare_create() already calls INIT_DELAYED_WORK().

---
 fs/smb/server/transport_rdma.c | 26 --------------------------
 1 file changed, 26 deletions(-)
fs/smb/server/transport_rdma.c +0 -26
index fdf8ac7d5d34..886a350819bf 100644
@@ -226,30 +226,6 @@ static void smb_direct_send_immediate_work(struct work_struct *work)
 	smb_direct_post_send_data(sc, NULL, NULL, 0, 0);
 }
 
-static void smb_direct_idle_connection_timer(struct work_struct *work)
-{
-	struct smbdirect_socket *sc =
-		container_of(work, struct smbdirect_socket, idle.timer_work.work);
-	struct smbdirect_socket_parameters *sp = &sc->parameters;
-
-	if (sc->idle.keepalive != SMBDIRECT_KEEPALIVE_NONE) {
-		smbdirect_connection_schedule_disconnect(sc, -ETIMEDOUT);
-		return;
-	}
-
-	if (sc->status != SMBDIRECT_SOCKET_CONNECTED)
-		return;
-
-	/*
-	 * Now use the keepalive timeout (instead of keepalive interval)
-	 * in order to wait for a response
-	 */
-	sc->idle.keepalive = SMBDIRECT_KEEPALIVE_PENDING;
-	mod_delayed_work(sc->workqueue, &sc->idle.timer_work,
-			 msecs_to_jiffies(sp->keepalive_timeout_msec));
-	queue_work(sc->workqueue, &sc->idle.immediate_work);
-}
-
 static struct smb_direct_transport *alloc_transport(struct rdma_cm_id *cm_id)
 {
 	struct smb_direct_transport *t;
@@ -292,8 +268,6 @@ static struct smb_direct_transport *alloc_transport(struct rdma_cm_id *cm_id)
 
 	sc->ib.dev = sc->rdma.cm_id->device;
 
-	INIT_DELAYED_WORK(&sc->idle.timer_work, smb_direct_idle_connection_timer);
-
 	conn = ksmbd_conn_alloc();
 	if (!conn)
 		goto err;
Signature · 1 line
2.43.0
author reviewer
#104 lore raw

PATCH v2 100/127 smb: server: make use of smbdirect_frwr_is_supported()

In reply to Stefan Metzmacher

This is an exact copy of rdma_frwr_is_supported().

---
 fs/smb/server/transport_rdma.c | 15 +++------------
 1 file changed, 3 insertions(+), 12 deletions(-)
fs/smb/server/transport_rdma.c +3 -12
index 886a350819bf..7181d9f62b09 100644
@@ -2179,15 +2179,6 @@ static int smb_direct_connect(struct smbdirect_socket *sc)
 	return 0;
 }
 
-static bool rdma_frwr_is_supported(struct ib_device_attr *attrs)
-{
-	if (!(attrs->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS))
-		return false;
-	if (attrs->max_fast_reg_page_list_len == 0)
-		return false;
-	return true;
-}
-
 static int smb_direct_handle_connect_request(struct rdma_cm_id *new_cm_id,
 					     struct rdma_cm_event *event)
 {
@@ -2199,7 +2190,7 @@ static int smb_direct_handle_connect_request(struct rdma_cm_id *new_cm_id,
 	u8 peer_responder_resources;
 	int ret;
 
-	if (!rdma_frwr_is_supported(&new_cm_id->device->attrs)) {
+	if (!smbdirect_frwr_is_supported(&new_cm_id->device->attrs)) {
 		ksmbd_debug(RDMA,
 			    "Fast Registration Work Requests is not supported. device capabilities=%llx\n",
 			    new_cm_id->device->attrs.device_cap_flags);
@@ -2361,7 +2352,7 @@ static int smb_direct_ib_client_add(struct ib_device *ib_dev)
 	if (ib_dev->node_type != RDMA_NODE_IB_CA)
 		smb_direct_port = SMB_DIRECT_PORT_IWARP;
 
-	if (!rdma_frwr_is_supported(&ib_dev->attrs))
+	if (!smbdirect_frwr_is_supported(&ib_dev->attrs))
 		return 0;
 
 	smb_dev = kzalloc(sizeof(*smb_dev), KSMBD_DEFAULT_GFP);
@@ -2485,7 +2476,7 @@ static bool ksmbd_find_rdma_capable_netdev(struct net_device *netdev)
 
 		ibdev = ib_device_get_by_netdev(netdev, RDMA_DRIVER_UNKNOWN);
 		if (ibdev) {
-			rdma_capable = rdma_frwr_is_supported(&ibdev->attrs);
+			rdma_capable = smbdirect_frwr_is_supported(&ibdev->attrs);
 			ib_device_put(ibdev);
 		}
 	}
Signature · 1 line
2.43.0
author reviewer
#105 lore raw

PATCH v2 101/127 smb: server: make use of smbdirect_connection_{alloc,free}_send_io()

In reply to Stefan Metzmacher

These are basically copies of smb_direct_{alloc,free}_sendmsg() just a bit simpler and with the gfp_mask mask abstracted.

For now we still use KSMBD_DEFAULT_GFP, which includes __GFP_RETRY_MAYFAIL.

The only difference is that we use ib_dma_unmap_page() for all sges, this simplifies the logic and doesn't matter as ib_dma_unmap_single() and ib_dma_unmap_page() both operate on dma_addr_t and dma_unmap_single_attrs() is just an alias for dma_unmap_page_attrs().
We already had such an inconsistency in the client
code where we use ib_dma_unmap_single(), while we mapped using ib_dma_map_page().

---
 fs/smb/server/transport_rdma.c | 58 +++++++---------------------------
 1 file changed, 11 insertions(+), 47 deletions(-)
fs/smb/server/transport_rdma.c +11 -47
index 7181d9f62b09..50dc87660b25 100644
@@ -258,6 +258,7 @@ static struct smb_direct_transport *alloc_transport(struct rdma_cm_id *cm_id)
 	smbdirect_socket_set_logging(sc, NULL,
 				     smb_direct_logging_needed,
 				     smb_direct_logging_vaprintf);
+	sc->send_io.mem.gfp_mask = KSMBD_DEFAULT_GFP;
 	/*
 	 * from here we operate on the copy.
 	 */
@@ -354,43 +355,6 @@ static void free_transport(struct smb_direct_transport *t)
 	ksmbd_conn_free(KSMBD_TRANS(t)->conn);
 }
 
-static struct smbdirect_send_io
-*smb_direct_alloc_sendmsg(struct smbdirect_socket *sc)
-{
-	struct smbdirect_send_io *msg;
-
-	msg = mempool_alloc(sc->send_io.mem.pool, KSMBD_DEFAULT_GFP);
-	if (!msg)
-		return ERR_PTR(-ENOMEM);
-	msg->socket = sc;
-	INIT_LIST_HEAD(&msg->sibling_list);
-	msg->num_sge = 0;
-	return msg;
-}
-
-static void smb_direct_free_sendmsg(struct smbdirect_socket *sc,
-				    struct smbdirect_send_io *msg)
-{
-	int i;
-
-	/*
-	 * The list needs to be empty!
-	 * The caller should take care of it.
-	 */
-	WARN_ON_ONCE(!list_empty(&msg->sibling_list));
-
-	if (msg->num_sge > 0) {
-		ib_dma_unmap_single(sc->ib.dev,
-				    msg->sge[0].addr, msg->sge[0].length,
-				    DMA_TO_DEVICE);
-		for (i = 1; i < msg->num_sge; i++)
-			ib_dma_unmap_page(sc->ib.dev,
-					  msg->sge[i].addr, msg->sge[i].length,
-					  DMA_TO_DEVICE);
-	}
-	mempool_free(msg, sc->send_io.mem.pool);
-}
-
 static int smb_direct_check_recvmsg(struct smbdirect_recv_io *recvmsg)
 {
 	struct smbdirect_socket *sc = recvmsg->socket;
@@ -780,11 +744,11 @@ static void send_done(struct ib_cq *cq, struct ib_wc *wc)
 	 */
 	list_for_each_entry_safe(sibling, next, &sendmsg->sibling_list, sibling_list) {
 		list_del_init(&sibling->sibling_list);
-		smb_direct_free_sendmsg(sc, sibling);
+		smbdirect_connection_free_send_io(sibling);
 		lcredits += 1;
 	}
 	/* Note this frees wc->wr_cqe, but not wc */
-	smb_direct_free_sendmsg(sc, sendmsg);
+	smbdirect_connection_free_send_io(sendmsg);
 	lcredits += 1;
 
 	if (wc->status != IB_WC_SUCCESS || wc->opcode != IB_WC_SEND) {
@@ -907,9 +871,9 @@ static int smb_direct_flush_send_list(struct smbdirect_socket *sc,
 
 		list_for_each_entry_safe(sibling, next, &last->sibling_list, sibling_list) {
 			list_del_init(&sibling->sibling_list);
-			smb_direct_free_sendmsg(sc, sibling);
+			smbdirect_connection_free_send_io(sibling);
 		}
-		smb_direct_free_sendmsg(sc, last);
+		smbdirect_connection_free_send_io(last);
 	}
 
 	return ret;
@@ -994,7 +958,7 @@ static int smb_direct_create_header(struct smbdirect_socket *sc,
 	int header_length;
 	int ret;
 
-	sendmsg = smb_direct_alloc_sendmsg(sc);
+	sendmsg = smbdirect_connection_alloc_send_io(sc);
 	if (IS_ERR(sendmsg))
 		return PTR_ERR(sendmsg);
 
@@ -1037,7 +1001,7 @@ static int smb_direct_create_header(struct smbdirect_socket *sc,
 						 DMA_TO_DEVICE);
 	ret = ib_dma_mapping_error(sc->ib.dev, sendmsg->sge[0].addr);
 	if (ret) {
-		smb_direct_free_sendmsg(sc, sendmsg);
+		smbdirect_connection_free_send_io(sendmsg);
 		return ret;
 	}
 
@@ -1193,7 +1157,7 @@ static int smb_direct_post_send_data(struct smbdirect_socket *sc,
 		goto err;
 	return 0;
 err:
-	smb_direct_free_sendmsg(sc, msg);
+	smbdirect_connection_free_send_io(msg);
 header_failed:
 	atomic_inc(&sc->send_io.credits.count);
 credit_failed:
@@ -1622,7 +1586,7 @@ static int smb_direct_send_negotiate_response(struct smbdirect_socket *sc,
 	struct smbdirect_negotiate_resp *resp;
 	int ret;
 
-	sendmsg = smb_direct_alloc_sendmsg(sc);
+	sendmsg = smbdirect_connection_alloc_send_io(sc);
 	if (IS_ERR(sendmsg))
 		return -ENOMEM;
 
@@ -1658,7 +1622,7 @@ static int smb_direct_send_negotiate_response(struct smbdirect_socket *sc,
 						 DMA_TO_DEVICE);
 	ret = ib_dma_mapping_error(sc->ib.dev, sendmsg->sge[0].addr);
 	if (ret) {
-		smb_direct_free_sendmsg(sc, sendmsg);
+		smbdirect_connection_free_send_io(sendmsg);
 		return ret;
 	}
 
@@ -1668,7 +1632,7 @@ static int smb_direct_send_negotiate_response(struct smbdirect_socket *sc,
 
 	ret = post_sendmsg(sc, NULL, sendmsg);
 	if (ret) {
-		smb_direct_free_sendmsg(sc, sendmsg);
+		smbdirect_connection_free_send_io(sendmsg);
 		return ret;
 	}
 
Signature · 1 line
2.43.0
author reviewer
#106 lore raw

PATCH v2 102/127 smb: server: make use of smbdirect_connection_send_io_done()

In reply to Stefan Metzmacher

This also wakes up send_io.pending.dec_wait_queue, which
is currently always empty in the server, but that might change in future. And we also don't spam the logs on IB_WC_WR_FLUSH_ERR.

---
 fs/smb/server/transport_rdma.c | 42 +---------------------------------
 1 file changed, 1 insertion(+), 41 deletions(-)
fs/smb/server/transport_rdma.c +1 -41
index 50dc87660b25..aff7ac3054bc 100644
@@ -726,46 +726,6 @@ static void smb_direct_post_recv_credits(struct work_struct *work)
 		queue_work(sc->workqueue, &sc->idle.immediate_work);
 }
 
-static void send_done(struct ib_cq *cq, struct ib_wc *wc)
-{
-	struct smbdirect_send_io *sendmsg, *sibling, *next;
-	struct smbdirect_socket *sc;
-	int lcredits = 0;
-
-	sendmsg = container_of(wc->wr_cqe, struct smbdirect_send_io, cqe);
-	sc = sendmsg->socket;
-
-	ksmbd_debug(RDMA, "Send completed. status='%s (%d)', opcode=%d\n",
-		    ib_wc_status_msg(wc->status), wc->status,
-		    wc->opcode);
-
-	/*
-	 * Free possible siblings and then the main send_io
-	 */
-	list_for_each_entry_safe(sibling, next, &sendmsg->sibling_list, sibling_list) {
-		list_del_init(&sibling->sibling_list);
-		smbdirect_connection_free_send_io(sibling);
-		lcredits += 1;
-	}
-	/* Note this frees wc->wr_cqe, but not wc */
-	smbdirect_connection_free_send_io(sendmsg);
-	lcredits += 1;
-
-	if (wc->status != IB_WC_SUCCESS || wc->opcode != IB_WC_SEND) {
-		pr_err("Send error. status='%s (%d)', opcode=%d\n",
-		       ib_wc_status_msg(wc->status), wc->status,
-		       wc->opcode);
-		smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
-		return;
-	}
-
-	atomic_add(lcredits, &sc->send_io.lcredits.count);
-	wake_up(&sc->send_io.lcredits.wait_queue);
-
-	if (atomic_dec_and_test(&sc->send_io.pending.count))
-		wake_up(&sc->send_io.pending.zero_wait_queue);
-}
-
 static int manage_credits_prior_sending(struct smbdirect_socket *sc)
 {
 	int new_credits;
@@ -1068,7 +1028,7 @@ static int post_sendmsg(struct smbdirect_socket *sc,
 					      msg->sge[i].addr, msg->sge[i].length,
 					      DMA_TO_DEVICE);
 
-	msg->cqe.done = send_done;
+	msg->cqe.done = smbdirect_connection_send_io_done;
 	msg->wr.opcode = IB_WR_SEND;
 	msg->wr.sg_list = &msg->sge[0];
 	msg->wr.num_sge = msg->num_sge;
Signature · 1 line
2.43.0
author reviewer
#107 lore raw

PATCH v2 103/127 smb: server: make use of smbdirect_connection_{create,destroy}_mem_pools()

In reply to Stefan Metzmacher

This were based on smb_direct_{create,destroy}_pools() in the server.

The main logical differences are the following:

We now don't use smbdirect_connection_get_recv_io() on cleanup, instead it uses list_for_each_entry_safe()...

We don't generate warnings if smbdirect_recv_io payload is copied into userspace buffers. This doesn't happen in the server anyway.

And it uses list_add_tail() just to let me feel
better when looking at the code...

---
 fs/smb/server/transport_rdma.c | 76 ++--------------------------------
 1 file changed, 3 insertions(+), 73 deletions(-)
fs/smb/server/transport_rdma.c +3 -73
index aff7ac3054bc..3c36c4c0580d 100644
@@ -202,7 +202,6 @@ static inline int get_buf_page_count(void *buf, int size)
 		(uintptr_t)buf / PAGE_SIZE;
 }
 
-static void smb_direct_destroy_pools(struct smbdirect_socket *sc);
 static void smb_direct_post_recv_credits(struct work_struct *work);
 static int smb_direct_post_send_data(struct smbdirect_socket *sc,
 				     struct smbdirect_send_batch *send_ctx,
@@ -259,6 +258,7 @@ static struct smb_direct_transport *alloc_transport(struct rdma_cm_id *cm_id)
 				     smb_direct_logging_needed,
 				     smb_direct_logging_vaprintf);
 	sc->send_io.mem.gfp_mask = KSMBD_DEFAULT_GFP;
+	sc->recv_io.mem.gfp_mask = KSMBD_DEFAULT_GFP;
 	/*
 	 * from here we operate on the copy.
 	 */
@@ -351,7 +351,7 @@ static void free_transport(struct smb_direct_transport *t)
 		rdma_destroy_id(sc->rdma.cm_id);
 	}
 
-	smb_direct_destroy_pools(sc);
+	smbdirect_connection_destroy_mem_pools(sc);
 	ksmbd_conn_free(KSMBD_TRANS(t)->conn);
 }
 
@@ -1716,76 +1716,6 @@ static int smb_direct_init_params(struct smbdirect_socket *sc)
 	return 0;
 }
 
-static void smb_direct_destroy_pools(struct smbdirect_socket *sc)
-{
-	struct smbdirect_recv_io *recvmsg;
-
-	while ((recvmsg = smbdirect_connection_get_recv_io(sc)))
-		mempool_free(recvmsg, sc->recv_io.mem.pool);
-
-	mempool_destroy(sc->recv_io.mem.pool);
-	sc->recv_io.mem.pool = NULL;
-
-	kmem_cache_destroy(sc->recv_io.mem.cache);
-	sc->recv_io.mem.cache = NULL;
-
-	mempool_destroy(sc->send_io.mem.pool);
-	sc->send_io.mem.pool = NULL;
-
-	kmem_cache_destroy(sc->send_io.mem.cache);
-	sc->send_io.mem.cache = NULL;
-}
-
-static int smb_direct_create_pools(struct smbdirect_socket *sc)
-{
-	struct smbdirect_socket_parameters *sp = &sc->parameters;
-	char name[80];
-	int i;
-	struct smbdirect_recv_io *recvmsg;
-
-	snprintf(name, sizeof(name), "smbdirect_send_io_pool_%p", sc);
-	sc->send_io.mem.cache = kmem_cache_create(name,
-					     sizeof(struct smbdirect_send_io) +
-					      sizeof(struct smbdirect_negotiate_resp),
-					     0, SLAB_HWCACHE_ALIGN, NULL);
-	if (!sc->send_io.mem.cache)
-		return -ENOMEM;
-
-	sc->send_io.mem.pool = mempool_create(sp->send_credit_target,
-					    mempool_alloc_slab, mempool_free_slab,
-					    sc->send_io.mem.cache);
-	if (!sc->send_io.mem.pool)
-		goto err;
-
-	snprintf(name, sizeof(name), "smbdirect_recv_io_pool_%p", sc);
-	sc->recv_io.mem.cache = kmem_cache_create(name,
-					     sizeof(struct smbdirect_recv_io) +
-					     sp->max_recv_size,
-					     0, SLAB_HWCACHE_ALIGN, NULL);
-	if (!sc->recv_io.mem.cache)
-		goto err;
-
-	sc->recv_io.mem.pool =
-		mempool_create(sp->recv_credit_max, mempool_alloc_slab,
-			       mempool_free_slab, sc->recv_io.mem.cache);
-	if (!sc->recv_io.mem.pool)
-		goto err;
-
-	for (i = 0; i < sp->recv_credit_max; i++) {
-		recvmsg = mempool_alloc(sc->recv_io.mem.pool, KSMBD_DEFAULT_GFP);
-		if (!recvmsg)
-			goto err;
-		recvmsg->socket = sc;
-		recvmsg->sge.length = 0;
-		list_add(&recvmsg->list, &sc->recv_io.free.list);
-	}
-
-	return 0;
-err:
-	smb_direct_destroy_pools(sc);
-	return -ENOMEM;
-}
-
 static u32 smb_direct_rdma_rw_send_wrs(struct ib_device *dev, const struct ib_qp_init_attr *attr)
 {
 	/*
@@ -2083,7 +2013,7 @@ static int smb_direct_connect(struct smbdirect_socket *sc)
 		return ret;
 	}
 
-	ret = smb_direct_create_pools(sc);
+	ret = smbdirect_connection_create_mem_pools(sc);
 	if (ret) {
 		pr_err("Can't init RDMA pool: %d\n", ret);
 		return ret;
Signature · 1 line
2.43.0
author reviewer
#108 lore raw

PATCH v2 104/127 smb: server: make use of smbdirect_map_sges_from_iter()

In reply to Stefan Metzmacher

It will make it easier to move stuff into common code when both client and server use smbdirect_map_sges_from_iter().

---
 fs/smb/server/transport_rdma.c | 58 +++++++++++-----------------------
 1 file changed, 19 insertions(+), 39 deletions(-)
fs/smb/server/transport_rdma.c +19 -39
index 3c36c4c0580d..4e2de2664e31 100644
@@ -1005,18 +1005,6 @@ static int get_sg_list(void *buf, int size, struct scatterlist *sg_list, int nen
 	return i;
 }
 
-static int get_mapped_sg_list(struct ib_device *device, void *buf, int size,
-			      struct scatterlist *sg_list, int nentries,
-			      enum dma_data_direction dir)
-{
-	int npages;
-
-	npages = get_sg_list(buf, size, sg_list, nentries);
-	if (npages < 0)
-		return -EINVAL;
-	return ib_dma_map_sg(device, sg_list, npages, dir);
-}
-
 static int post_sendmsg(struct smbdirect_socket *sc,
 			struct smbdirect_send_batch *send_ctx,
 			struct smbdirect_send_io *msg)
@@ -1060,10 +1048,9 @@ static int smb_direct_post_send_data(struct smbdirect_socket *sc,
 				     struct kvec *iov, int niov,
 				     int remaining_data_length)
 {
-	int i, j, ret;
+	int i, ret;
 	struct smbdirect_send_io *msg;
 	int data_length;
-	struct scatterlist sg[SMBDIRECT_SEND_IO_MAX_SGE - 1];
 
 	ret = wait_for_send_lcredit(sc, send_ctx);
 	if (ret)
@@ -1082,34 +1069,27 @@ static int smb_direct_post_send_data(struct smbdirect_socket *sc,
 	if (ret)
 		goto header_failed;
 
-	for (i = 0; i < niov; i++) {
-		struct ib_sge *sge;
-		int sg_cnt;
-
-		sg_init_table(sg, SMBDIRECT_SEND_IO_MAX_SGE - 1);
-		sg_cnt = get_mapped_sg_list(sc->ib.dev,
-					    iov[i].iov_base, iov[i].iov_len,
-					    sg, SMBDIRECT_SEND_IO_MAX_SGE - 1,
-					    DMA_TO_DEVICE);
-		if (sg_cnt <= 0) {
-			pr_err("failed to map buffer\n");
-			ret = -ENOMEM;
+	if (data_length) {
+		struct smbdirect_map_sges extract = {
+			.num_sge	= msg->num_sge,
+			.max_sge	= ARRAY_SIZE(msg->sge),
+			.sge		= msg->sge,
+			.device		= sc->ib.dev,
+			.local_dma_lkey	= sc->ib.pd->local_dma_lkey,
+			.direction	= DMA_TO_DEVICE,
+		};
+		struct iov_iter iter;
+
+		iov_iter_kvec(&iter, ITER_SOURCE, iov, niov, data_length);
+
+		ret = smbdirect_map_sges_from_iter(&iter, data_length, &extract);
+		if (ret < 0)
 			goto err;
-		} else if (sg_cnt + msg->num_sge > SMBDIRECT_SEND_IO_MAX_SGE) {
-			pr_err("buffer not fitted into sges\n");
-			ret = -E2BIG;
-			ib_dma_unmap_sg(sc->ib.dev, sg, sg_cnt,
-					DMA_TO_DEVICE);
+		if (WARN_ON_ONCE(ret != data_length)) {
+			ret = -EIO;
 			goto err;
 		}
-
-		for (j = 0; j < sg_cnt; j++) {
-			sge = &msg->sge[msg->num_sge];
-			sge->addr = sg_dma_address(&sg[j]);
-			sge->length = sg_dma_len(&sg[j]);
-			sge->lkey  = sc->ib.pd->local_dma_lkey;
-			msg->num_sge++;
-		}
+		msg->num_sge = extract.num_sge;
 	}
 
 	ret = post_sendmsg(sc, send_ctx, msg);
Signature · 1 line
2.43.0
author reviewer
#109 lore raw

PATCH v2 105/127 smb: server: make use of smbdirect_connection_qp_event_handler()

In reply to Stefan Metzmacher

This is a copy of smb_direct_qpair_handler()...

It will allow more code to be moved to common functions soon.

---
 fs/smb/server/transport_rdma.c | 19 +------------------
 1 file changed, 1 insertion(+), 18 deletions(-)
fs/smb/server/transport_rdma.c +1 -18
index 4e2de2664e31..ef2de6302768 100644
@@ -1501,23 +1501,6 @@ static int smb_direct_cm_handler(struct rdma_cm_id *cm_id,
 	return 0;
 }
 
-static void smb_direct_qpair_handler(struct ib_event *event, void *context)
-{
-	struct smbdirect_socket *sc = context;
-
-	ksmbd_debug(RDMA, "Received QP event. cm_id=%p, event=%s (%d)\n",
-		    sc->rdma.cm_id, ib_event_msg(event->event), event->event);
-
-	switch (event->event) {
-	case IB_EVENT_CQ_ERR:
-	case IB_EVENT_QP_FATAL:
-		smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
-		break;
-	default:
-		break;
-	}
-}
-
 static int smb_direct_send_negotiate_response(struct smbdirect_socket *sc,
 					      int failed)
 {
@@ -1857,7 +1840,7 @@ static int smb_direct_create_qpair(struct smbdirect_socket *sc)
 	 * again if max_rdma_ctxs is not 0.
 	 */
 	memset(&qp_attr, 0, sizeof(qp_attr));
-	qp_attr.event_handler = smb_direct_qpair_handler;
+	qp_attr.event_handler = smbdirect_connection_qp_event_handler;
 	qp_attr.qp_context = sc;
 	qp_attr.cap = qp_cap;
 	qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
Signature · 1 line
2.43.0
author reviewer
#110 lore raw

PATCH v2 106/127 smb: server: make use of smbdirect_connection_negotiate_rdma_resources()

In reply to Stefan Metzmacher

It's good to have this logic in a central place, it will allow us share more code soon.

---
 fs/smb/server/transport_rdma.c | 62 ++++------------------------------
 1 file changed, 7 insertions(+), 55 deletions(-)
fs/smb/server/transport_rdma.c +7 -55
index ef2de6302768..526ad5c19b6e 100644
@@ -2020,66 +2020,18 @@ static int smb_direct_handle_connect_request(struct rdma_cm_id *new_cm_id,
 	sc = &t->socket;
 	sp = &sc->parameters;
 
-	peer_initiator_depth = event->param.conn.initiator_depth;
-	peer_responder_resources = event->param.conn.responder_resources;
-	if (rdma_protocol_iwarp(new_cm_id->device, new_cm_id->port_num) &&
-	    event->param.conn.private_data_len == 8) {
-		/*
-		 * Legacy clients with only iWarp MPA v1 support
-		 * need a private blob in order to negotiate
-		 * the IRD/ORD values.
-		 */
-		const __be32 *ird_ord_hdr = event->param.conn.private_data;
-		u32 ird32 = be32_to_cpu(ird_ord_hdr[0]);
-		u32 ord32 = be32_to_cpu(ird_ord_hdr[1]);
-
-		/*
-		 * cifs.ko sends the legacy IRD/ORD negotiation
-		 * event if iWarp MPA v2 was used.
-		 *
-		 * Here we check that the values match and only
-		 * mark the client as legacy if they don't match.
-		 */
-		if ((u32)event->param.conn.initiator_depth != ird32 ||
-		    (u32)event->param.conn.responder_resources != ord32) {
-			/*
-			 * There are broken clients (old cifs.ko)
-			 * using little endian and also
-			 * struct rdma_conn_param only uses u8
-			 * for initiator_depth and responder_resources,
-			 * so we truncate the value to U8_MAX.
-			 *
-			 * smb_direct_accept_client() will then
-			 * do the real negotiation in order to
-			 * select the minimum between client and
-			 * server.
-			 */
-			ird32 = min_t(u32, ird32, U8_MAX);
-			ord32 = min_t(u32, ord32, U8_MAX);
-
-			sc->rdma.legacy_iwarp = true;
-			peer_initiator_depth = (u8)ird32;
-			peer_responder_resources = (u8)ord32;
-		}
-	}
-
 	/*
 	 * First set what the we as server are able to support
 	 */
 	sp->initiator_depth = min_t(u8, sp->initiator_depth,
-				   new_cm_id->device->attrs.max_qp_rd_atom);
+				    sc->ib.dev->attrs.max_qp_rd_atom);
 
-	/*
-	 * negotiate the value by using the minimum
-	 * between client and server if the client provided
-	 * non 0 values.
-	 */
-	if (peer_initiator_depth != 0)
-		sp->initiator_depth = min_t(u8, sp->initiator_depth,
-					   peer_initiator_depth);
-	if (peer_responder_resources != 0)
-		sp->responder_resources = min_t(u8, sp->responder_resources,
-					       peer_responder_resources);
+	peer_initiator_depth = event->param.conn.initiator_depth;
+	peer_responder_resources = event->param.conn.responder_resources;
+	smbdirect_connection_negotiate_rdma_resources(sc,
+						      peer_initiator_depth,
+						      peer_responder_resources,
+						      &event->param.conn);
 
 	ret = smb_direct_connect(sc);
 	if (ret)
Signature · 1 line
2.43.0
author reviewer
#111 lore raw

PATCH v2 107/127 smb: server: make use of smbdirect_connection_{create,destroy}_qp()

In reply to Stefan Metzmacher

It's good a use common code for this and it will allow us to share more code in the next steps.

Calling ib_drain_qp() twice is ok.

---
 fs/smb/server/transport_rdma.c | 217 +--------------------------------
 1 file changed, 6 insertions(+), 211 deletions(-)
fs/smb/server/transport_rdma.c +6 -211
index 526ad5c19b6e..2f55764a5f2e 100644
@@ -257,6 +257,7 @@ static struct smb_direct_transport *alloc_transport(struct rdma_cm_id *cm_id)
 	smbdirect_socket_set_logging(sc, NULL,
 				     smb_direct_logging_needed,
 				     smb_direct_logging_vaprintf);
+	sc->ib.poll_ctx = IB_POLL_WORKQUEUE;
 	sc->send_io.mem.gfp_mask = KSMBD_DEFAULT_GFP;
 	sc->recv_io.mem.gfp_mask = KSMBD_DEFAULT_GFP;
 	/*
@@ -318,11 +319,8 @@ static void free_transport(struct smb_direct_transport *t)
 	if (sc->rdma.cm_id)
 		rdma_lock_handler(sc->rdma.cm_id);
 
-	if (sc->ib.qp) {
+	if (sc->ib.qp)
 		ib_drain_qp(sc->ib.qp);
-		sc->ib.qp = NULL;
-		rdma_destroy_qp(sc->rdma.cm_id);
-	}
 
 	ksmbd_debug(RDMA, "drain the reassembly queue\n");
 	do {
@@ -340,12 +338,7 @@ static void free_transport(struct smb_direct_transport *t)
 	} while (recvmsg);
 	sc->recv_io.reassembly.data_length = 0;
 
-	if (sc->ib.send_cq)
-		ib_free_cq(sc->ib.send_cq);
-	if (sc->ib.recv_cq)
-		ib_free_cq(sc->ib.recv_cq);
-	if (sc->ib.pd)
-		ib_dealloc_pd(sc->ib.pd);
+	smbdirect_connection_destroy_qp(sc);
 	if (sc->rdma.cm_id) {
 		rdma_unlock_handler(sc->rdma.cm_id);
 		rdma_destroy_id(sc->rdma.cm_id);
@@ -1679,206 +1672,6 @@ static int smb_direct_init_params(struct smbdirect_socket *sc)
 	return 0;
 }
 
-static u32 smb_direct_rdma_rw_send_wrs(struct ib_device *dev, const struct ib_qp_init_attr *attr)
-{
-	/*
-	 * This could be split out of rdma_rw_init_qp()
-	 * and be a helper function next to rdma_rw_mr_factor()
-	 *
-	 * We can't check unlikely(rdma_rw_force_mr) here,
-	 * but that is most likely 0 anyway.
-	 */
-	u32 factor;
-
-	WARN_ON_ONCE(attr->port_num == 0);
-
-	/*
-	 * Each context needs at least one RDMA READ or WRITE WR.
-	 *
-	 * For some hardware we might need more, eventually we should ask the
-	 * HCA driver for a multiplier here.
-	 */
-	factor = 1;
-
-	/*
-	 * If the device needs MRs to perform RDMA READ or WRITE operations,
-	 * we'll need two additional MRs for the registrations and the
-	 * invalidation.
-	 */
-	if (rdma_protocol_iwarp(dev, attr->port_num) || dev->attrs.max_sgl_rd)
-		factor += 2;	/* inv + reg */
-
-	return factor * attr->cap.max_rdma_ctxs;
-}
-
-static int smb_direct_create_qpair(struct smbdirect_socket *sc)
-{
-	struct smbdirect_socket_parameters *sp = &sc->parameters;
-	int ret;
-	struct ib_qp_cap qp_cap;
-	struct ib_qp_init_attr qp_attr;
-	u32 max_send_wr;
-	u32 rdma_send_wr;
-
-	/*
-	 * Note that {rdma,ib}_create_qp() will call
-	 * rdma_rw_init_qp() if cap->max_rdma_ctxs is not 0.
-	 * It will adjust cap->max_send_wr to the required
-	 * number of additional WRs for the RDMA RW operations.
-	 * It will cap cap->max_send_wr to the device limit.
-	 *
-	 * +1 for ib_drain_qp
-	 */
-	qp_cap.max_send_wr = sp->send_credit_target + 1;
-	qp_cap.max_recv_wr = sp->recv_credit_max + 1;
-	qp_cap.max_send_sge = SMBDIRECT_SEND_IO_MAX_SGE;
-	qp_cap.max_recv_sge = SMBDIRECT_RECV_IO_MAX_SGE;
-	qp_cap.max_inline_data = 0;
-	qp_cap.max_rdma_ctxs = sc->rw_io.credits.max;
-
-	/*
-	 * Find out the number of max_send_wr
-	 * after rdma_rw_init_qp() adjusted it.
-	 *
-	 * We only do it on a temporary variable,
-	 * as rdma_create_qp() will trigger
-	 * rdma_rw_init_qp() again.
-	 */
-	memset(&qp_attr, 0, sizeof(qp_attr));
-	qp_attr.cap = qp_cap;
-	qp_attr.port_num = sc->rdma.cm_id->port_num;
-	rdma_send_wr = smb_direct_rdma_rw_send_wrs(sc->ib.dev, &qp_attr);
-	max_send_wr = qp_cap.max_send_wr + rdma_send_wr;
-
-	if (qp_cap.max_send_wr > sc->ib.dev->attrs.max_cqe ||
-	    qp_cap.max_send_wr > sc->ib.dev->attrs.max_qp_wr) {
-		pr_err("Possible CQE overrun: max_send_wr %d\n",
-		       qp_cap.max_send_wr);
-		pr_err("device %.*s reporting max_cqe %d max_qp_wr %d\n",
-		       IB_DEVICE_NAME_MAX,
-		       sc->ib.dev->name,
-		       sc->ib.dev->attrs.max_cqe,
-		       sc->ib.dev->attrs.max_qp_wr);
-		pr_err("consider lowering send_credit_target = %d\n",
-		       sp->send_credit_target);
-		return -EINVAL;
-	}
-
-	if (qp_cap.max_rdma_ctxs &&
-	    (max_send_wr >= sc->ib.dev->attrs.max_cqe ||
-	     max_send_wr >= sc->ib.dev->attrs.max_qp_wr)) {
-		pr_err("Possible CQE overrun: rdma_send_wr %d + max_send_wr %d = %d\n",
-		       rdma_send_wr, qp_cap.max_send_wr, max_send_wr);
-		pr_err("device %.*s reporting max_cqe %d max_qp_wr %d\n",
-		       IB_DEVICE_NAME_MAX,
-		       sc->ib.dev->name,
-		       sc->ib.dev->attrs.max_cqe,
-		       sc->ib.dev->attrs.max_qp_wr);
-		pr_err("consider lowering send_credit_target = %d, max_rdma_ctxs = %d\n",
-		       sp->send_credit_target, qp_cap.max_rdma_ctxs);
-		return -EINVAL;
-	}
-
-	if (qp_cap.max_recv_wr > sc->ib.dev->attrs.max_cqe ||
-	    qp_cap.max_recv_wr > sc->ib.dev->attrs.max_qp_wr) {
-		pr_err("Possible CQE overrun: max_recv_wr %d\n",
-		       qp_cap.max_recv_wr);
-		pr_err("device %.*s reporting max_cqe %d max_qp_wr %d\n",
-		       IB_DEVICE_NAME_MAX,
-		       sc->ib.dev->name,
-		       sc->ib.dev->attrs.max_cqe,
-		       sc->ib.dev->attrs.max_qp_wr);
-		pr_err("consider lowering receive_credit_max = %d\n",
-		       sp->recv_credit_max);
-		return -EINVAL;
-	}
-
-	if (qp_cap.max_send_sge > sc->ib.dev->attrs.max_send_sge ||
-	    qp_cap.max_recv_sge > sc->ib.dev->attrs.max_recv_sge) {
-		pr_err("device %.*s max_send_sge/max_recv_sge = %d/%d too small\n",
-		       IB_DEVICE_NAME_MAX,
-		       sc->ib.dev->name,
-		       sc->ib.dev->attrs.max_send_sge,
-		       sc->ib.dev->attrs.max_recv_sge);
-		return -EINVAL;
-	}
-
-	sc->ib.pd = ib_alloc_pd(sc->ib.dev, 0);
-	if (IS_ERR(sc->ib.pd)) {
-		pr_err("Can't create RDMA PD\n");
-		ret = PTR_ERR(sc->ib.pd);
-		sc->ib.pd = NULL;
-		return ret;
-	}
-
-	sc->ib.send_cq = ib_alloc_cq_any(sc->ib.dev, sc,
-					 max_send_wr,
-					 IB_POLL_WORKQUEUE);
-	if (IS_ERR(sc->ib.send_cq)) {
-		pr_err("Can't create RDMA send CQ\n");
-		ret = PTR_ERR(sc->ib.send_cq);
-		sc->ib.send_cq = NULL;
-		goto err;
-	}
-
-	sc->ib.recv_cq = ib_alloc_cq_any(sc->ib.dev, sc,
-					 qp_cap.max_recv_wr,
-					 IB_POLL_WORKQUEUE);
-	if (IS_ERR(sc->ib.recv_cq)) {
-		pr_err("Can't create RDMA recv CQ\n");
-		ret = PTR_ERR(sc->ib.recv_cq);
-		sc->ib.recv_cq = NULL;
-		goto err;
-	}
-
-	/*
-	 * We reset completely here!
-	 * As the above use was just temporary
-	 * to calc max_send_wr and rdma_send_wr.
-	 *
-	 * rdma_create_qp() will trigger rdma_rw_init_qp()
-	 * again if max_rdma_ctxs is not 0.
-	 */
-	memset(&qp_attr, 0, sizeof(qp_attr));
-	qp_attr.event_handler = smbdirect_connection_qp_event_handler;
-	qp_attr.qp_context = sc;
-	qp_attr.cap = qp_cap;
-	qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
-	qp_attr.qp_type = IB_QPT_RC;
-	qp_attr.send_cq = sc->ib.send_cq;
-	qp_attr.recv_cq = sc->ib.recv_cq;
-	qp_attr.port_num = ~0;
-
-	ret = rdma_create_qp(sc->rdma.cm_id, sc->ib.pd, &qp_attr);
-	if (ret) {
-		pr_err("Can't create RDMA QP: %d\n", ret);
-		goto err;
-	}
-
-	sc->ib.qp = sc->rdma.cm_id->qp;
-	sc->rdma.cm_id->event_handler = smb_direct_cm_handler;
-
-	return 0;
-err:
-	if (sc->ib.qp) {
-		sc->ib.qp = NULL;
-		rdma_destroy_qp(sc->rdma.cm_id);
-	}
-	if (sc->ib.recv_cq) {
-		ib_destroy_cq(sc->ib.recv_cq);
-		sc->ib.recv_cq = NULL;
-	}
-	if (sc->ib.send_cq) {
-		ib_destroy_cq(sc->ib.send_cq);
-		sc->ib.send_cq = NULL;
-	}
-	if (sc->ib.pd) {
-		ib_dealloc_pd(sc->ib.pd);
-		sc->ib.pd = NULL;
-	}
-	return ret;
-}
-
 static int smb_direct_prepare(struct ksmbd_transport *t)
 {
 	struct smb_direct_transport *st = SMBD_TRANS(t);
@@ -1970,6 +1763,8 @@ static int smb_direct_connect(struct smbdirect_socket *sc)
 {
 	int ret;
 
+	sc->rdma.cm_id->event_handler = smb_direct_cm_handler;
+
 	ret = smb_direct_init_params(sc);
 	if (ret) {
 		pr_err("Can't configure RDMA parameters\n");
@@ -1982,7 +1777,7 @@ static int smb_direct_connect(struct smbdirect_socket *sc)
 		return ret;
 	}
 
-	ret = smb_direct_create_qpair(sc);
+	ret = smbdirect_connection_create_qp(sc);
 	if (ret) {
 		pr_err("Can't accept RDMA client: %d\n", ret);
 		return ret;
Signature · 1 line
2.43.0
author reviewer
#112 lore raw

PATCH v2 108/127 smb: server: initialize recv_io->cqe.done = recv_done just once

In reply to Stefan Metzmacher

smbdirect_recv_io structures are pre-allocated so we can set the callback function just once.

This will make it easy to move smb_direct_post_recv to common code soon.

---
 fs/smb/server/transport_rdma.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/smb/server/transport_rdma.c +4 -1
index 2f55764a5f2e..0919ef007602 100644
@@ -547,7 +547,6 @@ static int smb_direct_post_recv(struct smbdirect_socket *sc,
 		return ret;
 	recvmsg->sge.length = sp->max_recv_size;
 	recvmsg->sge.lkey = sc->ib.pd->local_dma_lkey;
-	recvmsg->cqe.done = recv_done;
 
 	wr.wr_cqe = &recvmsg->cqe;
 	wr.next = NULL;
@@ -1761,6 +1760,7 @@ static int smb_direct_prepare(struct ksmbd_transport *t)
 
 static int smb_direct_connect(struct smbdirect_socket *sc)
 {
+	struct smbdirect_recv_io *recv_io;
 	int ret;
 
 	sc->rdma.cm_id->event_handler = smb_direct_cm_handler;
@@ -1777,6 +1777,9 @@ static int smb_direct_connect(struct smbdirect_socket *sc)
 		return ret;
 	}
 
+	list_for_each_entry(recv_io, &sc->recv_io.free.list, list)
+		recv_io->cqe.done = recv_done;
+
 	ret = smbdirect_connection_create_qp(sc);
 	if (ret) {
 		pr_err("Can't accept RDMA client: %d\n", ret);
Signature · 1 line
2.43.0
author reviewer
#113 lore raw

PATCH v2 109/127 smb: server: make use of smbdirect_connection_post_recv_io()

In reply to Stefan Metzmacher

The only difference is that smbdirect_connection_post_recv_io() returns early if the connection is already broken.

---
 fs/smb/server/transport_rdma.c | 39 ++--------------------------------
 1 file changed, 2 insertions(+), 37 deletions(-)
fs/smb/server/transport_rdma.c +2 -37
index 0919ef007602..4acae8e43b76 100644
@@ -531,41 +531,6 @@ static void recv_done(struct ib_cq *cq, struct ib_wc *wc)
 	smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
 }
 
-static int smb_direct_post_recv(struct smbdirect_socket *sc,
-				struct smbdirect_recv_io *recvmsg)
-{
-	struct smbdirect_socket_parameters *sp = &sc->parameters;
-	struct ib_recv_wr wr;
-	int ret;
-
-	recvmsg->sge.addr = ib_dma_map_single(sc->ib.dev,
-					      recvmsg->packet,
-					      sp->max_recv_size,
-					      DMA_FROM_DEVICE);
-	ret = ib_dma_mapping_error(sc->ib.dev, recvmsg->sge.addr);
-	if (ret)
-		return ret;
-	recvmsg->sge.length = sp->max_recv_size;
-	recvmsg->sge.lkey = sc->ib.pd->local_dma_lkey;
-
-	wr.wr_cqe = &recvmsg->cqe;
-	wr.next = NULL;
-	wr.sg_list = &recvmsg->sge;
-	wr.num_sge = 1;
-
-	ret = ib_post_recv(sc->ib.qp, &wr, NULL);
-	if (ret) {
-		pr_err("Can't post recv: %d\n", ret);
-		ib_dma_unmap_single(sc->ib.dev,
-				    recvmsg->sge.addr, recvmsg->sge.length,
-				    DMA_FROM_DEVICE);
-		recvmsg->sge.length = 0;
-		smbdirect_connection_schedule_disconnect(sc, ret);
-		return ret;
-	}
-	return ret;
-}
-
 static int smb_direct_read(struct ksmbd_transport *t, char *buf,
 			   unsigned int size, int unused)
 {
@@ -702,7 +667,7 @@ static void smb_direct_post_recv_credits(struct work_struct *work)
 
 			recvmsg->first_segment = false;
 
-			ret = smb_direct_post_recv(sc, recvmsg);
+			ret = smbdirect_connection_post_recv_io(recvmsg);
 			if (ret) {
 				pr_err("Can't post recv: %d\n", ret);
 				smbdirect_connection_put_recv_io(recvmsg);
@@ -1621,7 +1586,7 @@ static int smb_direct_prepare_negotiation(struct smbdirect_socket *sc)
 	if (!recvmsg)
 		return -ENOMEM;
 
-	ret = smb_direct_post_recv(sc, recvmsg);
+	ret = smbdirect_connection_post_recv_io(recvmsg);
 	if (ret) {
 		pr_err("Can't post recv: %d\n", ret);
 		goto out_err;
Signature · 1 line
2.43.0
author reviewer
#114 lore raw

PATCH v2 110/127 smb: server: make use of smbdirect_connection_recv_io_refill[_work]()

In reply to Stefan Metzmacher

This is basically a copy of smb_direct_post_recv_credits(), but there are several improvements compared to the existing function:

1. We calculate the number of missing posted buffers by getting the difference between recv_io.credits.target and recv_io.posted.count.

Instead of the difference between recv_io.credits.target and recv_io.credits.count, because recv_io.credits.count is only updated once a message is send to the peer.

It was not really a problem before, because we have a fixed number smbdirect_recv_io buffers, so the loop terminated when smbdirect_connection_get_recv_io() returns NULL.

But using recv_io.posted.count makes it easier to understand.

2. In order to tell the peer about the newly posted buffer and grant the credits, we only trigger the send immediate when we're not granting only the last possible credit.

This is mostly a difference relative to the servers smb_direct_post_recv_credits() implementation, which should avoid useless ping pong messages.

---
 fs/smb/server/transport_rdma.c | 53 +++++++---------------------------
 1 file changed, 10 insertions(+), 43 deletions(-)
fs/smb/server/transport_rdma.c +10 -43
index 4acae8e43b76..9cc8cffcc6e9 100644
@@ -202,7 +202,6 @@ static inline int get_buf_page_count(void *buf, int size)
 		(uintptr_t)buf / PAGE_SIZE;
 }
 
-static void smb_direct_post_recv_credits(struct work_struct *work);
 static int smb_direct_post_send_data(struct smbdirect_socket *sc,
 				     struct smbdirect_send_batch *send_ctx,
 				     struct kvec *iov, int niov,
@@ -651,38 +650,6 @@ static int smb_direct_read(struct ksmbd_transport *t, char *buf,
 	goto again;
 }
 
-static void smb_direct_post_recv_credits(struct work_struct *work)
-{
-	struct smbdirect_socket *sc =
-		container_of(work, struct smbdirect_socket, recv_io.posted.refill_work);
-	struct smbdirect_recv_io *recvmsg;
-	int credits = 0;
-	int ret;
-
-	if (atomic_read(&sc->recv_io.credits.count) < sc->recv_io.credits.target) {
-		while (true) {
-			recvmsg = smbdirect_connection_get_recv_io(sc);
-			if (!recvmsg)
-				break;
-
-			recvmsg->first_segment = false;
-
-			ret = smbdirect_connection_post_recv_io(recvmsg);
-			if (ret) {
-				pr_err("Can't post recv: %d\n", ret);
-				smbdirect_connection_put_recv_io(recvmsg);
-				break;
-			}
-			credits++;
-
-			atomic_inc(&sc->recv_io.posted.count);
-		}
-	}
-
-	if (credits)
-		queue_work(sc->workqueue, &sc->idle.immediate_work);
-}
-
 static int manage_credits_prior_sending(struct smbdirect_socket *sc)
 {
 	int new_credits;
@@ -1703,24 +1670,24 @@ static int smb_direct_prepare(struct ksmbd_transport *t)
 
 	/*
 	 * We negotiated with success, so we need to refill the recv queue.
-	 * We do that with sc->idle.immediate_work still being disabled
-	 * via smbdirect_socket_init(), so that queue_work(sc->workqueue,
-	 * &sc->idle.immediate_work) in smb_direct_post_recv_credits()
-	 * is a no-op.
 	 *
 	 * The message that grants the credits to the client is
 	 * the negotiate response.
 	 */
-	INIT_WORK(&sc->recv_io.posted.refill_work, smb_direct_post_recv_credits);
-	smb_direct_post_recv_credits(&sc->recv_io.posted.refill_work);
-	if (unlikely(sc->first_error))
-		return sc->first_error;
-	INIT_WORK(&sc->idle.immediate_work, smb_direct_send_immediate_work);
+	ret = smbdirect_connection_recv_io_refill(sc);
+	if (ret < 0)
+		return ret;
+	ret = 0;
 
 respond:
 	ret = smb_direct_send_negotiate_response(sc, ret);
+	if (ret)
+		return ret;
 
-	return ret;
+	INIT_WORK(&sc->recv_io.posted.refill_work, smbdirect_connection_recv_io_refill_work);
+	INIT_WORK(&sc->idle.immediate_work, smb_direct_send_immediate_work);
+
+	return 0;
 }
 
 static int smb_direct_connect(struct smbdirect_socket *sc)
Signature · 1 line
2.43.0
author reviewer
#115 lore raw

PATCH v2 111/127 smb: server: make use of smbdirect_get_buf_page_count()

In reply to Stefan Metzmacher

This will allow us to move code into common code between client and server soon.

---
 fs/smb/server/transport_rdma.c | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)
fs/smb/server/transport_rdma.c +6 -12
index 9cc8cffcc6e9..d8829cb57270 100644
@@ -196,12 +196,6 @@ unsigned int get_smbd_max_read_write_size(struct ksmbd_transport *kt)
 	return sp->max_read_write_size;
 }
 
-static inline int get_buf_page_count(void *buf, int size)
-{
-	return DIV_ROUND_UP((uintptr_t)buf + size, PAGE_SIZE) -
-		(uintptr_t)buf / PAGE_SIZE;
-}
-
 static int smb_direct_post_send_data(struct smbdirect_socket *sc,
 				     struct smbdirect_send_batch *send_ctx,
 				     struct kvec *iov, int niov,
@@ -828,7 +822,7 @@ static int wait_for_rw_credits(struct smbdirect_socket *sc, int credits)
 static int calc_rw_credits(struct smbdirect_socket *sc,
 			   char *buf, unsigned int len)
 {
-	return DIV_ROUND_UP(get_buf_page_count(buf, len),
+	return DIV_ROUND_UP(smbdirect_get_buf_page_count(buf, len),
 			    sc->rw_io.credits.num_pages);
 }
 
@@ -904,7 +898,7 @@ static int get_sg_list(void *buf, int size, struct scatterlist *sg_list, int nen
 	int offset, len;
 	int i = 0;
 
-	if (size <= 0 || nentries < get_buf_page_count(buf, size))
+	if (size <= 0 || nentries < smbdirect_get_buf_page_count(buf, size))
 		return -EINVAL;
 
 	offset = offset_in_page(buf);
@@ -1095,7 +1089,7 @@ static int smb_direct_writev(struct ksmbd_transport *t,
 			v->iov_len = min_t(size_t,
 					   iov[iov_idx].iov_len - iov_ofs,
 					   possible_bytes);
-			page_count = get_buf_page_count(v->iov_base, v->iov_len);
+			page_count = smbdirect_get_buf_page_count(v->iov_base, v->iov_len);
 			if (page_count > possible_vecs) {
 				/*
 				 * If the number of pages in the buffer
@@ -1124,7 +1118,7 @@ static int smb_direct_writev(struct ksmbd_transport *t,
 				size_t elen = min_t(size_t, v->iov_len - fplen, epages*PAGE_SIZE);
 
 				v->iov_len = fplen + elen;
-				page_count = get_buf_page_count(v->iov_base, v->iov_len);
+				page_count = smbdirect_get_buf_page_count(v->iov_base, v->iov_len);
 				if (WARN_ON_ONCE(page_count > possible_vecs)) {
 					/*
 					 * Something went wrong in the above
@@ -1289,7 +1283,7 @@ static int smb_direct_rdma_xmit(struct smb_direct_transport *t,
 
 		msg->sgt.sgl = &msg->sg_list[0];
 		ret = sg_alloc_table_chained(&msg->sgt,
-					     get_buf_page_count(desc_buf, desc_buf_len),
+					     smbdirect_get_buf_page_count(desc_buf, desc_buf_len),
 					     msg->sg_list, SG_CHUNK_SIZE);
 		if (ret) {
 			ret = -ENOMEM;
@@ -1303,7 +1297,7 @@ static int smb_direct_rdma_xmit(struct smb_direct_transport *t,
 
 		ret = rdma_rw_ctx_init(&msg->rdma_ctx, sc->ib.qp, sc->ib.qp->port,
 				       msg->sgt.sgl,
-				       get_buf_page_count(desc_buf, desc_buf_len),
+				       smbdirect_get_buf_page_count(desc_buf, desc_buf_len),
 				       0,
 				       le64_to_cpu(desc[i].offset),
 				       le32_to_cpu(desc[i].token),
Signature · 1 line
2.43.0
author reviewer
#116 lore raw

PATCH v2 112/127 smb: server: make use of smbdirect_connection_wait_for_credits()

In reply to Stefan Metzmacher

This will allow us to share more common code between client and server soon.

---
 fs/smb/server/transport_rdma.c | 42 ++++++++++------------------------
 1 file changed, 12 insertions(+), 30 deletions(-)
fs/smb/server/transport_rdma.c +12 -30
index d8829cb57270..3902ba403c0f 100644
@@ -757,27 +757,6 @@ static int smb_direct_flush_send_list(struct smbdirect_socket *sc,
 	return ret;
 }
 
-static int wait_for_credits(struct smbdirect_socket *sc,
-			    wait_queue_head_t *waitq, atomic_t *total_credits,
-			    int needed)
-{
-	int ret;
-
-	do {
-		if (atomic_sub_return(needed, total_credits) >= 0)
-			return 0;
-
-		atomic_add(needed, total_credits);
-		ret = wait_event_interruptible(*waitq,
-					       atomic_read(total_credits) >= needed ||
-					       sc->status != SMBDIRECT_SOCKET_CONNECTED);
-
-		if (sc->status != SMBDIRECT_SOCKET_CONNECTED)
-			return -ENOTCONN;
-		else if (ret < 0)
-			return ret;
-	} while (true);
-}
 
 static int wait_for_send_lcredit(struct smbdirect_socket *sc,
 				 struct smbdirect_send_batch *send_ctx)
@@ -790,10 +769,10 @@ static int wait_for_send_lcredit(struct smbdirect_socket *sc,
 			return ret;
 	}
 
-	return wait_for_credits(sc,
-				&sc->send_io.lcredits.wait_queue,
-				&sc->send_io.lcredits.count,
-				1);
+	return smbdirect_connection_wait_for_credits(sc,
+						     &sc->send_io.lcredits.wait_queue,
+						     &sc->send_io.lcredits.count,
+						     1);
 }
 
 static int wait_for_send_credits(struct smbdirect_socket *sc,
@@ -808,15 +787,18 @@ static int wait_for_send_credits(struct smbdirect_socket *sc,
 			return ret;
 	}
 
-	return wait_for_credits(sc, &sc->send_io.credits.wait_queue, &sc->send_io.credits.count, 1);
+	return smbdirect_connection_wait_for_credits(sc,
+						     &sc->send_io.credits.wait_queue,
+						     &sc->send_io.credits.count,
+						     1);
 }
 
 static int wait_for_rw_credits(struct smbdirect_socket *sc, int credits)
 {
-	return wait_for_credits(sc,
-				&sc->rw_io.credits.wait_queue,
-				&sc->rw_io.credits.count,
-				credits);
+	return smbdirect_connection_wait_for_credits(sc,
+						     &sc->rw_io.credits.wait_queue,
+						     &sc->rw_io.credits.count,
+						     credits);
 }
 
 static int calc_rw_credits(struct smbdirect_socket *sc,
Signature · 1 line
2.43.0
author reviewer
#117 lore raw

PATCH v2 113/127 smb: server: make use of functions from smbdirect_rw.c

In reply to Stefan Metzmacher

The copied code only got new names, some indentation/formatting changes, some variable names are changed too.

They also only use struct smbdirect_socket instead of struct smb_direct_transport.

But the logic is still the same.

---
 fs/smb/server/transport_rdma.c | 234 ++-------------------------------
 1 file changed, 11 insertions(+), 223 deletions(-)
fs/smb/server/transport_rdma.c +11 -223
index 3902ba403c0f..2845f58301fb 100644
@@ -253,6 +253,7 @@ static struct smb_direct_transport *alloc_transport(struct rdma_cm_id *cm_id)
 	sc->ib.poll_ctx = IB_POLL_WORKQUEUE;
 	sc->send_io.mem.gfp_mask = KSMBD_DEFAULT_GFP;
 	sc->recv_io.mem.gfp_mask = KSMBD_DEFAULT_GFP;
+	sc->rw_io.mem.gfp_mask = KSMBD_DEFAULT_GFP;
 	/*
 	 * from here we operate on the copy.
 	 */
@@ -793,21 +794,6 @@ static int wait_for_send_credits(struct smbdirect_socket *sc,
 						     1);
 }
 
-static int wait_for_rw_credits(struct smbdirect_socket *sc, int credits)
-{
-	return smbdirect_connection_wait_for_credits(sc,
-						     &sc->rw_io.credits.wait_queue,
-						     &sc->rw_io.credits.count,
-						     credits);
-}
-
-static int calc_rw_credits(struct smbdirect_socket *sc,
-			   char *buf, unsigned int len)
-{
-	return DIV_ROUND_UP(smbdirect_get_buf_page_count(buf, len),
-			    sc->rw_io.credits.num_pages);
-}
-
 static int smb_direct_create_header(struct smbdirect_socket *sc,
 				    int size, int remaining_data_length,
 				    struct smbdirect_send_io **sendmsg_out)
@@ -873,38 +859,6 @@ static int smb_direct_create_header(struct smbdirect_socket *sc,
 	return 0;
 }
 
-static int get_sg_list(void *buf, int size, struct scatterlist *sg_list, int nentries)
-{
-	bool high = is_vmalloc_addr(buf);
-	struct page *page;
-	int offset, len;
-	int i = 0;
-
-	if (size <= 0 || nentries < smbdirect_get_buf_page_count(buf, size))
-		return -EINVAL;
-
-	offset = offset_in_page(buf);
-	buf -= offset;
-	while (size > 0) {
-		len = min_t(int, PAGE_SIZE - offset, size);
-		if (high)
-			page = vmalloc_to_page(buf);
-		else
-			page = kmap_to_page(buf);
-
-		if (!sg_list)
-			return -EINVAL;
-		sg_set_page(sg_list, page, len, offset);
-		sg_list = sg_next(sg_list);
-
-		buf += PAGE_SIZE;
-		size -= len;
-		offset = 0;
-		i++;
-	}
-	return i;
-}
-
 static int post_sendmsg(struct smbdirect_socket *sc,
 			struct smbdirect_send_batch *send_ctx,
 			struct smbdirect_send_io *msg)
@@ -1154,185 +1108,16 @@ static int smb_direct_writev(struct ksmbd_transport *t,
 	return ret;
 }
 
-static void smb_direct_free_rdma_rw_msg(struct smb_direct_transport *t,
-					struct smbdirect_rw_io *msg,
-					enum dma_data_direction dir)
-{
-	struct smbdirect_socket *sc = &t->socket;
-
-	rdma_rw_ctx_destroy(&msg->rdma_ctx, sc->ib.qp, sc->ib.qp->port,
-			    msg->sgt.sgl, msg->sgt.nents, dir);
-	sg_free_table_chained(&msg->sgt, SG_CHUNK_SIZE);
-	kfree(msg);
-}
-
-static void read_write_done(struct ib_cq *cq, struct ib_wc *wc,
-			    enum dma_data_direction dir)
-{
-	struct smbdirect_rw_io *msg =
-		container_of(wc->wr_cqe, struct smbdirect_rw_io, cqe);
-	struct smbdirect_socket *sc = msg->socket;
-
-	if (wc->status != IB_WC_SUCCESS) {
-		msg->error = -EIO;
-		pr_err("read/write error. opcode = %d, status = %s(%d)\n",
-		       wc->opcode, ib_wc_status_msg(wc->status), wc->status);
-		if (wc->status != IB_WC_WR_FLUSH_ERR)
-			smbdirect_connection_schedule_disconnect(sc, msg->error);
-	}
-
-	complete(msg->completion);
-}
-
-static void read_done(struct ib_cq *cq, struct ib_wc *wc)
-{
-	read_write_done(cq, wc, DMA_FROM_DEVICE);
-}
-
-static void write_done(struct ib_cq *cq, struct ib_wc *wc)
-{
-	read_write_done(cq, wc, DMA_TO_DEVICE);
-}
-
-static int smb_direct_rdma_xmit(struct smb_direct_transport *t,
-				void *buf, int buf_len,
-				struct smbdirect_buffer_descriptor_v1 *desc,
-				unsigned int desc_len,
-				bool is_read)
-{
-	struct smbdirect_socket *sc = &t->socket;
-	struct smbdirect_socket_parameters *sp = &sc->parameters;
-	struct smbdirect_rw_io *msg, *next_msg;
-	int i, ret;
-	DECLARE_COMPLETION_ONSTACK(completion);
-	struct ib_send_wr *first_wr;
-	LIST_HEAD(msg_list);
-	char *desc_buf;
-	int credits_needed;
-	unsigned int desc_buf_len, desc_num = 0;
-
-	if (sc->status != SMBDIRECT_SOCKET_CONNECTED)
-		return -ENOTCONN;
-
-	if (buf_len > sp->max_read_write_size)
-		return -EINVAL;
-
-	/* calculate needed credits */
-	credits_needed = 0;
-	desc_buf = buf;
-	for (i = 0; i < desc_len / sizeof(*desc); i++) {
-		if (!buf_len)
-			break;
-
-		desc_buf_len = le32_to_cpu(desc[i].length);
-		if (!desc_buf_len)
-			return -EINVAL;
-
-		if (desc_buf_len > buf_len) {
-			desc_buf_len = buf_len;
-			desc[i].length = cpu_to_le32(desc_buf_len);
-			buf_len = 0;
-		}
-
-		credits_needed += calc_rw_credits(sc, desc_buf, desc_buf_len);
-		desc_buf += desc_buf_len;
-		buf_len -= desc_buf_len;
-		desc_num++;
-	}
-
-	ksmbd_debug(RDMA, "RDMA %s, len %#x, needed credits %#x\n",
-		    str_read_write(is_read), buf_len, credits_needed);
-
-	ret = wait_for_rw_credits(sc, credits_needed);
-	if (ret < 0)
-		return ret;
-
-	/* build rdma_rw_ctx for each descriptor */
-	desc_buf = buf;
-	for (i = 0; i < desc_num; i++) {
-		msg = kzalloc(struct_size(msg, sg_list, SG_CHUNK_SIZE),
-			      KSMBD_DEFAULT_GFP);
-		if (!msg) {
-			ret = -ENOMEM;
-			goto out;
-		}
-
-		desc_buf_len = le32_to_cpu(desc[i].length);
-
-		msg->socket = sc;
-		msg->cqe.done = is_read ? read_done : write_done;
-		msg->completion = &completion;
-
-		msg->sgt.sgl = &msg->sg_list[0];
-		ret = sg_alloc_table_chained(&msg->sgt,
-					     smbdirect_get_buf_page_count(desc_buf, desc_buf_len),
-					     msg->sg_list, SG_CHUNK_SIZE);
-		if (ret) {
-			ret = -ENOMEM;
-			goto free_msg;
-		}
-
-		ret = get_sg_list(desc_buf, desc_buf_len,
-				  msg->sgt.sgl, msg->sgt.orig_nents);
-		if (ret < 0)
-			goto free_table;
-
-		ret = rdma_rw_ctx_init(&msg->rdma_ctx, sc->ib.qp, sc->ib.qp->port,
-				       msg->sgt.sgl,
-				       smbdirect_get_buf_page_count(desc_buf, desc_buf_len),
-				       0,
-				       le64_to_cpu(desc[i].offset),
-				       le32_to_cpu(desc[i].token),
-				       is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
-		if (ret < 0) {
-			pr_err("failed to init rdma_rw_ctx: %d\n", ret);
-			goto free_table;
-		}
-
-		list_add_tail(&msg->list, &msg_list);
-		desc_buf += desc_buf_len;
-	}
-
-	/* concatenate work requests of rdma_rw_ctxs */
-	first_wr = NULL;
-	list_for_each_entry_reverse(msg, &msg_list, list) {
-		first_wr = rdma_rw_ctx_wrs(&msg->rdma_ctx, sc->ib.qp, sc->ib.qp->port,
-					   &msg->cqe, first_wr);
-	}
-
-	ret = ib_post_send(sc->ib.qp, first_wr, NULL);
-	if (ret) {
-		pr_err("failed to post send wr for RDMA R/W: %d\n", ret);
-		goto out;
-	}
-
-	msg = list_last_entry(&msg_list, struct smbdirect_rw_io, list);
-	wait_for_completion(&completion);
-	ret = msg->error;
-out:
-	list_for_each_entry_safe(msg, next_msg, &msg_list, list) {
-		list_del(&msg->list);
-		smb_direct_free_rdma_rw_msg(t, msg,
-					    is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
-	}
-	atomic_add(credits_needed, &sc->rw_io.credits.count);
-	wake_up(&sc->rw_io.credits.wait_queue);
-	return ret;
-
-free_table:
-	sg_free_table_chained(&msg->sgt, SG_CHUNK_SIZE);
-free_msg:
-	kfree(msg);
-	goto out;
-}
-
 static int smb_direct_rdma_write(struct ksmbd_transport *t,
 				 void *buf, unsigned int buflen,
 				 struct smbdirect_buffer_descriptor_v1 *desc,
 				 unsigned int desc_len)
 {
-	return smb_direct_rdma_xmit(SMBD_TRANS(t), buf, buflen,
-				    desc, desc_len, false);
+	struct smb_direct_transport *st = SMBD_TRANS(t);
+	struct smbdirect_socket *sc = &st->socket;
+
+	return smbdirect_connection_rdma_xmit(sc, buf, buflen,
+					      desc, desc_len, false);
 }
 
 static int smb_direct_rdma_read(struct ksmbd_transport *t,
@@ -1340,8 +1125,11 @@ static int smb_direct_rdma_read(struct ksmbd_transport *t,
 				struct smbdirect_buffer_descriptor_v1 *desc,
 				unsigned int desc_len)
 {
-	return smb_direct_rdma_xmit(SMBD_TRANS(t), buf, buflen,
-				    desc, desc_len, true);
+	struct smb_direct_transport *st = SMBD_TRANS(t);
+	struct smbdirect_socket *sc = &st->socket;
+
+	return smbdirect_connection_rdma_xmit(sc, buf, buflen,
+					      desc, desc_len, true);
 }
 
 static void smb_direct_disconnect(struct ksmbd_transport *t)
Signature · 1 line
2.43.0
author reviewer
#118 lore raw

PATCH v2 114/127 smb: server: make use of smbdirect_connection_destroy_sync()

In reply to Stefan Metzmacher

This is basically the same logic as before, but we now use common code, which will also be used by the server soon.

---
 fs/smb/server/transport_rdma.c | 49 +---------------------------------
 1 file changed, 1 insertion(+), 48 deletions(-)
fs/smb/server/transport_rdma.c +1 -48
index 2845f58301fb..98ecc9f7f482 100644
@@ -289,56 +289,9 @@ static void smb_direct_free_transport(struct ksmbd_transport *kt)
 static void free_transport(struct smb_direct_transport *t)
 {
 	struct smbdirect_socket *sc = &t->socket;
-	struct smbdirect_recv_io *recvmsg;
-
-	disable_work_sync(&sc->disconnect_work);
-	if (sc->status < SMBDIRECT_SOCKET_DISCONNECTING)
-		smbdirect_connection_disconnect_work(&sc->disconnect_work);
-	if (sc->status < SMBDIRECT_SOCKET_DISCONNECTED)
-		wait_event(sc->status_wait, sc->status == SMBDIRECT_SOCKET_DISCONNECTED);
-
-	/*
-	 * Wake up all waiters in all wait queues
-	 * in order to notice the broken connection.
-	 *
-	 * Most likely this was already called via
-	 * smbdirect_connection_disconnect_work(), but call it again...
-	 */
-	smbdirect_connection_wake_up_all(sc);
-
-	disable_work_sync(&sc->recv_io.posted.refill_work);
-	disable_delayed_work_sync(&sc->idle.timer_work);
-	disable_work_sync(&sc->idle.immediate_work);
-
-	if (sc->rdma.cm_id)
-		rdma_lock_handler(sc->rdma.cm_id);
 
-	if (sc->ib.qp)
-		ib_drain_qp(sc->ib.qp);
-
-	ksmbd_debug(RDMA, "drain the reassembly queue\n");
-	do {
-		unsigned long flags;
-
-		spin_lock_irqsave(&sc->recv_io.reassembly.lock, flags);
-		recvmsg = smbdirect_connection_reassembly_first_recv_io(sc);
-		if (recvmsg) {
-			list_del(&recvmsg->list);
-			spin_unlock_irqrestore(&sc->recv_io.reassembly.lock, flags);
-			smbdirect_connection_put_recv_io(recvmsg);
-		} else {
-			spin_unlock_irqrestore(&sc->recv_io.reassembly.lock, flags);
-		}
-	} while (recvmsg);
-	sc->recv_io.reassembly.data_length = 0;
-
-	smbdirect_connection_destroy_qp(sc);
-	if (sc->rdma.cm_id) {
-		rdma_unlock_handler(sc->rdma.cm_id);
-		rdma_destroy_id(sc->rdma.cm_id);
-	}
+	smbdirect_connection_destroy_sync(sc);
 
-	smbdirect_connection_destroy_mem_pools(sc);
 	ksmbd_conn_free(KSMBD_TRANS(t)->conn);
 }
 
Signature · 1 line
2.43.0
author reviewer
#119 lore raw

PATCH v2 115/127 smb: server: make use of smbdirect_connection_recvmsg()

In reply to Stefan Metzmacher

This is basically the same logic, it just operates on iov_iter_kvec() instead of a raw buffer pointer. This allows us to use common code between client and server.

We keep returning -EINTR instead of -ERESTARTSYS if wait_event_interruptible() fails. I don't if this is required, but changing it is a task for another patch.

---
 fs/smb/server/transport_rdma.c | 128 +++------------------------------
 1 file changed, 11 insertions(+), 117 deletions(-)
fs/smb/server/transport_rdma.c +11 -117
index 98ecc9f7f482..d64896a2a1d1 100644
@@ -201,12 +201,6 @@ static int smb_direct_post_send_data(struct smbdirect_socket *sc,
 				     struct kvec *iov, int niov,
 				     int remaining_data_length);
 
-static inline void
-*smbdirect_recv_io_payload(struct smbdirect_recv_io *recvmsg)
-{
-	return (void *)recvmsg->packet;
-}
-
 static void smb_direct_send_immediate_work(struct work_struct *work)
 {
 	struct smbdirect_socket *sc =
@@ -481,121 +475,21 @@ static void recv_done(struct ib_cq *cq, struct ib_wc *wc)
 static int smb_direct_read(struct ksmbd_transport *t, char *buf,
 			   unsigned int size, int unused)
 {
-	struct smbdirect_recv_io *recvmsg;
-	struct smbdirect_data_transfer *data_transfer;
-	int to_copy, to_read, data_read, offset;
-	u32 data_length, remaining_data_length, data_offset;
-	int rc;
 	struct smb_direct_transport *st = SMBD_TRANS(t);
 	struct smbdirect_socket *sc = &st->socket;
+	struct msghdr msg = { .msg_flags = 0, };
+	struct kvec iov = {
+		.iov_base = buf,
+		.iov_len = size,
+	};
+	int ret;
 
-again:
-	if (sc->status != SMBDIRECT_SOCKET_CONNECTED) {
-		pr_err("disconnected\n");
-		return -ENOTCONN;
-	}
-
-	/*
-	 * No need to hold the reassembly queue lock all the time as we are
-	 * the only one reading from the front of the queue. The transport
-	 * may add more entries to the back of the queue at the same time
-	 */
-	if (sc->recv_io.reassembly.data_length >= size) {
-		int queue_length;
-		int queue_removed = 0;
-		unsigned long flags;
-
-		/*
-		 * Need to make sure reassembly_data_length is read before
-		 * reading reassembly_queue_length and calling
-		 * smbdirect_connection_reassembly_first_recv_io. This call is lock free
-		 * as we never read at the end of the queue which are being
-		 * updated in SOFTIRQ as more data is received
-		 */
-		virt_rmb();
-		queue_length = sc->recv_io.reassembly.queue_length;
-		data_read = 0;
-		to_read = size;
-		offset = sc->recv_io.reassembly.first_entry_offset;
-		while (data_read < size) {
-			recvmsg = smbdirect_connection_reassembly_first_recv_io(sc);
-			data_transfer = smbdirect_recv_io_payload(recvmsg);
-			data_length = le32_to_cpu(data_transfer->data_length);
-			remaining_data_length =
-				le32_to_cpu(data_transfer->remaining_data_length);
-			data_offset = le32_to_cpu(data_transfer->data_offset);
-
-			/*
-			 * The upper layer expects RFC1002 length at the
-			 * beginning of the payload. Return it to indicate
-			 * the total length of the packet. This minimize the
-			 * change to upper layer packet processing logic. This
-			 * will be eventually remove when an intermediate
-			 * transport layer is added
-			 */
-			if (recvmsg->first_segment && size == 4) {
-				unsigned int rfc1002_len =
-					data_length + remaining_data_length;
-				*((__be32 *)buf) = cpu_to_be32(rfc1002_len);
-				data_read = 4;
-				recvmsg->first_segment = false;
-				ksmbd_debug(RDMA,
-					    "returning rfc1002 length %d\n",
-					    rfc1002_len);
-				goto read_rfc1002_done;
-			}
-
-			to_copy = min_t(int, data_length - offset, to_read);
-			memcpy(buf + data_read, (char *)data_transfer + data_offset + offset,
-			       to_copy);
-
-			/* move on to the next buffer? */
-			if (to_copy == data_length - offset) {
-				queue_length--;
-				/*
-				 * No need to lock if we are not at the
-				 * end of the queue
-				 */
-				if (queue_length) {
-					list_del(&recvmsg->list);
-				} else {
-					spin_lock_irqsave(&sc->recv_io.reassembly.lock, flags);
-					list_del(&recvmsg->list);
-					spin_unlock_irqrestore(&sc->recv_io.reassembly.lock, flags);
-				}
-				queue_removed++;
-				smbdirect_connection_put_recv_io(recvmsg);
-				offset = 0;
-			} else {
-				offset += to_copy;
-			}
-
-			to_read -= to_copy;
-			data_read += to_copy;
-		}
-
-		spin_lock_irqsave(&sc->recv_io.reassembly.lock, flags);
-		sc->recv_io.reassembly.data_length -= data_read;
-		sc->recv_io.reassembly.queue_length -= queue_removed;
-		spin_unlock_irqrestore(&sc->recv_io.reassembly.lock, flags);
-
-		sc->recv_io.reassembly.first_entry_offset = offset;
-		ksmbd_debug(RDMA,
-			    "returning to thread data_read=%d reassembly_data_length=%d first_entry_offset=%d\n",
-			    data_read, sc->recv_io.reassembly.data_length,
-			    sc->recv_io.reassembly.first_entry_offset);
-read_rfc1002_done:
-		return data_read;
-	}
-
-	ksmbd_debug(RDMA, "wait_event on more data\n");
-	rc = wait_event_interruptible(sc->recv_io.reassembly.wait_queue,
-				      sc->recv_io.reassembly.data_length >= size ||
-				       sc->status != SMBDIRECT_SOCKET_CONNECTED);
-	if (rc)
-		return -EINTR;
+	iov_iter_kvec(&msg.msg_iter, ITER_DEST, &iov, 1, size);
 
-	goto again;
+	ret = smbdirect_connection_recvmsg(sc, &msg, 0);
+	if (ret == -ERESTARTSYS)
+		ret = -EINTR;
+	return ret;
 }
 
 static int manage_credits_prior_sending(struct smbdirect_socket *sc)
Signature · 1 line
2.43.0
author reviewer
#120 lore raw

PATCH v2 116/127 smb: server: make use of smbdirect_connection_grant_recv_credits()

In reply to Stefan Metzmacher

This is already used by the client too and will help to share more common code.

---
 fs/smb/server/transport_rdma.c | 25 ++++---------------------
 1 file changed, 4 insertions(+), 21 deletions(-)
fs/smb/server/transport_rdma.c +4 -21
index d64896a2a1d1..55757c66cd44 100644
@@ -492,25 +492,6 @@ static int smb_direct_read(struct ksmbd_transport *t, char *buf,
 	return ret;
 }
 
-static int manage_credits_prior_sending(struct smbdirect_socket *sc)
-{
-	int new_credits;
-
-	if (atomic_read(&sc->recv_io.credits.count) >= sc->recv_io.credits.target)
-		return 0;
-
-	new_credits = atomic_read(&sc->recv_io.posted.count);
-	if (new_credits == 0)
-		return 0;
-
-	new_credits -= atomic_read(&sc->recv_io.credits.count);
-	if (new_credits <= 0)
-		return 0;
-
-	atomic_add(new_credits, &sc->recv_io.credits.count);
-	return new_credits;
-}
-
 static int manage_keep_alive_before_sending(struct smbdirect_socket *sc)
 {
 	struct smbdirect_socket_parameters *sp = &sc->parameters;
@@ -648,6 +629,7 @@ static int smb_direct_create_header(struct smbdirect_socket *sc,
 	struct smbdirect_socket_parameters *sp = &sc->parameters;
 	struct smbdirect_send_io *sendmsg;
 	struct smbdirect_data_transfer *packet;
+	u16 new_credits = 0;
 	int header_length;
 	int ret;
 
@@ -658,7 +640,8 @@ static int smb_direct_create_header(struct smbdirect_socket *sc,
 	/* Fill in the packet header */
 	packet = (struct smbdirect_data_transfer *)sendmsg->packet;
 	packet->credits_requested = cpu_to_le16(sp->send_credit_target);
-	packet->credits_granted = cpu_to_le16(manage_credits_prior_sending(sc));
+	new_credits = smbdirect_connection_grant_recv_credits(sc);
+	packet->credits_granted = cpu_to_le16(new_credits);
 
 	packet->flags = 0;
 	if (manage_keep_alive_before_sending(sc))
@@ -1064,7 +1047,7 @@ static int smb_direct_send_negotiate_response(struct smbdirect_socket *sc,
 		resp->reserved = 0;
 		resp->credits_requested =
 				cpu_to_le16(sp->send_credit_target);
-		resp->credits_granted = cpu_to_le16(manage_credits_prior_sending(sc));
+		resp->credits_granted = cpu_to_le16(smbdirect_connection_grant_recv_credits(sc));
 		resp->max_readwrite_size = cpu_to_le32(sp->max_read_write_size);
 		resp->preferred_send_size = cpu_to_le32(sp->max_send_size);
 		resp->max_receive_size = cpu_to_le32(sp->max_recv_size);
Signature · 1 line
2.43.0
author reviewer
#121 lore raw

PATCH v2 117/127 smb: server: make use of smbdirect_connection_request_keep_alive()

In reply to Stefan Metzmacher

This will help to share more common code soon.

---
 fs/smb/server/transport_rdma.c | 19 +------------------
 1 file changed, 1 insertion(+), 18 deletions(-)
fs/smb/server/transport_rdma.c +1 -18
index 55757c66cd44..72cd64149785 100644
@@ -492,23 +492,6 @@ static int smb_direct_read(struct ksmbd_transport *t, char *buf,
 	return ret;
 }
 
-static int manage_keep_alive_before_sending(struct smbdirect_socket *sc)
-{
-	struct smbdirect_socket_parameters *sp = &sc->parameters;
-
-	if (sc->idle.keepalive == SMBDIRECT_KEEPALIVE_PENDING) {
-		sc->idle.keepalive = SMBDIRECT_KEEPALIVE_SENT;
-		/*
-		 * Now use the keepalive timeout (instead of keepalive interval)
-		 * in order to wait for a response
-		 */
-		mod_delayed_work(sc->workqueue, &sc->idle.timer_work,
-				 msecs_to_jiffies(sp->keepalive_timeout_msec));
-		return 1;
-	}
-	return 0;
-}
-
 static int smb_direct_post_send(struct smbdirect_socket *sc,
 				struct ib_send_wr *wr)
 {
@@ -644,7 +627,7 @@ static int smb_direct_create_header(struct smbdirect_socket *sc,
 	packet->credits_granted = cpu_to_le16(new_credits);
 
 	packet->flags = 0;
-	if (manage_keep_alive_before_sending(sc))
+	if (smbdirect_connection_request_keep_alive(sc))
 		packet->flags |= cpu_to_le16(SMBDIRECT_FLAG_RESPONSE_REQUESTED);
 
 	packet->reserved = 0;
Signature · 1 line
2.43.0
author reviewer
#122 lore raw

PATCH v2 118/127 smb: server: move iov_iter_kvec() out of smb_direct_post_send_data()

In reply to Stefan Metzmacher

This will allow us to make the code more generic in order to move it to common with the client.

---
 fs/smb/server/transport_rdma.c | 38 ++++++++++++++++++----------------
 1 file changed, 20 insertions(+), 18 deletions(-)
fs/smb/server/transport_rdma.c +20 -18
index 72cd64149785..03aedfa92c88 100644
@@ -198,8 +198,8 @@ unsigned int get_smbd_max_read_write_size(struct ksmbd_transport *kt)
 
 static int smb_direct_post_send_data(struct smbdirect_socket *sc,
 				     struct smbdirect_send_batch *send_ctx,
-				     struct kvec *iov, int niov,
-				     int remaining_data_length);
+				     struct iov_iter *iter,
+				     size_t *remaining_data_length);
 
 static void smb_direct_send_immediate_work(struct work_struct *work)
 {
@@ -209,7 +209,7 @@ static void smb_direct_send_immediate_work(struct work_struct *work)
 	if (sc->status != SMBDIRECT_SOCKET_CONNECTED)
 		return;
 
-	smb_direct_post_send_data(sc, NULL, NULL, 0, 0);
+	smb_direct_post_send_data(sc, NULL, NULL, NULL);
 }
 
 static struct smb_direct_transport *alloc_transport(struct rdma_cm_id *cm_id)
@@ -712,12 +712,13 @@ static int post_sendmsg(struct smbdirect_socket *sc,
 
 static int smb_direct_post_send_data(struct smbdirect_socket *sc,
 				     struct smbdirect_send_batch *send_ctx,
-				     struct kvec *iov, int niov,
-				     int remaining_data_length)
+				     struct iov_iter *iter,
+				     size_t *_remaining_data_length)
 {
-	int i, ret;
 	struct smbdirect_send_io *msg;
-	int data_length;
+	u32 remaining_data_length = 0;
+	u32 data_length = 0;
+	int ret;
 
 	ret = wait_for_send_lcredit(sc, send_ctx);
 	if (ret)
@@ -727,16 +728,20 @@ static int smb_direct_post_send_data(struct smbdirect_socket *sc,
 	if (ret)
 		goto credit_failed;
 
-	data_length = 0;
-	for (i = 0; i < niov; i++)
-		data_length += iov[i].iov_len;
+	if (iter)
+		data_length = iov_iter_count(iter);
+
+	if (_remaining_data_length) {
+		*_remaining_data_length -= data_length;
+		remaining_data_length = *_remaining_data_length;
+	}
 
 	ret = smb_direct_create_header(sc, data_length, remaining_data_length,
 				       &msg);
 	if (ret)
 		goto header_failed;
 
-	if (data_length) {
+	if (iter) {
 		struct smbdirect_map_sges extract = {
 			.num_sge	= msg->num_sge,
 			.max_sge	= ARRAY_SIZE(msg->sge),
@@ -745,11 +750,8 @@ static int smb_direct_post_send_data(struct smbdirect_socket *sc,
 			.local_dma_lkey	= sc->ib.pd->local_dma_lkey,
 			.direction	= DMA_TO_DEVICE,
 		};
-		struct iov_iter iter;
-
-		iov_iter_kvec(&iter, ITER_SOURCE, iov, niov, data_length);
 
-		ret = smbdirect_map_sges_from_iter(&iter, data_length, &extract);
+		ret = smbdirect_map_sges_from_iter(iter, data_length, &extract);
 		if (ret < 0)
 			goto err;
 		if (WARN_ON_ONCE(ret != data_length)) {
@@ -809,6 +811,7 @@ static int smb_direct_writev(struct ksmbd_transport *t,
 		size_t possible_vecs;
 		size_t bytes = 0;
 		size_t nvecs = 0;
+		struct iov_iter iter;
 
 		/*
 		 * For the last message remaining_data_length should be
@@ -889,11 +892,10 @@ static int smb_direct_writev(struct ksmbd_transport *t,
 			}
 		}
 
-		remaining_data_length -= bytes;
+		iov_iter_kvec(&iter, ITER_SOURCE, vecs, nvecs, bytes);
 
 		ret = smb_direct_post_send_data(sc, &send_ctx,
-						vecs, nvecs,
-						remaining_data_length);
+						&iter, &remaining_data_length);
 		if (unlikely(ret)) {
 			error = ret;
 			goto done;
Signature · 1 line
2.43.0
author reviewer
#123 lore raw

PATCH v2 119/127 smb: server: inline smb_direct_create_header() into smb_direct_post_send_data()

In reply to Stefan Metzmacher

The point is that ib_dma_map_single() is done first, but the 'Fill in the packet header' will be done after smbdirect_map_sges_from_iter().

This will simplify further changes in order to
share common code with the client.

---
 fs/smb/server/transport_rdma.c | 128 +++++++++++++++------------------
 1 file changed, 57 insertions(+), 71 deletions(-)
fs/smb/server/transport_rdma.c +57 -71
index 03aedfa92c88..463520e1c07c 100644
@@ -605,73 +605,6 @@ static int wait_for_send_credits(struct smbdirect_socket *sc,
 						     1);
 }
 
-static int smb_direct_create_header(struct smbdirect_socket *sc,
-				    int size, int remaining_data_length,
-				    struct smbdirect_send_io **sendmsg_out)
-{
-	struct smbdirect_socket_parameters *sp = &sc->parameters;
-	struct smbdirect_send_io *sendmsg;
-	struct smbdirect_data_transfer *packet;
-	u16 new_credits = 0;
-	int header_length;
-	int ret;
-
-	sendmsg = smbdirect_connection_alloc_send_io(sc);
-	if (IS_ERR(sendmsg))
-		return PTR_ERR(sendmsg);
-
-	/* Fill in the packet header */
-	packet = (struct smbdirect_data_transfer *)sendmsg->packet;
-	packet->credits_requested = cpu_to_le16(sp->send_credit_target);
-	new_credits = smbdirect_connection_grant_recv_credits(sc);
-	packet->credits_granted = cpu_to_le16(new_credits);
-
-	packet->flags = 0;
-	if (smbdirect_connection_request_keep_alive(sc))
-		packet->flags |= cpu_to_le16(SMBDIRECT_FLAG_RESPONSE_REQUESTED);
-
-	packet->reserved = 0;
-	if (!size)
-		packet->data_offset = 0;
-	else
-		packet->data_offset = cpu_to_le32(24);
-	packet->data_length = cpu_to_le32(size);
-	packet->remaining_data_length = cpu_to_le32(remaining_data_length);
-	packet->padding = 0;
-
-	ksmbd_debug(RDMA,
-		    "credits_requested=%d credits_granted=%d data_offset=%d data_length=%d remaining_data_length=%d\n",
-		    le16_to_cpu(packet->credits_requested),
-		    le16_to_cpu(packet->credits_granted),
-		    le32_to_cpu(packet->data_offset),
-		    le32_to_cpu(packet->data_length),
-		    le32_to_cpu(packet->remaining_data_length));
-
-	/* Map the packet to DMA */
-	header_length = sizeof(struct smbdirect_data_transfer);
-	/* If this is a packet without payload, don't send padding */
-	if (!size)
-		header_length =
-			offsetof(struct smbdirect_data_transfer, padding);
-
-	sendmsg->sge[0].addr = ib_dma_map_single(sc->ib.dev,
-						 (void *)packet,
-						 header_length,
-						 DMA_TO_DEVICE);
-	ret = ib_dma_mapping_error(sc->ib.dev, sendmsg->sge[0].addr);
-	if (ret) {
-		smbdirect_connection_free_send_io(sendmsg);
-		return ret;
-	}
-
-	sendmsg->num_sge = 1;
-	sendmsg->sge[0].length = header_length;
-	sendmsg->sge[0].lkey = sc->ib.pd->local_dma_lkey;
-
-	*sendmsg_out = sendmsg;
-	return 0;
-}
-
 static int post_sendmsg(struct smbdirect_socket *sc,
 			struct smbdirect_send_batch *send_ctx,
 			struct smbdirect_send_io *msg)
@@ -715,11 +648,22 @@ static int smb_direct_post_send_data(struct smbdirect_socket *sc,
 				     struct iov_iter *iter,
 				     size_t *_remaining_data_length)
 {
+	const struct smbdirect_socket_parameters *sp = &sc->parameters;
 	struct smbdirect_send_io *msg;
+	struct smbdirect_data_transfer *packet;
+	size_t header_length;
+	u16 new_credits = 0;
 	u32 remaining_data_length = 0;
 	u32 data_length = 0;
 	int ret;
 
+	if (iter) {
+		header_length = sizeof(struct smbdirect_data_transfer);
+	} else {
+		/* If this is a packet without payload, don't send padding */
+		header_length = offsetof(struct smbdirect_data_transfer, padding);
+	}
+
 	ret = wait_for_send_lcredit(sc, send_ctx);
 	if (ret)
 		goto lcredit_failed;
@@ -736,10 +680,24 @@ static int smb_direct_post_send_data(struct smbdirect_socket *sc,
 		remaining_data_length = *_remaining_data_length;
 	}
 
-	ret = smb_direct_create_header(sc, data_length, remaining_data_length,
-				       &msg);
+	msg = smbdirect_connection_alloc_send_io(sc);
+	if (IS_ERR(msg)) {
+		ret = PTR_ERR(msg);
+		goto alloc_failed;
+	}
+
+	/* Map the packet to DMA */
+	msg->sge[0].addr = ib_dma_map_single(sc->ib.dev,
+					     msg->packet,
+					     header_length,
+					     DMA_TO_DEVICE);
+	ret = ib_dma_mapping_error(sc->ib.dev, msg->sge[0].addr);
 	if (ret)
-		goto header_failed;
+		goto err;
+
+	msg->sge[0].length = header_length;
+	msg->sge[0].lkey = sc->ib.pd->local_dma_lkey;
+	msg->num_sge = 1;
 
 	if (iter) {
 		struct smbdirect_map_sges extract = {
@@ -761,13 +719,41 @@ static int smb_direct_post_send_data(struct smbdirect_socket *sc,
 		msg->num_sge = extract.num_sge;
 	}
 
+	/* Fill in the packet header */
+	packet = (struct smbdirect_data_transfer *)msg->packet;
+	packet->credits_requested = cpu_to_le16(sp->send_credit_target);
+	new_credits = smbdirect_connection_grant_recv_credits(sc);
+	packet->credits_granted = cpu_to_le16(new_credits);
+
+	packet->flags = 0;
+	if (smbdirect_connection_request_keep_alive(sc))
+		packet->flags |= cpu_to_le16(SMBDIRECT_FLAG_RESPONSE_REQUESTED);
+
+	packet->reserved = 0;
+	if (!data_length)
+		packet->data_offset = 0;
+	else
+		packet->data_offset = cpu_to_le32(24);
+	packet->data_length = cpu_to_le32(data_length);
+	packet->remaining_data_length = cpu_to_le32(remaining_data_length);
+	packet->padding = 0;
+
+	ksmbd_debug(RDMA,
+		    "credits_req=%u credits_granted=%u flags=0x%x ofs=%u len=%u remaining=%u\n",
+		    le16_to_cpu(packet->credits_requested),
+		    le16_to_cpu(packet->credits_granted),
+		    le16_to_cpu(packet->flags),
+		    le32_to_cpu(packet->data_offset),
+		    le32_to_cpu(packet->data_length),
+		    le32_to_cpu(packet->remaining_data_length));
+
 	ret = post_sendmsg(sc, send_ctx, msg);
 	if (ret)
 		goto err;
 	return 0;
 err:
 	smbdirect_connection_free_send_io(msg);
-header_failed:
+alloc_failed:
 	atomic_inc(&sc->send_io.credits.count);
 credit_failed:
 	atomic_inc(&sc->send_io.lcredits.count);
Signature · 1 line
2.43.0
author reviewer
#124 lore raw

PATCH v2 120/127 smb: server: let smbdirect_map_sges_from_iter() truncate the message boundary

In reply to Stefan Metzmacher

smbdirect_map_sges_from_iter() already handles the case that only a limited number of sges are available. Its return value is data_length and the remaining bytes in the iter are remaining_data_length.

This is now much easier and will allow us to share more code with the client soon.

---
 fs/smb/server/transport_rdma.c | 151 +++++++--------------------------
 1 file changed, 31 insertions(+), 120 deletions(-)
fs/smb/server/transport_rdma.c +31 -120
index 463520e1c07c..d888b5396cd6 100644
@@ -199,7 +199,7 @@ unsigned int get_smbd_max_read_write_size(struct ksmbd_transport *kt)
 static int smb_direct_post_send_data(struct smbdirect_socket *sc,
 				     struct smbdirect_send_batch *send_ctx,
 				     struct iov_iter *iter,
-				     size_t *remaining_data_length);
+				     u32 remaining_data_length);
 
 static void smb_direct_send_immediate_work(struct work_struct *work)
 {
@@ -209,7 +209,7 @@ static void smb_direct_send_immediate_work(struct work_struct *work)
 	if (sc->status != SMBDIRECT_SOCKET_CONNECTED)
 		return;
 
-	smb_direct_post_send_data(sc, NULL, NULL, NULL);
+	smb_direct_post_send_data(sc, NULL, NULL, 0);
 }
 
 static struct smb_direct_transport *alloc_transport(struct rdma_cm_id *cm_id)
@@ -646,22 +646,26 @@ static int post_sendmsg(struct smbdirect_socket *sc,
 static int smb_direct_post_send_data(struct smbdirect_socket *sc,
 				     struct smbdirect_send_batch *send_ctx,
 				     struct iov_iter *iter,
-				     size_t *_remaining_data_length)
+				     u32 remaining_data_length)
 {
 	const struct smbdirect_socket_parameters *sp = &sc->parameters;
 	struct smbdirect_send_io *msg;
 	struct smbdirect_data_transfer *packet;
 	size_t header_length;
 	u16 new_credits = 0;
-	u32 remaining_data_length = 0;
 	u32 data_length = 0;
 	int ret;
 
 	if (iter) {
 		header_length = sizeof(struct smbdirect_data_transfer);
+		if (WARN_ON_ONCE(remaining_data_length == 0 ||
+				 iov_iter_count(iter) > remaining_data_length))
+			return -EINVAL;
 	} else {
 		/* If this is a packet without payload, don't send padding */
 		header_length = offsetof(struct smbdirect_data_transfer, padding);
+		if (WARN_ON_ONCE(remaining_data_length))
+			return -EINVAL;
 	}
 
 	ret = wait_for_send_lcredit(sc, send_ctx);
@@ -672,14 +676,6 @@ static int smb_direct_post_send_data(struct smbdirect_socket *sc,
 	if (ret)
 		goto credit_failed;
 
-	if (iter)
-		data_length = iov_iter_count(iter);
-
-	if (_remaining_data_length) {
-		*_remaining_data_length -= data_length;
-		remaining_data_length = *_remaining_data_length;
-	}
-
 	msg = smbdirect_connection_alloc_send_io(sc);
 	if (IS_ERR(msg)) {
 		ret = PTR_ERR(msg);
@@ -708,14 +704,14 @@ static int smb_direct_post_send_data(struct smbdirect_socket *sc,
 			.local_dma_lkey	= sc->ib.pd->local_dma_lkey,
 			.direction	= DMA_TO_DEVICE,
 		};
+		size_t payload_len = umin(iov_iter_count(iter),
+					  sp->max_send_size - sizeof(*packet));
 
-		ret = smbdirect_map_sges_from_iter(iter, data_length, &extract);
+		ret = smbdirect_map_sges_from_iter(iter, payload_len, &extract);
 		if (ret < 0)
 			goto err;
-		if (WARN_ON_ONCE(ret != data_length)) {
-			ret = -EIO;
-			goto err;
-		}
+		data_length = ret;
+		remaining_data_length -= data_length;
 		msg->num_sge = extract.num_sge;
 	}
 
@@ -768,13 +764,9 @@ static int smb_direct_writev(struct ksmbd_transport *t,
 	struct smb_direct_transport *st = SMBD_TRANS(t);
 	struct smbdirect_socket *sc = &st->socket;
 	struct smbdirect_socket_parameters *sp = &sc->parameters;
-	size_t remaining_data_length;
-	size_t iov_idx;
-	size_t iov_ofs;
-	size_t max_iov_size = sp->max_send_size -
-			sizeof(struct smbdirect_data_transfer);
 	int ret;
 	struct smbdirect_send_batch send_ctx;
+	struct iov_iter iter;
 	int error = 0;
 
 	if (sc->status != SMBDIRECT_SOCKET_CONNECTED)
@@ -783,112 +775,31 @@ static int smb_direct_writev(struct ksmbd_transport *t,
 	//FIXME: skip RFC1002 header..
 	if (WARN_ON_ONCE(niovs <= 1 || iov[0].iov_len != 4))
 		return -EINVAL;
-	buflen -= 4;
-	iov_idx = 1;
-	iov_ofs = 0;
-
-	remaining_data_length = buflen;
-	ksmbd_debug(RDMA, "Sending smb (RDMA): smb_len=%u\n", buflen);
-
-	smb_direct_send_ctx_init(&send_ctx, need_invalidate, remote_key);
-	while (remaining_data_length) {
-		struct kvec vecs[SMBDIRECT_SEND_IO_MAX_SGE - 1]; /* minus smbdirect hdr */
-		size_t possible_bytes = max_iov_size;
-		size_t possible_vecs;
-		size_t bytes = 0;
-		size_t nvecs = 0;
-		struct iov_iter iter;
-
-		/*
-		 * For the last message remaining_data_length should be
-		 * have been 0 already!
-		 */
-		if (WARN_ON_ONCE(iov_idx >= niovs)) {
-			error = -EINVAL;
-			goto done;
-		}
+	iov_iter_kvec(&iter, ITER_SOURCE, iov, niovs, buflen);
+	iov_iter_advance(&iter, 4);
 
-		/*
-		 * We have 2 factors which limit the arguments we pass
-		 * to smb_direct_post_send_data():
-		 *
-		 * 1. The number of supported sges for the send,
-		 *    while one is reserved for the smbdirect header.
-		 *    And we currently need one SGE per page.
-		 * 2. The number of negotiated payload bytes per send.
-		 */
-		possible_vecs = min_t(size_t, ARRAY_SIZE(vecs), niovs - iov_idx);
-
-		while (iov_idx < niovs && possible_vecs && possible_bytes) {
-			struct kvec *v = &vecs[nvecs];
-			int page_count;
-
-			v->iov_base = ((u8 *)iov[iov_idx].iov_base) + iov_ofs;
-			v->iov_len = min_t(size_t,
-					   iov[iov_idx].iov_len - iov_ofs,
-					   possible_bytes);
-			page_count = smbdirect_get_buf_page_count(v->iov_base, v->iov_len);
-			if (page_count > possible_vecs) {
-				/*
-				 * If the number of pages in the buffer
-				 * is to much (because we currently require
-				 * one SGE per page), we need to limit the
-				 * length.
-				 *
-				 * We know possible_vecs is at least 1,
-				 * so we always keep the first page.
-				 *
-				 * We need to calculate the number extra
-				 * pages (epages) we can also keep.
-				 *
-				 * We calculate the number of bytes in the
-				 * first page (fplen), this should never be
-				 * larger than v->iov_len because page_count is
-				 * at least 2, but adding a limitation feels
-				 * better.
-				 *
-				 * Then we calculate the number of bytes (elen)
-				 * we can keep for the extra pages.
-				 */
-				size_t epages = possible_vecs - 1;
-				size_t fpofs = offset_in_page(v->iov_base);
-				size_t fplen = min_t(size_t, PAGE_SIZE - fpofs, v->iov_len);
-				size_t elen = min_t(size_t, v->iov_len - fplen, epages*PAGE_SIZE);
-
-				v->iov_len = fplen + elen;
-				page_count = smbdirect_get_buf_page_count(v->iov_base, v->iov_len);
-				if (WARN_ON_ONCE(page_count > possible_vecs)) {
-					/*
-					 * Something went wrong in the above
-					 * logic...
-					 */
-					error = -EINVAL;
-					goto done;
-				}
-			}
-			possible_vecs -= page_count;
-			nvecs += 1;
-			possible_bytes -= v->iov_len;
-			bytes += v->iov_len;
-
-			iov_ofs += v->iov_len;
-			if (iov_ofs >= iov[iov_idx].iov_len) {
-				iov_idx += 1;
-				iov_ofs = 0;
-			}
-		}
+	/*
+	 * The size must fit into the negotiated
+	 * fragmented send size.
+	 */
+	if (iov_iter_count(&iter) > sp->max_fragmented_send_size)
+		return -EMSGSIZE;
 
-		iov_iter_kvec(&iter, ITER_SOURCE, vecs, nvecs, bytes);
+	ksmbd_debug(RDMA, "Sending smb (RDMA): smb_len=%zu\n",
+		    iov_iter_count(&iter));
 
-		ret = smb_direct_post_send_data(sc, &send_ctx,
-						&iter, &remaining_data_length);
+	smb_direct_send_ctx_init(&send_ctx, need_invalidate, remote_key);
+	while (iov_iter_count(&iter)) {
+		ret = smb_direct_post_send_data(sc,
+						&send_ctx,
+						&iter,
+						iov_iter_count(&iter));
 		if (unlikely(ret)) {
 			error = ret;
-			goto done;
+			break;
 		}
 	}
 
-done:
 	ret = smb_direct_flush_send_list(sc, &send_ctx, true);
 	if (unlikely(!ret && error))
 		ret = error;
Signature · 1 line
2.43.0
author reviewer
#125 lore raw

PATCH v2 121/127 smb: server: split out smb_direct_send_iter() out of smb_direct_writev()

In reply to Stefan Metzmacher

This will help to move to common code in future.

---
 fs/smb/server/transport_rdma.c | 47 +++++++++++++++++++++++-----------
 1 file changed, 32 insertions(+), 15 deletions(-)
fs/smb/server/transport_rdma.c +32 -15
index d888b5396cd6..8a5183426bbb 100644
@@ -757,43 +757,47 @@ static int smb_direct_post_send_data(struct smbdirect_socket *sc,
 	return ret;
 }
 
-static int smb_direct_writev(struct ksmbd_transport *t,
-			     struct kvec *iov, int niovs, int buflen,
-			     bool need_invalidate, unsigned int remote_key)
+static int smb_direct_send_iter(struct smbdirect_socket *sc,
+				struct iov_iter *iter,
+				bool need_invalidate,
+				unsigned int remote_key)
 {
-	struct smb_direct_transport *st = SMBD_TRANS(t);
-	struct smbdirect_socket *sc = &st->socket;
 	struct smbdirect_socket_parameters *sp = &sc->parameters;
 	int ret;
 	struct smbdirect_send_batch send_ctx;
-	struct iov_iter iter;
 	int error = 0;
+	__be32 hdr;
 
 	if (sc->status != SMBDIRECT_SOCKET_CONNECTED)
 		return -ENOTCONN;
 
-	//FIXME: skip RFC1002 header..
-	if (WARN_ON_ONCE(niovs <= 1 || iov[0].iov_len != 4))
+	/*
+	 * For now we expect the iter to have the full
+	 * message, including a 4 byte length header.
+	 */
+	if (iov_iter_count(iter) <= 4)
+		return -EINVAL;
+	if (!copy_from_iter_full(&hdr, sizeof(hdr), iter))
+		return -EFAULT;
+	if (iov_iter_count(iter) != be32_to_cpu(hdr))
 		return -EINVAL;
-	iov_iter_kvec(&iter, ITER_SOURCE, iov, niovs, buflen);
-	iov_iter_advance(&iter, 4);
 
 	/*
 	 * The size must fit into the negotiated
 	 * fragmented send size.
 	 */
-	if (iov_iter_count(&iter) > sp->max_fragmented_send_size)
+	if (iov_iter_count(iter) > sp->max_fragmented_send_size)
 		return -EMSGSIZE;
 
 	ksmbd_debug(RDMA, "Sending smb (RDMA): smb_len=%zu\n",
-		    iov_iter_count(&iter));
+		    iov_iter_count(iter));
 
 	smb_direct_send_ctx_init(&send_ctx, need_invalidate, remote_key);
-	while (iov_iter_count(&iter)) {
+	while (iov_iter_count(iter)) {
 		ret = smb_direct_post_send_data(sc,
 						&send_ctx,
-						&iter,
-						iov_iter_count(&iter));
+						iter,
+						iov_iter_count(iter));
 		if (unlikely(ret)) {
 			error = ret;
 			break;
@@ -820,6 +824,19 @@ static int smb_direct_writev(struct ksmbd_transport *t,
 	return ret;
 }
 
+static int smb_direct_writev(struct ksmbd_transport *t,
+			     struct kvec *iov, int niovs, int buflen,
+			     bool need_invalidate, unsigned int remote_key)
+{
+	struct smb_direct_transport *st = SMBD_TRANS(t);
+	struct smbdirect_socket *sc = &st->socket;
+	struct iov_iter iter;
+
+	iov_iter_kvec(&iter, ITER_SOURCE, iov, niovs, buflen);
+
+	return smb_direct_send_iter(sc, &iter, need_invalidate, remote_key);
+}
+
 static int smb_direct_rdma_write(struct ksmbd_transport *t,
 				 void *buf, unsigned int buflen,
 				 struct smbdirect_buffer_descriptor_v1 *desc,
Signature · 1 line
2.43.0
author reviewer
#126 lore raw

PATCH v2 122/127 smb: server: let smb_direct_post_send_data() return data_length

In reply to Stefan Metzmacher

This make it easier moving to common code shared with the client.

---
 fs/smb/server/transport_rdma.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/smb/server/transport_rdma.c +2 -2
index 8a5183426bbb..070c386dd2ea 100644
@@ -746,7 +746,7 @@ static int smb_direct_post_send_data(struct smbdirect_socket *sc,
 	ret = post_sendmsg(sc, send_ctx, msg);
 	if (ret)
 		goto err;
-	return 0;
+	return data_length;
 err:
 	smbdirect_connection_free_send_io(msg);
 alloc_failed:
@@ -798,7 +798,7 @@ static int smb_direct_send_iter(struct smbdirect_socket *sc,
 						&send_ctx,
 						iter,
 						iov_iter_count(iter));
-		if (unlikely(ret)) {
+		if (unlikely(ret < 0)) {
 			error = ret;
 			break;
 		}
Signature · 1 line
2.43.0
author reviewer
#127 lore raw

PATCH v2 123/127 smb: server: make use of smbdirect_connection_send_iter() and related functions

In reply to Stefan Metzmacher

This makes use of common code for sending messages, this will allow to make more use of common code in the next commits.

---
 fs/smb/server/transport_rdma.c | 355 +--------------------------------
 1 file changed, 4 insertions(+), 351 deletions(-)
fs/smb/server/transport_rdma.c +4 -351
index 070c386dd2ea..e3a410d773f6 100644
@@ -196,22 +196,6 @@ unsigned int get_smbd_max_read_write_size(struct ksmbd_transport *kt)
 	return sp->max_read_write_size;
 }
 
-static int smb_direct_post_send_data(struct smbdirect_socket *sc,
-				     struct smbdirect_send_batch *send_ctx,
-				     struct iov_iter *iter,
-				     u32 remaining_data_length);
-
-static void smb_direct_send_immediate_work(struct work_struct *work)
-{
-	struct smbdirect_socket *sc =
-		container_of(work, struct smbdirect_socket, idle.immediate_work);
-
-	if (sc->status != SMBDIRECT_SOCKET_CONNECTED)
-		return;
-
-	smb_direct_post_send_data(sc, NULL, NULL, 0);
-}
-
 static struct smb_direct_transport *alloc_transport(struct rdma_cm_id *cm_id)
 {
 	struct smb_direct_transport *t;
@@ -492,338 +476,6 @@ static int smb_direct_read(struct ksmbd_transport *t, char *buf,
 	return ret;
 }
 
-static int smb_direct_post_send(struct smbdirect_socket *sc,
-				struct ib_send_wr *wr)
-{
-	int ret;
-
-	atomic_inc(&sc->send_io.pending.count);
-	ret = ib_post_send(sc->ib.qp, wr, NULL);
-	if (ret) {
-		pr_err("failed to post send: %d\n", ret);
-		smbdirect_connection_schedule_disconnect(sc, ret);
-	}
-	return ret;
-}
-
-static void smb_direct_send_ctx_init(struct smbdirect_send_batch *send_ctx,
-				     bool need_invalidate_rkey,
-				     unsigned int remote_key)
-{
-	INIT_LIST_HEAD(&send_ctx->msg_list);
-	send_ctx->wr_cnt = 0;
-	send_ctx->need_invalidate_rkey = need_invalidate_rkey;
-	send_ctx->remote_key = remote_key;
-}
-
-static int smb_direct_flush_send_list(struct smbdirect_socket *sc,
-				      struct smbdirect_send_batch *send_ctx,
-				      bool is_last)
-{
-	struct smbdirect_send_io *first, *last;
-	int ret;
-
-	if (list_empty(&send_ctx->msg_list))
-		return 0;
-
-	first = list_first_entry(&send_ctx->msg_list,
-				 struct smbdirect_send_io,
-				 sibling_list);
-	last = list_last_entry(&send_ctx->msg_list,
-			       struct smbdirect_send_io,
-			       sibling_list);
-
-	if (send_ctx->need_invalidate_rkey) {
-		first->wr.opcode = IB_WR_SEND_WITH_INV;
-		first->wr.ex.invalidate_rkey = send_ctx->remote_key;
-		send_ctx->need_invalidate_rkey = false;
-		send_ctx->remote_key = 0;
-	}
-
-	last->wr.send_flags = IB_SEND_SIGNALED;
-	last->wr.wr_cqe = &last->cqe;
-
-	/*
-	 * Remove last from send_ctx->msg_list
-	 * and splice the rest of send_ctx->msg_list
-	 * to last->sibling_list.
-	 *
-	 * send_ctx->msg_list is a valid empty list
-	 * at the end.
-	 */
-	list_del_init(&last->sibling_list);
-	list_splice_tail_init(&send_ctx->msg_list, &last->sibling_list);
-	send_ctx->wr_cnt = 0;
-
-	ret = smb_direct_post_send(sc, &first->wr);
-	if (ret) {
-		struct smbdirect_send_io *sibling, *next;
-
-		list_for_each_entry_safe(sibling, next, &last->sibling_list, sibling_list) {
-			list_del_init(&sibling->sibling_list);
-			smbdirect_connection_free_send_io(sibling);
-		}
-		smbdirect_connection_free_send_io(last);
-	}
-
-	return ret;
-}
-
-
-static int wait_for_send_lcredit(struct smbdirect_socket *sc,
-				 struct smbdirect_send_batch *send_ctx)
-{
-	if (send_ctx && (atomic_read(&sc->send_io.lcredits.count) <= 1)) {
-		int ret;
-
-		ret = smb_direct_flush_send_list(sc, send_ctx, false);
-		if (ret)
-			return ret;
-	}
-
-	return smbdirect_connection_wait_for_credits(sc,
-						     &sc->send_io.lcredits.wait_queue,
-						     &sc->send_io.lcredits.count,
-						     1);
-}
-
-static int wait_for_send_credits(struct smbdirect_socket *sc,
-				 struct smbdirect_send_batch *send_ctx)
-{
-	int ret;
-
-	if (send_ctx &&
-	    (send_ctx->wr_cnt >= 16 || atomic_read(&sc->send_io.credits.count) <= 1)) {
-		ret = smb_direct_flush_send_list(sc, send_ctx, false);
-		if (ret)
-			return ret;
-	}
-
-	return smbdirect_connection_wait_for_credits(sc,
-						     &sc->send_io.credits.wait_queue,
-						     &sc->send_io.credits.count,
-						     1);
-}
-
-static int post_sendmsg(struct smbdirect_socket *sc,
-			struct smbdirect_send_batch *send_ctx,
-			struct smbdirect_send_io *msg)
-{
-	int i;
-
-	for (i = 0; i < msg->num_sge; i++)
-		ib_dma_sync_single_for_device(sc->ib.dev,
-					      msg->sge[i].addr, msg->sge[i].length,
-					      DMA_TO_DEVICE);
-
-	msg->cqe.done = smbdirect_connection_send_io_done;
-	msg->wr.opcode = IB_WR_SEND;
-	msg->wr.sg_list = &msg->sge[0];
-	msg->wr.num_sge = msg->num_sge;
-	msg->wr.next = NULL;
-
-	if (send_ctx) {
-		msg->wr.wr_cqe = NULL;
-		msg->wr.send_flags = 0;
-		if (!list_empty(&send_ctx->msg_list)) {
-			struct smbdirect_send_io *last;
-
-			last = list_last_entry(&send_ctx->msg_list,
-					       struct smbdirect_send_io,
-					       sibling_list);
-			last->wr.next = &msg->wr;
-		}
-		list_add_tail(&msg->sibling_list, &send_ctx->msg_list);
-		send_ctx->wr_cnt++;
-		return 0;
-	}
-
-	msg->wr.wr_cqe = &msg->cqe;
-	msg->wr.send_flags = IB_SEND_SIGNALED;
-	return smb_direct_post_send(sc, &msg->wr);
-}
-
-static int smb_direct_post_send_data(struct smbdirect_socket *sc,
-				     struct smbdirect_send_batch *send_ctx,
-				     struct iov_iter *iter,
-				     u32 remaining_data_length)
-{
-	const struct smbdirect_socket_parameters *sp = &sc->parameters;
-	struct smbdirect_send_io *msg;
-	struct smbdirect_data_transfer *packet;
-	size_t header_length;
-	u16 new_credits = 0;
-	u32 data_length = 0;
-	int ret;
-
-	if (iter) {
-		header_length = sizeof(struct smbdirect_data_transfer);
-		if (WARN_ON_ONCE(remaining_data_length == 0 ||
-				 iov_iter_count(iter) > remaining_data_length))
-			return -EINVAL;
-	} else {
-		/* If this is a packet without payload, don't send padding */
-		header_length = offsetof(struct smbdirect_data_transfer, padding);
-		if (WARN_ON_ONCE(remaining_data_length))
-			return -EINVAL;
-	}
-
-	ret = wait_for_send_lcredit(sc, send_ctx);
-	if (ret)
-		goto lcredit_failed;
-
-	ret = wait_for_send_credits(sc, send_ctx);
-	if (ret)
-		goto credit_failed;
-
-	msg = smbdirect_connection_alloc_send_io(sc);
-	if (IS_ERR(msg)) {
-		ret = PTR_ERR(msg);
-		goto alloc_failed;
-	}
-
-	/* Map the packet to DMA */
-	msg->sge[0].addr = ib_dma_map_single(sc->ib.dev,
-					     msg->packet,
-					     header_length,
-					     DMA_TO_DEVICE);
-	ret = ib_dma_mapping_error(sc->ib.dev, msg->sge[0].addr);
-	if (ret)
-		goto err;
-
-	msg->sge[0].length = header_length;
-	msg->sge[0].lkey = sc->ib.pd->local_dma_lkey;
-	msg->num_sge = 1;
-
-	if (iter) {
-		struct smbdirect_map_sges extract = {
-			.num_sge	= msg->num_sge,
-			.max_sge	= ARRAY_SIZE(msg->sge),
-			.sge		= msg->sge,
-			.device		= sc->ib.dev,
-			.local_dma_lkey	= sc->ib.pd->local_dma_lkey,
-			.direction	= DMA_TO_DEVICE,
-		};
-		size_t payload_len = umin(iov_iter_count(iter),
-					  sp->max_send_size - sizeof(*packet));
-
-		ret = smbdirect_map_sges_from_iter(iter, payload_len, &extract);
-		if (ret < 0)
-			goto err;
-		data_length = ret;
-		remaining_data_length -= data_length;
-		msg->num_sge = extract.num_sge;
-	}
-
-	/* Fill in the packet header */
-	packet = (struct smbdirect_data_transfer *)msg->packet;
-	packet->credits_requested = cpu_to_le16(sp->send_credit_target);
-	new_credits = smbdirect_connection_grant_recv_credits(sc);
-	packet->credits_granted = cpu_to_le16(new_credits);
-
-	packet->flags = 0;
-	if (smbdirect_connection_request_keep_alive(sc))
-		packet->flags |= cpu_to_le16(SMBDIRECT_FLAG_RESPONSE_REQUESTED);
-
-	packet->reserved = 0;
-	if (!data_length)
-		packet->data_offset = 0;
-	else
-		packet->data_offset = cpu_to_le32(24);
-	packet->data_length = cpu_to_le32(data_length);
-	packet->remaining_data_length = cpu_to_le32(remaining_data_length);
-	packet->padding = 0;
-
-	ksmbd_debug(RDMA,
-		    "credits_req=%u credits_granted=%u flags=0x%x ofs=%u len=%u remaining=%u\n",
-		    le16_to_cpu(packet->credits_requested),
-		    le16_to_cpu(packet->credits_granted),
-		    le16_to_cpu(packet->flags),
-		    le32_to_cpu(packet->data_offset),
-		    le32_to_cpu(packet->data_length),
-		    le32_to_cpu(packet->remaining_data_length));
-
-	ret = post_sendmsg(sc, send_ctx, msg);
-	if (ret)
-		goto err;
-	return data_length;
-err:
-	smbdirect_connection_free_send_io(msg);
-alloc_failed:
-	atomic_inc(&sc->send_io.credits.count);
-credit_failed:
-	atomic_inc(&sc->send_io.lcredits.count);
-lcredit_failed:
-	return ret;
-}
-
-static int smb_direct_send_iter(struct smbdirect_socket *sc,
-				struct iov_iter *iter,
-				bool need_invalidate,
-				unsigned int remote_key)
-{
-	struct smbdirect_socket_parameters *sp = &sc->parameters;
-	int ret;
-	struct smbdirect_send_batch send_ctx;
-	int error = 0;
-	__be32 hdr;
-
-	if (sc->status != SMBDIRECT_SOCKET_CONNECTED)
-		return -ENOTCONN;
-
-	/*
-	 * For now we expect the iter to have the full
-	 * message, including a 4 byte length header.
-	 */
-	if (iov_iter_count(iter) <= 4)
-		return -EINVAL;
-	if (!copy_from_iter_full(&hdr, sizeof(hdr), iter))
-		return -EFAULT;
-	if (iov_iter_count(iter) != be32_to_cpu(hdr))
-		return -EINVAL;
-
-	/*
-	 * The size must fit into the negotiated
-	 * fragmented send size.
-	 */
-	if (iov_iter_count(iter) > sp->max_fragmented_send_size)
-		return -EMSGSIZE;
-
-	ksmbd_debug(RDMA, "Sending smb (RDMA): smb_len=%zu\n",
-		    iov_iter_count(iter));
-
-	smb_direct_send_ctx_init(&send_ctx, need_invalidate, remote_key);
-	while (iov_iter_count(iter)) {
-		ret = smb_direct_post_send_data(sc,
-						&send_ctx,
-						iter,
-						iov_iter_count(iter));
-		if (unlikely(ret < 0)) {
-			error = ret;
-			break;
-		}
-	}
-
-	ret = smb_direct_flush_send_list(sc, &send_ctx, true);
-	if (unlikely(!ret && error))
-		ret = error;
-
-	/*
-	 * As an optimization, we don't wait for individual I/O to finish
-	 * before sending the next one.
-	 * Send them all and wait for pending send count to get to 0
-	 * that means all the I/Os have been out and we are good to return
-	 */
-
-	wait_event(sc->send_io.pending.zero_wait_queue,
-		   atomic_read(&sc->send_io.pending.count) == 0 ||
-		   sc->status != SMBDIRECT_SOCKET_CONNECTED);
-	if (sc->status != SMBDIRECT_SOCKET_CONNECTED && ret == 0)
-		ret = -ENOTCONN;
-
-	return ret;
-}
-
 static int smb_direct_writev(struct ksmbd_transport *t,
 			     struct kvec *iov, int niovs, int buflen,
 			     bool need_invalidate, unsigned int remote_key)
@@ -834,7 +486,8 @@ static int smb_direct_writev(struct ksmbd_transport *t,
 
 	iov_iter_kvec(&iter, ITER_SOURCE, iov, niovs, buflen);
 
-	return smb_direct_send_iter(sc, &iter, need_invalidate, remote_key);
+	return smbdirect_connection_send_iter(sc, &iter, 0,
+					      need_invalidate, remote_key);
 }
 
 static int smb_direct_rdma_write(struct ksmbd_transport *t,
@@ -970,7 +623,7 @@ static int smb_direct_send_negotiate_response(struct smbdirect_socket *sc,
 	sendmsg->sge[0].length = sizeof(*resp);
 	sendmsg->sge[0].lkey = sc->ib.pd->local_dma_lkey;
 
-	ret = post_sendmsg(sc, NULL, sendmsg);
+	ret = smbdirect_connection_post_send_io(sc, NULL, sendmsg);
 	if (ret) {
 		smbdirect_connection_free_send_io(sendmsg);
 		return ret;
@@ -1178,7 +831,7 @@ static int smb_direct_prepare(struct ksmbd_transport *t)
 		return ret;
 
 	INIT_WORK(&sc->recv_io.posted.refill_work, smbdirect_connection_recv_io_refill_work);
-	INIT_WORK(&sc->idle.immediate_work, smb_direct_send_immediate_work);
+	INIT_WORK(&sc->idle.immediate_work, smbdirect_connection_send_immediate_work);
 
 	return 0;
 }
Signature · 1 line
2.43.0
author reviewer
#128 lore raw

PATCH v2 124/127 smb: server: make use of smbdirect_{socket_init_accepting,connection_wait_for_connected}()

In reply to Stefan Metzmacher

This means we finally only use common functions in the server.

We still use the embedded struct smbdirect_socket and are able to access internals, but the will be removed in the next commits as well.

---
 fs/smb/server/transport_rdma.c | 575 ++-------------------------------
 1 file changed, 30 insertions(+), 545 deletions(-)
fs/smb/server/transport_rdma.c +30 -545
index e3a410d773f6..d6162b8be58b 100644
@@ -17,6 +17,7 @@
 #include <linux/highmem.h>
 #include <linux/scatterlist.h>
 #include <linux/string_choices.h>
+#include <linux/errname.h>
 #include <rdma/ib_verbs.h>
 #include <rdma/rdma_cm.h>
 #include <rdma/rw.h>
@@ -184,14 +185,14 @@ unsigned int get_smbd_max_read_write_size(struct ksmbd_transport *kt)
 {
 	struct smb_direct_transport *t;
 	struct smbdirect_socket *sc;
-	struct smbdirect_socket_parameters *sp;
+	const struct smbdirect_socket_parameters *sp;
 
 	if (kt->ops != &ksmbd_smb_direct_transport_ops)
 		return 0;
 
 	t = SMBD_TRANS(kt);
 	sc = &t->socket;
-	sp = &sc->parameters;
+	sp = smbdirect_socket_get_current_parameters(sc);
 
 	return sp->max_read_write_size;
 }
@@ -203,6 +204,7 @@ static struct smb_direct_transport *alloc_transport(struct rdma_cm_id *cm_id)
 	struct smbdirect_socket_parameters init_params = {};
 	struct smbdirect_socket_parameters *sp;
 	struct ksmbd_conn *conn;
+	int ret;
 
 	/*
 	 * Create the initial parameters
@@ -224,27 +226,25 @@ static struct smb_direct_transport *alloc_transport(struct rdma_cm_id *cm_id)
 	if (!t)
 		return NULL;
 	sc = &t->socket;
-	smbdirect_socket_prepare_create(sc, sp, smb_direct_wq);
+	ret = smbdirect_socket_init_accepting(cm_id, sc);
+	if (ret)
+		goto socket_init_failed;
 	smbdirect_socket_set_logging(sc, NULL,
 				     smb_direct_logging_needed,
 				     smb_direct_logging_vaprintf);
-	sc->ib.poll_ctx = IB_POLL_WORKQUEUE;
-	sc->send_io.mem.gfp_mask = KSMBD_DEFAULT_GFP;
-	sc->recv_io.mem.gfp_mask = KSMBD_DEFAULT_GFP;
-	sc->rw_io.mem.gfp_mask = KSMBD_DEFAULT_GFP;
-	/*
-	 * from here we operate on the copy.
-	 */
-	sp = &sc->parameters;
-
-	sc->rdma.cm_id = cm_id;
-	cm_id->context = sc;
-
-	sc->ib.dev = sc->rdma.cm_id->device;
+	ret = smbdirect_socket_set_initial_parameters(sc, sp);
+	if (ret)
+		goto set_params_failed;
+	ret = smbdirect_socket_set_kernel_settings(sc, IB_POLL_WORKQUEUE, KSMBD_DEFAULT_GFP);
+	if (ret)
+		goto set_settings_failed;
+	ret = smbdirect_socket_set_custom_workqueue(sc, smb_direct_wq);
+	if (ret)
+		goto set_workqueue_failed;
 
 	conn = ksmbd_conn_alloc();
 	if (!conn)
-		goto err;
+		goto conn_alloc_failed;
 
 	down_write(&conn_list_lock);
 	hash_add(conn_list, &conn->hlist, 0);
@@ -254,7 +254,12 @@ static struct smb_direct_transport *alloc_transport(struct rdma_cm_id *cm_id)
 	KSMBD_TRANS(t)->conn = conn;
 	KSMBD_TRANS(t)->ops = &ksmbd_smb_direct_transport_ops;
 	return t;
-err:
+
+conn_alloc_failed:
+set_workqueue_failed:
+set_settings_failed:
+set_params_failed:
+socket_init_failed:
 	kfree(t);
 	return NULL;
 }
@@ -273,189 +278,6 @@ static void free_transport(struct smb_direct_transport *t)
 	ksmbd_conn_free(KSMBD_TRANS(t)->conn);
 }
 
-static int smb_direct_check_recvmsg(struct smbdirect_recv_io *recvmsg)
-{
-	struct smbdirect_socket *sc = recvmsg->socket;
-
-	switch (sc->recv_io.expected) {
-	case SMBDIRECT_EXPECT_DATA_TRANSFER: {
-		struct smbdirect_data_transfer *req =
-			(struct smbdirect_data_transfer *)recvmsg->packet;
-		struct smb2_hdr *hdr = (struct smb2_hdr *)(recvmsg->packet
-				+ le32_to_cpu(req->data_offset));
-		ksmbd_debug(RDMA,
-			    "CreditGranted: %u, CreditRequested: %u, DataLength: %u, RemainingDataLength: %u, SMB: %x, Command: %u\n",
-			    le16_to_cpu(req->credits_granted),
-			    le16_to_cpu(req->credits_requested),
-			    req->data_length, req->remaining_data_length,
-			    hdr->ProtocolId, hdr->Command);
-		return 0;
-	}
-	case SMBDIRECT_EXPECT_NEGOTIATE_REQ: {
-		struct smbdirect_negotiate_req *req =
-			(struct smbdirect_negotiate_req *)recvmsg->packet;
-		ksmbd_debug(RDMA,
-			    "MinVersion: %u, MaxVersion: %u, CreditRequested: %u, MaxSendSize: %u, MaxRecvSize: %u, MaxFragmentedSize: %u\n",
-			    le16_to_cpu(req->min_version),
-			    le16_to_cpu(req->max_version),
-			    le16_to_cpu(req->credits_requested),
-			    le32_to_cpu(req->preferred_send_size),
-			    le32_to_cpu(req->max_receive_size),
-			    le32_to_cpu(req->max_fragmented_size));
-		if (le16_to_cpu(req->min_version) > 0x0100 ||
-		    le16_to_cpu(req->max_version) < 0x0100)
-			return -EOPNOTSUPP;
-		if (le16_to_cpu(req->credits_requested) <= 0 ||
-		    le32_to_cpu(req->max_receive_size) <= 128 ||
-		    le32_to_cpu(req->max_fragmented_size) <=
-					128 * 1024)
-			return -ECONNABORTED;
-
-		return 0;
-	}
-	case SMBDIRECT_EXPECT_NEGOTIATE_REP:
-		/* client only */
-		break;
-	}
-
-	/* This is an internal error */
-	return -EINVAL;
-}
-
-static void recv_done(struct ib_cq *cq, struct ib_wc *wc)
-{
-	struct smbdirect_recv_io *recvmsg;
-	struct smbdirect_socket *sc;
-	struct smbdirect_socket_parameters *sp;
-
-	recvmsg = container_of(wc->wr_cqe, struct smbdirect_recv_io, cqe);
-	sc = recvmsg->socket;
-	sp = &sc->parameters;
-
-	if (wc->status != IB_WC_SUCCESS || wc->opcode != IB_WC_RECV) {
-		smbdirect_connection_put_recv_io(recvmsg);
-		if (wc->status != IB_WC_WR_FLUSH_ERR) {
-			pr_err("Recv error. status='%s (%d)' opcode=%d\n",
-			       ib_wc_status_msg(wc->status), wc->status,
-			       wc->opcode);
-			smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
-		}
-		return;
-	}
-
-	ksmbd_debug(RDMA, "Recv completed. status='%s (%d)', opcode=%d\n",
-		    ib_wc_status_msg(wc->status), wc->status,
-		    wc->opcode);
-
-	ib_dma_sync_single_for_cpu(wc->qp->device, recvmsg->sge.addr,
-				   recvmsg->sge.length, DMA_FROM_DEVICE);
-
-	/*
-	 * Reset timer to the keepalive interval in
-	 * order to trigger our next keepalive message.
-	 */
-	sc->idle.keepalive = SMBDIRECT_KEEPALIVE_NONE;
-	mod_delayed_work(sc->workqueue, &sc->idle.timer_work,
-			 msecs_to_jiffies(sp->keepalive_interval_msec));
-
-	switch (sc->recv_io.expected) {
-	case SMBDIRECT_EXPECT_NEGOTIATE_REQ:
-		if (wc->byte_len < sizeof(struct smbdirect_negotiate_req)) {
-			smbdirect_connection_put_recv_io(recvmsg);
-			smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
-			return;
-		}
-		sc->recv_io.reassembly.full_packet_received = true;
-		WARN_ON_ONCE(sc->status != SMBDIRECT_SOCKET_NEGOTIATE_NEEDED);
-		sc->status = SMBDIRECT_SOCKET_NEGOTIATE_RUNNING;
-		smbdirect_connection_reassembly_append_recv_io(sc, recvmsg, 0);
-		wake_up(&sc->status_wait);
-		return;
-	case SMBDIRECT_EXPECT_DATA_TRANSFER: {
-		struct smbdirect_data_transfer *data_transfer =
-			(struct smbdirect_data_transfer *)recvmsg->packet;
-		u32 remaining_data_length, data_offset, data_length;
-		u16 old_recv_credit_target;
-
-		if (wc->byte_len <
-		    offsetof(struct smbdirect_data_transfer, padding)) {
-			smbdirect_connection_put_recv_io(recvmsg);
-			smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
-			return;
-		}
-
-		remaining_data_length = le32_to_cpu(data_transfer->remaining_data_length);
-		data_length = le32_to_cpu(data_transfer->data_length);
-		data_offset = le32_to_cpu(data_transfer->data_offset);
-		if (wc->byte_len < data_offset ||
-		    wc->byte_len < (u64)data_offset + data_length) {
-			smbdirect_connection_put_recv_io(recvmsg);
-			smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
-			return;
-		}
-		if (remaining_data_length > sp->max_fragmented_recv_size ||
-		    data_length > sp->max_fragmented_recv_size ||
-		    (u64)remaining_data_length + (u64)data_length >
-		    (u64)sp->max_fragmented_recv_size) {
-			smbdirect_connection_put_recv_io(recvmsg);
-			smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
-			return;
-		}
-
-		if (data_length) {
-			if (sc->recv_io.reassembly.full_packet_received)
-				recvmsg->first_segment = true;
-
-			if (le32_to_cpu(data_transfer->remaining_data_length))
-				sc->recv_io.reassembly.full_packet_received = false;
-			else
-				sc->recv_io.reassembly.full_packet_received = true;
-		}
-
-		atomic_dec(&sc->recv_io.posted.count);
-		atomic_dec(&sc->recv_io.credits.count);
-
-		old_recv_credit_target = sc->recv_io.credits.target;
-		sc->recv_io.credits.target =
-				le16_to_cpu(data_transfer->credits_requested);
-		sc->recv_io.credits.target =
-			min_t(u16, sc->recv_io.credits.target, sp->recv_credit_max);
-		sc->recv_io.credits.target =
-			max_t(u16, sc->recv_io.credits.target, 1);
-		atomic_add(le16_to_cpu(data_transfer->credits_granted),
-			   &sc->send_io.credits.count);
-
-		if (le16_to_cpu(data_transfer->flags) &
-		    SMBDIRECT_FLAG_RESPONSE_REQUESTED)
-			queue_work(sc->workqueue, &sc->idle.immediate_work);
-
-		if (atomic_read(&sc->send_io.credits.count) > 0)
-			wake_up(&sc->send_io.credits.wait_queue);
-
-		if (data_length) {
-			if (sc->recv_io.credits.target > old_recv_credit_target)
-				queue_work(sc->workqueue, &sc->recv_io.posted.refill_work);
-
-			smbdirect_connection_reassembly_append_recv_io(sc, recvmsg, data_length);
-			wake_up(&sc->recv_io.reassembly.wait_queue);
-		} else
-			smbdirect_connection_put_recv_io(recvmsg);
-
-		return;
-	}
-	case SMBDIRECT_EXPECT_NEGOTIATE_REP:
-		/* client only */
-		break;
-	}
-
-	/*
-	 * This is an internal error!
-	 */
-	WARN_ON_ONCE(sc->recv_io.expected != SMBDIRECT_EXPECT_DATA_TRANSFER);
-	smbdirect_connection_put_recv_io(recvmsg);
-	smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
-}
-
 static int smb_direct_read(struct ksmbd_transport *t, char *buf,
 			   unsigned int size, int unused)
 {
@@ -531,344 +353,24 @@ static void smb_direct_shutdown(struct ksmbd_transport *t)
 
 	ksmbd_debug(RDMA, "smb-direct shutdown cm_id=%p\n", sc->rdma.cm_id);
 
-	smbdirect_connection_disconnect_work(&sc->disconnect_work);
-}
-
-static int smb_direct_cm_handler(struct rdma_cm_id *cm_id,
-				 struct rdma_cm_event *event)
-{
-	struct smbdirect_socket *sc = cm_id->context;
-
-	ksmbd_debug(RDMA, "RDMA CM event. cm_id=%p event=%s (%d)\n",
-		    cm_id, rdma_event_msg(event->event), event->event);
-
-	switch (event->event) {
-	case RDMA_CM_EVENT_ESTABLISHED: {
-		WARN_ON_ONCE(sc->status != SMBDIRECT_SOCKET_RDMA_CONNECT_RUNNING);
-		sc->status = SMBDIRECT_SOCKET_NEGOTIATE_NEEDED;
-		wake_up(&sc->status_wait);
-		break;
-	}
-	case RDMA_CM_EVENT_DEVICE_REMOVAL:
-	case RDMA_CM_EVENT_DISCONNECTED: {
-		sc->status = SMBDIRECT_SOCKET_DISCONNECTED;
-		smbdirect_connection_disconnect_work(&sc->disconnect_work);
-		if (sc->ib.qp)
-			ib_drain_qp(sc->ib.qp);
-		break;
-	}
-	case RDMA_CM_EVENT_CONNECT_ERROR: {
-		sc->status = SMBDIRECT_SOCKET_DISCONNECTED;
-		smbdirect_connection_disconnect_work(&sc->disconnect_work);
-		break;
-	}
-	default:
-		pr_err("Unexpected RDMA CM event. cm_id=%p, event=%s (%d)\n",
-		       cm_id, rdma_event_msg(event->event),
-		       event->event);
-		break;
-	}
-	return 0;
-}
-
-static int smb_direct_send_negotiate_response(struct smbdirect_socket *sc,
-					      int failed)
-{
-	struct smbdirect_socket_parameters *sp = &sc->parameters;
-	struct smbdirect_send_io *sendmsg;
-	struct smbdirect_negotiate_resp *resp;
-	int ret;
-
-	sendmsg = smbdirect_connection_alloc_send_io(sc);
-	if (IS_ERR(sendmsg))
-		return -ENOMEM;
-
-	resp = (struct smbdirect_negotiate_resp *)sendmsg->packet;
-	if (failed) {
-		memset(resp, 0, sizeof(*resp));
-		resp->min_version = SMB_DIRECT_VERSION_LE;
-		resp->max_version = SMB_DIRECT_VERSION_LE;
-		resp->status = STATUS_NOT_SUPPORTED;
-
-		sc->status = SMBDIRECT_SOCKET_NEGOTIATE_FAILED;
-	} else {
-		resp->status = STATUS_SUCCESS;
-		resp->min_version = SMB_DIRECT_VERSION_LE;
-		resp->max_version = SMB_DIRECT_VERSION_LE;
-		resp->negotiated_version = SMB_DIRECT_VERSION_LE;
-		resp->reserved = 0;
-		resp->credits_requested =
-				cpu_to_le16(sp->send_credit_target);
-		resp->credits_granted = cpu_to_le16(smbdirect_connection_grant_recv_credits(sc));
-		resp->max_readwrite_size = cpu_to_le32(sp->max_read_write_size);
-		resp->preferred_send_size = cpu_to_le32(sp->max_send_size);
-		resp->max_receive_size = cpu_to_le32(sp->max_recv_size);
-		resp->max_fragmented_size =
-				cpu_to_le32(sp->max_fragmented_recv_size);
-
-		sc->recv_io.expected = SMBDIRECT_EXPECT_DATA_TRANSFER;
-		sc->status = SMBDIRECT_SOCKET_CONNECTED;
-	}
-
-	sendmsg->sge[0].addr = ib_dma_map_single(sc->ib.dev,
-						 (void *)resp, sizeof(*resp),
-						 DMA_TO_DEVICE);
-	ret = ib_dma_mapping_error(sc->ib.dev, sendmsg->sge[0].addr);
-	if (ret) {
-		smbdirect_connection_free_send_io(sendmsg);
-		return ret;
-	}
-
-	sendmsg->num_sge = 1;
-	sendmsg->sge[0].length = sizeof(*resp);
-	sendmsg->sge[0].lkey = sc->ib.pd->local_dma_lkey;
-
-	ret = smbdirect_connection_post_send_io(sc, NULL, sendmsg);
-	if (ret) {
-		smbdirect_connection_free_send_io(sendmsg);
-		return ret;
-	}
-
-	wait_event(sc->send_io.pending.zero_wait_queue,
-		   atomic_read(&sc->send_io.pending.count) == 0 ||
-		   sc->status != SMBDIRECT_SOCKET_CONNECTED);
-	if (sc->status != SMBDIRECT_SOCKET_CONNECTED)
-		return -ENOTCONN;
-
-	return 0;
-}
-
-static int smb_direct_accept_client(struct smbdirect_socket *sc)
-{
-	struct smbdirect_socket_parameters *sp = &sc->parameters;
-	struct rdma_conn_param conn_param;
-	__be32 ird_ord_hdr[2];
-	int ret;
-
-	/*
-	 * smb_direct_handle_connect_request()
-	 * already negotiated sp->initiator_depth
-	 * and sp->responder_resources
-	 */
-	memset(&conn_param, 0, sizeof(conn_param));
-	conn_param.initiator_depth = sp->initiator_depth;
-	conn_param.responder_resources = sp->responder_resources;
-
-	if (sc->rdma.legacy_iwarp) {
-		ird_ord_hdr[0] = cpu_to_be32(conn_param.responder_resources);
-		ird_ord_hdr[1] = cpu_to_be32(conn_param.initiator_depth);
-		conn_param.private_data = ird_ord_hdr;
-		conn_param.private_data_len = sizeof(ird_ord_hdr);
-	} else {
-		conn_param.private_data = NULL;
-		conn_param.private_data_len = 0;
-	}
-	conn_param.retry_count = SMB_DIRECT_CM_RETRY;
-	conn_param.rnr_retry_count = SMB_DIRECT_CM_RNR_RETRY;
-	conn_param.flow_control = 0;
-
-	/*
-	 * start with the negotiate timeout and SMBDIRECT_KEEPALIVE_PENDING
-	 * so that the timer will cause a disconnect.
-	 */
-	sc->idle.keepalive = SMBDIRECT_KEEPALIVE_PENDING;
-	mod_delayed_work(sc->workqueue, &sc->idle.timer_work,
-			 msecs_to_jiffies(sp->negotiate_timeout_msec));
-
-	WARN_ON_ONCE(sc->status != SMBDIRECT_SOCKET_RDMA_CONNECT_NEEDED);
-	sc->status = SMBDIRECT_SOCKET_RDMA_CONNECT_RUNNING;
-	ret = rdma_accept(sc->rdma.cm_id, &conn_param);
-	if (ret) {
-		pr_err("error at rdma_accept: %d\n", ret);
-		return ret;
-	}
-	return 0;
-}
-
-static int smb_direct_prepare_negotiation(struct smbdirect_socket *sc)
-{
-	struct smbdirect_recv_io *recvmsg;
-	int ret;
-
-	WARN_ON_ONCE(sc->status != SMBDIRECT_SOCKET_CREATED);
-	sc->status = SMBDIRECT_SOCKET_RDMA_CONNECT_NEEDED;
-
-	sc->recv_io.expected = SMBDIRECT_EXPECT_NEGOTIATE_REQ;
-
-	recvmsg = smbdirect_connection_get_recv_io(sc);
-	if (!recvmsg)
-		return -ENOMEM;
-
-	ret = smbdirect_connection_post_recv_io(recvmsg);
-	if (ret) {
-		pr_err("Can't post recv: %d\n", ret);
-		goto out_err;
-	}
-
-	ret = smb_direct_accept_client(sc);
-	if (ret) {
-		pr_err("Can't accept client\n");
-		goto out_err;
-	}
-
-	return 0;
-out_err:
-	smbdirect_connection_put_recv_io(recvmsg);
-	return ret;
-}
-
-static int smb_direct_init_params(struct smbdirect_socket *sc)
-{
-	struct smbdirect_socket_parameters *sp = &sc->parameters;
-	int max_send_sges;
-	unsigned int maxpages;
-
-	/* need 3 more sge. because a SMB_DIRECT header, SMB2 header,
-	 * SMB2 response could be mapped.
-	 */
-	max_send_sges = DIV_ROUND_UP(sp->max_send_size, PAGE_SIZE) + 3;
-	if (max_send_sges > SMBDIRECT_SEND_IO_MAX_SGE) {
-		pr_err("max_send_size %d is too large\n", sp->max_send_size);
-		return -EINVAL;
-	}
-
-	atomic_set(&sc->send_io.lcredits.count, sp->send_credit_target);
-
-	maxpages = DIV_ROUND_UP(sp->max_read_write_size, PAGE_SIZE);
-	sc->rw_io.credits.max = rdma_rw_mr_factor(sc->ib.dev,
-						  sc->rdma.cm_id->port_num,
-						  maxpages);
-	sc->rw_io.credits.num_pages = DIV_ROUND_UP(maxpages, sc->rw_io.credits.max);
-	/* add one extra in order to handle unaligned pages */
-	sc->rw_io.credits.max += 1;
-
-	sc->recv_io.credits.target = 1;
-
-	atomic_set(&sc->rw_io.credits.count, sc->rw_io.credits.max);
-
-	return 0;
+	smbdirect_socket_shutdown(sc);
 }
 
 static int smb_direct_prepare(struct ksmbd_transport *t)
 {
 	struct smb_direct_transport *st = SMBD_TRANS(t);
 	struct smbdirect_socket *sc = &st->socket;
-	struct smbdirect_socket_parameters *sp = &sc->parameters;
-	struct smbdirect_recv_io *recvmsg;
-	struct smbdirect_negotiate_req *req;
-	unsigned long flags;
 	int ret;
 
-	/*
-	 * We are waiting to pass the following states:
-	 *
-	 * SMBDIRECT_SOCKET_RDMA_CONNECT_NEEDED
-	 * SMBDIRECT_SOCKET_RDMA_CONNECT_RUNNING
-	 * SMBDIRECT_SOCKET_NEGOTIATE_NEEDED
-	 *
-	 * To finally get to SMBDIRECT_SOCKET_NEGOTIATE_RUNNING
-	 * in order to continue below.
-	 *
-	 * Everything else is unexpected and an error.
-	 */
-	ksmbd_debug(RDMA, "Waiting for SMB_DIRECT negotiate request\n");
-	ret = wait_event_interruptible_timeout(sc->status_wait,
-					sc->status != SMBDIRECT_SOCKET_RDMA_CONNECT_NEEDED &&
-					sc->status != SMBDIRECT_SOCKET_RDMA_CONNECT_RUNNING &&
-					sc->status != SMBDIRECT_SOCKET_NEGOTIATE_NEEDED,
-					msecs_to_jiffies(sp->negotiate_timeout_msec));
-	if (ret <= 0 || sc->status != SMBDIRECT_SOCKET_NEGOTIATE_RUNNING)
-		return ret < 0 ? ret : -ETIMEDOUT;
-
-	recvmsg = smbdirect_connection_reassembly_first_recv_io(sc);
-	if (!recvmsg)
-		return -ECONNABORTED;
-
-	ret = smb_direct_check_recvmsg(recvmsg);
-	if (ret)
-		goto put;
-
-	req = (struct smbdirect_negotiate_req *)recvmsg->packet;
-	sp->max_recv_size = min_t(int, sp->max_recv_size,
-				  le32_to_cpu(req->preferred_send_size));
-	sp->max_send_size = min_t(int, sp->max_send_size,
-				  le32_to_cpu(req->max_receive_size));
-	sp->max_fragmented_send_size =
-		le32_to_cpu(req->max_fragmented_size);
-	sp->max_fragmented_recv_size =
-		(sp->recv_credit_max * sp->max_recv_size) / 2;
-	sc->recv_io.credits.target = le16_to_cpu(req->credits_requested);
-	sc->recv_io.credits.target = min_t(u16, sc->recv_io.credits.target, sp->recv_credit_max);
-	sc->recv_io.credits.target = max_t(u16, sc->recv_io.credits.target, 1);
-
-put:
-	spin_lock_irqsave(&sc->recv_io.reassembly.lock, flags);
-	sc->recv_io.reassembly.queue_length--;
-	list_del(&recvmsg->list);
-	spin_unlock_irqrestore(&sc->recv_io.reassembly.lock, flags);
-	smbdirect_connection_put_recv_io(recvmsg);
-
-	if (ret == -ECONNABORTED)
-		return ret;
-
-	if (ret)
-		goto respond;
-
-	/*
-	 * We negotiated with success, so we need to refill the recv queue.
-	 *
-	 * The message that grants the credits to the client is
-	 * the negotiate response.
-	 */
-	ret = smbdirect_connection_recv_io_refill(sc);
-	if (ret < 0)
-		return ret;
-	ret = 0;
-
-respond:
-	ret = smb_direct_send_negotiate_response(sc, ret);
-	if (ret)
-		return ret;
-
-	INIT_WORK(&sc->recv_io.posted.refill_work, smbdirect_connection_recv_io_refill_work);
-	INIT_WORK(&sc->idle.immediate_work, smbdirect_connection_send_immediate_work);
-
-	return 0;
-}
-
-static int smb_direct_connect(struct smbdirect_socket *sc)
-{
-	struct smbdirect_recv_io *recv_io;
-	int ret;
-
-	sc->rdma.cm_id->event_handler = smb_direct_cm_handler;
-
-	ret = smb_direct_init_params(sc);
+	ksmbd_debug(RDMA, "SMB_DIRECT Waiting for connection\n");
+	ret = smbdirect_connection_wait_for_connected(sc);
 	if (ret) {
-		pr_err("Can't configure RDMA parameters\n");
+		ksmbd_debug(RDMA, "SMB_DIRECT connection failed %d => %s\n",
+			    ret, errname(ret));
 		return ret;
 	}
 
-	ret = smbdirect_connection_create_mem_pools(sc);
-	if (ret) {
-		pr_err("Can't init RDMA pool: %d\n", ret);
-		return ret;
-	}
-
-	list_for_each_entry(recv_io, &sc->recv_io.free.list, list)
-		recv_io->cqe.done = recv_done;
-
-	ret = smbdirect_connection_create_qp(sc);
-	if (ret) {
-		pr_err("Can't accept RDMA client: %d\n", ret);
-		return ret;
-	}
-
-	ret = smb_direct_prepare_negotiation(sc);
-	if (ret) {
-		pr_err("Can't negotiate: %d\n", ret);
-		return ret;
-	}
+	ksmbd_debug(RDMA, "SMB_DIRECT connection ready\n");
 	return 0;
 }
 
@@ -877,10 +379,7 @@ static int smb_direct_handle_connect_request(struct rdma_cm_id *new_cm_id,
 {
 	struct smb_direct_transport *t;
 	struct smbdirect_socket *sc;
-	struct smbdirect_socket_parameters *sp;
 	struct task_struct *handler;
-	u8 peer_initiator_depth;
-	u8 peer_responder_resources;
 	int ret;
 
 	if (!smbdirect_frwr_is_supported(&new_cm_id->device->attrs)) {
@@ -894,22 +393,8 @@ static int smb_direct_handle_connect_request(struct rdma_cm_id *new_cm_id,
 	if (!t)
 		return -ENOMEM;
 	sc = &t->socket;
-	sp = &sc->parameters;
-
-	/*
-	 * First set what the we as server are able to support
-	 */
-	sp->initiator_depth = min_t(u8, sp->initiator_depth,
-				    sc->ib.dev->attrs.max_qp_rd_atom);
-
-	peer_initiator_depth = event->param.conn.initiator_depth;
-	peer_responder_resources = event->param.conn.responder_resources;
-	smbdirect_connection_negotiate_rdma_resources(sc,
-						      peer_initiator_depth,
-						      peer_responder_resources,
-						      &event->param.conn);
 
-	ret = smb_direct_connect(sc);
+	ret = smbdirect_accept_connect_request(sc, &event->param.conn);
 	if (ret)
 		goto out_err;
 
Signature · 1 line
2.43.0
author reviewer
#129 lore raw

PATCH v2 125/127 smb: server: make use of smbdirect_socket_create_accepting()/smbdirect_socket_release()

In reply to Stefan Metzmacher

With this we no longer embed struct smbdirect_socket, which will allow us to make it private in the following commits.

---
 fs/smb/server/transport_rdma.c | 53 +++++++++++++++-------------------
 1 file changed, 23 insertions(+), 30 deletions(-)
fs/smb/server/transport_rdma.c +23 -30
index d6162b8be58b..85aed6963c86 100644
@@ -42,8 +42,6 @@
 #define SMB_DIRECT_PORT_IWARP		5445
 #define SMB_DIRECT_PORT_INFINIBAND	445
 
-#define SMB_DIRECT_VERSION_LE		cpu_to_le16(SMBDIRECT_V1)
-
 /* SMB_DIRECT negotiation timeout (for the server) in seconds */
 #define SMB_DIRECT_NEGOTIATE_TIMEOUT		5
 
@@ -59,11 +57,6 @@
  */
 #define SMB_DIRECT_CM_INITIATOR_DEPTH		8
 
-/* Maximum number of retries on data transfer operations */
-#define SMB_DIRECT_CM_RETRY			6
-/* No need to retry on Receiver Not Ready since SMB_DIRECT manages credits */
-#define SMB_DIRECT_CM_RNR_RETRY		0
-
 /*
  * User configurable initial values per SMB_DIRECT transport connection
  * as defined in [MS-SMBD] 3.1.1.1
@@ -107,7 +100,7 @@ static struct workqueue_struct *smb_direct_wq;
 struct smb_direct_transport {
 	struct ksmbd_transport	transport;
 
-	struct smbdirect_socket socket;
+	struct smbdirect_socket *socket;
 };
 
 static bool smb_direct_logging_needed(struct smbdirect_socket *sc,
@@ -184,15 +177,13 @@ void init_smbd_max_io_size(unsigned int sz)
 unsigned int get_smbd_max_read_write_size(struct ksmbd_transport *kt)
 {
 	struct smb_direct_transport *t;
-	struct smbdirect_socket *sc;
 	const struct smbdirect_socket_parameters *sp;
 
 	if (kt->ops != &ksmbd_smb_direct_transport_ops)
 		return 0;
 
 	t = SMBD_TRANS(kt);
-	sc = &t->socket;
-	sp = smbdirect_socket_get_current_parameters(sc);
+	sp = smbdirect_socket_get_current_parameters(t->socket);
 
 	return sp->max_read_write_size;
 }
@@ -225,10 +216,9 @@ static struct smb_direct_transport *alloc_transport(struct rdma_cm_id *cm_id)
 	t = kzalloc(sizeof(*t), KSMBD_DEFAULT_GFP);
 	if (!t)
 		return NULL;
-	sc = &t->socket;
-	ret = smbdirect_socket_init_accepting(cm_id, sc);
+	ret = smbdirect_socket_create_accepting(cm_id, &sc);
 	if (ret)
-		goto socket_init_failed;
+		goto socket_create_failed;
 	smbdirect_socket_set_logging(sc, NULL,
 				     smb_direct_logging_needed,
 				     smb_direct_logging_vaprintf);
@@ -253,28 +243,31 @@ static struct smb_direct_transport *alloc_transport(struct rdma_cm_id *cm_id)
 	conn->transport = KSMBD_TRANS(t);
 	KSMBD_TRANS(t)->conn = conn;
 	KSMBD_TRANS(t)->ops = &ksmbd_smb_direct_transport_ops;
+
+	t->socket = sc;
 	return t;
 
 conn_alloc_failed:
 set_workqueue_failed:
 set_settings_failed:
 set_params_failed:
-socket_init_failed:
+	smbdirect_socket_release(sc);
+socket_create_failed:
 	kfree(t);
 	return NULL;
 }
 
 static void smb_direct_free_transport(struct ksmbd_transport *kt)
 {
-	kfree(SMBD_TRANS(kt));
+	struct smb_direct_transport *t = SMBD_TRANS(kt);
+
+	smbdirect_socket_release(t->socket);
+	kfree(t);
 }
 
 static void free_transport(struct smb_direct_transport *t)
 {
-	struct smbdirect_socket *sc = &t->socket;
-
-	smbdirect_connection_destroy_sync(sc);
-
+	smbdirect_socket_shutdown(t->socket);
 	ksmbd_conn_free(KSMBD_TRANS(t)->conn);
 }
 
@@ -282,7 +275,7 @@ static int smb_direct_read(struct ksmbd_transport *t, char *buf,
 			   unsigned int size, int unused)
 {
 	struct smb_direct_transport *st = SMBD_TRANS(t);
-	struct smbdirect_socket *sc = &st->socket;
+	struct smbdirect_socket *sc = st->socket;
 	struct msghdr msg = { .msg_flags = 0, };
 	struct kvec iov = {
 		.iov_base = buf,
@@ -303,7 +296,7 @@ static int smb_direct_writev(struct ksmbd_transport *t,
 			     bool need_invalidate, unsigned int remote_key)
 {
 	struct smb_direct_transport *st = SMBD_TRANS(t);
-	struct smbdirect_socket *sc = &st->socket;
+	struct smbdirect_socket *sc = st->socket;
 	struct iov_iter iter;
 
 	iov_iter_kvec(&iter, ITER_SOURCE, iov, niovs, buflen);
@@ -318,7 +311,7 @@ static int smb_direct_rdma_write(struct ksmbd_transport *t,
 				 unsigned int desc_len)
 {
 	struct smb_direct_transport *st = SMBD_TRANS(t);
-	struct smbdirect_socket *sc = &st->socket;
+	struct smbdirect_socket *sc = st->socket;
 
 	return smbdirect_connection_rdma_xmit(sc, buf, buflen,
 					      desc, desc_len, false);
@@ -330,7 +323,7 @@ static int smb_direct_rdma_read(struct ksmbd_transport *t,
 				unsigned int desc_len)
 {
 	struct smb_direct_transport *st = SMBD_TRANS(t);
-	struct smbdirect_socket *sc = &st->socket;
+	struct smbdirect_socket *sc = st->socket;
 
 	return smbdirect_connection_rdma_xmit(sc, buf, buflen,
 					      desc, desc_len, true);
@@ -339,9 +332,9 @@ static int smb_direct_rdma_read(struct ksmbd_transport *t,
 static void smb_direct_disconnect(struct ksmbd_transport *t)
 {
 	struct smb_direct_transport *st = SMBD_TRANS(t);
-	struct smbdirect_socket *sc = &st->socket;
+	struct smbdirect_socket *sc = st->socket;
 
-	ksmbd_debug(RDMA, "Disconnecting cm_id=%p\n", sc->rdma.cm_id);
+	ksmbd_debug(RDMA, "Disconnecting sc=%p\n", sc);
 
 	free_transport(st);
 }
@@ -349,9 +342,9 @@ static void smb_direct_disconnect(struct ksmbd_transport *t)
 static void smb_direct_shutdown(struct ksmbd_transport *t)
 {
 	struct smb_direct_transport *st = SMBD_TRANS(t);
-	struct smbdirect_socket *sc = &st->socket;
+	struct smbdirect_socket *sc = st->socket;
 
-	ksmbd_debug(RDMA, "smb-direct shutdown cm_id=%p\n", sc->rdma.cm_id);
+	ksmbd_debug(RDMA, "smb-direct shutdown sc=%p\n", sc);
 
 	smbdirect_socket_shutdown(sc);
 }
@@ -359,7 +352,7 @@ static void smb_direct_shutdown(struct ksmbd_transport *t)
 static int smb_direct_prepare(struct ksmbd_transport *t)
 {
 	struct smb_direct_transport *st = SMBD_TRANS(t);
-	struct smbdirect_socket *sc = &st->socket;
+	struct smbdirect_socket *sc = st->socket;
 	int ret;
 
 	ksmbd_debug(RDMA, "SMB_DIRECT Waiting for connection\n");
@@ -392,7 +385,7 @@ static int smb_direct_handle_connect_request(struct rdma_cm_id *new_cm_id,
 	t = alloc_transport(new_cm_id);
 	if (!t)
 		return -ENOMEM;
-	sc = &t->socket;
+	sc = t->socket;
 
 	ret = smbdirect_accept_connect_request(sc, &event->param.conn);
 	if (ret)
Signature · 1 line
2.43.0
author reviewer
#130 lore raw

PATCH v2 126/127 smb: server: only use public smbdirect functions

In reply to Stefan Metzmacher

Also remove a lot of unused includes...

---
 fs/smb/server/smb2pdu.c        |  1 -
 fs/smb/server/transport_rdma.c | 25 +++++++++----------------
 fs/smb/server/transport_rdma.h |  2 ++
 3 files changed, 11 insertions(+), 17 deletions(-)
fs/smb/server/smb2pdu.c +0 -1
index f901ae18e68a..d20c8e5cd18e 100644
@@ -23,7 +23,6 @@
 #include "asn1.h"
 #include "connection.h"
 #include "transport_ipc.h"
-#include "../common/smbdirect/smbdirect.h"
 #include "transport_rdma.h"
 #include "vfs.h"
 #include "vfs_cache.h"
fs/smb/server/transport_rdma.c +9 -16
index 85aed6963c86..e7d54283ae47 100644
@@ -13,31 +13,16 @@
 
 #include <linux/kthread.h>
 #include <linux/list.h>
-#include <linux/mempool.h>
-#include <linux/highmem.h>
-#include <linux/scatterlist.h>
 #include <linux/string_choices.h>
 #include <linux/errname.h>
-#include <rdma/ib_verbs.h>
-#include <rdma/rdma_cm.h>
-#include <rdma/rw.h>
 
 #include "glob.h"
 #include "connection.h"
 #include "smb_common.h"
 #include "../common/smb2status.h"
-#include "../common/smbdirect/smbdirect.h"
-#include "../common/smbdirect/smbdirect_pdu.h"
-#include "../common/smbdirect/smbdirect_socket.h"
 #include "transport_rdma.h"
+#include "../common/smbdirect/smbdirect_public.h"
 
-/*
- * This is a temporary solution until all code
- * is moved to smbdirect_all_c_files.c and we
- * have an smbdirect.ko that exports the required
- * functions.
- */
-#include "../common/smbdirect/smbdirect_all_c_files.c"
 
 #define SMB_DIRECT_PORT_IWARP		5445
 #define SMB_DIRECT_PORT_INFINIBAND	445
@@ -642,3 +627,11 @@ static const struct ksmbd_transport_ops ksmbd_smb_direct_transport_ops = {
 	.rdma_write	= smb_direct_rdma_write,
 	.free_transport = smb_direct_free_transport,
 };
+
+/*
+ * This is a temporary solution until all code
+ * is moved to smbdirect_all_c_files.c and we
+ * have an smbdirect.ko that exports the required
+ * functions.
+ */
+#include "../common/smbdirect/smbdirect_all_c_files.c"
fs/smb/server/transport_rdma.h +2 -0
index 3f93c6a9f7e4..e16f625caed2 100644
@@ -27,4 +27,6 @@ static inline void init_smbd_max_io_size(unsigned int sz) { }
 static inline unsigned int get_smbd_max_read_write_size(struct ksmbd_transport *kt) { return 0; }
 #endif
 
+#include "../common/smbdirect/smbdirect.h"
+
 #endif /* __KSMBD_TRANSPORT_RDMA_H__ */
Signature · 1 line
2.43.0
author reviewer
#131 lore raw

PATCH v2 127/127 smb: server: make use of smbdirect.ko

In reply to Stefan Metzmacher

This means we no longer inline the common smbdirect .c files and use the exported functions from the module instead.

Note the connection specific logging is still redirect to ksmbd.ko functions via smbdirect_socket_set_logging().

We still don't use real socket layer, but we're very close...

---
 fs/smb/server/Kconfig          |  4 ++--
 fs/smb/server/transport_rdma.c | 10 ----------
 2 files changed, 2 insertions(+), 12 deletions(-)
fs/smb/server/Kconfig +2 -2
index 098cac98d31e..59a059ecabec 100644
@@ -49,8 +49,8 @@ if SMB_SERVER
 
 config SMB_SERVER_SMBDIRECT
 	bool "Support for SMB Direct protocol"
-	depends on SMB_SERVER=m && INFINIBAND && INFINIBAND_ADDR_TRANS || SMB_SERVER=y && INFINIBAND=y && INFINIBAND_ADDR_TRANS=y
-	select SG_POOL
+	depends on SMB_SERVER && INFINIBAND && INFINIBAND_ADDR_TRANS
+	select SMB_COMMON_SMBDIRECT
 	default n
 
 	help
fs/smb/server/transport_rdma.c +0 -10
index e7d54283ae47..c261082ff9c7 100644
@@ -9,8 +9,6 @@
 
 #define SUBMOD_NAME	"smb_direct"
 
-#define SMBDIRECT_USE_INLINE_C_FILES 1
-
 #include <linux/kthread.h>
 #include <linux/list.h>
 #include <linux/string_choices.h>
@@ -627,11 +625,3 @@ static const struct ksmbd_transport_ops ksmbd_smb_direct_transport_ops = {
 	.rdma_write	= smb_direct_rdma_write,
 	.free_transport = smb_direct_free_transport,
 };
-
-/*
- * This is a temporary solution until all code
- * is moved to smbdirect_all_c_files.c and we
- * have an smbdirect.ko that exports the required
- * functions.
- */
-#include "../common/smbdirect/smbdirect_all_c_files.c"
Signature · 1 line
2.43.0
author reviewer
#132 lore raw
In reply to Stefan Metzmacher

Hi,

I already used these debugging features:

CONFIG_KASAN_SHADOW_OFFSET=0xdffffc0000000000
CONFIG_HAVE_ARCH_KASAN=y
CONFIG_HAVE_ARCH_KASAN_VMALLOC=y
CONFIG_CC_HAS_KASAN_GENERIC=y
CONFIG_KASAN=y
CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX=y
CONFIG_KASAN_GENERIC=y
CONFIG_KASAN_INLINE=y
CONFIG_KASAN_STACK=y
CONFIG_KASAN_VMALLOC=y
CONFIG_KASAN_EXTRA_INFO=y

But now I turned on a lot more debugging features:

CONFIG_PRINTK_CALLER=y
CONFIG_STACKTRACE_BUILD_ID=y
CONFIG_DEBUG_INFO_COMPRESSED_ZSTD=y
CONFIG_DEBUG_SECTION_MISMATCH=y
CONFIG_MAGIC_SYSRQ_DEFAULT_ENABLE=0xffffffff
CONFIG_PAGE_EXTENSION=y
CONFIG_PAGE_OWNER=y
CONFIG_PAGE_TABLE_CHECK=y
CONFIG_DEBUG_PAGE_REF=y
CONFIG_PTDUMP_DEBUGFS=y
CONFIG_DEBUG_KMEMLEAK=y
CONFIG_DEBUG_KMEMLEAK_MEM_POOL_SIZE=16000
CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF=y
CONFIG_DEBUG_OBJECTS=y
CONFIG_DEBUG_OBJECTS_FREE=y
CONFIG_DEBUG_OBJECTS_TIMERS=y
CONFIG_DEBUG_OBJECTS_WORK=y
CONFIG_DEBUG_OBJECTS_RCU_HEAD=y
CONFIG_DEBUG_OBJECTS_PERCPU_COUNTER=y
CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT=0
CONFIG_WQ_WATCHDOG=y
CONFIG_PROVE_LOCKING=y
CONFIG_PROVE_RAW_LOCK_NESTING=y
CONFIG_LOCK_STAT=y
CONFIG_DEBUG_RT_MUTEXES=y
CONFIG_DEBUG_SPINLOCK=y
CONFIG_DEBUG_MUTEXES=y
CONFIG_DEBUG_WW_MUTEX_SLOWPATH=y
CONFIG_DEBUG_RWSEMS=y
CONFIG_DEBUG_LOCK_ALLOC=y
CONFIG_LOCKDEP=y
CONFIG_LOCKDEP_BITS=15
CONFIG_LOCKDEP_CHAINS_BITS=16
CONFIG_LOCKDEP_STACK_TRACE_BITS=19
CONFIG_LOCKDEP_STACK_TRACE_HASH_BITS=14
CONFIG_LOCKDEP_CIRCULAR_QUEUE_BITS=12
CONFIG_DEBUG_ATOMIC_SLEEP=y
CONFIG_TRACE_IRQFLAGS=y
CONFIG_TRACE_IRQFLAGS_NMI=y
CONFIG_DEBUG_IRQFLAGS=y
CONFIG_PROVE_RCU=y
CONFIG_RCU_TRACE=y
CONFIG_RING_BUFFER_ALLOW_SWAP=y
CONFIG_PREEMPTIRQ_TRACEPOINTS=y
CONFIG_TRACE_PREEMPT_TOGGLE=y
CONFIG_IRQSOFF_TRACER=y
CONFIG_PREEMPT_TRACER=y
CONFIG_TRACER_SNAPSHOT_PER_CPU_SWAP=y

And that luckily revealed some cases where
I called functions which might sleep in a
ib_post_recv completion function that
is called in a softirq as the client uses
IB_POLL_SOFTIRQ.

I means that I needed to deferr some of the
smbdirect_connect_negotiate_recv_done and
smbdirect_accept_negotiate_recv_done logic
to the workqueue.

And I added some includes to smbdirect_socket.h,
as some sparc cross compile bot was complaining
about a failing build.

I also did a few minor changes like using
sc->ib.dev consistently.

As well as setting sc->first_error as early as possible.

I took the current ksmbd-for-next-next at commit 2bd117c715790585d69a06bd5f795520fc67ce86 and rebased it on v6.18-rc4 and then removed my patches and added the modified once on top.

The result can be found in my for-6.19/fs-smb-20251104-v3 branch, at commit a73e9e7470238121b87b2128da2ed1fff9fd6d4b:
git fetch https://git.samba.org/metze/linux/wip.git for-6.19/fs-smb-20251104-v3
https://git.samba.org/?p=metze/linux/wip.git;a=shortlog;h=refs/heads/for-6.19/fs-smb-20251104-v3

The whole diff between the old and new patchset
follows below:

The new branch should replace ksmbd-for-next-next...

Thanks!
metze

 fs/smb/common/smbdirect/smbdirect_accept.c     | 154 +++++++++++++++-------
 fs/smb/common/smbdirect/smbdirect_connect.c    | 171 +++++++++++++++++--------
 fs/smb/common/smbdirect/smbdirect_connection.c |  91 +++++++++++--
 fs/smb/common/smbdirect/smbdirect_socket.h     |  15 +++
 4 files changed, 316 insertions(+), 115 deletions(-)
fs/smb/common/smbdirect/smbdirect_accept.c +106 -48
index 0ed3f43a7397..3455e7327663 100644
@@ -192,25 +192,14 @@ static int smbdirect_accept_init_params(struct smbdirect_socket *sc)
 	return 0;
 }
 
+static void smbdirect_accept_negotiate_recv_work(struct work_struct *work);
+
 static void smbdirect_accept_negotiate_recv_done(struct ib_cq *cq, struct ib_wc *wc)
 {
 	struct smbdirect_recv_io *recv_io =
 		container_of(wc->wr_cqe, struct smbdirect_recv_io, cqe);
 	struct smbdirect_socket *sc = recv_io->socket;
-	struct smbdirect_socket_parameters *sp = &sc->parameters;
-	struct smbdirect_negotiate_req *nreq;
-	u16 min_version;
-	u16 max_version;
-	u16 credits_requested;
-	u32 preferred_send_size;
-	u32 max_receive_size;
-	u32 max_fragmented_size;
-	struct smbdirect_send_io *send_io = NULL;
-	struct smbdirect_negotiate_resp *nrep;
-	struct ib_send_wr send_wr;
-	u32 ntstatus;
-	int posted;
-	int ret;
+	const struct smbdirect_socket_parameters *sp = &sc->parameters;
 
 	if (unlikely(wc->status != IB_WC_SUCCESS || WARN_ON_ONCE(wc->opcode != IB_WC_RECV))) {
 		if (wc->status != IB_WC_WR_FLUSH_ERR)
@@ -235,18 +224,98 @@ static void smbdirect_accept_negotiate_recv_done(struct ib_cq *cq, struct ib_wc
 	mod_delayed_work(sc->workqueue, &sc->idle.timer_work,
 			 msecs_to_jiffies(sp->keepalive_interval_msec));
 
-	ib_dma_sync_single_for_cpu(wc->qp->device,
+	ib_dma_sync_single_for_cpu(sc->ib.dev,
 				   recv_io->sge.addr,
 				   recv_io->sge.length,
 				   DMA_FROM_DEVICE);
 
-	if (wc->byte_len < sizeof(*nreq)) {
+	if (wc->byte_len < sizeof(struct smbdirect_negotiate_req)) {
 		smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
 			"wc->byte_len=%u < %zu\n",
-			wc->byte_len, sizeof(*nreq));
+			wc->byte_len, sizeof(struct smbdirect_negotiate_req));
 		goto error;
 	}
 
+	/*
+	 * We continue via the workqueue as we may have
+	 * complex work that might sleep.
+	 *
+	 * The work should already/still be disabled,
+	 * but smbdirect_connection_put_recv_io() disables
+	 * it again.
+	 *
+	 * Note that smbdirect_connection_put_recv_io()
+	 * only moved recv_io into the free list, but
+	 * we didn't call smbdirect_connection_recv_io_refill()
+	 * yet, so it won't be reused, but the cleanup code
+	 * on disconnect is able to find it, disables
+	 * recv_io->complex_work again.
+	 */
+	smbdirect_connection_put_recv_io(recv_io);
+	INIT_WORK(&recv_io->complex_work, smbdirect_accept_negotiate_recv_work);
+	queue_work(sc->workqueue, &recv_io->complex_work);
+	return;
+
+error:
+	/*
+	 * recv_io.posted.refill_work is still disabled,
+	 * so smbdirect_connection_put_recv_io() won't
+	 * start it.
+	 */
+	smbdirect_connection_put_recv_io(recv_io);
+	smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
+}
+
+static void smbdirect_accept_negotiate_recv_work(struct work_struct *work)
+{
+	struct smbdirect_recv_io *recv_io =
+		container_of(work, struct smbdirect_recv_io, complex_work);
+	struct smbdirect_socket *sc = recv_io->socket;
+	struct smbdirect_socket_parameters *sp = &sc->parameters;
+	struct smbdirect_negotiate_req *nreq;
+	u16 min_version;
+	u16 max_version;
+	u16 credits_requested;
+	u32 preferred_send_size;
+	u32 max_receive_size;
+	u32 max_fragmented_size;
+	struct smbdirect_send_io *send_io = NULL;
+	struct smbdirect_negotiate_resp *nrep;
+	struct ib_send_wr send_wr;
+	u32 ntstatus;
+	int posted;
+	int ret;
+
+	/*
+	 * make sure we won't start again...
+	 */
+	disable_work(work);
+
+	/*
+	 * Note recv_io is already part of the free list,
+	 * as smbdirect_connect_negotiate_recv_done() called
+	 * smbdirect_connection_put_recv_io(), but
+	 * it won't be reused before we call
+	 * smbdirect_connection_recv_io_refill() below.
+	 */
+
+	if (unlikely(sc->first_error))
+		return;
+
+	if (sc->status != SMBDIRECT_SOCKET_NEGOTIATE_RUNNING) {
+		/*
+		 * Something went wrong...
+		 */
+		smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
+			"status=%s first_error=%1pe local: %pISpsfc remote: %pISpsfc\n",
+			smbdirect_socket_status_string(sc->status),
+			SMBDIRECT_DEBUG_ERR_PTR(sc->first_error),
+			&sc->rdma.cm_id->route.addr.src_addr,
+			&sc->rdma.cm_id->route.addr.dst_addr);
+		smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
+		return;
+	}
+
 	nreq = (struct smbdirect_negotiate_req *)recv_io->packet;
 	min_version = le16_to_cpu(nreq->min_version);
 	max_version = le16_to_cpu(nreq->max_version);
@@ -281,7 +350,8 @@ static void smbdirect_accept_negotiate_recv_done(struct ib_cq *cq, struct ib_wc
 	if (credits_requested == 0) {
 		smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
 			"invalid: credits_requested == 0\n");
-		goto error;
+		smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
+		return;
 	}
 
 	if (max_receive_size < SMBDIRECT_MIN_RECEIVE_SIZE) {
@@ -289,7 +359,8 @@ static void smbdirect_accept_negotiate_recv_done(struct ib_cq *cq, struct ib_wc
 			"invalid: max_receive_size=%u < %u\n",
 			max_receive_size,
 			SMBDIRECT_MIN_RECEIVE_SIZE);
-		goto error;
+		smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
+		return;
 	}
 
 	if (max_fragmented_size < SMBDIRECT_MIN_FRAGMENTED_SIZE) {
@@ -297,7 +368,8 @@ static void smbdirect_accept_negotiate_recv_done(struct ib_cq *cq, struct ib_wc
 			"invalid: max_fragmented_size=%u < %u\n",
 			max_fragmented_size,
 			SMBDIRECT_MIN_FRAGMENTED_SIZE);
-		goto error;
+		smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
+		return;
 	}
 
 	/*
@@ -327,12 +399,6 @@ static void smbdirect_accept_negotiate_recv_done(struct ib_cq *cq, struct ib_wc
 	 */
 	sp->max_fragmented_send_size = max_fragmented_size;
 
-	/*
-	 * Give recv_io back to the recv_io.free.list,
-	 * so that the refill can also post it.
-	 */
-	smbdirect_connection_put_recv_io(recv_io);
-
 	/*
 	 * Prepare for receiving data_transfer messages
 	 */
@@ -350,7 +416,8 @@ static void smbdirect_accept_negotiate_recv_done(struct ib_cq *cq, struct ib_wc
 		smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
 			"smbdirect_connection_recv_io_refill() failed %1pe\n",
 			SMBDIRECT_DEBUG_ERR_PTR(posted));
-		goto error;
+		smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
+		return;
 	}
 
 	/*
@@ -365,11 +432,11 @@ static void smbdirect_accept_negotiate_recv_done(struct ib_cq *cq, struct ib_wc
 	send_io = smbdirect_connection_alloc_send_io(sc);
 	if (IS_ERR(send_io)) {
 		ret = PTR_ERR(send_io);
-		send_io = NULL;
 		smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
 			"smbdirect_connection_alloc_send_io() failed %1pe\n",
 			SMBDIRECT_DEBUG_ERR_PTR(ret));
-		goto error;
+		smbdirect_connection_schedule_disconnect(sc, ret);
+		return;
 	}
 	send_io->cqe.done = smbdirect_accept_negotiate_send_done;
 
@@ -430,7 +497,9 @@ static void smbdirect_accept_negotiate_recv_done(struct ib_cq *cq, struct ib_wc
 		smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
 			"ib_dma_mapping_error() failed %1pe\n",
 			SMBDIRECT_DEBUG_ERR_PTR(ret));
-		goto error;
+		smbdirect_connection_free_send_io(send_io);
+		smbdirect_connection_schedule_disconnect(sc, ret);
+		return;
 	}
 
 	send_io->sge[0].length = sizeof(*nrep);
@@ -455,30 +524,19 @@ static void smbdirect_accept_negotiate_recv_done(struct ib_cq *cq, struct ib_wc
 		smbdirect_log_rdma_send(sc, SMBDIRECT_LOG_ERR,
 			"smbdirect_connection_post_send_wr() failed %1pe\n",
 			SMBDIRECT_DEBUG_ERR_PTR(ret));
-		goto error;
+		/*
+		 * Note smbdirect_connection_free_send_io()
+		 * does ib_dma_unmap_page()
+		 */
+		smbdirect_connection_free_send_io(send_io);
+		smbdirect_connection_schedule_disconnect(sc, ret);
+		return;
 	}
 
 	/*
 	 * smbdirect_accept_negotiate_send_done
 	 * will do all remaining work...
 	 */
-	return;
-
-error:
-	/*
-	 * recv_io.posted.refill_work is still disabled,
-	 * so smbdirect_connection_put_recv_io() won't
-	 * start it.
-	 */
-	if (recv_io)
-		smbdirect_connection_put_recv_io(recv_io);
-	/*
-	 * Note smbdirect_connection_free_send_io()
-	 * does ib_dma_unmap_page()
-	 */
-	if (send_io)
-		smbdirect_connection_free_send_io(send_io);
-	smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
 }
 
 static void smbdirect_accept_negotiate_send_done(struct ib_cq *cq, struct ib_wc *wc)
fs/smb/common/smbdirect/smbdirect_connect.c +116 -55

261 lines not on this page · raw message

fs/smb/common/smbdirect/smbdirect_connection.c +79 -12

170 lines not on this page · raw message

fs/smb/common/smbdirect/smbdirect_socket.h +15 -0
index 65f25fc4b4a7..ef151ff1c02a 100644
@@ -6,6 +6,13 @@
 #ifndef __FS_SMB_COMMON_SMBDIRECT_SMBDIRECT_SOCKET_H__
 #define __FS_SMB_COMMON_SMBDIRECT_SMBDIRECT_SOCKET_H__
 
+#include <linux/wait.h>
+#include <linux/workqueue.h>
+#include <linux/kref.h>
+#include <linux/mempool.h>
+#include <linux/spinlock.h>
+#include <linux/mutex.h>
+#include <linux/completion.h>
 #include <rdma/rw.h>
 
 enum smbdirect_socket_status {
@@ -617,6 +624,14 @@ struct smbdirect_recv_io {
 #define SMBDIRECT_RECV_IO_MAX_SGE 1
 	struct ib_sge sge;
 
+	/*
+	 * We may need to handle complex
+	 * work that might sleep and are
+	 * not allowed to run in an interrupt
+	 * context.
+	 */
+	struct work_struct complex_work;
+
 	/* Link to free or reassembly list */
 	struct list_head list;
 
maintainer
#133 lore raw
Quoted 98 lines · Stefan Metzmacher

Hi,

I already used these debugging features:

CONFIG_KASAN_SHADOW_OFFSET=0xdffffc0000000000
CONFIG_HAVE_ARCH_KASAN=y
CONFIG_HAVE_ARCH_KASAN_VMALLOC=y
CONFIG_CC_HAS_KASAN_GENERIC=y
CONFIG_KASAN=y
CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX=y
CONFIG_KASAN_GENERIC=y
CONFIG_KASAN_INLINE=y
CONFIG_KASAN_STACK=y
CONFIG_KASAN_VMALLOC=y
CONFIG_KASAN_EXTRA_INFO=y

But now I turned on a lot more debugging features:

CONFIG_PRINTK_CALLER=y
CONFIG_STACKTRACE_BUILD_ID=y
CONFIG_DEBUG_INFO_COMPRESSED_ZSTD=y
CONFIG_DEBUG_SECTION_MISMATCH=y
CONFIG_MAGIC_SYSRQ_DEFAULT_ENABLE=0xffffffff
CONFIG_PAGE_EXTENSION=y
CONFIG_PAGE_OWNER=y
CONFIG_PAGE_TABLE_CHECK=y
CONFIG_DEBUG_PAGE_REF=y
CONFIG_PTDUMP_DEBUGFS=y
CONFIG_DEBUG_KMEMLEAK=y
CONFIG_DEBUG_KMEMLEAK_MEM_POOL_SIZE=16000
CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF=y
CONFIG_DEBUG_OBJECTS=y
CONFIG_DEBUG_OBJECTS_FREE=y
CONFIG_DEBUG_OBJECTS_TIMERS=y
CONFIG_DEBUG_OBJECTS_WORK=y
CONFIG_DEBUG_OBJECTS_RCU_HEAD=y
CONFIG_DEBUG_OBJECTS_PERCPU_COUNTER=y
CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT=0
CONFIG_WQ_WATCHDOG=y
CONFIG_PROVE_LOCKING=y
CONFIG_PROVE_RAW_LOCK_NESTING=y
CONFIG_LOCK_STAT=y
CONFIG_DEBUG_RT_MUTEXES=y
CONFIG_DEBUG_SPINLOCK=y
CONFIG_DEBUG_MUTEXES=y
CONFIG_DEBUG_WW_MUTEX_SLOWPATH=y
CONFIG_DEBUG_RWSEMS=y
CONFIG_DEBUG_LOCK_ALLOC=y
CONFIG_LOCKDEP=y
CONFIG_LOCKDEP_BITS=15
CONFIG_LOCKDEP_CHAINS_BITS=16
CONFIG_LOCKDEP_STACK_TRACE_BITS=19
CONFIG_LOCKDEP_STACK_TRACE_HASH_BITS=14
CONFIG_LOCKDEP_CIRCULAR_QUEUE_BITS=12
CONFIG_DEBUG_ATOMIC_SLEEP=y
CONFIG_TRACE_IRQFLAGS=y
CONFIG_TRACE_IRQFLAGS_NMI=y
CONFIG_DEBUG_IRQFLAGS=y
CONFIG_PROVE_RCU=y
CONFIG_RCU_TRACE=y
CONFIG_RING_BUFFER_ALLOW_SWAP=y
CONFIG_PREEMPTIRQ_TRACEPOINTS=y
CONFIG_TRACE_PREEMPT_TOGGLE=y
CONFIG_IRQSOFF_TRACER=y
CONFIG_PREEMPT_TRACER=y
CONFIG_TRACER_SNAPSHOT_PER_CPU_SWAP=y

And that luckily revealed some cases where
I called functions which might sleep in a
ib_post_recv completion function that
is called in a softirq as the client uses
IB_POLL_SOFTIRQ.

I means that I needed to deferr some of the
smbdirect_connect_negotiate_recv_done and
smbdirect_accept_negotiate_recv_done logic
to the workqueue.

And I added some includes to smbdirect_socket.h,
as some sparc cross compile bot was complaining
about a failing build.

I also did a few minor changes like using
sc->ib.dev consistently.

As well as setting sc->first_error as early as possible.

I took the current ksmbd-for-next-next at commit 2bd117c715790585d69a06bd5f795520fc67ce86 and rebased it on v6.18-rc4 and then removed my patches and added the modified once on top.

The result can be found in my for-6.19/fs-smb-20251104-v3 branch, at commit a73e9e7470238121b87b2128da2ed1fff9fd6d4b:
git fetch https://git.samba.org/metze/linux/wip.git for-6.19/fs-smb-20251104-v3
https://git.samba.org/?p=metze/linux/wip.git;a=shortlog;h=refs/heads/for-6.19/fs-smb-20251104-v3

Quoted 4 lines

The whole diff between the old and new patchset
follows below:

The new branch should replace ksmbd-for-next-next...

I will replace them with new ones.
Thanks!

Quoted 757 lines

Thanks!
metze

  fs/smb/common/smbdirect/smbdirect_accept.c     | 154 +++++++++++++++-------
  fs/smb/common/smbdirect/smbdirect_connect.c    | 171 +++++++++++++++++--------
  fs/smb/common/smbdirect/smbdirect_connection.c |  91 +++++++++++--
  fs/smb/common/smbdirect/smbdirect_socket.h     |  15 +++
  4 files changed, 316 insertions(+), 115 deletions(-)
fs/smb/common/smbdirect/smbdirect_accept.c +106 -48
index 0ed3f43a7397..3455e7327663 100644
@@ -192,25 +192,14 @@ static int smbdirect_accept_init_params(struct smbdirect_socket *sc)
        return 0;
  }
 
+static void smbdirect_accept_negotiate_recv_work(struct work_struct *work);
+
  static void smbdirect_accept_negotiate_recv_done(struct ib_cq *cq, struct ib_wc *wc)
  {
        struct smbdirect_recv_io *recv_io =
                container_of(wc->wr_cqe, struct smbdirect_recv_io, cqe);
        struct smbdirect_socket *sc = recv_io->socket;
-       struct smbdirect_socket_parameters *sp = &sc->parameters;
-       struct smbdirect_negotiate_req *nreq;
-       u16 min_version;
-       u16 max_version;
-       u16 credits_requested;
-       u32 preferred_send_size;
-       u32 max_receive_size;
-       u32 max_fragmented_size;
-       struct smbdirect_send_io *send_io = NULL;
-       struct smbdirect_negotiate_resp *nrep;
-       struct ib_send_wr send_wr;
-       u32 ntstatus;
-       int posted;
-       int ret;
+       const struct smbdirect_socket_parameters *sp = &sc->parameters;
 
        if (unlikely(wc->status != IB_WC_SUCCESS || WARN_ON_ONCE(wc->opcode != IB_WC_RECV))) {
                if (wc->status != IB_WC_WR_FLUSH_ERR)
@@ -235,18 +224,98 @@ static void smbdirect_accept_negotiate_recv_done(struct ib_cq *cq, struct ib_wc
        mod_delayed_work(sc->workqueue, &sc->idle.timer_work,
                         msecs_to_jiffies(sp->keepalive_interval_msec));
 
-       ib_dma_sync_single_for_cpu(wc->qp->device,
+       ib_dma_sync_single_for_cpu(sc->ib.dev,
                                   recv_io->sge.addr,
                                   recv_io->sge.length,
                                   DMA_FROM_DEVICE);
 
-       if (wc->byte_len < sizeof(*nreq)) {
+       if (wc->byte_len < sizeof(struct smbdirect_negotiate_req)) {
                smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
                        "wc->byte_len=%u < %zu\n",
-                       wc->byte_len, sizeof(*nreq));
+                       wc->byte_len, sizeof(struct smbdirect_negotiate_req));
                goto error;
        }
 
+       /*
+        * We continue via the workqueue as we may have
+        * complex work that might sleep.
+        *
+        * The work should already/still be disabled,
+        * but smbdirect_connection_put_recv_io() disables
+        * it again.
+        *
+        * Note that smbdirect_connection_put_recv_io()
+        * only moved recv_io into the free list, but
+        * we didn't call smbdirect_connection_recv_io_refill()
+        * yet, so it won't be reused, but the cleanup code
+        * on disconnect is able to find it, disables
+        * recv_io->complex_work again.
+        */
+       smbdirect_connection_put_recv_io(recv_io);
+       INIT_WORK(&recv_io->complex_work, smbdirect_accept_negotiate_recv_work);
+       queue_work(sc->workqueue, &recv_io->complex_work);
+       return;
+
+error:
+       /*
+        * recv_io.posted.refill_work is still disabled,
+        * so smbdirect_connection_put_recv_io() won't
+        * start it.
+        */
+       smbdirect_connection_put_recv_io(recv_io);
+       smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
+}
+
+static void smbdirect_accept_negotiate_recv_work(struct work_struct *work)
+{
+       struct smbdirect_recv_io *recv_io =
+               container_of(work, struct smbdirect_recv_io, complex_work);
+       struct smbdirect_socket *sc = recv_io->socket;
+       struct smbdirect_socket_parameters *sp = &sc->parameters;
+       struct smbdirect_negotiate_req *nreq;
+       u16 min_version;
+       u16 max_version;
+       u16 credits_requested;
+       u32 preferred_send_size;
+       u32 max_receive_size;
+       u32 max_fragmented_size;
+       struct smbdirect_send_io *send_io = NULL;
+       struct smbdirect_negotiate_resp *nrep;
+       struct ib_send_wr send_wr;
+       u32 ntstatus;
+       int posted;
+       int ret;
+
+       /*
+        * make sure we won't start again...
+        */
+       disable_work(work);
+
+       /*
+        * Note recv_io is already part of the free list,
+        * as smbdirect_connect_negotiate_recv_done() called
+        * smbdirect_connection_put_recv_io(), but
+        * it won't be reused before we call
+        * smbdirect_connection_recv_io_refill() below.
+        */
+
+       if (unlikely(sc->first_error))
+               return;
+
+       if (sc->status != SMBDIRECT_SOCKET_NEGOTIATE_RUNNING) {
+               /*
+                * Something went wrong...
+                */
+               smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
+                       "status=%s first_error=%1pe local: %pISpsfc remote: %pISpsfc\n",
+                       smbdirect_socket_status_string(sc->status),
+                       SMBDIRECT_DEBUG_ERR_PTR(sc->first_error),
+                       &sc->rdma.cm_id->route.addr.src_addr,
+                       &sc->rdma.cm_id->route.addr.dst_addr);
+               smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
+               return;
+       }
+
        nreq = (struct smbdirect_negotiate_req *)recv_io->packet;
        min_version = le16_to_cpu(nreq->min_version);
        max_version = le16_to_cpu(nreq->max_version);
@@ -281,7 +350,8 @@ static void smbdirect_accept_negotiate_recv_done(struct ib_cq *cq, struct ib_wc
        if (credits_requested == 0) {
                smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
                        "invalid: credits_requested == 0\n");
-               goto error;
+               smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
+               return;
        }
 
        if (max_receive_size < SMBDIRECT_MIN_RECEIVE_SIZE) {
@@ -289,7 +359,8 @@ static void smbdirect_accept_negotiate_recv_done(struct ib_cq *cq, struct ib_wc
                        "invalid: max_receive_size=%u < %u\n",
                        max_receive_size,
                        SMBDIRECT_MIN_RECEIVE_SIZE);
-               goto error;
+               smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
+               return;
        }
 
        if (max_fragmented_size < SMBDIRECT_MIN_FRAGMENTED_SIZE) {
@@ -297,7 +368,8 @@ static void smbdirect_accept_negotiate_recv_done(struct ib_cq *cq, struct ib_wc
                        "invalid: max_fragmented_size=%u < %u\n",
                        max_fragmented_size,
                        SMBDIRECT_MIN_FRAGMENTED_SIZE);
-               goto error;
+               smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
+               return;
        }
 
        /*
@@ -327,12 +399,6 @@ static void smbdirect_accept_negotiate_recv_done(struct ib_cq *cq, struct ib_wc
         */
        sp->max_fragmented_send_size = max_fragmented_size;
 
-       /*
-        * Give recv_io back to the recv_io.free.list,
-        * so that the refill can also post it.
-        */
-       smbdirect_connection_put_recv_io(recv_io);
-
        /*
         * Prepare for receiving data_transfer messages
         */
@@ -350,7 +416,8 @@ static void smbdirect_accept_negotiate_recv_done(struct ib_cq *cq, struct ib_wc
                smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
                        "smbdirect_connection_recv_io_refill() failed %1pe\n",
                        SMBDIRECT_DEBUG_ERR_PTR(posted));
-               goto error;
+               smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
+               return;
        }
 
        /*
@@ -365,11 +432,11 @@ static void smbdirect_accept_negotiate_recv_done(struct ib_cq *cq, struct ib_wc
        send_io = smbdirect_connection_alloc_send_io(sc);
        if (IS_ERR(send_io)) {
                ret = PTR_ERR(send_io);
-               send_io = NULL;
                smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
                        "smbdirect_connection_alloc_send_io() failed %1pe\n",
                        SMBDIRECT_DEBUG_ERR_PTR(ret));
-               goto error;
+               smbdirect_connection_schedule_disconnect(sc, ret);
+               return;
        }
        send_io->cqe.done = smbdirect_accept_negotiate_send_done;
 
@@ -430,7 +497,9 @@ static void smbdirect_accept_negotiate_recv_done(struct ib_cq *cq, struct ib_wc
                smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
                        "ib_dma_mapping_error() failed %1pe\n",
                        SMBDIRECT_DEBUG_ERR_PTR(ret));
-               goto error;
+               smbdirect_connection_free_send_io(send_io);
+               smbdirect_connection_schedule_disconnect(sc, ret);
+               return;
        }
 
        send_io->sge[0].length = sizeof(*nrep);
@@ -455,30 +524,19 @@ static void smbdirect_accept_negotiate_recv_done(struct ib_cq *cq, struct ib_wc
                smbdirect_log_rdma_send(sc, SMBDIRECT_LOG_ERR,
                        "smbdirect_connection_post_send_wr() failed %1pe\n",
                        SMBDIRECT_DEBUG_ERR_PTR(ret));
-               goto error;
+               /*
+                * Note smbdirect_connection_free_send_io()
+                * does ib_dma_unmap_page()
+                */
+               smbdirect_connection_free_send_io(send_io);
+               smbdirect_connection_schedule_disconnect(sc, ret);
+               return;
        }
 
        /*
         * smbdirect_accept_negotiate_send_done
         * will do all remaining work...
         */
-       return;
-
-error:
-       /*
-        * recv_io.posted.refill_work is still disabled,
-        * so smbdirect_connection_put_recv_io() won't
-        * start it.
-        */
-       if (recv_io)
-               smbdirect_connection_put_recv_io(recv_io);
-       /*
-        * Note smbdirect_connection_free_send_io()
-        * does ib_dma_unmap_page()
-        */
-       if (send_io)
-               smbdirect_connection_free_send_io(send_io);
-       smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
  }
 
  static void smbdirect_accept_negotiate_send_done(struct ib_cq *cq, struct ib_wc *wc)
fs/smb/common/smbdirect/smbdirect_connect.c +116 -55
index eb8e903c9fce..b9544a6ef59b 100644
@@ -536,23 +536,14 @@ static void smbdirect_connect_negotiate_send_done(struct ib_cq *cq, struct ib_wc
        }
  }
 
+static void smbdirect_connect_negotiate_recv_work(struct work_struct *work);
+
  static void smbdirect_connect_negotiate_recv_done(struct ib_cq *cq, struct ib_wc *wc)
  {
        struct smbdirect_recv_io *recv_io =
                container_of(wc->wr_cqe, struct smbdirect_recv_io, cqe);
        struct smbdirect_socket *sc = recv_io->socket;
-       struct smbdirect_socket_parameters *sp = &sc->parameters;
-       struct smbdirect_negotiate_resp *nrep;
-       u16 negotiated_version;
-       u16 credits_requested;
-       u16 credits_granted;
-       u32 status;
-       u32 max_readwrite_size;
-       u32 preferred_send_size;
-       u32 max_receive_size;
-       u32 max_fragmented_size;
-       int posted;
-       int ret;
+       const struct smbdirect_socket_parameters *sp = &sc->parameters;
 
        if (unlikely(wc->status != IB_WC_SUCCESS || WARN_ON_ONCE(wc->opcode != IB_WC_RECV))) {
                if (wc->status != IB_WC_WR_FLUSH_ERR)
@@ -574,18 +565,96 @@ static void smbdirect_connect_negotiate_recv_done(struct ib_cq *cq, struct ib_wc
        mod_delayed_work(sc->workqueue, &sc->idle.timer_work,
                         msecs_to_jiffies(sp->keepalive_interval_msec));
 
-       ib_dma_sync_single_for_cpu(wc->qp->device,
+       ib_dma_sync_single_for_cpu(sc->ib.dev,
                                   recv_io->sge.addr,
                                   recv_io->sge.length,
                                   DMA_FROM_DEVICE);
 
-       if (wc->byte_len < sizeof(*nrep)) {
+       if (wc->byte_len < sizeof(struct smbdirect_negotiate_resp)) {
                smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
                        "wc->byte_len=%u < %zu\n",
-                       wc->byte_len, sizeof(*nrep));
+                       wc->byte_len, sizeof(struct smbdirect_negotiate_resp));
                goto error;
        }
 
+       /*
+        * We continue via the workqueue as we may have
+        * complex work that might sleep.
+        *
+        * The work should already/still be disabled,
+        * but smbdirect_connection_put_recv_io() disables
+        * it again.
+        *
+        * Note that smbdirect_connection_put_recv_io()
+        * only moved recv_io into the free list, but
+        * we didn't call smbdirect_connection_recv_io_refill()
+        * yet, so it won't be reused, but the cleanup code
+        * on disconnect is able to find it, disables
+        * recv_io->complex_work again.
+        */
+       smbdirect_connection_put_recv_io(recv_io);
+       INIT_WORK(&recv_io->complex_work, smbdirect_connect_negotiate_recv_work);
+       queue_work(sc->workqueue, &recv_io->complex_work);
+       return;
+
+error:
+       /*
+        * recv_io.posted.refill_work is still disabled,
+        * so smbdirect_connection_put_recv_io() won't
+        * start it.
+        */
+       smbdirect_connection_put_recv_io(recv_io);
+       smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
+}
+
+static void smbdirect_connect_negotiate_recv_work(struct work_struct *work)
+{
+       struct smbdirect_recv_io *recv_io =
+               container_of(work, struct smbdirect_recv_io, complex_work);
+       struct smbdirect_socket *sc = recv_io->socket;
+       struct smbdirect_socket_parameters *sp = &sc->parameters;
+       struct smbdirect_negotiate_resp *nrep;
+       u16 negotiated_version;
+       u16 credits_requested;
+       u16 credits_granted;
+       u32 status;
+       u32 max_readwrite_size;
+       u32 preferred_send_size;
+       u32 max_receive_size;
+       u32 max_fragmented_size;
+       int posted;
+       int ret;
+
+       /*
+        * make sure we won't start again...
+        */
+       disable_work(work);
+
+       /*
+        * Note recv_io is already part of the free list,
+        * as smbdirect_connect_negotiate_recv_done() called
+        * smbdirect_connection_put_recv_io(), but
+        * it won't be reused before we call
+        * smbdirect_connection_recv_io_refill() below.
+        */
+
+       if (unlikely(sc->first_error))
+               return;
+
+       if (sc->status != SMBDIRECT_SOCKET_NEGOTIATE_RUNNING) {
+               /*
+                * Something went wrong...
+                */
+               smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
+                       "status=%s first_error=%1pe local: %pISpsfc remote: %pISpsfc\n",
+                       smbdirect_socket_status_string(sc->status),
+                       SMBDIRECT_DEBUG_ERR_PTR(sc->first_error),
+                       &sc->rdma.cm_id->route.addr.src_addr,
+                       &sc->rdma.cm_id->route.addr.dst_addr);
+               smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
+               return;
+       }
+
        nrep = (struct smbdirect_negotiate_resp *)recv_io->packet;
        negotiated_version = le16_to_cpu(nrep->negotiated_version);
        credits_requested = le16_to_cpu(nrep->credits_requested);
@@ -623,7 +692,16 @@ static void smbdirect_connect_negotiate_recv_done(struct ib_cq *cq, struct ib_wc
                smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
                        "invalid: negotiated_version=0x%x\n",
                        negotiated_version);
-               goto error;
+               smbdirect_connection_schedule_disconnect(sc, -ECONNREFUSED);
+               return;
+       }
+
+       if (status != 0) {
+               smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
+                       "invalid: status=0x%x != 0x0\n",
+                       status);
+               smbdirect_connection_schedule_disconnect(sc, -ECONNREFUSED);
+               return;
        }
 
        if (max_receive_size < SMBDIRECT_MIN_RECEIVE_SIZE) {
@@ -631,7 +709,8 @@ static void smbdirect_connect_negotiate_recv_done(struct ib_cq *cq, struct ib_wc
                        "invalid: max_receive_size=%u < %u\n",
                        max_receive_size,
                        SMBDIRECT_MIN_RECEIVE_SIZE);
-               goto error;
+               smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
+               return;
        }
 
        if (max_fragmented_size < SMBDIRECT_MIN_FRAGMENTED_SIZE) {
@@ -639,19 +718,22 @@ static void smbdirect_connect_negotiate_recv_done(struct ib_cq *cq, struct ib_wc
                        "invalid: max_fragmented_size=%u < %u\n",
                        max_fragmented_size,
                        SMBDIRECT_MIN_FRAGMENTED_SIZE);
-               goto error;
+               smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
+               return;
        }
 
        if (credits_granted == 0) {
                smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
                        "invalid: credits_granted == 0\n");
-               goto error;
+               smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
+               return;
        }
 
        if (credits_requested == 0) {
                smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
                        "invalid: credits_requested == 0\n");
-               goto error;
+               smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
+               return;
        }
 
        if (preferred_send_size > sp->max_recv_size) {
@@ -659,14 +741,8 @@ static void smbdirect_connect_negotiate_recv_done(struct ib_cq *cq, struct ib_wc
                        "invalid: preferred_send_size=%u < max_recv_size=%u\n",
                        preferred_send_size,
                        sp->max_recv_size);
-               goto error;
-       }
-
-       if (status != 0) {
-               smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
-                       "invalid: status=0x%x != 0x0\n",
-                       status);
-               goto error;
+               smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
+               return;
        }
 
        /*
@@ -701,7 +777,8 @@ static void smbdirect_connect_negotiate_recv_done(struct ib_cq *cq, struct ib_wc
                        "invalid: max_readwrite_size=%u < PAGE_SIZE(%lu)\n",
                        max_readwrite_size,
                        PAGE_SIZE);
-               goto error;
+               smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
+               return;
        }
        sp->max_frmr_depth = sp->max_read_write_size / PAGE_SIZE;
 
@@ -716,11 +793,14 @@ static void smbdirect_connect_negotiate_recv_done(struct ib_cq *cq, struct ib_wc
         */
        sp->max_fragmented_send_size = max_fragmented_size;
 
-       /*
-        * Give recv_io back to the recv_io.free.list,
-        * so that the refill can also post it.
-        */
-       smbdirect_connection_put_recv_io(recv_io);
+       ret = smbdirect_connection_create_mr_list(sc);
+       if (ret) {
+               smbdirect_log_rdma_mr(sc, SMBDIRECT_LOG_ERR,
+                       "smbdirect_connection_create_mr_list() failed %1pe\n",
+                       SMBDIRECT_DEBUG_ERR_PTR(ret));
+               smbdirect_connection_schedule_disconnect(sc, ret);
+               return;
+       }
 
        /*
         * Prepare for receiving data_transfer messages
@@ -731,14 +811,6 @@ static void smbdirect_connect_negotiate_recv_done(struct ib_cq *cq, struct ib_wc
                recv_io->cqe.done = smbdirect_connection_recv_io_done;
        recv_io = NULL;
 
-       ret = smbdirect_connection_create_mr_list(sc);
-       if (ret) {
-               smbdirect_log_rdma_mr(sc, SMBDIRECT_LOG_ERR,
-                       "smbdirect_connection_create_mr_list() failed %1pe\n",
-                       SMBDIRECT_DEBUG_ERR_PTR(ret));
-               goto error;
-       }
-
        /*
         * We should at least post 1 smbdirect_recv_io!
         */
@@ -747,7 +819,8 @@ static void smbdirect_connect_negotiate_recv_done(struct ib_cq *cq, struct ib_wc
                smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
                        "smbdirect_connection_recv_io_refill() failed %1pe\n",
                        SMBDIRECT_DEBUG_ERR_PTR(ret));
-               goto error;
+               smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
+               return;
        }
 
        /*
@@ -756,18 +829,6 @@ static void smbdirect_connect_negotiate_recv_done(struct ib_cq *cq, struct ib_wc
         * the waiter.
         */
        smbdirect_connection_negotiation_done(sc);
-
-       return;
-
-error:
-       /*
-        * recv_io.posted.refill_work is still disabled,
-        * so smbdirect_connection_put_recv_io() won't
-        * start it.
-        */
-       if (recv_io)
-               smbdirect_connection_put_recv_io(recv_io);
-       smbdirect_connection_schedule_disconnect(sc, -ECONNABORTED);
  }
 
  __SMBDIRECT_PUBLIC__
fs/smb/common/smbdirect/smbdirect_connection.c +79 -12
index ae9626888b5c..cc22e8b2f4d3 100644
@@ -742,6 +742,8 @@ int smbdirect_connection_create_mem_pools(struct smbdirect_socket *sc)
                        goto err;
                recv_io->socket = sc;
                recv_io->sge.length = 0;
+               INIT_WORK(&recv_io->complex_work, __smbdirect_socket_disabled_work);
+               disable_work_sync(&recv_io->complex_work);
                list_add_tail(&recv_io->list, &sc->recv_io.free.list);
        }
 
@@ -757,6 +759,10 @@ void smbdirect_connection_destroy_mem_pools(struct smbdirect_socket *sc)
        struct smbdirect_recv_io *recv_io, *next_io;
 
        list_for_each_entry_safe(recv_io, next_io, &sc->recv_io.free.list, list) {
+               /*
+                * The work should already be disabled
+                */
+               disable_work_sync(&recv_io->complex_work);
                list_del(&recv_io->list);
                mempool_free(recv_io, sc->recv_io.mem.pool);
        }
@@ -833,9 +839,10 @@ struct smbdirect_recv_io *smbdirect_connection_get_recv_io(struct smbdirect_sock
        unsigned long flags;
 
        spin_lock_irqsave(&sc->recv_io.free.lock, flags);
-       msg = list_first_entry_or_null(&sc->recv_io.free.list,
-                                      struct smbdirect_recv_io,
-                                      list);
+       if (likely(!sc->first_error))
+               msg = list_first_entry_or_null(&sc->recv_io.free.list,
+                                              struct smbdirect_recv_io,
+                                              list);
        if (likely(msg)) {
                list_del(&msg->list);
                sc->statistics.get_receive_buffer++;
@@ -851,6 +858,11 @@ void smbdirect_connection_put_recv_io(struct smbdirect_recv_io *msg)
        struct smbdirect_socket *sc = msg->socket;
        unsigned long flags;
 
+       /*
+        * Should already be disabled anyway.
+        */
+       disable_work(&msg->complex_work);
+
        if (likely(msg->sge.length != 0)) {
                ib_dma_unmap_single(sc->ib.dev,
                                    msg->sge.addr,
@@ -874,6 +886,11 @@ static void smbdirect_connection_reassembly_append_recv_io(struct smbdirect_sock
  {
        unsigned long flags;
 
+       /*
+        * The work should already/still be disabled
+        */
+       disable_work(&msg->complex_work);
+
        spin_lock_irqsave(&sc->recv_io.reassembly.lock, flags);
        list_add_tail(&msg->list, &sc->recv_io.reassembly.list);
        sc->recv_io.reassembly.queue_length++;
@@ -968,6 +985,14 @@ __SMBDIRECT_PRIVATE__
  void smbdirect_connection_schedule_disconnect(struct smbdirect_socket *sc,
                                              int error)
  {
+       struct smbdirect_recv_io *recv_io, *recv_tmp;
+       unsigned long flags;
+
+       if (sc->first_error == 0)
+               sc->first_error = error;
+       if (sc->first_error == 0)
+               sc->first_error = -ECONNABORTED;
+
        /*
         * make sure other work (than disconnect_work)
         * is not queued again but here we don't block and avoid
@@ -979,10 +1004,19 @@ void smbdirect_connection_schedule_disconnect(struct smbdirect_socket *sc,
        sc->idle.keepalive = SMBDIRECT_KEEPALIVE_NONE;
        disable_delayed_work(&sc->idle.timer_work);
 
-       if (sc->first_error == 0)
-               sc->first_error = error;
-       if (sc->first_error == 0)
-               sc->first_error = -ECONNABORTED;
+       /*
+        * If any complex work was scheduled we
+        * should disable it (only happens during
+        * negotiation)...
+        *
+        * Note that sc->first_error is set before,
+        * so any future smbdirect_connection_get_recv_io()
+        * will see it and return NULL.
+        */
+       spin_lock_irqsave(&sc->recv_io.free.lock, flags);
+       list_for_each_entry_safe(recv_io, recv_tmp, &sc->recv_io.free.list, list)
+               disable_work(&recv_io->complex_work);
+       spin_unlock_irqrestore(&sc->recv_io.free.lock, flags);
 
        switch (sc->status) {
        case SMBDIRECT_SOCKET_RESOLVE_ADDR_FAILED:
@@ -1037,12 +1071,17 @@ static void smbdirect_connection_disconnect_work(struct work_struct *work)
  {
        struct smbdirect_socket *sc =
                container_of(work, struct smbdirect_socket, disconnect_work);
+       struct smbdirect_recv_io *recv_io, *recv_tmp;
+       unsigned long flags;
 
        /*
         * This should not never be called in an interrupt!
         */
        WARN_ON_ONCE(in_interrupt());
 
+       if (sc->first_error == 0)
+               sc->first_error = -ECONNABORTED;
+
        /*
         * make sure this and other work is not queued again
         * but here we don't block and avoid
@@ -1055,8 +1094,19 @@ static void smbdirect_connection_disconnect_work(struct work_struct *work)
        sc->idle.keepalive = SMBDIRECT_KEEPALIVE_NONE;
        disable_delayed_work(&sc->idle.timer_work);
 
-       if (sc->first_error == 0)
-               sc->first_error = -ECONNABORTED;
+       /*
+        * If any complex work was scheduled we
+        * should disable it (only happens during
+        * negotiation)...
+        *
+        * Note that sc->first_error is set before,
+        * so any future smbdirect_connection_get_recv_io()
+        * will see it and return NULL.
+        */
+       spin_lock_irqsave(&sc->recv_io.free.lock, flags);
+       list_for_each_entry_safe(recv_io, recv_tmp, &sc->recv_io.free.list, list)
+               disable_work(&recv_io->complex_work);
+       spin_unlock_irqrestore(&sc->recv_io.free.lock, flags);
 
        switch (sc->status) {
        case SMBDIRECT_SOCKET_NEGOTIATE_NEEDED:
@@ -1216,6 +1266,24 @@ static void smbdirect_connection_destroy(struct smbdirect_socket *sc)
        disable_work_sync(&sc->idle.immediate_work);
        disable_delayed_work_sync(&sc->idle.timer_work);
 
+       /*
+        * If any complex work was scheduled we
+        * should disable it (only happens during
+        * negotiation)...
+        *
+        * Note was already set in sc->first_error in
+        * smbdirect_connection_schedule_disconnect() or
+        * smbdirect_connection_disconnect_work(), both
+        * before time before:
+        * spin_lock_irqsave(&sc->recv_io.free.lock, flags),
+        * so any future smbdirect_connection_get_recv_io()
+        * will see it and return NULL. And we don't
+        * need to get the lock here again, while
+        * trying disable_work_sync().
+        */
+       list_for_each_entry_safe(recv_io, recv_tmp, &sc->recv_io.free.list, list)
+               disable_work_sync(&recv_io->complex_work);
+
        if (sc->rdma.cm_id)
                rdma_lock_handler(sc->rdma.cm_id);
 
@@ -1231,9 +1299,8 @@ static void smbdirect_connection_destroy(struct smbdirect_socket *sc)
        spin_lock_irqsave(&sc->recv_io.reassembly.lock, flags);
        list_splice_tail_init(&sc->recv_io.reassembly.list, &all_list);
        spin_unlock_irqrestore(&sc->recv_io.reassembly.lock, flags);
-       list_for_each_entry_safe(recv_io, recv_tmp, &all_list, list) {
+       list_for_each_entry_safe(recv_io, recv_tmp, &all_list, list)
                smbdirect_connection_put_recv_io(recv_io);
-       }
        sc->recv_io.reassembly.data_length = 0;
 
        smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_INFO,
@@ -2002,7 +2069,7 @@ void smbdirect_connection_recv_io_done(struct ib_cq *cq, struct ib_wc *wc)
        mod_delayed_work(sc->workqueue, &sc->idle.timer_work,
                         msecs_to_jiffies(sp->keepalive_interval_msec));
 
-       ib_dma_sync_single_for_cpu(wc->qp->device,
+       ib_dma_sync_single_for_cpu(sc->ib.dev,
                                   recv_io->sge.addr,
                                   recv_io->sge.length,
                                   DMA_FROM_DEVICE);
fs/smb/common/smbdirect/smbdirect_socket.h +15 -0
index 65f25fc4b4a7..ef151ff1c02a 100644
@@ -6,6 +6,13 @@
  #ifndef __FS_SMB_COMMON_SMBDIRECT_SMBDIRECT_SOCKET_H__
  #define __FS_SMB_COMMON_SMBDIRECT_SMBDIRECT_SOCKET_H__
 
+#include <linux/wait.h>
+#include <linux/workqueue.h>
+#include <linux/kref.h>
+#include <linux/mempool.h>
+#include <linux/spinlock.h>
+#include <linux/mutex.h>
+#include <linux/completion.h>
  #include <rdma/rw.h>
 
  enum smbdirect_socket_status {
@@ -617,6 +624,14 @@ struct smbdirect_recv_io {
  #define SMBDIRECT_RECV_IO_MAX_SGE 1
        struct ib_sge sge;
 
+       /*
+        * We may need to handle complex
+        * work that might sleep and are
+        * not allowed to run in an interrupt
+        * context.
+        */
+       struct work_struct complex_work;
+
        /* Link to free or reassembly list */
        struct list_head list;