From patchwork Thu Aug 10 12:39:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zheng Yejian X-Patchwork-Id: 134008 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp451261vqi; Thu, 10 Aug 2023 07:16:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFBx0n/NJ19KZPZ4e1fSzhqWFN06pGWGSXVx4wvQDwWLlBUde0J7qwRRMcnDayreRBE4j3Q X-Received: by 2002:a05:6a20:1019:b0:13a:6bca:7a84 with SMTP id gs25-20020a056a20101900b0013a6bca7a84mr2633652pzc.44.1691676966808; Thu, 10 Aug 2023 07:16:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691676966; cv=none; d=google.com; s=arc-20160816; b=Ex3U88IDi5kHJJxTlBy+FSG9vfdAkJiOuwLwENhF403d/Aobcpr4MSheOjjQl7Fdr9 P/MjgbGCX3a9HeCAqVHn3dD7VdZHszrocbmEvQt0GjSeEhnDJe/tbiTLYBrslee5n62G 5UxA8UpgZoG13zSx+yf6uKEUrmvN9ZCPWrGPmbs6bUM3w13c6U0XIUBqBpCdCaTQnIk0 WTArbou+7CwPUrkjiXYX6AODnfX22W0PW4kd5pLDpS/xGZD5+E9uiHt/fQ/tdgLBu2zX OBqvbnPlK+70Dzoa/+O2hvhAAoc+ZEnhT1cM48Gzq6G693RK3Yeju6sTAb+cyK6NKm4L 8Tjw== 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 :message-id:date:subject:cc:to:from; bh=RToxi3+5skr3eI7hdsMTJH3wa008g0BiAQLhVxt+3V0=; fh=xCdvb0pchHpICiVB7Yft0edV6bsZADbcLyom6OXel/s=; b=wdKmIRM0h2v4x9ekFyVc/ECzFWf6EvWvsefEptOw+3djL2jv10GzrS7T0FGJTfQvZ/ fZTu7DLQrxZyirZjt7aLZUDpvKuDZeM3OCkkoUPSWqeD+aQ6w4aW7yzMIIMNkQt1ljlU ddwipGODCGkdLsjYIJtg77heJI1emOZxZpy8btGbm9IP5VTstbUDvtvR+/0jlmIkXEml okfnc3iKgLIKeagjQAuOR8myAzw5s+kmBwSNH1yLer3MT1CXts/TwmbvBp5u6Y6NH1qI hWWz8libkUxs22KqoA26uXfioham1qh8pyApIhLN6QDVPErT4m6rweDBWa9dfc24a4L6 WVXw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 132-20020a63028a000000b0055798b90375si1716865pgc.709.2023.08.10.07.15.43; Thu, 10 Aug 2023 07:16:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234003AbjHJMji (ORCPT + 99 others); Thu, 10 Aug 2023 08:39:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38814 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229631AbjHJMjh (ORCPT ); Thu, 10 Aug 2023 08:39:37 -0400 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EAE85212F; Thu, 10 Aug 2023 05:39:36 -0700 (PDT) Received: from dggpeml500012.china.huawei.com (unknown [172.30.72.54]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4RM60Z2SKrz1L9wc; Thu, 10 Aug 2023 20:38:22 +0800 (CST) Received: from localhost.localdomain (10.67.175.61) by dggpeml500012.china.huawei.com (7.185.36.15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Thu, 10 Aug 2023 20:39:34 +0800 From: Zheng Yejian To: , CC: , , , Subject: [PATCH] tracing: Fix race when concurrently splice_read trace_pipe Date: Thu, 10 Aug 2023 20:39:05 +0800 Message-ID: <20230810123905.1531061-1-zhengyejian1@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Originating-IP: [10.67.175.61] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpeml500012.china.huawei.com (7.185.36.15) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773851866982128409 X-GMAIL-MSGID: 1773851866982128409 When concurrently splice_read file trace_pipe and per_cpu/cpu*/trace_pipe, there are more data being read out than expected. The root cause is that in tracing_splice_read_pipe(), an entry is found outside locks, it may be read by multiple readers or consumed by other reader as starting printing it. To fix it, change to find entry after holding locks. Fixes: 7e53bd42d14c ("tracing: Consolidate protection of reader access to the ring buffer") Signed-off-by: Zheng Yejian --- kernel/trace/trace.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index b8870078ef58..f169d33b948f 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -7054,14 +7054,16 @@ static ssize_t tracing_splice_read_pipe(struct file *filp, if (ret <= 0) goto out_err; - if (!iter->ent && !trace_find_next_entry_inc(iter)) { + trace_event_read_lock(); + trace_access_lock(iter->cpu_file); + + if (!trace_find_next_entry_inc(iter)) { + trace_access_unlock(iter->cpu_file); + trace_event_read_unlock(); ret = -EFAULT; goto out_err; } - trace_event_read_lock(); - trace_access_lock(iter->cpu_file); - /* Fill as many pages as possible. */ for (i = 0, rem = len; i < spd.nr_pages_max && rem; i++) { spd.pages[i] = alloc_page(GFP_KERNEL);