Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp502592pxu; Tue, 1 Dec 2020 17:26:45 -0800 (PST) X-Google-Smtp-Source: ABdhPJz7JI28UuJsPEaOjV4gPli/p08t5O/Aa/vDicAOKRqL1FApFw2FtGJGNZhG8CpM9xqP+NuR X-Received: by 2002:a05:6402:456:: with SMTP id p22mr403806edw.26.1606872405680; Tue, 01 Dec 2020 17:26:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606872405; cv=none; d=google.com; s=arc-20160816; b=jEY//gkx5dbN+2h9HuQaDb9ZSCH3Gq+VAH0QHmeQ4q5MySCXyc7XsQPl8NV3hd57Xm g4iAdNjhfsXuAvfYSA3xFwa3GkA8juRA8u3bA1+IEnQl1uKDVS5AgHwFUMnoSeO42q3s Eq099GvPeRNQEMVHDZBHQCwFC0KQzjjccNmGg7W+4GbQZUO0VLUOlEJ2TQwsf61FK0rY xLbOhDNih+b5rRAAjsgHMNIYGuEr9iZ5hotqSLtQDCydB36GAiMnzNy5ROUYOHqEllHj MiD/QwZcoTCas8j7IqAou9YYLb4oWUGtQVeelR5HSaXGuyW6+kojH1411kh4ZhneDeLf IfQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:from:references:cc:to :subject; bh=0ys1LX9s12s3IG6xMZnNGva8SrEy7G+mDmjFHSfA2Qw=; b=ZQrMrFSzSA3B6t8E6j9BdxNOYfZOJJxswG87oSTSeFsDq/emVmH8CvDrl1ptdsUlZK RAx6h9Hidoxxt7Z/r5qBvH159TXc4C+GSaWwDM8ScHWfIcF756TZyO1wH8zS5L10wRhD 8k5UYAE65RheER1v1Ya+6LibfeMf+hbsWmFBXa/zvXa2u8/CViJZJhdrUL/9yJcGOOdZ KpzVwZsGFB6oMY/xicIrvsvrvBOG9qWxG2q7aV9e2ZPYtvItT2K1IK3afcdf6MnHJyUq zrP4sVxNFuCjOMsuuOH6A/li6lnYv37r/Wd+v/6PB8NGjSrCEYKtBXkOfuCBN81ZcGeW KQcg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b5si138093edz.214.2020.12.01.17.26.23; Tue, 01 Dec 2020 17:26:45 -0800 (PST) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727938AbgLBBY0 (ORCPT + 99 others); Tue, 1 Dec 2020 20:24:26 -0500 Received: from szxga05-in.huawei.com ([45.249.212.191]:8548 "EHLO szxga05-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727593AbgLBBY0 (ORCPT ); Tue, 1 Dec 2020 20:24:26 -0500 Received: from DGGEMS412-HUB.china.huawei.com (unknown [172.30.72.58]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4Cm1SQ0g6jzhl1s; Wed, 2 Dec 2020 09:23:18 +0800 (CST) Received: from [10.174.178.52] (10.174.178.52) by DGGEMS412-HUB.china.huawei.com (10.3.19.212) with Microsoft SMTP Server id 14.3.487.0; Wed, 2 Dec 2020 09:23:37 +0800 Subject: Re: [PATCH] kretprobe: avoid re-registration of the same kretprobe earlier To: Masami Hiramatsu , Steven Rostedt CC: , , , , , References: <20201124115719.11799-1-bobo.shaobowang@huawei.com> <20201130161850.34bcfc8a@gandalf.local.home> <20201202083253.9dbc76704149261e131345bf@kernel.org> From: "Wangshaobo (bobo)" Message-ID: <9dff21f8-4ab9-f9b2-64fd-cc8c5f731932@huawei.com> Date: Wed, 2 Dec 2020 09:23:35 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Thunderbird/68.1.0 MIME-Version: 1.0 In-Reply-To: <20201202083253.9dbc76704149261e131345bf@kernel.org> Content-Type: text/plain; charset="gbk"; format=flowed Content-Transfer-Encoding: 8bit X-Originating-IP: [10.174.178.52] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi steve, Masami, Thanks for your works, i will check code again and modify properly according to steve's suggestion. -- ShaoBo ?? 2020/12/2 7:32, Masami Hiramatsu ะด??: > On Mon, 30 Nov 2020 16:18:50 -0500 > Steven Rostedt wrote: > >> Masami, >> >> Can you review this patch, and also, should this go to -rc and stable? >> >> -- Steve > Thanks for ping me! > >> On Tue, 24 Nov 2020 19:57:19 +0800 >> Wang ShaoBo wrote: >> >>> Our system encountered a re-init error when re-registering same kretprobe, >>> where the kretprobe_instance in rp->free_instances is illegally accessed >>> after re-init. > Ah, OK. Anyway if re-register happens on kretprobe, it must lose instances > on the list before checking re-register in register_kprobe(). > So the idea looks good to me. > > >>> Implementation to avoid re-registration has been introduced for kprobe >>> before, but lags for register_kretprobe(). We must check if kprobe has >>> been re-registered before re-initializing kretprobe, otherwise it will >>> destroy the data struct of kretprobe registered, which can lead to memory >>> leak, system crash, also some unexpected behaviors. >>> >>> we use check_kprobe_rereg() to check if kprobe has been re-registered >>> before calling register_kretprobe(), for giving a warning message and >>> terminate registration process. >>> >>> Signed-off-by: Wang ShaoBo >>> Signed-off-by: Cheng Jian >>> --- >>> kernel/kprobes.c | 8 ++++++++ >>> 1 file changed, 8 insertions(+) >>> >>> diff --git a/kernel/kprobes.c b/kernel/kprobes.c >>> index 41fdbb7953c6..7f54a70136f3 100644 >>> --- a/kernel/kprobes.c >>> +++ b/kernel/kprobes.c >>> @@ -2117,6 +2117,14 @@ int register_kretprobe(struct kretprobe *rp) >>> } >>> } >>> >>> + /* >>> + * Return error if it's being re-registered, >>> + * also give a warning message to the developer. >>> + */ >>> + ret = check_kprobe_rereg(&rp->kp); >>> + if (WARN_ON(ret)) >>> + return ret; > If you call this here, you must make sure kprobe_addr() is called on rp->kp. > But if kretprobe_blacklist_size == 0, kprobe_addr() is not called before > this check. So it should be in between kprobe_on_func_entry() and > kretprobe_blacklist_size check, like this > > if (!kprobe_on_func_entry(rp->kp.addr, rp->kp.symbol_name, rp->kp.offset)) > return -EINVAL; > > addr = kprobe_addr(&rp->kp); > if (IS_ERR(addr)) > return PTR_ERR(addr); > rp->kp.addr = addr; > > ret = check_kprobe_rereg(&rp->kp); > if (WARN_ON(ret)) > return ret; > > if (kretprobe_blacklist_size) { > for (i = 0; > > + ret = check_kprobe_rereg(&rp->kp); > > > Thank you, > >