From patchwork Wed Jan 10 20:32:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 761597 Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CB55B4F200 for ; Wed, 10 Jan 2024 20:32:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="Vs9z/HE+" Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-40e43fb2659so41534015e9.1 for ; Wed, 10 Jan 2024 12:32:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1704918750; x=1705523550; 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=S4/ezTEXd/AGBE5ik3GQsGbwKaC7ixzvP0QmPnIVxHs=; b=Vs9z/HE+Iml7oygN16KpVcLUD5nh3xPNOH4zCK/oqxz+gbqVU9g1d2F/JZLszjYYet xiPC99nhFAcUDyq5M0+3zmH3LbTUDGkaZZPukzeWY+vcLskJxvC5b/YfX+3NXqOKLFBS JHk3jvW/XGam28Fz6YY0V9GCah8zB4QDBgCI/gqaBvwtlqVtgCAU3i8LinYCIBtdP3Gy L22NFuyW6CPCi/0mOwH92amTrcjiqQUq2IBzFxVQVpfn1yVrRJ9SsDPIcGThMI13qZZN +3ZWz6xNuz7N3ctV8xbOeAGJiL2nMLXBQbUpvjOLFxjYwvy08qt8BcYIwwWkqmOl5o4U zxwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704918750; x=1705523550; 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=S4/ezTEXd/AGBE5ik3GQsGbwKaC7ixzvP0QmPnIVxHs=; b=ef0rUXrNJr4xOdl+il+1lj3gxr2C+X2p3R0dMtiZEhV4L//QY2SLptffEgDPMTrcT3 mM+uxm1ylk1jBrqPmMFq1xgegvlbdI2adPX69ntnDA577ybWk06v86H5WeQGm74L3m9k uhc9AQWo0fkwsww+s2bTmdDF9LALA80tUiguL0BMS6MA0tRLBnVXSeQDGoSvtCfWTdun KuiEm2ySmSPqh+yFjr3Ud0SeF6MEBl05/GphVtSFslw6pr1a8iGBA31a7wizYHQokV8A vPB84iqKcGq34TSGYzB2+0YFn2tpny27xcm7dZyd3VV2HSqilTrw4ILH7tJuna8EaNpo /MBw== X-Gm-Message-State: AOJu0Yyr/0hqp3HHzEd3dOzH/Vv/gwkJ6IOo6LbDlezW8QlD1HDxxrVh V1Eoz5FydUdtZnCzE8kMCXAhTdN7RWfkYg== X-Google-Smtp-Source: AGHT+IG+Jh12VyGYmoVwBM+LMkw8YhuNMET4WndcS7w4Xu8ew7js3y1dau1a7gKopoa/qzRbjPgQ4w== X-Received: by 2002:a05:6000:372:b0:337:4758:43ef with SMTP id f18-20020a056000037200b00337475843efmr29206wrf.66.1704918750199; Wed, 10 Jan 2024 12:32:30 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:3342:1411:3dd8:cb70]) by smtp.gmail.com with ESMTPSA id z17-20020a5d44d1000000b003377e22ffdcsm2072172wrr.85.2024.01.10.12.32.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 12:32:29 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Boqun Feng Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH 1/3] cleanup: provide DEFINE_LOCK_GUARD_ARGS() Date: Wed, 10 Jan 2024 21:32:13 +0100 Message-Id: <20240110203215.36396-2-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240110203215.36396-1-brgl@bgdev.pl> References: <20240110203215.36396-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bartosz Golaszewski This macro allows defining lock guard with additional arguments that can be passed to the locking function. This is useful for implementing guards for nested locking. Signed-off-by: Bartosz Golaszewski --- include/linux/cleanup.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h index c2d09bc4f976..921db45023bb 100644 --- a/include/linux/cleanup.h +++ b/include/linux/cleanup.h @@ -246,5 +246,11 @@ __DEFINE_LOCK_GUARD_0(_name, _lock) static inline void * class_##_name##_ext##_lock_ptr(class_##_name##_t *_T) \ { return class_##_name##_lock_ptr(_T); } +/* + * Helper for implementing guard locks with additional arguments passed to + * the locking function. + */ +#define DEFINE_LOCK_GUARD_ARGS(_name, _type, _lock, _unlock, _args...) \ +DEFINE_CLASS(_name, _type, _unlock, ({ _lock; _T; }), _type _T, _args) #endif /* __LINUX_GUARDS_H */ From patchwork Wed Jan 10 20:32:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 761596 Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 164EC4F204 for ; Wed, 10 Jan 2024 20:32:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="bcCeQTBW" Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-40e5508ecb9so18306095e9.3 for ; Wed, 10 Jan 2024 12:32:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1704918751; x=1705523551; 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=jEdTJOXkqBku2qR0TKwyio+JuDhgwS/A5uq4+eCSKZQ=; b=bcCeQTBWfoKOlwG0FkwOzeULj1ZofP2hzqB7URHPeBIIsqPM5GO5XT37K125t9WWZl FCReJEQzF2BKe4bSGiZfXfq7dW2mU/q3SfCD2zGXDvs9VlzJraJG4Ft2DuilwEDnwzn/ j9g7NlzP2VDiCwT4tVl1NFgrvm7rHYJVsnMey0gJavvWgLt3yCcHEn4rPFU0i0FVYI3k rKcSmJrCZSrhS+TBER8HMpSMjDwz3TQuvln4IJee/sTmn71r4Vz5ZpGwkKt+Dc8+l2kY hysjRa0BH1KxRz9Vr6fbv0yWzbExCbYc0aodi1PugV1B6NvzVOw/tPNBxBs6USpSS2HI dcqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704918751; x=1705523551; 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=jEdTJOXkqBku2qR0TKwyio+JuDhgwS/A5uq4+eCSKZQ=; b=t+fSyTd7AKBzX9zXyYf1ZFpsxdb2g+Lp0ur5dOJP7LM3Paa5Lqgc612gfLWRg3jd8N ui+QacNDiGou/1MGAiftT+hmfohfpFsFGqctsuxPPkyf7/XVlKi1sGkt3Q+BwDG5aY5k C/jegtHTowVBygP9M39Poqv6ttlIYtRVNK0+iPVTg8eB0AHi1bgXEL1dzfJu75R3SIaw m/9iilERgDAzZRihaQr7jBkTr7XyXLQZfvQ6Sdew+xFGf2cSQrruQlG0yYE6FgOZTn8g Or6qsa3c1LlII13jqMErBX7evqEzg6MWRd8rcgwQggGegytIqB+c7JZw97gYFxqNrLik QDsg== X-Gm-Message-State: AOJu0YySHx4JfP0uOuIVVeC9cVlAQIqhRV3On+hgaTslRqSdeF3RZdDN FFFk8fMN2J2wzP0Ap0pyskuxYub+dJ+D5g== X-Google-Smtp-Source: AGHT+IGOWnDjmizlbioxwpOowdqUvcfTZHld35bp2LkmBUxDT+Htv/cWEoB1XeBN+FcoMgHgbEqcWg== X-Received: by 2002:a05:600c:6c7:b0:40d:9369:697f with SMTP id b7-20020a05600c06c700b0040d9369697fmr904262wmn.144.1704918751227; Wed, 10 Jan 2024 12:32:31 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:3342:1411:3dd8:cb70]) by smtp.gmail.com with ESMTPSA id z17-20020a5d44d1000000b003377e22ffdcsm2072172wrr.85.2024.01.10.12.32.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 12:32:30 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Boqun Feng Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH 2/3] locking/rwsem: provide a lock guard for down_read_nested() Date: Wed, 10 Jan 2024 21:32:14 +0100 Message-Id: <20240110203215.36396-3-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240110203215.36396-1-brgl@bgdev.pl> References: <20240110203215.36396-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bartosz Golaszewski This adds a lock guard for taking an RW semaphore for reading in nested context. It takes the nesting depth as a second argument. Signed-off-by: Bartosz Golaszewski --- include/linux/rwsem.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/include/linux/rwsem.h b/include/linux/rwsem.h index 9c29689ff505..298f5e60d30c 100644 --- a/include/linux/rwsem.h +++ b/include/linux/rwsem.h @@ -258,4 +258,10 @@ extern void up_read_non_owner(struct rw_semaphore *sem); # define up_read_non_owner(sem) up_read(sem) #endif +DEFINE_LOCK_GUARD_ARGS(rwsem_read_nested, + struct rw_semaphore *, + down_read_nested(_T, subclass), + up_read(_T), + int subclass); + #endif /* _LINUX_RWSEM_H */ From patchwork Wed Jan 10 20:32:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 762045 Received: from mail-wr1-f44.google.com (mail-wr1-f44.google.com [209.85.221.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E25724EB42 for ; Wed, 10 Jan 2024 20:32:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="xbMqzXT6" Received: by mail-wr1-f44.google.com with SMTP id ffacd0b85a97d-3376ead25e1so2966219f8f.3 for ; Wed, 10 Jan 2024 12:32:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1704918752; x=1705523552; 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=xH5jTr5aU0xfloUoZ4e4ojWO1uQm4oQS8ylJSBoL6uQ=; b=xbMqzXT6stNQwOSGEF4TKGCpRnQOd6QT1tOh6Ce9t9voyBgxAQbeqB/S8wJXSGRDJS RB/em+pZk4qULwl1Fuq6n2XPEo6DeZkIxYedQsL3gitV6Q/e9kdutei0vih14x4YRLyI lkVdN3YA55zyo02h/u1LhgswVYQpfStxRXARRys85opSjTU28T7M2aCq/LltUDH4LbGk 82h42UHGy/8OFOS/IJz3LNwYcda4w15NUaxnsvWAfSiQxZSIQXzLMEoiuwY/uAAcENTg n23XxLg7GqiirQ5qARDff/GNkFWT0byJ1N8BxyVhTpc7jr/v6E4Izgf/S7Mw3ktPbcLW KmHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704918752; x=1705523552; 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=xH5jTr5aU0xfloUoZ4e4ojWO1uQm4oQS8ylJSBoL6uQ=; b=C7b2DinnzYJM0XRCJJfEIuQWXKa6gfXsRCDIeg6Ou8179SMipkdWpFl83rXob5eclD otqL690oagETZxyu4F9Khp8zTm7HERmMwNL8Wukl2K2gg1FShNO4Ym634XvtJTBzb53d ToKaysOU1jnpBegcs/rvsPX99TnYQvs5r6Gkttxdu3i3SqCa3MCFZZ9JUgsF7HHT6Ute mrkL2YRuFwBDmvI6Hyu0AEnHd88J8NwHLjAl6h4a/rbH8qPcU+eSaJvCA9f1KKsTA8bQ oOz5GORp9nZaBEhMDKQNl8IBHdPCjSCcTdPrw6mAQTZWdJNM3sRYlcm+XZIQuGtqwN43 bq0Q== X-Gm-Message-State: AOJu0Yxhylt8hID6jOnuWLaJkDtDuD8Z0izUiNsDxB0Phx/CMpRwpVt1 6xspJGZ7h5OCR3RYHfcomzOpY5Pm4eRaPA== X-Google-Smtp-Source: AGHT+IHuqat0eKssBubXT+iurOkhZRe56orJMHJGefWABPD4z8mSZqUu5SuIAslg9U0SLC0yT9YsNg== X-Received: by 2002:a5d:4f03:0:b0:336:7f45:bc0f with SMTP id c3-20020a5d4f03000000b003367f45bc0fmr29434wru.46.1704918752208; Wed, 10 Jan 2024 12:32:32 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:3342:1411:3dd8:cb70]) by smtp.gmail.com with ESMTPSA id z17-20020a5d44d1000000b003377e22ffdcsm2072172wrr.85.2024.01.10.12.32.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 12:32:31 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Boqun Feng Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH 3/3] gpiolib: pin GPIO devices in place during descriptor lookup Date: Wed, 10 Jan 2024 21:32:15 +0100 Message-Id: <20240110203215.36396-4-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240110203215.36396-1-brgl@bgdev.pl> References: <20240110203215.36396-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bartosz Golaszewski There's time between when we locate the relevant descriptor during lookup and when we actually take the reference to its parent GPIO device where - if the GPIO device in question is removed - we'll end up with a dangling pointer to freed memory. Make sure devices cannot be removed until we hold a new reference to the device. In order to not overcomplicate the code with locking and unlocking variants of the same functions, just use the nesting feature of RW semaphores in gpio_device_find(). Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib.c | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 4c93cf73a826..972f33bc1a72 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -1114,7 +1114,7 @@ struct gpio_device *gpio_device_find(void *data, */ might_sleep(); - guard(rwsem_read)(&gpio_devices_sem); + guard(rwsem_read_nested)(&gpio_devices_sem, SINGLE_DEPTH_NESTING); list_for_each_entry(gdev, &gpio_devices, list) { if (gdev->chip && match(gdev->chip, data)) @@ -4134,27 +4134,33 @@ static struct gpio_desc *gpiod_find_and_request(struct device *consumer, struct gpio_desc *desc; int ret; - desc = gpiod_find_by_fwnode(fwnode, consumer, con_id, idx, &flags, &lookupflags); - if (gpiod_not_found(desc) && platform_lookup_allowed) { + scoped_guard(rwsem_read, &gpio_devices_sem) { + desc = gpiod_find_by_fwnode(fwnode, consumer, con_id, idx, + &flags, &lookupflags); + if (gpiod_not_found(desc) && platform_lookup_allowed) { + /* + * Either we are not using DT or ACPI, or their lookup + * did not return a result. In that case, use platform + * lookup as a fallback. + */ + dev_dbg(consumer, + "using lookup tables for GPIO lookup\n"); + desc = gpiod_find(consumer, con_id, idx, &lookupflags); + } + + if (IS_ERR(desc)) { + dev_dbg(consumer, "No GPIO consumer %s found\n", + con_id); + return desc; + } + /* - * Either we are not using DT or ACPI, or their lookup did not - * return a result. In that case, use platform lookup as a - * fallback. + * If a connection label was passed use that, else attempt to + * use the device name as label */ - dev_dbg(consumer, "using lookup tables for GPIO lookup\n"); - desc = gpiod_find(consumer, con_id, idx, &lookupflags); + ret = gpiod_request(desc, label); } - if (IS_ERR(desc)) { - dev_dbg(consumer, "No GPIO consumer %s found\n", con_id); - return desc; - } - - /* - * If a connection label was passed use that, else attempt to use - * the device name as label - */ - ret = gpiod_request(desc, label); if (ret) { if (!(ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE)) return ERR_PTR(ret);