Skip to content

Commit

Permalink
libnvdimm: fix mishandled nvdimm_clear_poison() return value
Browse files Browse the repository at this point in the history
Colin, via static analysis, reports that the length could be negative
from nvdimm_clear_poison() in the error case. There was a similar
problem with commit 0a3f27b "libnvdimm, namespace: avoid multiple
sector calculations" that I noticed when merging the for-4.10/libnvdimm
topic branch into libnvdimm-for-next, but I missed this one. Fix both of
them to the following procedure:

* if we clear a block's worth of media, clear that many blocks in
  badblocks

* if we clear less than the requested size of the transfer return an
  error

* always invalidate cache after any non-error / non-zero
  nvdimm_clear_poison result

Fixes: 82bf103 ("libnvdimm: check and clear poison before writing to pmem")
Fixes: 0a3f27b ("libnvdimm, namespace: avoid multiple sector calculations")
Cc: Fabian Frederick <[email protected]>
Cc: Dave Jiang <[email protected]>
Reported-by: Colin Ian King <[email protected]>
Signed-off-by: Dan Williams <[email protected]>
  • Loading branch information
djbw committed Dec 16, 2016
1 parent 9cf8bd5 commit 868f036
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
9 changes: 5 additions & 4 deletions drivers/nvdimm/claim.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,13 @@ static int nsio_rw_bytes(struct nd_namespace_common *ndns,
long cleared;

cleared = nvdimm_clear_poison(&ndns->dev, offset, size);
if (cleared != size) {
size = cleared;
if (cleared < size)
rc = -EIO;
if (cleared > 0 && cleared / 512) {
cleared /= 512;
badblocks_clear(&nsio->bb, sector, cleared);
}

badblocks_clear(&nsio->bb, sector, cleared >> 9);
invalidate_pmem(nsio->addr + offset, size);
} else
rc = -EIO;
}
Expand Down
21 changes: 14 additions & 7 deletions drivers/nvdimm/pmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,29 @@ static struct nd_region *to_region(struct pmem_device *pmem)
return to_nd_region(to_dev(pmem)->parent);
}

static void pmem_clear_poison(struct pmem_device *pmem, phys_addr_t offset,
static int pmem_clear_poison(struct pmem_device *pmem, phys_addr_t offset,
unsigned int len)
{
struct device *dev = to_dev(pmem);
sector_t sector, cleared;
sector_t sector;
long cleared;
int rc = 0;

sector = (offset - pmem->data_offset) / 512;
cleared = nvdimm_clear_poison(dev, pmem->phys_addr + offset, len) / 512;

if (cleared) {
dev_dbg(dev, "%s: %#llx clear %ld sector%s\n",
__func__, (unsigned long long) sector,
cleared, cleared > 1 ? "s" : "");
cleared = nvdimm_clear_poison(dev, pmem->phys_addr + offset, len);
if (cleared < len)
rc = -EIO;
if (cleared > 0 && cleared / 512) {
cleared /= 512;
dev_dbg(dev, "%s: %#llx clear %ld sector%s\n", __func__,
(unsigned long long) sector, cleared,
cleared > 1 ? "s" : "");
badblocks_clear(&pmem->bb, sector, cleared);
}
invalidate_pmem(pmem->virt_addr + offset, len);

return rc;
}

static void write_pmem(void *pmem_addr, struct page *page,
Expand Down

0 comments on commit 868f036

Please sign in to comment.