From patchwork Thu Aug 22 16:33:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Krzysztof Kozlowski X-Patchwork-Id: 822248 Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 299FB1CDA01 for ; Thu, 22 Aug 2024 16:33:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724344431; cv=none; b=thNha5ybmrDIrmGCHz5NyHWqFKdTH05RZlyyQYVlUnGTzK+IkHfoeElxxiYHSKXcyXzC+FBt/djWFN9D34uKzoOO0ErI9GEkzui+cptq8+WrsMaEsxzUKvgq/BV2reCIFZexb/Yx+rAsFqnpV67zmtZTB3uKqA1nzFydNUw8QpE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724344431; c=relaxed/simple; bh=naDm1RtTiN2zXIk2LJ0zsnhLTTN50E2lGQMRxqbBYAQ=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=YlAxcY64DbWFMEuZ4Tq1lOTMaFk4Oe9rEp94SJwbzHxxSRCsHbI0sCGmES3fR+98Js0v0pnEDEc965bGZvsa+JTYHEmdHOKfEBo27QbtExAr7+lAUmtTnXFW2F3bkgkJ6JXIlN2fX75kY+HwmsZ/3547gPgFAAwszRhDbeALKYY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=Y+n4D59I; arc=none smtp.client-ip=209.85.128.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="Y+n4D59I" Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-4280921baa2so984365e9.2 for ; Thu, 22 Aug 2024 09:33:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1724344428; x=1724949228; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=YTFuV6stQx7CKFbcUpz4unCbxA3iNnf+lqvoGhhxAkM=; b=Y+n4D59I2HB+AV7hE/UHEWDaBT9lqkund6rGWosa3Bp2hO46mHtyUhBK/n8qahj/jJ m886iUhAS4IYH7b0CnEO/w9Yb8nVXHsId0c00Ipm9jb+Sp5IXcY/4prFWz46q4SC53Hi U49xaRGMARU27zQU4iqixAEyhrreAPP7v1jHA96tUzHDpLbBhRhEO4iaVF3Cav2ywI77 85msSGArG0De5nBbixJP4v1ToKgcQOLzz1dprlCu9e2lnRHGdv6UXqSe6lyhJaPMeF9g 1RdelEwOnx3uO0MG7IkwSHd0ISb8ppnekjBrVNw6ku1TIdSHFhDpXlcnEBcYj+muPgWe q/4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724344428; x=1724949228; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=YTFuV6stQx7CKFbcUpz4unCbxA3iNnf+lqvoGhhxAkM=; b=Datv56bb4cRkj4G8F4Ff3O0JAKScPQqm++1RYhvXv4bN/BzblmfLQy+ROSbwr51pfR SQM27z/GxM/DDhd7G2piY/2jzKDU2BkLjEMmmHleXKb3+HGQGnZyXb8yLncagl4nH7qj Thiv+4bB9v2L4psEPisAd5cUCQcJ+Wzyz65EtvLqAbTYpLueFRjxwvAqalRLwur9C/x+ eSFDWBtutwmaMqohKX3LpbTiz15SehCpceRjDS5U4ffzf1IUz1Ea6ZODW0u/GFj5ucO/ BDfAFEZ5xPTx0PzUCg3iLiK+7lE4HLBx7ZkUliplM3mU/xn4dhwXiA2yjNSDVXtvtEAm BAuw== X-Forwarded-Encrypted: i=1; AJvYcCXLnt/Y18E3GcU349uq63s5BqaYbnHqvy7Qh0isKVKYrfuF3m9fWjHs76LzikmqR+RIZlTBHI+i5/1NQ/fd@vger.kernel.org X-Gm-Message-State: AOJu0YyJ8CLqxP4uI3n0j+ylH3Rp7v6oxdegYN818hPqm76bSwJyjbHj n5okxeON+jA6tL8jpkkdqNWpIuyQ8tLyCzyoZAB5ilqOukxF5BGs/GdMBJXHKus= X-Google-Smtp-Source: AGHT+IGvJ/iD05Q3C/AsOQTJKor76DdYYeZaTHMvrsybklOWmJKd355kURal8Tjnr9MKo4Z1wI636Q== X-Received: by 2002:a05:600c:3c85:b0:426:5dd5:f245 with SMTP id 5b1f17b1804b1-42abd231427mr29419205e9.2.1724344428442; Thu, 22 Aug 2024 09:33:48 -0700 (PDT) Received: from krzk-bin.. ([178.197.222.82]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3730813cdcfsm2069377f8f.29.2024.08.22.09.33.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2024 09:33:47 -0700 (PDT) From: Krzysztof Kozlowski To: Bjorn Andersson , Konrad Dybcio , Neil Armstrong , linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Krzysztof Kozlowski , stable@vger.kernel.org Subject: [PATCH 1/2] soc: qcom: pmic_glink: fix scope of __pmic_glink_lock in pmic_glink_rpmsg_probe() Date: Thu, 22 Aug 2024 18:33:44 +0200 Message-ID: <20240822163345.223787-1-krzysztof.kozlowski@linaro.org> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-arm-msm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 File-scope "__pmic_glink_lock" mutex protects the filke-scope "__pmic_glink", thus reference to it should be obtained under the lock, just like pmic_glink_rpmsg_remove() is doing. Otherwise we have a race during if PMIC GLINK device removal: the pmic_glink_rpmsg_probe() function could store local reference before mutex in driver removal is acquired. Fixes: 58ef4ece1e41 ("soc: qcom: pmic_glink: Introduce base PMIC GLINK driver") Cc: Signed-off-by: Krzysztof Kozlowski --- drivers/soc/qcom/pmic_glink.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/soc/qcom/pmic_glink.c b/drivers/soc/qcom/pmic_glink.c index 9606222993fd..452f30a9354d 100644 --- a/drivers/soc/qcom/pmic_glink.c +++ b/drivers/soc/qcom/pmic_glink.c @@ -217,10 +217,11 @@ static void pmic_glink_pdr_callback(int state, char *svc_path, void *priv) static int pmic_glink_rpmsg_probe(struct rpmsg_device *rpdev) { - struct pmic_glink *pg = __pmic_glink; + struct pmic_glink *pg; int ret = 0; mutex_lock(&__pmic_glink_lock); + pg = __pmic_glink; if (!pg) { ret = dev_err_probe(&rpdev->dev, -ENODEV, "no pmic_glink device to attach to\n"); goto out_unlock; From patchwork Thu Aug 22 16:33:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Krzysztof Kozlowski X-Patchwork-Id: 821370 Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D53091CDA12 for ; Thu, 22 Aug 2024 16:33:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724344433; cv=none; b=FcTryZpRUb4MOpM65xxUqRzLVDzaCAlWhX/1OrWtMCWwu3875vE/aPOypDKXyQiDg+Rzcf8hrwiTiPY+05nxc32gWimw7Kqx02LDznjiDXwshuwQvpxbW4FOLx3ZMPiXg4afs7P5dLPBGKpA9KthL8DTWpV+9c3D2U+UAcK9My0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724344433; c=relaxed/simple; bh=l6Kww17V1Gd5y62L9kocI/d35MuDd1bZ7ZhVlzMWiwQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=T+kqx36uWQSKJzy+9HB3iC5XZRe4BoPVeX/et/DdkVnHUkbye83dtupkIfyUr6nMVnivBRkWUHAMRS5o+YqlXbptqPW5foYwvtLtezQr9r/zgQVabk3hgZhjH0Oew1hNuIUvtCzVpoXGSDZOWO1b1VZ1hjEbEbNnCtbRa9+FsqE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=xEoKM56i; arc=none smtp.client-ip=209.85.128.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="xEoKM56i" Received: by mail-wm1-f42.google.com with SMTP id 5b1f17b1804b1-4290075c7e1so1409645e9.0 for ; Thu, 22 Aug 2024 09:33:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1724344430; x=1724949230; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=XU3KE8OpClc69UGPW8/tNiTT0P1BSjvACL1i+Qa8X78=; b=xEoKM56ixNpASyvW+VGhxbCPvos8dJiSA4mA3c7uYsrOkOrAhkRG0dxSGL2y/CO7Fy QTlILcKiflp9qiem5gKG7uRoBYeqLmEt2r61/NvEHAJQmwPBd8WcV2UlBBxiTCFBZQ7S uWh6cykZQT2c8/14OHrStD7GI02JSE+AGaAksHuZb/y7ke1ro9ABD/nFx7zTRUspFvYr fFap7YFBVNfDrfHlPUc9Jz0EBvwuAxbOnpBaSJp2Z1xpGFJ/pwNzji8tMd57uunwmV/I vvp5wATSqLXAYg+qjutuLxqTQsFkyp7p1iLJkif88asP+8FPkMe7jexmQwf2TVWV8eAd SEPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724344430; x=1724949230; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XU3KE8OpClc69UGPW8/tNiTT0P1BSjvACL1i+Qa8X78=; b=eevG9BVxASA083u/9Na27TqLrXqtEmHDPbgFEj4F7y86b+6diUEAzZK0phoghXiZ5z xWaCwo3/4ugH6LEFWBFjLjgqAwfIxnQgoPGfFiCTYEdZWPJ2kThxHT7tg2HfQXX39In7 ZyO7xc7fHV54Gx6IK1JC8HS+2VYCAWJpbHa4vnuSYiVzEijT1Q1/kGuBPNqYUDdH6NE9 SV4m7LAQSiNHOF75BbaWDbbDdErlmDLpA18sZ/oF+Z3xbZJa+C0GlF4sxQbDop0tN72S 5eU8BHbjVgP+uyZDkyDed54dib97WwUT+R6ASSciqVPmzMaV8ZKOHRWcA8IyuxCYG9ak xkiA== X-Forwarded-Encrypted: i=1; AJvYcCUc6Os8QIZEs0mgkZ7qwMlg3LMj9W6h5zGVdPvUdKFhSC/eWiaBr0Ky9ttVskM/+IN7EZ4wb46ClwUPAa84@vger.kernel.org X-Gm-Message-State: AOJu0YzJrPyKOUfihsPhFKMS+dvZGTd39lYuZRhWVf90LBfqFSTmmWnf CqEf5ri9rW6ER0etwcJ2FED3T4J4YW+PqaWkJ5qGcrnpFZL7T17783LpSqcJPqk= X-Google-Smtp-Source: AGHT+IG8UTa/fe8++7QtyBpzenYZedHNEThBpAvYXZlhDKR26418e4WI82bBos2ltyRQ0pHEuhJCIQ== X-Received: by 2002:a5d:6da8:0:b0:368:4c5:af5 with SMTP id ffacd0b85a97d-372fd72f091mr2165930f8f.10.1724344429994; Thu, 22 Aug 2024 09:33:49 -0700 (PDT) Received: from krzk-bin.. ([178.197.222.82]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3730813cdcfsm2069377f8f.29.2024.08.22.09.33.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2024 09:33:49 -0700 (PDT) From: Krzysztof Kozlowski To: Bjorn Andersson , Konrad Dybcio , Neil Armstrong , linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Krzysztof Kozlowski Subject: [PATCH 2/2] soc: qcom: pmic_glink: simplify locking with guard() Date: Thu, 22 Aug 2024 18:33:45 +0200 Message-ID: <20240822163345.223787-2-krzysztof.kozlowski@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240822163345.223787-1-krzysztof.kozlowski@linaro.org> References: <20240822163345.223787-1-krzysztof.kozlowski@linaro.org> Precedence: bulk X-Mailing-List: linux-arm-msm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Simplify error handling over locks with guard(). In few places this elimiates error gotos and local variables. Switch to guard() everywhere in the code for consistency, even if it does not bring benefit in such places. Signed-off-by: Krzysztof Kozlowski --- The part in pmic_glink_rpmsg_probe() depends on previous fix. --- drivers/soc/qcom/pmic_glink.c | 49 ++++++++++++----------------------- 1 file changed, 16 insertions(+), 33 deletions(-) diff --git a/drivers/soc/qcom/pmic_glink.c b/drivers/soc/qcom/pmic_glink.c index 452f30a9354d..dfed8b84eb5f 100644 --- a/drivers/soc/qcom/pmic_glink.c +++ b/drivers/soc/qcom/pmic_glink.c @@ -4,6 +4,7 @@ * Copyright (c) 2022, Linaro Ltd */ #include +#include #include #include #include @@ -97,31 +98,25 @@ void pmic_glink_client_register(struct pmic_glink_client *client) struct pmic_glink *pg = client->pg; unsigned long flags; - mutex_lock(&pg->state_lock); + guard(mutex)(&pg->state_lock); spin_lock_irqsave(&pg->client_lock, flags); list_add(&client->node, &pg->clients); client->pdr_notify(client->priv, pg->client_state); spin_unlock_irqrestore(&pg->client_lock, flags); - mutex_unlock(&pg->state_lock); - } EXPORT_SYMBOL_GPL(pmic_glink_client_register); int pmic_glink_send(struct pmic_glink_client *client, void *data, size_t len) { struct pmic_glink *pg = client->pg; - int ret; - mutex_lock(&pg->state_lock); + guard(mutex)(&pg->state_lock); if (!pg->ept) - ret = -ECONNRESET; + return -ECONNRESET; else - ret = rpmsg_send(pg->ept, data, len); - mutex_unlock(&pg->state_lock); - - return ret; + return rpmsg_send(pg->ept, data, len); } EXPORT_SYMBOL_GPL(pmic_glink_send); @@ -208,52 +203,42 @@ static void pmic_glink_pdr_callback(int state, char *svc_path, void *priv) { struct pmic_glink *pg = priv; - mutex_lock(&pg->state_lock); + guard(mutex)(&pg->state_lock); pg->pdr_state = state; pmic_glink_state_notify_clients(pg); - mutex_unlock(&pg->state_lock); } static int pmic_glink_rpmsg_probe(struct rpmsg_device *rpdev) { struct pmic_glink *pg; - int ret = 0; - mutex_lock(&__pmic_glink_lock); + guard(mutex)(&__pmic_glink_lock); pg = __pmic_glink; - if (!pg) { - ret = dev_err_probe(&rpdev->dev, -ENODEV, "no pmic_glink device to attach to\n"); - goto out_unlock; - } + if (!pg) + return dev_err_probe(&rpdev->dev, -ENODEV, "no pmic_glink device to attach to\n"); dev_set_drvdata(&rpdev->dev, pg); - mutex_lock(&pg->state_lock); + guard(mutex)(&pg->state_lock); pg->ept = rpdev->ept; pmic_glink_state_notify_clients(pg); - mutex_unlock(&pg->state_lock); -out_unlock: - mutex_unlock(&__pmic_glink_lock); - return ret; + return 0; } static void pmic_glink_rpmsg_remove(struct rpmsg_device *rpdev) { struct pmic_glink *pg; - mutex_lock(&__pmic_glink_lock); + guard(mutex)(&__pmic_glink_lock); pg = __pmic_glink; if (!pg) - goto out_unlock; + return; - mutex_lock(&pg->state_lock); + guard(mutex)(&pg->state_lock); pg->ept = NULL; pmic_glink_state_notify_clients(pg); - mutex_unlock(&pg->state_lock); -out_unlock: - mutex_unlock(&__pmic_glink_lock); } static const struct rpmsg_device_id pmic_glink_rpmsg_id_match[] = { @@ -326,9 +311,8 @@ static int pmic_glink_probe(struct platform_device *pdev) goto out_release_aux_devices; } - mutex_lock(&__pmic_glink_lock); + guard(mutex)(&__pmic_glink_lock); __pmic_glink = pg; - mutex_unlock(&__pmic_glink_lock); return 0; @@ -360,9 +344,8 @@ static void pmic_glink_remove(struct platform_device *pdev) if (pg->client_mask & BIT(PMIC_GLINK_CLIENT_UCSI)) pmic_glink_del_aux_device(pg, &pg->ucsi_aux); - mutex_lock(&__pmic_glink_lock); + guard(mutex)(&__pmic_glink_lock); __pmic_glink = NULL; - mutex_unlock(&__pmic_glink_lock); } static const unsigned long pmic_glink_sc8280xp_client_mask = BIT(PMIC_GLINK_CLIENT_BATT) |