Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp4150869pxu; Mon, 21 Dec 2020 05:33:34 -0800 (PST) X-Google-Smtp-Source: ABdhPJxf2tWN4R5pGNgYCTWHFI4lPu27cPrONU+inJmr0U1YsueTt5gd/uqgnEnF4DR44s4rmGSL X-Received: by 2002:a17:906:3712:: with SMTP id d18mr15691898ejc.178.1608557613947; Mon, 21 Dec 2020 05:33:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1608557613; cv=none; d=google.com; s=arc-20160816; b=y5fWEkyu7n4VTb/U5RHA9o/qZ7WB+hZt09d3pt5DvhoMeMQjF/yJsUdmVsfORkXq4n 5XiSkBod1HZM5kJV8PFzpVMrSeeMf2vP9N4iUgC5NOXoUWShAqflUO6GvVXKafU8xyiD vu1ws7FgHtwFWOJqbmJfvLFyokBplXrraQyRm1pKOoH4BZHRxP3DxrnAkllh1z8fitoN 2QShaBez8Wd+qXHGrsz/euVUBH0sIcilz8m+cVZ4C152dx9xYXnckEEAIemMU1uHp5hS yvpht08ergUAOEx1t5xs7FEtcKwAjZ7kjUR/HYMoC19KQwCjXAiygPYUxo2l7I9rXCGy yucQ== 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=+aKgbsTeB3WzKOXdb9q3nNeUHc+XqsYEYrAJ6FXMz4o=; b=mD3NOB8dIhzqft9Fchjj3E2yoE9BzKYCeRqf42fUKTb1suXi1hnEPnTlfaSJJ4yj/K fpCjqlINJ0GJWohOhIAg4UJf9d/1KVdJszMwIBxnRTSJM/Yqz9pGVesuA0uVqy7THOZv hRzb9XvKyC/zni4qC2sEnBE7MGBOaF4nQPV1jeo1C62N3U+EpRSPtKJmII1aHtIj0Huh EylybTZGR0sQbMtRnOfohhEEmqEcgHaQWxRgh9McCtIsvJRt5lxcGAIqOz0gyRLGoMm6 Qeq7QuxSI1kSx7UU+2olVNdKOqPGDi2GBtsXiHAgMYLQhN3/OiKmfe5XbeXGvoMwKLhD WrqQ== 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 1si11233808edv.426.2020.12.21.05.33.08; Mon, 21 Dec 2020 05:33:33 -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 S1726595AbgLUNcj (ORCPT + 99 others); Mon, 21 Dec 2020 08:32:39 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:9234 "EHLO szxga04-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725891AbgLUNcj (ORCPT ); Mon, 21 Dec 2020 08:32:39 -0500 Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.60]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4D00jD2GFHzksVK; Mon, 21 Dec 2020 21:30:56 +0800 (CST) Received: from [10.174.178.52] (10.174.178.52) by DGGEMS414-HUB.china.huawei.com (10.3.19.214) with Microsoft SMTP Server id 14.3.498.0; Mon, 21 Dec 2020 21:31:42 +0800 Subject: Re: [PATCH] kretprobe: avoid re-registration of the same kretprobe earlier To: Masami Hiramatsu CC: Steven Rostedt , , , , , , References: <20201124115719.11799-1-bobo.shaobowang@huawei.com> <20201130161850.34bcfc8a@gandalf.local.home> <20201202083253.9dbc76704149261e131345bf@kernel.org> <9dff21f8-4ab9-f9b2-64fd-cc8c5f731932@huawei.com> <20201215123119.35258dd5006942be247600db@kernel.org> From: "Wangshaobo (bobo)" Message-ID: Date: Mon, 21 Dec 2020 21:31:42 +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: <20201215123119.35258dd5006942be247600db@kernel.org> Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [10.174.178.52] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi steven, Masami, We have encountered a problem, when we attempted to use steven's suggestion as following, >>> 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; //there exists no-atomic operation risk, we should not modify any rp->kp's information, not all arch ensure atomic operation here. >>> >>> 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); it returns failure from register_kprobe() end called by register_kretprobe() when we registered a kretprobe through .symbol_name at first time(through .addr is OK), kprobe_addr() called at the begaining of register_kprobe() will recheck and failed at following place because at this time we symbol_name is not NULL and addr is also. static kprobe_opcode_t *_kprobe_addr(const char *symbol_name, unsigned int offset) { if ((symbol_name && addr) || (!symbol_name && !addr)) //we failed here So we attempted to move this sentence rp->kp.addr = addr to __get_valid_kprobe() like this to avoid explict usage of rp->kp.addr = addr in register_kretprobe(). diff --git a/kernel/kprobes.c b/kernel/kprobes.c index dd5821f753e6..ea014779edfe 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -1502,10 +1502,15 @@ static kprobe_opcode_t *kprobe_addr(struct kprobe *p) static struct kprobe *__get_valid_kprobe(struct kprobe *p) { struct kprobe *ap, *list_p; + void *addr; lockdep_assert_held(&kprobe_mutex); - ap = get_kprobe(p->addr); + addr = kprobe_addr(p); + if (IS_ERR(addr)) + return NULL; + + ap = get_kprobe(addr); if (unlikely(!ap)) return NULL; But it also failed when we second time attempted to register a same kretprobe, it is also becasue symbol_name and addr is not NULL when we used __get_valid_kprobe(). So it seems has no idea expect for modifying _kprobe_addr() like following this, the reason is that the patch 0bd476e6c671 ("kallsyms: unexport kallsyms_lookup_name() and kallsyms_on_each_symbol()") has telled us we'd better use symbol name to register but not address anymore. -static kprobe_opcode_t *_kprobe_addr(kprobe_opcode_t *addr, - const char *symbol_name, unsigned int offset) +static kprobe_opcode_t *_kprobe_addr(const char *symbol_name, + unsigned int offset) { - if ((symbol_name && addr) || (!symbol_name && !addr)) + kprobe_opcode_t *addr; + if (!symbol_name) goto invalid; For us, this modification has not caused a big impact on other modules, only expects a little influence on bpf from calling trace_kprobe_on_func_entry(), it can not use addr to fill in rp.kp in struct trace_event_call anymore. So i want to know your views, and i will resend this patch soon. thanks, Wang Shaobo >>> >