From patchwork Mon Dec 4 09:35:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 173161 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp2647707vqy; Mon, 4 Dec 2023 01:36:38 -0800 (PST) X-Google-Smtp-Source: AGHT+IHKPSY2Zk0bzbP7Pt+Xuc2kzR9nT20W1zb6pCHGoQiH7Mf5S3Fift7+4f9b6mpZJIe4py/w X-Received: by 2002:a05:6e02:1a27:b0:35d:59a2:333c with SMTP id g7-20020a056e021a2700b0035d59a2333cmr5090765ile.64.1701682598278; Mon, 04 Dec 2023 01:36:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701682598; cv=none; d=google.com; s=arc-20160816; b=Y+wRMs8vSB82nC70fnwzxEn/5fMsI56NJnVKcWa0Uz1uu/IoK6RVpJq5XEQFc9b0HU F5PeSVrXqbCy+9CNDnE8X+HFZAJyfYRoJZmNYG/0ZVujz/KFD18RD2H1c8u4VlvTREZt kRZIWl/Qo8f+zTWej+V0rDtpQ5cKZ7piFQz6z0tP45UhdYLJZ8WTCbuCLfYmMEiBFp/5 pzQO3tFNndUiRSzeZDaPkIDA1lRT0cZWceFc7F6yOw0KlvEqOKrwMmHQuPgrGB5lCrRT Wpq2/C99m0r1Qgh8FhK5trDbF+uIoRs95gQta/Zlo21HG7u0JN2LTPErnQnZDHjaScQP kJ2w== 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=HFuMwgQtxRBsqcDwlkNhBzIxKsoLICNRiZ90QrUgNUQ=; fh=fw6biwzn34ngIUS3OuPB+D+TvAzoz1YwnvitHxA+Jyw=; b=Y6fNKY5Hr5QfF6Ssw8hYvcPlGRvHKdFPelUQO0bocEE0j/m0NsM2iEbdquQDuBM4v2 0Zfl/s2mx3cx5RpofRch4pN78xh9qyIsY1MVbmdE3TNyuLGrQOgZy4EW+IOIJJVX7TQN ce51+T2WopQvwi7If8VMhFfEzJXgqs6iy2ThijY643Gv2o9V7AJuegHHMwgDexTmSL/L 4gvijRB4WdVt/cP2+W3jt4Q9vsJW3REm5OUGdpxjZaD4iFaOLwHVJH7f947iv/kv2FUE zBP0tESm6p3yXizpvsSigjjr6y3VG6SHoUZw71vueW+T5tWYE1vSsSEdHeIZvy5djsIa EqLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bgdev-pl.20230601.gappssmtp.com header.s=20230601 header.b=TyF8oO8Z; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id j35-20020a632323000000b005b8ebaa2937si7336723pgj.47.2023.12.04.01.36.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 01:36:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@bgdev-pl.20230601.gappssmtp.com header.s=20230601 header.b=TyF8oO8Z; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (Postfix) with ESMTP id B74FD80963AE; Mon, 4 Dec 2023 01:36:33 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235293AbjLDJgK (ORCPT + 99 others); Mon, 4 Dec 2023 04:36:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52834 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235310AbjLDJfa (ORCPT ); Mon, 4 Dec 2023 04:35:30 -0500 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7001F198 for ; Mon, 4 Dec 2023 01:35:24 -0800 (PST) Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-40c09f4814eso12382065e9.1 for ; Mon, 04 Dec 2023 01:35:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1701682523; x=1702287323; 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=HFuMwgQtxRBsqcDwlkNhBzIxKsoLICNRiZ90QrUgNUQ=; b=TyF8oO8Zgq6hfxPV1cMsjP9afRhf0hgNUOLbU4Rvb3ogaJCuwo2tbg9pTt3vb/IebM AROZcQCsq3zKUJZqkJvA/fEaIxOXKJjcEY6T6tvQg4RfBxMMkWwYrkG/6Y2w7hbESEN7 su11gsvp3FgytTg3ae+qC+lzENKTTqmjusFlxmrleMq4o8cB5QyzJNBbwlgm/W/MK0bn YkdfqaTBpL+zVAZDHpZyRPa5Vtxy7cyP0s5W4jrtvB/m+dZavr+DjeLyyYPbbdryuhXr ycGSXUWyqOvVk/mjPpKxnge3EzO6KwRO60THue+HMQ5hInhfNoUIR38bRvw0C15EIHPk Th4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701682523; x=1702287323; 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=HFuMwgQtxRBsqcDwlkNhBzIxKsoLICNRiZ90QrUgNUQ=; b=WKHjY75RKiio7Sj0ZBXC6yVU2TA1XKlUeR5gWyHBUHWbE/Pt5VOS1//7BRscMnPqbX 7MG+SCJ9jutxdhLBz5k9YgjdN3Kp9nGedFdHiuyyHQBIUUntVoP+4G5gDsIVucjr3M52 SQaomlg058SBE7n6/x771yC0kz9mPLx0vJiHzXVSbsFora+PaeIQ+31TXytvAUGcvw0L a3s9qtILSsYFiq9lZrIDvhjo6Xk0Cml0dLxrj2a/wmd+yPQmHAeEHcUMN5hXD/Syb/vE LnlJUWQPNvkYGM77Cj67BQEltq3HuoMr6C9zHfOOTwZ0Kkpil9POk8HbuF3s/tmoIIK+ f5Tw== X-Gm-Message-State: AOJu0YxPCani+AFql6SeQNyLlgYMymhCb1RO5KOqEwH7Tq0ywDilYGgv myBoJYpPSTm1sGM1ewIMIh7r0I26YshUxFZNiS0= X-Received: by 2002:a7b:c397:0:b0:40b:5e21:dd1e with SMTP id s23-20020a7bc397000000b0040b5e21dd1emr1025046wmj.76.1701682522864; Mon, 04 Dec 2023 01:35:22 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:4cb1:229e:8c33:122a]) by smtp.gmail.com with ESMTPSA id fs16-20020a05600c3f9000b0040b48690c49sm14266219wmb.6.2023.12.04.01.35.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 01:35:22 -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 v3 09/10] gpiolib: use gpiochip_dup_line_label() in for_each helpers Date: Mon, 4 Dec 2023 10:35:08 +0100 Message-Id: <20231204093509.19225-10-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231204093509.19225-1-brgl@bgdev.pl> References: <20231204093509.19225-1-brgl@bgdev.pl> MIME-Version: 1.0 X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,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 lipwig.vger.email 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 (lipwig.vger.email [0.0.0.0]); Mon, 04 Dec 2023 01:36:33 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784343531825633601 X-GMAIL-MSGID: 1784343531825633601 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 | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/include/linux/gpio/driver.h b/include/linux/gpio/driver.h index 9796a34e2fee..910fc50d3ab5 100644 --- a/include/linux/gpio/driver.h +++ b/include/linux/gpio/driver.h @@ -534,17 +534,38 @@ 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; + unsigned 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) \