Received: by 2002:a05:7412:4e10:b0:e2:908c:2ebd with SMTP id gb16csp57577rdb; Tue, 7 Nov 2023 00:01:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IGDUjTD7CaFPUpiRSs+RqYLyAF91sV+hQkppFDO15Ten3APfXFln8LRaXjmvjJsQK6KhxnW X-Received: by 2002:a05:6808:6381:b0:3a4:ccf:6a63 with SMTP id ec1-20020a056808638100b003a40ccf6a63mr31556549oib.55.1699344089874; Tue, 07 Nov 2023 00:01:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699344089; cv=none; d=google.com; s=arc-20160816; b=At+4JU5uxQmGRV28eNFL+0gTxfsfvXw9eiFUX8ZCMp8DBasgdxHunICDWdZujdXUKs 4+FO/+A0VA48uQdi8iG0Ue6saaQMLEZAr6H6rt1l/uPG48DscvXE3ffFYthQwAIfjIdx 4ZvuIbwPD4qx0c48q2TEiPtdriJjclsc97L/QGRPvxcjN8F2wy7xKNpMo8Ywam+h12mM vyh3qZd6GKZO/nFTbQIGCMXRSj2PoWkKiVW/ailUiBavNGDNfeIbI0cLDvH9SDLd8wGF LMecMg8Gz7C5p+tpPx+T/v4uWVWT9sjZGzTqSASKQ9hgLSIpufMNjJ8FBm79O+RvV949 it0w== 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:dkim-signature; bh=NDucsj2q75e98JcNIa4TZAP69g3rwEi7mKSn4CFEMBU=; fh=flkIg/GFMpBKVtegXxzrpVh7/XOA52jXSbi+VxLmA1w=; b=N/4waQyCvnSuFpbEewfTEma+F9wF9FIpQAvt8FsOEnJTswrtetKR88sUpXygglSjZK b56PEZjF37QX9cy5IiOD9qoH73oC2Hk/pDUfk+3y8QQHPOanr9gjIn8Sexmrg/VPYz0m yBpv1szvO1CI1CbWMjVf/EX3iKhl8NbMUrITalg9m710KyyzQvwr3CDwuSUUT9/pU73F wLl/0cWyjDIadK6+WlOMjqaatGm2Ai7s3cGwISh+kkIx+SbEwr8MyR3q4owf6mBHmoDd aFKzD8jvaDtZUHzJa7ojfvg7lQNejgA02+Enl0wzLNP/6l49q6V2082M4HnUIrHWeqd1 dbFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=Us2e+i2P; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id l63-20020a638842000000b005b83bc299dcsi1506843pgd.538.2023.11.07.00.01.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Nov 2023 00:01:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=Us2e+i2P; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 30C768027E92; Tue, 7 Nov 2023 00:01:07 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233612AbjKGIAE (ORCPT + 99 others); Tue, 7 Nov 2023 03:00:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54654 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229580AbjKGIAD (ORCPT ); Tue, 7 Nov 2023 03:00:03 -0500 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 254A3E8; Tue, 7 Nov 2023 00:00:00 -0800 (PST) Received: from pps.filterd (m0279865.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3A76eXJH018774; Tue, 7 Nov 2023 07:59:54 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=message-id : date : mime-version : subject : to : cc : references : from : in-reply-to : content-type : content-transfer-encoding; s=qcppdkim1; bh=NDucsj2q75e98JcNIa4TZAP69g3rwEi7mKSn4CFEMBU=; b=Us2e+i2PXGtk2wpqideV659ZhYUno2778ZsxkuKHVYOAK/tObf2iOf7lWgwc/vo31tSa jsL45xPwV4J621+Z/ObpKwtfgo8WcddDe2JA7ptHZfTD9HG04KscovN+rsVQdZ5Nvt9Q POn9rGKTxC3GXbFTFcZQTH5NBeX5V6RgfzkCs7yOGginv/MCOwoGHbzSDjqSpdEHsk7J I2ggwTlF1bJxCexj2e/77rN7Z8/FF3H2gRZxDq2Q/gKxW18UTy/nWL7BO1/sgcmd9rCz MCJji/nHjVW8bnpqsa4p0ck+3o7zZ9f1eNzvhvZ8V/QlsQysg+Tfhma/gLxjHkaMMWEc 0A== Received: from nasanppmta04.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3u6wer2vqs-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 07 Nov 2023 07:59:54 +0000 Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA04.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 3A77xrj1014858 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 7 Nov 2023 07:59:53 GMT Received: from [10.253.15.27] (10.80.80.8) by nasanex01b.na.qualcomm.com (10.46.141.250) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Mon, 6 Nov 2023 23:59:51 -0800 Message-ID: <00305327-d866-4da4-916c-fb414398bc3a@quicinc.com> Date: Tue, 7 Nov 2023 15:59:49 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 1/2] bus: mhi: host: Add spinlock to protect WP access when queueing TREs Content-Language: en-US To: Manivannan Sadhasivam , Jeffrey Hugo CC: , , , , References: <1694594861-12691-1-git-send-email-quic_qianyu@quicinc.com> <1694594861-12691-2-git-send-email-quic_qianyu@quicinc.com> <15526b95-518c-445a-be64-6a15259405fb@quicinc.com> <472817a7-78bb-25d9-b8c6-2d70f713b7fb@quicinc.com> <20231106045119.GB2474@thinkpad> From: Qiang Yu In-Reply-To: <20231106045119.GB2474@thinkpad> Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 8bit X-Originating-IP: [10.80.80.8] X-ClientProxiedBy: nasanex01b.na.qualcomm.com (10.46.141.250) To nasanex01b.na.qualcomm.com (10.46.141.250) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-ORIG-GUID: owIEIQkDwFhbK7exT3Kb00mP_xfOVM9N X-Proofpoint-GUID: owIEIQkDwFhbK7exT3Kb00mP_xfOVM9N X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.987,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-11-06_15,2023-11-02_03,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 malwarescore=0 spamscore=0 lowpriorityscore=0 clxscore=1015 impostorscore=0 phishscore=0 mlxscore=0 priorityscore=1501 adultscore=0 mlxlogscore=999 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2310240000 definitions=main-2311070065 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Tue, 07 Nov 2023 00:01:07 -0800 (PST) On 11/6/2023 12:51 PM, Manivannan Sadhasivam wrote: > On Fri, Oct 20, 2023 at 09:07:35AM -0600, Jeffrey Hugo wrote: >> On 10/16/2023 2:46 AM, Qiang Yu wrote: >>> On 9/29/2023 11:22 PM, Jeffrey Hugo wrote: >>>> On 9/24/2023 9:10 PM, Qiang Yu wrote: >>>>> On 9/22/2023 10:44 PM, Jeffrey Hugo wrote: >>>>>> On 9/13/2023 2:47 AM, Qiang Yu wrote: >>>>>>> From: Bhaumik Bhatt >>>>>>> >>>>>>> Protect WP accesses such that multiple threads queueing buffers for >>>>>>> incoming data do not race and access the same WP twice. >>>>>>> Ensure read and >>>>>>> write locks for the channel are not taken in succession >>>>>>> by dropping the >>>>>>> read lock from parse_xfer_event() such that a callback given to client >>>>>>> can potentially queue buffers and acquire the write lock >>>>>>> in that process. >>>>>>> Any queueing of buffers should be done without channel >>>>>>> read lock acquired >>>>>>> as it can result in multiple locks and a soft lockup. >>>>>>> >>>>>>> Signed-off-by: Bhaumik Bhatt >>>>>>> Signed-off-by: Qiang Yu >>>>>>> --- >>>>>>>   drivers/bus/mhi/host/main.c | 11 ++++++++++- >>>>>>>   1 file changed, 10 insertions(+), 1 deletion(-) >>>>>>> >>>>>>> diff --git a/drivers/bus/mhi/host/main.c b/drivers/bus/mhi/host/main.c >>>>>>> index dcf627b..13c4b89 100644 >>>>>>> --- a/drivers/bus/mhi/host/main.c >>>>>>> +++ b/drivers/bus/mhi/host/main.c >>>>>>> @@ -642,6 +642,7 @@ static int parse_xfer_event(struct >>>>>>> mhi_controller *mhi_cntrl, >>>>>>>               mhi_del_ring_element(mhi_cntrl, tre_ring); >>>>>>>               local_rp = tre_ring->rp; >>>>>>>   +            read_unlock_bh(&mhi_chan->lock); >>>>>> This doesn't work due to the >>>>>> write_lock_irqsave(&mhi_chan->lock, flags); on line 591. >>>>> Write_lock_irqsave(&mhi_chan->lock, flags) is used in case of >>>>> ev_code >= MHI_EV_CC_OOB. We only read_lock/read_unlock the >>>>> mhi_chan while ev_code < MHI_EV_CC_OOB. >>>> Sorry.  OOB != EOB >>>> >>>>>> I really don't like that we are unlocking the mhi_chan while >>>>>> still using it.  It opens up a window where the mhi_chan >>>>>> state can be updated between here and the client using the >>>>>> callback to queue a buf. >>>>>> >>>>>> Perhaps we need a new lock that just protects the wp, and >>>>>> needs to be only grabbed while mhi_chan->lock is held? >>>>> Since we have employed mhi_chan lock to protect the channel and >>>>> what we are concerned here is that client may queue buf to a >>>>> disabled or stopped channel, can we check channel state after >>>>> getting mhi_chan->lock like line 595. >>>>> >>>>> We can add the check after getting write lock in mhi_gen_tre() >>>>> and after getting read lock again here. >>>> I'm not sure that is sufficient.  After you unlock to notify the >>>> client, MHI is going to manipulate the packet count and runtime_pm >>>> without the lock (648-652).  It seems like that adds additional >>>> races which won't be covered by the additional check you propose. >>> I don't think read_lock_bh(&mhi_chan->lock) can protect runtime_pm and >>> the packet count here. Even if we do not unlock, mhi state and packet >>> count can still be changed because we did not get pm_lock here, which is >>> used in all mhi state transition function. >>> >>> I also checked all places that mhi_chan->lock is grabbed, did not see >>> packet count and runtime_pm be protected by write_lock(&mhi_chan->lock). >>> >>> >>> If you really don't like the unlock operation, we can also take a new >>> lock. But I think we only need to add the new lock in two places, >>> mhi_gen_tre and mhi_pm_m0_transition while mhi_chan->lock is held. >> Mani, if I recall correctly, you were the architect of the locking. Do you >> have an opinion? >> > TBH, the locking situation is a mess with MHI. Initially, we happen to have > separate locks for protecting various operations, but then during review, it was > advised to reuse existing locks and avoid having too many separate locks. > > This worked well but then we kind of abused the locks over time. I asked Hemant > and Bhaumik to audit the locks and fix them, but both of them left Qcom. > > So in this situation, the intent of the pm_lock was to protect concurrent access > against updating the pm_state. And it also happen to protect _other_things_ such > as runtime_put, pending_pkts etc... But not properly, because most of the time > read lock is taken in places where pm_state is being read. So there is still a > possibility of race while accessing these _other_things_. > > For this patch, I'm happy with dropping chan->lock before calling xfer_cb() and > I want someone (maybe Qiang) to do the audit of locking in general and come up > with fixes where needed. > > - Mani As discussed with Jeff before, we also need to check channel state before queue buffer and after re-lock in parse_xfer_event, so I also add the channel state check in next version patch. Probably I can do the audit of locking. It's a good chance for me to understand various locks in MHI host driver completely.