From patchwork Tue Mar 21 13:54:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Shevchenko X-Patchwork-Id: 72882 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp1798160wrt; Tue, 21 Mar 2023 07:07:59 -0700 (PDT) X-Google-Smtp-Source: AK7set95qnatIssKFwY2KLc7C97d8drWyS88hlv+RTa3DHsXto9pzBSyVYuhtoGyNCxFEgHTNiN7 X-Received: by 2002:a05:6e02:582:b0:323:669:16ac with SMTP id c2-20020a056e02058200b00323066916acmr1221070ils.12.1679407679532; Tue, 21 Mar 2023 07:07:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679407679; cv=none; d=google.com; s=arc-20160816; b=kyzTmuH/AYbD+vad4ZXAdZGsblmTv5htKAT1jOaLxcaSSG1cC/4ikYIO5V7bg7oUAm 1v6iicitHslCFpd97BDoPdwQHCGz5MzsA0Xzmtm0QIm/fusLiWLmBiJDE/zGEn7fE0pY c25lZjQD+nZLstd25OPV5jR0AOjslNdW1GtBJ3MmiwE/Ri1X2vvZWtRXS6uWdqMgF5Hq upelXMRhhaYIA1itQJCHgUzQRtzlfch58hnFNKHxgsxUDOiMt704+W9GFRHFK1QH00u+ l4y10I0IQsia4jLNtAPVlM4iTbvUSbzeSeh2O4JwBf+WagK4uvYeYme8lDHlRVWRJOym 1TSg== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=vSjDAh3q2WruyZduUpxoK8o8vRLljyQMSLGzSFzRl/s=; b=h8ar1fvd7tAP8NU2yuus5WTOa2dOnMAFtE2hdSn/yi2HoZCNc2cU9xBFG1Pmq0uN8D ztxfIR02sFTnT+3pWfq4okqqOzCr3eqOdqi9PM/1gb35Myy/MseTHO8MN4/hS3Ja91ZU 3dE9stzQ424OSm2DK5unYPzLofA0M/MOhH+nqbOEFeRNiz/PFxEQdDonmWjAO5NAnHNR IpSqbmpOS63y3S7HR1Vz79Y7pXCN9TR+D4m9ONcOWoHZH2T9y+D0CMM9VtOW/tErsxeJ uD6zwmmo7Quj0U/rqXOBSF05wZrsf7Ev8iYv+aooPEtWINTYq60WD8qTeCqAbZpSLKZu D5pA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=A9ObHXqc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o1-20020a027401000000b00406302063bcsi2382981jac.147.2023.03.21.07.07.45; Tue, 21 Mar 2023 07:07:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=A9ObHXqc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229964AbjCUNyK (ORCPT + 99 others); Tue, 21 Mar 2023 09:54:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231341AbjCUNyA (ORCPT ); Tue, 21 Mar 2023 09:54:00 -0400 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A1EC713DDE; Tue, 21 Mar 2023 06:53:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1679406816; x=1710942816; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=DfICKX1W2R7vQASH04NFqYi2PpmnHCZONn2UQmTBkkU=; b=A9ObHXqcAW2mIxUyil8fJqiVSD9F1K2VrHAIPQEFekUCPEvksduv6HCy 6r+sRBRODrSyF9gg9tBZIV7xniOgMNh3yIPr7ySOGHnRoNfzZ1TteW3vh KOTdMUZhvJOSbBNuoiJGWsusd3dmf+cQ5XlLn+qVexxqEOErh1Ec/2qd+ H6v7CIbQWtiuk2CYiK6/iu/sH9HDs+/vcj1CVmtFFlqCl1M+bVv1/Lkb6 STbTNN6OPSYsc78eBH2MUaQ2tWYqLeJkOOsP4sNEknG7BhaZo7lVHWLt3 QHxjBOEcC920WUEYqiQTATtghclseffUBlWe2ilSYzIBBrrMOFK9RUOe+ g==; X-IronPort-AV: E=McAfee;i="6600,9927,10656"; a="319333151" X-IronPort-AV: E=Sophos;i="5.98,279,1673942400"; d="scan'208";a="319333151" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Mar 2023 06:53:31 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10656"; a="713999589" X-IronPort-AV: E=Sophos;i="5.98,279,1673942400"; d="scan'208";a="713999589" Received: from black.fi.intel.com ([10.237.72.28]) by orsmga001.jf.intel.com with ESMTP; 21 Mar 2023 06:53:29 -0700 Received: by black.fi.intel.com (Postfix, from userid 1003) id F39203DD; Tue, 21 Mar 2023 15:54:15 +0200 (EET) From: Andy Shevchenko To: Andy Shevchenko , Bartosz Golaszewski , linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Linus Walleij , Bartosz Golaszewski Subject: [PATCH v1 1/2] gpiolib: Replace open coded krealloc() Date: Tue, 21 Mar 2023 15:54:11 +0200 Message-Id: <20230321135412.73240-1-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.40.0.1.gaa8946217a0b MIME-Version: 1.0 X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE,URIBL_BLOCKED autolearn=ham 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-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760986587217738140?= X-GMAIL-MSGID: =?utf-8?q?1760986587217738140?= gpiod_get_array() does a new allocation in some cases, followed by copying previously allocated placeholder for the descriptors. Replace that with krealloc(__GFP_ZERO), since it was kzalloc() originally. Signed-off-by: Andy Shevchenko Reviewed-by: Linus Walleij --- drivers/gpio/gpiolib.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 112d99a5eec4..3e94990f1f90 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -4283,12 +4283,14 @@ struct gpio_descs *__must_check gpiod_get_array(struct device *dev, struct gpio_array *array_info = NULL; struct gpio_chip *gc; int count, bitmap_size; + size_t descs_size; count = gpiod_count(dev, con_id); if (count < 0) return ERR_PTR(count); - descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL); + descs_size = struct_size(descs, desc, count); + descs = kzalloc(descs_size, GFP_KERNEL); if (!descs) return ERR_PTR(-ENOMEM); @@ -4312,20 +4314,17 @@ struct gpio_descs *__must_check gpiod_get_array(struct device *dev, bitmap_size = BITS_TO_LONGS(gc->ngpio > count ? gc->ngpio : count); - array = kzalloc(struct_size(descs, desc, count) + - struct_size(array_info, invert_mask, - 3 * bitmap_size), GFP_KERNEL); + array = krealloc(descs, descs_size + + struct_size(array_info, invert_mask, 3 * bitmap_size), + GFP_KERNEL | __GFP_ZERO); if (!array) { gpiod_put_array(descs); return ERR_PTR(-ENOMEM); } - memcpy(array, descs, - struct_size(descs, desc, descs->ndescs + 1)); - kfree(descs); - descs = array; - array_info = (void *)(descs->desc + count); + + array_info = (void *)descs + descs_size; array_info->get_mask = array_info->invert_mask + bitmap_size; array_info->set_mask = array_info->get_mask + From patchwork Tue Mar 21 13:54:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Shevchenko X-Patchwork-Id: 72898 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp1808388wrt; Tue, 21 Mar 2023 07:24:21 -0700 (PDT) X-Google-Smtp-Source: AK7set+69h2IwqWvgw05HFVblVDHLxHBhtMTZSFqReidWO+IZIEr3dedqwFIRzspZwnlrwaUNu00 X-Received: by 2002:a17:90a:3ea7:b0:23d:3761:6085 with SMTP id k36-20020a17090a3ea700b0023d37616085mr2899407pjc.34.1679408661395; Tue, 21 Mar 2023 07:24:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679408661; cv=none; d=google.com; s=arc-20160816; b=U7YNQYk2PyQ0qXfc1W1mrs1f08Nn3KTDdCj6BXXMA+pU4hXI/QRtc0bJN2xOQWJvZp 1sGbzJkK9sS1Fqw8tLLBfbLkEFESipLYKzYJSreeyV4/Fy1rHGRbuMwDXQ4IFKVr6Gj8 1Bstfrjep0JDRZlBNbRxqfwkCfn3vvrLe2vU+NTBY4NGWvKcLNOUmW/n9ebBM8UH/wqw 0O+1BrWUgpTYKOCbfXJP4yiMftUV4CMtvA8aVMm8FKUgJ2Bf4u2k88F34E8zMYtY9AME v0kl9v8rhXTwFZ8alrQhlqLghXHjH0HAF06bus8I6Z1KtQRCyQxzLtYjmS1B7nBasF6P Wx/g== 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=kAupJJU0uIZog4YlWSvDAKYE3uQy/ar+njTLxczARXw=; b=d/geA+mcMrpiFgwrwg/xszSJsfmPKRDXPrSHqE9gSZiQXOhKs/Dmb2SWZ5aj6IbSOf poQJ13TJgv+9lbPkIjkH7vs2VZ21kOKPbhLJ/khiCOchm0TdptQJVlzFAgQbpyrLtbo4 5kLoWIHCJeykALhWuXV1QJT9EbX9FbHRLCeZUhyQK+SDVsPE26Xf6+m+TYmO7LU1EN91 KSVo9TvIF6dZGwj5s9MC7o5oqjRfI2ElUvsTEz0/Xdvj8MOaaZ/uo83bFpjpjSAQGhqL PJ5TjZiCdtv1YEO2KHGZdLskzsmmF/qNiwNLtCNUAEsIYKs5f9Fdq+SqsSeW4Cb9kCqD VJzA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=CMFrIPzf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v6-20020a17090abb8600b00233b583bf5fsi18935864pjr.74.2023.03.21.07.23.48; Tue, 21 Mar 2023 07:24:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=CMFrIPzf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231219AbjCUNy0 (ORCPT + 99 others); Tue, 21 Mar 2023 09:54:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51484 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231341AbjCUNyV (ORCPT ); Tue, 21 Mar 2023 09:54:21 -0400 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B34094FCD8; Tue, 21 Mar 2023 06:53:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1679406839; x=1710942839; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=0ghqHSYYR9HkUFWf0Y7Mfj7c4+87GevTnsySVxln5WI=; b=CMFrIPzf6Inu55SPI4rE+3V4VJ4WEZ0+CQcPYObqyQRXGwGGFY3459LY 6DIM9PNUoxL9r3/N1qDNMKGSi4mRI/hFowN7kscvwj/YbUvF5lQyw4Cjz rbPbSxHNJSUmfSR3f1sIU0Xuo7NatJnTVOYkA9R4hl9qSy/fQpcw4LvO7 7X7oiGgM9+G0fs4xwG8UY137PJYDFHD2DsOFDBv8TPpFBbaLW5rWc6omw vvDBufdG++ei1fpYs4sPQEI7kfgxe3p2ySwY44J6fAsMV7lIQ8zt0K0nD zOe5fAjT5+BEUsIDmaysRuc52ELR4OLO0VIGSmk5ndG4Q8LYrREy9wKeo w==; X-IronPort-AV: E=McAfee;i="6600,9927,10656"; a="319333155" X-IronPort-AV: E=Sophos;i="5.98,279,1673942400"; d="scan'208";a="319333155" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Mar 2023 06:53:32 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10656"; a="713999591" X-IronPort-AV: E=Sophos;i="5.98,279,1673942400"; d="scan'208";a="713999591" Received: from black.fi.intel.com ([10.237.72.28]) by orsmga001.jf.intel.com with ESMTP; 21 Mar 2023 06:53:30 -0700 Received: by black.fi.intel.com (Postfix, from userid 1003) id 028C64FF; Tue, 21 Mar 2023 15:54:16 +0200 (EET) From: Andy Shevchenko To: Andy Shevchenko , Bartosz Golaszewski , linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Linus Walleij , Bartosz Golaszewski Subject: [PATCH v1 2/2] gpiolib: Check array_info for NULL only once in gpiod_get_array() Date: Tue, 21 Mar 2023 15:54:12 +0200 Message-Id: <20230321135412.73240-2-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.40.0.1.gaa8946217a0b In-Reply-To: <20230321135412.73240-1-andriy.shevchenko@linux.intel.com> References: <20230321135412.73240-1-andriy.shevchenko@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE,URIBL_BLOCKED autolearn=ham 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-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760987616555776712?= X-GMAIL-MSGID: =?utf-8?q?1760987616555776712?= gpiod_get_array() has a long if-else-if branching where each of them tests for the same variable to be not NULL. Instead, check for NULL before even going to that flow. Signed-off-by: Andy Shevchenko Reviewed-by: Linus Walleij --- drivers/gpio/gpiolib.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 3e94990f1f90..ee24428c5fe3 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -4294,7 +4294,7 @@ struct gpio_descs *__must_check gpiod_get_array(struct device *dev, if (!descs) return ERR_PTR(-ENOMEM); - for (descs->ndescs = 0; descs->ndescs < count; ) { + for (descs->ndescs = 0; descs->ndescs < count; descs->ndescs++) { desc = gpiod_get_index(dev, con_id, descs->ndescs, flags); if (IS_ERR(desc)) { gpiod_put_array(descs); @@ -4339,8 +4339,13 @@ struct gpio_descs *__must_check gpiod_get_array(struct device *dev, count - descs->ndescs); descs->info = array_info; } + + /* If there is no cache for fast bitmap processing path, continue */ + if (!array_info) + continue; + /* Unmark array members which don't belong to the 'fast' chip */ - if (array_info && array_info->chip != gc) { + if (array_info->chip != gc) { __clear_bit(descs->ndescs, array_info->get_mask); __clear_bit(descs->ndescs, array_info->set_mask); } @@ -4348,8 +4353,7 @@ struct gpio_descs *__must_check gpiod_get_array(struct device *dev, * Detect array members which belong to the 'fast' chip * but their pins are not in hardware order. */ - else if (array_info && - gpio_chip_hwgpio(desc) != descs->ndescs) { + else if (gpio_chip_hwgpio(desc) != descs->ndescs) { /* * Don't use fast path if all array members processed so * far belong to the same chip as this one but its pin @@ -4363,7 +4367,7 @@ struct gpio_descs *__must_check gpiod_get_array(struct device *dev, __clear_bit(descs->ndescs, array_info->set_mask); } - } else if (array_info) { + } else { /* Exclude open drain or open source from fast output */ if (gpiochip_line_is_open_drain(gc, descs->ndescs) || gpiochip_line_is_open_source(gc, descs->ndescs)) @@ -4374,8 +4378,6 @@ struct gpio_descs *__must_check gpiod_get_array(struct device *dev, __set_bit(descs->ndescs, array_info->invert_mask); } - - descs->ndescs++; } if (array_info) dev_dbg(dev,