RDMA resource tracking
v5 cover RDMA resource tracking
Changelog:v4->v5:
* Rewrote logic to acquire kernel owners of PD and CQ objects
* Simplified interface.
* Added logic to return summary for all PID namespaces and not only init PID namespace.
* Changed DB implementation from struct with arrays to be struct with hash table.
* Moved to use SRCU per-hash table.
* Dropped RW semaphores in favor of mutex to protect linked lists.
* Dropped QP named annotation patch.
* Used SPDX licenses.v3->v4:
* Added Steve ROB tags.
* Updated restrack.h to be compatible with kernel-doc format
* Fixed bug when not all QPs (more than one netlink page) were returned by netlink interface
* Removed "MAX" values from object summary. In followup patches, I will eturn object summary per-namespace and not for init PID namespace only. Max values can be seen with ibv_devinfo tool.
* Renamed "comm" to be kern_name.
* Fix spelling errors.v2->v3:
* Added support of PID namespaces.
* Rewrote rdma_restraack_add function to ensure that it won't appear in lists before it is valid.
* Replace pid/task name caching logic to use task_struct instead.
* Removed report of name of task's for user space objects. Users are expected to read it through /proc/PID/comm.v1->v2:
* Rebased on latest rdma/for-next
* Replaced mutex lock which protect linked lists to be RW semaphore. It has no impact on current implementation, because there is only one reader (nldev) and it is serialized. However better to be prepared for multiple readers from the beginning.
* Added reinitialization next QP entry logic to ensure that it exists and didn't vanish during fill_req_qp() work.v0->v1:
* Dropped RFC
* Separated to thre series, one for for-rc, and two for-next.
-------
The original goal of this series was to allow ability to view connection (QP) information about running processes, however I used this opportunity and created common infrastructure to track and report various resources. The report part is implemented in netlink (nldev), but smart ULPs can now create advanced usage models based on device utilization.
The current implementation relies on one lock per-object per-device, so creation/destroying of various objects (CQ, PD, e.t.c) on various or the same devices doesn't interfere each with another.
The data protection is performed with SRCU and its reader-writer model ensures that resource won't be destroyed till readers will finish their work.
Possible future work will include:
* Reducing number of locks in RDMA, because of SRCU.
* Converting CMA to be based completely on resource tracking.
* Addition of other objects and extending current to give full and detailed state of the RDMA kernel stack.
* Replacing synchronize_srcu with call_srcu to make destroy flow non-blocking.
* Provide reliable device reset flow, preserving resource creation ordering.
Thanks
---------------------------------------
Leon Romanovsky (8):
RDMA/core: Print caller name instead of function name RDMA/core: Save kernel caller name in PD and CQ objects RDMA/restrack: Add general infrastructure to track RDMA resources RDMA/core: Add resource tracking for create and destroy QPs RDMA/core: Add resource tracking for create and destroy CQs RDMA/core: Add resource tracking for create and destroy PDs RDMA/nldev: Provide global resource utilization RDMA/nldev: Provide detailed QP information
drivers/infiniband/core/Makefile | 2 +- drivers/infiniband/core/core_priv.h | 21 ++ drivers/infiniband/core/cq.c | 16 +- drivers/infiniband/core/device.c | 7 + drivers/infiniband/core/nldev.c | 364 +++++++++++++++++++++++++++++ drivers/infiniband/core/restrack.c | 135 +++++++++++ drivers/infiniband/core/uverbs_cmd.c | 7 +- drivers/infiniband/core/uverbs_std_types.c | 3 + drivers/infiniband/core/verbs.c | 13 +- include/rdma/ib_verbs.h | 27 ++- include/rdma/restrack.h | 136 +++++++++++ include/uapi/rdma/rdma_netlink.h | 55 +++++ 12 files changed, 773 insertions(+), 13 deletions(-) create mode 100644 drivers/infiniband/core/restrack.c create mode 100644 include/rdma/restrack.h
--
2.15.1
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
A message that is not in the archive (sent off-list or not yet received): 20180122125119.26419-1-leon-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org
PATCH v5 1/8 RDMA/core: Print caller name instead of function name
From: Leon Romanovsky <leonro-VPRAkNaXOzVWk0Htik3J/[email protected]>
Reuse the name from the Kconfig to mark the caller.
--- include/rdma/ib_verbs.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/rdma/ib_verbs.h +1 -1
@@ -2858,7 +2858,7 @@ enum ib_pd_flags {struct ib_pd *__ib_alloc_pd (struct ib_device *device,unsigned int flags,const char *caller);#define ib_alloc_pd (device, flags) \ - __ib_alloc_pd((device), (flags), __func__) + __ib_alloc_pd((device), (flags), KBUILD_MODNAME)void ib_dealloc_pd (struct ib_pd *pd);/**
Signature · 6 lines
2.15.1 -- To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH v5 2/8 RDMA/core: Save kernel caller name in PD and CQ objects
In reply to a message not in the archiveFrom: Leon Romanovsky <leonro-VPRAkNaXOzVWk0Htik3J/[email protected]>
The KBUILD_MODNAME variable contains the module name and it is known for kernel users during compilation, so let's reuse it to track the owners.
--- drivers/infiniband/core/cq.c | 11 +++++++---- drivers/infiniband/core/verbs.c | 1 + include/rdma/ib_verbs.h | 13 +++++++++++-- 3 files changed, 19 insertions(+), 6 deletions(-)
drivers/infiniband/core/cq.c +7 -4
@@ -120,20 +120,22 @@ static void ib_cq_completion_workqueue(struct ib_cq *cq, void *private) }/** -* ib_alloc_cq - allocate a completion queue +* __ib_alloc_cq - allocate a completion queue * @dev: device to allocate the CQ for * @private: driver private data, accessible from cq->cq_context * @nr_cqe: number of CQEs to allocate * @comp_vector: HCA completion vectors for this CQ * @poll_ctx: context to poll the CQ from. +* @caller: module owner name. * * This is the proper interface to allocate a CQ for in-kernel users. A * CQ allocated with this interface will automatically be polled from the * specified context. The ULP must use wr->wr_cqe instead of wr->wr_id * to use this CQ abstraction. */ -struct ib_cq *ib_alloc_cq (struct ib_device *dev,void *private, -int nr_cqe,int comp_vector,enum ib_poll_context poll_ctx) +struct ib_cq *__ib_alloc_cq (struct ib_device *dev,void *private, +int nr_cqe,int comp_vector, +enum ib_poll_context poll_ctx,const char *caller) {struct ib_cq_init_attr cq_attr = { .cqe = nr_cqe,
@@ -151,6 +153,7 @@ struct ib_cq *ib_alloc_cq(struct ib_device *dev, void *private, cq->event_handler =NULL ; cq->cq_context = private; cq->poll_ctx = poll_ctx; + cq->caller = caller;atomic_set (&cq->usecnt,0 ); cq->wc =kmalloc_array (IB_POLL_BATCH ,sizeof (*cq->wc),GFP_KERNEL );
@@ -185,7 +188,7 @@ struct ib_cq *ib_alloc_cq(struct ib_device *dev, void *private, cq->device->destroy_cq (cq);return ERR_PTR (ret); } -EXPORT_SYMBOL (ib_alloc_cq); +EXPORT_SYMBOL (__ib_alloc_cq);/** * ib_free_cq - free a completion queue
drivers/infiniband/core/verbs.c +1 -0
@@ -252,6 +252,7 @@ struct ib_pd *__ib_alloc_pd(struct ib_device *device, unsigned int flags, pd->__internal_mr =NULL ;atomic_set (&pd->usecnt,0 ); pd->flags = flags; + pd->caller = caller;if (device->attrs.device_cap_flags &IB_DEVICE_LOCAL_DMA_LKEY ) pd->local_dma_lkey = device->local_dma_lkey;
include/rdma/ib_verbs.h +11 -2
@@ -1530,6 +1530,7 @@ struct ib_pd {* Implementation details of the RDMA core, don't use in drivers: */ struct ib_mr *__internal_mr; +const char *caller; };struct ib_xrcd {
@@ -1570,6 +1571,10 @@ struct ib_cq {struct irq_poll iop;struct work_struct work; }; +/* +* Implementation details of the RDMA core, don't use in drivers: +*/ +const char *caller; };struct ib_srq {
@@ -3135,8 +3140,12 @@ static inline int ib_post_recv(struct ib_qp *qp,return qp->device->post_recv (qp, recv_wr, bad_recv_wr); } -struct ib_cq *ib_alloc_cq (struct ib_device *dev,void *private, -int nr_cqe,int comp_vector,enum ib_poll_context poll_ctx); +struct ib_cq *__ib_alloc_cq (struct ib_device *dev,void *private, +int nr_cqe,int comp_vector, +enum ib_poll_context poll_ctx,const char *caller); +#define ib_alloc_cq (device, priv, nr_cqe, comp_vect, poll_ctx) \ + __ib_alloc_cq((device), (priv), (nr_cqe), (comp_vect), (poll_ctx), KBUILD_MODNAME) +void ib_free_cq (struct ib_cq *cq);int ib_process_cq_direct (struct ib_cq *cq,int budget);
Signature · 6 lines
2.15.1 -- To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH v5 3/8 RDMA/restrack: Add general infrastructure to track RDMA resources
In reply to a message not in the archiveFrom: Leon Romanovsky <leonro-VPRAkNaXOzVWk0Htik3J/[email protected]>
The RDMA subsystem has very strict set of objects to work on it, but it completely lacks tracking facilities and no visibility of resource utilization.
The following patch adds such infrastructure to keep track of RDMA resources to help with debugging of user space applications. The primary user of this infrastructure is RDMA nldev netlink (following patches), but it is not limited too.
At this stage, the main three objects (PD, CQ and QP) are added, and more will be added later.
--- drivers/infiniband/core/Makefile | 2 +- drivers/infiniband/core/core_priv.h | 1 + drivers/infiniband/core/device.c | 7 ++ drivers/infiniband/core/restrack.c | 135 +++++++++++++++++++++++++++++++++++ include/rdma/ib_verbs.h | 12 ++++ include/rdma/restrack.h | 136 ++++++++++++++++++++++++++++++++++++ 6 files changed, 292 insertions(+), 1 deletion(-) create mode 100644 drivers/infiniband/core/restrack.c create mode 100644 include/rdma/restrack.h
drivers/infiniband/core/Makefile +1 -1
@@ -12,7 +12,7 @@ ib_core-y := packer.o ud_header.o verbs.o cq.o rw.o sysfs.o \ device.o fmr_pool.o cache.o netlink.o \ roce_gid_mgmt.o mr_pool.o addr.o sa_query.o \ multicast.o mad.o smi.o agent.o mad_rmpp.o \ - security.o nldev.o + security.o nldev.o restrack.o ib_core-$(CONFIG_INFINIBAND_USER_MEM) += umem.o ib_core-$(CONFIG_INFINIBAND_ON_DEMAND_PAGING) += umem_odp.o
drivers/infiniband/core/core_priv.h +1 -0
@@ -40,6 +40,7 @@#include <rdma/ib_verbs.h> #include <rdma/opa_addr.h> #include <rdma/ib_mad.h> +#include <rdma/restrack.h> #include "mad_priv.h" /* Total number of ports combined across all struct ib_devices's */
drivers/infiniband/core/device.c +7 -0
@@ -263,6 +263,11 @@ struct ib_device *ib_alloc_device(size_t size)if (!device)return NULL ; +if (rdma_restrack_init (&device->res)) { +kfree (device); +return NULL ; + } + device->dev.class = &ib_class;device_initialize (&device->dev);
@@ -596,6 +601,8 @@ void ib_unregister_device(struct ib_device *device) }up_read (&lists_rwsem); +rdma_restrack_clean (&device->res); +ib_device_unregister_rdmacg (device);ib_device_unregister_sysfs (device);
drivers/infiniband/core/restrack.c new file +135 -0
135 lines not on this page · raw message
include/rdma/ib_verbs.h +12 -0
@@ -63,6 +63,7 @@#include <linux/uaccess.h> #include <linux/cgroup_rdma.h> #include <uapi/rdma/ib_user_verbs.h> +#include <rdma/restrack.h> #define IB_FW_VERSION_NAME_MAX ETHTOOL_FWVERS_LEN
@@ -1531,6 +1532,7 @@ struct ib_pd {*/ struct ib_mr *__internal_mr;const char *caller; +struct rdma_restrack_entry res; };struct ib_xrcd {
@@ -1575,6 +1577,7 @@ struct ib_cq {* Implementation details of the RDMA core, don't use in drivers: */ const char *caller; +struct rdma_restrack_entry res; };struct ib_srq {
@@ -1751,6 +1754,11 @@ struct ib_qp {struct ib_rwq_ind_table *rwq_ind_tbl;struct ib_qp_security *qp_sec;u8 port; + +/* +* Implementation details of the RDMA core, don't use in drivers: +*/ +struct rdma_restrack_entry res; };struct ib_mr {
@@ -2357,6 +2365,10 @@ struct ib_device {#endif u32 index; +/* +* Implementation details of the RDMA core, don't use in drivers +*/ +struct rdma_restrack_root res;/** * The following mandatory functions are used only at device
include/rdma/restrack.h new file +136 -0
136 lines not on this page · raw message
Signature · 6 lines
2.15.1 -- To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
A message that is not in the archive (sent off-list or not yet received): 20180122125119.26419-4-leon-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org
Re: [PATCH rdma-next v5 3/8] RDMA/restrack: Add general infrastructure to track RDMA resources
drivers/infiniband/core/restrack.c line 134
Quoted 10 lines · Leon Romanovsky
drivers/infiniband/core/restrack.c new file +10
-0+mutex_lock (&dev->res.mutex); +hash_del_rcu (&res->node); +mutex_unlock (&dev->res.mutex); + + res->valid = false; + +if (res->task) +put_task_struct (res->task); +synchronize_srcu (&dev->res.srcu); +}
This locking is wrong..
Nothing can invalidate internal elements of 'res' until synchronize_srcu() returns, otherwise it creates races.
Eg here:
+ key = srcu_read_lock(&device->res.srcu);
+ hash_for_each_possible_rcu(device->res.hash, res, node, RDMA_RESTRACK_QP) {
+ if (idx < start) {
+ idx++;
+ continue;
+ }
+
+ if ((rdma_is_kernel_res(res) &&
+ task_active_pid_ns(current) != &init_pid_ns) ||
+ (!rdma_is_kernel_res(res) &&
+ task_active_pid_ns(current) != task_active_pid_ns(res->task)))
Will use-after-put res->task
Didn't audit closely enough to tell if valid is OK or not. Why isn't the hash_del sufficient? Why does valid exist?
Jason
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
A message that is not in the archive (sent off-list or not yet received): 20180123175433.GI30670-uk2M96/[email protected]
Re: [PATCH rdma-next v5 3/8] RDMA/restrack: Add general infrastructure to track RDMA resources
Quoted 13 lines · Jason Gunthorpe
drivers/infiniband/core/restrack.c line 134
Quoted 10 lines · Leon Romanovsky
drivers/infiniband/core/restrack.c new file +10
-0+mutex_lock (&dev->res.mutex); +hash_del_rcu (&res->node); +mutex_unlock (&dev->res.mutex); + + res->valid = false; + +if (res->task) +put_task_struct (res->task); +synchronize_srcu (&dev->res.srcu); +}This locking is wrong..
Actually, just don't use RCU.
We can't afford to call synchronize_srcu in the destroy() path of every object and the netlink user is not critical enough to bother optimizing the read side.
Replace the rcu and the mutex with a simple rwlock.
Jason
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
A message that is not in the archive (sent off-list or not yet received): 20180123175848.GK30670-uk2M96/[email protected]
Re: [PATCH rdma-next v5 3/8] RDMA/restrack: Add general infrastructure to track RDMA resources
Quoted 15 lines · Jason Gunthorpe
Quoted 13 lines · Jason Gunthorpe
drivers/infiniband/core/restrack.c line 134
Quoted 10 lines · Leon Romanovsky
drivers/infiniband/core/restrack.c new file +10
-0+mutex_lock (&dev->res.mutex); +hash_del_rcu (&res->node); +mutex_unlock (&dev->res.mutex); + + res->valid = false; + +if (res->task) +put_task_struct (res->task); +synchronize_srcu (&dev->res.srcu); +}This locking is wrong..
Quoted 5 lines
Actually, just don't use RCU.
We can't afford to call
synchronize_srcuin thedestroy()path of every object and the netlink user is not critical enough to bother optimizing the read side.
In large systems with many objects, such rwlock will block easily destroy of the objects.
Quoted 2 lines
Replace the rcu and the mutex with a simple rwlock.
My initial proposal was SRCU per-res to protect data with rwlock per-type to protect lists.
Quoted 2 lines
Jason
A message that is not in the archive (sent off-list or not yet received): 20180123183552.GW1393-U/[email protected]
Re: [PATCH rdma-next v5 3/8] RDMA/restrack: Add general infrastructure to track RDMA resources
Quoted 4 lines · Leon Romanovsky
Quoted 1 line
Replace the rcu and the mutex with a simple rwlock.
My initial proposal was SRCU per-res to protect data with rwlock per-type to protect lists.
I don't think SRCU per-res helps here. (and the memory overhead would be shocking, IMHO).
And if the goal is to avoid starvation of destroy from the reader side I don't think it helps, as you can seriously delay synchronize_srcu() by hitting the read side regions to force grace period waits.
I think you need a bit more adventure:
// Read side - could be a rwlockmutex_lock(ibdev->resource_lock)
cur = list_head;
cur->readers++;mutex_unlock();
[.. do slow stuff, push into the netlink skb ...]
mutex_lock();
cur->readers--;
if (cur->readers == 0)
wakeup(ibdev->wq);
cur = cur->nextmutex_unlock();
// Destroy sidemutex_lock(ibdev->resource_lock)wait_event_cmd(ibdev->wq, obj->readers == 0, mutex_unlock(), mutex_lock()); list_del(..);mutex_unlock();
Which would lock each resource only while being used, and the destroy will wait only if it conflicts with the ongoing read.
Getting the linked list processing to work properly with this kind of locking is hard.. The manual wq linked to the lock seems like a reasonable compromise.
Jason
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH rdma-next v5 3/8] RDMA/restrack: Add general infrastructure to track RDMA resources
In reply to a message not in the archiveQuoted 31 lines · Jason Gunthorpe
drivers/infiniband/core/restrack.c line 134
Quoted 10 lines · Leon Romanovsky
drivers/infiniband/core/restrack.c new file +10
-0+mutex_lock (&dev->res.mutex); +hash_del_rcu (&res->node); +mutex_unlock (&dev->res.mutex); + + res->valid = false; + +if (res->task) +put_task_struct (res->task); +synchronize_srcu (&dev->res.srcu); +}This locking is wrong..
Nothing can invalidate internal elements of 'res' until
synchronize_srcu()returns, otherwise it creates races.Eg here:
(quoted diff) +11
-0+ key = srcu_read_lock(&device->res.srcu); + hash_for_each_possible_rcu(device->res.hash, res, node, RDMA_RESTRACK_QP) { + if (idx < start) { + idx++; + continue; + } + + if ((rdma_is_kernel_res(res) && + task_active_pid_ns(current) != &init_pid_ns) || + (!rdma_is_kernel_res(res) && + task_active_pid_ns(current) != task_active_pid_ns(res->task)))
Quoted 4 lines
Will use-after-put
res->taskDidn't audit closely enough to tell if valid is OK or not. Why isn't the
hash_delsufficient? Why does valid exist?
Because, there are mlx5 QPs which partially flows through core code to register and deregister. Such QPs were created "manually" but destroyed with ib_destroy_qp.
Quoted 6 lines
Jason
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH v5 4/8 RDMA/core: Add resource tracking for create and destroy QPs
In reply to a message not in the archiveFrom: Leon Romanovsky <leonro-VPRAkNaXOzVWk0Htik3J/[email protected]>
Track create and destroy operations of QP objects.
--- drivers/infiniband/core/core_priv.h | 20 ++++++++++++++++++++ drivers/infiniband/core/uverbs_cmd.c | 3 +-- drivers/infiniband/core/verbs.c | 5 ++--- 3 files changed, 23 insertions(+), 5 deletions(-)
drivers/infiniband/core/core_priv.h +20 -0
@@ -301,4 +301,24 @@ struct ib_device *ib_device_get_by_index(u32 ifindex);/* RDMA device netlink */ void nldev_init (void );void nldev_exit (void ); + +static inline struct ib_qp *_ib_create_qp (struct ib_device *dev, +struct ib_pd *pd, +struct ib_qp_init_attr *attr, +struct ib_udata *udata) +{ +struct ib_qp *qp; + + qp = dev->create_qp (pd, attr, udata); +if (!IS_ERR (qp)) { + qp->device = dev; + qp->pd = pd; +if (attr->qp_type <IB_QPT_MAX ) { + qp->res.type =RDMA_RESTRACK_QP ; +rdma_restrack_add (&qp->res); + } + } + +return qp; +}#endif /* _CORE_PRIV_H */
drivers/infiniband/core/uverbs_cmd.c +1 -2
@@ -1514,7 +1514,7 @@ static int create_qp(struct ib_uverbs_file *file,if (cmd->qp_type ==IB_QPT_XRC_TGT ) qp =ib_create_qp (pd, &attr);else - qp = device->create_qp (pd, &attr, uhw); + qp =_ib_create_qp (device, pd, &attr, uhw);if (IS_ERR (qp)) { ret =PTR_ERR (qp);
@@ -1527,7 +1527,6 @@ static int create_qp(struct ib_uverbs_file *file, goto err_cb; qp->real_qp = qp; - qp->device = device; qp->pd = pd; qp->send_cq = attr.send_cq; qp->recv_cq = attr.recv_cq;
drivers/infiniband/core/verbs.c +2 -3
@@ -883,7 +883,7 @@ struct ib_qp *ib_create_qp(struct ib_pd *pd,if (qp_init_attr->cap.max_rdma_ctxs)rdma_rw_init_qp (device, qp_init_attr); - qp = device->create_qp (pd, qp_init_attr,NULL ); + qp =_ib_create_qp (device, pd, qp_init_attr,NULL );if (IS_ERR (qp))return qp;
@@ -893,7 +893,6 @@ struct ib_qp *ib_create_qp(struct ib_pd *pd,return ERR_PTR (ret); } - qp->device = device; qp->real_qp = qp; qp->uobject =NULL ; qp->qp_type = qp_init_attr->qp_type;
@@ -923,7 +922,6 @@ struct ib_qp *ib_create_qp(struct ib_pd *pd,atomic_inc (&qp_init_attr->srq->usecnt); } - qp->pd = pd; qp->send_cq = qp_init_attr->send_cq; qp->xrcd =NULL ;
@@ -1539,6 +1537,7 @@ int ib_destroy_qp(struct ib_qp *qp)if (!qp->uobject)rdma_rw_cleanup_mrs (qp); +rdma_restrack_del (&qp->res); ret = qp->device->destroy_qp (qp);if (!ret) {if (pd)
Signature · 6 lines
2.15.1 -- To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH v5 5/8 RDMA/core: Add resource tracking for create and destroy CQs
In reply to a message not in the archiveFrom: Leon Romanovsky <leonro-VPRAkNaXOzVWk0Htik3J/[email protected]>
Track create and destroy operations of CQ objects.
--- drivers/infiniband/core/cq.c | 5 +++++ drivers/infiniband/core/uverbs_cmd.c | 2 ++ drivers/infiniband/core/uverbs_std_types.c | 3 +++ drivers/infiniband/core/verbs.c | 3 +++ 4 files changed, 13 insertions(+)
drivers/infiniband/core/cq.c +5 -0
@@ -160,6 +160,9 @@ struct ib_cq *__ib_alloc_cq(struct ib_device *dev, void *private,if (!cq->wc) goto out_destroy_cq; + cq->res.type =RDMA_RESTRACK_CQ ; +rdma_restrack_add (&cq->res); +switch (cq->poll_ctx) {case IB_POLL_DIRECT : cq->comp_handler = ib_cq_completion_direct;
@@ -184,6 +187,7 @@ struct ib_cq *__ib_alloc_cq(struct ib_device *dev, void *private, out_free_wc:kfree (cq->wc); +rdma_restrack_del (&cq->res); out_destroy_cq: cq->device->destroy_cq (cq);return ERR_PTR (ret);
@@ -215,6 +219,7 @@ void ib_free_cq(struct ib_cq *cq) }kfree (cq->wc); +rdma_restrack_del (&cq->res); ret = cq->device->destroy_cq (cq);WARN_ON_ONCE (ret); }
drivers/infiniband/core/uverbs_cmd.c +2 -0
@@ -1033,6 +1033,8 @@ static struct ib_ucq_object *create_cq(struct ib_uverbs_file *file, goto err_cb;uobj_alloc_commit (&obj->uobject); + cq->res.type =RDMA_RESTRACK_CQ ; +rdma_restrack_add (&cq->res);return obj;
drivers/infiniband/core/uverbs_std_types.c +3 -0
@@ -35,6 +35,7 @@#include <rdma/ib_verbs.h> #include <linux/bug.h> #include <linux/file.h> +#include <rdma/restrack.h> #include "rdma_core.h" #include "uverbs.h"
@@ -319,6 +320,8 @@ static int uverbs_create_cq_handler(struct ib_device *ib_dev, obj->uobject.object = cq; obj->uobject.user_handle = user_handle;atomic_set (&cq->usecnt,0 ); + cq->res.type =RDMA_RESTRACK_CQ ; +rdma_restrack_add (&cq->res); ret =uverbs_copy_to (attrs,CREATE_CQ_RESP_CQE , &cq->cqe);if (ret)
drivers/infiniband/core/verbs.c +3 -0
@@ -1580,6 +1580,8 @@ struct ib_cq *ib_create_cq(struct ib_device *device, cq->event_handler = event_handler; cq->cq_context = cq_context;atomic_set (&cq->usecnt,0 ); + cq->res.type =RDMA_RESTRACK_CQ ; +rdma_restrack_add (&cq->res); }return cq;
@@ -1598,6 +1600,7 @@ int ib_destroy_cq(struct ib_cq *cq)if (atomic_read (&cq->usecnt))return -EBUSY ; +rdma_restrack_del (&cq->res);return cq->device->destroy_cq (cq); }EXPORT_SYMBOL (ib_destroy_cq);
Signature · 6 lines
2.15.1 -- To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH v5 6/8 RDMA/core: Add resource tracking for create and destroy PDs
In reply to a message not in the archiveFrom: Leon Romanovsky <leonro-VPRAkNaXOzVWk0Htik3J/[email protected]>
Track create and destroy operations of PD objects.
--- drivers/infiniband/core/uverbs_cmd.c | 2 ++ drivers/infiniband/core/verbs.c | 4 ++++ 2 files changed, 6 insertions(+)
drivers/infiniband/core/uverbs_cmd.c +2 -0
@@ -340,6 +340,8 @@ ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file, uobj->object = pd;memset (&resp,0 ,sizeof resp); resp.pd_handle = uobj->id; + pd->res.type =RDMA_RESTRACK_PD ; +rdma_restrack_add (&pd->res);if (copy_to_user (u64_to_user_ptr (cmd.response), &resp,sizeof resp)) { ret = -EFAULT ;
drivers/infiniband/core/verbs.c +4 -0
@@ -264,6 +264,9 @@ struct ib_pd *__ib_alloc_pd(struct ib_device *device, unsigned int flags, mr_access_flags |=IB_ACCESS_REMOTE_READ |IB_ACCESS_REMOTE_WRITE ; } + pd->res.type =RDMA_RESTRACK_PD ; +rdma_restrack_add (&pd->res); +if (mr_access_flags) {struct ib_mr *mr;
@@ -313,6 +316,7 @@ void ib_dealloc_pd(struct ib_pd *pd)requires the caller to guarantee we can't race here. */ WARN_ON (atomic_read (&pd->usecnt)); +rdma_restrack_del (&pd->res);/* Making delalloc_pd a void return is a WIP, no driver should return an error here. */ ret = pd->device->dealloc_pd (pd);
Signature · 6 lines
2.15.1 -- To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH v5 7/8 RDMA/nldev: Provide global resource utilization
In reply to a message not in the archiveFrom: Leon Romanovsky <leonro-VPRAkNaXOzVWk0Htik3J/[email protected]>
Export through netlink interface, the global device utilization for the rdmatool as the main user of RDMA nldev interface.
Provide both dumpit and doit callbacks.
As an example of possible output from rdmatool for system with 5 Mellanox's card
$ rdma res 1: mlx5_0: qp 4 cq 5 pd 3 2: mlx5_1: qp 4 cq 5 pd 3 3: mlx5_2: qp 4 cq 5 pd 3 4: mlx5_3: qp 2 cq 3 pd 2 5: mlx5_4: qp 4 cq 5 pd 3
--- drivers/infiniband/core/nldev.c | 151 +++++++++++++++++++++++++++++++++++++++ include/uapi/rdma/rdma_netlink.h | 10 +++ 2 files changed, 161 insertions(+)
drivers/infiniband/core/nldev.c +151 -0
@@ -31,6 +31,8 @@*/ #include <linux/module.h> +#include <linux/pid.h> +#include <linux/pid_namespace.h> #include <net/netlink.h> #include <rdma/rdma_netlink.h>
@@ -52,6 +54,11 @@ static const struct nla_policy nldev_policy[RDMA_NLDEV_ATTR_MAX] = { [RDMA_NLDEV_ATTR_PORT_STATE] = { .type = NLA_U8 }, [RDMA_NLDEV_ATTR_PORT_PHYS_STATE] = { .type = NLA_U8 }, [RDMA_NLDEV_ATTR_DEV_NODE_TYPE] = { .type = NLA_U8 }, + [RDMA_NLDEV_ATTR_RES_SUMMARY] = { .type = NLA_NESTED }, + [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY] = { .type = NLA_NESTED }, + [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME ] = { .type =NLA_NUL_STRING , + .len = 16 }, + [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR] = { .type = NLA_U64 }, };static int fill_nldev_handle (struct sk_buff *msg,struct ib_device *device)
@@ -134,6 +141,65 @@ static int fill_port_info(struct sk_buff *msg,return 0 ; } +static int fill_res_info_entry (struct sk_buff *msg, +const char *name,u64 curr) +{ +struct nlattr *entry_attr; + + entry_attr =nla_nest_start (msg,RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY ); +if (!entry_attr) +return -EMSGSIZE ; + +if (nla_put_string (msg,RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME , name)) + goto err; +if (nla_put_u64_64bit (msg, +RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR , curr,0 )) + goto err; + +nla_nest_end (msg, entry_attr); +return 0 ; + +err: +nla_nest_cancel (msg, entry_attr); +return -EMSGSIZE ; +} + +static int fill_res_info (struct sk_buff *msg,struct ib_device *device) +{ +static const char *names[RDMA_RESTRACK_MAX ] = { + [RDMA_RESTRACK_PD ] ="pd" , + [RDMA_RESTRACK_CQ ] ="cq" , + [RDMA_RESTRACK_QP ] ="qp" , + }; + +struct rdma_restrack_root *res = &device->res; +struct nlattr *table_attr; +int ret, i, curr; + +if (fill_nldev_handle (msg, device)) +return -EMSGSIZE ; + + table_attr =nla_nest_start (msg,RDMA_NLDEV_ATTR_RES_SUMMARY ); +if (!table_attr) +return -EMSGSIZE ; + +for (i =0 ; i <RDMA_RESTRACK_MAX ; i++) { +if (!names[i]) +continue ; + curr =rdma_restrack_count (res, i,task_active_pid_ns (current)); + ret =fill_res_info_entry (msg, names[i], curr); +if (ret) + goto err; + } + +nla_nest_end (msg, table_attr); +return 0 ; + +err: +nla_nest_cancel (msg, table_attr); +return ret; +} +static int nldev_get_doit (struct sk_buff *skb,struct nlmsghdr *nlh,struct netlink_ext_ack *extack) {
@@ -329,6 +395,87 @@ static int nldev_port_get_dumpit(struct sk_buff *skb,return skb->len; } +static int nldev_res_get_doit (struct sk_buff *skb,struct nlmsghdr *nlh, +struct netlink_ext_ack *extack) +{ +struct nlattr *tb[RDMA_NLDEV_ATTR_MAX ]; +struct ib_device *device; +struct sk_buff *msg; +u32 index; +int ret; + + ret =nlmsg_parse (nlh,0 , tb,RDMA_NLDEV_ATTR_MAX -1 , + nldev_policy, extack); +if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX ]) +return -EINVAL ; + + index =nla_get_u32 (tb[RDMA_NLDEV_ATTR_DEV_INDEX ]); + device =ib_device_get_by_index (index); +if (!device) +return -EINVAL ; + + msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL ); +if (!msg) + goto err; + + nlh =nlmsg_put (msg,NETLINK_CB (skb).portid, nlh->nlmsg_seq, +RDMA_NL_GET_TYPE (RDMA_NL_NLDEV ,RDMA_NLDEV_CMD_RES_GET ), +0 ,0 ); + + ret =fill_res_info (msg, device); +if (ret) + goto err_free; + +nlmsg_end (msg, nlh); +put_device (&device->dev); +return rdma_nl_unicast (msg,NETLINK_CB (skb).portid); + +err_free: +nlmsg_free (msg); +err: +put_device (&device->dev); +return ret; +} + +static int _nldev_res_get_dumpit (struct ib_device *device, +struct sk_buff *skb, +struct netlink_callback *cb, +unsigned int idx) +{ +int start = cb->args[0 ]; +struct nlmsghdr *nlh; + +if (idx < start) +return 0 ; + + nlh =nlmsg_put (skb,NETLINK_CB (cb->skb).portid, cb->nlh->nlmsg_seq, +RDMA_NL_GET_TYPE (RDMA_NL_NLDEV ,RDMA_NLDEV_CMD_RES_GET ), +0 ,NLM_F_MULTI ); + +if (fill_res_info (skb, device)) { +nlmsg_cancel (skb, nlh); + goto out; + } + +nlmsg_end (skb, nlh); + + idx++; + +out: + cb->args[0 ] = idx; +return skb->len; +} + +static int nldev_res_get_dumpit (struct sk_buff *skb, +struct netlink_callback *cb) +{ +/* +* There is no need to take lock, because +* we are relying on ib_core's lists_rwsem +*/ +return ib_enum_all_devs (_nldev_res_get_dumpit, skb, cb); +} +static const struct rdma_nl_cbs nldev_cb_table[RDMA_NLDEV_NUM_OPS ] = { [RDMA_NLDEV_CMD_GET ] = { .doit = nldev_get_doit,
@@ -338,6 +485,10 @@ static const struct rdma_nl_cbs nldev_cb_table[RDMA_NLDEV_NUM_OPS] = { .doit = nldev_port_get_doit, .dump = nldev_port_get_dumpit, }, + [RDMA_NLDEV_CMD_RES_GET] = { + .doit = nldev_res_get_doit, + .dump = nldev_res_get_dumpit, + }, };void __init nldev_init (void )
include/uapi/rdma/rdma_netlink.h +10 -0
@@ -236,6 +236,11 @@ enum rdma_nldev_command {RDMA_NLDEV_CMD_PORT_NEW ,RDMA_NLDEV_CMD_PORT_DEL , +RDMA_NLDEV_CMD_RES_GET ,/* can dump */ +RDMA_NLDEV_CMD_RES_SET , +RDMA_NLDEV_CMD_RES_NEW , +RDMA_NLDEV_CMD_RES_DEL , + RDMA_NLDEV_NUM_OPS };
@@ -303,6 +308,11 @@ enum rdma_nldev_attr {RDMA_NLDEV_ATTR_DEV_NODE_TYPE ,/* u8 */ +RDMA_NLDEV_ATTR_RES_SUMMARY ,/* nested table */ +RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY ,/* nested table */ +RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME ,/* string */ +RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR ,/* u64 */ +RDMA_NLDEV_ATTR_MAX };#endif /* _UAPI_RDMA_NETLINK_H */
Signature · 6 lines
2.15.1 -- To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH v5 8/8 RDMA/nldev: Provide detailed QP information
In reply to a message not in the archiveFrom: Leon Romanovsky <leonro-VPRAkNaXOzVWk0Htik3J/[email protected]>
Implement RDMA nldev netlink interface to get detailed QP information.
Currently only dumpit variant is implemented.
--- drivers/infiniband/core/nldev.c | 213 +++++++++++++++++++++++++++++++++++++++ include/uapi/rdma/rdma_netlink.h | 45 +++++++++ 2 files changed, 258 insertions(+)
drivers/infiniband/core/nldev.c +213 -0
@@ -59,6 +59,18 @@ static const struct nla_policy nldev_policy[RDMA_NLDEV_ATTR_MAX] = { [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME ] = { .type =NLA_NUL_STRING , .len = 16 }, [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR] = { .type = NLA_U64 }, + [RDMA_NLDEV_ATTR_RES_QP] = { .type = NLA_NESTED }, + [RDMA_NLDEV_ATTR_RES_QP_ENTRY] = { .type = NLA_NESTED }, + [RDMA_NLDEV_ATTR_RES_LQPN] = { .type = NLA_U32 }, + [RDMA_NLDEV_ATTR_RES_RQPN] = { .type = NLA_U32 }, + [RDMA_NLDEV_ATTR_RES_RQ_PSN] = { .type = NLA_U32 }, + [RDMA_NLDEV_ATTR_RES_SQ_PSN] = { .type = NLA_U32 }, + [RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE] = { .type = NLA_U8 }, + [RDMA_NLDEV_ATTR_RES_TYPE] = { .type = NLA_U8 }, + [RDMA_NLDEV_ATTR_RES_STATE] = { .type = NLA_U8 }, + [RDMA_NLDEV_ATTR_RES_PID] = { .type = NLA_U32 }, + [RDMA_NLDEV_ATTR_RES_KERN_NAME ] = { .type =NLA_NUL_STRING , + .len = TASK_COMM_LEN }, };static int fill_nldev_handle (struct sk_buff *msg,struct ib_device *device)
@@ -200,6 +212,78 @@ static int fill_res_info(struct sk_buff *msg, struct ib_device *device)return ret; } +static int fill_res_qp_entry (struct sk_buff *msg, +struct ib_qp *qp,uint32_t port) +{ +struct rdma_restrack_entry *res = &qp->res; +struct ib_qp_init_attr qp_init_attr; +struct nlattr *entry_attr; +struct ib_qp_attr qp_attr; +int ret; + + ret =ib_query_qp (qp, &qp_attr,0 , &qp_init_attr); +if (ret) +return ret; + +if (port && port != qp_attr.port_num) +return 0 ; + + entry_attr =nla_nest_start (msg,RDMA_NLDEV_ATTR_RES_QP_ENTRY ); +if (!entry_attr) + goto out; + +/* In create_qp() port is not set yet */ +if (qp_attr.port_num && +nla_put_u32 (msg,RDMA_NLDEV_ATTR_PORT_INDEX , qp_attr.port_num)) + goto err; + +if (nla_put_u32 (msg,RDMA_NLDEV_ATTR_RES_LQPN , qp->qp_num)) + goto err; +if (qp->qp_type ==IB_QPT_RC || qp->qp_type ==IB_QPT_UC ) { +if (nla_put_u32 (msg,RDMA_NLDEV_ATTR_RES_RQPN , + qp_attr.dest_qp_num)) + goto err; +if (nla_put_u32 (msg,RDMA_NLDEV_ATTR_RES_RQ_PSN , + qp_attr.rq_psn)) + goto err; + } + +if (nla_put_u32 (msg,RDMA_NLDEV_ATTR_RES_SQ_PSN , qp_attr.sq_psn)) + goto err; + +if (qp->qp_type ==IB_QPT_RC || qp->qp_type ==IB_QPT_UC || + qp->qp_type ==IB_QPT_XRC_INI || qp->qp_type ==IB_QPT_XRC_TGT ) { +if (nla_put_u8 (msg,RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE , + qp_attr.path_mig_state)) + goto err; + } +if (nla_put_u8 (msg,RDMA_NLDEV_ATTR_RES_TYPE , qp->qp_type)) + goto err; +if (nla_put_u8 (msg,RDMA_NLDEV_ATTR_RES_STATE , qp_attr.qp_state)) + goto err; + +/* +* Existence of task means that it is user QP and netlink +* user is invited to go and read /proc/PID/comm to get name +* of the task file and res->task_com should be NULL. +*/ +if (rdma_is_kernel_res (res)) { +if (nla_put_string (msg,RDMA_NLDEV_ATTR_RES_KERN_NAME , res->kern_name)) + goto err; + }else { +if (nla_put_u32 (msg,RDMA_NLDEV_ATTR_RES_PID ,task_pid_vnr (res->task))) + goto err; + } + +nla_nest_end (msg, entry_attr); +return 0 ; + +err: +nla_nest_cancel (msg, entry_attr); +out: +return -EMSGSIZE ; +} +static int nldev_get_doit (struct sk_buff *skb,struct nlmsghdr *nlh,struct netlink_ext_ack *extack) {
@@ -476,6 +560,122 @@ static int nldev_res_get_dumpit(struct sk_buff *skb,return ib_enum_all_devs (_nldev_res_get_dumpit, skb, cb); } +static int nldev_res_get_qp_dumpit (struct sk_buff *skb, +struct netlink_callback *cb) +{ +struct nlattr *tb[RDMA_NLDEV_ATTR_MAX ]; +struct rdma_restrack_entry *res; +int err, key, ret =0 , idx =0 ; +struct nlattr *table_attr; +struct ib_device *device; +int start = cb->args[0 ]; +struct ib_qp *qp =NULL ; +struct nlmsghdr *nlh; +u32 index, port =0 ; + + err =nlmsg_parse (cb->nlh,0 , tb,RDMA_NLDEV_ATTR_MAX -1 , + nldev_policy,NULL ); +/* +* Right now, we are expecting the device index to get QP information, +* but it is possible to extend this code to return all devices in +* one shot by checking the existence of RDMA_NLDEV_ATTR_DEV_INDEX. +* if it doesn't exist, we will iterate over all devices. +* +* But it is not needed for now. +*/ +if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX ]) +return -EINVAL ; + + index =nla_get_u32 (tb[RDMA_NLDEV_ATTR_DEV_INDEX ]); + device =ib_device_get_by_index (index); +if (!device) +return -EINVAL ; + +/* +* If no PORT_INDEX is supplied, we will return all QPs from that device +*/ +if (tb[RDMA_NLDEV_ATTR_PORT_INDEX ]) { + port =nla_get_u32 (tb[RDMA_NLDEV_ATTR_PORT_INDEX ]); +if (!rdma_is_port_valid (device, port)) { + ret = -EINVAL ; + goto err_index; + } + } + + nlh =nlmsg_put (skb,NETLINK_CB (cb->skb).portid, cb->nlh->nlmsg_seq, +RDMA_NL_GET_TYPE (RDMA_NL_NLDEV ,RDMA_NLDEV_CMD_RES_QP_GET ), +0 ,NLM_F_MULTI ); + +if (fill_nldev_handle (skb, device)) { + ret = -EMSGSIZE ; + goto err; + } + + table_attr =nla_nest_start (skb,RDMA_NLDEV_ATTR_RES_QP ); +if (!table_attr) { + ret = -EMSGSIZE ; + goto err; + } + + key =srcu_read_lock (&device->res.srcu); +hash_for_each_possible_rcu (device->res.hash, res, node,RDMA_RESTRACK_QP ) { +if (idx < start) { + idx++; +continue ; + } + +if ((rdma_is_kernel_res (res) && +task_active_pid_ns (current) != &init_pid_ns) || + (!rdma_is_kernel_res (res) && +task_active_pid_ns (current) !=task_active_pid_ns (res->task))) +/* +* 1. Kernel QPs should be visible in init namsapce only +* 2. Preent only QPs visible in the current namespace +*/ +continue ; + + qp = container_of(res, struct ib_qp, res); + ret =fill_res_qp_entry (skb, qp, port); + +if (ret == -EMSGSIZE ) +/* +* There is a chance to optimize here. +* It can be done by using list_prepare_entry +* and list_for_each_entry_continue afterwards. +*/ +break ; +if (ret) + goto res_err; + idx++; + } +srcu_read_unlock (&device->res.srcu, key); + +nla_nest_end (skb, table_attr); +nlmsg_end (skb, nlh); + cb->args[0 ] = idx; + +/* +* No more QPs to fill, cancel the message and +* return 0 to mark end of dumpit. +*/ +if (!qp) + goto err; + +put_device (&device->dev); +return skb->len; + +res_err: +nla_nest_cancel (skb, table_attr); +srcu_read_unlock (&device->res.srcu, key); + +err: +nlmsg_cancel (skb, nlh); + +err_index: +put_device (&device->dev); +return ret; +} +static const struct rdma_nl_cbs nldev_cb_table[RDMA_NLDEV_NUM_OPS ] = { [RDMA_NLDEV_CMD_GET ] = { .doit = nldev_get_doit,
@@ -489,6 +689,19 @@ static const struct rdma_nl_cbs nldev_cb_table[RDMA_NLDEV_NUM_OPS] = { .doit = nldev_res_get_doit, .dump = nldev_res_get_dumpit, }, + [RDMA_NLDEV_CMD_RES_QP_GET] = { + .dump = nldev_res_get_qp_dumpit, + /* + * .doit is not implemented yet for two reasons: + * 1. It is not needed yet. + * 2. There is a need to provide identifier, while it is easy + * for the QPs (device index + port index + LQPN), it is not + * the case for the rest of resources (PD and CQ). Because it + * is better to provide similar interface for all resources, + * let's wait till we will have other resources implemented + * too. + */ + }, };void __init nldev_init (void )
include/uapi/rdma/rdma_netlink.h +45 -0
57 lines not on this page · raw message
Signature · 6 lines
2.15.1 -- To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html