Received: by 10.223.185.116 with SMTP id b49csp1205778wrg; Fri, 16 Feb 2018 14:33:36 -0800 (PST) X-Google-Smtp-Source: AH8x226jzocG08JXd+Oea2T4pvwiiJga7GXCUD8O5amHckqS9Xldm5nfRKSCmUUfqXFVhCCudDZh X-Received: by 10.98.214.6 with SMTP id r6mr7175577pfg.50.1518820416353; Fri, 16 Feb 2018 14:33:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518820416; cv=none; d=google.com; s=arc-20160816; b=djUrLeIDAj6Ox0VcV54CZ5VX8RDm96UKaH0sZWktdwux649aGMA038HJynWI7FD2U7 9q54EhZIh65wgwiU9cjgN3EO+u/0CMA5ipb4n58LbiZZvQ6xobEkspjGsRSkDoHGhbLG T+VmwEjgDxailp6HAewCFmBsVSr4b4v9ljvZEJGg+OyH3Bhk+AOjcIBzSa6MYEiaVc3U 9Hk90PSGvLXeosia1mkU81LxuyJcwzEx9Of+/cQL3ZfNyB9idLxD+goBUEF5IcRY4ezw nUwqIkafYeHuDeHrxE01WTullL3TRjfU1TQmbtMhfflGgv1eNK1MNJ5KEyVE/I9tfDkx 9vbw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature :arc-authentication-results; bh=YHPrrw+ROr1K0WGOM/Wg/D+AGHjZit1laBi7lU5yVI4=; b=jCIo39FVlo+U0zvuxa1jpSZFw41N+BkUC+oILyFmL+MnFwkwYw86ejy9AwmCcYowJT LvIEYgXLuZaOtriqqoUOuulol4gnbBV82+JobLiXjteFZOIIk+wfYMFUkbwQ3U5MkicH aMCoV/wFjPrBQ8n8MnuMq4ykl2FCCcgyFe1OEzyi6lYSOgI2lQloS0T33doNc7/EpuP5 DuTcI2QezoghAze48zh8tfRCum8EVgGJQ+cVI0vFd+PfZD2gbtagFuvnwVxAnE+rwepc FCr+lyvQUoJs94IL8Df3OQzeE08ofkz1l3dWol2vcJK8gtNpnsfGoYgnGmTLICdsSIEE fFTQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=JV7mAFU6; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c7-v6si3660793plo.743.2018.02.16.14.33.22; Fri, 16 Feb 2018 14:33:36 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=JV7mAFU6; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750946AbeBPWci (ORCPT + 99 others); Fri, 16 Feb 2018 17:32:38 -0500 Received: from mail-pg0-f66.google.com ([74.125.83.66]:45636 "EHLO mail-pg0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750917AbeBPWcg (ORCPT ); Fri, 16 Feb 2018 17:32:36 -0500 Received: by mail-pg0-f66.google.com with SMTP id e11so3486195pgq.12; Fri, 16 Feb 2018 14:32:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=YHPrrw+ROr1K0WGOM/Wg/D+AGHjZit1laBi7lU5yVI4=; b=JV7mAFU69GTxJ4wQuY8fhMAgpcSquKYahifG7g0/U2xMiFL1auWVKjlV1Ue4LKbRkx AKroB/v/tWBpdJVo7ACs/4lwSOFCFDAkT2C/+r4aj9WVAWvY6kU5RQmqMKQCa2Xddkr0 B9tRjUJvRZnzM1V7hDkdHv02n4kA1Dt3DShTr1xbrHBckQ11j2StalyilgVuGlkkCXUB MIOz8bLvXwmHTy4nhE7bLg4SStBZtg3QLxBRWOwVi1g+2r5wXfGyaU3youLYWqO1vHjh /ynRvhTgD0AaI4IxU24WcmPzq7JFih3nI1v1ylJeTIHmstK4X/lADHHrP5DJrwvjTQpF 3ZnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=YHPrrw+ROr1K0WGOM/Wg/D+AGHjZit1laBi7lU5yVI4=; b=kS0uyrRcpENPp1hlYL3PpDazfdtsA/8yJCt0TOUTtcmYJt2IEHUy1ML5vdCOQKxfqM SzAZAQ8Sgglb8EqzR+Zc2wcQYSlusscdDxWFv8V+/z1DW/BUI+b5mJwOH9SULWct0bab r0O+ZB0aPzETbv2/XHNIGwIpGxe1VsUphB4c4sPyJ5/K3M9Yebvgq/YASYu9truC3Swu r9ClZtE/j4jKmXHAVluzyUzEQiD28z+VcNzMs6SCoRp53yBs5q5lbulsmKAKDaNcVCXo pTRBuP5LeQcBVmM6oq8isdcX7vH4DGSJiIFHlUeoXuVjdoY+ZJ8toMcYXkWGZHurLG6i I4aQ== X-Gm-Message-State: APf1xPC+x0vmD3EcOqwkv1kZB8zBCHpk3i5LZWSnRIzlpVVgE3ahi7Xt sppZMBZ7tDrxHbh3t11B4JuJQrq3 X-Received: by 10.98.185.24 with SMTP id z24mr7439876pfe.185.1518820355639; Fri, 16 Feb 2018 14:32:35 -0800 (PST) Received: from [192.168.1.70] (c-73-93-215-6.hsd1.ca.comcast.net. [73.93.215.6]) by smtp.gmail.com with ESMTPSA id f80sm54159996pff.81.2018.02.16.14.32.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 16 Feb 2018 14:32:35 -0800 (PST) Subject: Re: [PATCH] of: add early boot allocation of of_find_node_by_phandle() cache To: Chintan Pandya , Rob Herring Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org References: <1518655465-10759-1-git-send-email-frowand.list@gmail.com> <25deaf48-6590-4ca2-e0db-adaef1f49c5d@codeaurora.org> From: Frank Rowand Message-ID: <1284eceb-cb00-29ea-cfec-8dbe810bba6f@gmail.com> Date: Fri, 16 Feb 2018 14:32:34 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: <25deaf48-6590-4ca2-e0db-adaef1f49c5d@codeaurora.org> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 02/16/18 01:07, Chintan Pandya wrote: > > > On 2/15/2018 6:14 AM, frowand.list@gmail.com wrote: >> From: Frank Rowand >> >> The initial implementation of the of_find_node_by_phandle() cache >> allocates the cache using kcalloc().  Add an early boot allocation >> of the cache so it will be usable during early boot.  Switch over >> to the kcalloc() based cache once normal memory allocation >> becomes available. >> >> Signed-off-by: Frank Rowand >> --- >> >> This patch is optional, to be added at Rob's discretion.  The >> extra complexity is not as much as I had feared, but the boot >> speed up is also likely small. >> >>   drivers/of/base.c       | 33 +++++++++++++++++++++++++++++++++ >>   drivers/of/fdt.c        |  2 ++ >>   drivers/of/of_private.h |  2 ++ >>   3 files changed, 37 insertions(+) >> >> diff --git a/drivers/of/base.c b/drivers/of/base.c >> index ab545dfa9173..d7b1ff1209e8 100644 >> --- a/drivers/of/base.c >> +++ b/drivers/of/base.c >> @@ -16,9 +16,11 @@ >>     #define pr_fmt(fmt)    "OF: " fmt >>   +#include >>   #include >>   #include >>   #include >> +#include >>   #include >>   #include >>   #include >> @@ -131,6 +133,29 @@ static void of_populate_phandle_cache(void) >>       raw_spin_unlock_irqrestore(&devtree_lock, flags); >>   } >>   +void __init of_populate_phandle_cache_early(void) >> +{ >> +    u32 cache_entries; >> +    struct device_node *np; >> +    u32 phandles = 0; >> +    size_t size; >> + >> +    for_each_of_allnodes(np) >> +        if (np->phandle && np->phandle != OF_PHANDLE_ILLEGAL) >> +            phandles++; >> + >> +    cache_entries = roundup_pow_of_two(phandles); >> +    phandle_cache_mask = cache_entries - 1; >> + >> +    size = cache_entries * sizeof(*phandle_cache); >> +    phandle_cache = memblock_virt_alloc(size, 4); >> +    memset(phandle_cache, 0, size); >> + >> +    for_each_of_allnodes(np) >> +        if (np->phandle && np->phandle != OF_PHANDLE_ILLEGAL) >> +            phandle_cache[np->phandle & phandle_cache_mask] = np; >> +} > > There is a lot of code duplication in this function with > of_populate_phandle_cache. Would you think of taking out > common code or differ the function with extra bool parameter > to say 'early' or 'not early'. Good observation, and normally yes. My first implementation of this feature actually did what you suggest. It turns out to be a bit more complicated than one might expect because some of the code is marked __init. That results in passing the memory allocation function as a parameter to of_populate_phandle_cache(). See __unflatten_device_tree() for an example of what this entails. Then the parts of of_populate_cache() that need to be encapsulated in a 'if (!early)' test are scattered throughout the function, so the test becomes rather intrusive in terms of code readability. In the end, the method I chose results in cleaner code for of_populate_phandle_cache(), plus the memory used by of_populate_cache_early() gets reclaimed after boot, since it is marked __init. Both functions are relatively small and the code common to both is unlikely to be modified, so I do not see this causing a maintenance burden. -Frank >> + >>   #ifndef CONFIG_MODULES >>   static int __init of_free_phandle_cache(void) >>   { >> @@ -150,7 +175,15 @@ static int __init of_free_phandle_cache(void) >>     void __init of_core_init(void) >>   { >> +    unsigned long flags; >>       struct device_node *np; >> +    phys_addr_t size; >> + >> +    raw_spin_lock_irqsave(&devtree_lock, flags); >> +    size = (phandle_cache_mask + 1) * sizeof(*phandle_cache); >> +    memblock_free(__pa(phandle_cache), size); >> +    phandle_cache = NULL; >> +    raw_spin_unlock_irqrestore(&devtree_lock, flags); >>         of_populate_phandle_cache(); >>   diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c >> index 84aa9d676375..cb320df23f26 100644 >> --- a/drivers/of/fdt.c >> +++ b/drivers/of/fdt.c >> @@ -1264,6 +1264,8 @@ void __init unflatten_device_tree(void) >>       of_alias_scan(early_init_dt_alloc_memory_arch); >>         unittest_unflatten_overlay_base(); >> + >> +    of_populate_phandle_cache_early(); >>   } >>     /** >> diff --git a/drivers/of/of_private.h b/drivers/of/of_private.h >> index fa70650136b4..6720448c84cc 100644 >> --- a/drivers/of/of_private.h >> +++ b/drivers/of/of_private.h >> @@ -134,6 +134,8 @@ extern void __of_sysfs_remove_bin_file(struct device_node *np, >>   /* illegal phandle value (set when unresolved) */ >>   #define OF_PHANDLE_ILLEGAL    0xdeadbeef >>   +extern void __init of_populate_phandle_cache_early(void); >> + >>   /* iterators for transactions, used for overlays */ >>   /* forward iterator */ >>   #define for_each_transaction_entry(_oft, _te) \ >> > > Chintan