xref: /openbmc/linux/Documentation/infiniband/core_locking.rst (revision 4b4193256c8d3bc3a5397b5cd9494c2ad386317d)
197162a1eSMauro Carvalho Chehab===========================
297162a1eSMauro Carvalho ChehabInfiniBand Midlayer Locking
397162a1eSMauro Carvalho Chehab===========================
497162a1eSMauro Carvalho Chehab
597162a1eSMauro Carvalho Chehab  This guide is an attempt to make explicit the locking assumptions
697162a1eSMauro Carvalho Chehab  made by the InfiniBand midlayer.  It describes the requirements on
797162a1eSMauro Carvalho Chehab  both low-level drivers that sit below the midlayer and upper level
897162a1eSMauro Carvalho Chehab  protocols that use the midlayer.
997162a1eSMauro Carvalho Chehab
1097162a1eSMauro Carvalho ChehabSleeping and interrupt context
1197162a1eSMauro Carvalho Chehab==============================
1297162a1eSMauro Carvalho Chehab
1397162a1eSMauro Carvalho Chehab  With the following exceptions, a low-level driver implementation of
1497162a1eSMauro Carvalho Chehab  all of the methods in struct ib_device may sleep.  The exceptions
1597162a1eSMauro Carvalho Chehab  are any methods from the list:
1697162a1eSMauro Carvalho Chehab
1797162a1eSMauro Carvalho Chehab    - create_ah
1897162a1eSMauro Carvalho Chehab    - modify_ah
1997162a1eSMauro Carvalho Chehab    - query_ah
2097162a1eSMauro Carvalho Chehab    - destroy_ah
2197162a1eSMauro Carvalho Chehab    - post_send
2297162a1eSMauro Carvalho Chehab    - post_recv
2397162a1eSMauro Carvalho Chehab    - poll_cq
2497162a1eSMauro Carvalho Chehab    - req_notify_cq
2597162a1eSMauro Carvalho Chehab
2697162a1eSMauro Carvalho Chehab  which may not sleep and must be callable from any context.
2797162a1eSMauro Carvalho Chehab
2897162a1eSMauro Carvalho Chehab  The corresponding functions exported to upper level protocol
2997162a1eSMauro Carvalho Chehab  consumers:
3097162a1eSMauro Carvalho Chehab
31*bd0abfa8SGuoqing Jiang    - rdma_create_ah
32*bd0abfa8SGuoqing Jiang    - rdma_modify_ah
33*bd0abfa8SGuoqing Jiang    - rdma_query_ah
34*bd0abfa8SGuoqing Jiang    - rdma_destroy_ah
3597162a1eSMauro Carvalho Chehab    - ib_post_send
3697162a1eSMauro Carvalho Chehab    - ib_post_recv
3797162a1eSMauro Carvalho Chehab    - ib_req_notify_cq
3897162a1eSMauro Carvalho Chehab
3997162a1eSMauro Carvalho Chehab  are therefore safe to call from any context.
4097162a1eSMauro Carvalho Chehab
4197162a1eSMauro Carvalho Chehab  In addition, the function
4297162a1eSMauro Carvalho Chehab
4397162a1eSMauro Carvalho Chehab    - ib_dispatch_event
4497162a1eSMauro Carvalho Chehab
4597162a1eSMauro Carvalho Chehab  used by low-level drivers to dispatch asynchronous events through
4697162a1eSMauro Carvalho Chehab  the midlayer is also safe to call from any context.
4797162a1eSMauro Carvalho Chehab
4897162a1eSMauro Carvalho ChehabReentrancy
4997162a1eSMauro Carvalho Chehab----------
5097162a1eSMauro Carvalho Chehab
5197162a1eSMauro Carvalho Chehab  All of the methods in struct ib_device exported by a low-level
5297162a1eSMauro Carvalho Chehab  driver must be fully reentrant.  The low-level driver is required to
5397162a1eSMauro Carvalho Chehab  perform all synchronization necessary to maintain consistency, even
5497162a1eSMauro Carvalho Chehab  if multiple function calls using the same object are run
5597162a1eSMauro Carvalho Chehab  simultaneously.
5697162a1eSMauro Carvalho Chehab
5797162a1eSMauro Carvalho Chehab  The IB midlayer does not perform any serialization of function calls.
5897162a1eSMauro Carvalho Chehab
5997162a1eSMauro Carvalho Chehab  Because low-level drivers are reentrant, upper level protocol
6097162a1eSMauro Carvalho Chehab  consumers are not required to perform any serialization.  However,
6197162a1eSMauro Carvalho Chehab  some serialization may be required to get sensible results.  For
6297162a1eSMauro Carvalho Chehab  example, a consumer may safely call ib_poll_cq() on multiple CPUs
6397162a1eSMauro Carvalho Chehab  simultaneously.  However, the ordering of the work completion
6497162a1eSMauro Carvalho Chehab  information between different calls of ib_poll_cq() is not defined.
6597162a1eSMauro Carvalho Chehab
6697162a1eSMauro Carvalho ChehabCallbacks
6797162a1eSMauro Carvalho Chehab---------
6897162a1eSMauro Carvalho Chehab
6997162a1eSMauro Carvalho Chehab  A low-level driver must not perform a callback directly from the
7097162a1eSMauro Carvalho Chehab  same callchain as an ib_device method call.  For example, it is not
7197162a1eSMauro Carvalho Chehab  allowed for a low-level driver to call a consumer's completion event
7297162a1eSMauro Carvalho Chehab  handler directly from its post_send method.  Instead, the low-level
7397162a1eSMauro Carvalho Chehab  driver should defer this callback by, for example, scheduling a
7497162a1eSMauro Carvalho Chehab  tasklet to perform the callback.
7597162a1eSMauro Carvalho Chehab
7697162a1eSMauro Carvalho Chehab  The low-level driver is responsible for ensuring that multiple
7797162a1eSMauro Carvalho Chehab  completion event handlers for the same CQ are not called
7897162a1eSMauro Carvalho Chehab  simultaneously.  The driver must guarantee that only one CQ event
7997162a1eSMauro Carvalho Chehab  handler for a given CQ is running at a time.  In other words, the
8097162a1eSMauro Carvalho Chehab  following situation is not allowed::
8197162a1eSMauro Carvalho Chehab
8297162a1eSMauro Carvalho Chehab          CPU1                                    CPU2
8397162a1eSMauro Carvalho Chehab
8497162a1eSMauro Carvalho Chehab    low-level driver ->
8597162a1eSMauro Carvalho Chehab      consumer CQ event callback:
8697162a1eSMauro Carvalho Chehab        /* ... */
8797162a1eSMauro Carvalho Chehab        ib_req_notify_cq(cq, ...);
8897162a1eSMauro Carvalho Chehab                                          low-level driver ->
8997162a1eSMauro Carvalho Chehab        /* ... */                           consumer CQ event callback:
9097162a1eSMauro Carvalho Chehab                                              /* ... */
9197162a1eSMauro Carvalho Chehab        return from CQ event handler
9297162a1eSMauro Carvalho Chehab
9397162a1eSMauro Carvalho Chehab  The context in which completion event and asynchronous event
9497162a1eSMauro Carvalho Chehab  callbacks run is not defined.  Depending on the low-level driver, it
9597162a1eSMauro Carvalho Chehab  may be process context, softirq context, or interrupt context.
9697162a1eSMauro Carvalho Chehab  Upper level protocol consumers may not sleep in a callback.
9797162a1eSMauro Carvalho Chehab
9897162a1eSMauro Carvalho ChehabHot-plug
9997162a1eSMauro Carvalho Chehab--------
10097162a1eSMauro Carvalho Chehab
10197162a1eSMauro Carvalho Chehab  A low-level driver announces that a device is ready for use by
10297162a1eSMauro Carvalho Chehab  consumers when it calls ib_register_device(), all initialization
10397162a1eSMauro Carvalho Chehab  must be complete before this call.  The device must remain usable
10497162a1eSMauro Carvalho Chehab  until the driver's call to ib_unregister_device() has returned.
10597162a1eSMauro Carvalho Chehab
10697162a1eSMauro Carvalho Chehab  A low-level driver must call ib_register_device() and
10797162a1eSMauro Carvalho Chehab  ib_unregister_device() from process context.  It must not hold any
10897162a1eSMauro Carvalho Chehab  semaphores that could cause deadlock if a consumer calls back into
10997162a1eSMauro Carvalho Chehab  the driver across these calls.
11097162a1eSMauro Carvalho Chehab
11197162a1eSMauro Carvalho Chehab  An upper level protocol consumer may begin using an IB device as
11297162a1eSMauro Carvalho Chehab  soon as the add method of its struct ib_client is called for that
11397162a1eSMauro Carvalho Chehab  device.  A consumer must finish all cleanup and free all resources
11497162a1eSMauro Carvalho Chehab  relating to a device before returning from the remove method.
11597162a1eSMauro Carvalho Chehab
11697162a1eSMauro Carvalho Chehab  A consumer is permitted to sleep in its add and remove methods.
117