Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp4436188rdb; Mon, 11 Dec 2023 21:44:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IFeXv+RnBuPqN/vhmArJLNv66ILo1PdvKfsdsZpdl0FRpurZqMzVPvESBO9o+SIlsJIVsgj X-Received: by 2002:a05:6808:1489:b0:3b9:e687:d1b9 with SMTP id e9-20020a056808148900b003b9e687d1b9mr6973816oiw.36.1702359864920; Mon, 11 Dec 2023 21:44:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702359864; cv=none; d=google.com; s=arc-20160816; b=AN+3R4trYyGGV2qKiLjJ1G+NuPS+SQQG4PH/n/lEunlKGW7KrASITSArwkUkJpqE7Q tojEsM0h8QBSNPnTn0sFxG4hOdYDeLvqVM0QJ3kFQgaX8RZeExHGAmK7HSG5UtGAfYEd AUEwNQbCJb0mKVXJivz7Cgby2MywUw8WSSX0g6ciRWU2vCfSS8Hmr05S+z2xEaFw388n V1Iw+tj5C7kfiDstTiiA2qUFij0V/U1Zd9j6a01huDGBUc0GdVVQmJJ6m0Tj573rX5z0 sR/nNEWy/Ico/el/1PnkS/j1R9zLwb/8rPs1beYRtRPAlrnd9bvVsbibP5gIBYbPiBCB W2Jg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=5FHTk9soipYpHLOiG237jcWWN9G9Yj9KlCtcjJBsP8g=; fh=GNOA1jdeAuH+tfAEzFx15vIukVGFZnqrW9xHApUHjl0=; b=Njvnemk9N2yjhcxHMkz2PzGnjpzZvQCNpbxAiPsHJlvqkzXup7B5Ck0rkHcRT+puUY CI7yvBd5lKWIJ5QHBTG0396prfT02WZvnGsSo8GYYnwA0b5Tsu+Um0QHMRmg171oWtA+ 4K5yZt2ClEV1HjUWGUD8wF3bQphTUgt2dv4zU29qR8A6YBNcnVkOs9BowWGv2ipRR5/6 6+qqL+7gGR54kz4VYpuq6erEc+kir2skCwJb47LcqBXvUah90HpYDVCq6gm1EHZQ9lQF JNaOhhjm8IJr1XYAVQf3hWG6yhka2oHcNEAxlkR+eRuEa4Hv169GBOPQy07/NcLMLVhC +pQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Li0QMl4K; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id ck17-20020a056a02091100b005c668c65401si7548034pgb.480.2023.12.11.21.44.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 21:44:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Li0QMl4K; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id E48FF809FA6B; Mon, 11 Dec 2023 21:44:21 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230264AbjLLFns (ORCPT + 99 others); Tue, 12 Dec 2023 00:43:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50164 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230236AbjLLFnm (ORCPT ); Tue, 12 Dec 2023 00:43:42 -0500 Received: from mail-yw1-x112c.google.com (mail-yw1-x112c.google.com [IPv6:2607:f8b0:4864:20::112c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6E97EFD; Mon, 11 Dec 2023 21:43:42 -0800 (PST) Received: by mail-yw1-x112c.google.com with SMTP id 00721157ae682-5e1a2253045so7512287b3.2; Mon, 11 Dec 2023 21:43:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702359821; x=1702964621; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=5FHTk9soipYpHLOiG237jcWWN9G9Yj9KlCtcjJBsP8g=; b=Li0QMl4KLsU2cRm+n1o770GuzudtsX8Cba2xfgUkxCt5CcDlhcZ9DT4dU+fBW2Mg/j GDVFFk8fF04Zek9LeQQMNW8POWiV5fvLD2PAVeYabEvGaubUmGM5uDDjv8JDrQB+sFo3 zDe8yesplmy3fxjQ7NP6mGHPq8DjfK+j5VGkBj55TtvCKqlzTBN4nIhUsNLvbX36ByJf T9N4VMvBDvB9LtGqbVWd7g8+AG2TuEIcEdqZ4OpcruEs4aA/Qg1D4YjoATbPMHvNfmyr xbMv56RWAxu7cx7RfDIskiyUWmIAPGdwHbrEhDAGConu2ETbl4LOdCko/Tl6DPgkA5D9 ZLVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702359821; x=1702964621; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5FHTk9soipYpHLOiG237jcWWN9G9Yj9KlCtcjJBsP8g=; b=PST0Qf+6K46ze+bkNpqBNlSWzSSKf74GSiUGgKYeiE64rXsupbl4os9+CDLHrFvUsP SDTj+oXZoed4/DnF5ediOyoZX+cLxNzxTBLCQmsCiya61XZ9zjlmhzsTcjbVXR7+eFUf sgiGjUwtNuzTUg1Q4ynW6XAD3hBeXrcOclXnBe1p9IdXXlVJVd3BxVHoX28W9GG9iDk/ 9W8GzWaVJm46oSR+IrciG0BOV+Nuna2DH9pCONQ8O7X+aGzR/9m3VXM2qE67j00MzEZY webyN3uIDc/acpxiFfCHqEHw52x/Fjtmq63hpJ5vbGEfc7OqaQAiFRtN5brmdQfmqQDl 0kvg== X-Gm-Message-State: AOJu0Yxpd2Xul/VL0w1qTcquiuGHcbLWtzm0G8P2ktk0tsSkT23Hgh2x 4VuH3kcQarOTi+BsjjH0VhmrhAlpTHM= X-Received: by 2002:a0d:eb4a:0:b0:5de:8c10:e5a9 with SMTP id u71-20020a0deb4a000000b005de8c10e5a9mr4446367ywe.49.1702359821337; Mon, 11 Dec 2023 21:43:41 -0800 (PST) Received: from rigel.home.arpa (194-223-186-106.tpgi.com.au. [194.223.186.106]) by smtp.gmail.com with ESMTPSA id u13-20020a170902e5cd00b001d0be32b0basm7591836plf.217.2023.12.11.21.43.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 21:43:41 -0800 (PST) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, brgl@bgdev.pl, linus.walleij@linaro.org, andy@kernel.org Cc: Kent Gibson Subject: [PATCH 3/4] gpiolib: cdev: reduce locking in gpio_desc_to_lineinfo() Date: Tue, 12 Dec 2023 13:42:52 +0800 Message-Id: <20231212054253.50094-4-warthog618@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231212054253.50094-1-warthog618@gmail.com> References: <20231212054253.50094-1-warthog618@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Mon, 11 Dec 2023 21:44:22 -0800 (PST) Reduce the time holding the gpio_lock by snapshotting the desc flags, rather than testing them individually while holding the lock. Accept that the calculation of the used field is inherently racy, and only check the availabilty of the line from pinctrl if other checks pass, so avoiding the check for lines that are otherwise in use. Signed-off-by: Kent Gibson --- drivers/gpio/gpiolib-cdev.c | 66 ++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 34 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 7999c1a72cfa..37f2c9acc770 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -2398,22 +2398,12 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, struct gpio_v2_line_info *info) { struct gpio_chip *gc = desc->gdev->chip; - bool ok_for_pinctrl; - unsigned long flags; + unsigned long iflags, dflags; memset(info, 0, sizeof(*info)); info->offset = gpio_chip_hwgpio(desc); - /* - * This function takes a mutex so we must check this before taking - * the spinlock. - * - * FIXME: find a non-racy way to retrieve this information. Maybe a - * lock common to both frameworks? - */ - ok_for_pinctrl = pinctrl_gpio_can_use_line(gc, info->offset); - - spin_lock_irqsave(&gpio_lock, flags); + spin_lock_irqsave(&gpio_lock, iflags); if (desc->name) strscpy(info->name, desc->name, sizeof(info->name)); @@ -2421,51 +2411,59 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, if (desc->label) strscpy(info->consumer, desc->label, sizeof(info->consumer)); + dflags = READ_ONCE(desc->flags); + + spin_unlock_irqrestore(&gpio_lock, iflags); + /* - * Userspace only need to know that the kernel is using this GPIO so - * it can't use it. + * Userspace only need know that the kernel is using this GPIO so it + * can't use it. + * The calculation of the used flag is slightly racy, as it may read + * desc, gc and pinctrl state without a lock covering all three at + * once. Worst case if the line is in transition and the calculation + * is incorrect then it looks to the user like they performed the read + * on the wrong side of the transition - but that can always happen. + * The definitive test that a line is available to userspace is to + * request it. */ - info->flags = 0; - if (test_bit(FLAG_REQUESTED, &desc->flags) || - test_bit(FLAG_IS_HOGGED, &desc->flags) || - test_bit(FLAG_USED_AS_IRQ, &desc->flags) || - test_bit(FLAG_EXPORT, &desc->flags) || - test_bit(FLAG_SYSFS, &desc->flags) || + if (test_bit(FLAG_REQUESTED, &dflags) || + test_bit(FLAG_IS_HOGGED, &dflags) || + test_bit(FLAG_USED_AS_IRQ, &dflags) || + test_bit(FLAG_EXPORT, &dflags) || + test_bit(FLAG_SYSFS, &dflags) || !gpiochip_line_is_valid(gc, info->offset) || - !ok_for_pinctrl) + !pinctrl_gpio_can_use_line(gc, info->offset)) info->flags |= GPIO_V2_LINE_FLAG_USED; - if (test_bit(FLAG_IS_OUT, &desc->flags)) + if (test_bit(FLAG_IS_OUT, &dflags)) info->flags |= GPIO_V2_LINE_FLAG_OUTPUT; else info->flags |= GPIO_V2_LINE_FLAG_INPUT; - if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) + if (test_bit(FLAG_ACTIVE_LOW, &dflags)) info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW; - if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) + if (test_bit(FLAG_OPEN_DRAIN, &dflags)) info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN; - if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) + if (test_bit(FLAG_OPEN_SOURCE, &dflags)) info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE; - if (test_bit(FLAG_BIAS_DISABLE, &desc->flags)) + if (test_bit(FLAG_BIAS_DISABLE, &dflags)) info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED; - if (test_bit(FLAG_PULL_DOWN, &desc->flags)) + if (test_bit(FLAG_PULL_DOWN, &dflags)) info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN; - if (test_bit(FLAG_PULL_UP, &desc->flags)) + if (test_bit(FLAG_PULL_UP, &dflags)) info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP; - if (test_bit(FLAG_EDGE_RISING, &desc->flags)) + if (test_bit(FLAG_EDGE_RISING, &dflags)) info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING; - if (test_bit(FLAG_EDGE_FALLING, &desc->flags)) + if (test_bit(FLAG_EDGE_FALLING, &dflags)) info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING; - if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &desc->flags)) + if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &dflags)) info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME; - else if (test_bit(FLAG_EVENT_CLOCK_HTE, &desc->flags)) + else if (test_bit(FLAG_EVENT_CLOCK_HTE, &dflags)) info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE; - - spin_unlock_irqrestore(&gpio_lock, flags); } struct gpio_chardev_data { -- 2.39.2