Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp680086ybh; Tue, 10 Mar 2020 06:21:51 -0700 (PDT) X-Google-Smtp-Source: ADFU+vspFUifGPdU/XH0ZoPK6Al5UJJlWaZmFKBEK/GIVoFz57sSqeksUUFDXic9lsSjQP2ktI7K X-Received: by 2002:a05:6830:4b1:: with SMTP id l17mr9079586otd.275.1583846511689; Tue, 10 Mar 2020 06:21:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1583846511; cv=none; d=google.com; s=arc-20160816; b=FRks50oUXH7FcfJlP3GSNOT89WtA99WriV17Oj4t5gDjfVQRPFDoFVXuUHp+zGAq7s PnclncFOMHEI5BqntCsVd238ggBPAYIUuaitpKvWiJ0v72IcbrnSqGmLHfkq9OWQRctd ChjN470rj94aW3R2hc5kfXWyPCiZSs6rux11ipGvNphJJ5l8ap7MT6SVp84czGpwq7io CopGT+Y+WxPqNFKmC27k/s0sFU9WndjCQidplevLEfDU4CDhpuFFySOeFoo0HtYky2dv RXEQ54kRrhZ2NUg86Tk6H69cB1lw2nrDVT/AGe/iw6Tc26v74nAN6wP/kbOZy56vLj9Y dIXg== 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=m+0F6y0CksLM2u3AjxjMPGIRxenBVpiUs1A4IFybHhI=; b=xWclox5fPfVVi2QpiI8YoXXVmHw0S0lcB4ODt+jornqpd174cgLNpnm1DAlNHQEez4 sFbKMjcI/zXlt6iDm5iWhOSq+aE26fCCd3vVs/OP/q0yizRrCflRDKdRrSdJ6VnRoOVX byYUQHabFYOP5dcEVeTVdMF6wCGIklNhazJ933NBzeb5RMO9e4J8V+JvO8709GLwTIjd sTIHZC2fH0LTM9J3y/UlhQBi71j1iovm69GaBsmi/cDlTEtmT8V5fHECwSSaq6UJsbi2 8aR+NTDnMeBG77uHyTqm4AVRa1wiYWeZTd6C3f5vi8zhkZtA8/dCWibeK9LMoonw7Oqc LSqA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=fyOTKiw1; 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 l23si5339926oii.20.2020.03.10.06.21.38; Tue, 10 Mar 2020 06:21:51 -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=fyOTKiw1; 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 S1730582AbgCJNIh (ORCPT + 99 others); Tue, 10 Mar 2020 09:08:37 -0400 Received: from mail.kernel.org ([198.145.29.99]:55092 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731117AbgCJNIf (ORCPT ); Tue, 10 Mar 2020 09:08:35 -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 706D120409; Tue, 10 Mar 2020 13:08:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1583845713; bh=a/S5nGQZLSk83xkWXDu61r6mGcJa5cWNkEOt3jJGtic=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fyOTKiw1bWx8QKOnFx9YJq79ZkhV5htibtEcH+mQJ+WIjAY0fasP3VnsicJRpLK+R bR5IEy2ksLQII1VDtULJDbbHhdUil0mAdcSLrik5rMAZKy1ULZdUSnGSQqYN3Mkam7 8y3WR3WHIunkHKrnWmZm5/+4FyDy49PkJ0X6fMQ4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Masami Hiramatsu , "Steven Rostedt (VMware)" , Alexei Starovoitov , Peter Zijlstra , Thomas Gleixner , bristot@redhat.com, Ingo Molnar , Sasha Levin Subject: [PATCH 4.14 073/126] kprobes: Fix optimize_kprobe()/unoptimize_kprobe() cancellation logic Date: Tue, 10 Mar 2020 13:41:34 +0100 Message-Id: <20200310124208.657258685@linuxfoundation.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200310124203.704193207@linuxfoundation.org> References: <20200310124203.704193207@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 From: Masami Hiramatsu [ Upstream commit e4add247789e4ba5e08ad8256183ce2e211877d4 ] optimize_kprobe() and unoptimize_kprobe() cancels if a given kprobe is on the optimizing_list or unoptimizing_list already. However, since the following commit: f66c0447cca1 ("kprobes: Set unoptimized flag after unoptimizing code") modified the update timing of the KPROBE_FLAG_OPTIMIZED, it doesn't work as expected anymore. The optimized_kprobe could be in the following states: - [optimizing]: Before inserting jump instruction op.kp->flags has KPROBE_FLAG_OPTIMIZED and op->list is not empty. - [optimized]: jump inserted op.kp->flags has KPROBE_FLAG_OPTIMIZED and op->list is empty. - [unoptimizing]: Before removing jump instruction (including unused optprobe) op.kp->flags has KPROBE_FLAG_OPTIMIZED and op->list is not empty. - [unoptimized]: jump removed op.kp->flags doesn't have KPROBE_FLAG_OPTIMIZED and op->list is empty. Current code mis-expects [unoptimizing] state doesn't have KPROBE_FLAG_OPTIMIZED, and that can cause incorrect results. To fix this, introduce optprobe_queued_unopt() to distinguish [optimizing] and [unoptimizing] states and fixes the logic in optimize_kprobe() and unoptimize_kprobe(). [ mingo: Cleaned up the changelog and the code a bit. ] Signed-off-by: Masami Hiramatsu Reviewed-by: Steven Rostedt (VMware) Cc: Alexei Starovoitov Cc: Peter Zijlstra Cc: Thomas Gleixner Cc: bristot@redhat.com Fixes: f66c0447cca1 ("kprobes: Set unoptimized flag after unoptimizing code") Link: https://lkml.kernel.org/r/157840814418.7181.13478003006386303481.stgit@devnote2 Signed-off-by: Ingo Molnar Signed-off-by: Sasha Levin --- kernel/kprobes.c | 67 +++++++++++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 24 deletions(-) diff --git a/kernel/kprobes.c b/kernel/kprobes.c index 48bf93bbb22e2..66f1818d47620 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -625,6 +625,18 @@ void wait_for_kprobe_optimizer(void) mutex_unlock(&kprobe_mutex); } +static bool optprobe_queued_unopt(struct optimized_kprobe *op) +{ + struct optimized_kprobe *_op; + + list_for_each_entry(_op, &unoptimizing_list, list) { + if (op == _op) + return true; + } + + return false; +} + /* Optimize kprobe if p is ready to be optimized */ static void optimize_kprobe(struct kprobe *p) { @@ -646,17 +658,21 @@ static void optimize_kprobe(struct kprobe *p) return; /* Check if it is already optimized. */ - if (op->kp.flags & KPROBE_FLAG_OPTIMIZED) + if (op->kp.flags & KPROBE_FLAG_OPTIMIZED) { + if (optprobe_queued_unopt(op)) { + /* This is under unoptimizing. Just dequeue the probe */ + list_del_init(&op->list); + } return; + } op->kp.flags |= KPROBE_FLAG_OPTIMIZED; - if (!list_empty(&op->list)) - /* This is under unoptimizing. Just dequeue the probe */ - list_del_init(&op->list); - else { - list_add(&op->list, &optimizing_list); - kick_kprobe_optimizer(); - } + /* On unoptimizing/optimizing_list, op must have OPTIMIZED flag */ + if (WARN_ON_ONCE(!list_empty(&op->list))) + return; + + list_add(&op->list, &optimizing_list); + kick_kprobe_optimizer(); } /* Short cut to direct unoptimizing */ @@ -678,30 +694,33 @@ static void unoptimize_kprobe(struct kprobe *p, bool force) return; /* This is not an optprobe nor optimized */ op = container_of(p, struct optimized_kprobe, kp); - if (!kprobe_optimized(p)) { - /* Unoptimized or unoptimizing case */ - if (force && !list_empty(&op->list)) { - /* - * Only if this is unoptimizing kprobe and forced, - * forcibly unoptimize it. (No need to unoptimize - * unoptimized kprobe again :) - */ - list_del_init(&op->list); - force_unoptimize_kprobe(op); - } + if (!kprobe_optimized(p)) return; - } if (!list_empty(&op->list)) { - /* Dequeue from the optimization queue */ - list_del_init(&op->list); + if (optprobe_queued_unopt(op)) { + /* Queued in unoptimizing queue */ + if (force) { + /* + * Forcibly unoptimize the kprobe here, and queue it + * in the freeing list for release afterwards. + */ + force_unoptimize_kprobe(op); + list_move(&op->list, &freeing_list); + } + } else { + /* Dequeue from the optimizing queue */ + list_del_init(&op->list); + op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED; + } return; } + /* Optimized kprobe case */ - if (force) + if (force) { /* Forcibly update the code: this is a special case */ force_unoptimize_kprobe(op); - else { + } else { list_add(&op->list, &unoptimizing_list); kick_kprobe_optimizer(); } -- 2.20.1