From patchwork Thu Oct 26 18:37:37 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Scheller X-Patchwork-Id: 10028687 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 AD0AC601E8 for ; Thu, 26 Oct 2017 18:37:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A546828E65 for ; Thu, 26 Oct 2017 18:37:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9A1EF28E92; Thu, 26 Oct 2017 18:37:48 +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=-6.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B064028E65 for ; Thu, 26 Oct 2017 18:37:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932437AbdJZShp (ORCPT ); Thu, 26 Oct 2017 14:37:45 -0400 Received: from mail-wr0-f193.google.com ([209.85.128.193]:52756 "EHLO mail-wr0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932407AbdJZShm (ORCPT ); Thu, 26 Oct 2017 14:37:42 -0400 Received: by mail-wr0-f193.google.com with SMTP id k62so4074317wrc.9 for ; Thu, 26 Oct 2017 11:37:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=dHRqe8mc9mBnEA6oadi5Gsl+GBpxgJtgg/vKJ7NJPuM=; b=cmWAOM5ZfswrflesRQZ4I96u7NXTE/LZebUiU7qJvOddCV/aS+/5NGbN0Xl1hI3VNv al4sAq7+aii2py6+3CK4BJIabgIXbbr2Jc1Hp2SnvcKIo5hEd6lM8rxpdGGYCFo7yhty uM3auq3luek97+CwNuhKLBeUfqvUgBds/nWBeNXcKv+tF8LLcdcpdyAKyFgw07VStKoj 8yRe91YuNeEJx3DxCOLPBNmiNFXed+JMfHf5qTl74PqF4Mhj6hGo3Nla0hpUP2T3P1Ql MaRGiQoXBCoxiWi3ThG9kGzylFEOcg8SKxcpCUX1E8/ODz/N2oNBoObauovaAS8Db348 T1ew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=dHRqe8mc9mBnEA6oadi5Gsl+GBpxgJtgg/vKJ7NJPuM=; b=aZhlSSxln2LSoQaeowhNQS70OKndoSNa/xAkIH7B24b8QK66EjK9cYCLHCWTq8a1ZL N0lj52hti1SHXJYnkOB+ipX3ZvYb60Y9KWECh8zErSvquT20fq6cd9X4IQ+rE0FH0rDt 5xV6/DkD/fZiDTPuU4qwJnLhM7ZG/zZJuCyDMf6NvSFzOQkHmKTZD4h21vFBbMxOnwl9 bBs0dON1uYrX/dXibjCB78IyQIsUH3lwJO9Tu3M+3Y5QSafoUedDleJUWhzkg4WgRmzT 0VHnjTBSbuZE39TNsLU4erA/POFhwJ+5381O9dgBkKhtYAMsvjcLdhxyWZv+IrZ3se9n M6nA== X-Gm-Message-State: AMCzsaVEtzuYDyfanh3zVk+rd1fQunaUBse69ZayA5G67eKN0PGbcsEs BPuvAsMx+9SKe9Kl/47fW6+PXQ== X-Google-Smtp-Source: ABhQp+RtfF9t+lgFl/LXD5X+PcP/Um9g0vGSZ4CeCSoQPhwdPGUgj6VevpoRGqxOc9Ii88PLV+IclA== X-Received: by 10.223.163.11 with SMTP id c11mr6027366wrb.102.1509043060516; Thu, 26 Oct 2017 11:37:40 -0700 (PDT) Received: from dvbdev.wuest.de (ip-37-201-239-211.hsi13.unitymediagroup.de. [37.201.239.211]) by smtp.gmail.com with ESMTPSA id k130sm2610752wmg.12.2017.10.26.11.37.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 26 Oct 2017 11:37:39 -0700 (PDT) From: Daniel Scheller To: linux-media@vger.kernel.org, mchehab@kernel.org, mchehab@s-opensource.com Cc: jasmin@anw.at, rjkm@metzlerbros.de Subject: [PATCH v2] [media] dvb-frontends/stv0910: prevent consecutive mutex_unlock()'s Date: Thu, 26 Oct 2017 20:37:37 +0200 Message-Id: <20171026183737.10655-1-d.scheller.oss@gmail.com> X-Mailer: git-send-email 2.13.6 Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Daniel Scheller When calling gate_ctrl() with enable=0 if previously the mutex wasn't locked (ie. on enable=1 failure and subdrivers not handling this properly, or by otherwise badly behaving drivers), the i2c_lock could be unlocked consecutively which isn't allowed. Prevent this by checking the lock state, and actually call mutex_unlock() only when certain the lock is actually held. Signed-off-by: Daniel Scheller Tested-by: Jasmin Jessich Cc: Ralph Metzler --- Changes from v1 to v2: - use mutex_is_locked() instead of tracking the lock state in a separate variable - print message to KERN_DEBUG after a double unlock was prevented drivers/media/dvb-frontends/stv0910.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/drivers/media/dvb-frontends/stv0910.c b/drivers/media/dvb-frontends/stv0910.c index 73f6df0abbfe..5d0146782488 100644 --- a/drivers/media/dvb-frontends/stv0910.c +++ b/drivers/media/dvb-frontends/stv0910.c @@ -1240,8 +1240,17 @@ static int gate_ctrl(struct dvb_frontend *fe, int enable) if (write_reg(state, state->nr ? RSTV0910_P2_I2CRPT : RSTV0910_P1_I2CRPT, i2crpt) < 0) { - /* don't hold the I2C bus lock on failure */ - mutex_unlock(&state->base->i2c_lock); + /* + * don't hold the I2C bus lock on failure while preventing + * consecutive and disallowed calls to mutex_unlock() + */ + if (mutex_is_locked(&state->base->i2c_lock)) + mutex_unlock(&state->base->i2c_lock); + else + dev_dbg(&state->base->i2c->dev, + "%s(): prevented consecutive mutex_unlock\n", + __func__); + dev_err(&state->base->i2c->dev, "%s() write_reg failure (enable=%d)\n", __func__, enable); @@ -1250,8 +1259,15 @@ static int gate_ctrl(struct dvb_frontend *fe, int enable) state->i2crpt = i2crpt; - if (!enable) - mutex_unlock(&state->base->i2c_lock); + if (!enable) { + if (mutex_is_locked(&state->base->i2c_lock)) + mutex_unlock(&state->base->i2c_lock); + else + dev_dbg(&state->base->i2c->dev, + "%s(): prevented consecutive mutex_unlock\n", + __func__); + } + return 0; }