Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp3282502ybt; Mon, 22 Jun 2020 21:05:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxrtgGJEecKJEHPYNbVN68ntp/63Aj9y859eVxfOm41TPtbLzYFVJCOPF10Pxq58tE8N0n/ X-Received: by 2002:a17:906:560b:: with SMTP id f11mr19109179ejq.11.1592885123786; Mon, 22 Jun 2020 21:05:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592885123; cv=none; d=google.com; s=arc-20160816; b=eqs3uG6aWPVQk+rOdKoIONHnEM9H9PH7jfxcsVUX78Y9DMSoZvkA0y74cvvSybmCN1 qFWOWLz0v2b+IYcbkKCDSk0VLHP+HqcMq6HHgHjnK3tRapKQrTqozCd5HjR8H8crT4R3 K6iRmeQWSpEYbcaf8OvVLRENpEGlyiHxzkZDAET0bufJJ755foIUHGDeJL9+CC/KnrPK ZF24llawf4DAHa3BXd3LNdXKG/7W+l7ut145A5uChZiaenLOixvqGIzLMGoqBa+8g4WK qGaiBKX220tZVTXlDuLWDwjYqUsa/XzHkbA/2wtn4doJwncgmGxkmfd6sgOpHTkPMrru Wb4Q== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=p/RnkCoSxOcrfeG8nv0kDDgwKEpcPzECNRUCVLsijo0=; b=SbObG0FrOWLiUAvZMPwkI57OnTj4f61+Xmvd2I6O+WHxaR2trUbDu2JLGHV2CoT5GC 8njCiBUYBWl9JNHQjDsLr5FirIOuSUltIbA87ix4qsYpofrAP9GANozpWyACoWCMPN2F lGIQcbT56v/mZmpro2G0ehHR8fvbcj1Dwflnns14sMOwyIM0I2x1bg7Z+N1yRwRt+wha cFQ6G0IyIhilgWlc2hXoNQ/IZ31Lr2Xp/MeEABnlmo21Ep8Z12vlPsLZqjF1kXiStXHF Ae6lrQ7+n+buOxEEzGhjhTVy5vbylxHJS3Iasn9+aewpo+Su4HeRYAwGtnaSAt8ikB37 xGuQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="C//u3P1S"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o17si11033394edz.304.2020.06.22.21.05.01; Mon, 22 Jun 2020 21:05:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="C//u3P1S"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732183AbgFWECl (ORCPT + 99 others); Tue, 23 Jun 2020 00:02:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42218 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726429AbgFWECj (ORCPT ); Tue, 23 Jun 2020 00:02:39 -0400 Received: from mail-pf1-x441.google.com (mail-pf1-x441.google.com [IPv6:2607:f8b0:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3A66DC061573; Mon, 22 Jun 2020 21:02:39 -0700 (PDT) Received: by mail-pf1-x441.google.com with SMTP id p11so772580pff.11; Mon, 22 Jun 2020 21:02:39 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=p/RnkCoSxOcrfeG8nv0kDDgwKEpcPzECNRUCVLsijo0=; b=C//u3P1Spc6/1L9SIh/TwRbaXpyUzUVxDesN+Vt6guGDCCogN2Q9Q2zXKcQRGA48vt 7XYhTmLWFLK4A2VNL/nZG39K1Ri8I6h6PbHyt1fWPuA6CC3Ycd38sAWDukkFUMN8sE1t F+AsPf0FeqRXgNT9UaYCQNdEG5ODmlJVwp5/uNk91970wKEn1UM5iGwSobhdqzf2+AMM pDF+TryePR8YLS+Xi4HiBSniEuSynIDrbuG1Vcs42+v9cwGFBhHVqS+SpKHDTXJVePaQ 4fKDL3okt9hXe2Gk3YIR4Z9AhkNvuQPnYa/qjDeEMJpXyVg3us6adOglhaulRi15c6vt t8Bg== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=p/RnkCoSxOcrfeG8nv0kDDgwKEpcPzECNRUCVLsijo0=; b=SJzk0DZGKaPv3efD1tJg+6gpBObRiq5DtREgwfZc2Fg+WOHxSnWRmX2JPKmOPC61g/ gevQUJ6QJDLcWrxEQXNJsbGQDP8yBlGrFFPCMRdCfz5x38f2nyuntGyoQjQhKuAEwsFj zOwsscmDRLw9oGQXlamx1+NwrqfkXa18X6gQhHxo+lQF/ao7n8jN7dOeaJ2QTdOO+ADy VklOrmlGF2vJN7KGQ9feq2XIa2Z3OlceHD6cZahbqvY7xG/AquCXD+g3PQmhVdC+JCMt x0Imt7Ga66frFXolofnrg7YDaETCRETvJO3/GuEkFHUd9aBrRj5UgtumVVTlkdhqiQZk Q7Dg== X-Gm-Message-State: AOAM531zyjGFSNrQ57/mwMZQQNiUEqjmF5fSf+cXi7l3UVUDf013GSYw R0fZN04ngeCRmp2ribDTpS6/xYkA8p0= X-Received: by 2002:a05:6a00:7c8:: with SMTP id n8mr22516906pfu.116.1592884958383; Mon, 22 Jun 2020 21:02:38 -0700 (PDT) Received: from sol.lan (220-235-126-59.dyn.iinet.net.au. [220.235.126.59]) by smtp.gmail.com with ESMTPSA id n64sm12442315pga.38.2020.06.22.21.02.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jun 2020 21:02:37 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH 10/22] gpiolib: cdev: fix minor race in GET_LINEINFO_WATCH Date: Tue, 23 Jun 2020 12:00:55 +0800 Message-Id: <20200623040107.22270-11-warthog618@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200623040107.22270-1-warthog618@gmail.com> References: <20200623040107.22270-1-warthog618@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Merge separate usage of test_bit/set_bit into test_and_set_bit to remove the possibility of a race between the test and set. Similarly test_bit and clear_bit. In the existing code it is possible for two threads to race past the test_bit and then set or clear the watch bit, and neither return EBUSY. Signed-off-by: Kent Gibson --- drivers/gpio/gpiolib-cdev.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 5f5b715ed7f7..a727709b24a9 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -889,7 +889,7 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) hwgpio = gpio_chip_hwgpio(desc); - if (test_bit(hwgpio, gcdev->watched_lines)) + if (test_and_set_bit(hwgpio, gcdev->watched_lines)) return -EBUSY; gpio_desc_to_lineinfo(desc, &lineinfo); @@ -897,7 +897,6 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) return -EFAULT; - set_bit(hwgpio, gcdev->watched_lines); return 0; } else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) { if (copy_from_user(&offset, ip, sizeof(offset))) @@ -909,10 +908,9 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) hwgpio = gpio_chip_hwgpio(desc); - if (!test_bit(hwgpio, gcdev->watched_lines)) + if (!test_and_clear_bit(hwgpio, gcdev->watched_lines)) return -EBUSY; - clear_bit(hwgpio, gcdev->watched_lines); return 0; } return -EINVAL; -- 2.27.0