Received: by 2002:a25:b794:0:0:0:0:0 with SMTP id n20csp2748094ybh; Mon, 5 Aug 2019 06:14:22 -0700 (PDT) X-Google-Smtp-Source: APXvYqy5pj2F+rAfWHr+q8BxTzvkgEbHtuT/rnya233uqd9K3jLqpQUcZyt1y4Tdk60mWKty8fgp X-Received: by 2002:a63:2a08:: with SMTP id q8mr104074252pgq.415.1565010862598; Mon, 05 Aug 2019 06:14:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565010862; cv=none; d=google.com; s=arc-20160816; b=R6fk9mEcKOgrZUUeX2zlX2mh/wRzCC90J5c53LQm60aoSOOSkBS+617w42dFzs12rG xFLFWwTA+KcSAMFDRR0G1wSwqpmMXEjrta/sWa6eL8h662aDRas3lgw/734ZloCnKtuO CU11cj9SppwasdTbG8Va4pAekPL8WH2xXr8fOi/CyRkyiuZjgsxChhXso81Gteuwd2F9 Ri24Je5rlbr03gjpiwdOblYypXk1YTJDP54+QwuYMiyio0UGpgLS8YfiJ+Ja8j/TNbrO J1F2yHMaYhiPsIz5WJNntR3fYnGAvUt3Vz13As5ln0uPQYNhcKw4d+MId+BRWf9JxYQ/ cGeg== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=048LjQGmSONCZlvF9e0N3ULWAhLihYISZ0VhWZWObvw=; b=qMGvH4IFHlVrDqEYlWtGFaYEYwEWRGUH6BtphD5SJU5mP1bqa33pGtgLurAPm4oCKJ DLUBD2UzoQEQs/ByAAKQhAk7LffqvzQBlQIBBT65uIi7OBxwCowmxCl2jVinVdkbq2Bf BKE70xVE2PKnp+Vb7w7+1bk0Uhi6gbK0z4a+3dv0Im1ygDHXYAzRx1gVMpCmqnEMVRDp bKEJBwhF7iZU/dmoQVQcGQ9bPG+TJLrI4u/m4pHSieT0osEkOzfsCc+9sAQpijraGv2z 0xxlGq4jiNiw14JOKb35FsGT5MLQMYxyr7RJKIbEUpdnz3508fbT/hUyez65dxE6dJhJ q1yA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=tRmYn7+x; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 4si46470256pfo.266.2019.08.05.06.14.06; Mon, 05 Aug 2019 06:14:22 -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=tRmYn7+x; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730180AbfHENJ0 (ORCPT + 99 others); Mon, 5 Aug 2019 09:09:26 -0400 Received: from mail.kernel.org ([198.145.29.99]:47848 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729481AbfHENJW (ORCPT ); Mon, 5 Aug 2019 09:09:22 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (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 1F57820880; Mon, 5 Aug 2019 13:09:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1565010561; bh=Y/saj7d4SLB8Q8gEoJMJ2nzNrLOPRub4op9n2z48xSU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tRmYn7+xKtoJEztS11+6I2LpYZLlOSv1MJFKsPWviAW0hKTSJ3TpUQbqm2icU+SQ7 uem8ni5uJwrY77619huh3XFpyv5EIKo07i4zabMo47kkc5+yIftYr0hx4obtqh5/Ml GdcKNZioKpph56RUK6a7CqERqKTvpM8UQMHHiFzc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Cheng Jian , "Steven Rostedt (VMware)" , Sasha Levin Subject: [PATCH 4.19 05/74] ftrace: Enable trampoline when rec count returns back to one Date: Mon, 5 Aug 2019 15:02:18 +0200 Message-Id: <20190805124936.245763927@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190805124935.819068648@linuxfoundation.org> References: <20190805124935.819068648@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [ Upstream commit a124692b698b00026a58d89831ceda2331b2e1d0 ] Custom trampolines can only be enabled if there is only a single ops attached to it. If there's only a single callback registered to a function, and the ops has a trampoline registered for it, then we can call the trampoline directly. This is very useful for improving the performance of ftrace and livepatch. If more than one callback is registered to a function, the general trampoline is used, and the custom trampoline is not restored back to the direct call even if all the other callbacks were unregistered and we are back to one callback for the function. To fix this, set FTRACE_FL_TRAMP flag if rec count is decremented to one, and the ops that left has a trampoline. Testing After this patch : insmod livepatch_unshare_files.ko cat /sys/kernel/debug/tracing/enabled_functions unshare_files (1) R I tramp: 0xffffffffc0000000(klp_ftrace_handler+0x0/0xa0) ->ftrace_ops_assist_func+0x0/0xf0 echo unshare_files > /sys/kernel/debug/tracing/set_ftrace_filter echo function > /sys/kernel/debug/tracing/current_tracer cat /sys/kernel/debug/tracing/enabled_functions unshare_files (2) R I ->ftrace_ops_list_func+0x0/0x150 echo nop > /sys/kernel/debug/tracing/current_tracer cat /sys/kernel/debug/tracing/enabled_functions unshare_files (1) R I tramp: 0xffffffffc0000000(klp_ftrace_handler+0x0/0xa0) ->ftrace_ops_assist_func+0x0/0xf0 Link: http://lkml.kernel.org/r/1556969979-111047-1-git-send-email-cj.chengjian@huawei.com Signed-off-by: Cheng Jian Signed-off-by: Steven Rostedt (VMware) Signed-off-by: Sasha Levin --- kernel/trace/ftrace.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c index 118ecce143866..d9dd709b3c12f 100644 --- a/kernel/trace/ftrace.c +++ b/kernel/trace/ftrace.c @@ -1647,6 +1647,11 @@ static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec) return keep_regs; } +static struct ftrace_ops * +ftrace_find_tramp_ops_any(struct dyn_ftrace *rec); +static struct ftrace_ops * +ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops); + static bool __ftrace_hash_rec_update(struct ftrace_ops *ops, int filter_hash, bool inc) @@ -1775,15 +1780,17 @@ static bool __ftrace_hash_rec_update(struct ftrace_ops *ops, } /* - * If the rec had TRAMP enabled, then it needs to - * be cleared. As TRAMP can only be enabled iff - * there is only a single ops attached to it. - * In otherwords, always disable it on decrementing. - * In the future, we may set it if rec count is - * decremented to one, and the ops that is left - * has a trampoline. + * The TRAMP needs to be set only if rec count + * is decremented to one, and the ops that is + * left has a trampoline. As TRAMP can only be + * enabled if there is only a single ops attached + * to it. */ - rec->flags &= ~FTRACE_FL_TRAMP; + if (ftrace_rec_count(rec) == 1 && + ftrace_find_tramp_ops_any(rec)) + rec->flags |= FTRACE_FL_TRAMP; + else + rec->flags &= ~FTRACE_FL_TRAMP; /* * flags will be cleared in ftrace_check_record() @@ -1976,11 +1983,6 @@ static void print_ip_ins(const char *fmt, const unsigned char *p) printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]); } -static struct ftrace_ops * -ftrace_find_tramp_ops_any(struct dyn_ftrace *rec); -static struct ftrace_ops * -ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops); - enum ftrace_bug_type ftrace_bug_type; const void *ftrace_expected; -- 2.20.1