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);