Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp2338086rdb; Thu, 21 Sep 2023 16:04:57 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHfN+pcxW9t5VVt8jvBuERlXAVN038PGqV1gxU5SzWXe3VA2UaM/R8tSZlcBBn3wnJUYLEX X-Received: by 2002:a17:902:e551:b0:1c3:2fc8:1305 with SMTP id n17-20020a170902e55100b001c32fc81305mr7555532plf.47.1695337496680; Thu, 21 Sep 2023 16:04:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695337496; cv=none; d=google.com; s=arc-20160816; b=dTpJUAaZZRYIsvJveFCHlEvOQzD9oruu+8+E2iDaJUyL1LWxoKyQkvcKt798dxh+sy rE2JTNhIr5+f080nPRHpAS9cCxYAw6qRH2DHx1zXwi+Gl0TBnkgU61GeIJrMi4jKEegv DBMAwDT01qXn0qfiNu0GocLWYbsgws0zEwoLq6rd/i6/zYmyPZSX99G2kb2uvwIXCR4k nZKZuzo20yJg47nl/40qqZFO6kgXZUcJ14Z9J7WfzN2UZ9O0iU49n/uMgfZXDS0Ye6IV EPFEpkUGR/LJyQ0+GUw8O0fdvMQJLT7rQgIIZZSk6EqbabzQsq74+KfAxQPkrgdWUQvA iyYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=kXltOii2wS53njMI7u+c+UD2X9bZ0uGdWQnoMh/qxRU=; fh=nux99dLKaQZPtWtkuy2/AW/ozv3Qzao1Xyv3eoLFGu0=; b=CcHH+ex4NNDgIq/ZBMH8l3lzmNPsfpcBzd290S7IsD+P7bmP/0NSoAwBzuqeQcqH4r KonZ+xShm5Q3e3vWcS/vzAmTi56w/Iq1FFHUR9FcjQ+G5sOtNtR1YhugzO05psUhBIDz YXwf7HUbo3MIon5KffHs6fJFY+ZW64SWn1my7r7XDWflkXL8Pm+hZ+Y3w0iFR+6uL8N+ cFSz8l6stS5ddS/bD230Qcjv0Sbm7oIyGJiYhfNwDlibX2O0Ai0l6ttbl1ifOg2QcIM3 vT3XfDkW09YzRENfn+LWsnVW3xUr1V74dZ5wwiMkm+bGueKUqVJLwjKEKx0sATvw33b7 WzXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=bjJNwevJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id lb12-20020a170902fa4c00b001ab089f7329si2418861plb.73.2023.09.21.16.04.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Sep 2023 16:04:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=bjJNwevJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 59E1E80D0CFC; Thu, 21 Sep 2023 15:52:54 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229720AbjIUWwm (ORCPT + 99 others); Thu, 21 Sep 2023 18:52:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56424 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229449AbjIUWwm (ORCPT ); Thu, 21 Sep 2023 18:52:42 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2B66DF7; Thu, 21 Sep 2023 15:52:36 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C784EC433AD; Thu, 21 Sep 2023 22:52:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1695336755; bh=N+rCKzD4RnGgPH16U+ccWdzVA4uu9GwvbHIaEOV8lLA=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=bjJNwevJA42TvkguuRjTs76ETN2R6q990VwsQaX+fVZHYFTVg3twcHMCXBi+EwswY KDvM3rQnSpp34FNwfM0U/WFpPi5sxiWBihMbkmLQVD3dMwe0w1TV/6pnGa5fK7MnRq f/RnmF4zvwavGrTVGR9fsTL6T+hC8QShKMLdwjOkeOLZk0rpJgxA2dFhnnTnVB9Xwk TCSjg9WYJ/gz4jSOZ5+6CD4cbsh3pPk93gUBjEKQnbqdin3Ors6QaTCSNo54GO47rs GROCJ3RhQnOtTsYPDlxmiEmDKgihH28meWIoUHV/vobKuLSBg7uQPTbsXHKf9QQFkj fMbBtTpx4QTiw== Received: by mail-lf1-f54.google.com with SMTP id 2adb3069b0e04-50305abe5f0so2439551e87.2; Thu, 21 Sep 2023 15:52:35 -0700 (PDT) X-Gm-Message-State: AOJu0Yy//vzaxhq2PC0z+wVHGEwKZ4Ke1r5YMDZ7obAXNGj3Ajce+ERv D+V05edYEmk4pGL+49y5p6NzoHP4lvUy0x6hZ3E= X-Received: by 2002:a05:6512:ac7:b0:500:b3e3:6fa6 with SMTP id n7-20020a0565120ac700b00500b3e36fa6mr7384141lfu.68.1695336753899; Thu, 21 Sep 2023 15:52:33 -0700 (PDT) MIME-Version: 1.0 References: <20230918072955.2507221-1-rppt@kernel.org> <20230918072955.2507221-7-rppt@kernel.org> In-Reply-To: <20230918072955.2507221-7-rppt@kernel.org> From: Song Liu Date: Thu, 21 Sep 2023 15:52:21 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v3 06/13] mm/execmem: introduce execmem_data_alloc() To: Mike Rapoport Cc: linux-kernel@vger.kernel.org, Andrew Morton , =?UTF-8?B?QmrDtnJuIFTDtnBlbA==?= , Catalin Marinas , Christophe Leroy , "David S. Miller" , Dinh Nguyen , Heiko Carstens , Helge Deller , Huacai Chen , Kent Overstreet , Luis Chamberlain , Mark Rutland , Michael Ellerman , Nadav Amit , "Naveen N. Rao" , Palmer Dabbelt , Puranjay Mohan , Rick Edgecombe , Russell King , Steven Rostedt , Thomas Bogendoerfer , Thomas Gleixner , Will Deacon , bpf@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, linux-parisc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, loongarch@lists.linux.dev, netdev@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Thu, 21 Sep 2023 15:52:54 -0700 (PDT) On Mon, Sep 18, 2023 at 12:31=E2=80=AFAM Mike Rapoport wr= ote: > [...] > diff --git a/include/linux/execmem.h b/include/linux/execmem.h > index 519bdfdca595..09d45ac786e9 100644 > --- a/include/linux/execmem.h > +++ b/include/linux/execmem.h > @@ -29,6 +29,7 @@ > * @EXECMEM_KPROBES: parameters for kprobes > * @EXECMEM_FTRACE: parameters for ftrace > * @EXECMEM_BPF: parameters for BPF > + * @EXECMEM_MODULE_DATA: parameters for module data sections > * @EXECMEM_TYPE_MAX: > */ > enum execmem_type { > @@ -37,6 +38,7 @@ enum execmem_type { > EXECMEM_KPROBES, > EXECMEM_FTRACE, In longer term, I think we can improve the JITed code and merge kprobe/ftrace/bpf. to use the same ranges. Also, do we need special setting for FTRACE? If not, let's just remove it. > EXECMEM_BPF, > + EXECMEM_MODULE_DATA, > EXECMEM_TYPE_MAX, > }; Overall, it is great that kprobe/ftrace/bpf no longer depend on modules. OTOH, I think we should merge execmem_type and existing mod_mem_type. Otherwise, we still need to handle page permissions in multiple places. What is our plan for that? Thanks, Song > > @@ -107,6 +109,23 @@ struct execmem_params *execmem_arch_params(void); > */ > void *execmem_text_alloc(enum execmem_type type, size_t size); > > +/** > + * execmem_data_alloc - allocate memory for data coupled to code > + * @type: type of the allocation > + * @size: how many bytes of memory are required > + * > + * Allocates memory that will contain data coupled with executable code, > + * like data sections in kernel modules. > + * > + * The memory will have protections defined by architecture. > + * > + * The allocated memory will reside in an area that does not impose > + * restrictions on the addressing modes. > + * > + * Return: a pointer to the allocated memory or %NULL > + */ > +void *execmem_data_alloc(enum execmem_type type, size_t size); > + > /** > * execmem_free - free executable memory > * @ptr: pointer to the memory that should be freed > diff --git a/kernel/module/main.c b/kernel/module/main.c > index c4146bfcd0a7..2ae83a6abf66 100644 > --- a/kernel/module/main.c > +++ b/kernel/module/main.c > @@ -1188,25 +1188,16 @@ void __weak module_arch_freeing_init(struct modul= e *mod) > { > } > > -static bool mod_mem_use_vmalloc(enum mod_mem_type type) > -{ > - return IS_ENABLED(CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC) && > - mod_mem_type_is_core_data(type); > -} > - > static void *module_memory_alloc(unsigned int size, enum mod_mem_type ty= pe) > { > - if (mod_mem_use_vmalloc(type)) > - return vzalloc(size); > + if (mod_mem_type_is_data(type)) > + return execmem_data_alloc(EXECMEM_MODULE_DATA, size); > return execmem_text_alloc(EXECMEM_MODULE_TEXT, size); > } > > static void module_memory_free(void *ptr, enum mod_mem_type type) > { > - if (mod_mem_use_vmalloc(type)) > - vfree(ptr); > - else > - execmem_free(ptr); > + execmem_free(ptr); > } > > static void free_mod_mem(struct module *mod) > diff --git a/mm/execmem.c b/mm/execmem.c > index abcbd07e05ac..aeff85261360 100644 > --- a/mm/execmem.c > +++ b/mm/execmem.c > @@ -53,11 +53,23 @@ static void *execmem_alloc(size_t size, struct execme= m_range *range) > return kasan_reset_tag(p); > } > > +static inline bool execmem_range_is_data(enum execmem_type type) > +{ > + return type =3D=3D EXECMEM_MODULE_DATA; > +} > + > void *execmem_text_alloc(enum execmem_type type, size_t size) > { > return execmem_alloc(size, &execmem_params.ranges[type]); > } > > +void *execmem_data_alloc(enum execmem_type type, size_t size) > +{ > + WARN_ON_ONCE(!execmem_range_is_data(type)); > + > + return execmem_alloc(size, &execmem_params.ranges[type]); > +} > + > void execmem_free(void *ptr) > { > /* > @@ -93,7 +105,10 @@ static void execmem_init_missing(struct execmem_param= s *p) > struct execmem_range *r =3D &p->ranges[i]; > > if (!r->start) { > - r->pgprot =3D default_range->pgprot; > + if (execmem_range_is_data(i)) > + r->pgprot =3D PAGE_KERNEL; > + else > + r->pgprot =3D default_range->pgprot; > r->alignment =3D default_range->alignment; > r->start =3D default_range->start; > r->end =3D default_range->end; > -- > 2.39.2 >