From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id AE7B1D21684 for ; Tue, 15 Oct 2024 12:19:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2D8EC6B0099; Tue, 15 Oct 2024 08:19:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 289796B009A; Tue, 15 Oct 2024 08:19:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1507A6B009B; Tue, 15 Oct 2024 08:19:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id EA2F36B0099 for ; Tue, 15 Oct 2024 08:19:16 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id EAF6716156A for ; Tue, 15 Oct 2024 12:19:06 +0000 (UTC) X-FDA: 82675741428.14.347D5F4 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf05.hostedemail.com (Postfix) with ESMTP id 105DC100009 for ; Tue, 15 Oct 2024 12:19:00 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=bbKbeUOf; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf05.hostedemail.com: domain of rppt@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728994708; a=rsa-sha256; cv=none; b=6YEAv4yEMi/mn+7V+KGstux0igZEfyhLmb7zo92EPDOcCv2aoDrrE5XKMYSC2gGfOQRH1+ owoHJW/WoBwO9iQ7Cp0pUH6suy6r4Lkj8bWwkHCDxo/46wfzLh/NSPnoGPC+Z2NY0ifjit Y1iCiFFa5AdDp+WZFdwjvj/ipXUBw0I= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=bbKbeUOf; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf05.hostedemail.com: domain of rppt@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728994708; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=gGV91UbHL2dqxsQZ2l8lo08bZ0CX5oNToy1QFQEsWbc=; b=SJRh1XjZDZzjy5VqyeQJn353zwk/E7XlkUOMwRU+oxHzCRGnuCINgAPswABHAwS4WaXVyz 8rfBzRx+kCn9BhYmNvtjIxaWK69EHgKYOy/W6vAOhJTU/t0XMk+9Pcg6N04K/hyo19brT+ EZPdYElfCz33560nUjokCmpRdW7L2Nk= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 7F8905C58BA; Tue, 15 Oct 2024 12:19:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A0037C4CEC6; Tue, 15 Oct 2024 12:19:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1728994753; bh=h4+7by0rAf1o7D24l+y+0PKeuIiAXm/n19ReSlgbZAA=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=bbKbeUOfn9nyDXrtBn7g19Dpfjiiw77gfgeY76w/MQudeJ+b61eaCe1lNNUr/wZsx NPYYUCCNZqjY+e0FpiMV0ssGHtIM4kTnGCLC/d7Nkj7MozDRcNrxTwPsujBUIQd6dh I7iGN20nS0P09TvQqbm0ZYkIwkbZECUW+yKmPcegYF8REA5wenOFpRoO2SVolOkynd nTd0FKRDimQvqjAtQjL34RYpNOxdkS2f+FMd4CvvRoEYfynQsvPeYauSwnLRHBt0id XBJTOVhu64U/+vudH7ZYapQVuQFYm6Y/c5zlyGCODMyTrwKlF3NtvOZ9lcr+LvwugG ChpqrAC7oGC8A== Date: Tue, 15 Oct 2024 15:15:27 +0300 From: Mike Rapoport To: Suren Baghdasaryan 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 Message-ID: References: <20241014203646.1952505-1-surenb@google.com> <20241014203646.1952505-4-surenb@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20241014203646.1952505-4-surenb@google.com> X-Rspam-User: X-Rspamd-Queue-Id: 105DC100009 X-Rspamd-Server: rspam01 X-Stat-Signature: r7b997imo53eq8oqqe6yzwbau4ydwt9c X-HE-Tag: 1728994740-987049 X-HE-Meta: U2FsdGVkX1/2p9y7AzVuDS54nNwW7jtbUCCc9E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w1e8aWVOcygJBHJP/ueSMwEXyXQtC+a0svo2vEAEVnpRHhjHS1qKYLkPTzA/AEzrtxSixknM5Gj2I1IfSPqPYZw1Gy1KpCcbedEQTj1yhkYFLgtTE9noCJdHASaZeRKSFKdgh+IhMtSuwRadPEaBQQsuCkWcOzZREpW7U27Xrz6pKswBJA3FdC/ev4WSZPRK/W60ZKyAdiKW39jis8eaUWx4m0gcFSPzig= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 > --- > 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 > #include > #include > +#include > > 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.