From patchwork Thu Apr 4 03:35:38 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mathieu Poirier X-Patchwork-Id: 10884851 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 589FC1575 for ; Thu, 4 Apr 2019 03:52:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 35A41286D5 for ; Thu, 4 Apr 2019 03:52:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2A08828A45; Thu, 4 Apr 2019 03:52:29 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 85709286D5 for ; Thu, 4 Apr 2019 03:52:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=pjCT24T8aJha/SF5wN6cnNb4iYv1PrT60+iQA+xNYzg=; b=g6qEVVEoChdbvsKOzVWKD7NVDl 44QMvr/o48UF+MYW77v6jaWotfrE97JZpltzE01NzxpVHAg5QbrP8MtohmfyHCMpedHC9bWlNt/jG a6Tow68ItNjpvIbHPcs7dR5ZI3rAaRDG7OxalKiTqe1s0PE0pA3NcJeHzFrb8oUX5KrTsWv51e1oI L+MKW/w0D2PDqPL5wVlSCq8PFlHUqDFQChFshmfnhMGAojt4fpXHJ6FGU0+9KJ5rToCNdv8EbKoDG M87CZSFkWAqAKWf3K/A9pUSdRDAIK9k8UxrnURehYxpa4rxpgGFeKlK77vLGDTXIVGxWJ6Eo1v7y0 IHz6i8Pg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1hBtQI-00038H-JA; Thu, 04 Apr 2019 03:52:18 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1hBtPw-0002DC-2S for linux-arm-kernel@bombadil.infradead.org; Thu, 04 Apr 2019 03:51:56 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=References:In-Reply-To:Message-Id:Date: Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=kFYUPXqDT+4NKa1uTQaXULpv7Y+YefzCDvbN138zMnI=; b=ETos6PM+xFmMgyWix9FurwfzL 4hg278yNQcd9fXisacycnhF2XE5UNISmkaVZwpXetm84/YN1atRgLsCY4kVh8cKjkgliFazypEUvp lZWQQrSlN6+Uf1i+wW0XGI1h6sF5XSKgopAg6b5RrF7EczAUjpIGQdvg97p5Dxe/Zz9YLJoQzWimx RbJgQDxcwBhjSAKE0lUYRiGVkhiO/lw44+vgwshflKt+GGqH+Bzv/1NQY1PaSnHD66f9HQZyXQBbz B4WtcgfRbhZkEvTIIasw8LeH3CLHfE/PLddQ/0H3uEiSOZ6GR8PQAF+sJz5s7FgzNmV1yVRp5pas1 R81IFma0w==; Received: from mail-pf1-x444.google.com ([2607:f8b0:4864:20::444]) by casper.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1hBtBN-0008LR-4x for linux-arm-kernel@lists.infradead.org; Thu, 04 Apr 2019 03:36:56 +0000 Received: by mail-pf1-x444.google.com with SMTP id c8so613571pfd.10 for ; Wed, 03 Apr 2019 20:36:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=kFYUPXqDT+4NKa1uTQaXULpv7Y+YefzCDvbN138zMnI=; b=LTWy3uKHie6dKXq48SuUSLDOFWqCbGplQB45KxnD/vCJcF5L/sa/6SJCsJfosIpCPl vKFmCf6HgO8KQKoD1jtzcVSjz7N+SN/Vumg/EhLmnN98dw6WYr3+RXOJrXl5KlF3LOO/ ggUt2/E4oD0dsHEWIQ8WZTik71totE9jL6xSGHHwC+3bR7dOnPQTVaHHz7PcMHHdAzp8 iHehk1++FQ943S5EtIOSoTJKk6P+ZEKkjpaSMak+FOY7m8refm9ZzDNl+7bnuAwwmmK1 YBNM9mtRz+VTcMmRuCg7nFKbz6ofMXDes8uQJykGMOlO1RKxwfW1K62fR75y1GMZEIrv Oo2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=kFYUPXqDT+4NKa1uTQaXULpv7Y+YefzCDvbN138zMnI=; b=NCo83piVXPLQ3j8WRvE+XxajoZ7IBIEosfdGer/dGJZ7vn3zjo23MKwreQ9+CAPU/K iS4AbsFobuwj4toR5XlP8scsAgC9EEKCNtWy/uGmdKDCk6rGTdeS9GaKmj3MwB0GBB5/ /C2jcdoXwnPnrKugtSuanuzUUG9d345vDXRxWMU64fjzph4c6KcxsehqCvr6oqGIScPe H83Kv7AwXoSYrU+2hi6tsR89+Q+zfksYmjH+NxATLj/5NzsOzWaV2s4wJYvSVE/uG3Kc dyyzPRtJJn1LXcTQzDA3lDRU++A0US8sVR1pINvyx5acdKzs3LYQeVZ+b+T/Rn4SxP/B BepA== X-Gm-Message-State: APjAAAWy1Z5n+o5pFRBbwG6zbqxktrFdb72S4P8op7gqFVEDFQneiLbQ 6vv9l3UqxWQ6e4N3BfwtoiTlkNIGyJ0= X-Google-Smtp-Source: APXvYqxx/cle2EssAtFjyb90I0PMaH5UqnhmroQOmHt53z8YZ32ZmNf5mj3nCTrCe2iY/q7XgHIcjg== X-Received: by 2002:a63:4e57:: with SMTP id o23mr3467140pgl.368.1554349011296; Wed, 03 Apr 2019 20:36:51 -0700 (PDT) Received: from xps15.imgcgcw.net ([147.50.13.10]) by smtp.gmail.com with ESMTPSA id u5sm12212780pfm.121.2019.04.03.20.36.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 03 Apr 2019 20:36:50 -0700 (PDT) From: Mathieu Poirier To: linux-arm-kernel@lists.infradead.org Subject: [PATCH v3 17/20] coresight: tmc-etr: Allocate and free ETR memory buffers for CPU-wide scenarios Date: Wed, 3 Apr 2019 21:35:38 -0600 Message-Id: <20190404033541.14072-18-mathieu.poirier@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190404033541.14072-1-mathieu.poirier@linaro.org> References: <20190404033541.14072-1-mathieu.poirier@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190404_043653_300621_961484AE X-CRM114-Status: GOOD ( 28.20 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: suzuki.poulose@arm.com, alexander.shishkin@linux.intel.com, coresight@lists.linaro.org, linux-kernel@vger.kernel.org, peterz@infradead.org, mike.leach@arm.com, leo.yan@linaro.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP This patch uses the PID of the process being traced to allocate and free ETR memory buffers for CPU-wide scenarios. The implementation is tailored to handle both N:1 and 1:1 source/sink HW topologies. Signed-off-by: Mathieu Poirier --- .../hwtracing/coresight/coresight-tmc-etr.c | 107 +++++++++++++++++- 1 file changed, 104 insertions(+), 3 deletions(-) diff --git a/drivers/hwtracing/coresight/coresight-tmc-etr.c b/drivers/hwtracing/coresight/coresight-tmc-etr.c index 1346474ac019..61110ef41d00 100644 --- a/drivers/hwtracing/coresight/coresight-tmc-etr.c +++ b/drivers/hwtracing/coresight/coresight-tmc-etr.c @@ -8,6 +8,8 @@ #include #include #include +#include +#include #include #include #include @@ -26,6 +28,7 @@ struct etr_flat_buf { /* * etr_perf_buffer - Perf buffer used for ETR + * @drvdata - The ETR drvdaga this buffer has been allocated for. * @etr_buf - Actual buffer used by the ETR * @pid - The PID this etr_perf_buffer belongs to. * @snaphost - Perf session mode @@ -34,6 +37,7 @@ struct etr_flat_buf { * @pages - Array of Pages in the ring buffer. */ struct etr_perf_buffer { + struct tmc_drvdata *drvdata; struct etr_buf *etr_buf; pid_t pid; bool snapshot; @@ -1210,6 +1214,72 @@ alloc_etr_buf(struct tmc_drvdata *drvdata, struct perf_event *event, return etr_buf; } +static struct etr_buf * +get_perf_etr_buf_cpu_wide(struct tmc_drvdata *drvdata, + struct perf_event *event, int nr_pages, + void **pages, bool snapshot) +{ + int ret; + pid_t pid = task_pid_nr(event->owner); + struct etr_buf *etr_buf; + +retry: + /* + * An etr_perf_buffer is associated with an event and holds a reference + * to the AUX ring buffer that was created for that event. In CPU-wide + * N:1 mode multiple events (one per CPU), each with its own AUX ring + * buffer, share a sink. As such an etr_perf_buffer is created for each + * event but a single etr_buf associated with the ETR is shared between + * them. The last event in a trace session will copy the content of the + * etr_buf to its AUX ring buffer. Ring buffer associated to other + * events are simply not used an freed as events are destoyed. We still + * need to allocate a ring buffer for each event since we don't know + * which event will be last. + */ + + /* + * The first thing to do here is check if an etr_buf has already been + * allocated for this session. If so it is shared with this event, + * otherwise it is created. + */ + mutex_lock(&drvdata->idr_mutex); + etr_buf = idr_find(&drvdata->idr, pid); + if (etr_buf) { + refcount_inc(&etr_buf->refcount); + mutex_unlock(&drvdata->idr_mutex); + return etr_buf; + } + + /* If we made it here no buffer has been allocated, do so now. */ + mutex_unlock(&drvdata->idr_mutex); + + etr_buf = alloc_etr_buf(drvdata, event, nr_pages, pages, snapshot); + if (IS_ERR(etr_buf)) + return etr_buf; + + refcount_set(&etr_buf->refcount, 1); + + /* Now that we have a buffer, add it to the IDR. */ + mutex_lock(&drvdata->idr_mutex); + ret = idr_alloc(&drvdata->idr, etr_buf, pid, pid + 1, GFP_KERNEL); + mutex_unlock(&drvdata->idr_mutex); + + /* Another event with this session ID has allocated this buffer. */ + if (ret == -ENOSPC) { + tmc_free_etr_buf(etr_buf); + goto retry; + } + + /* The IDR can't allocate room for a new session, abandon ship. */ + if (ret == -ENOMEM) { + tmc_free_etr_buf(etr_buf); + return ERR_PTR(ret); + } + + + return etr_buf; +} + static struct etr_buf * get_perf_etr_buf_per_thread(struct tmc_drvdata *drvdata, struct perf_event *event, int nr_pages, @@ -1238,7 +1308,8 @@ get_perf_etr_buf(struct tmc_drvdata *drvdata, struct perf_event *event, return get_perf_etr_buf_per_thread(drvdata, event, nr_pages, pages, snapshot); - return ERR_PTR(-ENOENT); + return get_perf_etr_buf_cpu_wide(drvdata, event, nr_pages, + pages, snapshot); } static struct etr_perf_buffer * @@ -1265,7 +1336,13 @@ tmc_etr_setup_perf_buf(struct tmc_drvdata *drvdata, struct perf_event *event, return ERR_PTR(-ENOMEM); done: + /* + * Keep a reference to the ETR this buffer has been allocated for + * in order to have access to the IDR in tmc_free_etr_buffer(). + */ + etr_perf->drvdata = drvdata; etr_perf->etr_buf = etr_buf; + return etr_perf; } @@ -1295,9 +1372,33 @@ static void *tmc_alloc_etr_buffer(struct coresight_device *csdev, static void tmc_free_etr_buffer(void *config) { struct etr_perf_buffer *etr_perf = config; + struct tmc_drvdata *drvdata = etr_perf->drvdata; + struct etr_buf *buf, *etr_buf = etr_perf->etr_buf; + + if (!etr_buf) + goto free_etr_perf_buffer; + + mutex_lock(&drvdata->idr_mutex); + /* If we are not the last one to use the buffer, don't touch it. */ + if (!refcount_dec_and_test(&etr_buf->refcount)) { + mutex_unlock(&drvdata->idr_mutex); + goto free_etr_perf_buffer; + } + + /* We are the last one, remove from the IDR and free the buffer. */ + buf = idr_remove(&drvdata->idr, etr_perf->pid); + mutex_unlock(&drvdata->idr_mutex); + + /* + * Something went very wrong if the buffer associated with this ID + * is not the same in the IDR. Leak to avoid use after free. + */ + if (buf && WARN_ON(buf != etr_buf)) + goto free_etr_perf_buffer; + + tmc_free_etr_buf(etr_perf->etr_buf); - if (etr_perf->etr_buf) - tmc_free_etr_buf(etr_perf->etr_buf); +free_etr_perf_buffer: kfree(etr_perf); }