Received: by 2002:a05:7412:6592:b0:d7:7d3a:4fe2 with SMTP id m18csp1342485rdg; Fri, 11 Aug 2023 19:49:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF0Ti7zDpTV4N439qSG+QrDqkoC8kk7N8tvW/FIcLjusisvj5FRcUEQGCfsu88BELtlGy7d X-Received: by 2002:a05:6a20:2d7:b0:133:be16:509 with SMTP id 23-20020a056a2002d700b00133be160509mr4335433pzb.34.1691808568597; Fri, 11 Aug 2023 19:49:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691808568; cv=none; d=google.com; s=arc-20160816; b=rA/v5bCj6fCEBPH2YYwKNOaBFFwKIr3vPNvF/qHgHXfgl7wPd1Z1rZdxx66c5IRBDw 8ljvy5J7oFWdb15o+n3UHcv7/53XZnY7weXTCE///kMVvw9pfs80zAypheDXWCM+J5RN YqHM8SQ1UVOAt34RzE358hnTWXk6T0BvOsLHcpGTJieF9pp+tiQIo5brCW5dDGbuLOq/ Dg9HcpPrHW1gSCvsH0q64zeA5yuuP4dKkoa8pGFOWJqVLcy5rHJGjl4wc06b4w/W65Ae TiF1vdP7yu5JNMwKi192ue88gPUJw6RCZNJnkaUJEw8fWirj1EomGOLqW/fv4x3XaEMd KnCA== 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=rgEKft7VUfzvXEGNqvqVOWKZO3unFkBDEcCy1Ko1NFI=; fh=upu+m3hZGlvkSCyESXkpus+1flGG70eQuK5ANHNBDqs=; b=KJ1qmA0f1OEZiCO5fFVLK0gTnp4kdfDJQB2jSutX66qSt/vr8xu2fkJUOm2yWVQNrJ 9GQZzCr05QbxGGK/JvKH9nUgVclbnnF8Hl82I5oX70ugVWOfZWfM8ZRkU6J2gZUGCMeq 9tKLjgv98yALEq1Sfu6nGpfGidEKuIjEaeaxVjZIm3jVzk8T+a4o7ZEyNKgdmHMldKMW 8wQQOunFPufbD3eleRJODKHDUddP7l9RTclKqBHze2Laje300wL9+oCw74VufWmK9Whu 9fMAZBKtz2La92ElI4PK1Iql5oKgd3dqswwZsb61gJQFNK+U/SQIU1CMxvj6h80xdmOT z43Q== 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 l184-20020a6388c1000000b00563f49e7ba8si4211095pgd.831.2023.08.11.19.49.16; Fri, 11 Aug 2023 19:49:28 -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 S235522AbjHLCWr (ORCPT + 99 others); Fri, 11 Aug 2023 22:22:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229802AbjHLCWq (ORCPT ); Fri, 11 Aug 2023 22:22:46 -0400 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 89430127; Fri, 11 Aug 2023 19:22:45 -0700 (PDT) Received: from dggpeml500012.china.huawei.com (unknown [172.30.72.54]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4RN4Cs0bV0z1GDPH; Sat, 12 Aug 2023 10:21:29 +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; Sat, 12 Aug 2023 10:22:43 +0800 Message-ID: <0b83d0e5-5fe6-0cfb-4695-23c2cb86526d@huawei.com> Date: Sat, 12 Aug 2023 10:22:43 +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: Steven Rostedt CC: "Masami Hiramatsu (Google)" , , , References: <20230810123905.1531061-1-zhengyejian1@huawei.com> <20230811204257.99df8ba60d591f5bace38615@kernel.org> <20230811152413.76d5b72e@gandalf.local.home> From: Zheng Yejian In-Reply-To: <20230811152413.76d5b72e@gandalf.local.home> Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [10.67.110.218] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) 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,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/12 03:24, Steven Rostedt wrote: > On Fri, 11 Aug 2023 20:37:07 +0800 > Zheng Yejian wrote: > >> 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: > > And if it wasn't consumed? We just lost it? If 'iter->ent' was not consumed, trace_find_next_entry_inc() will find it again, will it? -- Zheng Yejian > >> >> 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 >> ... ... > > You missed: > > count = trace_seq_used(&iter->seq) - save_len; > if (rem < count) { > rem = 0; > iter->seq.seq.len = save_len; > > Where the above just threw away what was printed in the above > "print_trace_line()", and it never went to console. > > break; > } > Thanks for pointing this out! -- Zheng Yejian > -- Steve > > >> 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 >>>> >>> >>> > >