Message ID | 1432852553-24865-2-git-send-email-ross.zwisler@linux.intel.com (mailing list archive) |
---|---|
State | Superseded |
Headers | show |
On Thu, May 28, 2015 at 3:35 PM, Ross Zwisler <ross.zwisler@linux.intel.com> wrote: > Even though we use ioremap_nocache() to map our persistent memory in the > pmem driver, the memory we are mapping behaves like normal memory and > not I/O memory in that it can be accessed using regular memcpy() > operations and doesn't need to go through memcpy_toio() and > memcpy_fromio(). Force casting the pointers received from > ioremap_nocache() and given to iounmap() gives us the correct behavior > and makes sparse happy. > > Signed-off-by: Ross Zwisler <ross.zwisler@linux.intel.com> > Cc: Dan Williams <dan.j.williams@intel.com> > Cc: linux-nvdimm@lists.01.org > --- > drivers/block/nd/pmem.c | 7 ++++--- > 1 file changed, 4 insertions(+), 3 deletions(-) > > diff --git a/drivers/block/nd/pmem.c b/drivers/block/nd/pmem.c > index 5e8c9c629f22..a8712e41e7f5 100644 > --- a/drivers/block/nd/pmem.c > +++ b/drivers/block/nd/pmem.c > @@ -163,7 +163,8 @@ static struct pmem_device *pmem_alloc(struct device *dev, struct resource *res, > * of the CPU caches in case of a crash. > */ > err = -ENOMEM; > - pmem->virt_addr = ioremap_nocache(pmem->phys_addr, pmem->size); > + pmem->virt_addr = (__force void *)ioremap_nocache(pmem->phys_addr, > + pmem->size); I think I'd rather see casting when ->virt_addr is used (the __io_virt() helper can be used to make this a tad cleaner), or provide ioremap apis that don't attach __iomem to their return value. Because in this and other cases ioremap() is being on non "i/o" memory.
On Thu, 2015-05-28 at 15:47 -0700, Dan Williams wrote: > On Thu, May 28, 2015 at 3:35 PM, Ross Zwisler > <ross.zwisler@linux.intel.com> wrote: > > Even though we use ioremap_nocache() to map our persistent memory > > in the > > pmem driver, the memory we are mapping behaves like normal memory > > and > > not I/O memory in that it can be accessed using regular memcpy() > > operations and doesn't need to go through memcpy_toio() and > > memcpy_fromio(). Force casting the pointers received from > > ioremap_nocache() and given to iounmap() gives us the correct > > behavior > > and makes sparse happy. > > > > Signed-off-by: Ross Zwisler <ross.zwisler@linux.intel.com> > > Cc: Dan Williams <dan.j.williams@intel.com> > > Cc: linux-nvdimm@lists.01.org > > --- > > drivers/block/nd/pmem.c | 7 ++++--- > > 1 file changed, 4 insertions(+), 3 deletions(-) > > > > diff --git a/drivers/block/nd/pmem.c b/drivers/block/nd/pmem.c > > index 5e8c9c629f22..a8712e41e7f5 100644 > > --- a/drivers/block/nd/pmem.c > > +++ b/drivers/block/nd/pmem.c > > @@ -163,7 +163,8 @@ static struct pmem_device *pmem_alloc(struct > > device *dev, struct resource *res, > > * of the CPU caches in case of a crash. > > */ > > err = -ENOMEM; > > - pmem->virt_addr = ioremap_nocache(pmem->phys_addr, pmem > > ->size); > > + pmem->virt_addr = (__force void *)ioremap_nocache(pmem > > ->phys_addr, > > + pmem->size); > > I think I'd rather see casting when ->virt_addr is used (the > __io_virt() helper can be used to make this a tad cleaner), or > provide > ioremap apis that don't attach __iomem to their return value. > Because > in this and other cases ioremap() is being on non "i/o" memory. The reason that I thought this was cleaner was that now when you look at the pmem->virt_addr definition it is just a clean void* with no annotations. This correctly describes the memory to the user (it's usable as regular memory, it's in the kernel address space, etc.). Having the pointer itself annotated with __iomem feels weird to me because a random well meaning user could incorrectly try to use it as I/O memory.
On Fri, May 29, 2015 at 4:39 AM, Ross Zwisler <zwisler@gmail.com> wrote: > On Thu, 2015-05-28 at 15:47 -0700, Dan Williams wrote: >> On Thu, May 28, 2015 at 3:35 PM, Ross Zwisler >> <ross.zwisler@linux.intel.com> wrote: >> > Even though we use ioremap_nocache() to map our persistent memory >> > in the >> > pmem driver, the memory we are mapping behaves like normal memory >> > and >> > not I/O memory in that it can be accessed using regular memcpy() >> > operations and doesn't need to go through memcpy_toio() and >> > memcpy_fromio(). Force casting the pointers received from >> > ioremap_nocache() and given to iounmap() gives us the correct >> > behavior >> > and makes sparse happy. >> > >> > Signed-off-by: Ross Zwisler <ross.zwisler@linux.intel.com> >> > Cc: Dan Williams <dan.j.williams@intel.com> >> > Cc: linux-nvdimm@lists.01.org >> > --- >> > drivers/block/nd/pmem.c | 7 ++++--- >> > 1 file changed, 4 insertions(+), 3 deletions(-) >> > >> > diff --git a/drivers/block/nd/pmem.c b/drivers/block/nd/pmem.c >> > index 5e8c9c629f22..a8712e41e7f5 100644 >> > --- a/drivers/block/nd/pmem.c >> > +++ b/drivers/block/nd/pmem.c >> > @@ -163,7 +163,8 @@ static struct pmem_device *pmem_alloc(struct >> > device *dev, struct resource *res, >> > * of the CPU caches in case of a crash. >> > */ >> > err = -ENOMEM; >> > - pmem->virt_addr = ioremap_nocache(pmem->phys_addr, pmem >> > ->size); >> > + pmem->virt_addr = (__force void *)ioremap_nocache(pmem >> > ->phys_addr, >> > + pmem->size); >> >> I think I'd rather see casting when ->virt_addr is used (the >> __io_virt() helper can be used to make this a tad cleaner), or >> provide >> ioremap apis that don't attach __iomem to their return value. >> Because >> in this and other cases ioremap() is being on non "i/o" memory. > > The reason that I thought this was cleaner was that now when you look > at the pmem->virt_addr definition it is just a clean void* with no > annotations. This correctly describes the memory to the user (it's > usable as regular memory, it's in the kernel address space, etc.). > > Having the pointer itself annotated with __iomem feels weird to me > because a random well meaning user could incorrectly try to use it as > I/O memory. pmem->virt_addr does not leak outside the driver to random well meaning users. I think we have two options, provide physical address remap helpers from the outset (memremap()?) that don't attach __iomem, or put the casts on the non-iomem usages.
On Fri, May 29, 2015 at 5:53 AM, Dan Williams <dan.j.williams@intel.com> wrote: > On Fri, May 29, 2015 at 4:39 AM, Ross Zwisler <zwisler@gmail.com> wrote: >> On Thu, 2015-05-28 at 15:47 -0700, Dan Williams wrote: >>> On Thu, May 28, 2015 at 3:35 PM, Ross Zwisler >>> <ross.zwisler@linux.intel.com> wrote: >>> > Even though we use ioremap_nocache() to map our persistent memory >>> > in the >>> > pmem driver, the memory we are mapping behaves like normal memory >>> > and >>> > not I/O memory in that it can be accessed using regular memcpy() >>> > operations and doesn't need to go through memcpy_toio() and >>> > memcpy_fromio(). Force casting the pointers received from >>> > ioremap_nocache() and given to iounmap() gives us the correct >>> > behavior >>> > and makes sparse happy. >>> > >>> > Signed-off-by: Ross Zwisler <ross.zwisler@linux.intel.com> >>> > Cc: Dan Williams <dan.j.williams@intel.com> >>> > Cc: linux-nvdimm@lists.01.org >>> > --- >>> > drivers/block/nd/pmem.c | 7 ++++--- >>> > 1 file changed, 4 insertions(+), 3 deletions(-) >>> > >>> > diff --git a/drivers/block/nd/pmem.c b/drivers/block/nd/pmem.c >>> > index 5e8c9c629f22..a8712e41e7f5 100644 >>> > --- a/drivers/block/nd/pmem.c >>> > +++ b/drivers/block/nd/pmem.c >>> > @@ -163,7 +163,8 @@ static struct pmem_device *pmem_alloc(struct >>> > device *dev, struct resource *res, >>> > * of the CPU caches in case of a crash. >>> > */ >>> > err = -ENOMEM; >>> > - pmem->virt_addr = ioremap_nocache(pmem->phys_addr, pmem >>> > ->size); >>> > + pmem->virt_addr = (__force void *)ioremap_nocache(pmem >>> > ->phys_addr, >>> > + pmem->size); >>> >>> I think I'd rather see casting when ->virt_addr is used (the >>> __io_virt() helper can be used to make this a tad cleaner), or >>> provide >>> ioremap apis that don't attach __iomem to their return value. >>> Because >>> in this and other cases ioremap() is being on non "i/o" memory. >> >> The reason that I thought this was cleaner was that now when you look >> at the pmem->virt_addr definition it is just a clean void* with no >> annotations. This correctly describes the memory to the user (it's >> usable as regular memory, it's in the kernel address space, etc.). >> >> Having the pointer itself annotated with __iomem feels weird to me >> because a random well meaning user could incorrectly try to use it as >> I/O memory. > > pmem->virt_addr does not leak outside the driver to random well > meaning users. I think we have two options, provide physical address > remap helpers from the outset (memremap()?) that don't attach __iomem, > or put the casts on the non-iomem usages. In other words, I don't like the fact that we apply a coarse / private hack to fix a general kernel mis-annotation problem. Either fix it for everybody with something like memremap() or use __io_virt() to document the non-iomem usages of pmem->virt_addr like the other iomem-to-undecorated pointer helper routines in the kernel.
diff --git a/drivers/block/nd/pmem.c b/drivers/block/nd/pmem.c index 5e8c9c629f22..a8712e41e7f5 100644 --- a/drivers/block/nd/pmem.c +++ b/drivers/block/nd/pmem.c @@ -163,7 +163,8 @@ static struct pmem_device *pmem_alloc(struct device *dev, struct resource *res, * of the CPU caches in case of a crash. */ err = -ENOMEM; - pmem->virt_addr = ioremap_nocache(pmem->phys_addr, pmem->size); + pmem->virt_addr = (__force void *)ioremap_nocache(pmem->phys_addr, + pmem->size); if (!pmem->virt_addr) goto out_release_region; @@ -195,7 +196,7 @@ static struct pmem_device *pmem_alloc(struct device *dev, struct resource *res, out_free_queue: blk_cleanup_queue(pmem->pmem_queue); out_unmap: - iounmap(pmem->virt_addr); + iounmap((__force void __iomem *)pmem->virt_addr); out_release_region: release_mem_region(pmem->phys_addr, pmem->size); out_free_dev: @@ -209,7 +210,7 @@ static void pmem_free(struct pmem_device *pmem) del_gendisk(pmem->pmem_disk); put_disk(pmem->pmem_disk); blk_cleanup_queue(pmem->pmem_queue); - iounmap(pmem->virt_addr); + iounmap((__force void __iomem *)pmem->virt_addr); release_mem_region(pmem->phys_addr, pmem->size); kfree(pmem); }
Even though we use ioremap_nocache() to map our persistent memory in the pmem driver, the memory we are mapping behaves like normal memory and not I/O memory in that it can be accessed using regular memcpy() operations and doesn't need to go through memcpy_toio() and memcpy_fromio(). Force casting the pointers received from ioremap_nocache() and given to iounmap() gives us the correct behavior and makes sparse happy. Signed-off-by: Ross Zwisler <ross.zwisler@linux.intel.com> Cc: Dan Williams <dan.j.williams@intel.com> Cc: linux-nvdimm@lists.01.org --- drivers/block/nd/pmem.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)