Received: by 2002:a05:7412:6592:b0:d7:7d3a:4fe2 with SMTP id m18csp986707rdg; Fri, 11 Aug 2023 06:30:49 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH8o6qF55A1ZGpuWfXsa1bwJp/XUgyWOA9vjxIzmOdG6Hkjvcv/eBiKYQ/SN86eSK2bUSlC X-Received: by 2002:a05:6a00:15d6:b0:641:3bf8:6514 with SMTP id o22-20020a056a0015d600b006413bf86514mr2460610pfu.10.1691760649310; Fri, 11 Aug 2023 06:30:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691760649; cv=none; d=google.com; s=arc-20160816; b=a+xAFiHxw2k6ODR+A4p8e6rxZ42+BZB2oH3Yk4JHvMX7NtpPRRI1zk1XiDmRiA//rj VBP152IUxPiVyg2wZcO28B1peTQhRiEtCRSLwdSy1xFTtfnHOFiq/R7gHzg6na35PhM5 ISJC8EAOREvxWEjhY2ps+o84D2vlYMkQF1QPQWhcE9yrzcxL2gfiOEStqWDmtSxR34mA 3CNQZpiuLzey+0Yiks4XPjTl6rZl9k3bdyyc+yROCf45GVj5OJsqhxjq1VYTEVu8MHQw 2FfZZFxAPVCG49mnXmrHxeTyR7Ax49UQICCjaJccaeP705oQZoial1Q7gYPpXp95l1UU hqyA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=6dTflH3+MkchVZh2u5ksB9shAwGL9MfCpsakhXqgFnI=; fh=R+/nfcasVgbIh7ypwyTp8eqiZg0RXNzv/0AybIOEhk4=; b=bdetWVYNx/HxkrOrZacnjFKf0/7TjqN74ynGXM7ZSls9Kqc/H0V+G1qszAD562wlEl Nsu4MYI+700dGTOGtNCJRAGydZtrPXyTdTL7QC8JeqgYzGeGsq8XodP8jKayPkUpILx0 vKowea76lnsaiyhfTq06wsclyFFbmcMYc2tPGS1hjcTas8/egMzkA2Fw4ePlFppGNmN1 1Y9LRrBNZp01DJSBO831Qfz1qj+Ivh7D+U3J2WAOaP9t8NSj9oUtnnqnTgON9Scy737I CaPGXPMMCq8PBvUQ2a7wnwPGEYNzeRdahuVRsaVfazyke9koEaD7pSChc1so8Y3RAqne q6Zg== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d20-20020a631d54000000b0054fdcafcc67si3345871pgm.604.2023.08.11.06.30.36; Fri, 11 Aug 2023 06:30:49 -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 S235359AbjHKMhL (ORCPT + 99 others); Fri, 11 Aug 2023 08:37:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55606 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229379AbjHKMhK (ORCPT ); Fri, 11 Aug 2023 08:37:10 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7012310C; Fri, 11 Aug 2023 05:37:10 -0700 (PDT) Received: from dggpeml500012.china.huawei.com (unknown [172.30.72.57]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4RMjtQ1Cm0zTmYh; Fri, 11 Aug 2023 20:35:10 +0800 (CST) Received: from [10.67.110.218] (10.67.110.218) 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; Fri, 11 Aug 2023 20:37:08 +0800 Message-ID: Date: Fri, 11 Aug 2023 20:37:07 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.6.0 Subject: Re: [PATCH] tracing: Fix race when concurrently splice_read trace_pipe Content-Language: en-US To: "Masami Hiramatsu (Google)" CC: , , , References: <20230810123905.1531061-1-zhengyejian1@huawei.com> <20230811204257.99df8ba60d591f5bace38615@kernel.org> From: Zheng Yejian In-Reply-To: <20230811204257.99df8ba60d591f5bace38615@kernel.org> Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [10.67.110.218] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpeml500012.china.huawei.com (7.185.36.15) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL, 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 On 2023/8/11 19:42, Masami Hiramatsu (Google) wrote: > On Thu, 10 Aug 2023 20:39:05 +0800 > Zheng Yejian wrote: > >> 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)) { > > It seems you skips '!iter->ent' check. Is there any reason for this change? IIUC, 'iter->ent' may be the entry that was found but not consumed in last call tracing_splice_read_pipe(), and in this call, 'iter->ent' may have being consumed, so we may should find a new 'iter->ent' before printing it in tracing_fill_pipe_page(), see following reduced codes: tracing_splice_read_pipe() { if (!iter->ent && !trace_find_next_entry_inc(iter)) { // 1. find entry here ... ... } tracing_fill_pipe_page() { for (;;) { ... ... ret = print_trace_line(iter); // 2. print entry ... ... if (!trace_find_next_entry_inc()) { // 3. find next entry ... ... break; } } -- Thanks, Zheng Yejian > > Thank you, > >> + 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); >> -- >> 2.25.1 >> > >