History

lightnvm: track inflight target creations

When creation process is still in progress, target is not yet on targets list. This causes a chance for removing whole lightnvm subsystem by calling nvm_unregister() in the meantime and finally by causing kernel panic inside target init function. This patch changes the behaviour by adding kref variable which tracks all the users of nvm_dev structure. When nvm_dev is allocated, kref value is set to 1. Then before every target creation the value is increased and decreased after target removal. The extra reference is decreased when nvm subsystem is unregistered.

Igor Konopko committed 7 years ago

Commit e69397e1 parent a24eab52 files · +32 −10Browse files

Unified · Split

2 files
  1. drivers/lightnvm/core.c +31 −10
  2. include/linux/lightnvm.h +1 −0
drivers/lightnvm/core.c+31−10
@@ -45,6 +45,8 @@ struct nvm_dev_map { · 44 unmodified lines
4545 int num_ch;
4646 };
4747
48+static void nvm_free(struct kref *ref);
49+
4850 static struct nvm_target *nvm_find_target(struct nvm_dev *dev, const char *name)
4951 {
5052 struct nvm_target *tgt;
@@ -501,6 +503,7 @@ static int nvm_remove_tgt(struct nvm_dev *dev, struct nvm_ioctl_remove *remove) · 450 unmodified lines
501503 }
502504 __nvm_remove_target(t, true);
503505 mutex_unlock(&dev->mlock);
506+ kref_put(&dev->ref, nvm_free);
504507
505508 return 0;
506509 }
@@ -1094,15 +1097,16 @@ err_fmtype: · 587 unmodified lines
10941097 return ret;
10951098 }
10961099
1097−static void nvm_free(struct nvm_dev *dev)
1100+static void nvm_free(struct kref *ref)
10981101 {
1099− if (!dev)
1100− return;
1102+ struct nvm_dev *dev = container_of(ref, struct nvm_dev, ref);
11011103
11021104 if (dev->dma_pool)
11031105 dev->ops->destroy_dma_pool(dev->dma_pool);
11041106
1105− nvm_unregister_map(dev);
1107+ if (dev->rmap)
1108+ nvm_unregister_map(dev);
1109+
11061110 kfree(dev->lun_map);
11071111 kfree(dev);
11081112 }
@@ -1139,7 +1143,13 @@ err: · 30 unmodified lines
11391143
11401144 struct nvm_dev *nvm_alloc_dev(int node)
11411145 {
1142− return kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node);
1146+ struct nvm_dev *dev;
1147+
1148+ dev = kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node);
1149+ if (dev)
1150+ kref_init(&dev->ref);
1151+
1152+ return dev;
11431153 }
11441154 EXPORT_SYMBOL(nvm_alloc_dev);
11451155
@@ -1147,12 +1157,16 @@ int nvm_register(struct nvm_dev *dev) · 1 unmodified line
11471157 {
11481158 int ret, exp_pool_size;
11491159
1150− if (!dev->q || !dev->ops)
1160+ if (!dev->q || !dev->ops) {
1161+ kref_put(&dev->ref, nvm_free);
11511162 return -EINVAL;
1163+ }
11521164
11531165 ret = nvm_init(dev);
1154− if (ret)
1166+ if (ret) {
1167+ kref_put(&dev->ref, nvm_free);
11551168 return ret;
1169+ }
11561170
11571171 exp_pool_size = max_t(int, PAGE_SIZE,
11581172 (NVM_MAX_VLBA * (sizeof(u64) + dev->geo.sos)));
@@ -1162,7 +1176,7 @@ int nvm_register(struct nvm_dev *dev) · 3 unmodified lines
11621176 exp_pool_size);
11631177 if (!dev->dma_pool) {
11641178 pr_err("nvm: could not create dma pool\n");
1165− nvm_free(dev);
1179+ kref_put(&dev->ref, nvm_free);
11661180 return -ENOMEM;
11671181 }
11681182
@@ -1184,6 +1198,7 @@ void nvm_unregister(struct nvm_dev *dev) · 15 unmodified lines
11841198 if (t->dev->parent != dev)
11851199 continue;
11861200 __nvm_remove_target(t, false);
1201+ kref_put(&dev->ref, nvm_free);
11871202 }
11881203 mutex_unlock(&dev->mlock);
11891204
@@ -1191,13 +1206,14 @@ void nvm_unregister(struct nvm_dev *dev) · 1 unmodified line
11911206 list_del(&dev->devices);
11921207 up_write(&nvm_lock);
11931208
1194− nvm_free(dev);
1209+ kref_put(&dev->ref, nvm_free);
11951210 }
11961211 EXPORT_SYMBOL(nvm_unregister);
11971212
11981213 static int __nvm_configure_create(struct nvm_ioctl_create *create)
11991214 {
12001215 struct nvm_dev *dev;
1216+ int ret;
12011217
12021218 down_write(&nvm_lock);
12031219 dev = nvm_find_nvm_dev(create->dev);
@@ -1208,7 +1224,12 @@ static int __nvm_configure_create(struct nvm_ioctl_create *create) · 4 unmodified lines
12081224 return -EINVAL;
12091225 }
12101226
1211− return nvm_create_tgt(dev, create);
1227+ kref_get(&dev->ref);
1228+ ret = nvm_create_tgt(dev, create);
1229+ if (ret)
1230+ kref_put(&dev->ref, nvm_free);
1231+
1232+ return ret;
12121233 }
12131234
12141235 static long nvm_ioctl_info(struct file *file, void __user *arg)
include/linux/lightnvm.h+1−0
@@ -428,6 +428,7 @@ struct nvm_dev { · 427 unmodified lines
428428 char name[DISK_NAME_LEN];
429429 void *private_data;
430430
431+ struct kref ref;
431432 void *rmap;
432433
433434 struct mutex mlock;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t