From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-11.5 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,NICE_REPLY_A,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id ADE9FC433DF for ; Wed, 5 Aug 2020 10:52:15 +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 779592250E for ; Wed, 5 Aug 2020 10:52:15 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="SogN+76A" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 779592250E Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=arm.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.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-Type: Content-Transfer-Encoding:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:Date:Message-ID:From: References:To:Subject:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=XBxGKf/fvf7YCTMi7IaCfOJpq3Z7729stbe9k1vEmQE=; b=SogN+76ANB3JC/F2NssIwsUX2 DwhDYGZ4nr+LW9+Xhmi4Bui58mT8CqiKtoK/4N0ihCENd5sN0N17Ak75HAH87hECt5NZHHmwDbEK1 CymwuX6x12o5fGBkw5GFlTEwilC9xSlIqyudpHmFpXET0f+wgO63H60QA8Md9Rnzl3WcPVDoHHb9k 0qVz7HMJUZpHOVXsX9k3dk2Ru0x8iKpUoUJti35Smey5Jow5LByMXA8ltMJ6HpKzaAODdXwu7C+tZ IGagzReNLXGp/iw5CrfS7KzSeNG8/Q8afqIy3iNQ50lmoCw0VWuwhI6XT5HNfWoG71vJUN7qZmEs/ S5726m1eg==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1k3H0c-0001RS-Nx; Wed, 05 Aug 2020 10:50:58 +0000 Received: from foss.arm.com ([217.140.110.172]) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1k3H0Z-0001Qg-NV for linux-arm-kernel@lists.infradead.org; Wed, 05 Aug 2020 10:50:56 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id D248FD6E; Wed, 5 Aug 2020 03:50:52 -0700 (PDT) Received: from [10.37.12.82] (unknown [10.37.12.82]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id D94893FA32; Wed, 5 Aug 2020 03:50:47 -0700 (PDT) Subject: Re: [PATCH v7 06/25] coresight: add try_get_module() in coresight_grab_device() To: tingwei@codeaurora.org, mathieu.poirier@linaro.org, alexander.shishkin@linux.intel.com, mike.leach@linaro.org References: <20200805025458.2978-1-tingwei@codeaurora.org> <20200805025458.2978-7-tingwei@codeaurora.org> From: Suzuki K Poulose Message-ID: <7a0c543e-6e71-db1c-e207-79d0d78c89b8@arm.com> Date: Wed, 5 Aug 2020 11:55:40 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: <20200805025458.2978-7-tingwei@codeaurora.org> Content-Language: en-US X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200805_065055_897084_B919F21B X-CRM114-Status: GOOD ( 29.65 ) 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: tsoni@codeaurora.org, saiprakash.ranjan@codeaurora.org, kim.phillips@arm.com, jinlmao@codeaurora.org, gregkh@linuxfoundation.org, coresight@lists.linaro.org, rdunlap@infradead.org, ykaukab@suse.de, linux@armlinux.org.uk, leo.yan@linaro.org, linux-arm-kernel@lists.infradead.org Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On 08/05/2020 03:54 AM, Tingwei Zhang wrote: > 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 > --- > drivers/hwtracing/coresight/coresight.c | 39 +++++++++++++++++++++---- > 1 file changed, 34 insertions(+), 5 deletions(-) > > diff --git a/drivers/hwtracing/coresight/coresight.c b/drivers/hwtracing/coresight/coresight.c > index b7151c5f81b1..1626bc885dfe 100644 > --- a/drivers/hwtracing/coresight/coresight.c > +++ b/drivers/hwtracing/coresight/coresight.c > @@ -640,7 +640,7 @@ struct coresight_device *coresight_get_sink_by_id(u32 id) > * 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; > Please could you add a pair of helper functions to hold/drop reference to a device/driver ? i.e, static inline bool coresight_get_ref(struct coresight_device *csdev) { struct device *dev = csdev->dev.parent; /* Make sure the driver cant 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; } 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); } > @@ -648,10 +648,30 @@ static void coresight_grab_device(struct coresight_device *csdev) > struct coresight_device *child; > > child = csdev->pdata->conns[i].child_dev; > - if (child && child->type == CORESIGHT_DEV_TYPE_HELPER) > + if (child && child->type == CORESIGHT_DEV_TYPE_HELPER) { > + if (!try_module_get(child->dev.parent->driver->owner)) > + goto err; > + get_device(child->dev.parent); > pm_runtime_get_sync(child->dev.parent); > + } > } This could then be : if (child && child->type == CORESIGHT_DEV_TYPE_HELPER) if (!coresight_get_ref(child)) goto err; > + if (!try_module_get(csdev->dev.parent->driver->owner)) > + goto err; > + get_device(csdev->dev.parent); > pm_runtime_get_sync(csdev->dev.parent); if (coresight_get_ref(csdev)) return 0; > + 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) > + pm_runtime_put(child->dev.parent); > + put_device(child->dev.parent); > + module_put(child->dev.parent->driver->owner); > + } And this could be : if (child && child->type == CORESIGHT_DEV_TYPE_HELPER) coresight_put_ref(child); > + } > + return -ENODEV; > } > > /* > @@ -663,12 +683,17 @@ static void coresight_drop_device(struct coresight_device *csdev) > int i; > > pm_runtime_put(csdev->dev.parent); > + put_device(csdev->dev.parent); > + module_put(csdev->dev.parent->driver->owner); 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) > + if (child && child->type == CORESIGHT_DEV_TYPE_HELPER) { > pm_runtime_put(child->dev.parent); > + put_device(child->dev.parent); > + module_put(child->dev.parent->driver->owner); > + } coresight_put_ref(child); > } > } > > @@ -687,7 +712,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; > > @@ -721,7 +746,11 @@ static int _coresight_build_path(struct coresight_device *csdev, > if (!node) > return -ENOMEM; > > - coresight_grab_device(csdev); > + ret = coresight_grab_device(csdev); > + if (ret) { > + kfree(node); > + return ret; > + } > node->csdev = csdev; > list_add(&node->link, path); > > Cheers Suzuki _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel