From nobody Tue Nov 26 06:39:21 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.14]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7C1D61EF923; Mon, 21 Oct 2024 13:37:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.14 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729517835; cv=none; b=gh6N311bIWIt+Ij1xHtfXpQQhzSoIcpvWl/j6mhVYFoxh61GHTxOI9LX2RNf3eJliMbLQH/VbYZ5V14ssxcfyiEFXdVu6qT72izDX7HlUYB7hmbbTtWbOs0E7xUeHw5LAD5AXZM4vIxnlOiRe2/tiVerKLqikpyoyOIYNJdB0Gc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729517835; c=relaxed/simple; bh=3MUQhfcoH/nVDWknWR7lxyKol5ZT85TjIh+n55UNxI8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KVHhfrcoVF+I/BR9ueh2Zfs/b1w1N/uLJHKly028mOAo2jf09428wW40tcIlqsFyGc0T2grs3QhpXdL5Trzk+6Q1IX7+8585Jw9PyLj1rDynKAPs6gHZUIuwSKIIkCt5q26D+9uPlEpSHrRCM4YyO+ibPBf3WhudFlrlp/F+7RQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Z0q+qn87; arc=none smtp.client-ip=192.198.163.14 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Z0q+qn87" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1729517833; x=1761053833; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=3MUQhfcoH/nVDWknWR7lxyKol5ZT85TjIh+n55UNxI8=; b=Z0q+qn87ZE9PhAg+CeMukEkrrNPHpVgrBqZJy4TESj653tV2Nepn/ohn rVlYiMKNLsD9SnpnSJ7kYfarzqihBCvXZpBPTGNKbEubhTLHn0N8uLGHE K/ZJCa3SCevjym1mxzN/z1tVsQgs7IYvRrKApWKPsiiOQ0paBJm+JdlwS ap570DTDeDwp0L7nVIFcwTO0lzfkc/u+aaf0fZBsYrIdIBXkaHzK48hBU 0Wu8xPIxF/CRVmp9jI3NVPjz94ETr68dx/GTGngvPxADa0jYjudExs6iv tov7+QAWQEZtIF7RErNfiuXbuOfd+9aAUpvgakyi8gIUGZZhu6yXdal6t A==; X-CSE-ConnectionGUID: rtNzciWiQ4eH6FpCCA2+Xw== X-CSE-MsgGUID: CjsBjLeYQpWfHixrQxRK/g== X-IronPort-AV: E=McAfee;i="6700,10204,11232"; a="29210970" X-IronPort-AV: E=Sophos;i="6.11,221,1725346800"; d="scan'208";a="29210970" Received: from fmviesa003.fm.intel.com ([10.60.135.143]) by fmvoesa108.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Oct 2024 06:37:10 -0700 X-CSE-ConnectionGUID: 0cwJbabsQVqhUF0xY3S2SA== X-CSE-MsgGUID: IlOKi4PtRAeUX2e4GS+hDQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,221,1725346800"; d="scan'208";a="83544887" Received: from black.fi.intel.com ([10.237.72.28]) by fmviesa003.fm.intel.com with ESMTP; 21 Oct 2024 06:37:08 -0700 Received: by black.fi.intel.com (Postfix, from userid 1003) id 60EE536B; Mon, 21 Oct 2024 16:37:07 +0300 (EEST) From: Andy Shevchenko To: Mika Westerberg , Andy Shevchenko , linux-kernel@vger.kernel.org, platform-driver-x86@vger.kernel.org Cc: Andy Shevchenko , Hans de Goede , =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= , Ferry Toth Subject: [PATCH v3 3/5] platform/x86: intel_scu_ipc: Simplify code with cleanup helpers Date: Mon, 21 Oct 2024 16:34:31 +0300 Message-ID: <20241021133705.2933464-4-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.43.0.rc1.1336.g36b5255a03ac In-Reply-To: <20241021133705.2933464-1-andriy.shevchenko@linux.intel.com> References: <20241021133705.2933464-1-andriy.shevchenko@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use macros defined in linux/cleanup.h to automate resource lifetime control in the driver. The byproduct of this change is that the negative conditionals are swapped by positive ones that are being attached to the respective calls, hence the code uses the regular pattern, i.e. checking for the error first. Tested-by: Ferry Toth Acked-by: Mika Westerberg Signed-off-by: Andy Shevchenko --- drivers/platform/x86/intel_scu_ipc.c | 65 +++++++++++----------------- 1 file changed, 25 insertions(+), 40 deletions(-) diff --git a/drivers/platform/x86/intel_scu_ipc.c b/drivers/platform/x86/in= tel_scu_ipc.c index 290b38627542..9829d4145c58 100644 --- a/drivers/platform/x86/intel_scu_ipc.c +++ b/drivers/platform/x86/intel_scu_ipc.c @@ -13,6 +13,7 @@ * along with other APIs. */ =20 +#include #include #include #include @@ -99,23 +100,21 @@ static struct class intel_scu_ipc_class =3D { */ struct intel_scu_ipc_dev *intel_scu_ipc_dev_get(void) { - struct intel_scu_ipc_dev *scu =3D NULL; + guard(mutex)(&ipclock); =20 - mutex_lock(&ipclock); if (ipcdev) { get_device(&ipcdev->dev); /* * Prevent the IPC provider from being unloaded while it * is being used. */ - if (!try_module_get(ipcdev->owner)) - put_device(&ipcdev->dev); - else - scu =3D ipcdev; + if (try_module_get(ipcdev->owner)) + return ipcdev; + + put_device(&ipcdev->dev); } =20 - mutex_unlock(&ipclock); - return scu; + return NULL; } EXPORT_SYMBOL_GPL(intel_scu_ipc_dev_get); =20 @@ -289,12 +288,11 @@ static int pwr_reg_rdwr(struct intel_scu_ipc_dev *scu= , u16 *addr, u8 *data, =20 memset(cbuf, 0, sizeof(cbuf)); =20 - mutex_lock(&ipclock); + guard(mutex)(&ipclock); + scu =3D intel_scu_ipc_get(scu); - if (IS_ERR(scu)) { - mutex_unlock(&ipclock); + if (IS_ERR(scu)) return PTR_ERR(scu); - } =20 for (nc =3D 0; nc < count; nc++, offset +=3D 2) { cbuf[offset] =3D addr[nc]; @@ -324,7 +322,6 @@ static int pwr_reg_rdwr(struct intel_scu_ipc_dev *scu, = u16 *addr, u8 *data, wbuf[nc] =3D ipc_data_readl(scu, offset); memcpy(data, wbuf, count); } - mutex_unlock(&ipclock); return err; } =20 @@ -446,17 +443,15 @@ int intel_scu_ipc_dev_simple_command(struct intel_scu= _ipc_dev *scu, int cmd, u32 cmdval; int err; =20 - mutex_lock(&ipclock); + guard(mutex)(&ipclock); + scu =3D intel_scu_ipc_get(scu); - if (IS_ERR(scu)) { - mutex_unlock(&ipclock); + if (IS_ERR(scu)) return PTR_ERR(scu); - } =20 cmdval =3D sub << 12 | cmd; ipc_command(scu, cmdval); err =3D intel_scu_ipc_check_status(scu); - mutex_unlock(&ipclock); if (err) dev_err(&scu->dev, "IPC command %#x failed with %d\n", cmdval, err); return err; @@ -491,12 +486,11 @@ int intel_scu_ipc_dev_command_with_size(struct intel_= scu_ipc_dev *scu, int cmd, if (inbuflen > 4 || outbuflen > 4) return -EINVAL; =20 - mutex_lock(&ipclock); + guard(mutex)(&ipclock); + scu =3D intel_scu_ipc_get(scu); - if (IS_ERR(scu)) { - mutex_unlock(&ipclock); + if (IS_ERR(scu)) return PTR_ERR(scu); - } =20 memcpy(inbuf, in, inlen); for (i =3D 0; i < inbuflen; i++) @@ -515,7 +509,6 @@ int intel_scu_ipc_dev_command_with_size(struct intel_sc= u_ipc_dev *scu, int cmd, memcpy(out, outbuf, outlen); } =20 - mutex_unlock(&ipclock); if (err) dev_err(&scu->dev, "IPC command %#x failed with %d\n", cmdval, err); return err; @@ -572,18 +565,15 @@ __intel_scu_ipc_register(struct device *parent, struct intel_scu_ipc_dev *scu; void __iomem *ipc_base; =20 - mutex_lock(&ipclock); + guard(mutex)(&ipclock); + /* We support only one IPC */ - if (ipcdev) { - err =3D -EBUSY; - goto err_unlock; - } + if (ipcdev) + return ERR_PTR(-EBUSY); =20 scu =3D kzalloc(sizeof(*scu), GFP_KERNEL); - if (!scu) { - err =3D -ENOMEM; - goto err_unlock; - } + if (!scu) + return ERR_PTR(-ENOMEM); =20 scu->owner =3D owner; scu->dev.parent =3D parent; @@ -621,13 +611,11 @@ __intel_scu_ipc_register(struct device *parent, err =3D device_register(&scu->dev); if (err) { put_device(&scu->dev); - goto err_unlock; + return ERR_PTR(err); } =20 /* Assign device at last */ ipcdev =3D scu; - mutex_unlock(&ipclock); - return scu; =20 err_unmap: @@ -636,9 +624,6 @@ __intel_scu_ipc_register(struct device *parent, release_mem_region(scu_data->mem.start, resource_size(&scu_data->mem)); err_free: kfree(scu); -err_unlock: - mutex_unlock(&ipclock); - return ERR_PTR(err); } EXPORT_SYMBOL_GPL(__intel_scu_ipc_register); @@ -652,12 +637,12 @@ EXPORT_SYMBOL_GPL(__intel_scu_ipc_register); */ void intel_scu_ipc_unregister(struct intel_scu_ipc_dev *scu) { - mutex_lock(&ipclock); + guard(mutex)(&ipclock); + if (!WARN_ON(!ipcdev)) { ipcdev =3D NULL; device_unregister(&scu->dev); } - mutex_unlock(&ipclock); } EXPORT_SYMBOL_GPL(intel_scu_ipc_unregister); =20 --=20 2.43.0.rc1.1336.g36b5255a03ac