Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp5591426pxb; Mon, 28 Mar 2022 14:40:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJylI/drpGQ5BRTPpvCb5ciEZtQBim/n06sSjbDrqTKU6KCLPkVOPgErzKwKwCLrKC95NB9h X-Received: by 2002:a9f:37a3:0:b0:351:82a8:e21a with SMTP id q32-20020a9f37a3000000b0035182a8e21amr13938918uaq.98.1648503607933; Mon, 28 Mar 2022 14:40:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648503607; cv=none; d=google.com; s=arc-20160816; b=plTlONFRR9hZeP5o/CzeVUfJcdtrOGNPi53S/wH7S8VAT1SxslGnJTbhBFE2Kt/WIQ 6vXYwGFya98qYpsc1Nzf8htRPCIn3B3txcKX1wR/bh0cku9KfLSc1T6kjp5H6ZoOMVoC JqyQbgb26TuMylz5hEFeIbMoRZozC8o8V+X/BBOIPlqad5pnMHFgREw7/4vKaGy/oZH1 742aKAimO4i29dnXVsqktrn4LulSP1hdGiN9qHdGTl2HhpNdI20329veFwQPSzBnLHv8 UE4gQatxqWaL8NxGSzKadlAHh/hFPPmRviu+/Dn12sfSxmxjpurlxKQK9tSOsQkr9Hpi mCmQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from; bh=Gxa5X9QvbzC3JiVpg/gwlF5xIHQxGaMYUQOtVrQ37dM=; b=FxOvvmJ6zc4NuvCstBZS0injj+6hWkxYYt6oh9wTcXCzlocgQUy5JLt7q7nacaovES TgCmowMEFvXUo98YcQv9yrXap4pfisi0WDq1i0VaBLTdyzGr2UyNOmwQNUQVhrNG3iQd VPGGX7ZQKWUpxhQmVCSigBPn+8euCO0fIhIwJa8Ax2riqxz6fVfV6YliAQvDhZLOzbp8 kU2wI2USbdd1U+vk96/j5rKLd2jQk6Rm9wDdqje+D6xKwJxhwYANsKPuhe8cegldaYUp I7Ws1DXrW8jn9m3fJ+o4Q/ZFPtk8NqMNdU+ag4ooseBLZ23xh5EW0kvYsieR8kHf7tHP xUuQ== 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:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id k67-20020a676f46000000b0032564e287eesi3167109vsc.526.2022.03.28.14.40.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Mar 2022 14:40:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 9A69378FE3; Mon, 28 Mar 2022 14:17:02 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237632AbiC1CcO (ORCPT + 99 others); Sun, 27 Mar 2022 22:32:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35240 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237611AbiC1CcJ (ORCPT ); Sun, 27 Mar 2022 22:32:09 -0400 Received: from inva020.nxp.com (inva020.nxp.com [92.121.34.13]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 103833EB82; Sun, 27 Mar 2022 19:30:29 -0700 (PDT) Received: from inva020.nxp.com (localhost [127.0.0.1]) by inva020.eu-rdc02.nxp.com (Postfix) with ESMTP id E0C411A2337; Mon, 28 Mar 2022 04:30:27 +0200 (CEST) Received: from aprdc01srsp001v.ap-rdc01.nxp.com (aprdc01srsp001v.ap-rdc01.nxp.com [165.114.16.16]) by inva020.eu-rdc02.nxp.com (Postfix) with ESMTP id A8D621A2331; Mon, 28 Mar 2022 04:30:27 +0200 (CEST) Received: from localhost.localdomain (shlinux2.ap.freescale.net [10.192.224.44]) by aprdc01srsp001v.ap-rdc01.nxp.com (Postfix) with ESMTP id 969A0183AD0B; Mon, 28 Mar 2022 10:30:26 +0800 (+08) From: Shengjiu Wang To: bjorn.andersson@linaro.org, mathieu.poirier@linaro.org Cc: linux-remoteproc@vger.kernel.org, linux-kernel@vger.kernel.org, shengjiu.wang@gmail.com Subject: [PATCH v2 2/2] remoteproc: core: Move state checking to remoteproc_core Date: Mon, 28 Mar 2022 10:20:12 +0800 Message-Id: <1648434012-16655-3-git-send-email-shengjiu.wang@nxp.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1648434012-16655-1-git-send-email-shengjiu.wang@nxp.com> References: <1648434012-16655-1-git-send-email-shengjiu.wang@nxp.com> X-Virus-Scanned: ClamAV using ClamSMTP X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 There is no mutex protection of these state checking for 'stop' and 'detach' which can't guarantee there is no another instance is trying to do same operation. Consider two instances case: Instance1: echo stop > /sys/class/remoteproc/remoteproc0/state Instance2: echo stop > /sys/class/remoteproc/remoteproc0/state The issue is that the instance2 case may success, Or it may fail with -EINVAL, which is uncertain. So move this state checking in rproc_cdev_write() and state_store() for 'stop', 'detach' operation to 'rproc_shutdown' , 'rproc_detach' function under the mutex protection. Signed-off-by: Shengjiu Wang --- drivers/remoteproc/remoteproc_cdev.c | 7 ------- drivers/remoteproc/remoteproc_core.c | 11 +++++++++++ drivers/remoteproc/remoteproc_sysfs.c | 7 ------- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/drivers/remoteproc/remoteproc_cdev.c b/drivers/remoteproc/remoteproc_cdev.c index 62001eda780c..687f205fd70a 100644 --- a/drivers/remoteproc/remoteproc_cdev.c +++ b/drivers/remoteproc/remoteproc_cdev.c @@ -34,15 +34,8 @@ static ssize_t rproc_cdev_write(struct file *filp, const char __user *buf, size_ if (!strncmp(cmd, "start", len)) { ret = rproc_boot(rproc); } else if (!strncmp(cmd, "stop", len)) { - if (rproc->state != RPROC_RUNNING && - rproc->state != RPROC_ATTACHED) - return -EINVAL; - ret = rproc_shutdown(rproc); } else if (!strncmp(cmd, "detach", len)) { - if (rproc->state != RPROC_ATTACHED) - return -EINVAL; - ret = rproc_detach(rproc); } else { dev_err(&rproc->dev, "Unrecognized option\n"); diff --git a/drivers/remoteproc/remoteproc_core.c b/drivers/remoteproc/remoteproc_core.c index c510125769b9..4b344d7271e3 100644 --- a/drivers/remoteproc/remoteproc_core.c +++ b/drivers/remoteproc/remoteproc_core.c @@ -2075,6 +2075,12 @@ int rproc_shutdown(struct rproc *rproc) return ret; } + if (rproc->state != RPROC_RUNNING && + rproc->state != RPROC_ATTACHED) { + ret = -EINVAL; + goto out; + } + /* if the remote proc is still needed, bail out */ if (!atomic_dec_and_test(&rproc->power)) goto out; @@ -2134,6 +2140,11 @@ int rproc_detach(struct rproc *rproc) return ret; } + if (rproc->state != RPROC_ATTACHED) { + ret = -EINVAL; + goto out; + } + /* if the remote proc is still needed, bail out */ if (!atomic_dec_and_test(&rproc->power)) { ret = 0; diff --git a/drivers/remoteproc/remoteproc_sysfs.c b/drivers/remoteproc/remoteproc_sysfs.c index ac64d69085ab..8c7ea8922638 100644 --- a/drivers/remoteproc/remoteproc_sysfs.c +++ b/drivers/remoteproc/remoteproc_sysfs.c @@ -198,15 +198,8 @@ static ssize_t state_store(struct device *dev, if (ret) dev_err(&rproc->dev, "Boot failed: %d\n", ret); } else if (sysfs_streq(buf, "stop")) { - if (rproc->state != RPROC_RUNNING && - rproc->state != RPROC_ATTACHED) - return -EINVAL; - ret = rproc_shutdown(rproc); } else if (sysfs_streq(buf, "detach")) { - if (rproc->state != RPROC_ATTACHED) - return -EINVAL; - ret = rproc_detach(rproc); } else { dev_err(&rproc->dev, "Unrecognised option: %s\n", buf); -- 2.17.1