Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp6738672ioo; Thu, 2 Jun 2022 12:37:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx6tGslv2Gm51NpeRrJ04iJhx0gnt1JmLHjKjoRIeEdeWbjfkdtpEr+UiwnnHqgVKU2B4Pt X-Received: by 2002:a17:907:1c8a:b0:6e9:2a0d:d7b7 with SMTP id nb10-20020a1709071c8a00b006e92a0dd7b7mr5546373ejc.572.1654198637531; Thu, 02 Jun 2022 12:37:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654198637; cv=none; d=google.com; s=arc-20160816; b=lbksUcFi55nVEHigzYkcUudRO74zrX6OOuK3wCCM323LySAKpFkp6r13l3USbAjnjg ECxTBwLaYx52Mz2L5yYJ2AieyiwFUAg2G7RcgbCznhpv2r/0xL+vIIuq2kivMcWqnJUM k5Qv6/TDxFgBqkks342VUOQddSSQKgTl6TKPPS4PeR1iWi5Im7BuOqbwcRKvPAvgSFfH EnBLBrXy3ZoFeMiCSEMmgWjNt8YT96ggDenBicyUoyUKv3ThGuEcauoJ1euhTwZIZaic tvw97y9SvLgT3K7aOuMeiaW/Df4aUtzlTdK3pdVquvHNkBzzT4knL4US2/orU0wwqBjm e4zA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=07IS+Ue1DZu8UUgpAQL1++aymIiMdieShQwu8QDu6l0=; b=NHN1RV/+YhujYWHnkDeW8HI4Xi5J2wsqDQi3y4OGQzhrD189SmMKEnQs31sgrfKQHv po87ehaYAKH0B7UYJndLxJbcBc0Rwbxm+c6dgN6RM+daGGH/TxX+f48hI3p5FGtgOCcN d6jNkJ9+19entydTokpHMTNtlPZHDFCXFInRjvKNGXgVEBJKoRXmEOwLF146jVv/FIHt Z/SUHldzsMxqqaSI0z9QRec4p03M2dUS5PYry1xfTVyLmAVYsQu0KXq4BGuOSgNVZ6Bc iFZQsW29U3QNjN8TLxhp8w4+/mIM857zuHu2FhReDudUCJVUGKz3mzGCq5RwvxCUHbz5 yQDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=aBKDyTmM; 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=ibm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s18-20020a170906169200b006e7ee1a3502si5690093ejd.955.2022.06.02.12.36.51; Thu, 02 Jun 2022 12:37:17 -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=@ibm.com header.s=pp1 header.b=aBKDyTmM; 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=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237839AbiFBSIX (ORCPT + 99 others); Thu, 2 Jun 2022 14:08:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237103AbiFBSIT (ORCPT ); Thu, 2 Jun 2022 14:08:19 -0400 Received: from mx0a-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BB7FF26ADD; Thu, 2 Jun 2022 11:08:18 -0700 (PDT) Received: from pps.filterd (m0098420.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 252H59Lf000971; Thu, 2 Jun 2022 18:07:45 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=message-id : date : mime-version : subject : to : cc : references : from : in-reply-to : content-type : content-transfer-encoding; s=pp1; bh=07IS+Ue1DZu8UUgpAQL1++aymIiMdieShQwu8QDu6l0=; b=aBKDyTmMvOkD6ecwQRiesNIhLLxlyRiydyWOYBYmOE/RptBN+b8CuuAXTPYZUPUatTI0 xO/AiXAx/FMZzOoQFRWJ8JCQ59A1k6nKNxvpPIcK6lAgrfT1arL6PXMNhQZl7ebaiJzQ 8oxrL3egyDwhd9RvmT9EY1QfMZxFm5TcRxU80JrL9LGJH8/amqliO7nNs1SujTxp364M 2x/i4Wu1qoYHaKJz+5pdAy0dbk9NogBveq/W0nCQa8I1AiCVQOb93qHadntdZpApTrFh RvgetF7kCNoa/6aHS+QeO+f4pULvZua1lg0MvztS2PkG9/3xGF3TEP88B+VyehbAwvVF sw== Received: from pps.reinject (localhost [127.0.0.1]) by mx0b-001b2d01.pphosted.com (PPS) with ESMTPS id 3geusrra0t-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 02 Jun 2022 18:07:45 +0000 Received: from m0098420.ppops.net (m0098420.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 252Hwaui015157; Thu, 2 Jun 2022 18:07:45 GMT Received: from ppma02dal.us.ibm.com (a.bd.3ea9.ip4.static.sl-reverse.com [169.62.189.10]) by mx0b-001b2d01.pphosted.com (PPS) with ESMTPS id 3geusrra0b-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 02 Jun 2022 18:07:44 +0000 Received: from pps.filterd (ppma02dal.us.ibm.com [127.0.0.1]) by ppma02dal.us.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 252I4tU3001456; Thu, 2 Jun 2022 18:07:44 GMT Received: from b03cxnp08026.gho.boulder.ibm.com (b03cxnp08026.gho.boulder.ibm.com [9.17.130.18]) by ppma02dal.us.ibm.com with ESMTP id 3gd1adb22h-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 02 Jun 2022 18:07:43 +0000 Received: from b03ledav001.gho.boulder.ibm.com (b03ledav001.gho.boulder.ibm.com [9.17.130.232]) by b03cxnp08026.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 252I7f9723265626 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 2 Jun 2022 18:07:41 GMT Received: from b03ledav001.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id B78946E054; Thu, 2 Jun 2022 18:07:41 +0000 (GMT) Received: from b03ledav001.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 4636E6E052; Thu, 2 Jun 2022 18:07:37 +0000 (GMT) Received: from [9.160.56.145] (unknown [9.160.56.145]) by b03ledav001.gho.boulder.ibm.com (Postfix) with ESMTP; Thu, 2 Jun 2022 18:07:36 +0000 (GMT) Message-ID: Date: Thu, 2 Jun 2022 11:07:36 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.0 Subject: Re: [PATCH 1/3] of: dynamic: add of_property_alloc() and of_property_free() Content-Language: en-US To: Rob Herring Cc: =?UTF-8?B?Q2zDqW1lbnQgTMOpZ2Vy?= , Nathan Lynch , devicetree@vger.kernel.org, Ohhoon Kwon , David Hildenbrand , Steen Hegelund , Daniel Henrique Barboza , YueHaibing , linuxppc-dev , "linux-kernel@vger.kernel.org" , Paul Mackerras , "Aneesh Kumar K.V" , Thomas Petazzoni , Allan Nielsen , Andrew Morton , Laurent Dufour , Frank Rowand , Horatiu Vultur , David Gibson References: <20220504154033.750511-1-clement.leger@bootlin.com> <20220504154033.750511-2-clement.leger@bootlin.com> <42d9e1af-5576-ed8a-be3a-9dfea6ce1041@linux.ibm.com> From: Tyrel Datwyler In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: PET37afaEK9l44XPbLVjYo5IR_r_Q2Fo X-Proofpoint-GUID: b1VYbZAWTfivozu5KPIqRBRPnCUsSsr1 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.874,Hydra:6.0.517,FMLib:17.11.64.514 definitions=2022-06-02_05,2022-06-02_01,2022-02-23_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 mlxscore=0 mlxlogscore=999 priorityscore=1501 suspectscore=0 malwarescore=0 phishscore=0 impostorscore=0 clxscore=1015 adultscore=0 spamscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2204290000 definitions=main-2206020076 X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE 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 On 6/2/22 07:06, Rob Herring wrote: > On Wed, Jun 1, 2022 at 5:31 PM Tyrel Datwyler wrote: >> >> On 5/5/22 12:37, Rob Herring wrote: >>> On Wed, May 04, 2022 at 05:40:31PM +0200, Clément Léger wrote: >>>> Add function which allows to dynamically allocate and free properties. >>>> Use this function internally for all code that used the same logic >>>> (mainly __of_prop_dup()). >>>> >>>> Signed-off-by: Clément Léger >>>> --- >>>> drivers/of/dynamic.c | 101 ++++++++++++++++++++++++++++++------------- >>>> include/linux/of.h | 16 +++++++ >>>> 2 files changed, 88 insertions(+), 29 deletions(-) >>>> >>>> diff --git a/drivers/of/dynamic.c b/drivers/of/dynamic.c >>>> index cd3821a6444f..e8700e509d2e 100644 >>>> --- a/drivers/of/dynamic.c >>>> +++ b/drivers/of/dynamic.c >>>> @@ -313,9 +313,7 @@ static void property_list_free(struct property *prop_list) >>>> >>>> for (prop = prop_list; prop != NULL; prop = next) { >>>> next = prop->next; >>>> - kfree(prop->name); >>>> - kfree(prop->value); >>>> - kfree(prop); >>>> + of_property_free(prop); >>>> } >>>> } >>>> >>>> @@ -367,48 +365,95 @@ void of_node_release(struct kobject *kobj) >>>> } >>>> >>>> /** >>>> - * __of_prop_dup - Copy a property dynamically. >>>> - * @prop: Property to copy >>>> + * of_property_free - Free a property allocated dynamically. >>>> + * @prop: Property to be freed >>>> + */ >>>> +void of_property_free(const struct property *prop) >>>> +{ >>>> + kfree(prop->value); >>>> + kfree(prop->name); >>>> + kfree(prop); >>>> +} >>>> +EXPORT_SYMBOL(of_property_free); >>>> + >>>> +/** >>>> + * of_property_alloc - Allocate a property dynamically. >>>> + * @name: Name of the new property >>>> + * @value: Value that will be copied into the new property value >>>> + * @value_len: length of @value to be copied into the new property value >>>> + * @len: Length of new property value, must be greater than @value_len >>> >>> What's the usecase for the lengths being different? That doesn't seem >>> like a common case, so perhaps handle it with a NULL value and >>> non-zero length. Then the caller has to deal with populating >>> prop->value. >>> >>>> * @allocflags: Allocation flags (typically pass GFP_KERNEL) >>>> * >>>> - * Copy a property by dynamically allocating the memory of both the >>>> + * Create a property by dynamically allocating the memory of both the >>>> * property structure and the property name & contents. The property's >>>> * flags have the OF_DYNAMIC bit set so that we can differentiate between >>>> * dynamically allocated properties and not. >>>> * >>>> * Return: The newly allocated property or NULL on out of memory error. >>>> */ >>>> -struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags) >>>> +struct property *of_property_alloc(const char *name, const void *value, >>>> + int value_len, int len, gfp_t allocflags) >>>> { >>>> - struct property *new; >>>> + int alloc_len = len; >>>> + struct property *prop; >>>> + >>>> + if (len < value_len) >>>> + return NULL; >>>> >>>> - new = kzalloc(sizeof(*new), allocflags); >>>> - if (!new) >>>> + prop = kzalloc(sizeof(*prop), allocflags); >>>> + if (!prop) >>>> return NULL; >>>> >>>> + prop->name = kstrdup(name, allocflags); >>>> + if (!prop->name) >>>> + goto out_err; >>>> + >>>> /* >>>> - * NOTE: There is no check for zero length value. >>>> - * In case of a boolean property, this will allocate a value >>>> - * of zero bytes. We do this to work around the use >>>> - * of of_get_property() calls on boolean values. >>>> + * Even if the property has no value, it must be set to a >>>> + * non-null value since of_get_property() is used to check >>>> + * some values that might or not have a values (ranges for >>>> + * instance). Moreover, when the node is released, prop->value >>>> + * is kfreed so the memory must come from kmalloc. >>> >>> Allowing for NULL value didn't turn out well... >>> >>> We know that we can do the kfree because OF_DYNAMIC is set IIRC... >>> >>> If we do 1 allocation for prop and value, then we can test >>> for "prop->value == prop + 1" to determine if we need to free or not. >> >> If its a single allocation do we even need a test? Doesn't kfree(prop) take care >> of the property and the trailing memory allocated for the value? > > Yes, it does when it's a single alloc, but it's testing for when > prop->value is not a single allocation because we could have either. > Ok, that is the part I was missing. Thanks for the clarification. -Tyrel