From patchwork Wed Sep 20 07:32:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 142356 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp4050311vqi; Wed, 20 Sep 2023 04:10:46 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE53i4rd6xmqaUhc3v4wEY1LWiWAeyp5pGNFFiYeFmt5neNJF1bm8JhIhy6cuvqfRBny/Uo X-Received: by 2002:a05:6a21:1a0:b0:14c:d494:77d1 with SMTP id le32-20020a056a2101a000b0014cd49477d1mr2547981pzb.10.1695208246635; Wed, 20 Sep 2023 04:10:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695208246; cv=none; d=google.com; s=arc-20160816; b=UcRpBemjX0jmk0RZ2AqJcwRDxYgzRduUxS4x0W5XGWMczBqBXndTrhHgrUVn7xpDNs G/vQEMBpZ38bJra0NMSf2pSCZv1S6vsrZ0I/O4ZNiv10AYgEd82Okdf72ztUl9tOsrC7 LFR5zSno0SwnyJAiDZwvUF+4FLLhO4G3JMX3M0N1HsqbjIfzBsH+OEtLlQ5EbGrSFU6I 5IQ+53FMa1I59qsmd83/B6PgsoziEMKfE5wXwOKY02O0imGTcuK+BzoioC54Ho1d0VKj 5QIiYAYCRMzQjrVIsNZhPbih9fO8DameZD2+ejfHsVwPCV2TvkF9qmYY8Ca7vIKIzdWD QpPg== 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=KYrvQbB0FGaR2TMUy/U/vm4+WSiYScO9+N5v6KBchuQ=; fh=lCwMbqetBpO0Vd7a3gKFArRnLB2AASuTMycQRzK1uok=; b=p9+4h2hFpeky/plqyPKzWruig5rB1ASIDHpv9NAErH8oZ2PG/kCX8M5cdiaJ6sJCGo Zt6wRAxkbddOgVw1kkzBQhqyPi05JmLhFZH0z+9Z+yU2cVvN7HQeJ+rfbHgtNP8U+GWX pcVn7VUtlgqWpT3cBF4WrLIF2QkByazMD3UqBP+herYZeNF/C2010Kbb7DAmZYdmwYaK coYqPZjZLpUPJwk9x/AAV9Jwpv9GJojw4ZxtIfA4ZCpzjNHj0Fsp73mNItdjZOEg8woK jAC4Oi2PSiIxC7V6w/fEaOYFbiS+nK1O+i7NcRvYNd435KRjt/0coMfW7PNdgzomBoY1 whtA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bgdev-pl.20230601.gappssmtp.com header.s=20230601 header.b=KyREYUWE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id cw15-20020a056a00450f00b006688c47e5b4si11242368pfb.399.2023.09.20.04.10.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Sep 2023 04:10:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@bgdev-pl.20230601.gappssmtp.com header.s=20230601 header.b=KyREYUWE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 howler.vger.email (Postfix) with ESMTP id D4C108042AA2; Wed, 20 Sep 2023 00:33:13 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233774AbjITHdL (ORCPT + 26 others); Wed, 20 Sep 2023 03:33:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52782 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233727AbjITHdJ (ORCPT ); Wed, 20 Sep 2023 03:33:09 -0400 Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [IPv6:2a00:1450:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BB855CE for ; Wed, 20 Sep 2023 00:32:58 -0700 (PDT) Received: by mail-ej1-x630.google.com with SMTP id a640c23a62f3a-9aa0495f9cfso129398066b.1 for ; Wed, 20 Sep 2023 00:32:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1695195177; x=1695799977; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=KYrvQbB0FGaR2TMUy/U/vm4+WSiYScO9+N5v6KBchuQ=; b=KyREYUWECj3Z3ZaSBcaCno4kMnCH+FoyB3F+tjhrBOV47Cgd5d+ncxxjKQc/eHMGlh NVNF8KWyyf2dsMzOIlsdw97BcRvU//NUosTjIzvw77RJYSSeiv5e4mqOBnkXEfnNNrLb 3vWH97fvIcBLuCbJSuHgJ3qNb4yEb53xouxCXWLPiftIeGsUKFwq/jgY3DX11JdZdh3E XKIxHQR55UnVdMQHSuEMI0YMjAzEe5hLv7S1c3LpisGuJXtI2iPcuydo3OnMoY98Klfu duuBxfl5bSiTXyxqFimbXC4FGaOxLRqlPoiFWU4Pc5nFkcqb/ZF/oueI0e14k1xW1K7M 0qsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695195177; x=1695799977; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=KYrvQbB0FGaR2TMUy/U/vm4+WSiYScO9+N5v6KBchuQ=; b=nSKGJfrQuLws2rriLtH6BTnlJbwRjf6gLktEIuJJdLDbMMAyuE3ff+Ynl0ZIXhtCGI V/lrAh0PThFpGq6Kxb7Pv/Jq/mjiXQlqxn9vOZRYjFTnIOViWXQ8UnzN5wfOuaJyW1Y9 8SD7mDvgL88MKSwvZrLTwUJdHpaUq5Pttf7lSrpSxhK5ecHVzyHzgOKyB8lREcTnCI+3 F9d3+gxslHKe8pz6fFauBZfprm739MPK6Xwh9S7o7tLHyJfpmLGG+kURm5kdT6l38GDq FLB0aEojw+f4n7ZcUHpIc89xviR4eDQmMj1Hw/w77hW8gCl5YfkTO1jy0akEPG4MhEzD AyGg== X-Gm-Message-State: AOJu0YzP1vodo7xdgmxWBiUmt7yQxiDnXDDEh8NOpdhFbo3T1bDhiHdI 4m1qTGaeE72FcKS+fkEljQIN8w== X-Received: by 2002:a17:907:3ad1:b0:9ae:513f:a4f3 with SMTP id fi17-20020a1709073ad100b009ae513fa4f3mr196156ejc.32.1695195177081; Wed, 20 Sep 2023 00:32:57 -0700 (PDT) Received: from brgl-uxlite.. (static-212-193-78-212.thenetworkfactory.nl. [212.78.193.212]) by smtp.gmail.com with ESMTPSA id x26-20020a1709064a9a00b009a13fdc139fsm8819453eju.183.2023.09.20.00.32.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Sep 2023 00:32:56 -0700 (PDT) From: Bartosz Golaszewski To: Linus Walleij , Andy Shevchenko , Kent Gibson , Alexey Dobriyan , Peter Zijlstra , Linus Torvalds , akpm@linux-foundation.org Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v5] gpio: sim: fix an invalid __free() usage Date: Wed, 20 Sep 2023 09:32:53 +0200 Message-Id: <20230920073253.51742-1-brgl@bgdev.pl> X-Mailer: git-send-email 2.39.2 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 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Wed, 20 Sep 2023 00:33:14 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777554682544818262 X-GMAIL-MSGID: 1777554682544818262 From: Bartosz Golaszewski gpio_sim_make_line_names() returns NULL or ERR_PTR() so we must not use __free(kfree) on the returned address. Split this function into two, one that determines the size of the "gpio-line-names" array to allocate and one that actually sets the names at correct offsets. The allocation and assignment of the managed pointer happens in between. Fixes: 3faf89f27aab ("gpio: sim: simplify code with cleanup helpers") Reported-by: Alexey Dobriyan Closes: https://lore.kernel.org/all/07c32bf1-6c1a-49d9-b97d-f0ae4a2b42ab@p183/ Suggested-by: Linus Torvalds Signed-off-by: Bartosz Golaszewski --- Hopefully this is the last version of this patch. I restored the max() assignment from v3 but kept the code simpler than v2. Tested most corner cases that occurred to me. v4 -> v5: - restore checking for the higher offset in each iteration when counting named lines v3 -> v4: - simplify the line counting logic v2 -> v3: - restore the offset out-of-bounds checks v1 -> v2: - split the line name setting into two parts drivers/gpio/gpio-sim.c | 60 ++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 37 deletions(-) diff --git a/drivers/gpio/gpio-sim.c b/drivers/gpio/gpio-sim.c index 460389bb8e3f..3b7cdf44eb38 100644 --- a/drivers/gpio/gpio-sim.c +++ b/drivers/gpio/gpio-sim.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -718,52 +719,32 @@ gpio_sim_device_config_live_show(struct config_item *item, char *page) return sprintf(page, "%c\n", live ? '1' : '0'); } -static char **gpio_sim_make_line_names(struct gpio_sim_bank *bank, - unsigned int *line_names_size) +static unsigned int gpio_sim_get_line_names_size(struct gpio_sim_bank *bank) { - unsigned int max_offset = 0; - bool has_line_names = false; struct gpio_sim_line *line; - char **line_names; + unsigned int size = 0; list_for_each_entry(line, &bank->line_list, siblings) { - if (line->offset >= bank->num_lines) + if (!line->name || (line->offset >= bank->num_lines)) continue; - if (line->name) { - if (line->offset > max_offset) - max_offset = line->offset; - - /* - * max_offset can stay at 0 so it's not an indicator - * of whether line names were configured at all. - */ - has_line_names = true; - } + size = max(size, line->offset + 1); } - if (!has_line_names) - /* - * This is not an error - NULL means, there are no line - * names configured. - */ - return NULL; - - *line_names_size = max_offset + 1; + return size; +} - line_names = kcalloc(*line_names_size, sizeof(*line_names), GFP_KERNEL); - if (!line_names) - return ERR_PTR(-ENOMEM); +static void +gpio_sim_set_line_names(struct gpio_sim_bank *bank, char **line_names) +{ + struct gpio_sim_line *line; list_for_each_entry(line, &bank->line_list, siblings) { - if (line->offset >= bank->num_lines) + if (!line->name || (line->offset >= bank->num_lines)) continue; - if (line->name && (line->offset <= max_offset)) - line_names[line->offset] = line->name; + line_names[line->offset] = line->name; } - - return line_names; } static void gpio_sim_remove_hogs(struct gpio_sim_device *dev) @@ -867,7 +848,7 @@ gpio_sim_make_bank_swnode(struct gpio_sim_bank *bank, struct fwnode_handle *parent) { struct property_entry properties[GPIO_SIM_PROP_MAX]; - unsigned int prop_idx = 0, line_names_size = 0; + unsigned int prop_idx = 0, line_names_size; char **line_names __free(kfree) = NULL; memset(properties, 0, sizeof(properties)); @@ -878,14 +859,19 @@ gpio_sim_make_bank_swnode(struct gpio_sim_bank *bank, properties[prop_idx++] = PROPERTY_ENTRY_STRING("gpio-sim,label", bank->label); - line_names = gpio_sim_make_line_names(bank, &line_names_size); - if (IS_ERR(line_names)) - return ERR_CAST(line_names); + line_names_size = gpio_sim_get_line_names_size(bank); + if (line_names_size) { + line_names = kcalloc(line_names_size, sizeof(*line_names), + GFP_KERNEL); + if (!line_names) + return ERR_PTR(-ENOMEM); + + gpio_sim_set_line_names(bank, line_names); - if (line_names) properties[prop_idx++] = PROPERTY_ENTRY_STRING_ARRAY_LEN( "gpio-line-names", line_names, line_names_size); + } return fwnode_create_software_node(properties, parent); }