Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp226831ybt; Tue, 7 Jul 2020 21:21:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz0cAHb/A01RK+CyQnqhyEEj7TvnXeSGrQMrtfEoUAp2M+abqWBMBrgLTqGsnLGKoZu1QrK X-Received: by 2002:a05:6402:180a:: with SMTP id g10mr53583013edy.152.1594182071822; Tue, 07 Jul 2020 21:21:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594182071; cv=none; d=google.com; s=arc-20160816; b=N13CoPDCAZ/b/VwWh2jOV109uHRx6RX4GYoM5ClyPgPqSy8Z78WevCGzlu0l84cRbf G9D6Af4ZpTlh4x4HxKNK+r0eQkEgBj40m/urCmy+7XzQTsOjeEepW4Aj2DS4uk+VZQoh cJNNouZJHN3NUoN0trUzV/u5tMMXaAhHEjqakAewBEyi05NL4xmw7W9Tp4uobPsqb+1r 7OSx2WXZEj7KwXjz22IZ6pWKbpZ0TzesaGB/GJYVEnkaet9b+KtPOfywdP2eN2nXtcdu mPpVeFDvE/nJhYboVcaZmlYyaJmRDiGOp7B3oh0q76axw1spSK3HmlBUwt5ct7Nhca/V ikQQ== 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=0FCq6Jagl5VFciT39ywBKkxFPJGQbt40FgsHop4dXwM=; b=sz41ROFkgb30YuLdIQvX5E+T4BM6dHDZ8EeeCvMPRME02SQGOHmZdIbZHZUCRmFss8 bl7gK37jyOTvKE8vFoyuxST2mNL4vUfpTnwrfAnvD84iMbwGOHJJgezpl0cRzw6U7GED 061u6oIQNFneoezYQSYUW+o0zXQPNk88P0qrF3s7Gl95pG6DvRNdnomdTvcx+ksIZ8bN 3ScBTy56ieTc7RkAlhOgzkDpNxMvwwFw7uuBVjUFAUzT456gKRkaID8c8qXyRcxJl5ho LtSCv1Q2A77n08oT5Bh8FUzVj4aSCxBtju/FahdRRblHaA+McCbwpBMZdUl0MDe8tuKq ZeAw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=h0m7TxVh; 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 h1si14133618ejk.3.2020.07.07.21.20.49; Tue, 07 Jul 2020 21:21:11 -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=h0m7TxVh; 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 S1729428AbgGHET5 (ORCPT + 99 others); Wed, 8 Jul 2020 00:19:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725446AbgGHET4 (ORCPT ); Wed, 8 Jul 2020 00:19:56 -0400 Received: from mail-pj1-x1044.google.com (mail-pj1-x1044.google.com [IPv6:2607:f8b0:4864:20::1044]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C3D51C061755; Tue, 7 Jul 2020 21:19:56 -0700 (PDT) Received: by mail-pj1-x1044.google.com with SMTP id gc9so615979pjb.2; Tue, 07 Jul 2020 21:19:56 -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=0FCq6Jagl5VFciT39ywBKkxFPJGQbt40FgsHop4dXwM=; b=h0m7TxVhBMRwJfUj1x0NGxsSreUshm90Hhmtciq8BbsVAEKQV8O8Q9DVK6wDjSGQHE 9IMAELdWUMbfca602EjFbZkcgfBc1ItxTi9kiuGVVVYLumWzH8AmQJt9RyAx7Z1hOJq9 vjufK5lQkZTM/Ie2cvG8RyYfdcN4849NaM5ZpArHRduHJYanbujNT2u7JwsArUKfZUw/ uEZWX6fXttkIwjnnGe6rok2l/8cBwMJqZjR01XXK6Tn/Tmb63nKazIEm7qdabVHy6zaS yEc3OUI/QcI/kJGs4gwkir6tVGviwmitvXZ9YsOlv4aXJO2fmEMZY/tJm9GjllFXhNW7 V3Sg== 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=0FCq6Jagl5VFciT39ywBKkxFPJGQbt40FgsHop4dXwM=; b=Awcro8C67BbFodpCSiB47g2SkmfgchYnyXWHsPD1FIl2U2Iz1FCO1X+uI+MTShJgLO KQngCmYhkXYXWfIQUik0vOtHOp+dz3zcp39fgHQYwFCShleexgjO4RoQwtpzJ52hBIin c3CLJKwNOrpsSLSxqkQaq5U/SrBb7WkM8Sc3YcdTnAnhZpBbrDO5DTI36yTxVrjhemkh jkLi9pISlwh1BARJb/t71KXTIb7oSWn6r/iG6dXbFHMWhkBKXNrIPNMAYKdBIC8x0VHG VhHCoP3HSkEekPnrjjrGLZFnqjKNKnFtaDbuEi3VE1RCVW9qtFYUB9bitGXZb+5UIFSy yldw== X-Gm-Message-State: AOAM530FAz53UpoHWDMSYw2sdJ8Uqq6QnCZQpnYI0eJPB9tqHeJp4KUN /tQyq3JHTR6L0XF3dbMC03VbmhjD X-Received: by 2002:a17:902:b414:: with SMTP id x20mr36732197plr.143.1594181995676; Tue, 07 Jul 2020 21:19:55 -0700 (PDT) Received: from sol.lan (106-69-191-222.dyn.iinet.net.au. [106.69.191.222]) by smtp.gmail.com with ESMTPSA id gx23sm3821951pjb.39.2020.07.07.21.19.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jul 2020 21:19:54 -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/17] gpiolib: cdev: fix minor race in GET_LINEINFO_WATCH Date: Wed, 8 Jul 2020 12:15:53 +0800 Message-Id: <20200708041600.768775-11-warthog618@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200708041600.768775-1-warthog618@gmail.com> References: <20200708041600.768775-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 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index fe1b385deecc..b2b26dc25051 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -887,15 +887,16 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) hwgpio = gpio_chip_hwgpio(desc); - if (test_bit(hwgpio, cdev->watched_lines)) + if (test_and_set_bit(hwgpio, cdev->watched_lines)) return -EBUSY; gpio_desc_to_lineinfo(desc, &lineinfo); - if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) + if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { + clear_bit(hwgpio, cdev->watched_lines); return -EFAULT; + } - set_bit(hwgpio, cdev->watched_lines); return 0; } else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) { if (copy_from_user(&offset, ip, sizeof(offset))) @@ -907,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, cdev->watched_lines)) + if (!test_and_clear_bit(hwgpio, cdev->watched_lines)) return -EBUSY; - clear_bit(hwgpio, cdev->watched_lines); return 0; } return -EINVAL; -- 2.27.0