Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mm, devm_memremap_pages: fix shutdown handling

The last step before devm_memremap_pages() returns success is to allocate
a release action, devm_memremap_pages_release(), to tear the entire setup
down. However, the result from devm_add_action() is not checked.

Checking the error from devm_add_action() is not enough. The api
currently relies on the fact that the percpu_ref it is using is killed by
the time the devm_memremap_pages_release() is run. Rather than continue
this awkward situation, offload the responsibility of killing the
percpu_ref to devm_memremap_pages_release() directly. This allows
devm_memremap_pages() to do the right thing relative to init failures and
shutdown.

Without this change we could fail to register the teardown of
devm_memremap_pages(). The likelihood of hitting this failure is tiny as
small memory allocations almost always succeed. However, the impact of
the failure is large given any future reconfiguration, or disable/enable,
of an nvdimm namespace will fail forever as subsequent calls to
devm_memremap_pages() will fail to setup the pgmap_radix since there will
be stale entries for the physical address range.

An argument could be made to require that the ->kill() operation be set in
the @pgmap arg rather than passed in separately. However, it helps code
readability, tracking the lifetime of a given instance, to be able to grep
the kill routine directly at the devm_memremap_pages() call site.

Link: http://lkml.kernel.org/r/154275558526.76910.7535251937849268605.stgit@dwillia2-desk3.amr.corp.intel.com
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
Fixes: e8d513483300 ("memremap: change devm_memremap_pages interface...")
Reviewed-by: "Jérôme Glisse" <jglisse@redhat.com>
Reported-by: Logan Gunthorpe <logang@deltatee.com>
Reviewed-by: Logan Gunthorpe <logang@deltatee.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Cc: Balbir Singh <bsingharora@gmail.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Dan Williams and committed by
Linus Torvalds
a95c90f1 06489cfb

+38 -36
+3 -11
drivers/dax/pmem.c
··· 48 48 percpu_ref_exit(ref); 49 49 } 50 50 51 - static void dax_pmem_percpu_kill(void *data) 51 + static void dax_pmem_percpu_kill(struct percpu_ref *ref) 52 52 { 53 - struct percpu_ref *ref = data; 54 53 struct dax_pmem *dax_pmem = to_dax_pmem(ref); 55 54 56 55 dev_dbg(dax_pmem->dev, "trace\n"); ··· 111 112 } 112 113 113 114 dax_pmem->pgmap.ref = &dax_pmem->ref; 115 + dax_pmem->pgmap.kill = dax_pmem_percpu_kill; 114 116 addr = devm_memremap_pages(dev, &dax_pmem->pgmap); 115 - if (IS_ERR(addr)) { 116 - devm_remove_action(dev, dax_pmem_percpu_exit, &dax_pmem->ref); 117 - percpu_ref_exit(&dax_pmem->ref); 117 + if (IS_ERR(addr)) 118 118 return PTR_ERR(addr); 119 - } 120 - 121 - rc = devm_add_action_or_reset(dev, dax_pmem_percpu_kill, 122 - &dax_pmem->ref); 123 - if (rc) 124 - return rc; 125 119 126 120 /* adjust the dax_region resource to the start of data */ 127 121 memcpy(&res, &dax_pmem->pgmap.res, sizeof(res));
+5 -8
drivers/nvdimm/pmem.c
··· 309 309 blk_cleanup_queue(q); 310 310 } 311 311 312 - static void pmem_freeze_queue(void *q) 312 + static void pmem_freeze_queue(struct percpu_ref *ref) 313 313 { 314 + struct request_queue *q; 315 + 316 + q = container_of(ref, typeof(*q), q_usage_counter); 314 317 blk_freeze_queue_start(q); 315 318 } 316 319 ··· 405 402 406 403 pmem->pfn_flags = PFN_DEV; 407 404 pmem->pgmap.ref = &q->q_usage_counter; 405 + pmem->pgmap.kill = pmem_freeze_queue; 408 406 if (is_nd_pfn(dev)) { 409 407 if (setup_pagemap_fsdax(dev, &pmem->pgmap)) 410 408 return -ENOMEM; ··· 430 426 pmem->size, ARCH_MEMREMAP_PMEM); 431 427 memcpy(&bb_res, &nsio->res, sizeof(bb_res)); 432 428 } 433 - 434 - /* 435 - * At release time the queue must be frozen before 436 - * devm_memremap_pages is unwound 437 - */ 438 - if (devm_add_action_or_reset(dev, pmem_freeze_queue, q)) 439 - return -ENOMEM; 440 429 441 430 if (IS_ERR(addr)) 442 431 return PTR_ERR(addr);
+2
include/linux/memremap.h
··· 111 111 * @altmap: pre-allocated/reserved memory for vmemmap allocations 112 112 * @res: physical address range covered by @ref 113 113 * @ref: reference count that pins the devm_memremap_pages() mapping 114 + * @kill: callback to transition @ref to the dead state 114 115 * @dev: host device of the mapping for debug 115 116 * @data: private data pointer for page_free() 116 117 * @type: memory type: see MEMORY_* in memory_hotplug.h ··· 123 122 bool altmap_valid; 124 123 struct resource res; 125 124 struct percpu_ref *ref; 125 + void (*kill)(struct percpu_ref *ref); 126 126 struct device *dev; 127 127 void *data; 128 128 enum memory_type type;
+14 -16
kernel/memremap.c
··· 88 88 resource_size_t align_start, align_size; 89 89 unsigned long pfn; 90 90 91 + pgmap->kill(pgmap->ref); 91 92 for_each_device_pfn(pfn, pgmap) 92 93 put_page(pfn_to_page(pfn)); 93 - 94 - if (percpu_ref_tryget_live(pgmap->ref)) { 95 - dev_WARN(dev, "%s: page mapping is still live!\n", __func__); 96 - percpu_ref_put(pgmap->ref); 97 - } 98 94 99 95 /* pages are dead and unused, undo the arch mapping */ 100 96 align_start = res->start & ~(SECTION_SIZE - 1); ··· 112 116 /** 113 117 * devm_memremap_pages - remap and provide memmap backing for the given resource 114 118 * @dev: hosting device for @res 115 - * @pgmap: pointer to a struct dev_pgmap 119 + * @pgmap: pointer to a struct dev_pagemap 116 120 * 117 121 * Notes: 118 122 * 1/ At a minimum the res, ref and type members of @pgmap must be initialized ··· 121 125 * 2/ The altmap field may optionally be initialized, in which case altmap_valid 122 126 * must be set to true 123 127 * 124 - * 3/ pgmap.ref must be 'live' on entry and 'dead' before devm_memunmap_pages() 125 - * time (or devm release event). The expected order of events is that ref has 126 - * been through percpu_ref_kill() before devm_memremap_pages_release(). The 127 - * wait for the completion of all references being dropped and 128 - * percpu_ref_exit() must occur after devm_memremap_pages_release(). 128 + * 3/ pgmap->ref must be 'live' on entry and will be killed at 129 + * devm_memremap_pages_release() time, or if this routine fails. 129 130 * 130 131 * 4/ res is expected to be a host memory range that could feasibly be 131 132 * treated as a "System RAM" range, i.e. not a device mmio range, but ··· 137 144 struct dev_pagemap *conflict_pgmap; 138 145 pgprot_t pgprot = PAGE_KERNEL; 139 146 int error, nid, is_ram; 147 + 148 + if (!pgmap->ref || !pgmap->kill) 149 + return ERR_PTR(-EINVAL); 140 150 141 151 align_start = res->start & ~(SECTION_SIZE - 1); 142 152 align_size = ALIGN(res->start + resource_size(res), SECTION_SIZE) ··· 166 170 if (is_ram != REGION_DISJOINT) { 167 171 WARN_ONCE(1, "%s attempted on %s region %pr\n", __func__, 168 172 is_ram == REGION_MIXED ? "mixed" : "ram", res); 169 - return ERR_PTR(-ENXIO); 173 + error = -ENXIO; 174 + goto err_array; 170 175 } 171 - 172 - if (!pgmap->ref) 173 - return ERR_PTR(-EINVAL); 174 176 175 177 pgmap->dev = dev; 176 178 ··· 211 217 align_size >> PAGE_SHIFT, pgmap); 212 218 percpu_ref_get_many(pgmap->ref, pfn_end(pgmap) - pfn_first(pgmap)); 213 219 214 - devm_add_action(dev, devm_memremap_pages_release, pgmap); 220 + error = devm_add_action_or_reset(dev, devm_memremap_pages_release, 221 + pgmap); 222 + if (error) 223 + return ERR_PTR(error); 215 224 216 225 return __va(res->start); 217 226 ··· 225 228 err_pfn_remap: 226 229 pgmap_array_delete(res); 227 230 err_array: 231 + pgmap->kill(pgmap->ref); 228 232 return ERR_PTR(error); 229 233 } 230 234 EXPORT_SYMBOL_GPL(devm_memremap_pages);
+14 -1
tools/testing/nvdimm/test/iomap.c
··· 104 104 } 105 105 EXPORT_SYMBOL(__wrap_devm_memremap); 106 106 107 + static void nfit_test_kill(void *_pgmap) 108 + { 109 + struct dev_pagemap *pgmap = _pgmap; 110 + 111 + pgmap->kill(pgmap->ref); 112 + } 113 + 107 114 void *__wrap_devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap) 108 115 { 109 116 resource_size_t offset = pgmap->res.start; 110 117 struct nfit_test_resource *nfit_res = get_nfit_res(offset); 111 118 112 - if (nfit_res) 119 + if (nfit_res) { 120 + int rc; 121 + 122 + rc = devm_add_action_or_reset(dev, nfit_test_kill, pgmap); 123 + if (rc) 124 + return ERR_PTR(rc); 113 125 return nfit_res->buf + offset - nfit_res->res.start; 126 + } 114 127 return devm_memremap_pages(dev, pgmap); 115 128 } 116 129 EXPORT_SYMBOL_GPL(__wrap_devm_memremap_pages);