Received: by 10.192.165.156 with SMTP id m28csp537320imm; Thu, 19 Apr 2018 03:27:13 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/3kKge292nsszjeIEmbzCRz8jYQFSJHkVDTUY9EU0qz0hWJ4xAVUUdR4r/OOiFf+ng3tVD X-Received: by 2002:a17:902:108a:: with SMTP id c10-v6mr5145466pla.111.1524133633560; Thu, 19 Apr 2018 03:27:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524133633; cv=none; d=google.com; s=arc-20160816; b=j5w9lLS0fYcyYWxKKfPjq8UwmBx5SBXeLB6UEtm319GMVHS4N+57grGE+/U2IN/+Xg +qvPktyjKjJPx1hsYZ+f1jcWayvhauY6TCeVg3xrbBif6Vou2fCCQIoL3rHgPliM7dsg R1Cyw8Vc8tEnqti4eLdhbnW0PPP/u/PtCTVjrFuJAus2v8RsVRzyySaAE2Vq6nA3h7oW WxpGdipNEMkSNktmJJDrIi4t6jgjDdADquL8pFL2e7+Q6RiHtWGq+fU6KWOBuCFxlnOD rLi/FIQI9mFyhfUsonXDbKtWDyYRY+NckCS26xFAheQth1j79dZoud8DWkOgvUQv/QMv ZTGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:subject:references:in-reply-to:message-id :date:cc:to:from:arc-authentication-results; bh=Q6hd8LP8rzTnkZxcXEGJVHqXNHfWc6VlT62YKH7vpIc=; b=fquDCnZEZMSyEqPArxpKyoq1G3siB9xIX9Bbtbe6Jbc30U5n64wqQD6oXNrppWbVB8 1BQ+G4DAZP78LrrivL2dIuLfAiCV+eTvtBp4DarpNLcNBkFKZ94EWgBu6DkXD/xC8woo 1vzhkmL5Q1uAJrPB1cd2agpPuB6YtnR9QMWDz4b9Y/etA1qVl/uHmHBi449/++9/s6aC cL7uiGNSDVUvmZGjIe5dh2zDjJOlpJRjwdbCeF2R2qI1P3bvt5H1FgEWs7Ehm6ZdIY0a 9kjfRX+MCP9Hy+dX7NhA0AxC+HIP+Qeh7iMN/NhL6xKVaGYpDExvAT+/+DlhgeLGcQ04 aemw== 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 d10si2874074pfn.84.2018.04.19.03.26.59; Thu, 19 Apr 2018 03:27:13 -0700 (PDT) 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 S1752028AbeDSKZw (ORCPT + 99 others); Thu, 19 Apr 2018 06:25:52 -0400 Received: from mail.CARNet.hr ([161.53.123.6]:52400 "EHLO mail.carnet.hr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751042AbeDSKZv (ORCPT ); Thu, 19 Apr 2018 06:25:51 -0400 Received: from cnzgrivvl-t440p.carpriv.carnet.hr ([161.53.12.131]:41442 helo=gavran.carpriv.carnet.hr) by mail.carnet.hr with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1f96l6-0000vd-TB; Thu, 19 Apr 2018 12:25:46 +0200 Received: by gavran.carpriv.carnet.hr (Postfix, from userid 1000) id 8233420122; Thu, 19 Apr 2018 12:25:44 +0200 (CEST) From: Valentin Vidic To: Marcus Wolf Cc: =?UTF-8?q?Simon=20Sandstr=C3=B6m?= , Greg Kroah-Hartman , =?UTF-8?q?Luca=20S=C3=B6the?= , Marcin Ciupak , Michael Panzlaff , Derek Robson , devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org, Valentin Vidic Date: Thu, 19 Apr 2018 12:25:30 +0200 Message-Id: <20180419102530.5927-1-Valentin.Vidic@CARNet.hr> X-Mailer: git-send-email 2.17.0 In-Reply-To: <4dba7594-2661-edd5-24ba-cffdf24021c5@smarthome-wolf.de> References: <4dba7594-2661-edd5-24ba-cffdf24021c5@smarthome-wolf.de> X-SA-Exim-Connect-IP: 161.53.12.131 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on rigel.CARNet.hr X-Spam-Level: X-Spam-Status: No, score=-2.9 required=10.0 tests=ALL_TRUSTED,BAYES_00 autolearn=ham version=3.3.2 Subject: [PATCH v2] staging: pi433: cleanup tx_fifo locking X-SA-Exim-Version: 4.2.1 (built Mon, 26 Dec 2011 16:24:06 +0000) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org pi433_write requires locking due to multiple writers. After acquiring the lock check if enough free space is available in the kfifo to write the whole message. This check should prevent partial writes to tx_fifo so kfifo_reset is not needed anymore. pi433_tx_thread is the only reader so it does not require locking after kfifo_reset is removed. Signed-off-by: Valentin Vidic --- v2: print a warning if partial fifo write happens drivers/staging/pi433/pi433_if.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/drivers/staging/pi433/pi433_if.c b/drivers/staging/pi433/pi433_if.c index d1e0ddbc79ce..2a05eff88469 100644 --- a/drivers/staging/pi433/pi433_if.c +++ b/drivers/staging/pi433/pi433_if.c @@ -87,7 +87,7 @@ struct pi433_device { /* tx related values */ STRUCT_KFIFO_REC_1(MSG_FIFO_SIZE) tx_fifo; - struct mutex tx_fifo_lock; // TODO: check, whether necessary or obsolete + struct mutex tx_fifo_lock; /* serialize userspace writers */ struct task_struct *tx_task_struct; wait_queue_head_t tx_wait_queue; u8 free_in_fifo; @@ -589,19 +589,15 @@ pi433_tx_thread(void *data) * - size of message * - message */ - mutex_lock(&device->tx_fifo_lock); - retval = kfifo_out(&device->tx_fifo, &tx_cfg, sizeof(tx_cfg)); if (retval != sizeof(tx_cfg)) { dev_dbg(device->dev, "reading tx_cfg from fifo failed: got %d byte(s), expected %d", retval, (unsigned int)sizeof(tx_cfg)); - mutex_unlock(&device->tx_fifo_lock); continue; } retval = kfifo_out(&device->tx_fifo, &size, sizeof(size_t)); if (retval != sizeof(size_t)) { dev_dbg(device->dev, "reading msg size from fifo failed: got %d, expected %d", retval, (unsigned int)sizeof(size_t)); - mutex_unlock(&device->tx_fifo_lock); continue; } @@ -634,7 +630,6 @@ pi433_tx_thread(void *data) sizeof(device->buffer) - position); dev_dbg(device->dev, "read %d message byte(s) from fifo queue.", retval); - mutex_unlock(&device->tx_fifo_lock); /* if rx is active, we need to interrupt the waiting for * incoming telegrams, to be able to send something. @@ -818,7 +813,7 @@ pi433_write(struct file *filp, const char __user *buf, struct pi433_instance *instance; struct pi433_device *device; int retval; - unsigned int copied; + unsigned int required, available, copied; instance = filp->private_data; device = instance->device; @@ -833,6 +828,16 @@ pi433_write(struct file *filp, const char __user *buf, * - message */ mutex_lock(&device->tx_fifo_lock); + + required = sizeof(instance->tx_cfg) + sizeof(size_t) + count; + available = kfifo_avail(&device->tx_fifo); + if (required > available) { + dev_dbg(device->dev, "write to fifo failed: %d bytes required but %d available", + required, available); + mutex_unlock(&device->tx_fifo_lock); + return -EAGAIN; + } + retval = kfifo_in(&device->tx_fifo, &instance->tx_cfg, sizeof(instance->tx_cfg)); if (retval != sizeof(instance->tx_cfg)) @@ -855,8 +860,8 @@ pi433_write(struct file *filp, const char __user *buf, return copied; abort: - dev_dbg(device->dev, "write to fifo failed: 0x%x", retval); - kfifo_reset(&device->tx_fifo); // TODO: maybe find a solution, not to discard already stored, valid entries + dev_warn(device->dev, + "write to fifo failed, non recoverable: 0x%x", retval); mutex_unlock(&device->tx_fifo_lock); return -EAGAIN; } -- 2.17.0