diff mbox

[3/7] struct page: add field for vm_struct

Message ID 20180228200620.30026-4-igor.stoppa@huawei.com (mailing list archive)
State New, archived
Headers show

Commit Message

Igor Stoppa Feb. 28, 2018, 8:06 p.m. UTC
When a page is used for virtual memory, it is often necessary to obtain
a handler to the corresponding vm_struct, which refers to the virtually
continuous area generated when invoking vmalloc.

The struct page has a "mapping" field, which can be re-used, to store a
pointer to the parent area.

This will avoid more expensive searches, later on.

Signed-off-by: Igor Stoppa <igor.stoppa@huawei.com>
---
 include/linux/mm_types.h | 1 +
 mm/vmalloc.c             | 2 ++
 2 files changed, 3 insertions(+)

Comments

kernel test robot March 3, 2018, 2:35 a.m. UTC | #1
Hi Igor,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on next-20180223]
[cannot apply to linus/master mmotm/master char-misc/char-misc-testing v4.16-rc3 v4.16-rc2 v4.16-rc1 v4.16-rc3]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Igor-Stoppa/mm-security-ro-protection-for-dynamic-data/20180302-232215
reproduce:
        # apt-get install sparse
        make ARCH=x86_64 allmodconfig
        make C=1 CF=-D__CHECK_ENDIAN__


sparse warnings: (new ones prefixed by >>)

   lib/genalloc.c:624:29: sparse: undefined identifier 'exit_test'
>> lib/genalloc.c:624:29: sparse: call with no type!
   In file included from arch/x86/include/asm/bug.h:83:0,
                    from include/linux/bug.h:5,
                    from include/linux/mmdebug.h:5,
                    from include/linux/gfp.h:5,
                    from include/linux/slab.h:15,
                    from lib/genalloc.c:99:
   lib/genalloc.c: In function 'gen_pool_free':
   lib/genalloc.c:616:10: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'struct gen_pool *' [-Wformat=]
             "Trying to free unallocated memory"
             ^
   include/asm-generic/bug.h:98:50: note: in definition of macro '__WARN_printf'
    #define __WARN_printf(arg...) do { __warn_printk(arg); __WARN(); } while (0)
                                                     ^~~
   lib/genalloc.c:615:5: note: in expansion of macro 'WARN'
        WARN(true,
        ^~~~
   lib/genalloc.c:617:23: note: format string is defined here
             " from pool %s", pool);
                         ~^
   In file included from include/asm-generic/bug.h:5:0,
                    from arch/x86/include/asm/bug.h:83,
                    from include/linux/bug.h:5,
                    from include/linux/mmdebug.h:5,
                    from include/linux/gfp.h:5,
                    from include/linux/slab.h:15,
                    from lib/genalloc.c:99:
   lib/genalloc.c:624:17: error: implicit declaration of function 'exit_test'; did you mean 'exit_sem'? [-Werror=implicit-function-declaration]
       if (unlikely(exit_test(boundary < 0))) {
                    ^
   include/linux/compiler.h:77:42: note: in definition of macro 'unlikely'
    # define unlikely(x) __builtin_expect(!!(x), 0)
                                             ^
   In file included from arch/x86/include/asm/bug.h:83:0,
                    from include/linux/bug.h:5,
                    from include/linux/mmdebug.h:5,
                    from include/linux/gfp.h:5,
                    from include/linux/slab.h:15,
                    from lib/genalloc.c:99:
   lib/genalloc.c:626:16: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'struct gen_pool *' [-Wformat=]
        WARN(true, "Corrupted pool %s", pool);
                   ^
   include/asm-generic/bug.h:98:50: note: in definition of macro '__WARN_printf'
    #define __WARN_printf(arg...) do { __warn_printk(arg); __WARN(); } while (0)
                                                     ^~~
   lib/genalloc.c:626:5: note: in expansion of macro 'WARN'
        WARN(true, "Corrupted pool %s", pool);
        ^~~~
   lib/genalloc.c:634:10: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'struct gen_pool *' [-Wformat=]
             "Size provided differs from size "
             ^
   include/asm-generic/bug.h:98:50: note: in definition of macro '__WARN_printf'
    #define __WARN_printf(arg...) do { __warn_printk(arg); __WARN(); } while (0)
                                                     ^~~
   lib/genalloc.c:633:5: note: in expansion of macro 'WARN'
        WARN(true,
        ^~~~
   lib/genalloc.c:635:31: note: format string is defined here
             "measured from pool %s", pool);
                                 ~^
   In file included from arch/x86/include/asm/bug.h:83:0,
                    from include/linux/bug.h:5,
                    from include/linux/mmdebug.h:5,
                    from include/linux/gfp.h:5,
                    from include/linux/slab.h:15,
                    from lib/genalloc.c:99:
   lib/genalloc.c:643:10: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'struct gen_pool *' [-Wformat=]
             "Unexpected bitmap collision while"
             ^
   include/asm-generic/bug.h:98:50: note: in definition of macro '__WARN_printf'
    #define __WARN_printf(arg...) do { __warn_printk(arg); __WARN(); } while (0)
                                                     ^~~
   lib/genalloc.c:642:5: note: in expansion of macro 'WARN'
        WARN(true,
        ^~~~
   lib/genalloc.c:644:36: note: format string is defined here
             " freeing memory in pool %s", pool);
                                      ~^
   cc1: some warnings being treated as errors

vim +624 lib/genalloc.c

929f9727 Dean Nelson    2006-06-23  609  
7f184275 Huang Ying     2011-07-13  610  	rcu_read_lock();
7f184275 Huang Ying     2011-07-13  611  	list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
674470d9 Joonyoung Shim 2013-09-11  612  		if (addr >= chunk->start_addr && addr <= chunk->end_addr) {
3406f868 Igor Stoppa    2018-02-28  613  			if (unlikely(addr + size - 1 > chunk->end_addr)) {
7f184275 Huang Ying     2011-07-13  614  				rcu_read_unlock();
3406f868 Igor Stoppa    2018-02-28  615  				WARN(true,
3406f868 Igor Stoppa    2018-02-28  616  				     "Trying to free unallocated memory"
3406f868 Igor Stoppa    2018-02-28  617  				     " from pool %s", pool);
7f184275 Huang Ying     2011-07-13  618  				return;
f14f75b8 Jes Sorensen   2005-06-21  619  			}
3406f868 Igor Stoppa    2018-02-28  620  			start_entry = (addr - chunk->start_addr) >> order;
3406f868 Igor Stoppa    2018-02-28  621  			remaining_entries = (chunk->end_addr - addr) >> order;
3406f868 Igor Stoppa    2018-02-28  622  			boundary = get_boundary(chunk->entries, start_entry,
3406f868 Igor Stoppa    2018-02-28  623  						remaining_entries);
3406f868 Igor Stoppa    2018-02-28 @624  			if (unlikely(exit_test(boundary < 0))) {
3406f868 Igor Stoppa    2018-02-28  625  				rcu_read_unlock();
3406f868 Igor Stoppa    2018-02-28  626  				WARN(true, "Corrupted pool %s", pool);
3406f868 Igor Stoppa    2018-02-28  627  				return;
f14f75b8 Jes Sorensen   2005-06-21  628  			}
3406f868 Igor Stoppa    2018-02-28  629  			nentries = boundary - start_entry;
3406f868 Igor Stoppa    2018-02-28  630  			if (unlikely(size && (nentries !=
3406f868 Igor Stoppa    2018-02-28  631  					      mem_to_units(size, order)))) {
7f184275 Huang Ying     2011-07-13  632  				rcu_read_unlock();
3406f868 Igor Stoppa    2018-02-28  633  				WARN(true,
3406f868 Igor Stoppa    2018-02-28  634  				     "Size provided differs from size "
3406f868 Igor Stoppa    2018-02-28  635  				     "measured from pool %s", pool);
3406f868 Igor Stoppa    2018-02-28  636  				return;
3406f868 Igor Stoppa    2018-02-28  637  			}
3406f868 Igor Stoppa    2018-02-28  638  			remain = alter_bitmap_ll(CLEAR_BITS, chunk->entries,
3406f868 Igor Stoppa    2018-02-28  639  						 start_entry, nentries);
3406f868 Igor Stoppa    2018-02-28  640  			if (unlikely(remain)) {
3406f868 Igor Stoppa    2018-02-28  641  				rcu_read_unlock();
3406f868 Igor Stoppa    2018-02-28  642  				WARN(true,
3406f868 Igor Stoppa    2018-02-28  643  				     "Unexpected bitmap collision while"
3406f868 Igor Stoppa    2018-02-28  644  				     " freeing memory in pool %s", pool);
3406f868 Igor Stoppa    2018-02-28  645  				return;
3406f868 Igor Stoppa    2018-02-28  646  			}
3406f868 Igor Stoppa    2018-02-28  647  			atomic_long_add(nentries << order, &chunk->avail);
3406f868 Igor Stoppa    2018-02-28  648  			rcu_read_unlock();
3406f868 Igor Stoppa    2018-02-28  649  			return;
3406f868 Igor Stoppa    2018-02-28  650  		}
3406f868 Igor Stoppa    2018-02-28  651  	}
3406f868 Igor Stoppa    2018-02-28  652  	rcu_read_unlock();
3406f868 Igor Stoppa    2018-02-28  653  	WARN(true, "address not found in pool %s", pool->name);
f14f75b8 Jes Sorensen   2005-06-21  654  }
f14f75b8 Jes Sorensen   2005-06-21  655  EXPORT_SYMBOL(gen_pool_free);
7f184275 Huang Ying     2011-07-13  656  

:::::: The code at line 624 was first introduced by commit
:::::: 3406f8684f722bb52edc94f65976585acb6382ce genalloc: track beginning of allocations

:::::: TO: Igor Stoppa <igor.stoppa@huawei.com>
:::::: CC: 0day robot <fengguang.wu@intel.com>

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Jay Freyensee March 5, 2018, 8:31 p.m. UTC | #2
Reviewed-by: Jay Freyensee <why2jjj.linux@gmail.com>

On 2/28/18 12:06 PM, Igor Stoppa wrote:
> When a page is used for virtual memory, it is often necessary to obtain
> a handler to the corresponding vm_struct, which refers to the virtually
> continuous area generated when invoking vmalloc.
>
> The struct page has a "mapping" field, which can be re-used, to store a
> pointer to the parent area.
>
> This will avoid more expensive searches, later on.
>
> Signed-off-by: Igor Stoppa <igor.stoppa@huawei.com>
> ---
>   include/linux/mm_types.h | 1 +
>   mm/vmalloc.c             | 2 ++
>   2 files changed, 3 insertions(+)
>
> diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
> index fd1af6b9591d..c3a4825e10c0 100644
> --- a/include/linux/mm_types.h
> +++ b/include/linux/mm_types.h
> @@ -84,6 +84,7 @@ struct page {
>   		void *s_mem;			/* slab first object */
>   		atomic_t compound_mapcount;	/* first tail page */
>   		/* page_deferred_list().next	 -- second tail page */
> +		struct vm_struct *area;
>   	};
>   
>   	/* Second double word */
> diff --git a/mm/vmalloc.c b/mm/vmalloc.c
> index ebff729cc956..61a1ca22b0f6 100644
> --- a/mm/vmalloc.c
> +++ b/mm/vmalloc.c
> @@ -1536,6 +1536,7 @@ static void __vunmap(const void *addr, int deallocate_pages)
>   			struct page *page = area->pages[i];
>   
>   			BUG_ON(!page);
> +			page->area = NULL;
>   			__free_pages(page, 0);
>   		}
>   
> @@ -1705,6 +1706,7 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
>   			area->nr_pages = i;
>   			goto fail;
>   		}
> +		page->area = area;
>   		area->pages[i] = page;
>   		if (gfpflags_allow_blocking(gfp_mask|highmem_mask))
>   			cond_resched();

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
index fd1af6b9591d..c3a4825e10c0 100644
--- a/include/linux/mm_types.h
+++ b/include/linux/mm_types.h
@@ -84,6 +84,7 @@  struct page {
 		void *s_mem;			/* slab first object */
 		atomic_t compound_mapcount;	/* first tail page */
 		/* page_deferred_list().next	 -- second tail page */
+		struct vm_struct *area;
 	};
 
 	/* Second double word */
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index ebff729cc956..61a1ca22b0f6 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -1536,6 +1536,7 @@  static void __vunmap(const void *addr, int deallocate_pages)
 			struct page *page = area->pages[i];
 
 			BUG_ON(!page);
+			page->area = NULL;
 			__free_pages(page, 0);
 		}
 
@@ -1705,6 +1706,7 @@  static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 			area->nr_pages = i;
 			goto fail;
 		}
+		page->area = area;
 		area->pages[i] = page;
 		if (gfpflags_allow_blocking(gfp_mask|highmem_mask))
 			cond_resched();