From patchwork Thu Nov 30 13:46:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 171892 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp396895vqy; Thu, 30 Nov 2023 05:47:29 -0800 (PST) X-Google-Smtp-Source: AGHT+IGGdwymanP1yfNbPARexXVNR9FiBgbmviRwPeBjYy3427i2i2SsiBx0ViF3stEq7k0wDgrg X-Received: by 2002:a17:902:e752:b0:1cf:b5ae:2817 with SMTP id p18-20020a170902e75200b001cfb5ae2817mr19830107plf.54.1701352049268; Thu, 30 Nov 2023 05:47:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701352049; cv=none; d=google.com; s=arc-20160816; b=OFsa3ts25LVmqY2/u8h5fKsFVMrhuG/urbiMtg70KVa34xc8AIQDA1XPXqwLRs8lL/ aLuQFsTcceU6rKDQOf0BclwYTIb8B7aCD5O5qgZ8EIYDy2u4hP5MlBhYIKOmwLZAS8Rt j/rjaO0O9zibZbrHN0cp0OJ4ZKU30D9YQHzvznBuzanQEIbSttFNTOW+9NNwvLdjznNY Wck0HSlDX6Lv9cJox012HXT9K75+sjJkyK4SK/jAmNUyo/muy9opT+SAcMO2AaJR4vjw 3YsJ4QZukUUb09KpOCMCqUwslhxRsmqnUFhUl/hJ7rB6j7VUtUWY34jHetbX4iWJCRkP RcKg== 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=5JkcbylM7pAQh/R7rY3XvCoFJsyCRp+V27URZHzTJKg=; fh=fw6biwzn34ngIUS3OuPB+D+TvAzoz1YwnvitHxA+Jyw=; b=BLUV403e5YYGF46T474sAXUwxR7EcqL0qAAwaXNR/IWDINvCfKEGiI+4tlH54AgyQn h8hUkug8/ZP0C1FEp6aDcXsKcFXqWHItRnHSmD7HeEVvuSHjOhiHQOXfECNfoSpk8OxE mkJauQR3a+L3J0+LrGDuRROTPG3STNsF5GqgACYRrvG4DmQmIACnD51YknaKKAJkqhOO +CftpbHmDQC2eoC9EWBvA43icXVBRQri3pQNeRwgyHF6m8rPO8NCiANJ2VYAzcmB160y c+iaawBz0NU47oahLdwhN5eh04k3x643M8tNp3g5czuo5hQxlbyJ25CIYD/rMGPDqyll uXxg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bgdev-pl.20230601.gappssmtp.com header.s=20230601 header.b=1zTRCSLg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id o1-20020a170902d4c100b001cc53d110a6si1300790plg.481.2023.11.30.05.47.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 05:47:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@bgdev-pl.20230601.gappssmtp.com header.s=20230601 header.b=1zTRCSLg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 727E5802887B; Thu, 30 Nov 2023 05:47:27 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345897AbjK3NrJ (ORCPT + 99 others); Thu, 30 Nov 2023 08:47:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345581AbjK3Nql (ORCPT ); Thu, 30 Nov 2023 08:46:41 -0500 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D7DDE1715 for ; Thu, 30 Nov 2023 05:46:47 -0800 (PST) Received: by mail-wm1-x32a.google.com with SMTP id 5b1f17b1804b1-40b4c2ef584so7730645e9.3 for ; Thu, 30 Nov 2023 05:46:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1701352006; x=1701956806; 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=5JkcbylM7pAQh/R7rY3XvCoFJsyCRp+V27URZHzTJKg=; b=1zTRCSLgTdXz21Tn6xXOIRLL1FQTefqdWPzzetBIrrXOHVet7/oNgmIzaIBVxl3qBX Kj3oL0NiDJNwESGPukPK2xH/icTMM5wWoiB0atmwi8clfbVprTI0VtgWUXqodHjwjap2 xqF2Z2UHBkeb5N/DnI+zWuIhfBZQJXjJF9UXDRkFmW5tPBYAzKRVwuEroL9OucU8WCuS mxtcE23Qnwj2fOktg+5XCBwhmsTc5qAwOUOJkOVQiWRzyMNJhO6J5D222fzlW7NIarg+ n922xRYJYaRlAAR0IXKfVyXrgEdBx49HrnBTFkY206UW9UB2Ae8BvCmtSBK3w3B/ppsR CfCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701352006; x=1701956806; 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=5JkcbylM7pAQh/R7rY3XvCoFJsyCRp+V27URZHzTJKg=; b=Es2PD6iBlPHmsuo9tV4rgvKBq6JnbnyKx5dYuXfcNZH5OhTAnOYeBfd22u6PP0xF0A a+jKtdG1u/5tbQ/ZxNlSVN5qvTyrhqPCreMDxJS6a+TlNaudH/T/xy8B7Cbb8p0fptZ6 9DV0CSy5OLgqxYnobshOOEp4Tpq6c4D37+1CXCQFYhy//Gsz9kX3zZKuhUp9kU4Cxhvd Ub5rmVX5aHvy7acuRnzQ2OwW4LuzsJbwE7SGfl6eXVo94yMM3BM5za+O6pJR9oPrYkzu Fyijk9OONKozWu+tTc1Nlz7qgaIQ8IK65hnPrt2MgEkV62IW4csm+gnHQEmrkR4aEib5 r1OQ== X-Gm-Message-State: AOJu0Yz3IdCw3+2ltW0+eHok6BzcVEaQmwjnVR4S9cY1UrcFuaXjt9Ih InNBYSB3Tb4/Qxer0vjJKnQ0XNJqqkp7NXZn5Xs= X-Received: by 2002:a5d:4c84:0:b0:32f:b407:5b77 with SMTP id z4-20020a5d4c84000000b0032fb4075b77mr15296007wrs.64.1701352006413; Thu, 30 Nov 2023 05:46:46 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:ae84:904:6602:ec1e]) by smtp.gmail.com with ESMTPSA id n18-20020adffe12000000b0032d2489a399sm1574824wrr.49.2023.11.30.05.46.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 05:46:45 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Andy Shevchenko Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 09/10] gpiolib: use gpiochip_dup_line_label() in for_each helpers Date: Thu, 30 Nov 2023 14:46:29 +0100 Message-Id: <20231130134630.18198-10-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231130134630.18198-1-brgl@bgdev.pl> References: <20231130134630.18198-1-brgl@bgdev.pl> MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE, 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 lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Thu, 30 Nov 2023 05:47:27 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783996926391160268 X-GMAIL-MSGID: 1783996926391160268 From: Bartosz Golaszewski Rework for_each_requested_gpio_in_range() to use the new helper to retrieve a dynamically allocated copy of the descriptor label and free it at the end of each iteration. We need to leverage the CLASS()' destructor to make sure that the label is freed even when breaking out of the loop. Signed-off-by: Bartosz Golaszewski --- include/linux/gpio/driver.h | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/include/linux/gpio/driver.h b/include/linux/gpio/driver.h index 9796a34e2fee..b1ed501e9ee0 100644 --- a/include/linux/gpio/driver.h +++ b/include/linux/gpio/driver.h @@ -534,17 +534,36 @@ struct gpio_chip { const char *gpiochip_is_requested(struct gpio_chip *gc, unsigned int offset); char *gpiochip_dup_line_label(struct gpio_chip *gc, unsigned int offset); + +struct _gpiochip_for_each_data { + const char **label; + int *i; +}; + +DEFINE_CLASS(_gpiochip_for_each_data, + struct _gpiochip_for_each_data, + if (*_T.label) kfree(*_T.label), + ({ struct _gpiochip_for_each_data _data = { label, i }; + *_data.i = 0; + _data; }), + const char **label, int *i) + /** * for_each_requested_gpio_in_range - iterates over requested GPIOs in a given range - * @chip: the chip to query - * @i: loop variable - * @base: first GPIO in the range - * @size: amount of GPIOs to check starting from @base - * @label: label of current GPIO + * @_chip: the chip to query + * @_i: loop variable + * @_base: first GPIO in the range + * @_size: amount of GPIOs to check starting from @base + * @_label: label of current GPIO */ -#define for_each_requested_gpio_in_range(chip, i, base, size, label) \ - for (i = 0; i < size; i++) \ - if ((label = gpiochip_is_requested(chip, base + i)) == NULL) {} else +#define for_each_requested_gpio_in_range(_chip, _i, _base, _size, _label) \ + for (CLASS(_gpiochip_for_each_data, _data)(&_label, &_i); \ + *_data.i < _size; \ + (*_data.i)++, kfree(*(_data.label)), *_data.label = NULL) \ + if ((*_data.label = \ + gpiochip_dup_line_label(_chip, _base + *_data.i)) == NULL) {} \ + else if (IS_ERR(*_data.label)) {} \ + else /* Iterates over all requested GPIO of the given @chip */ #define for_each_requested_gpio(chip, i, label) \