From patchwork Sat Dec 11 15:54:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Armin Wolf X-Patchwork-Id: 12671823 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 08D7AC433F5 for ; Sat, 11 Dec 2021 15:55:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230204AbhLKPzA (ORCPT ); Sat, 11 Dec 2021 10:55:00 -0500 Received: from mout.gmx.net ([212.227.17.22]:60843 "EHLO mout.gmx.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231364AbhLKPzA (ORCPT ); Sat, 11 Dec 2021 10:55:00 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1639238080; bh=ejKcLUvHddgrUEMS0flKbxFEHFJj1izRytQyFU0ff5w=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=YbimlK7FU7tXMKuFv4h09xLR5bjQZ9Btur9XEcr2kvQzlS5QJoSyNJSWdpD9PQ7Z6 Pwyc5iIMJZdESjpoYVoxJtyq1PwIvBTE+cg8L7Hn4Ocpl3zp/UUulb/WOD093NJuTo KwDD6nfLV75exijEkehfO0QsWEGj8vyUq2qZeZI8= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from esprimo-mx.fritz.box ([91.137.126.34]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1Ml6qC-1mF4Oa1weS-00lS8R; Sat, 11 Dec 2021 16:54:40 +0100 From: Armin Wolf To: pali@kernel.org Cc: jdelvare@suse.com, linux@roeck-us.net, linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 1/2] hwmon: (dell-smm) Simplify ioctl handler Date: Sat, 11 Dec 2021 16:54:21 +0100 Message-Id: <20211211155422.16830-2-W_Armin@gmx.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211211155422.16830-1-W_Armin@gmx.de> References: <20211211155422.16830-1-W_Armin@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:tTCSxSzTXohQ7PY0EcALDH5K0O/uBfh9WZWj5MXCQVSqZpe2HS9 FXGJvTide0zsiLNsmqDQIP6jRTtceFWGIJ/lYY3O/6V8RJZCzvc0QwtXOKlHddS/fQ+fVPc IKEtpTfgRK2BYE/rckm2Lry2KHApSaeGaRsA0Uu5e+mhKaOUwxGgebVtOexUWAjgo5OqTK+ 7LW6XuTW0ZnBmVF6IIjuA== X-UI-Out-Filterresults: notjunk:1;V03:K0:11EJANrfG3s=:jE+fhfhZaXSPxTr4gtopjE ulgyNnIHn8q2Q8wye6chpOrrJcCb5U6oCFvHP/KmIJrXHewmn/3kq0L5fbvEX2vlbZsDWE2tz cf0HGni9lw6zT04MM1O64lCtzTK5Ee+Si3Pbnwa9bl89dxRgHJeUnBlmeJ7qSpmLsuJfC38ap hXjZlrUXcMC7Llx873fOxY218aEnR+gpKH/s7GzkN6Z6eKForaV2IOtSGQ+nQ0SAZie84HnNB gMMgXk+ngZ3hbB6N6/mWr3T6+KTvt6Y3uL+L/so4mHXPHUM2nR123VUh5/1ET7VjuXk9mC59f gbaCDXCqdzfLVOHcdlg5K4pJbwGeqlVR39DBzgR5dj72uY2aqwpn5Ww/Ck9Apbf6Ywc6nCujA YxzlSsGAhirjozaOod0BtSc9C3LZQyGPA2PpxrUAnoLwcCJkYkqCpnFTxnKZHgNjPPSgWgnO9 JVgSB38IPIElyDIb/RqrdwEoCADx/IVeP0cjD8q+F/MOahiKb2cVkicYHzYFH3HpUz0gMBGrY 8vpXziKb+Gp0dSRCrx3i1DJJCW2eaGFZWf3QknPSWUxGdrHwzhImJ+BwLJzWMnZVniwEQrX9z jj9iRm06vE3nI0OxU8tBHPgFHa35MShV6XxwEQ2FMPMNus+SdQ4drK/rE6ixKO0SAT7qwIrd9 PI3UUwkVOM570NpurbWobGv9MzGREjA8sTiyYIkbJz2IoRroLLObxVTLN+Hjo1ljTOkm4hFgU 4zry//2EwCQwCCrt6zDXiRVYtL5s7iJXarlCntO4gXEkC8Q7GtS9ssE5og5HudHMYa5dpBy0m N3SutanDdj4EqpKBwX9/WWUlfb91jqgV1Q0OfcpMD45RGl1zyiXHCc+SfVtJXeM34IwBJhIQg 8NF4OHUdYUWOsEGiE+/W8YWg8ZsN4hOlgT/HIKbmkM1L2KdwQvpL9BDMIk3HslUytWTbYMoUA ymgsIAafGzRo4hF5x27Fal3dpqrc3fp4e1n6DcppVJQkFV8V3TowTxrrFlW8S6JaKvlF/Lqe6 bRdHyS+8v1T+fIOSyFJ960YY1hjNyFMXIPCCabZlB4XMBSz4lO2X/1powPnudNPm3YIwiM/L7 IzrCnWrr9O62K4= Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org The second switch-case has no real purpose: - for I8K_BIOS_VERSION, val does not represent a return value, making the check for error values unnecessary. - for I8K_MACHINE_ID, val remains zero, so the error check is unnecessary too. Remove the switch-case and move the calls to copy_to_user() into the first switch-case for I8K_BIOS_VERSION/_MACHINE_ID. Omit buff[] since data->bios_machineid already contains the string with the necessary zero padding through devm_kzalloc(). Tested on a Dell Inspiron 3505. Signed-off-by: Armin Wolf Reviewed-by: Pali Rohár --- drivers/hwmon/dell-smm-hwmon.c | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) -- 2.30.2 diff --git a/drivers/hwmon/dell-smm-hwmon.c b/drivers/hwmon/dell-smm-hwmon.c index 5596c211f38d..186d40938036 100644 --- a/drivers/hwmon/dell-smm-hwmon.c +++ b/drivers/hwmon/dell-smm-hwmon.c @@ -454,7 +454,6 @@ i8k_ioctl_unlocked(struct file *fp, struct dell_smm_data *data, unsigned int cmd { int val = 0; int speed, err; - unsigned char buff[16]; int __user *argp = (int __user *)arg; if (!argp) @@ -468,15 +467,19 @@ i8k_ioctl_unlocked(struct file *fp, struct dell_smm_data *data, unsigned int cmd val = (data->bios_version[0] << 16) | (data->bios_version[1] << 8) | data->bios_version[2]; - break; + if (copy_to_user(argp, &val, sizeof(val))) + return -EFAULT; + + return 0; case I8K_MACHINE_ID: if (restricted && !capable(CAP_SYS_ADMIN)) return -EPERM; - strscpy_pad(buff, data->bios_machineid, sizeof(buff)); - break; + if (copy_to_user(argp, data->bios_machineid, sizeof(data->bios_machineid))) + return -EFAULT; + return 0; case I8K_FN_STATUS: val = i8k_get_fn_status(); break; @@ -527,23 +530,8 @@ i8k_ioctl_unlocked(struct file *fp, struct dell_smm_data *data, unsigned int cmd if (val < 0) return val; - switch (cmd) { - case I8K_BIOS_VERSION: - if (copy_to_user(argp, &val, 4)) - return -EFAULT; - - break; - case I8K_MACHINE_ID: - if (copy_to_user(argp, buff, 16)) - return -EFAULT; - - break; - default: - if (copy_to_user(argp, &val, sizeof(int))) - return -EFAULT; - - break; - } + if (copy_to_user(argp, &val, sizeof(int))) + return -EFAULT; return 0; } From patchwork Sat Dec 11 15:54:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Armin Wolf X-Patchwork-Id: 12671825 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 436FEC433FE for ; Sat, 11 Dec 2021 15:55:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231379AbhLKPzB (ORCPT ); Sat, 11 Dec 2021 10:55:01 -0500 Received: from mout.gmx.net ([212.227.17.21]:59927 "EHLO mout.gmx.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231368AbhLKPzA (ORCPT ); Sat, 11 Dec 2021 10:55:00 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1639238082; bh=m568THOr9bt8drU5mZJQIxwuBPDKHdZftpxVk033ksw=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=BcRJ3TK3aR+bEfFhLqjlK+A+L5muGiBb0E0rGUp2nHLRt7ODh+VgTNcmxKhS48HRE FiWEpIPAeYoF+zvsbnHKmvOAUlv6vWCfOHZ+R/AOPJfq8+BSEmbcb/7IZtXp6McCPW Ff0rc2wQxblAzhnhEbG2vouq6XWUjDhtJ9SYyvY0= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from esprimo-mx.fritz.box ([91.137.126.34]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1M6Daq-1mtsr41eES-006dXs; Sat, 11 Dec 2021 16:54:42 +0100 From: Armin Wolf To: pali@kernel.org Cc: jdelvare@suse.com, linux@roeck-us.net, linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/2] hwmon: (dell-smm) Unify i8k_ioctl() and i8k_ioctl_unlocked() Date: Sat, 11 Dec 2021 16:54:22 +0100 Message-Id: <20211211155422.16830-3-W_Armin@gmx.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211211155422.16830-1-W_Armin@gmx.de> References: <20211211155422.16830-1-W_Armin@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:xrtytWxXih+yIIn3dgrrUvFTd142Fajn1/6o4bZpufa12OUItdv yjfKHdKEENGmZ3fitgt45CLi/JydwyW+yL7aoOBcN7GvJNXFSplqKRBMSC6hbqilK4dfdvr 1Nl+Sf2rdImV9OfFLyMkZ6IkYmt/faFmeljPLVjHZmhYhOT/jhWE7ZgtPwGq+JumHmTdIz9 gerWhpmfaGGenhTOYrC+Q== X-UI-Out-Filterresults: notjunk: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/n7jJYtWIPnt2LBrsacDeURScjg/aZs9+44aLhA4GbRX Z4SBmTSbVW+VbY= Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org The only purpose of i8k_ioctl() is to call i8k_ioctl_unlocked() with i8k_mutex held. Judging from the hwmon code, this mutex only needs to be held when setting the fan speed/mode, so the operation of I8K_SET_FAN is guaranteed to be atomic. Unify both functions and reduce the locking of i8k_mutex to I8K_SET_FAN. Tested on a Dell Inspiron 3505. Signed-off-by: Armin Wolf Reviewed-by: Pali Rohár --- drivers/hwmon/dell-smm-hwmon.c | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) -- 2.30.2 diff --git a/drivers/hwmon/dell-smm-hwmon.c b/drivers/hwmon/dell-smm-hwmon.c index 186d40938036..d8c6e75bb374 100644 --- a/drivers/hwmon/dell-smm-hwmon.c +++ b/drivers/hwmon/dell-smm-hwmon.c @@ -449,12 +449,12 @@ static int i8k_get_power_status(void) * Procfs interface */ -static int -i8k_ioctl_unlocked(struct file *fp, struct dell_smm_data *data, unsigned int cmd, unsigned long arg) +static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg) { - int val = 0; - int speed, err; + struct dell_smm_data *data = PDE_DATA(file_inode(fp)); int __user *argp = (int __user *)arg; + int speed, err; + int val = 0; if (!argp) return -EINVAL; @@ -516,11 +516,13 @@ i8k_ioctl_unlocked(struct file *fp, struct dell_smm_data *data, unsigned int cmd if (copy_from_user(&speed, argp + 1, sizeof(int))) return -EFAULT; + mutex_lock(&data->i8k_mutex); err = i8k_set_fan(data, val, speed); if (err < 0) - return err; - - val = i8k_get_fan_status(data, val); + val = err; + else + val = i8k_get_fan_status(data, val); + mutex_unlock(&data->i8k_mutex); break; default: @@ -536,18 +538,6 @@ i8k_ioctl_unlocked(struct file *fp, struct dell_smm_data *data, unsigned int cmd return 0; } -static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg) -{ - struct dell_smm_data *data = PDE_DATA(file_inode(fp)); - long ret; - - mutex_lock(&data->i8k_mutex); - ret = i8k_ioctl_unlocked(fp, data, cmd, arg); - mutex_unlock(&data->i8k_mutex); - - return ret; -} - /* * Print the information for /proc/i8k. */