Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp1823785ybv; Fri, 21 Feb 2020 04:08:26 -0800 (PST) X-Google-Smtp-Source: APXvYqxpheW8Ft2mm0tdg9i+r0YSQVOKP4zGfIreq0QW7mDZQGqNL/IUBC2GUc0Az3P8n9B5mw11 X-Received: by 2002:a9d:6e8e:: with SMTP id a14mr26439522otr.109.1582286906811; Fri, 21 Feb 2020 04:08:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582286906; cv=none; d=google.com; s=arc-20160816; b=LdP75XMmTfwnFCNC+cn9trT6So4hOq0y9txolyHnPiNvfY+z3a5/GaRArFgDUwaZMQ p4yVzIbg/PSl6DljNXlbxTBl3WFykrBzSD8t7WmHAY+lQFun2UNvfpKJmH3mckbo9JLe 81sZiJwjFcfVNCVXRyuFA8ox9FRoorX5L5e0BPTm4PY1UFz6DaRhR/lhuPdHUZ6zW8Ut /Hnh8mtWn6fcAAMo/C6ESPed5yTNKTIfjer+vBvpfLQM09TPZo0HmZTPK1KCNXEpF9YD h+mlPepCxnFFgKzzZxdA9kOJg3HmkNiJExp5jzb9zWyukqAo603E7rZYmeYtXCmOD4Nc L5QQ== 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 :message-id:date:subject:cc:to:from; bh=5MyBOKDXfIR+Dg++9eqWrOiH93wLfpPOWdjlSOy3E4k=; b=UJs9R4KI2YBZfQTDwM9pPDyx6OZh64DtEYOtnt5SdlAfqil96kq5nRQIhRb/Xlvfj/ OxvKiPVhpVV830Dcoma9vBVykVt3KaCSyqAFlgiUQS9eo9sB83Gj+uxDdQJppMrciMNr 9AFotgnZtezazNyJqKhhGBvRswFPWkN2DaZ6At3/1wpf4hqD2ZXdMHI8u9YOy4Mjf45j J2UgsOo45j2UXeJ8Sa6Jk/zQI47YyOEzrqw1uZ3yDycJ+IwHsbViCbPjcVGYNfS9Hu2j Y0Ow713TdJGN6CVTTxUn4GcIBzT3l+TNnHGUKZOs9xjldSLis+wsIAmL9dGBcEl6JrTN h7yg== ARC-Authentication-Results: i=1; mx.google.com; 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 r22si1287720otn.192.2020.02.21.04.08.14; Fri, 21 Feb 2020 04:08:26 -0800 (PST) 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; 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 S1728085AbgBUMH1 (ORCPT + 99 others); Fri, 21 Feb 2020 07:07:27 -0500 Received: from mx0a-00128a01.pphosted.com ([148.163.135.77]:4650 "EHLO mx0a-00128a01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727053AbgBUMH0 (ORCPT ); Fri, 21 Feb 2020 07:07:26 -0500 Received: from pps.filterd (m0167088.ppops.net [127.0.0.1]) by mx0a-00128a01.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 01LC0PKc028503; Fri, 21 Feb 2020 07:07:25 -0500 Received: from nwd2mta3.analog.com ([137.71.173.56]) by mx0a-00128a01.pphosted.com with ESMTP id 2y8uduq0k7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 21 Feb 2020 07:07:24 -0500 Received: from SCSQMBX10.ad.analog.com (scsqmbx10.ad.analog.com [10.77.17.5]) by nwd2mta3.analog.com (8.14.7/8.14.7) with ESMTP id 01LC7Nkk032415 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=FAIL); Fri, 21 Feb 2020 07:07:23 -0500 Received: from SCSQCASHYB6.ad.analog.com (10.77.17.132) by SCSQMBX10.ad.analog.com (10.77.17.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1779.2; Fri, 21 Feb 2020 04:07:21 -0800 Received: from SCSQMBX10.ad.analog.com (10.77.17.5) by SCSQCASHYB6.ad.analog.com (10.77.17.132) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1779.2; Fri, 21 Feb 2020 04:06:32 -0800 Received: from zeus.spd.analog.com (10.64.82.11) by SCSQMBX10.ad.analog.com (10.77.17.5) with Microsoft SMTP Server id 15.1.1779.2 via Frontend Transport; Fri, 21 Feb 2020 04:07:21 -0800 Received: from tachici-Precision-5530.ad.analog.com ([10.48.65.175]) by zeus.spd.analog.com (8.15.1/8.15.1) with ESMTP id 01LC7H1L018878; Fri, 21 Feb 2020 07:07:18 -0500 From: Alexandru Tachici To: , CC: , Alexandru Tachici , Alexandru Ardelean Subject: [PATCH v2] iio: industrialio-core: Fix debugfs read Date: Fri, 21 Feb 2020 14:06:55 +0200 Message-ID: <20200221120655.20252-1-alexandru.tachici@analog.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-ADIRoutedOnPrem: True X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.138,18.0.572 definitions=2020-02-21_03:2020-02-19,2020-02-21 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 malwarescore=0 impostorscore=0 spamscore=0 suspectscore=0 mlxlogscore=841 phishscore=0 mlxscore=0 priorityscore=1501 clxscore=1015 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2001150001 definitions=main-2002210093 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Currently iio_debugfs_read_reg calls debugfs_reg_access every time it is ran. Reading the same hardware register multiple times during the same reading of a debugfs file can cause unintended effects. For example for each: cat iio:device0/direct_reg_access the file_operations.read function will be called at least twice. First will return the full length of the string in bytes and the second will return 0. This patch makes iio_debugfs_read_reg to call debugfs_reg_access only when the user's buffer position (*ppos) is 0. (meaning it is the beginning of a new reading of the debugfs file). Fixes: e553f182d55b ("staging: iio: core: Introduce debugfs support, add support for direct register access") Signed-off-by: Alexandru Ardelean Signed-off-by: Alexandru Tachici --- drivers/iio/industrialio-core.c | 15 +++++++++++---- include/linux/iio/iio.h | 2 ++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c index 65ff0d067018..c4d5104934fc 100644 --- a/drivers/iio/industrialio-core.c +++ b/drivers/iio/industrialio-core.c @@ -301,11 +301,14 @@ static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf, size_t count, loff_t *ppos) { struct iio_dev *indio_dev = file->private_data; - char buf[20]; unsigned val = 0; - ssize_t len; int ret; + if (*ppos > 0) + return simple_read_from_buffer(userbuf, count, ppos, + indio_dev->read_buf, + indio_dev->read_buf_len); + ret = indio_dev->info->debugfs_reg_access(indio_dev, indio_dev->cached_reg_addr, 0, &val); @@ -314,9 +317,13 @@ static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf, return ret; } - len = snprintf(buf, sizeof(buf), "0x%X\n", val); + indio_dev->read_buf_len = snprintf(indio_dev->read_buf, + sizeof(indio_dev->read_buf), + "0x%X\n", val); - return simple_read_from_buffer(userbuf, count, ppos, buf, len); + return simple_read_from_buffer(userbuf, count, ppos, + indio_dev->read_buf, + indio_dev->read_buf_len); } static ssize_t iio_debugfs_write_reg(struct file *file, diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h index 862ce0019eba..eed58ed2f368 100644 --- a/include/linux/iio/iio.h +++ b/include/linux/iio/iio.h @@ -568,6 +568,8 @@ struct iio_dev { #if defined(CONFIG_DEBUG_FS) struct dentry *debugfs_dentry; unsigned cached_reg_addr; + char read_buf[20]; + unsigned int read_buf_len; #endif }; -- 2.20.1