Received: by 2002:a25:ca44:0:0:0:0:0 with SMTP id a65csp1557654ybg; Wed, 29 Jul 2020 18:12:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJweP7tizuZg1cwtBNniG6n5jVfV5u2EDCEU6RwbVKjzLy1hpk0JZgc71EsjGELzQ4muiuv7 X-Received: by 2002:a05:6402:3193:: with SMTP id di19mr451047edb.98.1596071562558; Wed, 29 Jul 2020 18:12:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596071562; cv=none; d=google.com; s=arc-20160816; b=dkNp5CJIbHScOOSEpqp0Sm6eV7Mps3RrBhx4gJYs9cb/01fYKC4zo4gcx/t7LM0RTD qysnK67xzFtggyJN45zMAKOsIqf88ao7XMRnQm4EdcE5YWJNLyBPWd0gPU+2qA36A4uO J/3VM7PHuD3Bk6TvFye3/wTZzmrARxAF5/KnCqf5dsTJ+eJM03uSSJqrq7eyASPlXSe7 Em+Ux5ZTRWzNPzLOLlQHCoP55NitSKkn8lMrfIeyy4p0+A2KCWH3qq5ZeSt5+nj7WoEH aDgrMm4GVwDqWw8Mey6KfxIgcyGprwxCY7QM371RRu3UQApfgxXd8Yw8ZzcL9X2Cg8BJ TouQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=JolKwNNmN+dSNzSIuDJe7VDHgyr9tjJ/ouWxyhHv/4c=; b=hKTRtyrsPR+scIjb06HhznYBhhESpRZOH+9re5RItEHkeMQy2nEdQ2pdeGPaVilmUG qnHk3vAdssT/A8Lrf17RY5QYHqY3xJbpACPcAdOxYWckJZzyl7ArFdmRErWeANxj1ajg wlKckrpoSUxNQVNW+FAqh+kURr8Zn+p/9csQ124iewiTdUxTxbhs5MDz/aEu/gXH8t2L xmo/lu09gh7FpnO+w2LdjiUMq7ttZL9R0g+Cz3hsGlxRWJ3n23DbRuMyFHnBJxpwuFst bJo8BpObnae8Inl6o+0f+G1caDdxuiYos2Mxuz45Nm2yqD+tn0V/ENhQmhzMgi+LJJPh eA4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=jL5PSsBg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dn20si3269319ejc.111.2020.07.29.18.12.17; Wed, 29 Jul 2020 18:12:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=jL5PSsBg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726794AbgG3BJI (ORCPT + 99 others); Wed, 29 Jul 2020 21:09:08 -0400 Received: from mail.kernel.org ([198.145.29.99]:60996 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726287AbgG3BJH (ORCPT ); Wed, 29 Jul 2020 21:09:07 -0400 Received: from devnote2 (NE2965lan1.rev.em-net.ne.jp [210.141.244.193]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 457902074B; Thu, 30 Jul 2020 01:09:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1596071347; bh=TswWeoP8M0WaG5VC/mGNEtkfyyPr2p2vDX8T2CicOqo=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=jL5PSsBgnU5manfhdob8yg1u89clnWVnjQeRzlXIsBviSsEmu+iLFWqHgnrgtv9aM EWOZOBHXdYGVmE8LS2z4x4VMtPrpzvrAU9/qjWWbJ8sAzn6UG+6oydkw73PeXWhzjQ baxcMDjGDRrzc4e9/x2jIHHqzoRXforSoasIEIQA= Date: Thu, 30 Jul 2020 10:09:02 +0900 From: Masami Hiramatsu To: Ard Biesheuvel Cc: Mike Rapoport , Jarkko Sakkinen , Ingo Molnar , Linux Kernel Mailing List , linux-mm@kvack.org, Andi Kleen , Peter Zijlstra , "Naveen N. Rao" , Anil S Keshavamurthy , "David S. Miller" , Jessica Yu Subject: Re: [PATCH v5 5/6] kprobes: Use text_alloc() and text_free() Message-Id: <20200730100902.c5fa86a87a72ced0bce3dcf4@kernel.org> In-Reply-To: References: <20200724050553.1724168-1-jarkko.sakkinen@linux.intel.com> <20200724050553.1724168-6-jarkko.sakkinen@linux.intel.com> <20200724092746.GD517988@gmail.com> <20200725031648.GG17052@linux.intel.com> <20200726081408.GB2927915@kernel.org> <20200728171715.0800093e2226e3d72b04a3ae@kernel.org> <20200728223545.ce4ff78cac73b571a27bb357@kernel.org> <20200729105054.06f74749eb933c08342e6dd6@kernel.org> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.32; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 29 Jul 2020 09:13:21 +0300 Ard Biesheuvel wrote: > On Wed, 29 Jul 2020 at 04:51, Masami Hiramatsu wrote: > > > > On Tue, 28 Jul 2020 20:51:08 +0300 > > Ard Biesheuvel wrote: > > > > > On Tue, 28 Jul 2020 at 16:35, Masami Hiramatsu wrote: > > > > > > > > On Tue, 28 Jul 2020 13:56:43 +0300 > > > > Ard Biesheuvel wrote: > > > > > > > > > On Tue, 28 Jul 2020 at 11:17, Masami Hiramatsu wrote: > > > > > > > Masami or Peter should correct me if I am wrong, but it seems to me > > > > > > > that the way kprobes uses these pages does not require them to be in > > > > > > > relative branching range of the core kernel on any architecture, given > > > > > > > that they are populated with individual instruction opcodes that are > > > > > > > executed in single step mode, and relative branches are emulated (when > > > > > > > needed) > > > > > > > > > > > > Actually, x86 and arm has the "relative branching range" requirements > > > > > > for the jump optimized kprobes. For the other architectures, I think > > > > > > we don't need it. Only executable text buffer is needed. > > > > > > > > > > > > > > > > Thanks for the explanation. Today, arm64 uses the definition below. > > > > > > > > > > void *alloc_insn_page(void) > > > > > { > > > > > return __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START, VMALLOC_END, > > > > > GFP_KERNEL, PAGE_KERNEL_ROX, VM_FLUSH_RESET_PERMS, > > > > > NUMA_NO_NODE, __builtin_return_address(0)); > > > > > } > > > > > > > > > > Do you think we could use that as the generic implementation if we use > > > > > MODULES_START/_END as the allocation window? > > > > > > > > Yes, but for the generic implementation, we don't need to consider the > > > > relative branching range since we can override it for x86 and arm. > > > > (and that will be almost same as module_alloc() default code) > > > > > > Indeed. So having kprobes specific macros that default to > > > VMALLOC_START/END but can be overridden would be sufficient. > > > > > > > BTW, is PAGE_KERNEL_ROX flag available generically? > > > > > > > > > > Turns out that it is not :-( > > > > Hmm, in that case, we need to use PAGE_KERNEL_EXEC. > > > > In the result, may it be similar to this? :) > > > > void * __weak module_alloc(unsigned long size) > > { > > return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END, > > GFP_KERNEL, PAGE_KERNEL_EXEC, VM_FLUSH_RESET_PERMS, > > NUMA_NO_NODE, __builtin_return_address(0)); > > } > > > > The major difference between module_alloc() and kprobe's alloc_page_insn() > > is the alloc_page_insn() makes the page ROX after allocating the pages *ONLY* > > on x86 and arm64. > > > > Right. > > > $ git grep -w alloc_insn_page -- arch > > arch/arm64/kernel/probes/kprobes.c:void *alloc_insn_page(void) > > arch/x86/kernel/kprobes/core.c:void *alloc_insn_page(void) > > > > However since the module_alloc() owns its arch-dependent implementations > > most of major architectures, if we implement independent text_alloc_kprobe(), > > we need to make deadcopies of module_alloc() for each architecture. > > > > No, that is what we are trying to avoid. > > > $ git grep 'module_alloc(unsigned' arch/ > > arch/arm/kernel/module.c:void *module_alloc(unsigned long size) > > arch/arm64/kernel/module.c:void *module_alloc(unsigned long size) > > arch/mips/kernel/module.c:void *module_alloc(unsigned long size) > > arch/nds32/kernel/module.c:void *module_alloc(unsigned long size) > > arch/nios2/kernel/module.c:void *module_alloc(unsigned long size) > > arch/parisc/kernel/module.c:void *module_alloc(unsigned long size) > > arch/riscv/kernel/module.c:void *module_alloc(unsigned long size) > > arch/s390/kernel/module.c:void *module_alloc(unsigned long size) > > arch/sparc/kernel/module.c:void *module_alloc(unsigned long size) > > arch/unicore32/kernel/module.c:void *module_alloc(unsigned long size) > > arch/x86/kernel/module.c:void *module_alloc(unsigned long size) > > > > It seems that some constrains for module_alloc() exists for above > > architectures. > > > > Anyway, for kprobe's text_alloc() requirements are > > - It must be executable for the arch which uses a single-step out-of-line. > > (and need to be registered to KASAN?) > > No, kasan shadow is not needed here. OK, I just saw the KASAN shadow was generated (kasan_module_alloc was called) in module_alloc() on x86. So current x86 alloc_insn_page() has it. > > - It must be ROX if implemented (currently only for x86 and arm64) > > x86 does not actually define thr macro, but the result is the same. Yes, alloc_insn_page() for x86 is adding the RO and X flag on the page. > > - It must be in the range of relative branching only for x86 and arm. > > > > So in summary, the generic module_alloc() above can be reused for > kprobes on all arches except x86 and arm64, right? Then we can remove > the call to it, and drop the modules dependency. Yes, that's correct. If there is a generic module_alloc(), kprobes is happy to use it :) Thank you, -- Masami Hiramatsu