Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp13736733ybl; Sun, 29 Dec 2019 19:30:16 -0800 (PST) X-Google-Smtp-Source: APXvYqzKs3tB8620PT71LmZFKS+KRX7MbXL1M8PLAtKUnxRdBedXJ4xVEBwWgV7ypXXLiornWAj3 X-Received: by 2002:a05:6830:1cd3:: with SMTP id p19mr68030134otg.118.1577676616558; Sun, 29 Dec 2019 19:30:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1577676616; cv=none; d=google.com; s=arc-20160816; b=C596XYlG/uGnnFlPK7IMcbafimXj/UaxJJiH2+ppav/4pil6ggittDy1rn3nUkwjVl 1cyl38UAqci8veA0NUeotYz5auviHgwan5XI7+B9l5H+VXAmn4Rlp+1Sq6248502Aotr 1WGK9ladt/pP1S4UVqrwijypHXkiEkLUw3ms0DFT1QeHc6M1lcghei/KnKc96XCGc+QS RyV3clc+JboXv7EByqyueiN89Of3h0HQq3l+5Urlty6+xyKTS2MvVgE3XuayectKRXEG IDNzIyA6f45+dRGZ46q6PT6RsgXOR8Qb2H+pgbEIRAPMZ8byh1TbNOnB2ipdFbC0rT6D 2Mng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=/gSpV16gqUugWiREKjeRqrw8NqwpDXSNtuIvz7v7i9E=; b=vTVYMh4xqvocw4bcrNl5pN2klxqeI3nNUhhUekqnnXriGDW+MG7CViFBTtVhLZGoe9 jFwxXq1PKyPKROIlo3N1bnb+XG3bj1l2EpWu9ES5XbtZ97+fts31ZSPJM42dbVDQUqC8 BUCJ0PeMD33RcdE7jq8jLZcjqfL2XcIpmzH5NfA/NLFZcfG0X8aPt/TclqA0gzeLcSxw UGHwPQFzkTLeJOwoue6FSfJ4xvtzlv28pA4NCWh7wvv+p6fkN6GcAN4eunuPmAifF4j9 IlTUhYcB4aXAZnIE4HBL3gGIwSwMHo0YPtl077Dj0wB1L1grodTwcgTbt42/2n9VdiFk qWKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ljUmIFIw; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t184si20922279oig.184.2019.12.29.19.30.04; Sun, 29 Dec 2019 19:30:16 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ljUmIFIw; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727107AbfL3D2R (ORCPT + 99 others); Sun, 29 Dec 2019 22:28:17 -0500 Received: from mail-il1-f194.google.com ([209.85.166.194]:46119 "EHLO mail-il1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726894AbfL3D2R (ORCPT ); Sun, 29 Dec 2019 22:28:17 -0500 Received: by mail-il1-f194.google.com with SMTP id t17so26845775ilm.13; Sun, 29 Dec 2019 19:28:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=/gSpV16gqUugWiREKjeRqrw8NqwpDXSNtuIvz7v7i9E=; b=ljUmIFIw0TpKNbfOsZzPy0594sjYu/WDS2ogd7eV+SZZ16rOMs8kMTDMp6r6z/HcDC gqeF9js1oULvAa9uPuKoajyItsjEtWIyVR5bWaYyUdJ6TJCtRwzq4+RZVsJAoLwTgmCe ULropm0NWnpv5zpmOeO+HeH1QR+jfN9rvbHK1upIzD+IeCFoYPpaQIq080RnM95H2VwG TqqF12ZBn8tHuciOJ2AXNUyop1HJqJacA0AgEg+pthBgH0U4qE8OnMLggownEhd75RHg 8ltU/kKIbQzSd85SF6xHDqt6XgBdTXu0malc/i+gHCoLM+Bkv2aHzp4g+/NbaLl2cqwE 2fyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=/gSpV16gqUugWiREKjeRqrw8NqwpDXSNtuIvz7v7i9E=; b=N1I9eg27O08XzFzVQ/o7vFqIKqDyPv8a+JG//EYzmkzl1GT/BStW5HsFjH9t/h/dvO Ynr33qAXVCm2EkRx+7mFaIB4QunFHDZooKAaB2GRPLCwRZgEYabuD9/iDa6XAGD8jNOT 0CaAW2yrqgSQyB+pCqLSybuOKHk/dVOUaXv+TJz1ivPjsbODhI6q79fzg6+kN4TS+ZGd 9jNXDFS+UK/EzxlCq0kqEqqjhH8iZibpuL7hP1flwjtbBsKleCc+qINdJOmkGZG0xCjP Ld9P8N75LGhLRyNXcMCG73X2BO9oqRTAof+Vr6mI3uVnFeFKbKThrCnBzKnMHB3018BA amTw== X-Gm-Message-State: APjAAAWRDPcxlHNtL/BsTBtor7+HF6wsmRCeK7HmLp3NIg5MT/giGjr1 8fVy8sJDvXHvN1yGvIaCV1hJtS62TkW8AHumQ/c= X-Received: by 2002:a92:84d1:: with SMTP id y78mr55495168ilk.69.1577676496461; Sun, 29 Dec 2019 19:28:16 -0800 (PST) MIME-Version: 1.0 References: <20191121072910.31665-1-bibby.hsieh@mediatek.com> <1575426135.31411.2.camel@mtksdaap41> In-Reply-To: <1575426135.31411.2.camel@mtksdaap41> From: Jassi Brar Date: Sun, 29 Dec 2019 21:28:05 -0600 Message-ID: Subject: Re: [PATCH] soc: mediatek: cmdq: avoid racing condition with mutex To: CK Hu Cc: Bibby Hsieh , Matthias Brugger , Rob Herring , Devicetree List , Linux Kernel Mailing List , linux-arm-kernel , linux-mediatek@lists.infradead.org, srv_heupstream@mediatek.com, Nicolas Boichat , Dennis-YC Hsieh , Houlong Wei Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Dec 3, 2019 at 8:22 PM CK Hu wrote: > > Hi, Jassi: > > Are mbox_send_message() and mbox_client_txdone() thread-safe? If these > two are thread-safe, this bug should be fixed in mailbox core not > client. > mbox_client_txdone should be called only when the client _knows_ the message has been sent. There is difference between knowing when tx is done, and assuming tx-done because there is no way of knowing it. Your issue arises because you immediately call mbox_client_txdone after mbox_send_message, which may be the only way to do it but that doesn't mean you shouldn't have to take any other precautions (like a mutex). So I think your patch is reasonable. Cheers! > Regards, > CK > > On Thu, 2019-11-21 at 15:29 +0800, Bibby Hsieh wrote: > > If cmdq client is multi thread user, racing will occur without mutex > > protection. It will make the C message queued in mailbox's queue > > always need D message's triggering. > > > > Thread A Thread B Thread C Thread D... > > ----------------------------------------------------------------------------------- > > mbox_send_message() > > send_data() > > mbox_send_message() > > *exit > > mbox_send_message() > > *exit > > mbox_client_txdone() > > tx_tick() > > mbox_client_txdone() > > tx_tick() > > mbox_client_txdone() > > tx_tick() > > msg_submit() > > send_data() > > msg_submit() > > *exit > > msg_submit() > > *exit > > ----------------------------------------------------------------------------------- > > > > Signed-off-by: Bibby Hsieh > > --- > > drivers/soc/mediatek/mtk-cmdq-helper.c | 3 +++ > > include/linux/soc/mediatek/mtk-cmdq.h | 1 + > > 2 files changed, 4 insertions(+) > > > > diff --git a/drivers/soc/mediatek/mtk-cmdq-helper.c b/drivers/soc/mediatek/mtk-cmdq-helper.c > > index 9add0fd5fa6c..9e35e0beffaa 100644 > > --- a/drivers/soc/mediatek/mtk-cmdq-helper.c > > +++ b/drivers/soc/mediatek/mtk-cmdq-helper.c > > @@ -81,6 +81,7 @@ struct cmdq_client *cmdq_mbox_create(struct device *dev, int index, u32 timeout) > > client->client.dev = dev; > > client->client.tx_block = false; > > client->chan = mbox_request_channel(&client->client, index); > > + mutex_init(&client->mutex); > > > > if (IS_ERR(client->chan)) { > > long err; > > @@ -352,9 +353,11 @@ int cmdq_pkt_flush_async(struct cmdq_pkt *pkt, cmdq_async_flush_cb cb, > > spin_unlock_irqrestore(&client->lock, flags); > > } > > > > + mutex_lock(&client->mutex); > > mbox_send_message(client->chan, pkt); > > /* We can send next packet immediately, so just call txdone. */ > > mbox_client_txdone(client->chan, 0); > > + mutex_unlock(&client->mutex); > > > > return 0; > > } > > diff --git a/include/linux/soc/mediatek/mtk-cmdq.h b/include/linux/soc/mediatek/mtk-cmdq.h > > index a74c1d5acdf3..0f9071cd1bc7 100644 > > --- a/include/linux/soc/mediatek/mtk-cmdq.h > > +++ b/include/linux/soc/mediatek/mtk-cmdq.h > > @@ -28,6 +28,7 @@ struct cmdq_client { > > struct mbox_chan *chan; > > struct timer_list timer; > > u32 timeout_ms; /* in unit of microsecond */ > > + struct mutex mutex; > > }; > > > > /** >