From patchwork Tue Jan 2 15:59:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 184398 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:6f82:b0:100:9c79:88ff with SMTP id tb2csp4536398dyb; Tue, 2 Jan 2024 08:10:22 -0800 (PST) X-Google-Smtp-Source: AGHT+IGCbEFlXuqnGyXvuiAOWjJXnY7jHg3eAHwTzraC89XeyAUJ3MU/ecIOUmIbPBs+2uSh9nsg X-Received: by 2002:a05:6e02:1989:b0:35f:deea:1c86 with SMTP id g9-20020a056e02198900b0035fdeea1c86mr32675067ilf.3.1704211822276; Tue, 02 Jan 2024 08:10:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704211822; cv=none; d=google.com; s=arc-20160816; b=OxzBYhmDg3M8g0V3ryMcM9cEbnpYqxiK5n3+0SUbmqheIIuGtpo2s5DYlb0L2E6K9U hv8u1bw8tNWuVquCzwhWI3qBJWz5AfSQ6hZT5il3LN99zt7ZJB8dEaxG6wrTIODMAQ6m HKiImNa5sF41S6mwx3k52IYJtj5PYOYXfXSbKKn3R4zzEWTn9ply5PEpq/Qg9xCI3e0k t9pLDdVEIntbGlnG15gY38xYTOWXiXNqbE/AljL8DAwQW8aKzwy4X2/BVlUIM6DyVRx4 5jJiiJwJzf8mfZYkQk/N0P4yqH/zORxgxIV4blgSp9cupkBbGdsj4PNJyCitSULhwGHz Is7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=qgJ68qs44Lcqe8TCw142wLnFP4mWeBCuDvsZcDC2Hj8=; fh=kmduAntvCxYKWJxf0o5GnU6TlPpo49/gb3O/sJ4FX9Y=; b=S3Se2tQbvs2uFxVcAUMf/H8iFSp+dDyhi01A6OmiPT7NPlCDbTlfHVWFn1t8Gwwf43 tXOxwLbJcyD+FNmhJHPGUEhRjKDHv3HxHju+NhNpAoWdbz/z/PAnv6eEXBIflALwkj/s AS/8230dt6EzPFI7Z/K59NiwatAbY7Ojo6b3b6u6K0nWpIhzi3uhkgGDGxu3yJMYVvQc 2JFI+DwyxIdNQLQI/2yQAF+kb5ilzl2ab5DXMiouFomWMysMR/JxQtHYHPoVyZcQbT7z ccpG1BmmhkSy+s+HLzEuiGd9LvZqjKDQ+DV9lL45fBQ2wMySg3lVm0MnjQK5c48W3Kf2 p2rg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bgdev-pl.20230601.gappssmtp.com header.s=20230601 header.b="I/F8dAhF"; spf=pass (google.com: domain of linux-kernel+bounces-14576-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14576-ouuuleilei=gmail.com@vger.kernel.org" Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id bs123-20020a632881000000b005c688ef7549si20316872pgb.98.2024.01.02.08.10.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 08:10:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-14576-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@bgdev-pl.20230601.gappssmtp.com header.s=20230601 header.b="I/F8dAhF"; spf=pass (google.com: domain of linux-kernel+bounces-14576-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14576-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 57FF528411B for ; Tue, 2 Jan 2024 16:00:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1DECF15491; Tue, 2 Jan 2024 16:00:00 +0000 (UTC) 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="I/F8dAhF" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (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 E242014AB1 for ; Tue, 2 Jan 2024 15:59:55 +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 Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-40d5aefcc2fso55870685e9.0 for ; Tue, 02 Jan 2024 07:59:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1704211194; x=1704815994; 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=qgJ68qs44Lcqe8TCw142wLnFP4mWeBCuDvsZcDC2Hj8=; b=I/F8dAhFXk0gDICtKIkqAqTnOg96FpLU0X46Glli0PXSC2kBkyfsfY1S5YqNULZXPI yPPQ57I2EQom14KzZ+ffs/HGpgeHfuEnDbP4wR8D4udgWEYefmk+4i2UBs2wjXgzjgnA /UnINcjnZdYKpb11EhtDot1WGNtDhANBfXT4eblPeC8QCmhf1o7r0BVOr787RqTU3k2z d8sJEfrmlrwxIR1Af6XCD76n5XYmf046A3X/vXhy0x8iG/amMqQVMiWGlaUO2XUa4rN9 +16AIIattoOlPqoSlGKGiHViiq4rMlRVD+cgiKxIwhOA8G3Gmcoqf1m6VLPQRTld2OOX TWJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704211194; x=1704815994; 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=qgJ68qs44Lcqe8TCw142wLnFP4mWeBCuDvsZcDC2Hj8=; b=VKjGfm9ro9PPZp3x2rWL2Sz3vGr1kRyPvJivgJ8+9SHMt3vemKkkiib1tsfVD5xaMh IMCoEn8lHL+nCeHJXTV1sCDtaDcbtv13FZUIhNrApHCm/5IrffuvmEatweNcSzBhzqX7 jkaD6BfdFUcK1nQf4NthNcUk4yUSd7bnBzvp18cejUjKME2sGHhm2x+997HrfuDvWdVz 9BS9xrBV0YEUbG9Hah0osyiT398t0NO6NXZFW5za8Kzibt3m7azB+0HX4R+Xbr0dorcc peuMSRtqRPwkbH1fhV6Gn08eUr3H/jFznELjC79b8qMkmRwIuilQToyz1u9E59klNVPm fvqA== X-Gm-Message-State: AOJu0YxLxK+Ejs1JGHKTaesfIhp5OmBkCXrgLq5OQa/b3bsTiLOHpFtP kzqC6/25z/lWmQh/q033bG15a21iCpkdFCDn4vgmydYFUrk= X-Received: by 2002:a05:600c:2a8d:b0:40c:610e:3c69 with SMTP id x13-20020a05600c2a8d00b0040c610e3c69mr9638192wmd.36.1704211194128; Tue, 02 Jan 2024 07:59:54 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:6f51:fa47:f00a:a53c]) by smtp.gmail.com with ESMTPSA id u14-20020a05600c138e00b0040d62f97e3csm20556641wmf.10.2024.01.02.07.59.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 07:59:53 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 1/3] gpiolib: remove the GPIO device from the list when it's unregistered Date: Tue, 2 Jan 2024 16:59:47 +0100 Message-Id: <20240102155949.73434-2-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240102155949.73434-1-brgl@bgdev.pl> References: <20240102155949.73434-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1786995615856463809 X-GMAIL-MSGID: 1786995615856463809 From: Bartosz Golaszewski If we wait until the GPIO device's .release() callback gets invoked before we remove it from the global device list, then we risk that someone will look it up using gpio_device_find() between where we dropped the last reference and before .release() is done taking a reference again to an object that's being released. The device must be removed when it's being unregistered - just like how we remove it from the GPIO bus. Fixes: ff2b13592299 ("gpio: make the gpiochip a real device") Signed-off-by: Bartosz Golaszewski Reviewed-by: Linus Walleij --- drivers/gpio/gpiolib.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index e21497b989a1..e019c4243809 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -651,9 +651,6 @@ static void gpiodev_release(struct device *dev) { struct gpio_device *gdev = to_gpio_device(dev); - scoped_guard(mutex, &gpio_devices_lock) - list_del(&gdev->list); - ida_free(&gpio_ida, gdev->id); kfree_const(gdev->label); kfree(gdev->descs); @@ -1068,6 +1065,9 @@ void gpiochip_remove(struct gpio_chip *gc) dev_crit(&gdev->dev, "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n"); + scoped_guard(mutex, &gpio_devices_lock) + list_del(&gdev->list); + /* * The gpiochip side puts its use of the device to rest here: * if there are no userspace clients, the chardev and device will From patchwork Tue Jan 2 15:59:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 184394 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:6f82:b0:100:9c79:88ff with SMTP id tb2csp4529209dyb; Tue, 2 Jan 2024 08:00:51 -0800 (PST) X-Google-Smtp-Source: AGHT+IE4CZeXovEfgZiP/y7JF76J7hYCO1vbEYQCazsVqGbgxeGxeuhpXkZob77wt5TgA+vLIDQp X-Received: by 2002:a05:6871:2b17:b0:1fb:75a:6d41 with SMTP id dr23-20020a0568712b1700b001fb075a6d41mr19786841oac.104.1704211251238; Tue, 02 Jan 2024 08:00:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704211251; cv=none; d=google.com; s=arc-20160816; b=JJFwxvU3RFZvCftkPaIXpAQsvFgTG+rRdX1rOIm7b01g+0g7RUYAqz/YoRohDIRvGY 765zBtaVIuDITD8LJt97jWVbo/kGwYxTHg2jiIydb//AIE0qMTy8nPnTq6btKCPinpc5 D8fIZQ9cnQkOSo7ZCB6U56oNWEV6NHacVsfELHlbvzms479MgOhL9v/yhZ2OAVyk2d8V Ey4yz1KyeU2N++hre3Z39Xe00LWyuT8SM02xj+Auj04HwJ7YO+8G96T1niHQhG8XVEX5 DEO7ZDpyTm6ErkmVZtjkNYh9pAaZkoIObGGdKrHqVSsIucu8Sf2WGZZTZiJOqShmpT8A KBgQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=g3wf/pQ/AJWgbz0DcSp/EQGOfmOL52HnCJ3+CUimtjU=; fh=kmduAntvCxYKWJxf0o5GnU6TlPpo49/gb3O/sJ4FX9Y=; b=qKhlypWMN8B6ksjJTZD1STD1Am+Yw1yUGTDJKYUnFqM/rg7ygJHVIDioo33FINeeoX pTetEkbbYJ7NrXf+GiEP1Ok8JKGlV/bN9d+CCCYFB7xxIEE0K99d4ZAhfWNuYh+R1Ger nEyLH6Exrh1PL/iAQW6eJmUghEZJCv0v9gTwzZoftQd9L7P+SfznVFmje/ssf+IdYgKL toGxbVQzjIvXfEwfjQeXQqXMBrf0EGQfWiD0MTM/gHX7AzO6/DJO+UqOBJ3INfs5asUh LJtnQ6v7z56Y2uNMTQs1FP3p13dW2WFWXEjF79tgLXxV0L+17VT+R4GjMZwKtRZO6JYA mCyA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bgdev-pl.20230601.gappssmtp.com header.s=20230601 header.b=PduPRUQs; spf=pass (google.com: domain of linux-kernel+bounces-14577-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14577-ouuuleilei=gmail.com@vger.kernel.org" Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id b26-20020a05620a089a00b0078154cbc43dsi15870218qka.652.2024.01.02.08.00.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 08:00:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-14577-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@bgdev-pl.20230601.gappssmtp.com header.s=20230601 header.b=PduPRUQs; spf=pass (google.com: domain of linux-kernel+bounces-14577-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14577-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 08D881C224FE for ; Tue, 2 Jan 2024 16:00:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 28561154B1; Tue, 2 Jan 2024 16:00:01 +0000 (UTC) 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="PduPRUQs" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (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 EEC8814ABF for ; Tue, 2 Jan 2024 15:59:56 +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 Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-40d88f9e602so14444325e9.3 for ; Tue, 02 Jan 2024 07:59:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1704211195; x=1704815995; 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=g3wf/pQ/AJWgbz0DcSp/EQGOfmOL52HnCJ3+CUimtjU=; b=PduPRUQsYm/KpfCIrEUCZUXYsQ7W+JTChiO0vxlA4tCt/TnMtN6KAmdNhvsJUnN1+9 xk6NawI3a+MvdzLGPMnsSw2Ri/H0/Zkk2YL6tte1jfjHmTyySxByGhzaZiTecbsKmjVk DHsTPO+WwUPvfXF8/AxcogXxL6p0FZGsdPO+kr+yKEw28WVDfDcmyhUgqgzwuZoq1Cvx x3yfPJGfsSSFvl1deH43Mj17jFTX+vZ2ljdRKl6vgifEzzmRhzCGeEz5v5YwyioaEboQ UlVfdZgS40bP1AqxgpXDJMIkCndRDH+LzRHzxRhQ7AaIFcZsFmSW68eUSs7Wsju8//Cq sD3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704211195; x=1704815995; 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=g3wf/pQ/AJWgbz0DcSp/EQGOfmOL52HnCJ3+CUimtjU=; b=L9XnvL7PJBh5k3yoMwDY/Xwul2Ok8Gyy5g7w2pcfp7FXHh7se5ZWJsA3F50RHe9Fxw FPM02cNteZYZItl79G9eZsTqW9GBHwzj6WaagfEW5YMqOrI5mdEFT31M7wbSs4OSJtFI 5NaW1F3hH/+fScNeytx2H8usiigpzS/K06Xxiby8glnPd++kfGc7+jHS/5OWsI/bt8U6 iUNrIdfGqZtaS993AK7v8JbiB09V31VEVodgPGgSv4IvM0dHq4v3JlBSQKXMggnVW2YV R81jhTvKk7fgBL7JYeyULVulrx63nT+J2u+kBmB04KUdNz4SU+Ajd/3gnh+xtF3XHFpo IcFw== X-Gm-Message-State: AOJu0YwSbFxY6zUdttEnIhMZTctSd2UKMXm7VxrexVuO9XhGCakhv6cz 7W+U/2ZP5S63PCMoK8DnHVPOtTtXvYTDnA== X-Received: by 2002:a05:600c:1ca9:b0:40d:5b0d:b105 with SMTP id k41-20020a05600c1ca900b0040d5b0db105mr6173715wms.117.1704211195108; Tue, 02 Jan 2024 07:59:55 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:6f51:fa47:f00a:a53c]) by smtp.gmail.com with ESMTPSA id u14-20020a05600c138e00b0040d62f97e3csm20556641wmf.10.2024.01.02.07.59.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 07:59:54 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 2/3] gpiolib: replace the GPIO device mutex with a read-write semaphore Date: Tue, 2 Jan 2024 16:59:48 +0100 Message-Id: <20240102155949.73434-3-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240102155949.73434-1-brgl@bgdev.pl> References: <20240102155949.73434-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1786995017165665494 X-GMAIL-MSGID: 1786995017165665494 From: Bartosz Golaszewski There are only two spots where we modify (add to or remove objects from) the GPIO device list. Readers should be able to access it concurrently. Replace the mutex with a read-write semaphore and adjust the locking operations accordingly. Signed-off-by: Bartosz Golaszewski Reviewed-by: Linus Walleij --- drivers/gpio/gpiolib-sysfs.c | 2 +- drivers/gpio/gpiolib.c | 18 +++++++++--------- drivers/gpio/gpiolib.h | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/drivers/gpio/gpiolib-sysfs.c b/drivers/gpio/gpiolib-sysfs.c index 2d29ae37d953..4dbf298bb5dd 100644 --- a/drivers/gpio/gpiolib-sysfs.c +++ b/drivers/gpio/gpiolib-sysfs.c @@ -773,7 +773,7 @@ int gpiochip_sysfs_register_all(void) struct gpio_device *gdev; int ret; - guard(mutex)(&gpio_devices_lock); + guard(rwsem_read)(&gpio_devices_sem); list_for_each_entry(gdev, &gpio_devices, list) { if (gdev->mockdev) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index e019c4243809..4c93cf73a826 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -85,7 +85,7 @@ static DEFINE_MUTEX(gpio_lookup_lock); static LIST_HEAD(gpio_lookup_list); LIST_HEAD(gpio_devices); -DEFINE_MUTEX(gpio_devices_lock); +DECLARE_RWSEM(gpio_devices_sem); static DEFINE_MUTEX(gpio_machine_hogs_mutex); static LIST_HEAD(gpio_machine_hogs); @@ -118,7 +118,7 @@ struct gpio_desc *gpio_to_desc(unsigned gpio) { struct gpio_device *gdev; - scoped_guard(mutex, &gpio_devices_lock) { + scoped_guard(rwsem_read, &gpio_devices_sem) { list_for_each_entry(gdev, &gpio_devices, list) { if (gdev->base <= gpio && gdev->base + gdev->ngpio > gpio) @@ -402,7 +402,7 @@ static struct gpio_desc *gpio_name_to_desc(const char * const name) if (!name) return NULL; - guard(mutex)(&gpio_devices_lock); + guard(rwsem_read)(&gpio_devices_sem); list_for_each_entry(gdev, &gpio_devices, list) { struct gpio_desc *desc; @@ -871,7 +871,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, gdev->ngpio = gc->ngpio; - scoped_guard(mutex, &gpio_devices_lock) { + scoped_guard(rwsem_write, &gpio_devices_sem) { /* * TODO: this allocates a Linux GPIO number base in the global * GPIO numberspace for this chip. In the long run we want to @@ -1001,7 +1001,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, goto err_print_message; } err_remove_from_list: - scoped_guard(mutex, &gpio_devices_lock) + scoped_guard(rwsem_write, &gpio_devices_sem) list_del(&gdev->list); err_free_label: kfree_const(gdev->label); @@ -1065,7 +1065,7 @@ void gpiochip_remove(struct gpio_chip *gc) dev_crit(&gdev->dev, "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n"); - scoped_guard(mutex, &gpio_devices_lock) + scoped_guard(rwsem_write, &gpio_devices_sem) list_del(&gdev->list); /* @@ -1114,7 +1114,7 @@ struct gpio_device *gpio_device_find(void *data, */ might_sleep(); - guard(mutex)(&gpio_devices_lock); + guard(rwsem_read)(&gpio_devices_sem); list_for_each_entry(gdev, &gpio_devices, list) { if (gdev->chip && match(gdev->chip, data)) @@ -4730,7 +4730,7 @@ static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos) s->private = ""; - guard(mutex)(&gpio_devices_lock); + guard(rwsem_read)(&gpio_devices_sem); list_for_each_entry(gdev, &gpio_devices, list) { if (index-- == 0) @@ -4745,7 +4745,7 @@ static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos) struct gpio_device *gdev = v; void *ret = NULL; - scoped_guard(mutex, &gpio_devices_lock) { + scoped_guard(rwsem_read, &gpio_devices_sem) { if (list_is_last(&gdev->list, &gpio_devices)) ret = NULL; else diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index 0ce7451a6b24..97df54abf57a 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -137,7 +137,7 @@ int gpiod_set_transitory(struct gpio_desc *desc, bool transitory); extern spinlock_t gpio_lock; extern struct list_head gpio_devices; -extern struct mutex gpio_devices_lock; +extern struct rw_semaphore gpio_devices_sem; void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action); From patchwork Tue Jan 2 15:59:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 184395 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:6f82:b0:100:9c79:88ff with SMTP id tb2csp4529381dyb; Tue, 2 Jan 2024 08:01:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IGMyjQ/pmMDX/neSNKSASHoHUsHEawzHlDyq1gkOkiDnmlnS0x2QBzs/VX9HdHdM6B499b9 X-Received: by 2002:a05:6214:20cf:b0:67a:cf80:cf12 with SMTP id 15-20020a05621420cf00b0067acf80cf12mr30081541qve.0.1704211262367; Tue, 02 Jan 2024 08:01:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704211262; cv=none; d=google.com; s=arc-20160816; b=GDYEF+WmVv3fhmlnhKr9UuNbkidXLS47RHEL5pntt0hE6mapbuyeZPuhUpQ89eI7m7 XLyCEsH8oDJ9uHuigyHgMhzMBXe1bxThyiPBB96FTFdA2jD9yZmQJJ7+QP95t7Njmkdg 6so+x7jQZ9JWSBkgUWK4OgJQIwFxunaoIgemIvP+sqe+eQOdhBs7/bKDmrxfrRDY1YlW Md0O2/ebF9MK5gjZflJI0ALrCuhaXeerRmdMSTrVRiNsXb6aLklShTlX3DHNvx1bZzOL HV2kiu1oCclavCGIufK9QAxFQ/MwDgDwhpQjP8p/mOq5uUiv35CprOQarUoQkJ0fIwEt xJaA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=2esXyx5UxoOKpIF+VnQ6ASTzOMASqrG8+CTbzdc5ia0=; fh=kmduAntvCxYKWJxf0o5GnU6TlPpo49/gb3O/sJ4FX9Y=; b=wNdIbdqX9Ln139sIDal4zsaOHJdmkISp/LX6/KaEzaiZKVK+YUVzUbeUNP3KjpDdIs klJqvFXIC/8o+2HxkefPRWPfHB73wuUKJ6RE/+BlRdc2Il/PWrDcb62TrfZtHG5ndleu scwpRAvc+hqI0c9Wkjxe6wdcV0VGrpcuActAwz8a1kCactlGdicMKMiD9s67OiVngxx6 tSwSENNr/bcy/RT4uAZAG8lU09pHbxz0jVcE/oRFotL6e7gzcAxx/S/1JtF2vFxXAxsO e5WhgXfIC7TLOJ8AcrQy7uU2OsyT0wlJMd7vdUwo3ds3RqrheNgRasCJpDg6SsV+geQk cB2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bgdev-pl.20230601.gappssmtp.com header.s=20230601 header.b=ZVJl1vpI; spf=pass (google.com: domain of linux-kernel+bounces-14578-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14578-ouuuleilei=gmail.com@vger.kernel.org" Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id s7-20020a0ce307000000b0067ebf3c2041si28090690qvl.355.2024.01.02.08.01.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 08:01:02 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-14578-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@bgdev-pl.20230601.gappssmtp.com header.s=20230601 header.b=ZVJl1vpI; spf=pass (google.com: domain of linux-kernel+bounces-14578-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14578-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 2B7701C2232F for ; Tue, 2 Jan 2024 16:01:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D4DAE156CD; Tue, 2 Jan 2024 16:00:02 +0000 (UTC) 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="ZVJl1vpI" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (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 0322D14F6F for ; Tue, 2 Jan 2024 15:59:57 +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 Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-40d6b4e2945so44728235e9.0 for ; Tue, 02 Jan 2024 07:59:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1704211196; x=1704815996; 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=2esXyx5UxoOKpIF+VnQ6ASTzOMASqrG8+CTbzdc5ia0=; b=ZVJl1vpIC1Dkqy4CjWJ4VibUTVzS7tUNHY6MU70rkwr6SM1vw5pRiC5ecHO19R9e9Q MO+sePdzIm1zQYnqmHoYJkf1K1a3/UOoBtjVcxE1OBmhG+t3lZYpmLWzbdXo8YVH1F0a u0aLLOzthTcnlQxTy6hJxvy1wn78e/oDRjbTYJgpFCHiIxYQrQrzmTnPo4J5sNrRaX6F 9b4ubW5JOaU9xlhvhhImOpMsYAiwc5TSfp9p62Cjs+4y52gmL4SxeC8Dj+p54SPCAdlD lSfBGVVv6f5vSvK8SoyzTCoZuwaGYzRreXiWD1wCk6w/GHAwqBT9cdQpq0mUsmdYWwmI bT2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704211196; x=1704815996; 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=2esXyx5UxoOKpIF+VnQ6ASTzOMASqrG8+CTbzdc5ia0=; b=RpLCcUiqVEMhnFJdf0OpeIveTl00fP6lGE2ESWCtXx8/TL6b607/e1zUgbIRGkB7Qp VKq/2UZTxhN59HnP2WTzhxOP/daqMB2PS0LEALYNSHh1ZmplMJU72A70b9Lrx1cg7mxd H+YR+UgzH/WsGs4I2orbbyqEq9ZPpRiMr69du7zN5cSqYAgCNreilRDMtEtG9ya67Xmy HyvIDnMJrEZxQh8bM9JuULRqwTvjnUleVwojcyJ6BUXWInq2GcSyATVR88to9l6caoHg hrj3247l0DqWwi8CACw5P0+OWBeO0ryYeUi0uSOeaeP6DHaAIZn8CtoTcekn+G68d21s a0xA== X-Gm-Message-State: AOJu0YyIqYs3NvBMb09zBqSEgzGsTWIxMfYVEA8mxvAl51lYhWQhj6w7 1ywjen77VIz2tIkue/C+WQuRGvSgN1SEdo2HxXSxUCfGwrs= X-Received: by 2002:a05:600c:28b:b0:40c:6eda:9260 with SMTP id 11-20020a05600c028b00b0040c6eda9260mr6544470wmk.56.1704211196193; Tue, 02 Jan 2024 07:59:56 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:6f51:fa47:f00a:a53c]) by smtp.gmail.com with ESMTPSA id u14-20020a05600c138e00b0040d62f97e3csm20556641wmf.10.2024.01.02.07.59.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 07:59:55 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 3/3] gpiolib: pin GPIO devices in place during descriptor lookup Date: Tue, 2 Jan 2024 16:59:49 +0100 Message-Id: <20240102155949.73434-4-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240102155949.73434-1-brgl@bgdev.pl> References: <20240102155949.73434-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1786995028559899559 X-GMAIL-MSGID: 1786995028559899559 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. Signed-off-by: Bartosz Golaszewski Reviewed-by: Linus Walleij --- drivers/gpio/gpiolib.c | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 4c93cf73a826..be57f8d6aeae 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -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);