Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp2836784pxk; Tue, 15 Sep 2020 03:46:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwLDxB1brO/vVsOt0cn1wHJmQYcN1Dy0hnoK/a9AHrvv3gzrtfB7WaSqnIVFBfVtKQ/VXIL X-Received: by 2002:a17:906:4093:: with SMTP id u19mr19669475ejj.520.1600166790767; Tue, 15 Sep 2020 03:46:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600166790; cv=none; d=google.com; s=arc-20160816; b=EGy9oSunq2VfzHwJS/dRJlo4orM9/pXLVS4u7XOq8M5QofjGBcjLw4FOK7y0Yn3gfT wEznQhO/gS3RGKWWjk8hyqt0EFeitb2b2gjih7veI7pVOmYXVHzsKpWRqqTeI0yAJqvL ee71R5UMnkmibI4qXpgs2T8oxri1GE/N1sP9UwI5Hae+qBKj5YvQunV9w1uMg2tBmV3u 3iL9EzVemV2pvp5NRv8SKCXtrD/Hui3uOKsO43NvqfNdmCIw21qEua7OT0+i0YNuN8SK Et4051W+qH/jcm8AtgJmthO7/lRcTN18ke6hV77n8EeSpVPefDZYgiNHQjj7tb5LIMuL aw1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dmarc-filter:dkim-signature; bh=okazmhXZpoupNz1FvPJOTzYNXrNAHsOdniGfyvBHuSg=; b=eR2Y8vnXUw+JnAdKJ1lteMwFVaeLecmoB3dZhGaVt/xD9BRhENfZ91GRvd8DjsK+0W Qjzn3lnRYKaR+rnQ8czqF3KBi1QUtsaN1/FP6osGELlE9hZdhwiYdRSa+vVcxPq2w5mP bQwgcWwN2UBuaCHY55kiE9SbSjqYjbWkLxuHgnj+7em7fl93lTd8vp9dm6txd4ZU/cjM qwarvCCuG5RbqTgLPZ4PqmXiXeehKgWIp89RJqt5dxN7bAzvwoJxi94qLN0jC8nHAk6J mWa0GGlxn3oxhU7lTSJwm4u7v1zIMYwySNfZlO7DN3fFr3kcvkAczP2amtFVZ3W0N/7B 3Eow== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@mg.codeaurora.org header.s=smtp header.b=uNTAtLwR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u15si9129503edo.55.2020.09.15.03.46.08; Tue, 15 Sep 2020 03:46:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=fail header.i=@mg.codeaurora.org header.s=smtp header.b=uNTAtLwR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726378AbgIOKmm (ORCPT + 99 others); Tue, 15 Sep 2020 06:42:42 -0400 Received: from mail29.static.mailgun.info ([104.130.122.29]:61760 "EHLO mail29.static.mailgun.info" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726357AbgIOKm3 (ORCPT ); Tue, 15 Sep 2020 06:42:29 -0400 DKIM-Signature: a=rsa-sha256; v=1; c=relaxed/relaxed; d=mg.codeaurora.org; q=dns/txt; s=smtp; t=1600166548; h=Content-Transfer-Encoding: MIME-Version: References: In-Reply-To: Message-Id: Date: Subject: Cc: To: From: Sender; bh=okazmhXZpoupNz1FvPJOTzYNXrNAHsOdniGfyvBHuSg=; b=uNTAtLwRcRSxeLIulLCaHaPvZhbT+MVXd14es7wS+ccV2iak6OrU6fksbGz7gjitpWy77HFM /D7zyB00G0+a5q02S/6tOiR5EmCc5080V5A5wCbWq9xv+4TTucJhAkm/5alBcdFsrR2wnuZO mgy95o65wMSzUqHPmzjjW2TG5ok= X-Mailgun-Sending-Ip: 104.130.122.29 X-Mailgun-Sid: WyI0MWYwYSIsICJsaW51eC1rZXJuZWxAdmdlci5rZXJuZWwub3JnIiwgImJlOWU0YSJd Received: from smtp.codeaurora.org (ec2-35-166-182-171.us-west-2.compute.amazonaws.com [35.166.182.171]) by smtp-out-n03.prod.us-west-2.postgun.com with SMTP id 5f609a929bdf68cc035a255c (version=TLS1.2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256); Tue, 15 Sep 2020 10:42:26 GMT Received: by smtp.codeaurora.org (Postfix, from userid 1001) id 23A33C433F1; Tue, 15 Sep 2020 10:42:26 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-caf-mail-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=ALL_TRUSTED,BAYES_00,SPF_FAIL, URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from tingweiz-gv.qualcomm.com (unknown [180.166.53.21]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: tingwei) by smtp.codeaurora.org (Postfix) with ESMTPSA id 77B5DC43395; Tue, 15 Sep 2020 10:42:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 smtp.codeaurora.org 77B5DC43395 Authentication-Results: aws-us-west-2-caf-mail-1.web.codeaurora.org; dmarc=none (p=none dis=none) header.from=codeaurora.org Authentication-Results: aws-us-west-2-caf-mail-1.web.codeaurora.org; spf=fail smtp.mailfrom=tingwei@codeaurora.org From: Tingwei Zhang To: Mathieu Poirier , Suzuki K Poulose , Alexander Shishkin , Mike Leach Cc: Tingwei Zhang , Greg Kroah-Hartman , Leo Yan , Randy Dunlap , Russell King , Kim Phillips , Mian Yousaf Kaukab , tsoni@codeaurora.org, Sai Prakash Ranjan , Mao Jinlong , coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH v11 06/24] coresight: add try_get_module() in coresight_grab_device() Date: Tue, 15 Sep 2020 18:40:58 +0800 Message-Id: <20200915104116.16789-7-tingwei@codeaurora.org> X-Mailer: git-send-email 2.20.0 In-Reply-To: <20200915104116.16789-1-tingwei@codeaurora.org> References: <20200915104116.16789-1-tingwei@codeaurora.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Signed-off-by: Tingwei Zhang Tested-by: Mike Leach Suggested-by: Suzuki K Poulose --- 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); -- The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project