linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Mike Rapoport <rppt@kernel.org>
To: Suren Baghdasaryan <surenb@google.com>
Cc: akpm@linux-foundation.org, kent.overstreet@linux.dev,
	corbet@lwn.net, arnd@arndb.de, mcgrof@kernel.org,
	paulmck@kernel.org, thuth@redhat.com, tglx@linutronix.de,
	bp@alien8.de, xiongwei.song@windriver.com, ardb@kernel.org,
	david@redhat.com, vbabka@suse.cz, mhocko@suse.com,
	hannes@cmpxchg.org, roman.gushchin@linux.dev, dave@stgolabs.net,
	willy@infradead.org, liam.howlett@oracle.com,
	pasha.tatashin@soleen.com, souravpanda@google.com,
	keescook@chromium.org, dennis@kernel.org, jhubbard@nvidia.com,
	yuzhao@google.com, vvvvvv@google.com, rostedt@goodmis.org,
	iamjoonsoo.kim@lge.com, rientjes@google.com, minchan@google.com,
	kaleshsingh@google.com, linux-doc@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org,
	linux-mm@kvack.org, linux-modules@vger.kernel.org,
	kernel-team@android.com
Subject: Re: [PATCH v3 3/5] alloc_tag: populate memory for module tags as needed
Date: Tue, 15 Oct 2024 15:15:27 +0300	[thread overview]
Message-ID: <Zw5c3zjW4sUUmont@kernel.org> (raw)
In-Reply-To: <20241014203646.1952505-4-surenb@google.com>

On Mon, Oct 14, 2024 at 01:36:44PM -0700, Suren Baghdasaryan wrote:
> The memory reserved for module tags does not need to be backed by
> physical pages until there are tags to store there. Change the way
> we reserve this memory to allocate only virtual area for the tags
> and populate it with physical pages as needed when we load a module.
> 
> Signed-off-by: Suren Baghdasaryan <surenb@google.com>
> ---
>  include/linux/execmem.h | 11 ++++++
>  include/linux/vmalloc.h |  9 +++++
>  lib/alloc_tag.c         | 84 +++++++++++++++++++++++++++++++++--------
>  mm/execmem.c            | 16 ++++++++
>  mm/vmalloc.c            |  4 +-
>  5 files changed, 106 insertions(+), 18 deletions(-)
> 
> diff --git a/include/linux/execmem.h b/include/linux/execmem.h
> index 7436aa547818..a159a073270a 100644
> --- a/include/linux/execmem.h
> +++ b/include/linux/execmem.h
> @@ -127,6 +127,17 @@ void *execmem_alloc(enum execmem_type type, size_t size);
>   */
>  void execmem_free(void *ptr);
>  
> +/**
> + * execmem_vmap - create virtual mapping for executable memory
> + * @type: type of the allocation
> + * @size: size of the virtual mapping in bytes
> + *
> + * Maps virtually contiguous area that can be populated with executable code.
> + *
> + * Return: the area descriptor on success or %NULL on failure.
> + */
> +struct vm_struct *execmem_vmap(enum execmem_type type, size_t size);
> +

I think it's better limit it to EXECMEM_MODULE_DATA

>  /**
>   * execmem_update_copy - copy an update to executable memory
>   * @dst:  destination address to update
> diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h
> index 9a012cd4fad2..9d64cc6f24d1 100644
> --- a/include/linux/vmalloc.h
> +++ b/include/linux/vmalloc.h
> @@ -202,6 +202,9 @@ extern int remap_vmalloc_range_partial(struct vm_area_struct *vma,
>  extern int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
>  							unsigned long pgoff);
>  
> +int vmap_pages_range(unsigned long addr, unsigned long end,
> +		pgprot_t prot, struct page **pages, unsigned int page_shift);
> +
>
>  /*
>   * Architectures can set this mask to a combination of PGTBL_P?D_MODIFIED values
>   * and let generic vmalloc and ioremap code know when arch_sync_kernel_mappings()
> @@ -239,6 +242,12 @@ extern struct vm_struct *__get_vm_area_caller(unsigned long size,
>  					unsigned long flags,
>  					unsigned long start, unsigned long end,
>  					const void *caller);
> +struct vm_struct *__get_vm_area_node(unsigned long size,
> +				     unsigned long align, unsigned long shift,
> +				     unsigned long flags, unsigned long start,
> +				     unsigned long end, int node, gfp_t gfp_mask,
> +				     const void *caller);
> +

This is not used outside mm/, let's put it into mm/internal.h

>  void free_vm_area(struct vm_struct *area);
>  extern struct vm_struct *remove_vm_area(const void *addr);
>  extern struct vm_struct *find_vm_area(const void *addr);
> diff --git a/lib/alloc_tag.c b/lib/alloc_tag.c
> index b10e7f17eeda..648f32d52b8d 100644
> --- a/lib/alloc_tag.c
> +++ b/lib/alloc_tag.c
> @@ -8,6 +8,7 @@
>  #include <linux/proc_fs.h>
>  #include <linux/seq_buf.h>
>  #include <linux/seq_file.h>
> +#include <linux/vmalloc.h>
>  
>  static struct codetag_type *alloc_tag_cttype;
>  
> @@ -153,6 +154,7 @@ static void __init procfs_init(void)
>  #ifdef CONFIG_MODULES
>  
>  static struct maple_tree mod_area_mt = MTREE_INIT(mod_area_mt, MT_FLAGS_ALLOC_RANGE);
> +static struct vm_struct *vm_module_tags;
>  /* A dummy object used to indicate an unloaded module */
>  static struct module unloaded_mod;
>  /* A dummy object used to indicate a module prepended area */
> @@ -195,6 +197,25 @@ static void clean_unused_module_areas_locked(void)
>  	}
>  }
>  
> +static int vm_module_tags_grow(unsigned long addr, unsigned long bytes)
> +{
> +	struct page **next_page = vm_module_tags->pages + vm_module_tags->nr_pages;
> +	unsigned long more_pages = ALIGN(bytes, PAGE_SIZE) >> PAGE_SHIFT;
> +	unsigned long nr;
> +
> +	nr = alloc_pages_bulk_array_node(GFP_KERNEL | __GFP_NOWARN,
> +					 NUMA_NO_NODE, more_pages, next_page);
> +	if (nr != more_pages)
> +		return -ENOMEM;
> +
> +	vm_module_tags->nr_pages += nr;
> +	if (vmap_pages_range(addr, addr + (nr << PAGE_SHIFT),
> +			     PAGE_KERNEL, next_page, PAGE_SHIFT) < 0)
> +		return -ENOMEM;
> +
> +	return 0;
> +}
> +
>  static void *reserve_module_tags(struct module *mod, unsigned long size,
>  				 unsigned int prepend, unsigned long align)
>  {
> @@ -202,7 +223,7 @@ static void *reserve_module_tags(struct module *mod, unsigned long size,
>  	MA_STATE(mas, &mod_area_mt, 0, section_size - 1);
>  	bool cleanup_done = false;
>  	unsigned long offset;
> -	void *ret;
> +	void *ret = NULL;
>  
>  	/* If no tags return NULL */
>  	if (size < sizeof(struct alloc_tag))
> @@ -239,7 +260,7 @@ static void *reserve_module_tags(struct module *mod, unsigned long size,
>  		goto repeat;
>  	} else {
>  		ret = ERR_PTR(-ENOMEM);
> -		goto out;
> +		goto unlock;
>  	}
>  
>  found:
> @@ -254,7 +275,7 @@ static void *reserve_module_tags(struct module *mod, unsigned long size,
>  		mas_store(&mas, &prepend_mod);
>  		if (mas_is_err(&mas)) {
>  			ret = ERR_PTR(xa_err(mas.node));
> -			goto out;
> +			goto unlock;
>  		}
>  		mas.index = offset;
>  		mas.last = offset + size - 1;
> @@ -263,7 +284,7 @@ static void *reserve_module_tags(struct module *mod, unsigned long size,
>  			ret = ERR_PTR(xa_err(mas.node));
>  			mas.index = pad_start;
>  			mas_erase(&mas);
> -			goto out;
> +			goto unlock;
>  		}
>  
>  	} else {
> @@ -271,18 +292,33 @@ static void *reserve_module_tags(struct module *mod, unsigned long size,
>  		mas_store(&mas, mod);
>  		if (mas_is_err(&mas)) {
>  			ret = ERR_PTR(xa_err(mas.node));
> -			goto out;
> +			goto unlock;
>  		}
>  	}
> +unlock:
> +	mas_unlock(&mas);
> +	if (IS_ERR(ret))
> +		return ret;
>  
> -	if (module_tags.size < offset + size)
> -		module_tags.size = offset + size;
> +	if (module_tags.size < offset + size) {
> +		unsigned long phys_size = vm_module_tags->nr_pages << PAGE_SHIFT;
>  
> -	ret = (struct alloc_tag *)(module_tags.start_addr + offset);
> -out:
> -	mas_unlock(&mas);
> +		module_tags.size = offset + size;
> +		if (phys_size < module_tags.size) {
> +			int grow_res;
> +
> +			grow_res = vm_module_tags_grow(module_tags.start_addr + phys_size,
> +						       module_tags.size - phys_size);
> +			if (grow_res) {
> +				static_branch_disable(&mem_alloc_profiling_key);
> +				pr_warn("Failed to allocate tags memory for module %s. Memory profiling is disabled!\n",
> +					mod->name);
> +				return ERR_PTR(grow_res);
> +			}
> +		}
> +	}

The diff for reserve_module_tags() is hard to read, and the function itself
becomes really complex to follow with all the gotos back and forth.
Maybe it's possible to split out some parts of it as helpers?

> -	return ret;
> +	return (struct alloc_tag *)(module_tags.start_addr + offset);
>  }
>  

-- 
Sincerely yours,
Mike.


  reply	other threads:[~2024-10-15 12:19 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-14 20:36 [PATCH v3 0/5] page allocation tag compression Suren Baghdasaryan
2024-10-14 20:36 ` [PATCH v3 1/5] maple_tree: add mas_for_each_rev() helper Suren Baghdasaryan
2024-10-16  1:48   ` Liam R. Howlett
2024-10-16  5:33     ` Suren Baghdasaryan
2024-10-14 20:36 ` [PATCH v3 2/5] alloc_tag: load module tags into separate contiguous memory Suren Baghdasaryan
2024-10-14 23:51   ` Andrew Morton
2024-10-15  2:10     ` Suren Baghdasaryan
2024-10-15 21:08       ` Shakeel Butt
2024-10-15 22:59         ` Suren Baghdasaryan
2024-10-14 20:36 ` [PATCH v3 3/5] alloc_tag: populate memory for module tags as needed Suren Baghdasaryan
2024-10-15 12:15   ` Mike Rapoport [this message]
2024-10-15 14:49     ` Suren Baghdasaryan
2024-10-14 20:36 ` [PATCH v3 4/5] alloc_tag: introduce pgalloc_tag_ref to abstract page tag references Suren Baghdasaryan
2024-10-14 20:36 ` [PATCH v3 5/5] alloc_tag: config to store page allocation tag refs in page flags Suren Baghdasaryan
2024-10-14 23:48   ` Yosry Ahmed
2024-10-14 23:53     ` John Hubbard
2024-10-14 23:56       ` Yosry Ahmed
2024-10-15  0:03         ` John Hubbard
2024-10-15  1:40           ` Matthew Wilcox
2024-10-15  2:03             ` Suren Baghdasaryan
2024-10-15  1:58           ` Suren Baghdasaryan
2024-10-15  8:10             ` Yosry Ahmed
2024-10-15 15:06               ` Suren Baghdasaryan
2024-10-15  7:32       ` David Hildenbrand
2024-10-15 14:59         ` Suren Baghdasaryan
2024-10-15 15:42           ` David Hildenbrand
2024-10-15 15:58             ` Suren Baghdasaryan
2024-10-18 13:03               ` Michal Hocko
2024-10-18 16:04                 ` Suren Baghdasaryan
2024-10-18 17:08                   ` Michal Hocko
2024-10-18 17:45                     ` Suren Baghdasaryan
2024-10-18 21:57                       ` Suren Baghdasaryan
2024-10-21  7:26                         ` Michal Hocko
2024-10-21  9:13                           ` David Hildenbrand
2024-10-21 15:05                             ` Suren Baghdasaryan
2024-10-21 15:34                               ` Michal Hocko
2024-10-21 15:41                                 ` Suren Baghdasaryan
2024-10-21 15:49                                   ` David Hildenbrand
2024-10-21 15:57                                   ` Michal Hocko
2024-10-21 16:16                                     ` Suren Baghdasaryan
2024-10-21 16:23                                       ` Michal Hocko
2024-10-21 16:32                                         ` Suren Baghdasaryan
2024-10-21 18:12                                           ` John Hubbard
2024-10-21  7:21                       ` Michal Hocko
2024-10-14 23:32 ` [PATCH v3 0/5] page allocation tag compression Andrew Morton
2024-10-15  1:48   ` Suren Baghdasaryan
2024-10-15 16:26     ` Suren Baghdasaryan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Zw5c3zjW4sUUmont@kernel.org \
    --to=rppt@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=ardb@kernel.org \
    --cc=arnd@arndb.de \
    --cc=bp@alien8.de \
    --cc=corbet@lwn.net \
    --cc=dave@stgolabs.net \
    --cc=david@redhat.com \
    --cc=dennis@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=iamjoonsoo.kim@lge.com \
    --cc=jhubbard@nvidia.com \
    --cc=kaleshsingh@google.com \
    --cc=keescook@chromium.org \
    --cc=kent.overstreet@linux.dev \
    --cc=kernel-team@android.com \
    --cc=liam.howlett@oracle.com \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-modules@vger.kernel.org \
    --cc=mcgrof@kernel.org \
    --cc=mhocko@suse.com \
    --cc=minchan@google.com \
    --cc=pasha.tatashin@soleen.com \
    --cc=paulmck@kernel.org \
    --cc=rientjes@google.com \
    --cc=roman.gushchin@linux.dev \
    --cc=rostedt@goodmis.org \
    --cc=souravpanda@google.com \
    --cc=surenb@google.com \
    --cc=tglx@linutronix.de \
    --cc=thuth@redhat.com \
    --cc=vbabka@suse.cz \
    --cc=vvvvvv@google.com \
    --cc=willy@infradead.org \
    --cc=xiongwei.song@windriver.com \
    --cc=yuzhao@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).