Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp1930740ybv; Fri, 14 Feb 2020 08:25:20 -0800 (PST) X-Google-Smtp-Source: APXvYqyLbfonCSQ9xSfBgCMLb4NkpwgBlh/vd5MxU+JzxQrnubLJNTqj1NRZ+T2LnDVFnUj2u3N2 X-Received: by 2002:a05:6830:128e:: with SMTP id z14mr2919889otp.184.1581697520745; Fri, 14 Feb 2020 08:25:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581697520; cv=none; d=google.com; s=arc-20160816; b=rWiHD3HeRIhzZCG2BkYfxt8MZMpPCTrCOM5EYIJ/vGN2pHcpQBQhYusHHfOBAdqFW4 OG8QFZnPmPZPNDP18FLJAPCWnwLuTPxyjfdXLZE3ojE9HY+HzxD3eHo6rIZgKa+F/BSl NOc/M6XWYrE7F8UF63lSb+Qeh1VCwgnCMM2z/i21ekkffY4TYoHryX9InGmQ7/s5OEo7 iRUrPQml1Vz+WVS5MFTpJ7CWDBsMXeh3lNrbugbgVOwBkMUfiW3/RsoFxIyT4ElHyu89 D1KyRacRNkXYwrWXZQ0dvmRaH+utThSV+ysrRBUBjniyNYapoFT+cKMbNEcmiL/jl9Te zCcA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=VHiLewSwHbxjpH/ELpwL7u3Ga9spl6zv4GnkYtlzVm4=; b=OIsXXpr27TJQuQAhIrheGBJDolkS0aL7MmgL5bxt/67kfZB1Q5sE9DV8v8K7rF8Exn btYkImmGxWItwHHcUhyzWROLNDoV/nA4KHNEP73DwmQVn4El3AgFtD+UZ7qnvCqfhzy0 kKK1h1ryZrGTaIf91OkbxXaXq5MQG5JC/4GneY4FTQijxWHG/wFn6GnGF63Rg8uKkI9f sUa1pSxYg4yJUX9RGXjB/z52AfZHJQhj9AnipmOSHj1QR7cx6ZBT1nnEgd5zpLZw/ZrX 0cruHHe6/QuvMJrYq5TkoVtzkSraTOaU2oWJDYltGcKD6XWDNXwFZy0y3P2KOW23OPSP EGRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=V8EjFA+O; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j1si3127105otn.53.2020.02.14.08.25.09; Fri, 14 Feb 2020 08:25:20 -0800 (PST) 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=V8EjFA+O; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2405675AbgBNQXg (ORCPT + 99 others); Fri, 14 Feb 2020 11:23:36 -0500 Received: from mail.kernel.org ([198.145.29.99]:56916 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2393134AbgBNQVz (ORCPT ); Fri, 14 Feb 2020 11:21:55 -0500 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E34B5246B0; Fri, 14 Feb 2020 16:21:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1581697315; bh=lB5TLfuGV58S6odYNjUEb5vwxlM7uKdv5QOqd/pCY1E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=V8EjFA+OHylbw8vZc2ASw+bwNHRV5tOHvq9RaxbqBKYmgWhXgs1ddGnBtt4E7ZeW/ wxnX10iiW8ivbvghm1ARDcbkWQLDED3pfOt5A1tMoG1Oti7xva85KIPP94V6izC880 OGyOFoHVMQ8x3ivbq/1isUoiaxswofOHbopQP7aE= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: "Steven Rostedt (VMware)" , Luis Henriques , Sasha Levin Subject: [PATCH AUTOSEL 4.9 026/141] tracing: Fix very unlikely race of registering two stat tracers Date: Fri, 14 Feb 2020 11:19:26 -0500 Message-Id: <20200214162122.19794-26-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200214162122.19794-1-sashal@kernel.org> References: <20200214162122.19794-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: "Steven Rostedt (VMware)" [ Upstream commit dfb6cd1e654315168e36d947471bd2a0ccd834ae ] Looking through old emails in my INBOX, I came across a patch from Luis Henriques that attempted to fix a race of two stat tracers registering the same stat trace (extremely unlikely, as this is done in the kernel, and probably doesn't even exist). The submitted patch wasn't quite right as it needed to deal with clean up a bit better (if two stat tracers were the same, it would have the same files). But to make the code cleaner, all we needed to do is to keep the all_stat_sessions_mutex held for most of the registering function. Link: http://lkml.kernel.org/r/1410299375-20068-1-git-send-email-luis.henriques@canonical.com Fixes: 002bb86d8d42f ("tracing/ftrace: separate events tracing and stats tracing engine") Reported-by: Luis Henriques Signed-off-by: Steven Rostedt (VMware) Signed-off-by: Sasha Levin --- kernel/trace/trace_stat.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/kernel/trace/trace_stat.c b/kernel/trace/trace_stat.c index bc97b10e56ccc..d19f2191960ea 100644 --- a/kernel/trace/trace_stat.c +++ b/kernel/trace/trace_stat.c @@ -305,7 +305,7 @@ static int init_stat_file(struct stat_session *session) int register_stat_tracer(struct tracer_stat *trace) { struct stat_session *session, *node; - int ret; + int ret = -EINVAL; if (!trace) return -EINVAL; @@ -316,17 +316,15 @@ int register_stat_tracer(struct tracer_stat *trace) /* Already registered? */ mutex_lock(&all_stat_sessions_mutex); list_for_each_entry(node, &all_stat_sessions, session_list) { - if (node->ts == trace) { - mutex_unlock(&all_stat_sessions_mutex); - return -EINVAL; - } + if (node->ts == trace) + goto out; } - mutex_unlock(&all_stat_sessions_mutex); + ret = -ENOMEM; /* Init the session */ session = kzalloc(sizeof(*session), GFP_KERNEL); if (!session) - return -ENOMEM; + goto out; session->ts = trace; INIT_LIST_HEAD(&session->session_list); @@ -335,15 +333,16 @@ int register_stat_tracer(struct tracer_stat *trace) ret = init_stat_file(session); if (ret) { destroy_session(session); - return ret; + goto out; } + ret = 0; /* Register */ - mutex_lock(&all_stat_sessions_mutex); list_add_tail(&session->session_list, &all_stat_sessions); + out: mutex_unlock(&all_stat_sessions_mutex); - return 0; + return ret; } void unregister_stat_tracer(struct tracer_stat *trace) -- 2.20.1