Commit b95f5f43 authored by Dan Williams's avatar Dan Williams

libnvdimm: convert to statically allocated badblocks

If a device will ever have badblocks it should always have a badblocks
instance available.  So, similar to md, embed a badblocks instance in
pmem_device.  This reduces pointer chasing in the i/o fast path, and
simplifies the init path.
Reported-by: default avatarVishal Verma <vishal.l.verma@intel.com>
Signed-off-by: default avatarDan Williams <dan.j.williams@intel.com>
parent 87ba05df
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
* General Public License for more details. * General Public License for more details.
*/ */
#include <linux/libnvdimm.h> #include <linux/libnvdimm.h>
#include <linux/badblocks.h>
#include <linux/export.h> #include <linux/export.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/blkdev.h> #include <linux/blkdev.h>
...@@ -360,21 +361,19 @@ struct nvdimm_bus *__nvdimm_bus_register(struct device *parent, ...@@ -360,21 +361,19 @@ struct nvdimm_bus *__nvdimm_bus_register(struct device *parent,
} }
EXPORT_SYMBOL_GPL(__nvdimm_bus_register); EXPORT_SYMBOL_GPL(__nvdimm_bus_register);
static void set_badblock(struct gendisk *disk, sector_t s, int num) static void set_badblock(struct badblocks *bb, sector_t s, int num)
{ {
struct device *dev = disk->driverfs_dev; dev_dbg(bb->dev, "Found a poison range (0x%llx, 0x%llx)\n",
dev_dbg(dev, "Found a poison range (0x%llx, 0x%llx)\n",
(u64) s * 512, (u64) num * 512); (u64) s * 512, (u64) num * 512);
/* this isn't an error as the hardware will still throw an exception */ /* this isn't an error as the hardware will still throw an exception */
if (disk_set_badblocks(disk, s, num)) if (badblocks_set(bb, s, num, 1))
dev_info_once(dev, "%s: failed for sector %llx\n", dev_info_once(bb->dev, "%s: failed for sector %llx\n",
__func__, (u64) s); __func__, (u64) s);
} }
/** /**
* __add_badblock_range() - Convert a physical address range to bad sectors * __add_badblock_range() - Convert a physical address range to bad sectors
* @disk: the disk associated with the namespace * @bb: badblocks instance to populate
* @ns_offset: namespace offset where the error range begins (in bytes) * @ns_offset: namespace offset where the error range begins (in bytes)
* @len: number of bytes of poison to be added * @len: number of bytes of poison to be added
* *
...@@ -382,25 +381,18 @@ static void set_badblock(struct gendisk *disk, sector_t s, int num) ...@@ -382,25 +381,18 @@ static void set_badblock(struct gendisk *disk, sector_t s, int num)
* the bounds of physical addresses for this namespace, i.e. lies in the * the bounds of physical addresses for this namespace, i.e. lies in the
* interval [ns_start, ns_start + ns_size) * interval [ns_start, ns_start + ns_size)
*/ */
static int __add_badblock_range(struct gendisk *disk, u64 ns_offset, u64 len) static void __add_badblock_range(struct badblocks *bb, u64 ns_offset, u64 len)
{ {
unsigned int sector_size = queue_logical_block_size(disk->queue); const unsigned int sector_size = 512;
sector_t start_sector; sector_t start_sector;
u64 num_sectors; u64 num_sectors;
u32 rem; u32 rem;
int rc;
start_sector = div_u64(ns_offset, sector_size); start_sector = div_u64(ns_offset, sector_size);
num_sectors = div_u64_rem(len, sector_size, &rem); num_sectors = div_u64_rem(len, sector_size, &rem);
if (rem) if (rem)
num_sectors++; num_sectors++;
if (!disk->bb) {
rc = disk_alloc_badblocks(disk);
if (rc)
return rc;
}
if (unlikely(num_sectors > (u64)INT_MAX)) { if (unlikely(num_sectors > (u64)INT_MAX)) {
u64 remaining = num_sectors; u64 remaining = num_sectors;
sector_t s = start_sector; sector_t s = start_sector;
...@@ -408,33 +400,27 @@ static int __add_badblock_range(struct gendisk *disk, u64 ns_offset, u64 len) ...@@ -408,33 +400,27 @@ static int __add_badblock_range(struct gendisk *disk, u64 ns_offset, u64 len)
while (remaining) { while (remaining) {
int done = min_t(u64, remaining, INT_MAX); int done = min_t(u64, remaining, INT_MAX);
set_badblock(disk, s, done); set_badblock(bb, s, done);
remaining -= done; remaining -= done;
s += done; s += done;
} }
} else } else
set_badblock(disk, start_sector, num_sectors); set_badblock(bb, start_sector, num_sectors);
return 0;
} }
/** /**
* nvdimm_namespace_add_poison() - Convert a list of poison ranges to badblocks * nvdimm_namespace_add_poison() - Convert a list of poison ranges to badblocks
* @disk: the gendisk associated with the namespace where badblocks
* will be stored
* @offset: offset at the start of the namespace before 'sector 0'
* @ndns: the namespace containing poison ranges * @ndns: the namespace containing poison ranges
* @bb: badblocks instance to populate
* @offset: offset at the start of the namespace before 'sector 0'
* *
* The poison list generated during NFIT initialization may contain multiple, * The poison list generated during NFIT initialization may contain multiple,
* possibly overlapping ranges in the SPA (System Physical Address) space. * possibly overlapping ranges in the SPA (System Physical Address) space.
* Compare each of these ranges to the namespace currently being initialized, * Compare each of these ranges to the namespace currently being initialized,
* and add badblocks to the gendisk for all matching sub-ranges * and add badblocks to the gendisk for all matching sub-ranges
*
* Return:
* 0 - Success
*/ */
int nvdimm_namespace_add_poison(struct gendisk *disk, resource_size_t offset, void nvdimm_namespace_add_poison(struct nd_namespace_common *ndns,
struct nd_namespace_common *ndns) struct badblocks *bb, resource_size_t offset)
{ {
struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev); struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
struct nd_region *nd_region = to_nd_region(ndns->dev.parent); struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
...@@ -442,7 +428,6 @@ int nvdimm_namespace_add_poison(struct gendisk *disk, resource_size_t offset, ...@@ -442,7 +428,6 @@ int nvdimm_namespace_add_poison(struct gendisk *disk, resource_size_t offset,
struct list_head *poison_list; struct list_head *poison_list;
u64 ns_start, ns_end, ns_size; u64 ns_start, ns_end, ns_size;
struct nd_poison *pl; struct nd_poison *pl;
int rc;
ns_size = nvdimm_namespace_capacity(ndns) - offset; ns_size = nvdimm_namespace_capacity(ndns) - offset;
ns_start = nsio->res.start + offset; ns_start = nsio->res.start + offset;
...@@ -451,7 +436,7 @@ int nvdimm_namespace_add_poison(struct gendisk *disk, resource_size_t offset, ...@@ -451,7 +436,7 @@ int nvdimm_namespace_add_poison(struct gendisk *disk, resource_size_t offset,
nvdimm_bus = to_nvdimm_bus(nd_region->dev.parent); nvdimm_bus = to_nvdimm_bus(nd_region->dev.parent);
poison_list = &nvdimm_bus->poison_list; poison_list = &nvdimm_bus->poison_list;
if (list_empty(poison_list)) if (list_empty(poison_list))
return 0; return;
list_for_each_entry(pl, poison_list, list) { list_for_each_entry(pl, poison_list, list) {
u64 pl_end = pl->start + pl->length - 1; u64 pl_end = pl->start + pl->length - 1;
...@@ -470,10 +455,7 @@ int nvdimm_namespace_add_poison(struct gendisk *disk, resource_size_t offset, ...@@ -470,10 +455,7 @@ int nvdimm_namespace_add_poison(struct gendisk *disk, resource_size_t offset,
len = pl->length; len = pl->length;
else else
len = ns_start + ns_size - pl->start; len = ns_start + ns_size - pl->start;
__add_badblock_range(bb, start - ns_start, len);
rc = __add_badblock_range(disk, start - ns_start, len);
if (rc)
return rc;
continue; continue;
} }
/* Deal with overlap for poison starting before the namespace */ /* Deal with overlap for poison starting before the namespace */
...@@ -484,14 +466,9 @@ int nvdimm_namespace_add_poison(struct gendisk *disk, resource_size_t offset, ...@@ -484,14 +466,9 @@ int nvdimm_namespace_add_poison(struct gendisk *disk, resource_size_t offset,
len = pl->start + pl->length - ns_start; len = pl->start + pl->length - ns_start;
else else
len = ns_size; len = ns_size;
__add_badblock_range(bb, 0, len);
rc = __add_badblock_range(disk, 0, len);
if (rc)
return rc;
} }
} }
return 0;
} }
EXPORT_SYMBOL_GPL(nvdimm_namespace_add_poison); EXPORT_SYMBOL_GPL(nvdimm_namespace_add_poison);
......
...@@ -268,8 +268,8 @@ int nvdimm_namespace_attach_btt(struct nd_namespace_common *ndns); ...@@ -268,8 +268,8 @@ int nvdimm_namespace_attach_btt(struct nd_namespace_common *ndns);
int nvdimm_namespace_detach_btt(struct nd_namespace_common *ndns); int nvdimm_namespace_detach_btt(struct nd_namespace_common *ndns);
const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns, const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
char *name); char *name);
int nvdimm_namespace_add_poison(struct gendisk *disk, resource_size_t offset, void nvdimm_namespace_add_poison(struct nd_namespace_common *ndns,
struct nd_namespace_common *ndns); struct badblocks *bb, resource_size_t offset);
int nd_blk_region_init(struct nd_region *nd_region); int nd_blk_region_init(struct nd_region *nd_region);
void __nd_iostat_start(struct bio *bio, unsigned long *start); void __nd_iostat_start(struct bio *bio, unsigned long *start);
static inline bool nd_iostat_start(struct bio *bio, unsigned long *start) static inline bool nd_iostat_start(struct bio *bio, unsigned long *start)
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include <linux/module.h> #include <linux/module.h>
#include <linux/memory_hotplug.h> #include <linux/memory_hotplug.h>
#include <linux/moduleparam.h> #include <linux/moduleparam.h>
#include <linux/badblocks.h>
#include <linux/vmalloc.h> #include <linux/vmalloc.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/pmem.h> #include <linux/pmem.h>
...@@ -41,6 +42,7 @@ struct pmem_device { ...@@ -41,6 +42,7 @@ struct pmem_device {
phys_addr_t data_offset; phys_addr_t data_offset;
void __pmem *virt_addr; void __pmem *virt_addr;
size_t size; size_t size;
struct badblocks bb;
}; };
static int pmem_major; static int pmem_major;
...@@ -168,7 +170,6 @@ static int pmem_attach_disk(struct device *dev, ...@@ -168,7 +170,6 @@ static int pmem_attach_disk(struct device *dev,
{ {
int nid = dev_to_node(dev); int nid = dev_to_node(dev);
struct gendisk *disk; struct gendisk *disk;
int ret;
pmem->pmem_queue = blk_alloc_queue_node(GFP_KERNEL, nid); pmem->pmem_queue = blk_alloc_queue_node(GFP_KERNEL, nid);
if (!pmem->pmem_queue) if (!pmem->pmem_queue)
...@@ -196,10 +197,9 @@ static int pmem_attach_disk(struct device *dev, ...@@ -196,10 +197,9 @@ static int pmem_attach_disk(struct device *dev,
disk->driverfs_dev = dev; disk->driverfs_dev = dev;
set_capacity(disk, (pmem->size - pmem->data_offset) / 512); set_capacity(disk, (pmem->size - pmem->data_offset) / 512);
pmem->pmem_disk = disk; pmem->pmem_disk = disk;
if (devm_init_badblocks(dev, &pmem->bb))
ret = nvdimm_namespace_add_poison(disk, pmem->data_offset, ndns); return -ENOMEM;
if (ret) nvdimm_namespace_add_poison(ndns, &pmem->bb, pmem->data_offset);
return ret;
add_disk(disk); add_disk(disk);
revalidate_disk(disk); revalidate_disk(disk);
......
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