Skip to content

Commit 6ff3e91

Browse files
committed
libnvdimm, namespace: sort namespaces by dpa at init
Add more determinism to initial namespace device-name assignments by sorting the namespaces by starting dpa. Signed-off-by: Dan Williams <dan.j.williams@intel.com>
1 parent 0e3b0d1 commit 6ff3e91

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

drivers/nvdimm/namespace_devs.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
#include <linux/module.h>
1414
#include <linux/device.h>
15+
#include <linux/sort.h>
1516
#include <linux/slab.h>
1617
#include <linux/pmem.h>
1718
#include <linux/list.h>
@@ -66,17 +67,17 @@ static struct device_type namespace_blk_device_type = {
6667
.release = namespace_blk_release,
6768
};
6869

69-
static bool is_namespace_pmem(struct device *dev)
70+
static bool is_namespace_pmem(const struct device *dev)
7071
{
7172
return dev ? dev->type == &namespace_pmem_device_type : false;
7273
}
7374

74-
static bool is_namespace_blk(struct device *dev)
75+
static bool is_namespace_blk(const struct device *dev)
7576
{
7677
return dev ? dev->type == &namespace_blk_device_type : false;
7778
}
7879

79-
static bool is_namespace_io(struct device *dev)
80+
static bool is_namespace_io(const struct device *dev)
8081
{
8182
return dev ? dev->type == &namespace_io_device_type : false;
8283
}
@@ -1919,6 +1920,31 @@ struct device *create_namespace_blk(struct nd_region *nd_region,
19191920
return ERR_PTR(-ENXIO);
19201921
}
19211922

1923+
static int cmp_dpa(const void *a, const void *b)
1924+
{
1925+
const struct device *dev_a = *(const struct device **) a;
1926+
const struct device *dev_b = *(const struct device **) b;
1927+
struct nd_namespace_blk *nsblk_a, *nsblk_b;
1928+
struct nd_namespace_pmem *nspm_a, *nspm_b;
1929+
1930+
if (is_namespace_io(dev_a))
1931+
return 0;
1932+
1933+
if (is_namespace_blk(dev_a)) {
1934+
nsblk_a = to_nd_namespace_blk(dev_a);
1935+
nsblk_b = to_nd_namespace_blk(dev_b);
1936+
1937+
return memcmp(&nsblk_a->res[0]->start, &nsblk_b->res[0]->start,
1938+
sizeof(resource_size_t));
1939+
}
1940+
1941+
nspm_a = to_nd_namespace_pmem(dev_a);
1942+
nspm_b = to_nd_namespace_pmem(dev_b);
1943+
1944+
return memcmp(&nspm_a->nsio.res.start, &nspm_b->nsio.res.start,
1945+
sizeof(resource_size_t));
1946+
}
1947+
19221948
static struct device **scan_labels(struct nd_region *nd_region)
19231949
{
19241950
struct nd_mapping *nd_mapping = &nd_region->mapping[0];
@@ -2034,6 +2060,9 @@ static struct device **scan_labels(struct nd_region *nd_region)
20342060
}
20352061
}
20362062

2063+
if (count > 1)
2064+
sort(devs, count, sizeof(struct device *), cmp_dpa, NULL);
2065+
20372066
return devs;
20382067

20392068
err:

include/linux/nd.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,19 @@ struct nd_namespace_blk {
107107
struct resource **res;
108108
};
109109

110-
static inline struct nd_namespace_io *to_nd_namespace_io(struct device *dev)
110+
static inline struct nd_namespace_io *to_nd_namespace_io(const struct device *dev)
111111
{
112112
return container_of(dev, struct nd_namespace_io, common.dev);
113113
}
114114

115-
static inline struct nd_namespace_pmem *to_nd_namespace_pmem(struct device *dev)
115+
static inline struct nd_namespace_pmem *to_nd_namespace_pmem(const struct device *dev)
116116
{
117117
struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
118118

119119
return container_of(nsio, struct nd_namespace_pmem, nsio);
120120
}
121121

122-
static inline struct nd_namespace_blk *to_nd_namespace_blk(struct device *dev)
122+
static inline struct nd_namespace_blk *to_nd_namespace_blk(const struct device *dev)
123123
{
124124
return container_of(dev, struct nd_namespace_blk, common.dev);
125125
}

0 commit comments

Comments
 (0)