From patchwork Thu Aug 11 19:02:22 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrej Krutak X-Patchwork-Id: 9275871 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id EE37260780 for ; Thu, 11 Aug 2016 19:54:50 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DA57528786 for ; Thu, 11 Aug 2016 19:54:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CEDB328787; Thu, 11 Aug 2016 19:54:50 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_NONE,T_DKIM_INVALID autolearn=no version=3.3.1 Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0EFA42875A for ; Thu, 11 Aug 2016 19:54:50 +0000 (UTC) Received: by alsa0.perex.cz (Postfix, from userid 1000) id 467CC267E0D; Thu, 11 Aug 2016 21:54:49 +0200 (CEST) Received: from alsa0.perex.cz (localhost [127.0.0.1]) by alsa0.perex.cz (Postfix) with ESMTP id 59A0E267ADC; Thu, 11 Aug 2016 21:42:05 +0200 (CEST) X-Original-To: alsa-devel@alsa-project.org Delivered-To: alsa-devel@alsa-project.org Received: by alsa0.perex.cz (Postfix, from userid 1000) id B5F8A267A7B; Thu, 11 Aug 2016 21:42:02 +0200 (CEST) Received: from mail-wm0-f67.google.com (mail-wm0-f67.google.com [74.125.82.67]) by alsa0.perex.cz (Postfix) with ESMTP id B80DE267754 for ; Thu, 11 Aug 2016 21:04:06 +0200 (CEST) Received: by mail-wm0-f67.google.com with SMTP id i5so932975wmg.2 for ; Thu, 11 Aug 2016 12:04:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=SWEiqZCmGcWf9ORcnPb8jCZ2oD5ekotolZniRIslV2A=; b=GagXuedcyT1dKDcXghoEphH76tJeRdLfa6K6q8EnWdr2MNVrh+VKlqP8LZrgpJKhbR 2sPLrdvRmIC/G+XC56R5APMrqLzKWdsMbsvWFgsCprXcBMFq1SrPVd4QcKbdzrSoArC0 ngh2jexiXJ/8VIUSb753QhcSpZScVrDIBEvX9PwuDI7nfNctr3z6Oc0BGM0brK/wn1SD ddahwFqTH0LH/DAJs8Yk4XjEtYj2Fc88xgnO5BEBqpBATaTPbaw5eCpkQ5xDnu7kvoWi 1SrNDJZq/FQxvHhWycRAS2QzK8NFZi5vRN+Pk6vWJg9aSjgDwOLnh81+9kZtIPptR6di 07Nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=SWEiqZCmGcWf9ORcnPb8jCZ2oD5ekotolZniRIslV2A=; b=hxkUcYgS28yHtDJbAhuCB4nRqVdJBWGWXSsQJfhFC5JXQ/GkYAhSmfJBbys9+NSZwP VfnFED3RjC5t//Rbt5tTOwP+5ef5Lcc1fEQcy3lIEG+JhTJzlPirm4NMcvbyWLoXE/pO DZS8QEsK8ErQzA+QJYekUuZL2RZLss7+m2glhh4UzcAh9PkVHzKmZKauj0Di5GoaSvy8 1MTiP0sIEC/l9toG+jQQcLGGH83bnxEXpDNwjG/KFk2cB8Yq0bE4Oiox+RIolimlYxiW dzSc0b4w+tNUy9q3WzPZxX5BPGJFy0Ee/g//xUxXxPg6CPZmrUh6mvsa9TMJvyo6RWsq 2kow== X-Gm-Message-State: AEkoousFQbbED8tmg8xCnkJwHTkRTs2IzIKQ7ElvHBDxKd9fSxuux0Z/XIse0PLs825EZQ== X-Received: by 10.28.165.3 with SMTP id o3mr11780213wme.3.1470942246436; Thu, 11 Aug 2016 12:04:06 -0700 (PDT) Received: from localhost.localdomain ([94.229.40.98]) by smtp.gmail.com with ESMTPSA id x6sm4001376wjk.26.2016.08.11.12.04.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 11 Aug 2016 12:04:05 -0700 (PDT) From: Andrej Krutak To: alsa-devel@alsa-project.org Date: Thu, 11 Aug 2016 21:02:22 +0200 Message-Id: <1470942147-19848-11-git-send-email-dev@andree.sk> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1470942147-19848-1-git-send-email-dev@andree.sk> References: <1470942147-19848-1-git-send-email-dev@andree.sk> Cc: Andrej Krutak Subject: [alsa-devel] [PATCH 10/15] ALSA: line6: Add proper locks for hwdep open/release/read X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: alsa-devel-bounces@alsa-project.org Sender: alsa-devel-bounces@alsa-project.org X-Virus-Scanned: ClamAV using ClamSMTP Signed-off-by: Andrej Krutak --- sound/usb/line6/driver.c | 16 +++++++++++----- sound/usb/line6/driver.h | 3 ++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/sound/usb/line6/driver.c b/sound/usb/line6/driver.c index 0a0e324..344465f 100644 --- a/sound/usb/line6/driver.c +++ b/sound/usb/line6/driver.c @@ -541,8 +541,7 @@ static int line6_hwdep_open(struct snd_hwdep *hw, struct file *file) line6->buffer_circular.head = 0; line6->buffer_circular.tail = 0; sema_init(&line6->buffer_circular.sem, 0); - - line6->buffer_circular.active = 1; + spin_lock_init(&line6->buffer_circular.read_lock); line6->buffer_circular.data = kmalloc(LINE6_MESSAGE_MAXLEN * LINE6_MESSAGE_MAXCOUNT, GFP_KERNEL); @@ -567,7 +566,7 @@ static int line6_hwdep_release(struct snd_hwdep *hw, struct file *file) /* By this time, no readers are waiting, we can safely recreate the * semaphore at next open. */ - line6->buffer_circular.active = 0; + clear_bit(0, &line6->buffer_circular.active); kfree(line6->buffer_circular.data); kfree(line6->buffer_circular.data_len); @@ -581,18 +580,23 @@ line6_hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count, { struct usb_line6 *line6 = hwdep->private_data; unsigned long tail; + long rv = 0; if (down_interruptible(&line6->buffer_circular.sem)) { return -ERESTARTSYS; } /* There must an item now in the buffer... */ + spin_lock(&line6->buffer_circular.read_lock); + tail = line6->buffer_circular.tail; if (line6->buffer_circular.data_len[tail] > count) { /* Buffer too small; allow re-read of the current item... */ up(&line6->buffer_circular.sem); - return -EINVAL; + + rv = -EINVAL; + goto end; } if (copy_to_user(buf, @@ -608,7 +612,9 @@ line6_hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count, smp_store_release(&line6->buffer_circular.tail, (tail + 1) & (LINE6_MESSAGE_MAXCOUNT - 1)); - return 0; +end: + spin_unlock(&line6->buffer_circular.read_lock); + return rv; } /* Write directly (no buffering) to device by user*/ diff --git a/sound/usb/line6/driver.h b/sound/usb/line6/driver.h index f4ab6cd..05e155c 100644 --- a/sound/usb/line6/driver.h +++ b/sound/usb/line6/driver.h @@ -159,12 +159,13 @@ struct usb_line6 { /* Circular buffer for non-MIDI control messages */ struct { - int active; + unsigned long active; char *data; int *data_len; unsigned long head, tail; /* Actually is up'd # of items in the buffer - times */ struct semaphore sem; + spinlock_t read_lock; } buffer_circular; /* If MIDI is supported, buffer_message contains the pre-processed data;