Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp676189ybh; Tue, 10 Mar 2020 06:17:57 -0700 (PDT) X-Google-Smtp-Source: ADFU+vv584Qrl3gRUUm7techp7V2DCfousdTFKUkR6BqXuy/tHHxfaInTXycu+PULy5b38nynhUR X-Received: by 2002:a05:6830:2105:: with SMTP id i5mr16276697otc.141.1583846277551; Tue, 10 Mar 2020 06:17:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1583846277; cv=none; d=google.com; s=arc-20160816; b=zfq3sok2YY7bjWzhBqUkPG8yA+7SJSEIJdHxfLeydWv69OU6HsuRA5u12Gnv370THP +VQ4DzfTIzGi0sy61W8Q1TVuRdcM73wMZnFJHkdLnMHJ3iWRxPkdu+w6TRw8oas2zYXc XYcjW5Ds5lNk2FIyVCk06+kjbH9lUKx3PCUpG9b4ar40RVGHR+zEkMmQ+Pzrx1bSuPZx BkbnEpxSzjA3G0V8l67MNK76g7bU0q5UGD92u3kqssnHqFWYQAuBPe+GRxVR8yj2uk+O nq3KI3AhgFhEFU75Cf6GX4DoNhqnyqfXTp6MT4cfXnxvXatS+Nj1Y5JqeVYFomCip8NJ 2I+g== 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=ZvX7Sym9e8Bfimp9fC7Xm41DIzduPYoHBJdd71RrnkM=; b=kAPlNBrzBgj2MeY4+cub/2+7F3NZy3wlnNtSW7C3JTVjItJibp6PHxw6E4jvQlIxPe k6T2VhvvReEj+Y735o0nDsc4YZbR+7Ti/mCjc8Tm6oQFxWnen3UfjSSqo1yGf51TQWp0 QPKa4cQjEF5uYGGdcfjH6D+HAOw824KgtkG3GVKwJuwG+ypl3Bh83IBlR/KpdH7GWjy5 AQK2bhwsa9jSMBSNVs4aAWhnfu5jO3bWZhVHlA894k+FKGnCE5Oak6FwAIHdkW0ANta6 BgVhr6It3mwei6ijrFvNmMq479mTv6A9i2/Twu6tbbbfGmtAopFuvK/ud8cFVO90qNDj 8OOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=r9YvFjD2; 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 r194si2005318oie.66.2020.03.10.06.17.44; Tue, 10 Mar 2020 06:17:57 -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=r9YvFjD2; 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 S1731536AbgCJNML (ORCPT + 99 others); Tue, 10 Mar 2020 09:12:11 -0400 Received: from mail.kernel.org ([198.145.29.99]:34222 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731531AbgCJNMK (ORCPT ); Tue, 10 Mar 2020 09:12:10 -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 4E38024691; Tue, 10 Mar 2020 13:12:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1583845929; bh=5QtnYJUNesSsA553RhbH24ReMP7ISeWfaAXIhQL7htI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=r9YvFjD2IY8KQHFMB2JF/YPoEUVOTZBbAo8lQtHHTNe6+hfIrX8qoB6aI42mAIdiH hw+L4zaOu36g1lcyuic7y3HNlsVLIhW3woZ9SKesF59sR3sCH9znkkIjFdhb4T49Gk uCj9OJT9sZj2PX0LwxUsf8O4Kd1TNOOn/mYon3x8= 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.19 06/86] kprobes: Fix optimize_kprobe()/unoptimize_kprobe() cancellation logic Date: Tue, 10 Mar 2020 13:44:30 +0100 Message-Id: <20200310124531.148780809@linuxfoundation.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200310124530.808338541@linuxfoundation.org> References: <20200310124530.808338541@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 00050a22f6a12..92aad49b82f9c 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