cregit-Linux how code gets into the kernel

Release 4.11 arch/s390/mm/vmem.c

Directory: arch/s390/mm
/*
 *    Copyright IBM Corp. 2006
 *    Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
 */

#include <linux/bootmem.h>
#include <linux/pfn.h>
#include <linux/mm.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/hugetlb.h>
#include <linux/slab.h>
#include <linux/memblock.h>
#include <asm/cacheflush.h>
#include <asm/pgalloc.h>
#include <asm/pgtable.h>
#include <asm/setup.h>
#include <asm/tlbflush.h>
#include <asm/sections.h>

static DEFINE_MUTEX(vmem_mutex);


struct memory_segment {
	
struct list_head list;
	
unsigned long start;
	
unsigned long size;
};

static LIST_HEAD(mem_segs);


static void __ref *vmem_alloc_pages(unsigned int order) { unsigned long size = PAGE_SIZE << order; if (slab_is_available()) return (void *)__get_free_pages(GFP_KERNEL, order); return (void *) memblock_alloc(size, size); }

Contributors

PersonTokensPropCommitsCommitProp
Heiko Carstens49100.00%3100.00%
Total49100.00%3100.00%


static inline pud_t *vmem_pud_alloc(void) { pud_t *pud = NULL; pud = vmem_alloc_pages(2); if (!pud) return NULL; clear_table((unsigned long *) pud, _REGION3_ENTRY_EMPTY, PAGE_SIZE * 4); return pud; }

Contributors

PersonTokensPropCommitsCommitProp
Martin Schwidefsky3978.00%250.00%
Heiko Carstens1122.00%250.00%
Total50100.00%4100.00%


pmd_t *vmem_pmd_alloc(void) { pmd_t *pmd = NULL; pmd = vmem_alloc_pages(2); if (!pmd) return NULL; clear_table((unsigned long *) pmd, _SEGMENT_ENTRY_EMPTY, PAGE_SIZE * 4); return pmd; }

Contributors

PersonTokensPropCommitsCommitProp
Heiko Carstens3470.83%266.67%
Martin Schwidefsky1429.17%133.33%
Total48100.00%3100.00%


pte_t __ref *vmem_pte_alloc(void) { unsigned long size = PTRS_PER_PTE * sizeof(pte_t); pte_t *pte; if (slab_is_available()) pte = (pte_t *) page_table_alloc(&init_mm); else pte = (pte_t *) memblock_alloc(size, size); if (!pte) return NULL; clear_table((unsigned long *) pte, _PAGE_INVALID, size); return pte; }

Contributors

PersonTokensPropCommitsCommitProp
Heiko Carstens4657.50%450.00%
Martin Schwidefsky3341.25%337.50%
Philipp Hachtmann11.25%112.50%
Total80100.00%8100.00%

/* * Add a physical memory range to the 1:1 mapping. */
static int vmem_add_mem(unsigned long start, unsigned long size) { unsigned long pgt_prot, sgt_prot, r3_prot; unsigned long pages4k, pages1m, pages2g; unsigned long end = start + size; unsigned long address = start; pgd_t *pg_dir; pud_t *pu_dir; pmd_t *pm_dir; pte_t *pt_dir; int ret = -ENOMEM; pgt_prot = pgprot_val(PAGE_KERNEL); sgt_prot = pgprot_val(SEGMENT_KERNEL); r3_prot = pgprot_val(REGION3_KERNEL); if (!MACHINE_HAS_NX) { pgt_prot &= ~_PAGE_NOEXEC; sgt_prot &= ~_SEGMENT_ENTRY_NOEXEC; r3_prot &= ~_REGION_ENTRY_NOEXEC; } pages4k = pages1m = pages2g = 0; while (address < end) { pg_dir = pgd_offset_k(address); if (pgd_none(*pg_dir)) { pu_dir = vmem_pud_alloc(); if (!pu_dir) goto out; pgd_populate(&init_mm, pg_dir, pu_dir); } pu_dir = pud_offset(pg_dir, address); if (MACHINE_HAS_EDAT2 && pud_none(*pu_dir) && address && !(address & ~PUD_MASK) && (address + PUD_SIZE <= end) && !debug_pagealloc_enabled()) { pud_val(*pu_dir) = address | r3_prot; address += PUD_SIZE; pages2g++; continue; } if (pud_none(*pu_dir)) { pm_dir = vmem_pmd_alloc(); if (!pm_dir) goto out; pud_populate(&init_mm, pu_dir, pm_dir); } pm_dir = pmd_offset(pu_dir, address); if (MACHINE_HAS_EDAT1 && pmd_none(*pm_dir) && address && !(address & ~PMD_MASK) && (address + PMD_SIZE <= end) && !debug_pagealloc_enabled()) { pmd_val(*pm_dir) = address | sgt_prot; address += PMD_SIZE; pages1m++; continue; } if (pmd_none(*pm_dir)) { pt_dir = vmem_pte_alloc(); if (!pt_dir) goto out; pmd_populate(&init_mm, pm_dir, pt_dir); } pt_dir = pte_offset_kernel(pm_dir, address); pte_val(*pt_dir) = address | pgt_prot; address += PAGE_SIZE; pages4k++; } ret = 0; out: update_page_count(PG_DIRECT_MAP_4K, pages4k); update_page_count(PG_DIRECT_MAP_1M, pages1m); update_page_count(PG_DIRECT_MAP_2G, pages2g); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Heiko Carstens25762.84%750.00%
Martin Schwidefsky11327.63%428.57%
Gerald Schaefer317.58%214.29%
Christian Bornträger81.96%17.14%
Total409100.00%14100.00%

/* * Remove a physical memory range from the 1:1 mapping. * Currently only invalidates page table entries. */
static void vmem_remove_range(unsigned long start, unsigned long size) { unsigned long pages4k, pages1m, pages2g; unsigned long end = start + size; unsigned long address = start; pgd_t *pg_dir; pud_t *pu_dir; pmd_t *pm_dir; pte_t *pt_dir; pages4k = pages1m = pages2g = 0; while (address < end) { pg_dir = pgd_offset_k(address); if (pgd_none(*pg_dir)) { address += PGDIR_SIZE; continue; } pu_dir = pud_offset(pg_dir, address); if (pud_none(*pu_dir)) { address += PUD_SIZE; continue; } if (pud_large(*pu_dir)) { pud_clear(pu_dir); address += PUD_SIZE; pages2g++; continue; } pm_dir = pmd_offset(pu_dir, address); if (pmd_none(*pm_dir)) { address += PMD_SIZE; continue; } if (pmd_large(*pm_dir)) { pmd_clear(pm_dir); address += PMD_SIZE; pages1m++; continue; } pt_dir = pte_offset_kernel(pm_dir, address); pte_clear(&init_mm, address, pt_dir); address += PAGE_SIZE; pages4k++; } flush_tlb_kernel_range(start, end); update_page_count(PG_DIRECT_MAP_4K, -pages4k); update_page_count(PG_DIRECT_MAP_1M, -pages1m); update_page_count(PG_DIRECT_MAP_2G, -pages2g); }

Contributors

PersonTokensPropCommitsCommitProp
Heiko Carstens20785.89%666.67%
Gerald Schaefer177.05%111.11%
Martin Schwidefsky177.05%222.22%
Total241100.00%9100.00%

/* * Add a backed mem_map array to the virtual mem_map array. */
int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node) { unsigned long pgt_prot, sgt_prot; unsigned long address = start; pgd_t *pg_dir; pud_t *pu_dir; pmd_t *pm_dir; pte_t *pt_dir; int ret = -ENOMEM; pgt_prot = pgprot_val(PAGE_KERNEL); sgt_prot = pgprot_val(SEGMENT_KERNEL); if (!MACHINE_HAS_NX) { pgt_prot &= ~_PAGE_NOEXEC; sgt_prot &= ~_SEGMENT_ENTRY_NOEXEC; } for (address = start; address < end;) { pg_dir = pgd_offset_k(address); if (pgd_none(*pg_dir)) { pu_dir = vmem_pud_alloc(); if (!pu_dir) goto out; pgd_populate(&init_mm, pg_dir, pu_dir); } pu_dir = pud_offset(pg_dir, address); if (pud_none(*pu_dir)) { pm_dir = vmem_pmd_alloc(); if (!pm_dir) goto out; pud_populate(&init_mm, pu_dir, pm_dir); } pm_dir = pmd_offset(pu_dir, address); if (pmd_none(*pm_dir)) { /* Use 1MB frames for vmemmap if available. We always * use large frames even if they are only partially * used. * Otherwise we would have also page tables since * vmemmap_populate gets called for each section * separately. */ if (MACHINE_HAS_EDAT1) { void *new_page; new_page = vmemmap_alloc_block(PMD_SIZE, node); if (!new_page) goto out; pmd_val(*pm_dir) = __pa(new_page) | sgt_prot; address = (address + PMD_SIZE) & PMD_MASK; continue; } pt_dir = vmem_pte_alloc(); if (!pt_dir) goto out; pmd_populate(&init_mm, pm_dir, pt_dir); } else if (pmd_large(*pm_dir)) { address = (address + PMD_SIZE) & PMD_MASK; continue; } pt_dir = pte_offset_kernel(pm_dir, address); if (pte_none(*pt_dir)) { void *new_page; new_page = vmemmap_alloc_block(PAGE_SIZE, node); if (!new_page) goto out; pte_val(*pt_dir) = __pa(new_page) | pgt_prot; } address += PAGE_SIZE; } ret = 0; out: return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Heiko Carstens25169.92%541.67%
Martin Schwidefsky9927.58%541.67%
Johannes Weiner71.95%18.33%
Gerald Schaefer20.56%18.33%
Total359100.00%12100.00%


void vmemmap_free(unsigned long start, unsigned long end) { }

Contributors

PersonTokensPropCommitsCommitProp
Tang Chen866.67%150.00%
Johannes Weiner433.33%150.00%
Total12100.00%2100.00%

/* * Add memory segment to the segment list if it doesn't overlap with * an already present segment. */
static int insert_memory_segment(struct memory_segment *seg) { struct memory_segment *tmp; if (seg->start + seg->size > VMEM_MAX_PHYS || seg->start + seg->size < seg->start) return -ERANGE; list_for_each_entry(tmp, &mem_segs, list) { if (seg->start >= tmp->start + tmp->size) continue; if (seg->start + seg->size <= tmp->start) continue; return -ENOSPC; } list_add(&seg->list, &mem_segs); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Heiko Carstens102100.00%3100.00%
Total102100.00%3100.00%

/* * Remove memory segment from the segment list. */
static void remove_memory_segment(struct memory_segment *seg) { list_del(&seg->list); }

Contributors

PersonTokensPropCommitsCommitProp
Heiko Carstens19100.00%1100.00%
Total19100.00%1100.00%


static void __remove_shared_memory(struct memory_segment *seg) { remove_memory_segment(seg); vmem_remove_range(seg->start, seg->size); }

Contributors

PersonTokensPropCommitsCommitProp
Heiko Carstens27100.00%1100.00%
Total27100.00%1100.00%


int vmem_remove_mapping(unsigned long start, unsigned long size) { struct memory_segment *seg; int ret; mutex_lock(&vmem_mutex); ret = -ENOENT; list_for_each_entry(seg, &mem_segs, list) { if (seg->start == start && seg->size == size) break; } if (seg->start != start || seg->size != size) goto out; ret = 0; __remove_shared_memory(seg); kfree(seg); out: mutex_unlock(&vmem_mutex); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Heiko Carstens99100.00%2100.00%
Total99100.00%2100.00%


int vmem_add_mapping(unsigned long start, unsigned long size) { struct memory_segment *seg; int ret; mutex_lock(&vmem_mutex); ret = -ENOMEM; seg = kzalloc(sizeof(*seg), GFP_KERNEL); if (!seg) goto out; seg->start = start; seg->size = size; ret = insert_memory_segment(seg); if (ret) goto out_free; ret = vmem_add_mem(start, size); if (ret) goto out_remove; goto out; out_remove: __remove_shared_memory(seg); out_free: kfree(seg); out: mutex_unlock(&vmem_mutex); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Heiko Carstens123100.00%2100.00%
Total123100.00%2100.00%

/* * map whole physical memory to virtual memory (identity mapping) * we reserve enough space in the vmalloc area for vmemmap to hotplug * additional memory segments. */
void __init vmem_map_init(void) { struct memblock_region *reg; for_each_memblock(memory, reg) vmem_add_mem(reg->base, reg->size); __set_memory((unsigned long) _stext, (_etext - _stext) >> PAGE_SHIFT, SET_MEMORY_RO | SET_MEMORY_X); __set_memory((unsigned long) _etext, (_eshared - _etext) >> PAGE_SHIFT, SET_MEMORY_RO); __set_memory((unsigned long) _sinittext, (_einittext - _sinittext) >> PAGE_SHIFT, SET_MEMORY_RO | SET_MEMORY_X); pr_info("Write protected kernel read-only data: %luk\n", (_eshared - _stext) >> 10); }

Contributors

PersonTokensPropCommitsCommitProp
Martin Schwidefsky5552.88%114.29%
Heiko Carstens3331.73%457.14%
Gerald Schaefer1110.58%114.29%
Philipp Hachtmann54.81%114.29%
Total104100.00%7100.00%

/* * Convert memblock.memory to a memory segment list so there is a single * list that contains all memory segments. */
static int __init vmem_convert_memory_chunk(void) { struct memblock_region *reg; struct memory_segment *seg; mutex_lock(&vmem_mutex); for_each_memblock(memory, reg) { seg = kzalloc(sizeof(*seg), GFP_KERNEL); if (!seg) panic("Out of memory...\n"); seg->start = reg->base; seg->size = reg->size; insert_memory_segment(seg); } mutex_unlock(&vmem_mutex); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Heiko Carstens7081.40%150.00%
Philipp Hachtmann1618.60%150.00%
Total86100.00%2100.00%

core_initcall(vmem_convert_memory_chunk);

Overall Contributors

PersonTokensPropCommitsCommitProp
Heiko Carstens139773.84%2155.26%
Martin Schwidefsky37019.56%821.05%
Gerald Schaefer673.54%25.26%
Philipp Hachtmann261.37%12.63%
Johannes Weiner110.58%12.63%
Christian Bornträger90.48%25.26%
Tang Chen80.42%12.63%
Tejun Heo30.16%12.63%
Paul Gortmaker10.05%12.63%
Total1892100.00%38100.00%
Directory: arch/s390/mm
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
Created with cregit.