Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
0002 /*
0003  * Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved.
0004  */
0005 
0006 #ifndef _RDMA_RESTRACK_H_
0007 #define _RDMA_RESTRACK_H_
0008 
0009 #include <linux/typecheck.h>
0010 #include <linux/sched.h>
0011 #include <linux/kref.h>
0012 #include <linux/completion.h>
0013 #include <linux/sched/task.h>
0014 #include <uapi/rdma/rdma_netlink.h>
0015 #include <linux/xarray.h>
0016 
0017 struct ib_device;
0018 struct sk_buff;
0019 
0020 /**
0021  * enum rdma_restrack_type - HW objects to track
0022  */
0023 enum rdma_restrack_type {
0024     /**
0025      * @RDMA_RESTRACK_PD: Protection domain (PD)
0026      */
0027     RDMA_RESTRACK_PD,
0028     /**
0029      * @RDMA_RESTRACK_CQ: Completion queue (CQ)
0030      */
0031     RDMA_RESTRACK_CQ,
0032     /**
0033      * @RDMA_RESTRACK_QP: Queue pair (QP)
0034      */
0035     RDMA_RESTRACK_QP,
0036     /**
0037      * @RDMA_RESTRACK_CM_ID: Connection Manager ID (CM_ID)
0038      */
0039     RDMA_RESTRACK_CM_ID,
0040     /**
0041      * @RDMA_RESTRACK_MR: Memory Region (MR)
0042      */
0043     RDMA_RESTRACK_MR,
0044     /**
0045      * @RDMA_RESTRACK_CTX: Verbs contexts (CTX)
0046      */
0047     RDMA_RESTRACK_CTX,
0048     /**
0049      * @RDMA_RESTRACK_COUNTER: Statistic Counter
0050      */
0051     RDMA_RESTRACK_COUNTER,
0052     /**
0053      * @RDMA_RESTRACK_SRQ: Shared receive queue (SRQ)
0054      */
0055     RDMA_RESTRACK_SRQ,
0056     /**
0057      * @RDMA_RESTRACK_MAX: Last entry, used for array dclarations
0058      */
0059     RDMA_RESTRACK_MAX
0060 };
0061 
0062 /**
0063  * struct rdma_restrack_entry - metadata per-entry
0064  */
0065 struct rdma_restrack_entry {
0066     /**
0067      * @valid: validity indicator
0068      *
0069      * The entries are filled during rdma_restrack_add,
0070      * can be attempted to be free during rdma_restrack_del.
0071      *
0072      * As an example for that, see mlx5 QPs with type MLX5_IB_QPT_HW_GSI
0073      */
0074     bool            valid;
0075     /**
0076      * @no_track: don't add this entry to restrack DB
0077      *
0078      * This field is used to mark an entry that doesn't need to be added to
0079      * internal restrack DB and presented later to the users at the nldev
0080      * query stage.
0081      */
0082     u8          no_track : 1;
0083     /*
0084      * @kref: Protect destroy of the resource
0085      */
0086     struct kref     kref;
0087     /*
0088      * @comp: Signal that all consumers of resource are completed their work
0089      */
0090     struct completion   comp;
0091     /**
0092      * @task: owner of resource tracking entity
0093      *
0094      * There are two types of entities: created by user and created
0095      * by kernel.
0096      *
0097      * This is relevant for the entities created by users.
0098      * For the entities created by kernel, this pointer will be NULL.
0099      */
0100     struct task_struct  *task;
0101     /**
0102      * @kern_name: name of owner for the kernel created entities.
0103      */
0104     const char      *kern_name;
0105     /**
0106      * @type: various objects in restrack database
0107      */
0108     enum rdma_restrack_type type;
0109     /**
0110      * @user: user resource
0111      */
0112     bool            user;
0113     /**
0114      * @id: ID to expose to users
0115      */
0116     u32 id;
0117 };
0118 
0119 int rdma_restrack_count(struct ib_device *dev,
0120             enum rdma_restrack_type type);
0121 /**
0122  * rdma_is_kernel_res() - check the owner of resource
0123  * @res:  resource entry
0124  */
0125 static inline bool rdma_is_kernel_res(const struct rdma_restrack_entry *res)
0126 {
0127     return !res->user;
0128 }
0129 
0130 /**
0131  * rdma_restrack_get() - grab to protect resource from release
0132  * @res:  resource entry
0133  */
0134 int __must_check rdma_restrack_get(struct rdma_restrack_entry *res);
0135 
0136 /**
0137  * rdma_restrack_put() - release resource
0138  * @res:  resource entry
0139  */
0140 int rdma_restrack_put(struct rdma_restrack_entry *res);
0141 
0142 /*
0143  * Helper functions for rdma drivers when filling out
0144  * nldev driver attributes.
0145  */
0146 int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value);
0147 int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name,
0148                    u32 value);
0149 int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value);
0150 int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name,
0151                    u64 value);
0152 int rdma_nl_put_driver_string(struct sk_buff *msg, const char *name,
0153                   const char *str);
0154 int rdma_nl_stat_hwcounter_entry(struct sk_buff *msg, const char *name,
0155                  u64 value);
0156 
0157 struct rdma_restrack_entry *rdma_restrack_get_byid(struct ib_device *dev,
0158                            enum rdma_restrack_type type,
0159                            u32 id);
0160 
0161 /**
0162  * rdma_restrack_no_track() - don't add resource to the DB
0163  * @res: resource entry
0164  *
0165  * Every user of thie API should be cross examined.
0166  * Probaby you don't need to use this function.
0167  */
0168 static inline void rdma_restrack_no_track(struct rdma_restrack_entry *res)
0169 {
0170     res->no_track = true;
0171 }
0172 static inline bool rdma_restrack_is_tracked(struct rdma_restrack_entry *res)
0173 {
0174     return !res->no_track;
0175 }
0176 #endif /* _RDMA_RESTRACK_H_ */