Received: by 2002:a05:6358:700f:b0:131:369:b2a3 with SMTP id 15csp3288842rwo; Fri, 4 Aug 2023 02:26:14 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGnMMERTNzSYnwIFXPQAkppSxdfcCEoBr33xqH9cQFgbEZzO1RGz9AAR/Ee4VBQ4JotVkpT X-Received: by 2002:aca:e1d6:0:b0:3a7:3374:9a43 with SMTP id y205-20020acae1d6000000b003a733749a43mr1081655oig.57.1691141174029; Fri, 04 Aug 2023 02:26:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691141173; cv=none; d=google.com; s=arc-20160816; b=WKQvsPevzROoRMXSCcBaJ/rfqx892Y2SFgf8Io+ZPxWqTHGQxzaogj+0iuOTtulmQZ G+Y5R2zycK9VT/gQqQXj1CakTQxD/1Uar/u8I1hp9XOg8XobObRk8tx3p+/G3W9JUoAg PSLZUEzB7BXcEjaYu6TanPaVI4rry7xf9DKJMjUrxHDqED/gW1S6PxIUiOx79jrhRC1l TXQJOAcG+QUb9msuwr1xIspSFPV3Jmg4MCgmuYANMvs/MnDz3xbY4kh08csTKK3VMobC alANrKdU+WlV7Ws4QLjHcFNkn7CqsiOa4oWNo0RzPWnN6B3kJuT099wobSBBhOyZBrEO rQJQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=+iHBGhJXynwnhQCVuVl9Ews8lxiOyVuD+Lg2g9CnSDU=; fh=LKLX59HcJ2CQdeGZoBuQOQ27p+qXqfA7CYen3cQobjs=; b=Buha6Ys5Nqc1Y2aPdLFYuVtGnbHiXJ6jIMl9KY/8fa9hhBjCPMOx+2m04xvdD8q7ze tKHu8+P8jl3IBkpW8EjrogLjd61gwPg5HGaROKQRu1VztvZNo6faYafkdufD0t6vXdE1 ZM91NJez12W/iJekfVl5CBu/WE1tUdii3L4YcT/H/aR52PxH+yNlFwQn3oOaRsMi+Jbk kC07QtCWXG5gCf9oF6fHxZtww12dpfyNutS/xpCwX9LTZOxfoGT79VSnccvqrOeWAxi5 XXMdK3kGwbhVLMLGZ4byLaz1d+yU2M2yS7pwqA3yC0NCTsyKZpkGq2r9qN0ZXQYP892D +jvA== 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=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id nu6-20020a17090b1b0600b0024e4ace1a41si1719295pjb.183.2023.08.04.02.26.02; Fri, 04 Aug 2023 02:26:13 -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=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234460AbjHDIPa (ORCPT + 99 others); Fri, 4 Aug 2023 04:15:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53738 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234456AbjHDIP2 (ORCPT ); Fri, 4 Aug 2023 04:15:28 -0400 Received: from out30-97.freemail.mail.aliyun.com (out30-97.freemail.mail.aliyun.com [115.124.30.97]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8114E12B for ; Fri, 4 Aug 2023 01:15:27 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R451e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018045170;MF=tianruidong@linux.alibaba.com;NM=1;PH=DS;RN=8;SR=0;TI=SMTPD_---0Vp.kW5O_1691136922; Received: from localhost(mailfrom:tianruidong@linux.alibaba.com fp:SMTPD_---0Vp.kW5O_1691136922) by smtp.aliyun-inc.com; Fri, 04 Aug 2023 16:15:24 +0800 From: Ruidong Tian To: linux-kernel@vger.kernel.org Cc: tianruidong@linux.alibaba.com, alexander.shishkin@linux.intel.com, coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, mike.leach@linaro.org, suzuki.poulose@arm.com, james.clark@arm.com Subject: [PATCH v3 1/1] coresight: tmc: Explicit type conversions to prevent integer overflow Date: Fri, 4 Aug 2023 16:15:14 +0800 Message-Id: <20230804081514.120171-2-tianruidong@linux.alibaba.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20230804081514.120171-1-tianruidong@linux.alibaba.com> References: <20230714084349.31567-1-tianruidong@linux.alibaba.com> <20230804081514.120171-1-tianruidong@linux.alibaba.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS, UNPARSEABLE_RELAY,URIBL_BLOCKED,USER_IN_DEF_SPF_WL 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 Perf cs_etm session executed unexpectedly when AUX buffer > 1G. perf record -C 0 -m ,2G -e cs_etm// -- [ perf record: Captured and wrote 2.615 MB perf.data ] Perf only collect about 2M perf data rather than 2G. This is becasuse the operation, "nr_pages << PAGE_SHIFT", in coresight tmc driver, will overflow when nr_pages >= 0x80000(correspond to 1G AUX buffer). The overflow cause buffer allocation to fail, and TMC driver will alloc minimal buffer size(1M). You can just get about 2M perf data(1M AUX buffer + perf data header) at least. Explicit convert nr_pages to 64 bit to avoid overflow. Fixes: 22f429f19c41 ("coresight: etm-perf: Add support for ETR backend") Signed-off-by: Ruidong Tian Reviewed-by: James Clark --- drivers/hwtracing/coresight/coresight-tmc-etf.c | 2 +- drivers/hwtracing/coresight/coresight-tmc-etr.c | 5 +++-- drivers/hwtracing/coresight/coresight-tmc.h | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/drivers/hwtracing/coresight/coresight-tmc-etf.c b/drivers/hwtracing/coresight/coresight-tmc-etf.c index 79d8c64eac49..7406b65e2cdd 100644 --- a/drivers/hwtracing/coresight/coresight-tmc-etf.c +++ b/drivers/hwtracing/coresight/coresight-tmc-etf.c @@ -452,7 +452,7 @@ static int tmc_set_etf_buffer(struct coresight_device *csdev, return -EINVAL; /* wrap head around to the amount of space we have */ - head = handle->head & ((buf->nr_pages << PAGE_SHIFT) - 1); + head = handle->head & (((unsigned long)buf->nr_pages << PAGE_SHIFT) - 1); /* find the page to write to */ buf->cur = head / PAGE_SIZE; diff --git a/drivers/hwtracing/coresight/coresight-tmc-etr.c b/drivers/hwtracing/coresight/coresight-tmc-etr.c index 766325de0e29..66dc5f97a009 100644 --- a/drivers/hwtracing/coresight/coresight-tmc-etr.c +++ b/drivers/hwtracing/coresight/coresight-tmc-etr.c @@ -45,7 +45,8 @@ struct etr_perf_buffer { }; /* Convert the perf index to an offset within the ETR buffer */ -#define PERF_IDX2OFF(idx, buf) ((idx) % ((buf)->nr_pages << PAGE_SHIFT)) +#define PERF_IDX2OFF(idx, buf) \ + ((idx) % ((unsigned long)(buf)->nr_pages << PAGE_SHIFT)) /* Lower limit for ETR hardware buffer */ #define TMC_ETR_PERF_MIN_BUF_SIZE SZ_1M @@ -1267,7 +1268,7 @@ alloc_etr_buf(struct tmc_drvdata *drvdata, struct perf_event *event, * than the size requested via sysfs. */ if ((nr_pages << PAGE_SHIFT) > drvdata->size) { - etr_buf = tmc_alloc_etr_buf(drvdata, (nr_pages << PAGE_SHIFT), + etr_buf = tmc_alloc_etr_buf(drvdata, ((ssize_t)nr_pages << PAGE_SHIFT), 0, node, NULL); if (!IS_ERR(etr_buf)) goto done; diff --git a/drivers/hwtracing/coresight/coresight-tmc.h b/drivers/hwtracing/coresight/coresight-tmc.h index b97da39652d2..0ee48c5ba764 100644 --- a/drivers/hwtracing/coresight/coresight-tmc.h +++ b/drivers/hwtracing/coresight/coresight-tmc.h @@ -325,7 +325,7 @@ ssize_t tmc_sg_table_get_data(struct tmc_sg_table *sg_table, static inline unsigned long tmc_sg_table_buf_size(struct tmc_sg_table *sg_table) { - return sg_table->data_pages.nr_pages << PAGE_SHIFT; + return (unsigned long)sg_table->data_pages.nr_pages << PAGE_SHIFT; } struct coresight_device *tmc_etr_get_catu_device(struct tmc_drvdata *drvdata); -- 2.33.1