LCOV - code coverage report
Current view: top level - mm - mmu_gather.c (source / functions) Hit Total Coverage
Test: landlock.info Lines: 100 117 85.5 %
Date: 2021-04-22 12:43:58 Functions: 13 15 86.7 %

          Line data    Source code
       1             : #include <linux/gfp.h>
       2             : #include <linux/highmem.h>
       3             : #include <linux/kernel.h>
       4             : #include <linux/mmdebug.h>
       5             : #include <linux/mm_types.h>
       6             : #include <linux/pagemap.h>
       7             : #include <linux/rcupdate.h>
       8             : #include <linux/smp.h>
       9             : #include <linux/swap.h>
      10             : 
      11             : #include <asm/pgalloc.h>
      12             : #include <asm/tlb.h>
      13             : 
      14             : #ifndef CONFIG_MMU_GATHER_NO_GATHER
      15             : 
      16        3847 : static bool tlb_next_batch(struct mmu_gather *tlb)
      17             : {
      18        3847 :         struct mmu_gather_batch *batch;
      19             : 
      20        3847 :         batch = tlb->active;
      21        3847 :         if (batch->next) {
      22           0 :                 tlb->active = batch->next;
      23           0 :                 return true;
      24             :         }
      25             : 
      26        3847 :         if (tlb->batch_count == MAX_GATHER_BATCH_COUNT)
      27             :                 return false;
      28             : 
      29        3847 :         batch = (void *)__get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0);
      30        3847 :         if (!batch)
      31             :                 return false;
      32             : 
      33        3847 :         tlb->batch_count++;
      34        3847 :         batch->next = NULL;
      35        3847 :         batch->nr   = 0;
      36        3847 :         batch->max  = MAX_GATHER_BATCH;
      37             : 
      38        3847 :         tlb->active->next = batch;
      39        3847 :         tlb->active = batch;
      40             : 
      41        3847 :         return true;
      42             : }
      43             : 
      44       21544 : static void tlb_batch_pages_flush(struct mmu_gather *tlb)
      45             : {
      46       21544 :         struct mmu_gather_batch *batch;
      47             : 
      48       30610 :         for (batch = &tlb->local; batch && batch->nr; batch = batch->next) {
      49        9066 :                 free_pages_and_swap_cache(batch->pages, batch->nr);
      50        9066 :                 batch->nr = 0;
      51             :         }
      52       21544 :         tlb->active = &tlb->local;
      53       21544 : }
      54             : 
      55       21474 : static void tlb_batch_list_free(struct mmu_gather *tlb)
      56             : {
      57       21474 :         struct mmu_gather_batch *batch, *next;
      58             : 
      59       25320 :         for (batch = tlb->local.next; batch; batch = next) {
      60        3847 :                 next = batch->next;
      61        3847 :                 free_pages((unsigned long)batch, 0);
      62             :         }
      63       21473 :         tlb->local.next = NULL;
      64       21473 : }
      65             : 
      66      883525 : bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page, int page_size)
      67             : {
      68      883525 :         struct mmu_gather_batch *batch;
      69             : 
      70      883525 :         VM_BUG_ON(!tlb->end);
      71             : 
      72             : #ifdef CONFIG_MMU_GATHER_PAGE_SIZE
      73             :         VM_WARN_ON(tlb->page_size != page_size);
      74             : #endif
      75             : 
      76      883525 :         batch = tlb->active;
      77             :         /*
      78             :          * Add the page and check if we are full. If so
      79             :          * force a flush.
      80             :          */
      81      883525 :         batch->pages[batch->nr++] = page;
      82      883525 :         if (batch->nr == batch->max) {
      83        3847 :                 if (!tlb_next_batch(tlb))
      84             :                         return true;
      85        3847 :                 batch = tlb->active;
      86             :         }
      87      883525 :         VM_BUG_ON_PAGE(batch->nr > batch->max, page);
      88             : 
      89             :         return false;
      90             : }
      91             : 
      92             : #endif /* MMU_GATHER_NO_GATHER */
      93             : 
      94             : #ifdef CONFIG_MMU_GATHER_TABLE_FREE
      95             : 
      96        4739 : static void __tlb_remove_table_free(struct mmu_table_batch *batch)
      97             : {
      98        4739 :         int i;
      99             : 
     100       37802 :         for (i = 0; i < batch->nr; i++)
     101       33063 :                 __tlb_remove_table(batch->tables[i]);
     102             : 
     103        4739 :         free_page((unsigned long)batch);
     104        4739 : }
     105             : 
     106             : #ifdef CONFIG_MMU_GATHER_RCU_TABLE_FREE
     107             : 
     108             : /*
     109             :  * Semi RCU freeing of the page directories.
     110             :  *
     111             :  * This is needed by some architectures to implement software pagetable walkers.
     112             :  *
     113             :  * gup_fast() and other software pagetable walkers do a lockless page-table
     114             :  * walk and therefore needs some synchronization with the freeing of the page
     115             :  * directories. The chosen means to accomplish that is by disabling IRQs over
     116             :  * the walk.
     117             :  *
     118             :  * Architectures that use IPIs to flush TLBs will then automagically DTRT,
     119             :  * since we unlink the page, flush TLBs, free the page. Since the disabling of
     120             :  * IRQs delays the completion of the TLB flush we can never observe an already
     121             :  * freed page.
     122             :  *
     123             :  * Architectures that do not have this (PPC) need to delay the freeing by some
     124             :  * other means, this is that means.
     125             :  *
     126             :  * What we do is batch the freed directory pages (tables) and RCU free them.
     127             :  * We use the sched RCU variant, as that guarantees that IRQ/preempt disabling
     128             :  * holds off grace periods.
     129             :  *
     130             :  * However, in order to batch these pages we need to allocate storage, this
     131             :  * allocation is deep inside the MM code and can thus easily fail on memory
     132             :  * pressure. To guarantee progress we fall back to single table freeing, see
     133             :  * the implementation of tlb_remove_table_one().
     134             :  *
     135             :  */
     136             : 
     137           0 : static void tlb_remove_table_smp_sync(void *arg)
     138             : {
     139             :         /* Simply deliver the interrupt */
     140           0 : }
     141             : 
     142           0 : static void tlb_remove_table_sync_one(void)
     143             : {
     144             :         /*
     145             :          * This isn't an RCU grace period and hence the page-tables cannot be
     146             :          * assumed to be actually RCU-freed.
     147             :          *
     148             :          * It is however sufficient for software page-table walkers that rely on
     149             :          * IRQ disabling.
     150             :          */
     151           0 :         smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
     152             : }
     153             : 
     154        4739 : static void tlb_remove_table_rcu(struct rcu_head *head)
     155             : {
     156        4739 :         __tlb_remove_table_free(container_of(head, struct mmu_table_batch, rcu));
     157        4739 : }
     158             : 
     159        4746 : static void tlb_remove_table_free(struct mmu_table_batch *batch)
     160             : {
     161        4746 :         call_rcu(&batch->rcu, tlb_remove_table_rcu);
     162             : }
     163             : 
     164             : #else /* !CONFIG_MMU_GATHER_RCU_TABLE_FREE */
     165             : 
     166             : static void tlb_remove_table_sync_one(void) { }
     167             : 
     168             : static void tlb_remove_table_free(struct mmu_table_batch *batch)
     169             : {
     170             :         __tlb_remove_table_free(batch);
     171             : }
     172             : 
     173             : #endif /* CONFIG_MMU_GATHER_RCU_TABLE_FREE */
     174             : 
     175             : /*
     176             :  * If we want tlb_remove_table() to imply TLB invalidates.
     177             :  */
     178        4746 : static inline void tlb_table_invalidate(struct mmu_gather *tlb)
     179             : {
     180        4746 :         if (tlb_needs_table_invalidate()) {
     181             :                 /*
     182             :                  * Invalidate page-table caches used by hardware walkers. Then
     183             :                  * we still need to RCU-sched wait while freeing the pages
     184             :                  * because software walkers can still be in-flight.
     185             :                  */
     186        4746 :                 tlb_flush_mmu_tlbonly(tlb);
     187             :         }
     188             : }
     189             : 
     190           0 : static void tlb_remove_table_one(void *table)
     191             : {
     192           0 :         tlb_remove_table_sync_one();
     193           0 :         __tlb_remove_table(table);
     194           0 : }
     195             : 
     196       21544 : static void tlb_table_flush(struct mmu_gather *tlb)
     197             : {
     198       21544 :         struct mmu_table_batch **batch = &tlb->batch;
     199             : 
     200       21544 :         if (*batch) {
     201        4746 :                 tlb_table_invalidate(tlb);
     202        4746 :                 tlb_remove_table_free(*batch);
     203        4746 :                 *batch = NULL;
     204             :         }
     205       21544 : }
     206             : 
     207       33094 : void tlb_remove_table(struct mmu_gather *tlb, void *table)
     208             : {
     209       33094 :         struct mmu_table_batch **batch = &tlb->batch;
     210             : 
     211       33094 :         if (*batch == NULL) {
     212        4746 :                 *batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
     213        4746 :                 if (*batch == NULL) {
     214           0 :                         tlb_table_invalidate(tlb);
     215           0 :                         tlb_remove_table_one(table);
     216           0 :                         return;
     217             :                 }
     218        4746 :                 (*batch)->nr = 0;
     219             :         }
     220             : 
     221       33094 :         (*batch)->tables[(*batch)->nr++] = table;
     222       33094 :         if ((*batch)->nr == MAX_TABLE_BATCH)
     223           0 :                 tlb_table_flush(tlb);
     224             : }
     225             : 
     226       21474 : static inline void tlb_table_init(struct mmu_gather *tlb)
     227             : {
     228       21474 :         tlb->batch = NULL;
     229             : }
     230             : 
     231             : #else /* !CONFIG_MMU_GATHER_TABLE_FREE */
     232             : 
     233             : static inline void tlb_table_flush(struct mmu_gather *tlb) { }
     234             : static inline void tlb_table_init(struct mmu_gather *tlb) { }
     235             : 
     236             : #endif /* CONFIG_MMU_GATHER_TABLE_FREE */
     237             : 
     238       21544 : static void tlb_flush_mmu_free(struct mmu_gather *tlb)
     239             : {
     240       21544 :         tlb_table_flush(tlb);
     241             : #ifndef CONFIG_MMU_GATHER_NO_GATHER
     242       21544 :         tlb_batch_pages_flush(tlb);
     243             : #endif
     244             : }
     245             : 
     246       21544 : void tlb_flush_mmu(struct mmu_gather *tlb)
     247             : {
     248       21544 :         tlb_flush_mmu_tlbonly(tlb);
     249       21544 :         tlb_flush_mmu_free(tlb);
     250       21544 : }
     251             : 
     252             : /**
     253             :  * tlb_gather_mmu - initialize an mmu_gather structure for page-table tear-down
     254             :  * @tlb: the mmu_gather structure to initialize
     255             :  * @mm: the mm_struct of the target address space
     256             :  * @fullmm: @mm is without users and we're going to destroy the full address
     257             :  *          space (exit/execve)
     258             :  *
     259             :  * Called to initialize an (on-stack) mmu_gather structure for page-table
     260             :  * tear-down from @mm.
     261             :  */
     262       21474 : static void __tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm,
     263             :                              bool fullmm)
     264             : {
     265       21474 :         tlb->mm = mm;
     266       21474 :         tlb->fullmm = fullmm;
     267             : 
     268             : #ifndef CONFIG_MMU_GATHER_NO_GATHER
     269       21474 :         tlb->need_flush_all = 0;
     270       21474 :         tlb->local.next = NULL;
     271       21474 :         tlb->local.nr   = 0;
     272       21474 :         tlb->local.max  = ARRAY_SIZE(tlb->__pages);
     273       21474 :         tlb->active     = &tlb->local;
     274       21474 :         tlb->batch_count = 0;
     275             : #endif
     276             : 
     277       21474 :         tlb_table_init(tlb);
     278             : #ifdef CONFIG_MMU_GATHER_PAGE_SIZE
     279             :         tlb->page_size = 0;
     280             : #endif
     281             : 
     282       21474 :         __tlb_reset_range(tlb);
     283       21474 :         inc_tlb_flush_pending(tlb->mm);
     284       21474 : }
     285             : 
     286       17737 : void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm)
     287             : {
     288       17737 :         __tlb_gather_mmu(tlb, mm, false);
     289       17737 : }
     290             : 
     291        3737 : void tlb_gather_mmu_fullmm(struct mmu_gather *tlb, struct mm_struct *mm)
     292             : {
     293        3737 :         __tlb_gather_mmu(tlb, mm, true);
     294        3737 : }
     295             : 
     296             : /**
     297             :  * tlb_finish_mmu - finish an mmu_gather structure
     298             :  * @tlb: the mmu_gather structure to finish
     299             :  *
     300             :  * Called at the end of the shootdown operation to free up any resources that
     301             :  * were required.
     302             :  */
     303       21474 : void tlb_finish_mmu(struct mmu_gather *tlb)
     304             : {
     305             :         /*
     306             :          * If there are parallel threads are doing PTE changes on same range
     307             :          * under non-exclusive lock (e.g., mmap_lock read-side) but defer TLB
     308             :          * flush by batching, one thread may end up seeing inconsistent PTEs
     309             :          * and result in having stale TLB entries.  So flush TLB forcefully
     310             :          * if we detect parallel PTE batching threads.
     311             :          *
     312             :          * However, some syscalls, e.g. munmap(), may free page tables, this
     313             :          * needs force flush everything in the given range. Otherwise this
     314             :          * may result in having stale TLB entries for some architectures,
     315             :          * e.g. aarch64, that could specify flush what level TLB.
     316             :          */
     317       21474 :         if (mm_tlb_flush_nested(tlb->mm)) {
     318             :                 /*
     319             :                  * The aarch64 yields better performance with fullmm by
     320             :                  * avoiding multiple CPUs spamming TLBI messages at the
     321             :                  * same time.
     322             :                  *
     323             :                  * On x86 non-fullmm doesn't yield significant difference
     324             :                  * against fullmm.
     325             :                  */
     326           0 :                 tlb->fullmm = 1;
     327           0 :                 __tlb_reset_range(tlb);
     328           0 :                 tlb->freed_tables = 1;
     329             :         }
     330             : 
     331       21474 :         tlb_flush_mmu(tlb);
     332             : 
     333             : #ifndef CONFIG_MMU_GATHER_NO_GATHER
     334       21473 :         tlb_batch_list_free(tlb);
     335             : #endif
     336       21473 :         dec_tlb_flush_pending(tlb->mm);
     337       21474 : }

Generated by: LCOV version 1.14