Path: blob/master/arch/powerpc/platforms/powernv/memtrace.c
26481 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* Copyright (C) IBM Corporation, 2014, 20173* Anton Blanchard, Rashmica Gupta.4*/56#define pr_fmt(fmt) "memtrace: " fmt78#include <linux/bitops.h>9#include <linux/string.h>10#include <linux/memblock.h>11#include <linux/init.h>12#include <linux/moduleparam.h>13#include <linux/fs.h>14#include <linux/debugfs.h>15#include <linux/slab.h>16#include <linux/memory.h>17#include <linux/memory_hotplug.h>18#include <linux/numa.h>19#include <asm/machdep.h>20#include <asm/cacheflush.h>2122/* This enables us to keep track of the memory removed from each node. */23struct memtrace_entry {24void *mem;25u64 start;26u64 size;27u32 nid;28struct dentry *dir;29char name[16];30};3132static DEFINE_MUTEX(memtrace_mutex);33static u64 memtrace_size;3435static struct memtrace_entry *memtrace_array;36static unsigned int memtrace_array_nr;373839static ssize_t memtrace_read(struct file *filp, char __user *ubuf,40size_t count, loff_t *ppos)41{42struct memtrace_entry *ent = filp->private_data;4344return simple_read_from_buffer(ubuf, count, ppos, ent->mem, ent->size);45}4647static int memtrace_mmap(struct file *filp, struct vm_area_struct *vma)48{49struct memtrace_entry *ent = filp->private_data;50unsigned long ent_nrpages = ent->size >> PAGE_SHIFT;51unsigned long vma_nrpages = vma_pages(vma);5253/* The requested page offset should be within object's page count */54if (vma->vm_pgoff >= ent_nrpages)55return -EINVAL;5657/* The requested mapping range should remain within the bounds */58if (vma_nrpages > ent_nrpages - vma->vm_pgoff)59return -EINVAL;6061vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);62return remap_pfn_range(vma, vma->vm_start, PHYS_PFN(ent->start) + vma->vm_pgoff,63vma->vm_end - vma->vm_start, vma->vm_page_prot);64}6566static const struct file_operations memtrace_fops = {67.llseek = default_llseek,68.read = memtrace_read,69.open = simple_open,70.mmap = memtrace_mmap,71};7273#define FLUSH_CHUNK_SIZE SZ_1G74/**75* flush_dcache_range_chunked(): Write any modified data cache blocks out to76* memory and invalidate them, in chunks of up to FLUSH_CHUNK_SIZE77* Does not invalidate the corresponding instruction cache blocks.78*79* @start: the start address80* @stop: the stop address (exclusive)81* @chunk: the max size of the chunks82*/83static void flush_dcache_range_chunked(unsigned long start, unsigned long stop,84unsigned long chunk)85{86unsigned long i;8788for (i = start; i < stop; i += chunk) {89flush_dcache_range(i, min(stop, i + chunk));90cond_resched();91}92}9394static u64 memtrace_alloc_node(u32 nid, u64 size)95{96const unsigned long nr_pages = PHYS_PFN(size);97unsigned long pfn, start_pfn;98struct page *page;99100/*101* Trace memory needs to be aligned to the size, which is guaranteed102* by alloc_contig_pages().103*/104page = alloc_contig_pages(nr_pages, GFP_KERNEL | __GFP_THISNODE |105__GFP_NOWARN | __GFP_ZERO, nid, NULL);106if (!page)107return 0;108start_pfn = page_to_pfn(page);109110/*111* Before we go ahead and use this range as cache inhibited range112* flush the cache.113*/114flush_dcache_range_chunked((unsigned long)pfn_to_kaddr(start_pfn),115(unsigned long)pfn_to_kaddr(start_pfn + nr_pages),116FLUSH_CHUNK_SIZE);117118/*119* Set pages PageOffline(), to indicate that nobody (e.g., hibernation,120* dumping, ...) should be touching these pages.121*/122for (pfn = start_pfn; pfn < start_pfn + nr_pages; pfn++)123__SetPageOffline(pfn_to_page(pfn));124125arch_remove_linear_mapping(PFN_PHYS(start_pfn), size);126127return PFN_PHYS(start_pfn);128}129130static int memtrace_init_regions_runtime(u64 size)131{132u32 nid;133u64 m;134135memtrace_array = kcalloc(num_online_nodes(),136sizeof(struct memtrace_entry), GFP_KERNEL);137if (!memtrace_array) {138pr_err("Failed to allocate memtrace_array\n");139return -EINVAL;140}141142for_each_online_node(nid) {143m = memtrace_alloc_node(nid, size);144145/*146* A node might not have any local memory, so warn but147* continue on.148*/149if (!m) {150pr_err("Failed to allocate trace memory on node %d\n", nid);151continue;152}153154pr_info("Allocated trace memory on node %d at 0x%016llx\n", nid, m);155156memtrace_array[memtrace_array_nr].start = m;157memtrace_array[memtrace_array_nr].size = size;158memtrace_array[memtrace_array_nr].nid = nid;159memtrace_array_nr++;160}161162return 0;163}164165static struct dentry *memtrace_debugfs_dir;166167static int memtrace_init_debugfs(void)168{169int ret = 0;170int i;171172for (i = 0; i < memtrace_array_nr; i++) {173struct dentry *dir;174struct memtrace_entry *ent = &memtrace_array[i];175176ent->mem = ioremap(ent->start, ent->size);177/* Warn but continue on */178if (!ent->mem) {179pr_err("Failed to map trace memory at 0x%llx\n",180ent->start);181ret = -1;182continue;183}184185snprintf(ent->name, 16, "%08x", ent->nid);186dir = debugfs_create_dir(ent->name, memtrace_debugfs_dir);187188ent->dir = dir;189debugfs_create_file_unsafe("trace", 0600, dir, ent, &memtrace_fops);190debugfs_create_x64("start", 0400, dir, &ent->start);191debugfs_create_x64("size", 0400, dir, &ent->size);192}193194return ret;195}196197static int memtrace_free(int nid, u64 start, u64 size)198{199struct mhp_params params = { .pgprot = PAGE_KERNEL };200const unsigned long nr_pages = PHYS_PFN(size);201const unsigned long start_pfn = PHYS_PFN(start);202unsigned long pfn;203int ret;204205ret = arch_create_linear_mapping(nid, start, size, ¶ms);206if (ret)207return ret;208209for (pfn = start_pfn; pfn < start_pfn + nr_pages; pfn++)210__ClearPageOffline(pfn_to_page(pfn));211212free_contig_range(start_pfn, nr_pages);213return 0;214}215216/*217* Iterate through the chunks of memory we allocated and attempt to expose218* them back to the kernel.219*/220static int memtrace_free_regions(void)221{222int i, ret = 0;223struct memtrace_entry *ent;224225for (i = memtrace_array_nr - 1; i >= 0; i--) {226ent = &memtrace_array[i];227228/* We have freed this chunk previously */229if (ent->nid == NUMA_NO_NODE)230continue;231232/* Remove from io mappings */233if (ent->mem) {234iounmap(ent->mem);235ent->mem = 0;236}237238if (memtrace_free(ent->nid, ent->start, ent->size)) {239pr_err("Failed to free trace memory on node %d\n",240ent->nid);241ret += 1;242continue;243}244245/*246* Memory was freed successfully so clean up references to it247* so on reentry we can tell that this chunk was freed.248*/249debugfs_remove_recursive(ent->dir);250pr_info("Freed trace memory back on node %d\n", ent->nid);251ent->size = ent->start = ent->nid = NUMA_NO_NODE;252}253if (ret)254return ret;255256/* If all chunks of memory were freed successfully, reset globals */257kfree(memtrace_array);258memtrace_array = NULL;259memtrace_size = 0;260memtrace_array_nr = 0;261return 0;262}263264static int memtrace_enable_set(void *data, u64 val)265{266int rc = -EAGAIN;267u64 bytes;268269/*270* Don't attempt to do anything if size isn't aligned to a memory271* block or equal to zero.272*/273bytes = memory_block_size_bytes();274if (val & (bytes - 1)) {275pr_err("Value must be aligned with 0x%llx\n", bytes);276return -EINVAL;277}278279mutex_lock(&memtrace_mutex);280281/* Free all previously allocated memory. */282if (memtrace_size && memtrace_free_regions())283goto out_unlock;284285if (!val) {286rc = 0;287goto out_unlock;288}289290/* Allocate memory. */291if (memtrace_init_regions_runtime(val))292goto out_unlock;293294if (memtrace_init_debugfs())295goto out_unlock;296297memtrace_size = val;298rc = 0;299out_unlock:300mutex_unlock(&memtrace_mutex);301return rc;302}303304static int memtrace_enable_get(void *data, u64 *val)305{306*val = memtrace_size;307return 0;308}309310DEFINE_SIMPLE_ATTRIBUTE(memtrace_init_fops, memtrace_enable_get,311memtrace_enable_set, "0x%016llx\n");312313static int memtrace_init(void)314{315memtrace_debugfs_dir = debugfs_create_dir("memtrace",316arch_debugfs_dir);317318debugfs_create_file("enable", 0600, memtrace_debugfs_dir,319NULL, &memtrace_init_fops);320321return 0;322}323machine_device_initcall(powernv, memtrace_init);324325326