Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp4076981pxu; Mon, 30 Nov 2020 17:20:10 -0800 (PST) X-Google-Smtp-Source: ABdhPJx0f26aWjSleTBY8Cy0lo8bjofLDCGkNgZgRUz5rtA940zEZRCOyz57n8sWwNWk3PlKJlIx X-Received: by 2002:a50:8f64:: with SMTP id 91mr575202edy.310.1606785610179; Mon, 30 Nov 2020 17:20:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606785610; cv=none; d=google.com; s=arc-20160816; b=w74CJV3iIXlDy1FWtb8G51cnDzqzG2ontOoOTCos4SYAORu8O2EIytXqoG9/FtDEJT mRcPmqXLRBXxf0hDWItXJfUX9haz1oJTKfSYKeuOwY8Jg1JMCwcZa8BJx5W2ywkY4qnv h2J3JZblWZbvavWxDigTYZrgUQG935tXS4Zj4OWa0V3U5Xd/rxMdkD2DcKqpFxGqcD8J ntc4mRpeSMme46Fmupy+5ppYTlZH3CEun7eRK3D6ctHE3RmFZikIgllTz6fMhLPP8JCK KL6caBw2SduRYkc5OZ18Cizx/Uj3Llj/4/42WTQ/0UsrmKsTAMrUvtTh7tE4fiKdPhJm dSSg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dmarc-filter:sender:dkim-signature; bh=b9xG0nohUwSnQ3pYpgsP+NV1uIFeQ16NwbRzsCQSzQg=; b=OSmsYTSPjS8+FmxfPQm4UK+nVVyn9yeMHm67p/4+YOWra0MwiRtfDKpDxKeGK6xER6 sJvLGDJgzyjcY8W6KcoK6GXaeLskTpk+Ktp1uczF4TxQyS0GZPVKdYWg8re9LCXqadc6 m4fK269sxhXMbW0PyGEzAVI8v8mizIs+rcfZHvYowtp2VioQDz7HQiSWg5E6O312002X QjXR7Lmr5GE4YRlIYNYeJ1NeV4dARzAyqZfEMJTnlzlYstdK/GSGLXDvjHtGIMX9SBWC f5HsdB41evLhrF0PvIZd6KEqidSURKLaiBr+mtAnOeq1PRrQqakvymcn3Cic3/sgmF0P GvGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@mg.codeaurora.org header.s=smtp header.b=pZuz5JDY; 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 u7si9777341ejm.200.2020.11.30.17.19.47; Mon, 30 Nov 2020 17:20:10 -0800 (PST) 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=pass header.i=@mg.codeaurora.org header.s=smtp header.b=pZuz5JDY; 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 S1731002AbgLABRg (ORCPT + 99 others); Mon, 30 Nov 2020 20:17:36 -0500 Received: from m42-4.mailgun.net ([69.72.42.4]:27528 "EHLO m42-4.mailgun.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726684AbgLABRf (ORCPT ); Mon, 30 Nov 2020 20:17:35 -0500 DKIM-Signature: a=rsa-sha256; v=1; c=relaxed/relaxed; d=mg.codeaurora.org; q=dns/txt; s=smtp; t=1606785434; h=Content-Transfer-Encoding: Content-Type: In-Reply-To: MIME-Version: Date: Message-ID: From: References: Cc: To: Subject: Sender; bh=b9xG0nohUwSnQ3pYpgsP+NV1uIFeQ16NwbRzsCQSzQg=; b=pZuz5JDYp9bTPL4WlnNRjd/pZMjeXg9yXFTUtG2lS3yhk5mKGQSfrBO+8nktxYg5TY9TkrRE bTyBq0MyrrjKvkIGV1q2dawnYN1kjI5nZMxNSA3nFWFmx8S9F/TsyaXnu+prVlT4A329HSme yVPUogx3CX5oGuB0hbKkADPjNcM= X-Mailgun-Sending-Ip: 69.72.42.4 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-n08.prod.us-west-2.postgun.com with SMTP id 5fc5997fe8c9bf49ad6bbcd1 (version=TLS1.2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256); Tue, 01 Dec 2020 01:16:47 GMT Sender: hemantk=codeaurora.org@mg.codeaurora.org Received: by smtp.codeaurora.org (Postfix, from userid 1001) id 61604C433ED; Tue, 1 Dec 2020 01:16:47 +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, NICE_REPLY_A,SPF_FAIL,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from [10.46.162.249] (i-global254.qualcomm.com [199.106.103.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: hemantk) by smtp.codeaurora.org (Postfix) with ESMTPSA id C140EC433C6; Tue, 1 Dec 2020 01:16:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 smtp.codeaurora.org C140EC433C6 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=hemantk@codeaurora.org Subject: Re: [PATCH v13 4/4] bus: mhi: Add userspace client interface driver To: Loic Poulain Cc: Manivannan Sadhasivam , Greg Kroah-Hartman , linux-arm-msm , open list , Jeffrey Hugo , Bhaumik Bhatt , Network Development References: <1606533966-22821-1-git-send-email-hemantk@codeaurora.org> <1606533966-22821-5-git-send-email-hemantk@codeaurora.org> From: Hemant Kumar Message-ID: Date: Mon, 30 Nov 2020 17:16:45 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Loic, On 11/30/20 10:22 AM, Loic Poulain wrote: > On Sat, 28 Nov 2020 at 04:26, Hemant Kumar wrote: >> >> This MHI client driver allows userspace clients to transfer >> raw data between MHI device and host using standard file operations. >> Driver instantiates UCI device object which is associated to device >> file node. UCI device object instantiates UCI channel object when device >> file node is opened. UCI channel object is used to manage MHI channels >> by calling MHI core APIs for read and write operations. MHI channels >> are started as part of device open(). MHI channels remain in start >> state until last release() is called on UCI device file node. Device >> file node is created with format > > [...] > >> +struct uci_chan { >> + struct uci_dev *udev; >> + wait_queue_head_t ul_wq; >> + >> + /* ul channel lock to synchronize multiple writes */ >> + struct mutex write_lock; >> + >> + wait_queue_head_t dl_wq; >> + >> + /* dl channel lock to synchronize multiple reads */ >> + struct mutex read_lock; >> + >> + /* >> + * protects pending list in bh context, channel release, read and >> + * poll >> + */ >> + spinlock_t dl_pending_lock; >> + >> + struct list_head dl_pending; >> + struct uci_buf *cur_buf; >> + size_t dl_size; >> + struct kref ref_count; >> +}; > > [...] > >> + * struct uci_dev - MHI UCI device >> + * @minor: UCI device node minor number >> + * @mhi_dev: associated mhi device object >> + * @uchan: UCI uplink and downlink channel object >> + * @mtu: max TRE buffer length >> + * @enabled: Flag to track the state of the UCI device >> + * @lock: mutex lock to manage uchan object >> + * @ref_count: uci_dev reference count >> + */ >> +struct uci_dev { >> + unsigned int minor; >> + struct mhi_device *mhi_dev; >> + struct uci_chan *uchan; > > Why a pointer to uci_chan and not just plainly integrating the > structure here, AFAIU uci_chan describes the channels and is just a > subpart of uci_dev. That would reduce the number of dynamic > allocations you manage and the extra kref. do you even need a separate > structure for this? This goes back to one of my patch versions i tried to address concern from Greg. Since we need to ref count the channel as well as the uci device i decoupled the two objects and used two reference counts for two different objects. Copying from V7 patch history V7: - Decoupled uci device and uci channel objects. uci device is associated with device file node. uci channel is associated with MHI channels. uci device refers to uci channel to perform MHI channel operations for device file operations like read() and write(). uci device increments its reference count for every open(). uci device calls mhi_uci_dev_start_chan() to start the MHI channel. uci channel object is tracking number of times MHI channel is referred. This allows to keep the MHI channel in start state until last release() is called. After that uci channel reference count goes to 0 and uci channel clean up is performed which stops the MHI channel. After the last call to release() if driver is removed uci reference count becomes 0 and uci object is cleaned up. > > [...] > >> +static int mhi_uci_dev_start_chan(struct uci_dev *udev) >> +{ >> + int ret = 0; >> + struct uci_chan *uchan; >> + >> + mutex_lock(&udev->lock); >> + if (!udev->uchan || !kref_get_unless_zero(&udev->uchan->ref_count)) { > > This test is suspicious, kref_get_unless_zero should be enough to test, right? kref_get_unless_zero is de-referencing uchan->ref_count for the first open uchan is set to NULL, for that NULL check is added for uchan. > > if (kref_get_unless_zero(&udev->ref)) > goto skip_init; > >> + uchan = kzalloc(sizeof(*uchan), GFP_KERNEL); >> + if (!uchan) { >> + ret = -ENOMEM; >> + goto error_chan_start; >> + } >> + >> + udev->uchan = uchan; >> + uchan->udev = udev; >> + init_waitqueue_head(&uchan->ul_wq); >> + init_waitqueue_head(&uchan->dl_wq); >> + mutex_init(&uchan->write_lock); >> + mutex_init(&uchan->read_lock); >> + spin_lock_init(&uchan->dl_pending_lock); >> + INIT_LIST_HEAD(&uchan->dl_pending); >> + >> + ret = mhi_prepare_for_transfer(udev->mhi_dev); >> + if (ret) { >> + dev_err(&udev->mhi_dev->dev, "Error starting transfer channels\n"); >> + goto error_chan_cleanup; >> + } >> + >> + ret = mhi_queue_inbound(udev); >> + if (ret) >> + goto error_chan_cleanup; >> + >> + kref_init(&uchan->ref_count); >> + } >> + >> + mutex_unlock(&udev->lock); >> + >> + return 0; >> + >> +error_chan_cleanup: >> + mhi_uci_dev_chan_release(&uchan->ref_count); >> +error_chan_start: >> + mutex_unlock(&udev->lock); >> + return ret; >> +} > > Regards, > Loic > Thanks, Hemant -- The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project