From patchwork Mon Jan 11 14:35:13 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bamvor Zhang Jian X-Patchwork-Id: 59548 Delivered-To: patch@linaro.org Received: by 10.112.130.2 with SMTP id oa2csp2148506lbb; Mon, 11 Jan 2016 06:36:23 -0800 (PST) X-Received: by 10.98.7.156 with SMTP id 28mr26614130pfh.49.1452522983103; Mon, 11 Jan 2016 06:36:23 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v78si28933401pfa.239.2016.01.11.06.36.22; Mon, 11 Jan 2016 06:36:23 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-gpio-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-gpio-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-gpio-owner@vger.kernel.org; dkim=neutral (body hash did not verify) header.i=@linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1759803AbcAKOgW (ORCPT + 4 others); Mon, 11 Jan 2016 09:36:22 -0500 Received: from mail-pf0-f171.google.com ([209.85.192.171]:36183 "EHLO mail-pf0-f171.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1759004AbcAKOgV (ORCPT ); Mon, 11 Jan 2016 09:36:21 -0500 Received: by mail-pf0-f171.google.com with SMTP id n128so45640696pfn.3 for ; Mon, 11 Jan 2016 06:36:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=wibba4L5sxDXNeyEKmdA3EjlUmreKJRrWFBniD/TVN4=; b=DvIZR3x7TTd7+cZIxOdwkjDd/qSHQqdEliQ3T4ykg+d1dbStqLfLfb4q9UYMEu4cpI fQhSp62jd1anCAVGD+geniDjihGtRYYX9+CQauypR8ElOBHDILxHymLEQfmuiQA4hogF 3FH/dP2wQZFRH7zu3W103HUsA+y6S51MxIhfc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=wibba4L5sxDXNeyEKmdA3EjlUmreKJRrWFBniD/TVN4=; b=ag4NGt/5OM5tDL755In5DEZfA4RxInKh64bgaNF6PtGV/9QQwoB0x5m3y8x6EwRNOM V9E1QWRRhsB0aIIRAAyJBWCEhEDQSmXVdygfMWTsrJWIxcXJF8AOyTXg8NP92xaW011m tSK5a3d8uy26J3akPXAw2L0XEXs76QSgIePToG2vU68MjPT2r4qul3dzOwJC84HtFwDQ QW7Us+4ROM/Ra5WLLAoR7vIyGq2cTR0AnrsOo7HKMr+hDLo+6bqy10Jtp87vru2NmFOJ eZNdPxovaAEJbXkbXn4nxKArxuzey3PB0bvU2xrtPNlhlPEgocj002+OwUvrKe1F5Qli NprA== X-Gm-Message-State: ALoCoQmHdxOPcQM/6sSXyQC0JFoWzZpl5kTBhLRViJmay5wF4cwMaq8yFr8pp5e2in8QRlO+f1pOrlOlD6wjVUYW+7Y5081LNg== X-Received: by 10.98.7.156 with SMTP id 28mr26613905pfh.49.1452522981241; Mon, 11 Jan 2016 06:36:21 -0800 (PST) Received: from linux-j170.lan ([103.61.136.139]) by smtp.gmail.com with ESMTPSA id o75sm23856685pfi.17.2016.01.11.06.35.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 11 Jan 2016 06:36:20 -0800 (PST) From: Bamvor Jian Zhang To: julien.grossholtz@savoirfairelinux.com Cc: linux-gpio@vger.kernel.org, linus.walleij@linaro.org, broonie@kernel.org, kernel@savoirfairelinux.com, arnd@arndb.de, bamvor.zhangjian@linaro.org Subject: Re: [Kernel] [PATCH] gpiolib: rewrite gpiochip_add_to_list Date: Mon, 11 Jan 2016 22:35:13 +0800 Message-Id: <1452522913-6298-1-git-send-email-bamvor.zhangjian@linaro.org> X-Mailer: git-send-email 2.1.4 In-Reply-To: References: Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Hi, Julien On 01/09/2016 11:16 PM, Bamvor Zhang Jian wrote: > Hi, Julien > > On 01/09/2016 06:56 AM, Julien Grossholtz wrote: >> Hi, >> >> The issue is in the break condition of the list_for_each_entry_safe loop. >> If there is a chip at the beginning it breaks even if there is another >> one after. You should check the next chip: >> if (chip->base > prev->base && chip->base + chip->ngpio <= next->base) > Thanks you suggestion. It indeed fix the issue you mentioned. > I will send the new version after I make sure there is no other corner case. Could you do me favor to test the following patches? It works for the following ranges: [0,31], [32,64], [-1,32], [-1, 32] and the other test cases I wrote. Regards Bamvor --- drivers/gpio/gpiolib.c | 65 ++++++++++++++++++++++---------------------------- 1 file changed, 28 insertions(+), 37 deletions(-) -- 2.1.4 -- To unsubscribe from this list: send the line "unsubscribe linux-gpio" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 3db34e7..9460f94 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -189,55 +189,46 @@ EXPORT_SYMBOL_GPL(gpiod_get_direction); */ static int gpiochip_add_to_list(struct gpio_chip *chip) { - struct gpio_chip *iterator; - struct gpio_chip *previous = NULL; + struct gpio_chip *prev, *next; if (list_empty(&gpio_chips)) { - list_add_tail(&chip->list, &gpio_chips); + /* initial entry in list */ + list_add(&chip->list, &gpio_chips); return 0; } - list_for_each_entry(iterator, &gpio_chips, list) { - if (iterator->base >= chip->base + chip->ngpio) { - /* - * Iterator is the first GPIO chip so there is no - * previous one - */ - if (!previous) { - goto found; - } else { - /* - * We found a valid range(means - * [base, base + ngpio - 1]) between previous - * and iterator chip. - */ - if (previous->base + previous->ngpio - <= chip->base) - goto found; - } - } - previous = iterator; + next = list_entry(gpio_chips.next, struct gpio_chip, list); + if (chip->base + chip->ngpio <= next->base) { + /* add before first entry */ + list_add(&chip->list, &gpio_chips); + return 0; } - /* - * We are beyond the last chip in the list and iterator now - * points to the head. - * Let iterator point to the last chip in the list. - */ + prev = list_entry(gpio_chips.prev, struct gpio_chip, list); + if (prev->base + prev->ngpio <= chip->base) { + /* add behind last entry */ + list_add_tail(&chip->list, &gpio_chips); + return 0; + } - iterator = list_last_entry(&gpio_chips, struct gpio_chip, list); - if (iterator->base + iterator->ngpio <= chip->base) - goto found; + list_for_each_entry_safe(prev, next, &gpio_chips, list) { + /* at the end of the list */ + if (&(next->list) == &gpio_chips) + break; - dev_err(chip->parent, - "GPIO integer space overlap, cannot add chip\n"); - return -EBUSY; + /* add between prev and next */ + if ( prev->base + prev->ngpio <= chip->base + && chip->base + chip->ngpio <= next->base) { + list_add(&chip->list, &prev->list); + return 0; + } + } -found: - list_add_tail(&chip->list, &iterator->list); - return 0; + dev_err(chip->parent, "GPIO integer space overlap, cannot add chip\n"); + return -EBUSY; } + /** * Convert a GPIO name to its descriptor */