History

memremap: replace the altmap_valid field with a PGMAP_ALTMAP_VALID flag

Add a flags field to struct dev_pagemap to replace the altmap_valid boolean to be a little more extensible. Also add a pgmap_altmap() helper to find the optional altmap and clean up the code using the altmap using it.

Christoph Hellwig committed 7 years ago

Commit 514caf21 parent 80a72d09 files · +29 −43Browse files

Unified · Split

9 files
  1. arch/powerpc/mm/mem.c +1 −9
  2. arch/x86/mm/init_64.c +2 −6
  3. drivers/nvdimm/pfn_devs.c +1 −2
  4. drivers/nvdimm/pmem.c +0 −1
  5. include/linux/memremap.h +11 −1
  6. kernel/memremap.c +10 −16
  7. mm/hmm.c +0 −1
  8. mm/memory_hotplug.c +2 −4
  9. mm/page_alloc.c +2 −3
arch/powerpc/mm/mem.c+1−9
@@ -131,17 +131,9 @@ void __ref arch_remove_memory(int nid, u64 start, u64 size, · 130 unmodified lines
131131 {
132132 unsigned long start_pfn = start >> PAGE_SHIFT;
133133 unsigned long nr_pages = size >> PAGE_SHIFT;
134− struct page *page;
134+ struct page *page = pfn_to_page(start_pfn) + vmem_altmap_offset(altmap);
135135 int ret;
136136
137− /*
138− * If we have an altmap then we need to skip over any reserved PFNs
139− * when querying the zone.
140− */
141− page = pfn_to_page(start_pfn);
142− if (altmap)
143− page += vmem_altmap_offset(altmap);
144−
145137 __remove_pages(page_zone(page), start_pfn, nr_pages, altmap);
146138
147139 /* Remove htab bolted mappings for this section of memory */
arch/x86/mm/init_64.c+2−6
@@ -1213,13 +1213,9 @@ void __ref arch_remove_memory(int nid, u64 start, u64 size, · 1212 unmodified lines
12131213 {
12141214 unsigned long start_pfn = start >> PAGE_SHIFT;
12151215 unsigned long nr_pages = size >> PAGE_SHIFT;
1216− struct page *page = pfn_to_page(start_pfn);
1217− struct zone *zone;
1216+ struct page *page = pfn_to_page(start_pfn) + vmem_altmap_offset(altmap);
1217+ struct zone *zone = page_zone(page);
12181218
1219− /* With altmap the first mapped page is offset from @start */
1220− if (altmap)
1221− page += vmem_altmap_offset(altmap);
1222− zone = page_zone(page);
12231219 __remove_pages(zone, start_pfn, nr_pages, altmap);
12241220 kernel_physical_mapping_remove(start, start + size);
12251221 }
drivers/nvdimm/pfn_devs.c+1−2
@@ -622,7 +622,6 @@ static int __nvdimm_setup_pfn(struct nd_pfn *nd_pfn, struct dev_pagemap *pgmap) · 621 unmodified lines
622622 if (offset < reserve)
623623 return -EINVAL;
624624 nd_pfn->npfns = le64_to_cpu(pfn_sb->npfns);
625− pgmap->altmap_valid = false;
626625 } else if (nd_pfn->mode == PFN_MODE_PMEM) {
627626 nd_pfn->npfns = PFN_SECTION_ALIGN_UP((resource_size(res)
628627 - offset) / PAGE_SIZE);
@@ -634,7 +633,7 @@ static int __nvdimm_setup_pfn(struct nd_pfn *nd_pfn, struct dev_pagemap *pgmap) · 5 unmodified lines
634633 memcpy(altmap, &__altmap, sizeof(*altmap));
635634 altmap->free = PHYS_PFN(offset - reserve);
636635 altmap->alloc = 0;
637− pgmap->altmap_valid = true;
636+ pgmap->flags |= PGMAP_ALTMAP_VALID;
638637 } else
639638 return -ENXIO;
640639
drivers/nvdimm/pmem.c+0−1
@@ -412,7 +412,6 @@ static int pmem_attach_disk(struct device *dev, · 411 unmodified lines
412412 bb_res.start += pmem->data_offset;
413413 } else if (pmem_should_map_pages(dev)) {
414414 memcpy(&pmem->pgmap.res, &nsio->res, sizeof(pmem->pgmap.res));
415− pmem->pgmap.altmap_valid = false;
416415 pmem->pgmap.type = MEMORY_DEVICE_FS_DAX;
417416 pmem->pgmap.ops = &fsdax_pagemap_ops;
418417 addr = devm_memremap_pages(dev, &pmem->pgmap);
include/linux/memremap.h+11−1
@@ -88,6 +88,8 @@ struct dev_pagemap_ops { · 87 unmodified lines
8888 vm_fault_t (*migrate_to_ram)(struct vm_fault *vmf);
8989 };
9090
91+#define PGMAP_ALTMAP_VALID (1 << 0)
92+
9193 /**
9294 * struct dev_pagemap - metadata for ZONE_DEVICE mappings
9395 * @altmap: pre-allocated/reserved memory for vmemmap allocations
@@ -96,19 +98,27 @@ struct dev_pagemap_ops { · 2 unmodified lines
9698 * @dev: host device of the mapping for debug
9799 * @data: private data pointer for page_free()
98100 * @type: memory type: see MEMORY_* in memory_hotplug.h
101+ * @flags: PGMAP_* flags to specify defailed behavior
99102 * @ops: method table
100103 */
101104 struct dev_pagemap {
102105 struct vmem_altmap altmap;
103− bool altmap_valid;
104106 struct resource res;
105107 struct percpu_ref *ref;
106108 struct device *dev;
107109 enum memory_type type;
110+ unsigned int flags;
108111 u64 pci_p2pdma_bus_offset;
109112 const struct dev_pagemap_ops *ops;
110113 };
111114
115+static inline struct vmem_altmap *pgmap_altmap(struct dev_pagemap *pgmap)
116+{
117+ if (pgmap->flags & PGMAP_ALTMAP_VALID)
118+ return &pgmap->altmap;
119+ return NULL;
120+}
121+
112122 #ifdef CONFIG_ZONE_DEVICE
113123 void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap);
114124 void devm_memunmap_pages(struct device *dev, struct dev_pagemap *pgmap);
kernel/memremap.c+10−16
@@ -54,14 +54,8 @@ static void pgmap_array_delete(struct resource *res) · 53 unmodified lines
5454
5555 static unsigned long pfn_first(struct dev_pagemap *pgmap)
5656 {
57− const struct resource *res = &pgmap->res;
58− struct vmem_altmap *altmap = &pgmap->altmap;
59− unsigned long pfn;
60−
61− pfn = res->start >> PAGE_SHIFT;
62− if (pgmap->altmap_valid)
63− pfn += vmem_altmap_offset(altmap);
64− return pfn;
57+ return (pgmap->res.start >> PAGE_SHIFT) +
58+ vmem_altmap_offset(pgmap_altmap(pgmap));
6559 }
6660
6761 static unsigned long pfn_end(struct dev_pagemap *pgmap)
@@ -109,7 +103,7 @@ static void devm_memremap_pages_release(void *data) · 41 unmodified lines
109103 align_size >> PAGE_SHIFT, NULL);
110104 } else {
111105 arch_remove_memory(nid, align_start, align_size,
112− pgmap->altmap_valid ? &pgmap->altmap : NULL);
106+ pgmap_altmap(pgmap));
113107 kasan_remove_zero_shadow(__va(align_start), align_size);
114108 }
115109 mem_hotplug_done();
@@ -129,8 +123,8 @@ static void devm_memremap_pages_release(void *data) · 13 unmodified lines
129123 * 1/ At a minimum the res, ref and type and ops members of @pgmap must be
130124 * initialized by the caller before passing it to this function
131125 *
132− * 2/ The altmap field may optionally be initialized, in which case altmap_valid
133− * must be set to true
126+ * 2/ The altmap field may optionally be initialized, in which case
127+ * PGMAP_ALTMAP_VALID must be set in pgmap->flags.
134128 *
135129 * 3/ pgmap->ref must be 'live' on entry and will be killed and reaped
136130 * at devm_memremap_pages_release() time, or if this routine fails.
@@ -142,15 +136,13 @@ static void devm_memremap_pages_release(void *data) · 5 unmodified lines
142136 void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
143137 {
144138 resource_size_t align_start, align_size, align_end;
145− struct vmem_altmap *altmap = pgmap->altmap_valid ?
146− &pgmap->altmap : NULL;
147139 struct resource *res = &pgmap->res;
148140 struct dev_pagemap *conflict_pgmap;
149141 struct mhp_restrictions restrictions = {
150142 /*
151143 * We do not want any optional features only our own memmap
152144 */
153− .altmap = altmap,
145+ .altmap = pgmap_altmap(pgmap),
154146 };
155147 pgprot_t pgprot = PAGE_KERNEL;
156148 int error, nid, is_ram;
@@ -274,7 +266,7 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap) · 117 unmodified lines
274266
275267 zone = &NODE_DATA(nid)->node_zones[ZONE_DEVICE];
276268 move_pfn_range_to_zone(zone, align_start >> PAGE_SHIFT,
277− align_size >> PAGE_SHIFT, altmap);
269+ align_size >> PAGE_SHIFT, pgmap_altmap(pgmap));
278270 }
279271
280272 mem_hotplug_done();
@@ -319,7 +311,9 @@ EXPORT_SYMBOL_GPL(devm_memunmap_pages); · 38 unmodified lines
319311 unsigned long vmem_altmap_offset(struct vmem_altmap *altmap)
320312 {
321313 /* number of pfns from base where pfn_to_page() is valid */
322− return altmap->reserve + altmap->free;
314+ if (altmap)
315+ return altmap->reserve + altmap->free;
316+ return 0;
323317 }
324318
325319 void vmem_altmap_free(struct vmem_altmap *altmap, unsigned long nr_pfns)
mm/hmm.c+0−1
@@ -1442,7 +1442,6 @@ struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops, · 1441 unmodified lines
14421442 devmem->pagemap.type = MEMORY_DEVICE_PRIVATE;
14431443 devmem->pagemap.res = *devmem->resource;
14441444 devmem->pagemap.ops = &hmm_pagemap_ops;
1445− devmem->pagemap.altmap_valid = false;
14461445 devmem->pagemap.ref = &devmem->ref;
14471446
14481447 result = devm_memremap_pages(devmem->device, &devmem->pagemap);
mm/memory_hotplug.c+2−4
@@ -557,10 +557,8 @@ void __remove_pages(struct zone *zone, unsigned long phys_start_pfn, · 556 unmodified lines
557557 int sections_to_remove;
558558
559559 /* In the ZONE_DEVICE case device driver owns the memory region */
560− if (is_dev_zone(zone)) {
561− if (altmap)
562− map_offset = vmem_altmap_offset(altmap);
563− }
560+ if (is_dev_zone(zone))
561+ map_offset = vmem_altmap_offset(altmap);
564562
565563 clear_zone_contiguous(zone);
566564
mm/page_alloc.c+2−3
@@ -5853,6 +5853,7 @@ void __ref memmap_init_zone_device(struct zone *zone, · 5852 unmodified lines
58535853 {
58545854 unsigned long pfn, end_pfn = start_pfn + size;
58555855 struct pglist_data *pgdat = zone->zone_pgdat;
5856+ struct vmem_altmap *altmap = pgmap_altmap(pgmap);
58565857 unsigned long zone_idx = zone_idx(zone);
58575858 unsigned long start = jiffies;
58585859 int nid = pgdat->node_id;
@@ -5865,9 +5866,7 @@ void __ref memmap_init_zone_device(struct zone *zone, · 6 unmodified lines
58655866 * of the pages reserved for the memmap, so we can just jump to
58665867 * the end of that region and start processing the device pages.
58675868 */
5868− if (pgmap->altmap_valid) {
5869− struct vmem_altmap *altmap = &pgmap->altmap;
5870−
5869+ if (altmap) {
58715870 start_pfn = altmap->base_pfn + vmem_altmap_offset(altmap);
58725871 size = end_pfn - start_pfn;
58735872 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t