From patchwork Thu Dec 21 01:20:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 757298 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) (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 142A63C1E; Thu, 21 Dec 2023 01:21:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="GsAZ+dax" Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-6d741fb7c8eso295543b3a.2; Wed, 20 Dec 2023 17:21:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1703121673; x=1703726473; 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=dLcenJ/GoxU7aCIQv2oBr9pxlEYNKaNvVPdm4SRt55Y=; b=GsAZ+daxbBZTZMEMcH66YqMkzeelDrlYtZ0dd0+jcDh+X5cuXMzvb8oypy7IyBzrG5 MJV0O1tML/Snl8EytCfYb2NnyWZI2SWEXjGKhzxGCDIpC78kmuZkZJZ7pebFPx3BzR7N EPSzSET7cYNTN5nMDYkChZWyaBWt9ukIosCSTPktXqsEqXgv+Cckizjl34z5KkLYA1ka WOrzVaXZwTYZg+bdlSXN8e/vEzwjn2iDe7YsB8ZemWlbOHDrgJafAalkJfBCSq4ChdLd H//GKNFRaDMm432hUkzPYSo7EeTyvoJAurOQA9WsdHSOu9POKBANwpK+qKCvQcSIInZE QvIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703121673; x=1703726473; 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=dLcenJ/GoxU7aCIQv2oBr9pxlEYNKaNvVPdm4SRt55Y=; b=cl4C2nZA90GuF3ALYlN9ZFPiKUTzw6Yp5dztFp4oQ/iiD0f5G7Bv4GqVlotcFoFlRi tYNYbPpUD/UxG4wVH9mVYswg2z3Wd48GYXlC9j28yZ93lFCG2e3cUDF7DNy0Ya6HoFCt VyCH83znfbc/3sL5DuEryM1W5IS5Mi4nynopvGraT7GJo/fnVfBtJ8MGeC2kBI0Fai0L hMnJ34aPFA3EVNTraDPmj/xagFcWNDGMOP5OPPiOiZWv6nVhXL8uiXiAfticA4qzX3c+ sccFXz43c6uyPP8l0d3uFV+c9courSO+MFeZU4V30zNPrULBo2gxPVxxGn/oSaoEzgDQ TzvQ== X-Gm-Message-State: AOJu0Yw6n6jdSduVPg/NPr7zu0F2u/+1J5/OBHfjwdntX4LS5sotL5Ed Jp6LN7s72CpyujUEhuag672SP+D53kQ= X-Google-Smtp-Source: AGHT+IEqkmG2Ao53YtUpYcDvMLRUEX90Wrk8sVbrTuf+S00szNsTkGtOh0Nvv1On8JL9lsJri+oJiw== X-Received: by 2002:a05:6a00:f8e:b0:6d8:cf8e:e627 with SMTP id ct14-20020a056a000f8e00b006d8cf8ee627mr5338333pfb.37.1703121673191; Wed, 20 Dec 2023 17:21:13 -0800 (PST) Received: from rigel.home.arpa (60-241-235-125.tpgi.com.au. [60.241.235.125]) by smtp.gmail.com with ESMTPSA id x1-20020a056a00270100b006d088356541sm375959pfv.104.2023.12.20.17.21.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Dec 2023 17:21:12 -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 v2 1/5] gpiolib: cdev: add gpio_device locking wrapper around gpio_ioctl() Date: Thu, 21 Dec 2023 09:20:36 +0800 Message-Id: <20231221012040.17763-2-warthog618@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231221012040.17763-1-warthog618@gmail.com> References: <20231221012040.17763-1-warthog618@gmail.com> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 While the GPIO cdev gpio_ioctl() call is in progress, the kernel can call gpiochip_remove() which will set gdev->chip to NULL, after which any subsequent access will cause a crash. gpio_ioctl() was overlooked by the previous fix to protect syscalls (bdbbae241a04), so add protection for that. Fixes: bdbbae241a04 ("gpiolib: protect the GPIO device against being dropped while in use by user-space") Fixes: d7c51b47ac11 ("gpio: userspace ABI for reading/writing GPIO lines") Fixes: 3c0d9c635ae2 ("gpiolib: cdev: support GPIO_V2_GET_LINE_IOCTL and GPIO_V2_LINE_GET_VALUES_IOCTL") Fixes: aad955842d1c ("gpiolib: cdev: support GPIO_V2_GET_LINEINFO_IOCTL and GPIO_V2_GET_LINEINFO_WATCH_IOCTL") Signed-off-by: Kent Gibson --- drivers/gpio/gpiolib-cdev.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 744734405912..9155c54acc1e 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -2598,10 +2598,7 @@ static int lineinfo_unwatch(struct gpio_chardev_data *cdev, void __user *ip) return 0; } -/* - * gpio_ioctl() - ioctl handler for the GPIO chardev - */ -static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) +static long gpio_ioctl_unlocked(struct file *file, unsigned int cmd, unsigned long arg) { struct gpio_chardev_data *cdev = file->private_data; struct gpio_device *gdev = cdev->gdev; @@ -2638,6 +2635,17 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) } } +/* + * gpio_ioctl() - ioctl handler for the GPIO chardev + */ +static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) +{ + struct gpio_chardev_data *cdev = file->private_data; + + return call_ioctl_locked(file, cmd, arg, cdev->gdev, + gpio_ioctl_unlocked); +} + #ifdef CONFIG_COMPAT static long gpio_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg) From patchwork Thu Dec 21 01:20:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 757686 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) (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 3D61B441C; Thu, 21 Dec 2023 01:21:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hgbhec6t" Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-6d940d14d69so212279b3a.1; Wed, 20 Dec 2023 17:21:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1703121690; x=1703726490; 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=H0+OiIGPu+HUKHHy83yKhozaVuLruzud+CSwPTxOtko=; b=hgbhec6tvgvwNiiyP5nKYCA6iNmD6dQGhr9/zh0FhG8eWSliw9VBPzfKxNxi7yBhJr b15+szLGoY7oGoedeG05KwiQEPwetJJMXfbwm2LtZ0esMx1nipPAoDKlXFLOakIhK/Oh OU/WcgmhyXr4IHCKOdCRWrF7kWDcl7c4aQjg7K2CBRcjePORavqbiO6ZBmSJETwfMZqR jX0eBV2gj5ZHYFasrqiMtvVLwYtGgJoT7HVpVmd7qv/jHT5k6YUC4pmnhyMy4IrfClat 5aruZdTW+FYTVS9LCIahbfHm4FOGn5+5YU5y5idSGi7Ukfk/U9cDLtgSTods14WSASlf P5zQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703121690; x=1703726490; 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=H0+OiIGPu+HUKHHy83yKhozaVuLruzud+CSwPTxOtko=; b=X2/UfVVMofnIUY873y92NtgNwxfskjmVBwQn+Bs8IBoO+pcSeLvFgA1RixubaeiChz 0O53iT4bcM8b4NxgB3dp9BHuBskSrIuEMuUHMp5hRzPX58p4TsALBFDuBiuwC1Tlw2VK Vk77+7RBH9T7IvkzUZ26mBYRjwtjzROW10yQRNVvZuUzY6N3Bf9SoG3Fd3EdtFPI8tba LTHcnpM8rOmUY+sZ7yu13PgUEv1iYEjDi8R1+b1EWr7DFV4LToCqdbrwuFkQTbRbJlFi XqDpKdpqlhWDkgfvLUssZ+en54+pnIS3yqhwylOOuG61NInCHrYXIY/v3pj0lgcwcwPC MMSg== X-Gm-Message-State: AOJu0Yz9MRCTxf38P4f1JJonfW/kHFmZhYbY9y/mtB3aB9Remuh7sNs/ uov/qXf+7AtrgyD5v3CMsyaY8H64Qy4= X-Google-Smtp-Source: AGHT+IHGKZ4b7MGMa++Om/ndy9Q7vwRGyDFcNcsiV5eXyjufAjLXNZQzsPMqzpAfpWk5LXbX3WcN4Q== X-Received: by 2002:a62:b40f:0:b0:6d2:6b1f:b82 with SMTP id h15-20020a62b40f000000b006d26b1f0b82mr5942972pfn.67.1703121689794; Wed, 20 Dec 2023 17:21:29 -0800 (PST) Received: from rigel.home.arpa (60-241-235-125.tpgi.com.au. [60.241.235.125]) by smtp.gmail.com with ESMTPSA id x1-20020a056a00270100b006d088356541sm375959pfv.104.2023.12.20.17.21.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Dec 2023 17:21:29 -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 v2 2/5] gpiolib: cdev: include overflow.h Date: Thu, 21 Dec 2023 09:20:37 +0800 Message-Id: <20231221012040.17763-3-warthog618@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231221012040.17763-1-warthog618@gmail.com> References: <20231221012040.17763-1-warthog618@gmail.com> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 struct_size() is used to calculate struct linereq size, so explicitly include overflow.h. Signed-off-by: Kent Gibson --- drivers/gpio/gpiolib-cdev.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 9155c54acc1e..942fe115b726 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include From patchwork Thu Dec 21 01:20:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 757297 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) (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 E5F2F441E; Thu, 21 Dec 2023 01:21:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="UpQHKdco" Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-6d939e2f594so279749b3a.3; Wed, 20 Dec 2023 17:21:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1703121703; x=1703726503; 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=2G9xQuVfP3PvQMdAXCehswOfS0hQP216b/ZuRf+VAvA=; b=UpQHKdcoXOEWAcWuDy31PP25zJVptPd+LUeGrfIi3iVEd/PE2/YLCprDGR5UIj6Hal l9AFsVLl1mp+WHsHQ1UeSLrhPO8+8JSrNcrs4FMWu82a1Y/9BD2O3xkQmSYru4mDAYug /ighy7zw/2h1CDr4mi3n8XZnTO7GWgmwjgjGEDptlVoHAn0xV+gXDi2Aw9y6IA3bpubX P/E8SCvZKs3HaLmsJch1huEk12CoWRyzAxvkTBmZU+3CxqqwY/JJHnT/66fIo14DykEC LoTxZdc7z8DsjZxXxa1/uYjC+7ljVwPqbPIAxZ/SSpHTIXjn5bBTXp8+MoOXl/sdGeM1 lqJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703121703; x=1703726503; 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=2G9xQuVfP3PvQMdAXCehswOfS0hQP216b/ZuRf+VAvA=; b=jQyy+Hwq/6UK28qHB0JRmE3UvIVH9DjlfO3OPSp2YlWcKybWsOQfZxySQuZRMw0J7x DJ783CNaCjeQcj0pf0RZJSLHEHSR9oLYfE8/OwhuY6gedPLaoL1907JjuMvFG8eotlgE uQwACUe1SUDjCUg4Fn7mqdJBXHDDBhG7N479/AACETnBQ8yJZB1UCzUQs4072I9eiLtR kRrQ/AXiznriZowbQei+V8+9O/UjHQMFuR7rAi5/F/WD2pOpUWyidzl5z61Ot3IS+JRs a824tCtwcj3FjnWCSe2dnp3jIf2TShitgaewqmzPmY3eilPqDMmjdNPkF4W1oyGz/k7y ODZQ== X-Gm-Message-State: AOJu0YzmTTaFnsH2VVylnSzt4Ap4WQdDiVF49cDPpwlKjhbsmFIh9/mh Dy/PqsjdUJvb981RCf4eJlbYBDNEYwE= X-Google-Smtp-Source: AGHT+IGaXPh8OyVDZdKii9OHBFSWscknH+m41jrQOdLjMqxtaMJ7/pHaKVR5fBc2ByL8/fa5OCCwlg== X-Received: by 2002:a05:6a20:7da4:b0:194:dd52:bdc7 with SMTP id v36-20020a056a207da400b00194dd52bdc7mr856347pzj.56.1703121703104; Wed, 20 Dec 2023 17:21:43 -0800 (PST) Received: from rigel.home.arpa (60-241-235-125.tpgi.com.au. [60.241.235.125]) by smtp.gmail.com with ESMTPSA id x1-20020a056a00270100b006d088356541sm375959pfv.104.2023.12.20.17.21.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Dec 2023 17:21:42 -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 v2 3/5] gpiolib: cdev: allocate linereq using kvzalloc() Date: Thu, 21 Dec 2023 09:20:38 +0800 Message-Id: <20231221012040.17763-4-warthog618@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231221012040.17763-1-warthog618@gmail.com> References: <20231221012040.17763-1-warthog618@gmail.com> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The size of struct linereq may exceed a page, so allocate space for it using kvzalloc() instead of kzalloc() to handle the case where memory is heavily fragmented and kzalloc() cannot find a sufficient contiguous region. Signed-off-by: Kent Gibson --- drivers/gpio/gpiolib-cdev.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 942fe115b726..5424c878627e 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -1723,7 +1723,7 @@ static void linereq_free(struct linereq *lr) kfifo_free(&lr->events); kfree(lr->label); gpio_device_put(lr->gdev); - kfree(lr); + kvfree(lr); } static int linereq_release(struct inode *inode, struct file *file) @@ -1788,7 +1788,7 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) if (ret) return ret; - lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL); + lr = kvzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL); if (!lr) return -ENOMEM; lr->num_lines = ulr.num_lines; From patchwork Thu Dec 21 01:20:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 757685 Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) (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 EDC2D441E; Thu, 21 Dec 2023 01:21:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="N07OHKQE" Received: by mail-pg1-f177.google.com with SMTP id 41be03b00d2f7-5cd86e3a9afso193163a12.1; Wed, 20 Dec 2023 17:21:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1703121718; x=1703726518; 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=yEnEXmwh1Gejk6pPQlc8WucbY92GrZLw8XwSVMxV33c=; b=N07OHKQEe61SbHlssDrW4vxmLV2ZY84Ek83lmNtSQpSMkCkNhm3kGU8Ao0M/+ZkMFz tp85b1kgcnMnoQsntmr97PhhA6Fdgw4qCy6qW5gLkl4MpqF0HsbQtAg+zU6TS9B1D0D8 SH/dFD6ghJklPVMc4UPn/7n5Ra1sZW1MBu8r0U1EY2obysjHGRlIZq2iVyui/kY3DzAA zl2pP2NGEeJbPVv6cAbC+1pxZ7cAAc0I+7Y8gGTSCoKnCWhe2KgJq7rO9S0AfIg10gpI B7pEkYwyFIc6QcjploHcTKf2qv9yAFVOuLFnT7dSy3ehj+wKvduiwAfsCrp/c7HcqEI2 aeBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703121718; x=1703726518; 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=yEnEXmwh1Gejk6pPQlc8WucbY92GrZLw8XwSVMxV33c=; b=NsxbN0FF0V1JH3AQDe1XmbAxWQUexph8rBdgVPV5mCh29nX5Ve/ad0636qn92LuKwa 0bZT1BVrtyeX5zDM2dBayitjzrU2NxJs/8Nw5SEwlnpDUpCiadD+gAFJsx6fRIw95CFD +a/J4ZfKKv6worN24n+Bor5x51TOim/bIkUOGJ9vgEjlYcUgrBNkVkJ+n78pqs3FsUfn qeaViLhEFlYz41JJjsy9+A5Ei/vCRKhBeXS6K4JP6uQe8/jHS5u0Be1dpkEWF8NRBXFc 6jdaq0xMJARmBZ+9nh74tPmuoKYeo/xvMy1FJaNxzeL0H/9NC+UJ1UaINxikMKCY8N6F nqKA== X-Gm-Message-State: AOJu0YwPHdcr40lLmPrgxJq1NhSS5AORhLRZezTNduUlgwlaiFncH1xK OSQwCg4JGYTkqp2WJ1dhrOnzX1M0DQw= X-Google-Smtp-Source: AGHT+IFfTfkGLm95NUUWpKuP+zmUZsVpGtLx41MHnx183BKcRu25hozCR1/YFyspLeDvSuncst2u0A== X-Received: by 2002:a05:6a21:99a0:b0:195:f2:1fb3 with SMTP id ve32-20020a056a2199a000b0019500f21fb3mr370653pzb.42.1703121717904; Wed, 20 Dec 2023 17:21:57 -0800 (PST) Received: from rigel.home.arpa (60-241-235-125.tpgi.com.au. [60.241.235.125]) by smtp.gmail.com with ESMTPSA id x1-20020a056a00270100b006d088356541sm375959pfv.104.2023.12.20.17.21.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Dec 2023 17:21:57 -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 v2 4/5] gpiolib: cdev: replace locking wrappers for config_mutex with guards Date: Thu, 21 Dec 2023 09:20:39 +0800 Message-Id: <20231221012040.17763-5-warthog618@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231221012040.17763-1-warthog618@gmail.com> References: <20231221012040.17763-1-warthog618@gmail.com> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 After the adoption of guard(), the locking wrappers that hold the config_mutex for linereq_set_values() and linereq_set_config() no longer add value, so combine them into the functions they wrap. Signed-off-by: Kent Gibson --- drivers/gpio/gpiolib-cdev.c | 63 ++++++++++++++----------------------- 1 file changed, 24 insertions(+), 39 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 5424c878627e..9ff2b447cc20 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -1454,14 +1454,19 @@ static long linereq_get_values(struct linereq *lr, void __user *ip) return 0; } -static long linereq_set_values_unlocked(struct linereq *lr, - struct gpio_v2_line_values *lv) +static long linereq_set_values(struct linereq *lr, void __user *ip) { DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); + struct gpio_v2_line_values lv; struct gpio_desc **descs; unsigned int i, didx, num_set; int ret; + if (copy_from_user(&lv, ip, sizeof(lv))) + return -EFAULT; + + guard(mutex)(&lr->config_mutex); + /* * gpiod_set_array_value_complex() requires compacted desc and val * arrays, rather than the sparse ones in lv. @@ -1472,12 +1477,12 @@ static long linereq_set_values_unlocked(struct linereq *lr, bitmap_zero(vals, GPIO_V2_LINES_MAX); /* scan requested lines to determine the subset to be set */ for (num_set = 0, i = 0; i < lr->num_lines; i++) { - if (lv->mask & BIT_ULL(i)) { + if (lv.mask & BIT_ULL(i)) { /* setting inputs is not allowed */ if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags)) return -EPERM; /* add to compacted values */ - if (lv->bits & BIT_ULL(i)) + if (lv.bits & BIT_ULL(i)) __set_bit(num_set, vals); num_set++; /* capture desc for the num_set == 1 case */ @@ -1493,7 +1498,7 @@ static long linereq_set_values_unlocked(struct linereq *lr, if (!descs) return -ENOMEM; for (didx = 0, i = 0; i < lr->num_lines; i++) { - if (lv->mask & BIT_ULL(i)) { + if (lv.mask & BIT_ULL(i)) { descs[didx] = lr->lines[i].desc; didx++; } @@ -1507,31 +1512,28 @@ static long linereq_set_values_unlocked(struct linereq *lr, return ret; } -static long linereq_set_values(struct linereq *lr, void __user *ip) -{ - struct gpio_v2_line_values lv; - - if (copy_from_user(&lv, ip, sizeof(lv))) - return -EFAULT; - - guard(mutex)(&lr->config_mutex); - - return linereq_set_values_unlocked(lr, &lv); -} - -static long linereq_set_config_unlocked(struct linereq *lr, - struct gpio_v2_line_config *lc) +static long linereq_set_config(struct linereq *lr, void __user *ip) { + struct gpio_v2_line_config lc; struct gpio_desc *desc; struct line *line; unsigned int i; u64 flags, edflags; int ret; + if (copy_from_user(&lc, ip, sizeof(lc))) + return -EFAULT; + + ret = gpio_v2_line_config_validate(&lc, lr->num_lines); + if (ret) + return ret; + + guard(mutex)(&lr->config_mutex); + for (i = 0; i < lr->num_lines; i++) { line = &lr->lines[i]; desc = lr->lines[i].desc; - flags = gpio_v2_line_config_flags(lc, i); + flags = gpio_v2_line_config_flags(&lc, i); gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags); edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS; /* @@ -1539,7 +1541,7 @@ static long linereq_set_config_unlocked(struct linereq *lr, * or output, else the line will be treated "as is". */ if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { - int val = gpio_v2_line_config_output_value(lc, i); + int val = gpio_v2_line_config_output_value(&lc, i); edge_detector_stop(line); ret = gpiod_direction_output(desc, val); @@ -1550,7 +1552,7 @@ static long linereq_set_config_unlocked(struct linereq *lr, if (ret) return ret; - ret = edge_detector_update(line, lc, i, edflags); + ret = edge_detector_update(line, &lc, i, edflags); if (ret) return ret; } @@ -1562,23 +1564,6 @@ static long linereq_set_config_unlocked(struct linereq *lr, return 0; } -static long linereq_set_config(struct linereq *lr, void __user *ip) -{ - struct gpio_v2_line_config lc; - int ret; - - if (copy_from_user(&lc, ip, sizeof(lc))) - return -EFAULT; - - ret = gpio_v2_line_config_validate(&lc, lr->num_lines); - if (ret) - return ret; - - guard(mutex)(&lr->config_mutex); - - return linereq_set_config_unlocked(lr, &lc); -} - static long linereq_ioctl_unlocked(struct file *file, unsigned int cmd, unsigned long arg) { From patchwork Thu Dec 21 01:20:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 757296 Received: from mail-ot1-f47.google.com (mail-ot1-f47.google.com [209.85.210.47]) (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 559DF4419; Thu, 21 Dec 2023 01:22:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="UVdHktCq" Received: by mail-ot1-f47.google.com with SMTP id 46e09a7af769-6da69dd6e9fso189255a34.1; Wed, 20 Dec 2023 17:22:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1703121732; x=1703726532; 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=4oIuaHLOj8O8LSsFxzvBYQlFdWLFJBkiRSrLUWFUI2U=; b=UVdHktCq3w70KOorItu17K2Fve6VFbveD+CAUOP4rWxUIH1HjMW7rIAvf5g5Qtt8LN OZolnuC8WjLp4TOXj3v6nWJRNDKXJHJbiSd4/RPadPKTCVHUjFI++oc39SQYK8JKydiO 8iwPJBeH7CMNbgG438InKpWQc4XNX47SMAj7FmV2twmGPhXqvW/heInOW/MnSAaNa9jo SZpiCTOOGZ0RVdlRbsFEbuG6YXZskEoi+rtGlbyR606P63D5NvW+HHm9SXB3lrb373fx 6hdSEmB5Q72jJXv27Z69bSXf9nmqXs7z/LoMQ4XAtmo7nwsJevvgTGn/ar7cmcVQfERW bFAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703121732; x=1703726532; 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=4oIuaHLOj8O8LSsFxzvBYQlFdWLFJBkiRSrLUWFUI2U=; b=ivf1zgAFhOOp16VNnUHxKuhVg8JTinjQ4IrXWTp/tlUeNdvhTnZFcFve7m+WSQezfg MVNRPwgDk65aw55nqAPshX6yOPLj2A1PfMoDplJFb8GfsCqkpucPPSyncgZE6oxCIk9j sQ+S/NFjGOAeGxyFigcn/spUgj2zRvyckcB8vTvJQ1XFf3rK0YvFI3aocUf+8XDIji36 3+/CSMhjcHaE8Cela1zDTuXGW2afQodx/OxRjswAk+Vaf8ArNYz3cFKMKgNhMH0mWqIc nL+MuDc/lgk/guHTNg613ajkr/uhqzgNeJUUiX67bJMKJU2xl+A3Xi7fYv5hjVHvu6ML lEUg== X-Gm-Message-State: AOJu0Yy1DUtxVWNMmsop8xfteevzuRjWL8iPBdHgN1h/PVAgxiP+UrbE 8nmahs87tWLBXT44/KndQxTELwUC0ZI= X-Google-Smtp-Source: AGHT+IGAnDITY3XgghV1SlTFLy/8kmlDmtPvvVrotVqkefawGSjwgRRMI0P217+kADCnXhW3jseUIQ== X-Received: by 2002:a05:6830:138a:b0:6db:b215:ad8d with SMTP id d10-20020a056830138a00b006dbb215ad8dmr1070230otq.36.1703121732119; Wed, 20 Dec 2023 17:22:12 -0800 (PST) Received: from rigel.home.arpa (60-241-235-125.tpgi.com.au. [60.241.235.125]) by smtp.gmail.com with ESMTPSA id x1-20020a056a00270100b006d088356541sm375959pfv.104.2023.12.20.17.22.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Dec 2023 17:22:11 -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 v2 5/5] gpiolib: cdev: replace locking wrappers for gpio_device with guards Date: Thu, 21 Dec 2023 09:20:40 +0800 Message-Id: <20231221012040.17763-6-warthog618@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231221012040.17763-1-warthog618@gmail.com> References: <20231221012040.17763-1-warthog618@gmail.com> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Replace the wrapping functions that inhibit removal of the gpio chip with equivalent guards. Signed-off-by: Kent Gibson --- drivers/gpio/gpiolib-cdev.c | 205 +++++++++--------------------------- 1 file changed, 47 insertions(+), 158 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 9ff2b447cc20..2a88736629ef 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -65,45 +66,6 @@ typedef long (*ioctl_fn)(struct file *, unsigned int, unsigned long); typedef ssize_t (*read_fn)(struct file *, char __user *, size_t count, loff_t *); -static __poll_t call_poll_locked(struct file *file, - struct poll_table_struct *wait, - struct gpio_device *gdev, poll_fn func) -{ - __poll_t ret; - - down_read(&gdev->sem); - ret = func(file, wait); - up_read(&gdev->sem); - - return ret; -} - -static long call_ioctl_locked(struct file *file, unsigned int cmd, - unsigned long arg, struct gpio_device *gdev, - ioctl_fn func) -{ - long ret; - - down_read(&gdev->sem); - ret = func(file, cmd, arg); - up_read(&gdev->sem); - - return ret; -} - -static ssize_t call_read_locked(struct file *file, char __user *buf, - size_t count, loff_t *f_ps, - struct gpio_device *gdev, read_fn func) -{ - ssize_t ret; - - down_read(&gdev->sem); - ret = func(file, buf, count, f_ps); - up_read(&gdev->sem); - - return ret; -} - /* * GPIO line handle management */ @@ -238,8 +200,8 @@ static long linehandle_set_config(struct linehandle_state *lh, return 0; } -static long linehandle_ioctl_unlocked(struct file *file, unsigned int cmd, - unsigned long arg) +static long linehandle_ioctl(struct file *file, unsigned int cmd, + unsigned long arg) { struct linehandle_state *lh = file->private_data; void __user *ip = (void __user *)arg; @@ -248,6 +210,8 @@ static long linehandle_ioctl_unlocked(struct file *file, unsigned int cmd, unsigned int i; int ret; + guard(rwsem_read)(&lh->gdev->sem); + if (!lh->gdev->chip) return -ENODEV; @@ -297,15 +261,6 @@ static long linehandle_ioctl_unlocked(struct file *file, unsigned int cmd, } } -static long linehandle_ioctl(struct file *file, unsigned int cmd, - unsigned long arg) -{ - struct linehandle_state *lh = file->private_data; - - return call_ioctl_locked(file, cmd, arg, lh->gdev, - linehandle_ioctl_unlocked); -} - #ifdef CONFIG_COMPAT static long linehandle_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg) @@ -1564,12 +1519,14 @@ static long linereq_set_config(struct linereq *lr, void __user *ip) return 0; } -static long linereq_ioctl_unlocked(struct file *file, unsigned int cmd, - unsigned long arg) +static long linereq_ioctl(struct file *file, unsigned int cmd, + unsigned long arg) { struct linereq *lr = file->private_data; void __user *ip = (void __user *)arg; + guard(rwsem_read)(&lr->gdev->sem); + if (!lr->gdev->chip) return -ENODEV; @@ -1585,15 +1542,6 @@ static long linereq_ioctl_unlocked(struct file *file, unsigned int cmd, } } -static long linereq_ioctl(struct file *file, unsigned int cmd, - unsigned long arg) -{ - struct linereq *lr = file->private_data; - - return call_ioctl_locked(file, cmd, arg, lr->gdev, - linereq_ioctl_unlocked); -} - #ifdef CONFIG_COMPAT static long linereq_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg) @@ -1602,12 +1550,14 @@ static long linereq_ioctl_compat(struct file *file, unsigned int cmd, } #endif -static __poll_t linereq_poll_unlocked(struct file *file, - struct poll_table_struct *wait) +static __poll_t linereq_poll(struct file *file, + struct poll_table_struct *wait) { struct linereq *lr = file->private_data; __poll_t events = 0; + guard(rwsem_read)(&lr->gdev->sem); + if (!lr->gdev->chip) return EPOLLHUP | EPOLLERR; @@ -1620,22 +1570,16 @@ static __poll_t linereq_poll_unlocked(struct file *file, return events; } -static __poll_t linereq_poll(struct file *file, - struct poll_table_struct *wait) -{ - struct linereq *lr = file->private_data; - - return call_poll_locked(file, wait, lr->gdev, linereq_poll_unlocked); -} - -static ssize_t linereq_read_unlocked(struct file *file, char __user *buf, - size_t count, loff_t *f_ps) +static ssize_t linereq_read(struct file *file, char __user *buf, + size_t count, loff_t *f_ps) { struct linereq *lr = file->private_data; struct gpio_v2_line_event le; ssize_t bytes_read = 0; int ret; + guard(rwsem_read)(&lr->gdev->sem); + if (!lr->gdev->chip) return -ENODEV; @@ -1677,15 +1621,6 @@ static ssize_t linereq_read_unlocked(struct file *file, char __user *buf, return bytes_read; } -static ssize_t linereq_read(struct file *file, char __user *buf, - size_t count, loff_t *f_ps) -{ - struct linereq *lr = file->private_data; - - return call_read_locked(file, buf, count, f_ps, lr->gdev, - linereq_read_unlocked); -} - static void linereq_free(struct linereq *lr) { struct line *line; @@ -1938,12 +1873,14 @@ struct lineevent_state { (GPIOEVENT_REQUEST_RISING_EDGE | \ GPIOEVENT_REQUEST_FALLING_EDGE) -static __poll_t lineevent_poll_unlocked(struct file *file, - struct poll_table_struct *wait) +static __poll_t lineevent_poll(struct file *file, + struct poll_table_struct *wait) { struct lineevent_state *le = file->private_data; __poll_t events = 0; + guard(rwsem_read)(&le->gdev->sem); + if (!le->gdev->chip) return EPOLLHUP | EPOLLERR; @@ -1955,14 +1892,6 @@ static __poll_t lineevent_poll_unlocked(struct file *file, return events; } -static __poll_t lineevent_poll(struct file *file, - struct poll_table_struct *wait) -{ - struct lineevent_state *le = file->private_data; - - return call_poll_locked(file, wait, le->gdev, lineevent_poll_unlocked); -} - static int lineevent_unregistered_notify(struct notifier_block *nb, unsigned long action, void *data) { @@ -1979,8 +1908,8 @@ struct compat_gpioeevent_data { u32 id; }; -static ssize_t lineevent_read_unlocked(struct file *file, char __user *buf, - size_t count, loff_t *f_ps) +static ssize_t lineevent_read(struct file *file, char __user *buf, + size_t count, loff_t *f_ps) { struct lineevent_state *le = file->private_data; struct gpioevent_data ge; @@ -1988,6 +1917,8 @@ static ssize_t lineevent_read_unlocked(struct file *file, char __user *buf, ssize_t ge_size; int ret; + guard(rwsem_read)(&le->gdev->sem); + if (!le->gdev->chip) return -ENODEV; @@ -2042,15 +1973,6 @@ static ssize_t lineevent_read_unlocked(struct file *file, char __user *buf, return bytes_read; } -static ssize_t lineevent_read(struct file *file, char __user *buf, - size_t count, loff_t *f_ps) -{ - struct lineevent_state *le = file->private_data; - - return call_read_locked(file, buf, count, f_ps, le->gdev, - lineevent_read_unlocked); -} - static void lineevent_free(struct lineevent_state *le) { if (le->device_unregistered_nb.notifier_call) @@ -2071,13 +1993,15 @@ static int lineevent_release(struct inode *inode, struct file *file) return 0; } -static long lineevent_ioctl_unlocked(struct file *file, unsigned int cmd, - unsigned long arg) +static long lineevent_ioctl(struct file *file, unsigned int cmd, + unsigned long arg) { struct lineevent_state *le = file->private_data; void __user *ip = (void __user *)arg; struct gpiohandle_data ghd; + guard(rwsem_read)(&le->gdev->sem); + if (!le->gdev->chip) return -ENODEV; @@ -2103,15 +2027,6 @@ static long lineevent_ioctl_unlocked(struct file *file, unsigned int cmd, return -EINVAL; } -static long lineevent_ioctl(struct file *file, unsigned int cmd, - unsigned long arg) -{ - struct lineevent_state *le = file->private_data; - - return call_ioctl_locked(file, cmd, arg, le->gdev, - lineevent_ioctl_unlocked); -} - #ifdef CONFIG_COMPAT static long lineevent_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg) @@ -2584,12 +2499,17 @@ static int lineinfo_unwatch(struct gpio_chardev_data *cdev, void __user *ip) return 0; } -static long gpio_ioctl_unlocked(struct file *file, unsigned int cmd, unsigned long arg) +/* + * gpio_ioctl() - ioctl handler for the GPIO chardev + */ +static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { struct gpio_chardev_data *cdev = file->private_data; struct gpio_device *gdev = cdev->gdev; void __user *ip = (void __user *)arg; + guard(rwsem_read)(&gdev->sem); + /* We fail any subsequent ioctl():s when the chip is gone */ if (!gdev->chip) return -ENODEV; @@ -2621,17 +2541,6 @@ static long gpio_ioctl_unlocked(struct file *file, unsigned int cmd, unsigned lo } } -/* - * gpio_ioctl() - ioctl handler for the GPIO chardev - */ -static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) -{ - struct gpio_chardev_data *cdev = file->private_data; - - return call_ioctl_locked(file, cmd, arg, cdev->gdev, - gpio_ioctl_unlocked); -} - #ifdef CONFIG_COMPAT static long gpio_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg) @@ -2679,12 +2588,14 @@ static int gpio_device_unregistered_notify(struct notifier_block *nb, return NOTIFY_OK; } -static __poll_t lineinfo_watch_poll_unlocked(struct file *file, - struct poll_table_struct *pollt) +static __poll_t lineinfo_watch_poll(struct file *file, + struct poll_table_struct *pollt) { struct gpio_chardev_data *cdev = file->private_data; __poll_t events = 0; + guard(rwsem_read)(&cdev->gdev->sem); + if (!cdev->gdev->chip) return EPOLLHUP | EPOLLERR; @@ -2697,17 +2608,8 @@ static __poll_t lineinfo_watch_poll_unlocked(struct file *file, return events; } -static __poll_t lineinfo_watch_poll(struct file *file, - struct poll_table_struct *pollt) -{ - struct gpio_chardev_data *cdev = file->private_data; - - return call_poll_locked(file, pollt, cdev->gdev, - lineinfo_watch_poll_unlocked); -} - -static ssize_t lineinfo_watch_read_unlocked(struct file *file, char __user *buf, - size_t count, loff_t *off) +static ssize_t lineinfo_watch_read(struct file *file, char __user *buf, + size_t count, loff_t *off) { struct gpio_chardev_data *cdev = file->private_data; struct gpio_v2_line_info_changed event; @@ -2715,6 +2617,8 @@ static ssize_t lineinfo_watch_read_unlocked(struct file *file, char __user *buf, int ret; size_t event_size; + guard(rwsem_read)(&cdev->gdev->sem); + if (!cdev->gdev->chip) return -ENODEV; @@ -2777,15 +2681,6 @@ static ssize_t lineinfo_watch_read_unlocked(struct file *file, char __user *buf, return bytes_read; } -static ssize_t lineinfo_watch_read(struct file *file, char __user *buf, - size_t count, loff_t *off) -{ - struct gpio_chardev_data *cdev = file->private_data; - - return call_read_locked(file, buf, count, off, cdev->gdev, - lineinfo_watch_read_unlocked); -} - /** * gpio_chrdev_open() - open the chardev for ioctl operations * @inode: inode for this chardev @@ -2799,17 +2694,15 @@ static int gpio_chrdev_open(struct inode *inode, struct file *file) struct gpio_chardev_data *cdev; int ret = -ENOMEM; - down_read(&gdev->sem); + guard(rwsem_read)(&gdev->sem); /* Fail on open if the backing gpiochip is gone */ - if (!gdev->chip) { - ret = -ENODEV; - goto out_unlock; - } + if (!gdev->chip) + return -ENODEV; cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); if (!cdev) - goto out_unlock; + return -ENODEV; cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL); if (!cdev->watched_lines) @@ -2838,8 +2731,6 @@ static int gpio_chrdev_open(struct inode *inode, struct file *file) if (ret) goto out_unregister_device_notifier; - up_read(&gdev->sem); - return ret; out_unregister_device_notifier: @@ -2853,8 +2744,6 @@ static int gpio_chrdev_open(struct inode *inode, struct file *file) bitmap_free(cdev->watched_lines); out_free_cdev: kfree(cdev); -out_unlock: - up_read(&gdev->sem); return ret; }