Commit 64917f4c authored by Ivan Orlov's avatar Ivan Orlov Committed by Leon Romanovsky

RDMA: Make all 'class' structures const

Now that the driver core allows for struct class to be in read-only
memory, making all 'class' structures to be declared at build time
placing them into read-only memory, instead of having to be dynamically
allocated at load time.

Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Leon Romanovsky <leon@kernel.org>
Cc: Dennis Dalessandro <dennis.dalessandro@cornelisnetworks.com>
Cc: "Md. Haris Iqbal" <haris.iqbal@ionos.com>
Cc: Jack Wang <jinpu.wang@ionos.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Yishai Hadas <yishaih@nvidia.com>
Cc: Ivan Orlov <ivan.orlov0322@gmail.com>
Cc: Benjamin Tissoires <benjamin.tissoires@redhat.com>
Suggested-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: default avatarIvan Orlov <ivan.orlov0322@gmail.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Link: https://lore.kernel.org/r/2023080427-commuting-crewless-cbee@gregkhSigned-off-by: default avatarLeon Romanovsky <leon@kernel.org>
parent 849b1955
...@@ -72,12 +72,23 @@ enum { ...@@ -72,12 +72,23 @@ enum {
#define IB_UVERBS_BASE_DEV MKDEV(IB_UVERBS_MAJOR, IB_UVERBS_BASE_MINOR) #define IB_UVERBS_BASE_DEV MKDEV(IB_UVERBS_MAJOR, IB_UVERBS_BASE_MINOR)
static dev_t dynamic_uverbs_dev; static dev_t dynamic_uverbs_dev;
static struct class *uverbs_class;
static DEFINE_IDA(uverbs_ida); static DEFINE_IDA(uverbs_ida);
static int ib_uverbs_add_one(struct ib_device *device); static int ib_uverbs_add_one(struct ib_device *device);
static void ib_uverbs_remove_one(struct ib_device *device, void *client_data); static void ib_uverbs_remove_one(struct ib_device *device, void *client_data);
static char *uverbs_devnode(const struct device *dev, umode_t *mode)
{
if (mode)
*mode = 0666;
return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
}
static const struct class uverbs_class = {
.name = "infiniband_verbs",
.devnode = uverbs_devnode,
};
/* /*
* Must be called with the ufile->device->disassociate_srcu held, and the lock * Must be called with the ufile->device->disassociate_srcu held, and the lock
* must be held until use of the ucontext is finished. * must be held until use of the ucontext is finished.
...@@ -1117,7 +1128,7 @@ static int ib_uverbs_add_one(struct ib_device *device) ...@@ -1117,7 +1128,7 @@ static int ib_uverbs_add_one(struct ib_device *device)
} }
device_initialize(&uverbs_dev->dev); device_initialize(&uverbs_dev->dev);
uverbs_dev->dev.class = uverbs_class; uverbs_dev->dev.class = &uverbs_class;
uverbs_dev->dev.parent = device->dev.parent; uverbs_dev->dev.parent = device->dev.parent;
uverbs_dev->dev.release = ib_uverbs_release_dev; uverbs_dev->dev.release = ib_uverbs_release_dev;
uverbs_dev->groups[0] = &dev_attr_group; uverbs_dev->groups[0] = &dev_attr_group;
...@@ -1235,13 +1246,6 @@ static void ib_uverbs_remove_one(struct ib_device *device, void *client_data) ...@@ -1235,13 +1246,6 @@ static void ib_uverbs_remove_one(struct ib_device *device, void *client_data)
put_device(&uverbs_dev->dev); put_device(&uverbs_dev->dev);
} }
static char *uverbs_devnode(const struct device *dev, umode_t *mode)
{
if (mode)
*mode = 0666;
return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
}
static int __init ib_uverbs_init(void) static int __init ib_uverbs_init(void)
{ {
int ret; int ret;
...@@ -1262,16 +1266,13 @@ static int __init ib_uverbs_init(void) ...@@ -1262,16 +1266,13 @@ static int __init ib_uverbs_init(void)
goto out_alloc; goto out_alloc;
} }
uverbs_class = class_create("infiniband_verbs"); ret = class_register(&uverbs_class);
if (IS_ERR(uverbs_class)) { if (ret) {
ret = PTR_ERR(uverbs_class);
pr_err("user_verbs: couldn't create class infiniband_verbs\n"); pr_err("user_verbs: couldn't create class infiniband_verbs\n");
goto out_chrdev; goto out_chrdev;
} }
uverbs_class->devnode = uverbs_devnode; ret = class_create_file(&uverbs_class, &class_attr_abi_version.attr);
ret = class_create_file(uverbs_class, &class_attr_abi_version.attr);
if (ret) { if (ret) {
pr_err("user_verbs: couldn't create abi_version attribute\n"); pr_err("user_verbs: couldn't create abi_version attribute\n");
goto out_class; goto out_class;
...@@ -1286,7 +1287,7 @@ static int __init ib_uverbs_init(void) ...@@ -1286,7 +1287,7 @@ static int __init ib_uverbs_init(void)
return 0; return 0;
out_class: out_class:
class_destroy(uverbs_class); class_unregister(&uverbs_class);
out_chrdev: out_chrdev:
unregister_chrdev_region(dynamic_uverbs_dev, unregister_chrdev_region(dynamic_uverbs_dev,
...@@ -1303,7 +1304,7 @@ static int __init ib_uverbs_init(void) ...@@ -1303,7 +1304,7 @@ static int __init ib_uverbs_init(void)
static void __exit ib_uverbs_cleanup(void) static void __exit ib_uverbs_cleanup(void)
{ {
ib_unregister_client(&uverbs_client); ib_unregister_client(&uverbs_client);
class_destroy(uverbs_class); class_unregister(&uverbs_class);
unregister_chrdev_region(IB_UVERBS_BASE_DEV, unregister_chrdev_region(IB_UVERBS_BASE_DEV,
IB_UVERBS_NUM_FIXED_MINOR); IB_UVERBS_NUM_FIXED_MINOR);
unregister_chrdev_region(dynamic_uverbs_dev, unregister_chrdev_region(dynamic_uverbs_dev,
......
...@@ -10,8 +10,29 @@ ...@@ -10,8 +10,29 @@
#include "hfi.h" #include "hfi.h"
#include "device.h" #include "device.h"
static struct class *class; static char *hfi1_devnode(const struct device *dev, umode_t *mode)
static struct class *user_class; {
if (mode)
*mode = 0600;
return kasprintf(GFP_KERNEL, "%s", dev_name(dev));
}
static const struct class class = {
.name = "hfi1",
.devnode = hfi1_devnode,
};
static char *hfi1_user_devnode(const struct device *dev, umode_t *mode)
{
if (mode)
*mode = 0666;
return kasprintf(GFP_KERNEL, "%s", dev_name(dev));
}
static const struct class user_class = {
.name = "hfi1_user",
.devnode = hfi1_user_devnode,
};
static dev_t hfi1_dev; static dev_t hfi1_dev;
int hfi1_cdev_init(int minor, const char *name, int hfi1_cdev_init(int minor, const char *name,
...@@ -37,9 +58,9 @@ int hfi1_cdev_init(int minor, const char *name, ...@@ -37,9 +58,9 @@ int hfi1_cdev_init(int minor, const char *name,
} }
if (user_accessible) if (user_accessible)
device = device_create(user_class, NULL, dev, NULL, "%s", name); device = device_create(&user_class, NULL, dev, NULL, "%s", name);
else else
device = device_create(class, NULL, dev, NULL, "%s", name); device = device_create(&class, NULL, dev, NULL, "%s", name);
if (IS_ERR(device)) { if (IS_ERR(device)) {
ret = PTR_ERR(device); ret = PTR_ERR(device);
...@@ -72,26 +93,6 @@ const char *class_name(void) ...@@ -72,26 +93,6 @@ const char *class_name(void)
return hfi1_class_name; return hfi1_class_name;
} }
static char *hfi1_devnode(const struct device *dev, umode_t *mode)
{
if (mode)
*mode = 0600;
return kasprintf(GFP_KERNEL, "%s", dev_name(dev));
}
static const char *hfi1_class_name_user = "hfi1_user";
static const char *class_name_user(void)
{
return hfi1_class_name_user;
}
static char *hfi1_user_devnode(const struct device *dev, umode_t *mode)
{
if (mode)
*mode = 0666;
return kasprintf(GFP_KERNEL, "%s", dev_name(dev));
}
int __init dev_init(void) int __init dev_init(void)
{ {
int ret; int ret;
...@@ -102,27 +103,21 @@ int __init dev_init(void) ...@@ -102,27 +103,21 @@ int __init dev_init(void)
goto done; goto done;
} }
class = class_create(class_name()); ret = class_register(&class);
if (IS_ERR(class)) { if (ret) {
ret = PTR_ERR(class);
pr_err("Could not create device class (err %d)\n", -ret); pr_err("Could not create device class (err %d)\n", -ret);
unregister_chrdev_region(hfi1_dev, HFI1_NMINORS); unregister_chrdev_region(hfi1_dev, HFI1_NMINORS);
goto done; goto done;
} }
class->devnode = hfi1_devnode;
user_class = class_create(class_name_user()); ret = class_register(&user_class);
if (IS_ERR(user_class)) { if (ret) {
ret = PTR_ERR(user_class);
pr_err("Could not create device class for user accessible files (err %d)\n", pr_err("Could not create device class for user accessible files (err %d)\n",
-ret); -ret);
class_destroy(class); class_unregister(&class);
class = NULL;
user_class = NULL;
unregister_chrdev_region(hfi1_dev, HFI1_NMINORS); unregister_chrdev_region(hfi1_dev, HFI1_NMINORS);
goto done; goto done;
} }
user_class->devnode = hfi1_user_devnode;
done: done:
return ret; return ret;
...@@ -130,11 +125,8 @@ int __init dev_init(void) ...@@ -130,11 +125,8 @@ int __init dev_init(void)
void dev_cleanup(void) void dev_cleanup(void)
{ {
class_destroy(class); class_unregister(&class);
class = NULL; class_unregister(&user_class);
class_destroy(user_class);
user_class = NULL;
unregister_chrdev_region(hfi1_dev, HFI1_NMINORS); unregister_chrdev_region(hfi1_dev, HFI1_NMINORS);
} }
...@@ -2250,7 +2250,9 @@ static ssize_t qib_write_iter(struct kiocb *iocb, struct iov_iter *from) ...@@ -2250,7 +2250,9 @@ static ssize_t qib_write_iter(struct kiocb *iocb, struct iov_iter *from)
return qib_user_sdma_writev(rcd, pq, iter_iov(from), from->nr_segs); return qib_user_sdma_writev(rcd, pq, iter_iov(from), from->nr_segs);
} }
static struct class *qib_class; static const struct class qib_class = {
.name = "ipath",
};
static dev_t qib_dev; static dev_t qib_dev;
int qib_cdev_init(int minor, const char *name, int qib_cdev_init(int minor, const char *name,
...@@ -2281,7 +2283,7 @@ int qib_cdev_init(int minor, const char *name, ...@@ -2281,7 +2283,7 @@ int qib_cdev_init(int minor, const char *name,
goto err_cdev; goto err_cdev;
} }
device = device_create(qib_class, NULL, dev, NULL, "%s", name); device = device_create(&qib_class, NULL, dev, NULL, "%s", name);
if (!IS_ERR(device)) if (!IS_ERR(device))
goto done; goto done;
ret = PTR_ERR(device); ret = PTR_ERR(device);
...@@ -2325,9 +2327,8 @@ int __init qib_dev_init(void) ...@@ -2325,9 +2327,8 @@ int __init qib_dev_init(void)
goto done; goto done;
} }
qib_class = class_create("ipath"); ret = class_register(&qib_class);
if (IS_ERR(qib_class)) { if (ret) {
ret = PTR_ERR(qib_class);
pr_err("Could not create device class (err %d)\n", -ret); pr_err("Could not create device class (err %d)\n", -ret);
unregister_chrdev_region(qib_dev, QIB_NMINORS); unregister_chrdev_region(qib_dev, QIB_NMINORS);
} }
...@@ -2338,10 +2339,8 @@ int __init qib_dev_init(void) ...@@ -2338,10 +2339,8 @@ int __init qib_dev_init(void)
void qib_dev_cleanup(void) void qib_dev_cleanup(void)
{ {
if (qib_class) { if (class_is_registered(&qib_class))
class_destroy(qib_class); class_unregister(&qib_class);
qib_class = NULL;
}
unregister_chrdev_region(qib_dev, QIB_NMINORS); unregister_chrdev_region(qib_dev, QIB_NMINORS);
} }
......
...@@ -45,7 +45,9 @@ static struct rtrs_rdma_dev_pd dev_pd = { ...@@ -45,7 +45,9 @@ static struct rtrs_rdma_dev_pd dev_pd = {
}; };
static struct workqueue_struct *rtrs_wq; static struct workqueue_struct *rtrs_wq;
static struct class *rtrs_clt_dev_class; static const struct class rtrs_clt_dev_class = {
.name = "rtrs-client",
};
static inline bool rtrs_clt_is_connected(const struct rtrs_clt_sess *clt) static inline bool rtrs_clt_is_connected(const struct rtrs_clt_sess *clt)
{ {
...@@ -2698,7 +2700,7 @@ static struct rtrs_clt_sess *alloc_clt(const char *sessname, size_t paths_num, ...@@ -2698,7 +2700,7 @@ static struct rtrs_clt_sess *alloc_clt(const char *sessname, size_t paths_num,
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
} }
clt->dev.class = rtrs_clt_dev_class; clt->dev.class = &rtrs_clt_dev_class;
clt->dev.release = rtrs_clt_dev_release; clt->dev.release = rtrs_clt_dev_release;
uuid_gen(&clt->paths_uuid); uuid_gen(&clt->paths_uuid);
INIT_LIST_HEAD_RCU(&clt->paths_list); INIT_LIST_HEAD_RCU(&clt->paths_list);
...@@ -3151,16 +3153,17 @@ static const struct rtrs_rdma_dev_pd_ops dev_pd_ops = { ...@@ -3151,16 +3153,17 @@ static const struct rtrs_rdma_dev_pd_ops dev_pd_ops = {
static int __init rtrs_client_init(void) static int __init rtrs_client_init(void)
{ {
rtrs_rdma_dev_pd_init(0, &dev_pd); int ret = 0;
rtrs_clt_dev_class = class_create("rtrs-client"); rtrs_rdma_dev_pd_init(0, &dev_pd);
if (IS_ERR(rtrs_clt_dev_class)) { ret = class_register(&rtrs_clt_dev_class);
if (ret) {
pr_err("Failed to create rtrs-client dev class\n"); pr_err("Failed to create rtrs-client dev class\n");
return PTR_ERR(rtrs_clt_dev_class); return ret;
} }
rtrs_wq = alloc_workqueue("rtrs_client_wq", 0, 0); rtrs_wq = alloc_workqueue("rtrs_client_wq", 0, 0);
if (!rtrs_wq) { if (!rtrs_wq) {
class_destroy(rtrs_clt_dev_class); class_unregister(&rtrs_clt_dev_class);
return -ENOMEM; return -ENOMEM;
} }
...@@ -3170,7 +3173,7 @@ static int __init rtrs_client_init(void) ...@@ -3170,7 +3173,7 @@ static int __init rtrs_client_init(void)
static void __exit rtrs_client_exit(void) static void __exit rtrs_client_exit(void)
{ {
destroy_workqueue(rtrs_wq); destroy_workqueue(rtrs_wq);
class_destroy(rtrs_clt_dev_class); class_unregister(&rtrs_clt_dev_class);
rtrs_rdma_dev_pd_deinit(&dev_pd); rtrs_rdma_dev_pd_deinit(&dev_pd);
} }
......
...@@ -164,7 +164,7 @@ static int rtrs_srv_create_once_sysfs_root_folders(struct rtrs_srv_path *srv_pat ...@@ -164,7 +164,7 @@ static int rtrs_srv_create_once_sysfs_root_folders(struct rtrs_srv_path *srv_pat
*/ */
goto unlock; goto unlock;
} }
srv->dev.class = rtrs_dev_class; srv->dev.class = &rtrs_dev_class;
err = dev_set_name(&srv->dev, "%s", srv_path->s.sessname); err = dev_set_name(&srv->dev, "%s", srv_path->s.sessname);
if (err) if (err)
goto unlock; goto unlock;
......
...@@ -27,7 +27,9 @@ MODULE_LICENSE("GPL"); ...@@ -27,7 +27,9 @@ MODULE_LICENSE("GPL");
#define MAX_HDR_SIZE PAGE_SIZE #define MAX_HDR_SIZE PAGE_SIZE
static struct rtrs_rdma_dev_pd dev_pd; static struct rtrs_rdma_dev_pd dev_pd;
struct class *rtrs_dev_class; const struct class rtrs_dev_class = {
.name = "rtrs-server",
};
static struct rtrs_srv_ib_ctx ib_ctx; static struct rtrs_srv_ib_ctx ib_ctx;
static int __read_mostly max_chunk_size = DEFAULT_MAX_CHUNK_SIZE; static int __read_mostly max_chunk_size = DEFAULT_MAX_CHUNK_SIZE;
...@@ -2253,11 +2255,10 @@ static int __init rtrs_server_init(void) ...@@ -2253,11 +2255,10 @@ static int __init rtrs_server_init(void)
err); err);
return err; return err;
} }
rtrs_dev_class = class_create("rtrs-server"); err = class_register(&rtrs_dev_class);
if (IS_ERR(rtrs_dev_class)) { if (err)
err = PTR_ERR(rtrs_dev_class);
goto out_err; goto out_err;
}
rtrs_wq = alloc_workqueue("rtrs_server_wq", 0, 0); rtrs_wq = alloc_workqueue("rtrs_server_wq", 0, 0);
if (!rtrs_wq) { if (!rtrs_wq) {
err = -ENOMEM; err = -ENOMEM;
...@@ -2267,7 +2268,7 @@ static int __init rtrs_server_init(void) ...@@ -2267,7 +2268,7 @@ static int __init rtrs_server_init(void)
return 0; return 0;
out_dev_class: out_dev_class:
class_destroy(rtrs_dev_class); class_unregister(&rtrs_dev_class);
out_err: out_err:
return err; return err;
} }
...@@ -2275,7 +2276,7 @@ static int __init rtrs_server_init(void) ...@@ -2275,7 +2276,7 @@ static int __init rtrs_server_init(void)
static void __exit rtrs_server_exit(void) static void __exit rtrs_server_exit(void)
{ {
destroy_workqueue(rtrs_wq); destroy_workqueue(rtrs_wq);
class_destroy(rtrs_dev_class); class_unregister(&rtrs_dev_class);
rtrs_rdma_dev_pd_deinit(&dev_pd); rtrs_rdma_dev_pd_deinit(&dev_pd);
} }
......
...@@ -129,7 +129,7 @@ struct rtrs_srv_ib_ctx { ...@@ -129,7 +129,7 @@ struct rtrs_srv_ib_ctx {
int ib_dev_count; int ib_dev_count;
}; };
extern struct class *rtrs_dev_class; extern const struct class rtrs_dev_class;
void close_path(struct rtrs_srv_path *srv_path); void close_path(struct rtrs_srv_path *srv_path);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment