Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp370941rwb; Fri, 18 Nov 2022 02:42:01 -0800 (PST) X-Google-Smtp-Source: AA0mqf49mHDDXep63NZtK6lfhAaHfeL1PwRZ8UkEKLFtopp1hc0ZDfqdOtl3z64uxGLMi9MQtDp0 X-Received: by 2002:a17:902:8a81:b0:186:94b8:5982 with SMTP id p1-20020a1709028a8100b0018694b85982mr7143634plo.102.1668768121037; Fri, 18 Nov 2022 02:42:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668768121; cv=none; d=google.com; s=arc-20160816; b=YICFN+vnhkYN8R3WaOk51rEF+wYc3Ja+lG7cUouA7YsLE+ET9U/EtiaD3TV9P0Ef5m N1hGq9kJhYUS/cHDOXgUhk8YxzzUOTQd2Ymf8UHPU6/YWGA3/emgzXIly8PsFdM/r4qz gog2bmnUtMhYwP3YsMRPZNGddR8cAYoWeFDJs1oVz4aQiqxFwWfsL76Z4XPt4hmAf0Tx P6sgP8OXAC49vU5sY/F5+KyzSX81b+r87XDhbJBOQ9KRpvl4WrhxoGAvber7CgZKXpyO q0rvyvUuMTecxpRLSC0FkK0Rg9G7m2cHRvJh4CMThBZq5RvlN2QN+clcZXn3/inEy423 afIA== 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=mGBaITupHARP4RALGbOvB+V2hlxfFNKNoO8sqphx0Us=; b=OhU5M1WJI6R2x2PSkUcLFuYfxn52zP0NQ63uQyryp2HeOpZJo4i8zGcs5zydqUN1A7 tVI1/vNc/Dm0U4sfkxqjFaeReLkWdpLh4evgotLcdnxHcs8++lj/aicLJ6/fw8SIvpwX Wj7M3qCH7Luw580cJh3bBzVzS7WqSFw/dVYHdrX3Mn+slzUC0itVCTTYh1ha3Sn3SLSL vT5dY0+ID9v5E/sSLJC15cmNrw7X/qAXurjpo5mAHaiU2sk2M3stlNz3r6U9CyxBx3If 3Ht3so+pAqbuZB/GeKNzfM3bwNd13UW+8Ep/etb7Q90QopfAe3TJGSvSDEBsoVOcaghB N9Sw== 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 g10-20020a170902d1ca00b00186994f6e90si3000215plb.529.2022.11.18.02.41.45; Fri, 18 Nov 2022 02:42:01 -0800 (PST) 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 S241645AbiKRK2c (ORCPT + 91 others); Fri, 18 Nov 2022 05:28:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52856 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235171AbiKRK23 (ORCPT ); Fri, 18 Nov 2022 05:28:29 -0500 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D7768DF09 for ; Fri, 18 Nov 2022 02:28:28 -0800 (PST) Received: from dggemv703-chm.china.huawei.com (unknown [172.30.72.54]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4NDCfL5h3xzHvrW; Fri, 18 Nov 2022 18:27:54 +0800 (CST) Received: from kwepemm600003.china.huawei.com (7.193.23.202) by dggemv703-chm.china.huawei.com (10.3.19.46) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 18 Nov 2022 18:28:27 +0800 Received: from ubuntu1804.huawei.com (10.67.174.61) by kwepemm600003.china.huawei.com (7.193.23.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 18 Nov 2022 18:28:26 +0800 From: Yang Jihong To: , , CC: Subject: [PATCH v2] tracing: Fix infinite loop in tracing_read_pipe on overflowed print_trace_line Date: Fri, 18 Nov 2022 18:25:21 +0800 Message-ID: <20221118102521.62362-1-yangjihong1@huawei.com> X-Mailer: git-send-email 2.30.GIT MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.67.174.61] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To kwepemm600003.china.huawei.com (7.193.23.202) 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 print_trace_line may overflow seq_file buffer. If the event is not consumed, the while loop keeps peeking this event, causing a infinite loop. Signed-off-by: Yang Jihong --- Changes since v1: - Print partial line to show the broken trace event when overflowed print_trace_line kernel/trace/trace.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 47a44b055a1d..81c36dc80212 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -6786,7 +6786,32 @@ tracing_read_pipe(struct file *filp, char __user *ubuf, ret = print_trace_line(iter); if (ret == TRACE_TYPE_PARTIAL_LINE) { - /* don't print partial lines */ + /* + * If one trace_line of the tracer overflows seq_file + * buffer, trace_seq_to_user returns -EBUSY. + * In this case, we need to consume, otherwise, + * while loop will peek this event next time, + * resulting in an infinite loop. + */ + if (trace_seq_has_overflowed(&iter->seq)) { + /* + * Print the partial line, + * that way we can see the broken trace event. + */ + char dots[] = "...\n"; + + iter->seq.full = 0; + if (seq_buf_buffer_left(&iter->seq.seq) < strlen(dots)) { + iter->seq.seq.len = + seq_buf_used(&iter->seq.seq) - strlen(dots); + } + trace_seq_puts(&iter->seq, dots); + + trace_consume(iter); + break; + } + + /* In other cases, don't print partial lines */ iter->seq.seq.len = save_len; break; } -- 2.30.GIT