Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp7067781rwr; Tue, 25 Apr 2023 07:42:25 -0700 (PDT) X-Google-Smtp-Source: AKy350btF5OBH32uce0aBZ/vi5yJFJmD2USaWkb57uHxxZg0O/NnJ448ZR3rjOgLTAn0GpKvpWVp X-Received: by 2002:a05:6a21:1105:b0:ee:2bc1:6e01 with SMTP id oh5-20020a056a21110500b000ee2bc16e01mr19314428pzb.24.1682433744921; Tue, 25 Apr 2023 07:42:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682433744; cv=none; d=google.com; s=arc-20160816; b=hgVYspFHZ3OmKUlzKMM9dhLqZ1OgebGp7T2dwJXwVqKG6rSk91vcVT+pw0U3kT+BCM M4OfW95l7Avudoiwk0+USEx2oaAh9qXsZK1AdaybD1ZMlYVvSbqPaM6mILeNKh+yFBJ0 1zUz9v9km7yawoDLoCNEIo0udYzmWXx71YMOUYYX+Ck9b/D4JE14DZQu0WejmwmdYw6c lWAfV+q3yJV3c+DqV/e6EtjGQtA2W4++/qo3bXGbAhf95Fqk6tobkiGXi+uWEaLP++zE NA4BgBMcH282dC/u23SZWROosaSoNj2i5AP01REmBc+jX4ZdGEyG1CpY7En+ElrWQxX7 eWjg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=nC8UaQLLxEp174wuHt6223ZsVI18SNb7LMGJ3j9JYz0=; b=xxXFqJQrYilzH77BiEo/XW4cpiWaN4zVSdrZ6SXa74tAKUQWOEPRGer3BlDVVHaekT hyicklyL6YXtUV6DrQp/zQNQ+HUB9gpza3tM95lxA5HDmjFnTd8OK4U6SrAr+CZUtJ1c TyzVZZtazd51EDF63erN2AbakCaDzzzHCcX1PGiC0TTMggOa69FDR9CIEp2vg9hmJz0V laPM8MnNnV7PP13mmRyOUbFVD5Y1BWGYb1niM94K+t/YHUU/gkWLV0NvnBHXQgJGwV1U MkkHou55FM/o1TtqUC8dFAvomNT8KBc+77kzwpztuapPDX/5xLCxuzFYn49b2QV5VG/7 Ze3Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 126-20020a630184000000b00524cf94760csi12106031pgb.335.2023.04.25.07.42.13; Tue, 25 Apr 2023 07:42:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234090AbjDYOlh (ORCPT + 99 others); Tue, 25 Apr 2023 10:41:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52570 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234210AbjDYOla (ORCPT ); Tue, 25 Apr 2023 10:41:30 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 15E0335A4 for ; Tue, 25 Apr 2023 07:41:28 -0700 (PDT) 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 CF3934B3; Tue, 25 Apr 2023 07:42:11 -0700 (PDT) Received: from [192.168.1.3] (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 974993F587; Tue, 25 Apr 2023 07:41:26 -0700 (PDT) Message-ID: <73717cc1-3444-e9fa-ddfc-01254fb94f1d@arm.com> Date: Tue, 25 Apr 2023 15:41:09 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.0 Subject: Re: [PATCH v5 13/13] coresight: Fix CTI module refcount leak by making it a helper device Content-Language: en-US To: Suzuki K Poulose Cc: Mathieu Poirier , Leo Yan , Alexander Shishkin , Greg Kroah-Hartman , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, coresight@lists.linaro.org, quic_jinlmao@quicinc.com, mike.leach@linaro.org References: <20230404155121.1824126-1-james.clark@arm.com> <20230404155121.1824126-14-james.clark@arm.com> <51111c59-064f-1458-44ea-5fdae9f26211@arm.com> <2c6cbccb-44e9-edaf-f1a1-ac9c5175537f@arm.com> <7dab2287-97ce-7603-9b9e-445135758d09@arm.com> From: James Clark In-Reply-To: <7dab2287-97ce-7603-9b9e-445135758d09@arm.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-5.6 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 24/04/2023 14:22, Suzuki K Poulose wrote: > On 24/04/2023 12:09, James Clark wrote: >> >> >> On 24/04/2023 11:43, Suzuki K Poulose wrote: >>> On 04/04/2023 16:51, James Clark wrote: >>>> The CTI module has some hard coded refcounting code that has a leak. >>>> For example running perf and then trying to unload it fails: >>>> >>>>     perf record -e cs_etm// -a -- ls >>>>     rmmod coresight_cti >>>> >>>>     rmmod: ERROR: Module coresight_cti is in use >>>> >>>> The coresight core already handles references of devices in use, so by >>>> making CTI a normal helper device, we get working refcounting for free. >>>> >>>> Signed-off-by: James Clark >>>> --- >>>>    drivers/hwtracing/coresight/coresight-core.c  | 104 >>>> ++++++------------ >>>>    .../hwtracing/coresight/coresight-cti-core.c  |  52 +++++---- >>>>    .../hwtracing/coresight/coresight-cti-sysfs.c |   4 +- >>>>    drivers/hwtracing/coresight/coresight-cti.h   |   4 +- >>>>    drivers/hwtracing/coresight/coresight-priv.h  |   4 +- >>>>    drivers/hwtracing/coresight/coresight-sysfs.c |   4 + >>>>    include/linux/coresight.h                     |  30 +---- >>>>    7 files changed, 75 insertions(+), 127 deletions(-) >>>> >>>> diff --git a/drivers/hwtracing/coresight/coresight-core.c >>>> b/drivers/hwtracing/coresight/coresight-core.c >>>> index 16689fe4ba98..2af416bba983 100644 >>>> --- a/drivers/hwtracing/coresight/coresight-core.c >>>> +++ b/drivers/hwtracing/coresight/coresight-core.c >>>> @@ -236,60 +236,44 @@ void coresight_disclaim_device(struct >>>> coresight_device *csdev) >>>>    } >>>>    EXPORT_SYMBOL_GPL(coresight_disclaim_device); >>>>    -/* enable or disable an associated CTI device of the supplied CS >>>> device */ >>>> -static int >>>> -coresight_control_assoc_ectdev(struct coresight_device *csdev, bool >>>> enable) >>>> +/* >>>> + * Add a helper as an output device. This function takes the >>>> @coresight_mutex >>>> + * because it's assumed that it's called from the helper device, >>>> outside of the >>>> + * core code where the mutex would already be held. Don't add new >>>> calls to this >>>> + * from inside the core code, instead try to add the new helper to >>>> the DT and >>>> + * ACPI where it will be picked up and linked automatically. >>>> + */ >>>> +void coresight_add_helper(struct coresight_device *csdev, >>>> +              struct coresight_device *helper) >>>>    { >>>> -    int ect_ret = 0; >>>> -    struct coresight_device *ect_csdev = csdev->ect_dev; >>>> -    struct module *mod; >>>> +    int i; >>>> +    struct coresight_connection conn = {}; >>>> +    struct coresight_connection *new_conn; >>>>    -    if (!ect_csdev) >>>> -        return 0; >>>> -    if ((!ect_ops(ect_csdev)->enable) || >>>> (!ect_ops(ect_csdev)->disable)) >>>> -        return 0; >>>> +    mutex_lock(&coresight_mutex); >>>> +    conn.dest_fwnode = fwnode_handle_get(dev_fwnode(&helper->dev)); >>>> +    conn.dest_dev = helper; >>>> +    conn.dest_port = conn.src_port = -1; >>>> +    conn.src_dev = csdev; >>>>    -    mod = ect_csdev->dev.parent->driver->owner; >>>> -    if (enable) { >>>> -        if (try_module_get(mod)) { >>>> -            ect_ret = ect_ops(ect_csdev)->enable(ect_csdev); >>>> -            if (ect_ret) { >>>> -                module_put(mod); >>>> -            } else { >>>> -                get_device(ect_csdev->dev.parent); >>>> -                csdev->ect_enabled = true; >>>> -            } >>>> -        } else >>>> -            ect_ret = -ENODEV; >>>> -    } else { >>>> -        if (csdev->ect_enabled) { >>>> -            ect_ret = ect_ops(ect_csdev)->disable(ect_csdev); >>>> -            put_device(ect_csdev->dev.parent); >>>> -            module_put(mod); >>>> -            csdev->ect_enabled = false; >>>> -        } >>>> -    } >>>> +    /* >>>> +     * Check for duplicates because this is called every time a helper >>>> +     * device is re-loaded. Existing connections will get re-linked >>>> +     * automatically. >>>> +     */ >>>> +    for (i = 0; i < csdev->pdata->nr_outconns; ++i) >>>> +        if (csdev->pdata->out_conns[i]->dest_fwnode == >>>> conn.dest_fwnode) >>>> +            goto unlock; >>>>    -    /* output warning if ECT enable is preventing trace >>>> operation */ >>>> -    if (ect_ret) >>>> -        dev_info(&csdev->dev, "Associated ECT device (%s) %s >>>> failed\n", >>>> -             dev_name(&ect_csdev->dev), >>>> -             enable ? "enable" : "disable"); >>>> -    return ect_ret; >>>> -} >>>> +    new_conn = >>>> +        coresight_add_out_conn(csdev->dev.parent, csdev->pdata, >>>> &conn); >>> >>> ultra minor nit: >>>      new_conn = coresight_add_out_conn(...., >>>                        .... ); >> >> This whole patchset is now formatted with the kernel clang-format rules. >> Are you sure this one is against the conventions? > > It is not against convention, but there are no hard line rules for > these. > > The only suggestion is to split the lines sensibly with > readability stressed. > > https://www.kernel.org/doc/html/latest/process/coding-style.html#breaking-long-lines-and-strings > > "Statements longer than 80 columns should be broken into sensible > chunks, unless exceeding 80 columns significantly increases readability > and does not hide information. > > Descendants are always substantially shorter than the parent and are > placed substantially to the right. A very commonly used style is to > align descendants to a function open parenthesis." > > > I personally find it : > >     result = rather_long_function_statement(arg1, arg2, >                             ........); > > way better readable than : > >     result = >         rather_long_function_statement(.....); > >> >> The problem is running the formatter on all changed lines makes it >> almost impossible to go back and undo indents like this. > > Haven't used it, but it does seem to say it may not be perfect ;-). > That said, I am not too strict about this. You may leave it unchanged > if it is painful. > > Suzuki > Upon further inspection I think it might actually be a bug in clang-format. When only the ); falls over the column limit it doesn't know that it needs to wrap the previous token to stick with the rules. Or something like that. I'll probably leave that debugging rabbit hole for another time. Anyway I fixed this one in v6.