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 A9708D216BF for ; Tue, 15 Oct 2024 14:49:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 233306B0089; Tue, 15 Oct 2024 10:49:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1E3B96B008A; Tue, 15 Oct 2024 10:49:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0842D6B008C; Tue, 15 Oct 2024 10:49:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id DF70D6B0089 for ; Tue, 15 Oct 2024 10:49:32 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 887BB1616CD for ; Tue, 15 Oct 2024 14:49:22 +0000 (UTC) X-FDA: 82676119974.10.6F52C8F Received: from mail-qt1-f173.google.com (mail-qt1-f173.google.com [209.85.160.173]) by imf04.hostedemail.com (Postfix) with ESMTP id 7A5B640004 for ; Tue, 15 Oct 2024 14:49:20 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=LINM3YQq; spf=pass (imf04.hostedemail.com: domain of surenb@google.com designates 209.85.160.173 as permitted sender) smtp.mailfrom=surenb@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729003697; a=rsa-sha256; cv=none; b=qGh2+kvbriyVttW0ofWKWI/tnolhMNo5hH5WWb0uwQgMX2YQDLcQ6uhEnlvsFAoujNGxQA lVy7vay0hY3HZrSrXLPTKh+5I0Jppz8bFouomA34bzJsAsXMvSGJh+CNS1x7MBZZ6r+LRU hiF+0cHYULpHQBkpY9aXibMCpjYRdcw= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=LINM3YQq; spf=pass (imf04.hostedemail.com: domain of surenb@google.com designates 209.85.160.173 as permitted sender) smtp.mailfrom=surenb@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729003697; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=KGfVMDlLn1825g+J14Hy1oDH1uXbeKzHCn9d9Ylz3+Q=; b=uPvlhkYYUS8eK96QludLArdImG4QPZSr5AimKjYr4lm70nrFfx9769lty0WdnN+4RoBEYF n2Gw/YWAzPFq8V0Dypxf5kivZnOm23/RAsaCERNJ8F5h4azmRQezzuEr1e3R/mWMjAWcpZ L79CyEEn/Ddp1T066Gp8O6Klvpm2SsM= Received: by mail-qt1-f173.google.com with SMTP id d75a77b69052e-45fb0ebb1d0so699161cf.1 for ; Tue, 15 Oct 2024 07:49:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1729003767; x=1729608567; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=KGfVMDlLn1825g+J14Hy1oDH1uXbeKzHCn9d9Ylz3+Q=; b=LINM3YQq83T0t8NVverCnwdQTj7Md7eQ9RmeDqPFWlRNXCRgfGkYArDtgf/FkFrpZH 1JgORN4VxdQjT/5CBorcbv8ec1wk+jNcgcEOp2LYHmTHhzLs3ugTQPl/JF6OX4JTsQTU dHmYeA4hmfOsbEbWGt1efBtJn/krROi99YLg9t6qgOZ67LrBRjuGPKDI6dJPYe9IyxU8 i/NDeG/oprQkhoapw6p/L+6Qf2FGeIGeHSmYkrGAC+sCdZuoHDrTZTNxOr7riimIpx4E Owg71y6TL7RJGJL0EY+I16U+48CdJ/FLhFrcfXGho2QW+8zvWBS1SekXoW5Kr3H/AyTa Eq/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729003767; x=1729608567; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KGfVMDlLn1825g+J14Hy1oDH1uXbeKzHCn9d9Ylz3+Q=; b=Dp76q+d4awbGqOi8X014lameTJ5ZXpJiK3YLQingKL5+MxAZI5Yopxnjv1NroAKOZ3 bK4WFDPwi8N63wjJOPPc0oWbzs6GQC2fK9SIWWevJhsRqxqe70YmKF0fwFz98BvM7lDd rmdiHO9ZBtgHNqeXjUt0UHfjVLiB7LVFlsU/mvcRml33N0uUbQJ1KiRUHSBNtbkoCKpX jXWyxXmEbCgarSz6tyuL073jiCjA3JbKyet99r7DEgqcPf3x4wnqzuaqEGbfvHnWYK/T VuZNblqNDyCUjmxzhadLMny8TMR4CALAwbB2xIwlLFBwW2VLk/AhDp+92c/ewleOj5Ua Ia8A== X-Forwarded-Encrypted: i=1; AJvYcCW02z5/Sr/9Q/BZvJvkijqi86Jso4xWWSHEXlTWwtLNvVyt1M5FndOwKSx3nXqzU4+qMt9HgBqsCA==@kvack.org X-Gm-Message-State: AOJu0YzYsdegECyaPic/yawgS8MNi+VPSQjAlzqgiFEAl9N4fU2AbGGT R8ZtS5nYXV4uDrGmRk4h6nLHm9q3fpHxjDMVH6lYl/uRZjRC3sWwiRwH5Xbk5WKoZcbLMEEr3Hv peDOwexczMMTkfVQU3L+VXNj1Yb5rDJ5l3OxZ X-Google-Smtp-Source: AGHT+IHyqoTyx/YlQBvl8SRkssyjUIklgUBoWFJuAmw8xTcFULp90DIWTR4dOeGeR2G47cVqTZdstcTSZdSOnU18ol8= X-Received: by 2002:a05:622a:820c:b0:45d:8d1f:c505 with SMTP id d75a77b69052e-46059c43f44mr7776181cf.15.1729003766319; Tue, 15 Oct 2024 07:49:26 -0700 (PDT) MIME-Version: 1.0 References: <20241014203646.1952505-1-surenb@google.com> <20241014203646.1952505-4-surenb@google.com> In-Reply-To: From: Suren Baghdasaryan Date: Tue, 15 Oct 2024 07:49:13 -0700 Message-ID: Subject: Re: [PATCH v3 3/5] alloc_tag: populate memory for module tags as needed To: Mike Rapoport 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Stat-Signature: uy6aespfnmc1xiri1ocgy6dsfadweohs X-Rspamd-Queue-Id: 7A5B640004 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1729003760-446006 X-HE-Meta: U2FsdGVkX1/JYMY5mDfJjOzRad1Yl+ppz/WyhjXg9tomszDDhIFukj5zF8jXBdZrrye4WZUNV3x/QsDGye9mDSSl/ulW0Rb4oOmpzHgz6/wBFTnyFsWBPKlNA3ITRyHNa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f7xs4dYOW4+Hj5SDqg3TUhAbPUEtJjkaVkw5wMU4zub+aEDfylFqxvRcMUgQHQdBGv0SpzGLJnz9EcD 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 Tue, Oct 15, 2024 at 5:19=E2=80=AFAM 'Mike Rapoport' via kernel-team wrote: > > 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 executabl= e 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 Ack. > > > /** > > * 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_ar= ea_struct *vma, > > extern int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, > > unsigned long pgo= ff); > > > > +int vmap_pages_range(unsigned long addr, unsigned long end, > > + pgprot_t prot, struct page **pages, unsigned int page_shi= ft); > > + > > > > /* > > * Architectures can set this mask to a combination of PGTBL_P?D_MODIF= IED 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(unsi= gned long size, > > unsigned long flags, > > unsigned long start, unsigned lon= g end, > > const void *caller); > > +struct vm_struct *__get_vm_area_node(unsigned long size, > > + unsigned long align, unsigned long s= hift, > > + unsigned long flags, unsigned long s= tart, > > + unsigned long end, int node, gfp_t g= fp_mask, > > + const void *caller); > > + > > This is not used outside mm/, let's put it into mm/internal.h Ack. > > > 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 =3D MTREE_INIT(mod_area_mt, MT_FL= AGS_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 =3D vm_module_tags->pages + vm_module_tag= s->nr_pages; > > + unsigned long more_pages =3D ALIGN(bytes, PAGE_SIZE) >> PAGE_SHIF= T; > > + unsigned long nr; > > + > > + nr =3D alloc_pages_bulk_array_node(GFP_KERNEL | __GFP_NOWARN, > > + NUMA_NO_NODE, more_pages, next_p= age); > > + if (nr !=3D more_pages) > > + return -ENOMEM; > > + > > + vm_module_tags->nr_pages +=3D 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 siz= e, > > unsigned int prepend, unsigned long alig= n) > > { > > @@ -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 =3D false; > > unsigned long offset; > > - void *ret; > > + void *ret =3D 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 =3D 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 =3D ERR_PTR(xa_err(mas.node)); > > - goto out; > > + goto unlock; > > } > > mas.index =3D offset; > > mas.last =3D offset + size - 1; > > @@ -263,7 +284,7 @@ static void *reserve_module_tags(struct module *mod= , unsigned long size, > > ret =3D ERR_PTR(xa_err(mas.node)); > > mas.index =3D pad_start; > > mas_erase(&mas); > > - goto out; > > + goto unlock; > > } > > > > } else { > > @@ -271,18 +292,33 @@ static void *reserve_module_tags(struct module *m= od, unsigned long size, > > mas_store(&mas, mod); > > if (mas_is_err(&mas)) { > > ret =3D 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 =3D offset + size; > > + if (module_tags.size < offset + size) { > > + unsigned long phys_size =3D vm_module_tags->nr_pages << P= AGE_SHIFT; > > > > - ret =3D (struct alloc_tag *)(module_tags.start_addr + offset); > > -out: > > - mas_unlock(&mas); > > + module_tags.size =3D offset + size; > > + if (phys_size < module_tags.size) { > > + int grow_res; > > + > > + grow_res =3D vm_module_tags_grow(module_tags.star= t_addr + phys_size, > > + module_tags.size -= phys_size); > > + if (grow_res) { > > + static_branch_disable(&mem_alloc_profilin= g_key); > > + pr_warn("Failed to allocate tags memory f= or 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 itse= lf > 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? Got it. Will refactor this function to make it easier to review. Thanks for the prompt review, Mike! > > > - return ret; > > + return (struct alloc_tag *)(module_tags.start_addr + offset); > > } > > > > -- > Sincerely yours, > Mike. > > To unsubscribe from this group and stop receiving emails from it, send an= email to kernel-team+unsubscribe@android.com. >