Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp27062ybp; Thu, 3 Oct 2019 09:42:08 -0700 (PDT) X-Google-Smtp-Source: APXvYqwGLv5mShjjOFuiF4No+AcU/HMM/wCbP+2WyecemLHBMxaoDid7WeSFg15aP2ogfL5hcp4U X-Received: by 2002:a17:906:cf82:: with SMTP id um2mr8648995ejb.254.1570120927916; Thu, 03 Oct 2019 09:42:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570120927; cv=none; d=google.com; s=arc-20160816; b=IgF/2ZISuMr5CktxTveP0kNeKmsOr6OYi5ge1F2IwWEDIYbmWpd6FE7qiHIZFEbksi f6QDvH1f0blhPT7/IxNCD6OGGWPgnlnJnz0LqhQGF498YgWP30K1PmOgBOGq8n73zBB6 hDRTQbYoIGF5uqPC3CKWPF8dm5Tkr9eME4ud6MT7I3PrV1ehlxY2Xatk9tFqWSnlnHw5 t9HuDD92gG5Xw7cit+Lm6O+qiM3yC0wAlZaIZMa86Oz3zFWeU4fptsc89ErbG++zIQG5 cPpzNm/iGLHf8aN07IWtub3xFg8Uo6D7wN1zBGQz+4FyXpi+oEVfB+1OcPcSr4PVo2PO WTNg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=NWPH4dq7/mkXGsRzGqjIwoAuADFLruBeYJUjf8Nd9Jo=; b=wHcrW5al3sYxzBMZfD8UWLAB5TM3Bog2UcI15ObJh4KkPlwJGz2zecDmlB6ZzZQ+ro Hbxr/SUqUvNOp2wnWN0Bv179u7Qcwzda53mneo3yvVfiSDYirfmKhM2tODNahDtA2bhO QR5nR27qzEUaGhhpVhijbX4VwUZshwjf6GPyhrLDEL0vAlasvhzMtJJPcAe71v6ZTHCa HIIoeBLMnGtyjy2cCK0Tn/dZabs95gr+xD/IY4jrl6+L0h+SbvrYgXq1WrN298L44YFL lTCMYEoc7/vwIv9GOx48FlQa307MTBw0F6tFTjf7sadwuJpsZPhmd4/araQe8Hc1zy2n WiWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Z+1Ta1xK; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 13si1905170edw.357.2019.10.03.09.41.43; Thu, 03 Oct 2019 09:42:07 -0700 (PDT) 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=@kernel.org header.s=default header.b=Z+1Ta1xK; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2404785AbfJCQjH (ORCPT + 99 others); Thu, 3 Oct 2019 12:39:07 -0400 Received: from mail.kernel.org ([198.145.29.99]:47728 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2404700AbfJCQiH (ORCPT ); Thu, 3 Oct 2019 12:38:07 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A50B821783; Thu, 3 Oct 2019 16:38:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1570120686; bh=L0SmYGtaGcxI3+BLpfscRZ+R1XBFygt6MAgF3wzw8/g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Z+1Ta1xKwhsl7y+aFZQnoEMzuLwm2HhuUMa5UWrDXdHLzYoRg6nRq3qcwKL8RCiwd 65vXIHyXP65a2rVQaQpdUGEU2f9Hm/cuap9q/jpTuedS4XhUxfvdSJDOl3IVYxxakY B+bFg8fnSdIwN3OVA4dCpEceKjXq5+mIwB7Im1As= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Pi-Hsun Shih , Enric Balletbo i Serra , Sasha Levin Subject: [PATCH 5.2 313/313] platform/chrome: cros_ec_rpmsg: Fix race with host command when probe failed Date: Thu, 3 Oct 2019 17:54:51 +0200 Message-Id: <20191003154604.030111391@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191003154533.590915454@linuxfoundation.org> References: <20191003154533.590915454@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Pi-Hsun Shih [ Upstream commit 71cddb7097e2b0feb855d7fd7d59afd12cbee4bb ] Since the rpmsg_endpoint is created before probe is called, it's possible that a host event is received during cros_ec_register, and there would be some pending work in the host_event_work workqueue while cros_ec_register is called. If cros_ec_register fails, when the leftover work in host_event_work run, the ec_dev from the drvdata of the rpdev could be already set to NULL, causing kernel crash when trying to run cros_ec_get_next_event. Fix this by creating the rpmsg_endpoint by ourself, and when cros_ec_register fails (or on remove), destroy the endpoint first (to make sure there's no more new calls to cros_ec_rpmsg_callback), and then cancel all works in the host_event_work workqueue. Cc: stable@vger.kernel.org Fixes: 2de89fd98958 ("platform/chrome: cros_ec: Add EC host command support using rpmsg") Signed-off-by: Pi-Hsun Shih Signed-off-by: Enric Balletbo i Serra Signed-off-by: Sasha Levin --- drivers/platform/chrome/cros_ec_rpmsg.c | 32 +++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/drivers/platform/chrome/cros_ec_rpmsg.c b/drivers/platform/chrome/cros_ec_rpmsg.c index 5d3fb2abad1d6..bec19d4814aba 100644 --- a/drivers/platform/chrome/cros_ec_rpmsg.c +++ b/drivers/platform/chrome/cros_ec_rpmsg.c @@ -41,6 +41,7 @@ struct cros_ec_rpmsg { struct rpmsg_device *rpdev; struct completion xfer_ack; struct work_struct host_event_work; + struct rpmsg_endpoint *ept; }; /** @@ -72,7 +73,6 @@ static int cros_ec_pkt_xfer_rpmsg(struct cros_ec_device *ec_dev, struct cros_ec_command *ec_msg) { struct cros_ec_rpmsg *ec_rpmsg = ec_dev->priv; - struct rpmsg_device *rpdev = ec_rpmsg->rpdev; struct ec_host_response *response; unsigned long timeout; int len; @@ -85,7 +85,7 @@ static int cros_ec_pkt_xfer_rpmsg(struct cros_ec_device *ec_dev, dev_dbg(ec_dev->dev, "prepared, len=%d\n", len); reinit_completion(&ec_rpmsg->xfer_ack); - ret = rpmsg_send(rpdev->ept, ec_dev->dout, len); + ret = rpmsg_send(ec_rpmsg->ept, ec_dev->dout, len); if (ret) { dev_err(ec_dev->dev, "rpmsg send failed\n"); return ret; @@ -196,11 +196,24 @@ static int cros_ec_rpmsg_callback(struct rpmsg_device *rpdev, void *data, return 0; } +static struct rpmsg_endpoint * +cros_ec_rpmsg_create_ept(struct rpmsg_device *rpdev) +{ + struct rpmsg_channel_info chinfo = {}; + + strscpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE); + chinfo.src = rpdev->src; + chinfo.dst = RPMSG_ADDR_ANY; + + return rpmsg_create_ept(rpdev, cros_ec_rpmsg_callback, NULL, chinfo); +} + static int cros_ec_rpmsg_probe(struct rpmsg_device *rpdev) { struct device *dev = &rpdev->dev; struct cros_ec_rpmsg *ec_rpmsg; struct cros_ec_device *ec_dev; + int ret; ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL); if (!ec_dev) @@ -225,7 +238,18 @@ static int cros_ec_rpmsg_probe(struct rpmsg_device *rpdev) INIT_WORK(&ec_rpmsg->host_event_work, cros_ec_rpmsg_host_event_function); - return cros_ec_register(ec_dev); + ec_rpmsg->ept = cros_ec_rpmsg_create_ept(rpdev); + if (!ec_rpmsg->ept) + return -ENOMEM; + + ret = cros_ec_register(ec_dev); + if (ret < 0) { + rpmsg_destroy_ept(ec_rpmsg->ept); + cancel_work_sync(&ec_rpmsg->host_event_work); + return ret; + } + + return 0; } static void cros_ec_rpmsg_remove(struct rpmsg_device *rpdev) @@ -233,6 +257,7 @@ static void cros_ec_rpmsg_remove(struct rpmsg_device *rpdev) struct cros_ec_device *ec_dev = dev_get_drvdata(&rpdev->dev); struct cros_ec_rpmsg *ec_rpmsg = ec_dev->priv; + rpmsg_destroy_ept(ec_rpmsg->ept); cancel_work_sync(&ec_rpmsg->host_event_work); } @@ -249,7 +274,6 @@ static struct rpmsg_driver cros_ec_driver_rpmsg = { }, .probe = cros_ec_rpmsg_probe, .remove = cros_ec_rpmsg_remove, - .callback = cros_ec_rpmsg_callback, }; module_rpmsg_driver(cros_ec_driver_rpmsg); -- 2.20.1