hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/nvdimm/namespace_devs.c
....@@ -1,14 +1,6 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
3
- *
4
- * This program is free software; you can redistribute it and/or modify
5
- * it under the terms of version 2 of the GNU General Public License as
6
- * published by the Free Software Foundation.
7
- *
8
- * This program is distributed in the hope that it will be useful, but
9
- * WITHOUT ANY WARRANTY; without even the implied warranty of
10
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
- * General Public License for more details.
124 */
135 #include <linux/module.h>
146 #include <linux/device.h>
....@@ -18,6 +10,7 @@
1810 #include <linux/nd.h>
1911 #include "nd-core.h"
2012 #include "pmem.h"
13
+#include "pfn.h"
2114 #include "nd.h"
2215
2316 static void namespace_io_release(struct device *dev)
....@@ -52,35 +45,9 @@
5245 kfree(nsblk);
5346 }
5447
55
-static const struct device_type namespace_io_device_type = {
56
- .name = "nd_namespace_io",
57
- .release = namespace_io_release,
58
-};
59
-
60
-static const struct device_type namespace_pmem_device_type = {
61
- .name = "nd_namespace_pmem",
62
- .release = namespace_pmem_release,
63
-};
64
-
65
-static const struct device_type namespace_blk_device_type = {
66
- .name = "nd_namespace_blk",
67
- .release = namespace_blk_release,
68
-};
69
-
70
-static bool is_namespace_pmem(const struct device *dev)
71
-{
72
- return dev ? dev->type == &namespace_pmem_device_type : false;
73
-}
74
-
75
-static bool is_namespace_blk(const struct device *dev)
76
-{
77
- return dev ? dev->type == &namespace_blk_device_type : false;
78
-}
79
-
80
-static bool is_namespace_io(const struct device *dev)
81
-{
82
- return dev ? dev->type == &namespace_io_device_type : false;
83
-}
48
+static bool is_namespace_pmem(const struct device *dev);
49
+static bool is_namespace_blk(const struct device *dev);
50
+static bool is_namespace_io(const struct device *dev);
8451
8552 static int is_uuid_busy(struct device *dev, void *data)
8653 {
....@@ -274,11 +241,10 @@
274241 if (dev->driver || to_ndns(dev)->claim)
275242 return -EBUSY;
276243
277
- input = kmemdup(buf, len + 1, GFP_KERNEL);
244
+ input = kstrndup(buf, len, GFP_KERNEL);
278245 if (!input)
279246 return -ENOMEM;
280247
281
- input[len] = '\0';
282248 pos = strim(input);
283249 if (strlen(pos) + 1 > NSLABEL_NAME_LEN) {
284250 rc = -EINVAL;
....@@ -419,7 +385,7 @@
419385 struct nd_region *nd_region = to_nd_region(dev->parent);
420386 ssize_t rc;
421387
422
- device_lock(dev);
388
+ nd_device_lock(dev);
423389 nvdimm_bus_lock(dev);
424390 wait_nvdimm_bus_probe_idle(dev);
425391 rc = __alt_name_store(dev, buf, len);
....@@ -427,7 +393,7 @@
427393 rc = nd_namespace_label_update(nd_region, dev);
428394 dev_dbg(dev, "%s(%zd)\n", rc < 0 ? "fail " : "", rc);
429395 nvdimm_bus_unlock(dev);
430
- device_unlock(dev);
396
+ nd_device_unlock(dev);
431397
432398 return rc < 0 ? rc : len;
433399 }
....@@ -576,6 +542,11 @@
576542 {
577543 bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0;
578544 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
545
+ unsigned long align;
546
+
547
+ align = nd_region->align / nd_region->ndr_mappings;
548
+ valid->start = ALIGN(valid->start, align);
549
+ valid->end = ALIGN_DOWN(valid->end + 1, align) - 1;
579550
580551 if (valid->start >= valid->end)
581552 goto invalid;
....@@ -1015,10 +986,10 @@
1015986 return -ENXIO;
1016987 }
1017988
1018
- div_u64_rem(val, SZ_4K * nd_region->ndr_mappings, &remainder);
989
+ div_u64_rem(val, nd_region->align, &remainder);
1019990 if (remainder) {
1020
- dev_dbg(dev, "%llu is not %dK aligned\n", val,
1021
- (SZ_4K * nd_region->ndr_mappings) / SZ_1K);
991
+ dev_dbg(dev, "%llu is not %ldK aligned\n", val,
992
+ nd_region->align / SZ_1K);
1022993 return -EINVAL;
1023994 }
1024995
....@@ -1086,7 +1057,7 @@
10861057 if (rc)
10871058 return rc;
10881059
1089
- device_lock(dev);
1060
+ nd_device_lock(dev);
10901061 nvdimm_bus_lock(dev);
10911062 wait_nvdimm_bus_probe_idle(dev);
10921063 rc = __size_store(dev, val);
....@@ -1112,7 +1083,7 @@
11121083 dev_dbg(dev, "%llx %s (%d)\n", val, rc < 0 ? "fail" : "success", rc);
11131084
11141085 nvdimm_bus_unlock(dev);
1115
- device_unlock(dev);
1086
+ nd_device_unlock(dev);
11161087
11171088 return rc < 0 ? rc : len;
11181089 }
....@@ -1295,7 +1266,7 @@
12951266 } else
12961267 return -ENXIO;
12971268
1298
- device_lock(dev);
1269
+ nd_device_lock(dev);
12991270 nvdimm_bus_lock(dev);
13001271 wait_nvdimm_bus_probe_idle(dev);
13011272 if (to_ndns(dev)->claim)
....@@ -1311,7 +1282,7 @@
13111282 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
13121283 buf[len - 1] == '\n' ? "" : "\n");
13131284 nvdimm_bus_unlock(dev);
1314
- device_unlock(dev);
1285
+ nd_device_unlock(dev);
13151286
13161287 return rc < 0 ? rc : len;
13171288 }
....@@ -1338,7 +1309,7 @@
13381309 return -ENXIO;
13391310 return sprintf(buf, "%#llx\n", (unsigned long long) res->start);
13401311 }
1341
-static DEVICE_ATTR_RO(resource);
1312
+static DEVICE_ATTR_ADMIN_RO(resource);
13421313
13431314 static const unsigned long blk_lbasize_supported[] = { 512, 520, 528,
13441315 4096, 4104, 4160, 4224, 0 };
....@@ -1385,7 +1356,7 @@
13851356 } else
13861357 return -ENXIO;
13871358
1388
- device_lock(dev);
1359
+ nd_device_lock(dev);
13891360 nvdimm_bus_lock(dev);
13901361 if (to_ndns(dev)->claim)
13911362 rc = -EBUSY;
....@@ -1396,7 +1367,7 @@
13961367 dev_dbg(dev, "result: %zd %s: %s%s", rc, rc < 0 ? "tried" : "wrote",
13971368 buf, buf[len - 1] == '\n' ? "" : "\n");
13981369 nvdimm_bus_unlock(dev);
1399
- device_unlock(dev);
1370
+ nd_device_unlock(dev);
14001371
14011372 return rc ? rc : len;
14021373 }
....@@ -1511,35 +1482,35 @@
15111482 struct nd_namespace_common *ndns = to_ndns(dev);
15121483 ssize_t rc;
15131484
1514
- device_lock(dev);
1485
+ nd_device_lock(dev);
15151486 rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : "");
1516
- device_unlock(dev);
1487
+ nd_device_unlock(dev);
15171488
15181489 return rc;
15191490 }
15201491 static DEVICE_ATTR_RO(holder);
15211492
1522
-static ssize_t __holder_class_store(struct device *dev, const char *buf)
1493
+static int __holder_class_store(struct device *dev, const char *buf)
15231494 {
15241495 struct nd_namespace_common *ndns = to_ndns(dev);
15251496
15261497 if (dev->driver || ndns->claim)
15271498 return -EBUSY;
15281499
1529
- if (strcmp(buf, "btt") == 0 || strcmp(buf, "btt\n") == 0)
1530
- ndns->claim_class = btt_claim_class(dev);
1531
- else if (strcmp(buf, "pfn") == 0 || strcmp(buf, "pfn\n") == 0)
1500
+ if (sysfs_streq(buf, "btt")) {
1501
+ int rc = btt_claim_class(dev);
1502
+
1503
+ if (rc < NVDIMM_CCLASS_NONE)
1504
+ return rc;
1505
+ ndns->claim_class = rc;
1506
+ } else if (sysfs_streq(buf, "pfn"))
15321507 ndns->claim_class = NVDIMM_CCLASS_PFN;
1533
- else if (strcmp(buf, "dax") == 0 || strcmp(buf, "dax\n") == 0)
1508
+ else if (sysfs_streq(buf, "dax"))
15341509 ndns->claim_class = NVDIMM_CCLASS_DAX;
1535
- else if (strcmp(buf, "") == 0 || strcmp(buf, "\n") == 0)
1510
+ else if (sysfs_streq(buf, ""))
15361511 ndns->claim_class = NVDIMM_CCLASS_NONE;
15371512 else
15381513 return -EINVAL;
1539
-
1540
- /* btt_claim_class() could've returned an error */
1541
- if (ndns->claim_class < 0)
1542
- return ndns->claim_class;
15431514
15441515 return 0;
15451516 }
....@@ -1548,17 +1519,17 @@
15481519 struct device_attribute *attr, const char *buf, size_t len)
15491520 {
15501521 struct nd_region *nd_region = to_nd_region(dev->parent);
1551
- ssize_t rc;
1522
+ int rc;
15521523
1553
- device_lock(dev);
1524
+ nd_device_lock(dev);
15541525 nvdimm_bus_lock(dev);
15551526 wait_nvdimm_bus_probe_idle(dev);
15561527 rc = __holder_class_store(dev, buf);
15571528 if (rc >= 0)
15581529 rc = nd_namespace_label_update(nd_region, dev);
1559
- dev_dbg(dev, "%s(%zd)\n", rc < 0 ? "fail " : "", rc);
1530
+ dev_dbg(dev, "%s(%d)\n", rc < 0 ? "fail " : "", rc);
15601531 nvdimm_bus_unlock(dev);
1561
- device_unlock(dev);
1532
+ nd_device_unlock(dev);
15621533
15631534 return rc < 0 ? rc : len;
15641535 }
....@@ -1569,7 +1540,7 @@
15691540 struct nd_namespace_common *ndns = to_ndns(dev);
15701541 ssize_t rc;
15711542
1572
- device_lock(dev);
1543
+ nd_device_lock(dev);
15731544 if (ndns->claim_class == NVDIMM_CCLASS_NONE)
15741545 rc = sprintf(buf, "\n");
15751546 else if ((ndns->claim_class == NVDIMM_CCLASS_BTT) ||
....@@ -1581,7 +1552,7 @@
15811552 rc = sprintf(buf, "dax\n");
15821553 else
15831554 rc = sprintf(buf, "<unknown>\n");
1584
- device_unlock(dev);
1555
+ nd_device_unlock(dev);
15851556
15861557 return rc;
15871558 }
....@@ -1595,7 +1566,7 @@
15951566 char *mode;
15961567 ssize_t rc;
15971568
1598
- device_lock(dev);
1569
+ nd_device_lock(dev);
15991570 claim = ndns->claim;
16001571 if (claim && is_nd_btt(claim))
16011572 mode = "safe";
....@@ -1608,7 +1579,7 @@
16081579 else
16091580 mode = "raw";
16101581 rc = sprintf(buf, "%s\n", mode);
1611
- device_unlock(dev);
1582
+ nd_device_unlock(dev);
16121583
16131584 return rc;
16141585 }
....@@ -1654,11 +1625,8 @@
16541625 {
16551626 struct device *dev = container_of(kobj, struct device, kobj);
16561627
1657
- if (a == &dev_attr_resource.attr) {
1658
- if (is_namespace_blk(dev))
1659
- return 0;
1660
- return 0400;
1661
- }
1628
+ if (a == &dev_attr_resource.attr && is_namespace_blk(dev))
1629
+ return 0;
16621630
16631631 if (is_namespace_pmem(dev) || is_namespace_blk(dev)) {
16641632 if (a == &dev_attr_size.attr)
....@@ -1667,11 +1635,11 @@
16671635 return a->mode;
16681636 }
16691637
1670
- if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr
1671
- || a == &dev_attr_holder.attr
1672
- || a == &dev_attr_holder_class.attr
1673
- || a == &dev_attr_force_raw.attr
1674
- || a == &dev_attr_mode.attr)
1638
+ /* base is_namespace_io() attributes */
1639
+ if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr ||
1640
+ a == &dev_attr_holder.attr || a == &dev_attr_holder_class.attr ||
1641
+ a == &dev_attr_force_raw.attr || a == &dev_attr_mode.attr ||
1642
+ a == &dev_attr_resource.attr)
16751643 return a->mode;
16761644
16771645 return 0;
....@@ -1688,6 +1656,39 @@
16881656 &nd_numa_attribute_group,
16891657 NULL,
16901658 };
1659
+
1660
+static const struct device_type namespace_io_device_type = {
1661
+ .name = "nd_namespace_io",
1662
+ .release = namespace_io_release,
1663
+ .groups = nd_namespace_attribute_groups,
1664
+};
1665
+
1666
+static const struct device_type namespace_pmem_device_type = {
1667
+ .name = "nd_namespace_pmem",
1668
+ .release = namespace_pmem_release,
1669
+ .groups = nd_namespace_attribute_groups,
1670
+};
1671
+
1672
+static const struct device_type namespace_blk_device_type = {
1673
+ .name = "nd_namespace_blk",
1674
+ .release = namespace_blk_release,
1675
+ .groups = nd_namespace_attribute_groups,
1676
+};
1677
+
1678
+static bool is_namespace_pmem(const struct device *dev)
1679
+{
1680
+ return dev ? dev->type == &namespace_pmem_device_type : false;
1681
+}
1682
+
1683
+static bool is_namespace_blk(const struct device *dev)
1684
+{
1685
+ return dev ? dev->type == &namespace_blk_device_type : false;
1686
+}
1687
+
1688
+static bool is_namespace_io(const struct device *dev)
1689
+{
1690
+ return dev ? dev->type == &namespace_io_device_type : false;
1691
+}
16911692
16921693 struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev)
16931694 {
....@@ -1712,8 +1713,8 @@
17121713 * Flush any in-progess probes / removals in the driver
17131714 * for the raw personality of this namespace.
17141715 */
1715
- device_lock(&ndns->dev);
1716
- device_unlock(&ndns->dev);
1716
+ nd_device_lock(&ndns->dev);
1717
+ nd_device_unlock(&ndns->dev);
17171718 if (ndns->dev.driver) {
17181719 dev_dbg(&ndns->dev, "is active, can't bind %s\n",
17191720 dev_name(dev));
....@@ -1744,6 +1745,22 @@
17441745 return ERR_PTR(-ENODEV);
17451746 }
17461747
1748
+ /*
1749
+ * Note, alignment validation for fsdax and devdax mode
1750
+ * namespaces happens in nd_pfn_validate() where infoblock
1751
+ * padding parameters can be applied.
1752
+ */
1753
+ if (pmem_should_map_pages(dev)) {
1754
+ struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
1755
+ struct resource *res = &nsio->res;
1756
+
1757
+ if (!IS_ALIGNED(res->start | (res->end + 1),
1758
+ memremap_compat_align())) {
1759
+ dev_err(&ndns->dev, "%pr misaligned, unable to map\n", res);
1760
+ return ERR_PTR(-EOPNOTSUPP);
1761
+ }
1762
+ }
1763
+
17471764 if (is_namespace_pmem(&ndns->dev)) {
17481765 struct nd_namespace_pmem *nspm;
17491766
....@@ -1767,6 +1784,23 @@
17671784 return ndns;
17681785 }
17691786 EXPORT_SYMBOL(nvdimm_namespace_common_probe);
1787
+
1788
+int devm_namespace_enable(struct device *dev, struct nd_namespace_common *ndns,
1789
+ resource_size_t size)
1790
+{
1791
+ if (is_namespace_blk(&ndns->dev))
1792
+ return 0;
1793
+ return devm_nsio_enable(dev, to_nd_namespace_io(&ndns->dev), size);
1794
+}
1795
+EXPORT_SYMBOL_GPL(devm_namespace_enable);
1796
+
1797
+void devm_namespace_disable(struct device *dev, struct nd_namespace_common *ndns)
1798
+{
1799
+ if (is_namespace_blk(&ndns->dev))
1800
+ return;
1801
+ devm_nsio_disable(dev, to_nd_namespace_io(&ndns->dev));
1802
+}
1803
+EXPORT_SYMBOL_GPL(devm_namespace_disable);
17701804
17711805 static struct device **create_namespace_io(struct nd_region *nd_region)
17721806 {
....@@ -1831,8 +1865,8 @@
18311865 && !guid_equal(&nd_set->type_guid,
18321866 &nd_label->type_guid)) {
18331867 dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n",
1834
- nd_set->type_guid.b,
1835
- nd_label->type_guid.b);
1868
+ &nd_set->type_guid,
1869
+ &nd_label->type_guid);
18361870 continue;
18371871 }
18381872
....@@ -2087,7 +2121,6 @@
20872121 }
20882122 dev_set_name(dev, "namespace%d.%d", nd_region->id, nsblk->id);
20892123 dev->parent = &nd_region->dev;
2090
- dev->groups = nd_namespace_attribute_groups;
20912124
20922125 return &nsblk->common.dev;
20932126 }
....@@ -2118,8 +2151,6 @@
21182151 return NULL;
21192152 }
21202153 dev_set_name(dev, "namespace%d.%d", nd_region->id, nspm->id);
2121
- dev->parent = &nd_region->dev;
2122
- dev->groups = nd_namespace_attribute_groups;
21232154 nd_namespace_pmem_set_resource(nd_region, nspm, 0);
21242155
21252156 return dev;
....@@ -2237,8 +2268,8 @@
22372268 if (namespace_label_has(ndd, type_guid)) {
22382269 if (!guid_equal(&nd_set->type_guid, &nd_label->type_guid)) {
22392270 dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n",
2240
- nd_set->type_guid.b,
2241
- nd_label->type_guid.b);
2271
+ &nd_set->type_guid,
2272
+ &nd_label->type_guid);
22422273 return ERR_PTR(-EAGAIN);
22432274 }
22442275
....@@ -2473,9 +2504,30 @@
24732504 return devs;
24742505 }
24752506
2507
+static void deactivate_labels(void *region)
2508
+{
2509
+ struct nd_region *nd_region = region;
2510
+ int i;
2511
+
2512
+ for (i = 0; i < nd_region->ndr_mappings; i++) {
2513
+ struct nd_mapping *nd_mapping = &nd_region->mapping[i];
2514
+ struct nvdimm_drvdata *ndd = nd_mapping->ndd;
2515
+ struct nvdimm *nvdimm = nd_mapping->nvdimm;
2516
+
2517
+ mutex_lock(&nd_mapping->lock);
2518
+ nd_mapping_free_labels(nd_mapping);
2519
+ mutex_unlock(&nd_mapping->lock);
2520
+
2521
+ put_ndd(ndd);
2522
+ nd_mapping->ndd = NULL;
2523
+ if (ndd)
2524
+ atomic_dec(&nvdimm->busy);
2525
+ }
2526
+}
2527
+
24762528 static int init_active_labels(struct nd_region *nd_region)
24772529 {
2478
- int i;
2530
+ int i, rc = 0;
24792531
24802532 for (i = 0; i < nd_region->ndr_mappings; i++) {
24812533 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
....@@ -2491,16 +2543,17 @@
24912543 if (!ndd) {
24922544 if (test_bit(NDD_LOCKED, &nvdimm->flags))
24932545 /* fail, label data may be unreadable */;
2494
- else if (test_bit(NDD_ALIASING, &nvdimm->flags))
2546
+ else if (test_bit(NDD_LABELING, &nvdimm->flags))
24952547 /* fail, labels needed to disambiguate dpa */;
24962548 else
2497
- return 0;
2549
+ continue;
24982550
24992551 dev_err(&nd_region->dev, "%s: is %s, failing probe\n",
25002552 dev_name(&nd_mapping->nvdimm->dev),
25012553 test_bit(NDD_LOCKED, &nvdimm->flags)
25022554 ? "locked" : "disabled");
2503
- return -ENXIO;
2555
+ rc = -ENXIO;
2556
+ goto out;
25042557 }
25052558 nd_mapping->ndd = ndd;
25062559 atomic_inc(&nvdimm->busy);
....@@ -2517,6 +2570,12 @@
25172570 if (!label_ent)
25182571 break;
25192572 label = nd_label_active(ndd, j);
2573
+ if (test_bit(NDD_NOBLK, &nvdimm->flags)) {
2574
+ u32 flags = __le32_to_cpu(label->flags);
2575
+
2576
+ flags &= ~NSLABEL_FLAG_LOCAL;
2577
+ label->flags = __cpu_to_le32(flags);
2578
+ }
25202579 label_ent->label = label;
25212580
25222581 mutex_lock(&nd_mapping->lock);
....@@ -2524,16 +2583,21 @@
25242583 mutex_unlock(&nd_mapping->lock);
25252584 }
25262585
2527
- if (j >= count)
2528
- continue;
2529
-
2530
- mutex_lock(&nd_mapping->lock);
2531
- nd_mapping_free_labels(nd_mapping);
2532
- mutex_unlock(&nd_mapping->lock);
2533
- return -ENOMEM;
2586
+ if (j < count)
2587
+ break;
25342588 }
25352589
2536
- return 0;
2590
+ if (i < nd_region->ndr_mappings)
2591
+ rc = -ENOMEM;
2592
+
2593
+out:
2594
+ if (rc) {
2595
+ deactivate_labels(nd_region);
2596
+ return rc;
2597
+ }
2598
+
2599
+ return devm_add_action_or_reset(&nd_region->dev, deactivate_labels,
2600
+ nd_region);
25372601 }
25382602
25392603 int nd_region_register_namespaces(struct nd_region *nd_region, int *err)
....@@ -2590,7 +2654,6 @@
25902654 if (id < 0)
25912655 break;
25922656 dev_set_name(dev, "namespace%d.%d", nd_region->id, id);
2593
- dev->groups = nd_namespace_attribute_groups;
25942657 nd_device_register(dev);
25952658 }
25962659 if (i)