From patchwork Wed Feb 21 19:36:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Shevchenko X-Patchwork-Id: 204469 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp1322838dyc; Wed, 21 Feb 2024 14:05:18 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX3EDUxe5Nxing0611fhd9B6pg/W8Py9dxGcOtI6C8qodLprP+rzp8KkgOwYoUPFu6CCVAYA/8X9ptcfKU45MV797ZImg== X-Google-Smtp-Source: AGHT+IHDHL7THVaMOb1Z5hxy9Ou4VUptsI99wx2kCZ4QOdFsdrkY0pHa73khuoJkD1MZAeGWF0gy X-Received: by 2002:a9d:6383:0:b0:6e2:eb55:f063 with SMTP id w3-20020a9d6383000000b006e2eb55f063mr20913050otk.27.1708553117857; Wed, 21 Feb 2024 14:05:17 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708553117; cv=pass; d=google.com; s=arc-20160816; b=xKLBRt3wQyWo//6+MflbUKguVmnkAKrBOmMUCbD6oL5CDi11/lCa5Ow+BRG/2hjv56 2W3QR1K2/elp7nBiwqSI3Y1CYD+82oblHj/uAU4k7hrgzsNBEMD+K7sqxDI6Jz30adTw MZvSH4nPp6FdavYYay4pEtzJf+OkDWWwxGqHiJbCaR0ntSwIBv9YWQ+Bdd6SHAVHwY7e SvNyezASobNXfWiMwBe+rbkIfmRiJ3Sbt+3JiNM/7VHCewARd9sF0ZWks8LlrE1MdAtp UuzLtoyTmMoLKzPOKpGLtX7S9fZpSN8C42TL11eGWNahdfDeCQALCN2S0P6nQWW9x0+v edHQ== ARC-Message-Signature: i=2; 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:message-id:date:subject:cc:to :from:dkim-signature; bh=A0JesZy62rAkIlxH0kpDfsUdgh1XlTf3kmjf7x1RPe4=; fh=NPMp9GFIdVjeGPEuneJtItyZfXCAUr5mEOBv8M3Hyn8=; b=lINNqC56fJPNiGk633H1FPr778Ef51og+GmsUNNpkw8oyyhfdeRNKCnM+boioxQuAo UKS65dX549rzrazVsclQgt5+8Jc8CHIq+auDH4GoIb0BOMmQUeOM2EV9k7SmMvSDuCDE pgoQ0Lupc9AbVCJnWPYXEZbW6SYtr89C/ffdJHrdaqleYvx3suzXZ57E/6pe1sxNYvoe 15n8CgYNhsX6ycSOfpMetseAkak5TuMJ32WHSFL7gVmcRLePXQrI1ccePBOFXWMjqE5u 0pFg/nfdE01lXqmbhI8XYssUfUvHjuP8ThICRmDGETVaqlDXOokAfa33kWTUMf22VUUo u51w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=LkWCgejR; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-75376-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75376-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id o6-20020ac85546000000b0042dac5fe341si11727568qtr.210.2024.02.21.14.05.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 14:05:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-75376-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=@intel.com header.s=Intel header.b=LkWCgejR; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-75376-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75376-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com 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 602151C240E1 for ; Wed, 21 Feb 2024 19:37:30 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 11CA186AD4; Wed, 21 Feb 2024 19:36:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="LkWCgejR" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C126A82D9B; Wed, 21 Feb 2024 19:36:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708544214; cv=none; b=jrwzNC7Fy+eu48926a9s6nGhRayt+fZozqFI5oNHvykbC6tQgJxYTuzuPwRfAF7LHqR/A+6Sbfesf8mIG3qPLx/MoU/k5Ul8sWdWi+RziChP+/OoT0xQCsiWAEb8znmcG7gFDVz5PgAi8b01TuGAS1dgwH9HRYBKgB4nMbJt4TU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708544214; c=relaxed/simple; bh=zLZx7XTpA+GzodcHyHuHe6VoqkIpFjTFDogIv3yWIms=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=Z27ZWhjCMCyeDYlkETBcbd2rYtOpA2P9Z9uzLA/Lb1EtcIgI500oMBeQWtAriyEuJSCkTwKI0z40KDM6YHLhT6fKVWYdyYr1e0Giwm8L0CixWOY+UNHfO0nZ0m9QpltTyb8/C8BMFfjUC0Cc11uHoc/LuJDB717b7KuutspsjKw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=LkWCgejR; arc=none smtp.client-ip=198.175.65.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708544213; x=1740080213; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=zLZx7XTpA+GzodcHyHuHe6VoqkIpFjTFDogIv3yWIms=; b=LkWCgejRcOvcsttvIO3x8uG0/rTiNq4RhP3GuPwx67v0Kxu5zyVqx9th ohjxoclGkwdfcYCvfIMZYbJtFoLcYoBIjFxY2aLqcuHb6vqcZ3JCNwhWy auC4Lm+PlnOcXWxdsEpfBmc6uUJ1mFaCCH52IxlMmfSH7wSVC3n2LTLrY 9OKSdxh+IaMRTYon9j2OfwUy+qs2kp08GpPtma1d0MoVKsJ1Y8/ySSirv 93MNG2lAlkppR6+b2LV5MmO0FAoHkkoTRlC/W+LQ8EPb8mX6HJ3vJXDeh 8EXIf/d/S/kwwqi7ebS2d1HQTVb+BrFTt6Z4F/6pTzZ1jYTboSp79hyaf w==; X-IronPort-AV: E=McAfee;i="6600,9927,10991"; a="25192459" X-IronPort-AV: E=Sophos;i="6.06,176,1705392000"; d="scan'208";a="25192459" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Feb 2024 11:36:52 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10991"; a="936693497" X-IronPort-AV: E=Sophos;i="6.06,176,1705392000"; d="scan'208";a="936693497" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga001.fm.intel.com with ESMTP; 21 Feb 2024 11:36:49 -0800 Received: by black.fi.intel.com (Postfix, from userid 1003) id BDF941FD; Wed, 21 Feb 2024 21:36:48 +0200 (EET) From: Andy Shevchenko To: Bartosz Golaszewski , Linus Walleij , linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Bartosz Golaszewski , Andy Shevchenko Subject: [PATCH v1 1/1] gpiolib: Deduplicate cleanup for-loop in gpiochip_add_data_with_key() Date: Wed, 21 Feb 2024 21:36:47 +0200 Message-ID: <20240221193647.13777-1-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.43.0.rc1.1.gbec44491f096 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: 1791547794120596828 X-GMAIL-MSGID: 1791547794120596828 There is no need to repeat for-loop twice in the error path in gpiochip_add_data_with_key(). Deduplicate it. While at it, rename loop variable to be more specific and avoid ambguity. It also properly unwinds the SRCU, i.e. in reversed order of allocating. Signed-off-by: Andy Shevchenko --- drivers/gpio/gpiolib.c | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 1706edb3ee3f..60fa7816c799 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -861,7 +861,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, struct lock_class_key *request_key) { struct gpio_device *gdev; - unsigned int i, j; + unsigned int desc_index; int base = 0; int ret = 0; @@ -965,8 +965,8 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, } } - for (i = 0; i < gc->ngpio; i++) - gdev->descs[i].gdev = gdev; + for (desc_index = 0; desc_index < gc->ngpio; desc_index++) + gdev->descs[desc_index].gdev = gdev; BLOCKING_INIT_NOTIFIER_HEAD(&gdev->line_state_notifier); BLOCKING_INIT_NOTIFIER_HEAD(&gdev->device_notifier); @@ -992,19 +992,16 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, if (ret) goto err_cleanup_gdev_srcu; - for (i = 0; i < gc->ngpio; i++) { - struct gpio_desc *desc = &gdev->descs[i]; + for (desc_index = 0; desc_index < gc->ngpio; desc_index++) { + struct gpio_desc *desc = &gdev->descs[desc_index]; ret = init_srcu_struct(&desc->srcu); - if (ret) { - for (j = 0; j < i; j++) - cleanup_srcu_struct(&gdev->descs[j].srcu); - goto err_free_gpiochip_mask; - } + if (ret) + goto err_cleanup_desc_srcu; - if (gc->get_direction && gpiochip_line_is_valid(gc, i)) { + if (gc->get_direction && gpiochip_line_is_valid(gc, desc_index)) { assign_bit(FLAG_IS_OUT, - &desc->flags, !gc->get_direction(gc, i)); + &desc->flags, !gc->get_direction(gc, desc_index)); } else { assign_bit(FLAG_IS_OUT, &desc->flags, !gc->direction_input); @@ -1061,9 +1058,8 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, gpiochip_free_hogs(gc); of_gpiochip_remove(gc); err_cleanup_desc_srcu: - for (i = 0; i < gdev->ngpio; i++) - cleanup_srcu_struct(&gdev->descs[i].srcu); -err_free_gpiochip_mask: + while (desc_index--) + cleanup_srcu_struct(&gdev->descs[desc_index].srcu); gpiochip_free_valid_mask(gc); err_cleanup_gdev_srcu: cleanup_srcu_struct(&gdev->srcu);