Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2632846pxj; Mon, 14 Jun 2021 03:34:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzPct3iIExKo7MV38DqGGqA3zOTh7InnQQBoeGq3jq4LIalxwzjEgNWj0Iw7dwKa4wo0fgm X-Received: by 2002:a05:6402:152:: with SMTP id s18mr15947278edu.221.1623666878983; Mon, 14 Jun 2021 03:34:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623666878; cv=none; d=google.com; s=arc-20160816; b=y7Kz0zq8ENt4GWp+qoUPyF/3npZSLhskfcwJ0UKfG+H/qT4VuRhdPAGbXrLeUAIF3Y gaZBL7MtCNlCi3A1Ve32tPefqimrgz+f0K4yF5WiDeCw6azS2NNbR6JNWxZDHqjbM3Y2 m2G6FKyUdtINIJtqtBst4OggZ4eMKOfUSb1v73I6DDAT2qZF9gFkz6qw2hP7QlNBmOEs pxkl3z8qiJ/hGFolL/c8EBmWMSdveTgLhrqCaoI3Z2dYZYyLp9flJxRNozARzuRca67h hIwhx/ic8O9KZi8PwHS92QYhQoGyh/KEFommkiPb+AccSzl669aD2w/OIRNvnfmv9bNK 4HRw== 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=RhlsYwoPTO2DsLg+nP3ZVzpde7X4NiVoKIfmgTJozkI=; b=nxyQ6oP+TmuydggjVZMFuUUctEFgPLFqv9Ih1NeY4TtrsVCdDv4KiiBZ0gBbRWdhlx 5KhPcI/KEax0PEOsB6KjtHxRCxKbtKWlSvzHuw0aHCIviHxAmXMtqUYdPkIeHI2b7GT3 7aqq74iqrYI9br2u/nokk0qsJPaKdXkMPCkbuweZjbBhfmwRLh7kY7WNIYjx+lUkI9So nHHp/jJwel3VTfbe5TT83ZgmoWmRSpYtCgNodfWY8IR7VpZnJz04ui0ke/U4N5uvTovr 1y6oFAIWYkVrC0B/nx1cpOS1TqFg8AUpEp7Y2DzxxgHu4gyXpC5y3KmrVgZylml0YUzy CUcg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=FAM8qqn3; 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 28si10464271ejk.511.2021.06.14.03.34.15; Mon, 14 Jun 2021 03:34:38 -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; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=FAM8qqn3; 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 S233041AbhFNKco (ORCPT + 99 others); Mon, 14 Jun 2021 06:32:44 -0400 Received: from mail.kernel.org ([198.145.29.99]:38272 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232943AbhFNKbn (ORCPT ); Mon, 14 Jun 2021 06:31:43 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 95C22611CA; Mon, 14 Jun 2021 10:29:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1623666573; bh=bmft2FcajagDR/odu6MXaQBmZdHh4zPQQTp5ndNVwSQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FAM8qqn3+a9Q5W9P3+E+bjUcaKSBfOT226A3C1Y0d5Z+Y2dmrtSY652BGxAotShFe dCXRbwwW6ZGuGjiEsHrWvvHiRtp/HiQlxxq0G09n/SH+xYuYUjSQdzlIepaUmvZ2Zw iQwD6fWOnFZ68WI+89wYD547fQC36PIJJV0WYyx0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+142c9018f5962db69c7e@syzkaller.appspotmail.com, Marco Elver , "Peter Zijlstra (Intel)" Subject: [PATCH 4.4 28/34] perf: Fix data race between pin_count increment/decrement Date: Mon, 14 Jun 2021 12:27:19 +0200 Message-Id: <20210614102642.487259384@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210614102641.582612289@linuxfoundation.org> References: <20210614102641.582612289@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: Marco Elver commit 6c605f8371159432ec61cbb1488dcf7ad24ad19a upstream. KCSAN reports a data race between increment and decrement of pin_count: write to 0xffff888237c2d4e0 of 4 bytes by task 15740 on cpu 1: find_get_context kernel/events/core.c:4617 __do_sys_perf_event_open kernel/events/core.c:12097 [inline] __se_sys_perf_event_open kernel/events/core.c:11933 ... read to 0xffff888237c2d4e0 of 4 bytes by task 15743 on cpu 0: perf_unpin_context kernel/events/core.c:1525 [inline] __do_sys_perf_event_open kernel/events/core.c:12328 [inline] __se_sys_perf_event_open kernel/events/core.c:11933 ... Because neither read-modify-write here is atomic, this can lead to one of the operations being lost, resulting in an inconsistent pin_count. Fix it by adding the missing locking in the CPU-event case. Fixes: fe4b04fa31a6 ("perf: Cure task_oncpu_function_call() races") Reported-by: syzbot+142c9018f5962db69c7e@syzkaller.appspotmail.com Signed-off-by: Marco Elver Signed-off-by: Peter Zijlstra (Intel) Link: https://lkml.kernel.org/r/20210527104711.2671610-1-elver@google.com Signed-off-by: Greg Kroah-Hartman --- kernel/events/core.c | 2 ++ 1 file changed, 2 insertions(+) --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -3497,7 +3497,9 @@ find_get_context(struct pmu *pmu, struct cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); ctx = &cpuctx->ctx; get_ctx(ctx); + raw_spin_lock_irqsave(&ctx->lock, flags); ++ctx->pin_count; + raw_spin_unlock_irqrestore(&ctx->lock, flags); return ctx; }