Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp994793pxa; Wed, 19 Aug 2020 22:52:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxcj9M5N+vbKRgZg6D3BR+FiiKkJrK5AW78py32tyx8P9y/0wHcrCQVILd6udXfEJaS75RD X-Received: by 2002:a17:906:2506:: with SMTP id i6mr1689555ejb.72.1597902774737; Wed, 19 Aug 2020 22:52:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597902774; cv=none; d=google.com; s=arc-20160816; b=xD9XN3QGW02Qn5AWvayG3gNZK3K5XroZy/YZ2bprtq6mcCTA7EICwh6DtTnbjq+61D JJtamHhHhX+w3PKZu7LmY9EJl6Il/dzkl+3hriQAc49OlPfQsEQRuEIWPMKEI+yacGSK LhtC1Iw7IO47PA/rjc0lnDwGePPaTUTzWMgmFhelJwj6NPM09kDe+0EpaiqaZdmIGW9f sC76BHUk9ov/AleA1hUNDbIhJaeZ5HEAW27RfpjCI/yncS9OfUqkcHax7NcqlmuiMcqz AviXqVns5WqxP5B8kFpWv84csRtKeXlSiXCuyoybnDqaxpFz3ARcZePeMk6oyuXjTqZ7 xyIw== 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=0JWnrqYnoICZtBZ5ETAhr8dod5Y3jvxCvvbjFVLwcIw=; b=QoqGoTfBrIA+Ek14AmdWiY8j5X5WyB+anccLy7h3PGEtGSXrDk+/oo/Rq0GSlflKkz uEx0pFRfV4kW7JgN1c2q95Rn5Q5QYFOxpd0Hm/TfVbFsUyHV7S9cDnfG6MTjS6VFSs5K 5XIfp930Qfs3rHV8Wdavh0EVYXk/ssyH24KZ6x/SJX/MIFT0eqjdat8q3P7GAKUn+1c2 KFG30p8LNPCX/43uYOMmOvK3Vhe7OI1TvW6rmzgz37QP13wibGW3GB5oD8Iow/oinhU/ /OY+rkAkvymsSFYhvXPSUAxwg16k0hgCoOXhuAdLItatDIWLmPJ1gAKffEfdBMOJQeT6 vApQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@mg.codeaurora.org header.s=smtp header.b=Icuw4MbD; 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 i4si725085edl.77.2020.08.19.22.52.31; Wed, 19 Aug 2020 22:52:54 -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=Icuw4MbD; 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 S1726956AbgHTFsj (ORCPT + 99 others); Thu, 20 Aug 2020 01:48:39 -0400 Received: from m43-7.mailgun.net ([69.72.43.7]:60629 "EHLO m43-7.mailgun.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725768AbgHTFsj (ORCPT ); Thu, 20 Aug 2020 01:48:39 -0400 DKIM-Signature: a=rsa-sha256; v=1; c=relaxed/relaxed; d=mg.codeaurora.org; q=dns/txt; s=smtp; t=1597902518; h=Content-Transfer-Encoding: MIME-Version: References: In-Reply-To: Message-Id: Date: Subject: Cc: To: From: Sender; bh=0JWnrqYnoICZtBZ5ETAhr8dod5Y3jvxCvvbjFVLwcIw=; b=Icuw4MbDE/Gq/MEccxO4TkFQjA90fJVvnGYv6x6i40N8oNVxL882f+cwg3p0OYjXfRiCXL63 qsluMtYyYlHm52h0q791HaMf4mB1wsBQQ0uESvN57URq+yBthMEh8IRNUDfe8CwNdgbtYQc7 w2aVt2FEi69LPE90GyeUFUQzWgc= X-Mailgun-Sending-Ip: 69.72.43.7 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-n02.prod.us-west-2.postgun.com with SMTP id 5f3e0ea6b3f338a0905aa187 (version=TLS1.2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256); Thu, 20 Aug 2020 05:48:22 GMT Received: by smtp.codeaurora.org (Postfix, from userid 1001) id C1542C433A1; Thu, 20 Aug 2020 05:48:20 +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=-1.0 required=2.0 tests=ALL_TRUSTED,SPF_NONE, URIBL_BLOCKED autolearn=unavailable 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 60CC8C433C6; Thu, 20 Aug 2020 05:48:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 smtp.codeaurora.org 60CC8C433C6 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=none 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 v9 06/24] coresight: add try_get_module() in coresight_grab_device() Date: Thu, 20 Aug 2020 13:47:00 +0800 Message-Id: <20200820054718.20115-7-tingwei@codeaurora.org> X-Mailer: git-send-email 2.20.0 In-Reply-To: <20200820054718.20115-1-tingwei@codeaurora.org> References: <20200820054718.20115-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 cd17aea6b50d..b040d4421f84 100644 --- a/drivers/hwtracing/coresight/coresight.c +++ b/drivers/hwtracing/coresight/coresight.c @@ -633,13 +633,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; @@ -648,9 +680,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; } /* @@ -661,13 +704,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); } } @@ -686,7 +729,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; @@ -716,11 +759,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