Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp2588760rdb; Mon, 4 Dec 2023 01:36:42 -0800 (PST) X-Google-Smtp-Source: AGHT+IG3I+cWvhQkjWbCbtdhvK3dxdWKTQISt90CL4qaZ1pBKSKLdMd3Hg4DR+/SU3dSEfvtDtx0 X-Received: by 2002:a05:6358:6583:b0:170:2421:fa03 with SMTP id x3-20020a056358658300b001702421fa03mr2219813rwh.41.1701682602101; Mon, 04 Dec 2023 01:36:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701682602; cv=none; d=google.com; s=arc-20160816; b=qC3KmOVY8DLy9iLFmPB1NqTnWRBixw49vX1Bs4qCx6helnmbOxgkmE1swyYwr/6LVQ 89NXmM1djuYUdRZwOwZFffl0+xiftu3KjUQm7EVzPqC08PmLUridnOTMlzi6hgqyICnB 9WWo3ZIVSSLC296A+nGCplVBUuWGMHNljljyLb8YwaKcoRbdsfXVZzXl0w2/W3ZZzuYs i+oxcEO0guY9CFrCVHPrpcIiVd8OZoAdA43J/dNdPclgwK8fv7K+RS1G7eNfI0GLt3OK N7QhhneqFIyTqgsGuFjyIdvPH2EHH2a1ASBL+vCQIVt70qd/HHvJsGMQfXjD/MXsxYRX jZgA== 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 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id do11-20020a056a004a0b00b006cd853221easi7180376pfb.362.2023.12.04.01.36.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 01:36:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; 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 2620:137:e000::3:3 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 6325A807D0E7; Mon, 4 Dec 2023 01:36:39 -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 Content-Transfer-Encoding: 8bit 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:39 -0800 (PST) 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) \ -- 2.40.1