Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp4330556ybb; Tue, 7 Apr 2020 05:30:14 -0700 (PDT) X-Google-Smtp-Source: APiQypK708nGw0zieWskuLho8nE4WQPrvlCITLl67c0M8EOZuc8e/6rI9hanZjE++kAGlfjea+9J X-Received: by 2002:a4a:3bd7:: with SMTP id s206mr1621663oos.89.1586262613939; Tue, 07 Apr 2020 05:30:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586262613; cv=none; d=google.com; s=arc-20160816; b=fYZoE9/bmgbyFG8BfqwIlBH4UszuwJckQZH/FO3BUI31t/Ni7bR6B1zwJ+4GGxj2uV HPN3Ln7rjSZ/Q/FQ10gVe1ooMDQ+aK+Bq8xRRFl/mcpO2Jv58rv8Quk1LwrFcwH82dx6 jz1IbzeY4zscMbpwTIO96AICvRBJvtxs+NBMSm7xCLkVh0VQSeoGoNVsdGeLXP0VixJv B7Cmz46PO096F1CKmBgl0mTFBWzQPBQoNSXqrpvIYygkq6SqmZc0gcmICw15P8HX7JEH t0KinIk2NK/qkzpHKo41eDwDOC0Y+MexulC8Rx3vtHH7crBbVmfcAXendBExZ5ujeE+o RRyQ== 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=bXbNYpMYQORbKDlgfoLvrDMjtp6TrFePij4Cjrr5Dpo=; b=g0LUypr/GWRygQWfQR+sOspsdVw423/kVMFxcZGwY7bZsp+MN7dInmFWLYV1FgJim+ KlOdzbfZSG0ZCCsOwTqO5fbjTtdmIkGpbFFWxLpHNyQElw/GdyCyJjqixvq0Tj0uV4su zOL+SlZsl1ip+4Ma7uVCpor0exMOGHs41vEt1kSHl7IRozZ3CnZNORwNHnQLcTsFJAYP lkWZVcohl4okGaUwXtLM6Iqh/A8xYa0qePtEPh8Y6c0sK8t4FIcIBQX1yMgHQX+kk4wD UAAzSNpSrVbvGbDWOqCG0N5HFDv3x36iXzLsaoX6rDEgwuiML9pHdOPcWX4vhsR7FM1J WBpQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=kUGwajbM; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id n2si1068397oon.60.2020.04.07.05.30.01; Tue, 07 Apr 2020 05:30:13 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=kUGwajbM; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1728627AbgDGM3Y (ORCPT + 99 others); Tue, 7 Apr 2020 08:29:24 -0400 Received: from mail.kernel.org ([198.145.29.99]:35476 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728075AbgDGM3Y (ORCPT ); Tue, 7 Apr 2020 08:29:24 -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 50D0F2051A; Tue, 7 Apr 2020 12:29:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1586262562; bh=hrXJIm2yARS2f7WqTD+GSOvFj12Qjg9tKPNB5u2C4Zw=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=kUGwajbMjegrZ9/FXEfvB/C/P+YdB3LOk1YwoNOBffm+Plv2nIpa/cAVirE/B3fGw V2yjSVgwFPyr9GPyB0RsXD2GdCV4E2PYH7lMIfzcrtsGMJbG00wMWvVY/7AGuIoJ6I fwU5Z+J4ITndAyObWgHa4x5VUu35POgDzptHveRk= Date: Tue, 7 Apr 2020 21:29:18 +0900 From: Masami Hiramatsu To: Zong Li Cc: Palmer Dabbelt , Paul Walmsley , Albert Ou , linux-riscv , "linux-kernel@vger.kernel.org List" Subject: Re: [PATCH v3 8/9] riscv: introduce interfaces to patch kernel code Message-Id: <20200407212918.235324cbc82e9e4deb839b14@kernel.org> In-Reply-To: References: <20200401003233.17fe4b6f7075e5b8f0ed5114@kernel.org> <20200402101733.1ef240faeaeada6e4d38ae80@kernel.org> <20200404121428.596911ba5653f8b18a80eab2@kernel.org> X-Mailer: Sylpheed 3.5.1 (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 Mon, 6 Apr 2020 18:36:42 +0800 Zong Li wrote: > On Sat, Apr 4, 2020 at 8:12 PM Zong Li wrote: > > > > On Sat, Apr 4, 2020 at 11:14 AM Masami Hiramatsu wrote: > > > > > > Hi Zong, > > > > > > On Fri, 3 Apr 2020 17:04:51 +0800 > > > Zong Li wrote: > > > > > > > > > > > +{ > > > > > > > > + void *waddr = addr; > > > > > > > > + bool across_pages = (((uintptr_t) addr & ~PAGE_MASK) + len) > PAGE_SIZE; > > > > > > > > + unsigned long flags = 0; > > > > > > > > + int ret; > > > > > > > > + > > > > > > > > + raw_spin_lock_irqsave(&patch_lock, flags); > > > > > > > > > > > > > > This looks a bit odd since stop_machine() is protected by its own mutex, > > > > > > > and also the irq is already disabled here. > > > > > > > > > > > > We need it because we don't always enter the riscv_patch_text_nosync() > > > > > > through stop_machine mechanism. If we call the > > > > > > riscv_patch_text_nosync() directly, we need a lock to protect the > > > > > > page. > > > > > > > > > > Oh, OK, but it leads another question. Is that safe to patch the > > > > > text without sync? Would you use it for UP system? > > > > > I think it is better to clarify "in what case user can call _nosync()" > > > > > and add a comment on it. > > > > > > > > The ftrace is one of the cases, as documentation of ftrace said, when > > > > dynamic ftrace is initialized, it calls kstop_machine to make the > > > > machine act like a uniprocessor so that it can freely modify code > > > > without worrying about other processors executing that same code. So > > > > the ftrace called the _nosync interface here directly. > > > > > > Hmm, even though, since it already running under kstop_machine(), no > > > other thread will run. > > > Could you consider to use text_mutex instead of that? The text_mutex > > > is already widely used in x86 and kernel/kprobes.c etc. > > > > > > (Hmm, it seems except for x86, alternative code don't care about > > > racing...) > > > > > The mutex_lock doesn't seem to work in ftrace context, I think it > might be the reason why other architectures didn't use text_mutex in > somewhere. Yes, you need to implement ftrace_arch_code_modify_prepare() and ftrace_arch_code_modify_post_process() in arch/riscv/kernel/ftrace.c. Please see arch/x86/kernel/ftrace.c. Thank you, > > # echo function > current_tracer > [ 28.198070] BUG: sleeping function called from invalid context at > kernel/locking/mutex.c:281 > [ 28.198663] in_atomic(): 1, irqs_disabled(): 1, non_block: 0, pid: > 11, name: migration/0 > [ 28.199491] CPU: 0 PID: 11 Comm: migration/0 Not tainted > 5.6.0-00012-gd6f56a7a4be2-dirty #10 > [ 28.200330] Call Trace: > [ 28.200798] [] walk_stackframe+0x0/0xcc > [ 28.201395] [] show_stack+0x3c/0x46 > [ 28.200798] [] walk_stackframe+0x0/0xcc > [ 28.201395] [] show_stack+0x3c/0x46 > [ 28.201898] [] dump_stack+0x76/0x90 > [ 28.202329] [] ___might_sleep+0x100/0x10e > [ 28.202720] [] __might_sleep+0x4a/0x78 > [ 28.203033] [] mutex_lock+0x2c/0x54 > [ 28.203397] [] patch_insn_write+0x32/0xd8 > [ 28.203780] [] patch_text_nosync+0x10/0x32 > [ 28.204139] [] __ftrace_modify_call+0x5c/0x6c > [ 28.204497] [] ftrace_update_ftrace_func+0x20/0x4a > [ 28.204919] [] ftrace_modify_all_code+0xa0/0x148 > [ 28.205378] [] __ftrace_modify_code+0x12/0x1c > [ 28.205793] [] multi_cpu_stop+0xa2/0x158 > [ 28.206147] [] cpu_stopper_thread+0xa4/0x13a > [ 28.206510] [] smpboot_thread_fn+0xf8/0x1da > [ 28.206868] [] kthread+0xfa/0x12a > [ 28.207201] [] ret_from_exception+0x0/0xc > > > > > Yes, text_mutex seems to be great. I'll change to use text_mutex in > > the next version if it works fine after testing. Thanks. > > > > > Thank you, > > > -- > > > Masami Hiramatsu -- Masami Hiramatsu