Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp340215pxk; Thu, 24 Sep 2020 06:58:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzuXKkKuQiZRMc7vdDYpCuk3iOChGaXWmfM0esBImB/yVvA0rxCT2r1K91mLnxE0B6RNeKh X-Received: by 2002:a17:906:3ca2:: with SMTP id b2mr15522ejh.460.1600955938858; Thu, 24 Sep 2020 06:58:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600955938; cv=none; d=google.com; s=arc-20160816; b=vUkSed/8FoBssWP8Wn/Itv5np2qyZ+kISIABqKKkMovBw2QJ+WcYYVZFcMbiNG2Hod Rx+Kl4g5tn4PTwbk0RxT7AATN9QCAuDQ6RVLZQKuseuU/tqnTYHWshs1qugMntXJ0G1k ZAAYVbZ/I6quLof4C4MBdJZRiMKImM2pKyBj0BgZk9yZikYJONq1If/RQUbM6xlHgi4x tbsPf/FcNDFrcUGw177Qbn6ZUj8zaQQw0/NPdYd2dhZU/T+Gghaw1pHh/D+tnMNUZJWJ 8/Qgx9OKU5EK0LjrTh4QUZB+AM2WhGlZBjW95NWGYKZNlKyU1Jsry4Zzth7ROYufCNma e8HA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from; bh=frr+Y6z08ZAEdDzFcZg1r+VXiHE55UW1nd01jNvH4Sw=; b=j4edambJLrakZq1LfsgzIMOs1De5CzJFN48JBuIs55bhiAnuyDzKy/+0G9rf3cOPnT mAuvmBPZlaMF7AUpIEqf5TqEppXqVmRJTulSg5hyYc1lf1059GBONyhpa/3L+TlkbXSj WYpRVQ671UPEbplZsXaWfQmVvvBDFKGlzaRbwuKSqlpw0in5NcJAaKywQDo8ZFQpzfdL PYnXP2QSME050H/dvBB0hL8xH1Sl+wsBJwJgDoOL4WOqHFG91NHzaldqfCVwvTHWnPel Kt6ashs7LiXH0TXJJuUguly3PvoomNVXP1HmaT+CsMZKiq4sk7fMMoAwlX6lB11AzGAT /6TQ== 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 z34si2629313ede.447.2020.09.24.06.58.34; Thu, 24 Sep 2020 06:58:58 -0700 (PDT) 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 S1728065AbgIXNzX (ORCPT + 99 others); Thu, 24 Sep 2020 09:55:23 -0400 Received: from alexa-out.qualcomm.com ([129.46.98.28]:46521 "EHLO alexa-out.qualcomm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727749AbgIXNzW (ORCPT ); Thu, 24 Sep 2020 09:55:22 -0400 Received: from ironmsg09-lv.qualcomm.com ([10.47.202.153]) by alexa-out.qualcomm.com with ESMTP; 24 Sep 2020 06:55:21 -0700 Received: from ironmsg02-blr.qualcomm.com ([10.86.208.131]) by ironmsg09-lv.qualcomm.com with ESMTP/TLS/AES256-SHA; 24 Sep 2020 06:55:19 -0700 Received: from gkohli-linux.qualcomm.com ([10.204.78.26]) by ironmsg02-blr.qualcomm.com with ESMTP; 24 Sep 2020 19:25:07 +0530 Received: by gkohli-linux.qualcomm.com (Postfix, from userid 427023) id 7FBC621323; Thu, 24 Sep 2020 19:25:06 +0530 (IST) From: Gaurav Kohli To: rostedt@goodmis.org Cc: linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org, Gaurav Kohli , stable@vger.kernel.org Subject: [PATCH v1] trace: Fix race in trace_open and buffer resize call Date: Thu, 24 Sep 2020 19:25:05 +0530 Message-Id: <1600955705-27382-1-git-send-email-gkohli@codeaurora.org> X-Mailer: git-send-email 2.7.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Below race can come, if trace_open and resize of cpu buffer is running parallely on different cpus CPUX CPUY ring_buffer_resize atomic_read(&buffer->resize_disabled) tracing_open tracing_reset_online_cpus ring_buffer_reset_cpu rb_reset_cpu rb_update_pages remove/insert pages resetting pointer This race can cause data abort or some times infinte loop in rb_remove_pages and rb_insert_pages while checking pages for sanity. Signed-off-by: Gaurav Kohli Cc: stable@vger.kernel.org --- Changes since v0: -Addressed Steven's review comments. diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 93ef0ab..15bf28b 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -4866,6 +4866,9 @@ void ring_buffer_reset_cpu(struct trace_buffer *buffer, int cpu) if (!cpumask_test_cpu(cpu, buffer->cpumask)) return; + /* prevent another thread from changing buffer sizes */ + mutex_lock(&buffer->mutex); + atomic_inc(&cpu_buffer->resize_disabled); atomic_inc(&cpu_buffer->record_disabled); @@ -4876,6 +4879,8 @@ void ring_buffer_reset_cpu(struct trace_buffer *buffer, int cpu) atomic_dec(&cpu_buffer->record_disabled); atomic_dec(&cpu_buffer->resize_disabled); + + mutex_unlock(&buffer->mutex); } EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu); @@ -4889,6 +4894,9 @@ void ring_buffer_reset_online_cpus(struct trace_buffer *buffer) struct ring_buffer_per_cpu *cpu_buffer; int cpu; + /* prevent another thread from changing buffer sizes */ + mutex_lock(&buffer->mutex); + for_each_online_buffer_cpu(buffer, cpu) { cpu_buffer = buffer->buffers[cpu]; @@ -4907,6 +4915,8 @@ void ring_buffer_reset_online_cpus(struct trace_buffer *buffer) atomic_dec(&cpu_buffer->record_disabled); atomic_dec(&cpu_buffer->resize_disabled); } + + mutex_unlock(&buffer->mutex); } /** -- Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project