From patchwork Mon Sep 28 16:34:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mathieu Poirier X-Patchwork-Id: 11804077 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3EBCF139A for ; Mon, 28 Sep 2020 16:38:48 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 11C422080C for ; Mon, 28 Sep 2020 16:38:48 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="DzyXNngT"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="SKEOX13/" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 11C422080C Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version: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=9nqVQ6jvcSuajiA0KOeDwaDA7T474nflt37H+ZG/xTI=; b=DzyXNngTNNzJEJBAt70O8dBfS M7xe2uc+0uqAoRwIhlinIj2lvbVRd8T2CS5dJXmjKeJeF8lBke0iWuuUwqs/IH9zdAipU2FKSxDQW sNqdPFTFGZimGIxD55usrnQAMkcNzicoEf2By/fWTx7uUquVvlmTjRxqqQG+rgdkc2d70T/Idm1D/ gAjZueAggrK1/QibbnR6HCDepQkmrkSKVb5Bs3ug08bT+yOc82v7fNsh3XRh0CGleCEPRY60AZtx6 HqJhSqzGl8l47yx3uhdpfakHGuIDxN+V4g10D3Pk6OVKJ4v3xgvMhzFS6PMAPqVCpuLoI+mQ2gBs9 B0wKszt+Q==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kMw8a-0000vZ-45; Mon, 28 Sep 2020 16:36:28 +0000 Received: from mail-pf1-x441.google.com ([2607:f8b0:4864:20::441]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kMw7X-0000XN-9f for linux-arm-kernel@lists.infradead.org; Mon, 28 Sep 2020 16:35:28 +0000 Received: by mail-pf1-x441.google.com with SMTP id d9so1531018pfd.3 for ; Mon, 28 Sep 2020 09:35:23 -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 :mime-version:content-transfer-encoding; bh=Ez4LWIQoeyGf2VTxB83p0ifF8EHclgO0IOs2psbJ0hU=; b=SKEOX13/WnvoWJd9IhPVayw5UH+4LkiPWZmUtQ6QVkceYFlowc61HnDyhaQRfC7q3n IRHXvJFSsIcvH3PXoyFuESFdztXAPJRiYeXfNQ1i2FBnpu8Gh0/qaFykKcjuG7Iqk/FZ fHvXAu08YsGb0I0NNm3Of+Lme9zidVmtDbKyiOUj9yMWl3HweHjFxOX/kWuCSzL52/z0 7C3v/6nImfrG/Q+DumpD+mFCMDCy5/z3mkvp37ryW2eBkTCihHw886rIoJJl3ps/UtA1 RRXB/PB8Cag00NCjmpiP6iqpqs4u8e2GZowtzsKtYjzBJ/xiRcVAR9UtQUj9wPbUp8lr J4Gw== 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:mime-version:content-transfer-encoding; bh=Ez4LWIQoeyGf2VTxB83p0ifF8EHclgO0IOs2psbJ0hU=; b=HPr1fMQOE7aDV8+MdcqROydDEDsg+mOcU+rpi45Dv9oly+fL34h9/Vt0TqlquoeJ/v AEegLvdOHL29CPWXA1iVFW4phtzvoL044SbWwgOKNn3cDViHPr1PZlJwsIy7GlNENAVj 7N9qWfNO3ksiCf/fa5Aq+zuHe3CENupgh0qFhr3EZMXJQXQdUlNl/MtIJkzWLOLu7PfC ggSyvX8pjtv7FgfX6QJtkGHmR+vvnGb8szDk3aovhBBa1yLVGf4sUzUJYROUDONh5WZb ZVGmvBTcAPng83LsZkgjUc91uIZ+VErBw+d3uPJ2oVZlBmj4dulHqRvZV2lbuCKVu5YR ZAdw== X-Gm-Message-State: AOAM530eSHImoQaQ14LJ+sUpbQ69A51eDMWpJslIaRWoiw4KtwfLLXHx 3RlIv7NUXtUg/bSD63qTdK+BBg== X-Google-Smtp-Source: ABdhPJxPedhVcg8S3n31HR9Hw3o8M9RSKN43o1bmDzOqCJZmi4xgcBsE3zwzb6QqNCmsBvlq3TpT6g== X-Received: by 2002:a62:cfc4:0:b029:142:440e:231d with SMTP id b187-20020a62cfc40000b0290142440e231dmr218346pfg.7.1601310921347; Mon, 28 Sep 2020 09:35:21 -0700 (PDT) Received: from xps15.cg.shawcable.net (S0106002369de4dac.cg.shawcable.net. [68.147.8.254]) by smtp.gmail.com with ESMTPSA id f19sm2345640pfd.45.2020.09.28.09.35.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Sep 2020 09:35:20 -0700 (PDT) From: Mathieu Poirier To: gregkh@linuxfoundation.org Subject: [PATCH 06/25] coresight: Add try_get_module() in coresight_grab_device() Date: Mon, 28 Sep 2020 10:34:54 -0600 Message-Id: <20200928163513.70169-7-mathieu.poirier@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200928163513.70169-1-mathieu.poirier@linaro.org> References: <20200928163513.70169-1-mathieu.poirier@linaro.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200928_123523_407158_D1DE6F54 X-CRM114-Status: GOOD ( 23.22 ) X-Spam-Score: -0.2 (/) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (-0.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:441 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org From: Tingwei Zhang When coresight device is in an active session, driver module of that device should not be removed. Use try_get_module() in coresight_grab_device() to prevent module to be unloaded. Use get_device()/put_device() to protect device data in the middle of active session. Suggested-by: Suzuki K Poulose Signed-off-by: Tingwei Zhang Tested-by: Mike Leach Reviewed-by: Suzuki K Poulose Signed-off-by: Mathieu Poirier --- drivers/hwtracing/coresight/coresight.c | 60 ++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 7 deletions(-) diff --git a/drivers/hwtracing/coresight/coresight.c b/drivers/hwtracing/coresight/coresight.c index d1aea3a1e7b6..4faa4aa3964d 100644 --- a/drivers/hwtracing/coresight/coresight.c +++ b/drivers/hwtracing/coresight/coresight.c @@ -629,13 +629,45 @@ struct coresight_device *coresight_get_sink_by_id(u32 id) return dev ? to_coresight_device(dev) : NULL; } +/** + * coresight_get_ref- Helper function to increase reference count to module + * and device. + * Return true in successful case and power up the device. + * Return false when failed to get reference of module. + */ +static inline bool coresight_get_ref(struct coresight_device *csdev) +{ + struct device *dev = csdev->dev.parent; + + /* Make sure the driver can't be removed */ + if (!try_module_get(dev->driver->owner)) + return false; + /* Make sure the device can't go away */ + get_device(dev); + pm_runtime_get_sync(dev); + return true; +} + +/** + * coresight_put_ref- Helper function to decrease reference count to module + * and device. Power off the device. + */ +static inline void coresight_put_ref(struct coresight_device *csdev) +{ + struct device *dev = csdev->dev.parent; + + pm_runtime_put(dev); + put_device(dev); + module_put(dev->driver->owner); +} + /* * coresight_grab_device - Power up this device and any of the helper * devices connected to it for trace operation. Since the helper devices * don't appear on the trace path, they should be handled along with the * the master device. */ -static void coresight_grab_device(struct coresight_device *csdev) +static int coresight_grab_device(struct coresight_device *csdev) { int i; @@ -644,9 +676,20 @@ static void coresight_grab_device(struct coresight_device *csdev) child = csdev->pdata->conns[i].child_dev; if (child && child->type == CORESIGHT_DEV_TYPE_HELPER) - pm_runtime_get_sync(child->dev.parent); + if (!coresight_get_ref(child)) + goto err; + } + if (coresight_get_ref(csdev)) + return 0; +err: + for (i--; i >= 0; i--) { + struct coresight_device *child; + + child = csdev->pdata->conns[i].child_dev; + if (child && child->type == CORESIGHT_DEV_TYPE_HELPER) + coresight_put_ref(child); } - pm_runtime_get_sync(csdev->dev.parent); + return -ENODEV; } /* @@ -657,13 +700,13 @@ static void coresight_drop_device(struct coresight_device *csdev) { int i; - pm_runtime_put(csdev->dev.parent); + coresight_put_ref(csdev); for (i = 0; i < csdev->pdata->nr_outport; i++) { struct coresight_device *child; child = csdev->pdata->conns[i].child_dev; if (child && child->type == CORESIGHT_DEV_TYPE_HELPER) - pm_runtime_put(child->dev.parent); + coresight_put_ref(child); } } @@ -682,7 +725,7 @@ static int _coresight_build_path(struct coresight_device *csdev, struct coresight_device *sink, struct list_head *path) { - int i; + int i, ret; bool found = false; struct coresight_node *node; @@ -712,11 +755,14 @@ static int _coresight_build_path(struct coresight_device *csdev, * is tell the PM runtime core we need this element and add a node * for it. */ + ret = coresight_grab_device(csdev); + if (ret) + return ret; + node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL); if (!node) return -ENOMEM; - coresight_grab_device(csdev); node->csdev = csdev; list_add(&node->link, path);