Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp9418038pxu; Mon, 28 Dec 2020 15:51:12 -0800 (PST) X-Google-Smtp-Source: ABdhPJzSHUgsnltloaI5uwm2cgs7nTlYyPP3ceNeAFt26Re1oHXsFw6EHPNMVbKqwxeAhRk4euBA X-Received: by 2002:a17:906:3c11:: with SMTP id h17mr42647583ejg.20.1609199471973; Mon, 28 Dec 2020 15:51:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609199471; cv=none; d=google.com; s=arc-20160816; b=Ku59r47NoTjXPZlW1Kp+Q1Gb4/QafPCRLC0Dqw4xIUVbA72iFj/O0n81KSth/n//DQ zmkN2NWpXqKZlhntlDOVXIYWRnqiWWVBlvZBAXbEAfaWeG0XLQXCQkET3KfZzMKKmuoc UcIzEYIb6+Wm6VaVPOY1dzzCaMsvmzkH3xQgCj7bRA96btzRtBjsYqO7ffklis2MqbaO 68X0AnR+fBviXqvcTqdpaywNgDmWi4w6VHHv67yLhMZEqau8bKuJnC8V/18L7pRqHBWd koM3FtArD+Ndg1UaslpazJUplLdmBUo/1YbSOfnIm7AN3iSIlJ3zkHOZmBrn/lwxElkb WtPA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=UUPZlQFbm4jNR2XUEROnhthKeVrXNgW+atVJyb1hAGc=; b=ksVShEOARmpdjzii7AispkI9Er0QWLqIVtjqp9qAHkWZBsc6hdyZ2lmnrkCEoQEtPE WoAKcZn7GLJaFMra4Vqk5MQ6xnDtiUk9KLWalQdOZGftlSca7CtrDHSknsQVrx/0FREj KLDTWPwsyzEndVX3laqdrh8hmdu8RjnyI8kaR7CDICvRgE8O2qGX8GGJhJNL91r55w88 xDVrSzdf4tTU50TLZQ3ruWfzr8XBXN/Uk0SOjScl2t88eoZArFaT1fXqXaIZNa0/PCcM 1aJrjoZqT3pZHI6mbK1hT1EhdvBFOnnBrtcwLFU4O1YXoIehfIkW6UBKNgBvDkHlhhni +Ruw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=uY2q7+bd; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v7si19085684ejr.236.2020.12.28.15.50.50; Mon, 28 Dec 2020 15:51:11 -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; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=uY2q7+bd; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2505732AbgL1Ojg (ORCPT + 99 others); Mon, 28 Dec 2020 09:39:36 -0500 Received: from mail.kernel.org ([198.145.29.99]:37236 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2503674AbgL1O3n (ORCPT ); Mon, 28 Dec 2020 09:29:43 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 0E59B2063A; Mon, 28 Dec 2020 14:29:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1609165767; bh=nyx47SjlkERTqGlLQ6jggC8WWVcJ966K7g9l1ftwhnI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uY2q7+bdVFfDVJP7NgEGXpgIMDoBVFZYSM/3tp7VA7mh6Jngqefeq8wmoRqWELoow zOTrkRGPo384MBKoevoWx1rNyGG2zslRAd5xAH9xJTJmcHHHeCcZ7MetwGIp0kdjls RjZYgryD6+Ealogwi4BtoUDfCnxbSIoAWnQmhVZo= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, David Hildenbrand , Oscar Salvador , Michael Ellerman Subject: [PATCH 5.10 617/717] powerpc/powernv/memtrace: Fix crashing the kernel when enabling concurrently Date: Mon, 28 Dec 2020 13:50:15 +0100 Message-Id: <20201228125050.482097286@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201228125020.963311703@linuxfoundation.org> References: <20201228125020.963311703@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: David Hildenbrand commit d6718941a2767fb383e105d257d2105fe4f15f0e upstream. It's very easy to crash the kernel right now by simply trying to enable memtrace concurrently, hammering on the "enable" interface loop.sh: #!/bin/bash dmesg --console-off while true; do echo 0x40000000 > /sys/kernel/debug/powerpc/memtrace/enable done [root@localhost ~]# loop.sh & [root@localhost ~]# loop.sh & Resulting quickly in a kernel crash. Let's properly protect using a mutex. Fixes: 9d5171a8f248 ("powerpc/powernv: Enable removal of memory for in memory tracing") Cc: stable@vger.kernel.org# v4.14+ Signed-off-by: David Hildenbrand Reviewed-by: Oscar Salvador Signed-off-by: Michael Ellerman Link: https://lore.kernel.org/r/20201111145322.15793-3-david@redhat.com Signed-off-by: Greg Kroah-Hartman --- arch/powerpc/platforms/powernv/memtrace.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) --- a/arch/powerpc/platforms/powernv/memtrace.c +++ b/arch/powerpc/platforms/powernv/memtrace.c @@ -30,6 +30,7 @@ struct memtrace_entry { char name[16]; }; +static DEFINE_MUTEX(memtrace_mutex); static u64 memtrace_size; static struct memtrace_entry *memtrace_array; @@ -279,6 +280,7 @@ static int memtrace_online(void) static int memtrace_enable_set(void *data, u64 val) { + int rc = -EAGAIN; u64 bytes; /* @@ -291,25 +293,31 @@ static int memtrace_enable_set(void *dat return -EINVAL; } + mutex_lock(&memtrace_mutex); + /* Re-add/online previously removed/offlined memory */ if (memtrace_size) { if (memtrace_online()) - return -EAGAIN; + goto out_unlock; } - if (!val) - return 0; + if (!val) { + rc = 0; + goto out_unlock; + } /* Offline and remove memory */ if (memtrace_init_regions_runtime(val)) - return -EINVAL; + goto out_unlock; if (memtrace_init_debugfs()) - return -EINVAL; + goto out_unlock; memtrace_size = val; - - return 0; + rc = 0; +out_unlock: + mutex_unlock(&memtrace_mutex); + return rc; } static int memtrace_enable_get(void *data, u64 *val)