Received: by 10.213.65.68 with SMTP id h4csp105757imn; Sat, 24 Mar 2018 15:10:40 -0700 (PDT) X-Google-Smtp-Source: AG47ELse07YbCdbmHkei9nTPxbm8NsDRAeXqoQLMZVlMRHV7w91U/5dOw6MczGo622ZXajxwW1WJ X-Received: by 2002:a17:902:102:: with SMTP id 2-v6mr34734491plb.48.1521929440542; Sat, 24 Mar 2018 15:10:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521929440; cv=none; d=google.com; s=arc-20160816; b=Mo/KfBxIkkLun1ONbVKymBXkr9Ar72JuQZv+6jdzMA03MVF7PW9xeK0rJju5aEnYso A6//nLkhzhKKM08gImvpF6EVATREgPGhmNSYzre+Y725AJGyXlC4JRd0R/85DLp60CLA I3yui8BBQOKJHVjFKqnzgQwpCA+YQGtuLTX/p9LyX3G9EWk578x9YCgTTsCzDUob9SRV XeT8XjsexKhChA84UMQmoHBicPjqDrzRtY0WEpT3uUg5DZOsYYDftnzt6P3+/nL0ozaP 5B5na3BQWFcnlhlduhbHc4e6JsMRPYFBLRwSHB8YbSi2+LGkSkugy2AqNtUlfrcWtdfW wQzA== 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=OpoNPc+/ZnNBMKiExSySAX4PTA51SDjO8gq2cTceafs=; b=Adnvq0lW9GWrGoI0kePDynYidQoImldzvUrst4MXRZS9mhgzdvp+21Z7T3ktF+DAda 5vJmdJVtOm8lYZJer1QQT00sTZ/QMlRa5B1x78wIz4mxjZ6VllONx6Ileb9Eu/IrMa71 kQCXur+CATFzLxbLl8+OgGzdjbzohk1KHrDVxywa51MEd5i+shExMB4dvYeZCPWclM3f oALGkWcuOQnh6XIbEBUSxSYTyBx9IQDEtQyVQnGG+oxwrG8pGFUhLuIeKZ6T4wImimXu p7tEbMlyyiU8wa6cbgSj8kLlHfn5DSr44XWrsq136XRIEs+znGSNA54k4p5A8cLq9urQ fA5g== 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 x4-v6si11222802plv.81.2018.03.24.15.10.25; Sat, 24 Mar 2018 15:10:40 -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 S1752496AbeCXWJe (ORCPT + 99 others); Sat, 24 Mar 2018 18:09:34 -0400 Received: from mail.CARNet.hr ([161.53.123.6]:47521 "EHLO mail.carnet.hr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752302AbeCXWJc (ORCPT ); Sat, 24 Mar 2018 18:09:32 -0400 Received: from [2001:b68:ff:12::131] (port=38590 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 1ezrLq-0000SH-5P; Sat, 24 Mar 2018 23:09:27 +0100 Received: by gavran.carpriv.carnet.hr (Postfix, from userid 1000) id D73F924EBF; Sat, 24 Mar 2018 23:09:25 +0100 (CET) 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: Sat, 24 Mar 2018 23:09:01 +0100 Message-Id: <20180324220901.28864-1-Valentin.Vidic@CARNet.hr> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180323221821.GP31333@gavran.carpriv.carnet.hr> References: <20180323221821.GP31333@gavran.carpriv.carnet.hr> X-SA-Exim-Connect-IP: 2001:b68:ff: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, URIBL_BLOCKED autolearn=ham version=3.3.2 Subject: [PATCH] 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 kfifo 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 kfifo so kfifo_reset is not needed anymore. pi433_tx_thread is the only kfifo reader so it does not require locking after kfifo_reset is also removed. Signed-off-by: Valentin Vidic --- drivers/staging/pi433/pi433_if.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/drivers/staging/pi433/pi433_if.c b/drivers/staging/pi433/pi433_if.c index d1e0ddbc79ce..97239b0eb322 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 multiple 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)) @@ -856,7 +861,6 @@ pi433_write(struct file *filp, const char __user *buf, 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 mutex_unlock(&device->tx_fifo_lock); return -EAGAIN; } -- 2.16.2