Received: by 2002:a89:413:0:b0:1fd:dba5:e537 with SMTP id m19csp1755432lqs; Sun, 16 Jun 2024 03:17:43 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXKiJv9ovKkVwMmflgH8fgpwMr3HrEMb4j5Ed4aitdTNRl4CpHCu+9XtMAGgHGPjPX81tw7AwKs+1Z9ECRecN1DVNdVgI8ubI4A6Dup9w== X-Google-Smtp-Source: AGHT+IFiQg7PXhJYKGwxNHDF3bhVLh6ep4BJpAzEV4Avouh1q6Lpg/FvzFV95O9VNSyhf5SequIb X-Received: by 2002:a17:906:f85b:b0:a6f:6126:18aa with SMTP id a640c23a62f3a-a6f61261e92mr494739666b.67.1718533063474; Sun, 16 Jun 2024 03:17:43 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718533063; cv=pass; d=google.com; s=arc-20160816; b=TYB/vbUEp/Gg0+FgoxVMT0nHkC8zhCe2CN1+ooQvvMNaj+H+JXf7i8ECbrZIjOMHKp XZ/DJJ6X/CEf2FgjXeeoriprV70iPLzWC+vVRCJJEjaWzlibGXZg+OurIeDrCv77W9Y3 KIR0dABuEdXQ5BVUJSGroL6olW3lC2MQ/gLLUuI9mO89fO6DnAEWbzMyQYzHnVBVvB2M P9w4PuouzU+lHFzoseRj13wFifYqXhn9JAGwRjoDecK6/7F8FunjEA+NQef+dD6yRS94 bwx5MZCjtI+YE8bPVDD1XLWdf1AyU+wpdHFYnrCHCG3OTp9eZcw8mBYhf+da3RtfDvMV f9KQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=p3rFYErsO758fuDsxjyIKiofnZDAYl4hPEb/1z+SDqA=; fh=9+bARruekkZWAeX1G+IMW1ZT1t8sj9/cfZjLNERDrPA=; b=WbFgC7GcQKJCPxUIkAXi2NziU+03y6+tlDWQfSN6EXo0S7Z9qI4XJh5YrIHZ8Zu5fh F0BZEifzzHdcES56RHMuDoAx/yaKTrksUo+/aKY7fTd7KLuAT3pdnTytoKMHFXW7AOgq 0WcreLgdM5oLMdetUaG+ZdEajy4SnGlKIqAW3Qce3SVVih2Z2K7HA3Fqwg0fTgZoqock y49g0HDz8wfAxuj2kC1hx29S9LXR5ZTWNsiqXT61pcmyurl0CSUqQ5Bs48juR/Sd8Uwq 797We+bBHbUhPPXayLZjVtUPJWxgHfEqYXW5kBku8rvZYv9Uv+JrVgxYyrg5TzeRpjZQ G2Og==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=LMc6TE1Q; arc=pass (i=1 dkim=pass dkdomain=linuxfoundation.org); spf=pass (google.com: domain of linux-kernel+bounces-216232-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-216232-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a640c23a62f3a-a6f56e3b34csi339715066b.867.2024.06.16.03.17.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Jun 2024 03:17:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-216232-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=LMc6TE1Q; arc=pass (i=1 dkim=pass dkdomain=linuxfoundation.org); spf=pass (google.com: domain of linux-kernel+bounces-216232-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-216232-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 2B2BB1F214D7 for ; Sun, 16 Jun 2024 10:17:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5432D186E44; Sun, 16 Jun 2024 10:17:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="LMc6TE1Q" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7A5D03A8CB for ; Sun, 16 Jun 2024 10:17:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718533056; cv=none; b=eVfMkMho4CIZOhx6wBTzDF5FKnyhRvYSdiSYpK/EkGi1Rpr6luc/62gQ0AympS7Go+PrgktnssuqyZnGRdz2SuDZ13lNUCcB9pHEQHtVoIzqGR/7w3fto//+8G9oSwxlUZMZcf3WQn/4Z2pKMFmGRzZKwdLDiQakefbKo6AergI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718533056; c=relaxed/simple; bh=bOEN/u7pYiKgtP4Y4pNIsuRPr7dd6tBtflnqUiQL3hk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=tNVJ71fhB04oWG635D8nKz666QDwZPuKqOXP1c37fdDEvBDATSu/DUkWvpPGdz7FVzvHVy3qCNH8Px3xZpHS1GjJ4HaExw7+Dk/C8OoND+hf/tUVR4WeJCOafPruB4kRO4aBwSxlXLQiYqCfKeTEkMG7rIDqz94Zxzj4+S0sAS0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=LMc6TE1Q; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8964DC2BBFC; Sun, 16 Jun 2024 10:17:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1718533055; bh=bOEN/u7pYiKgtP4Y4pNIsuRPr7dd6tBtflnqUiQL3hk=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=LMc6TE1QenstXtavqQ+fxCwuQsDp+FovTFs+JbtrjgGkGCoKlrGpcE+M3Rxh8NvgE diRMbIJ+9WMPHqbFLKEizojYMjXTmTkCxGdX7EI/n0zjlnV6r9b0jmWapIB5JrP658 VsvyX3E73lrt5gLBdKGWMILa5pa9m45ownVKGOrw= Date: Sun, 16 Jun 2024 12:17:33 +0200 From: Greg KH To: quic_zijuhu Cc: rafael@kernel.org, andriy.shevchenko@linux.intel.com, brgl@bgdev.pl, linux-kernel@vger.kernel.org Subject: Re: [PATCH v1] devres: Fix devm_krealloc() allocating memory with wrong size Message-ID: <2024061611-cyclist-hardiness-c15f@gregkh> References: <1718531655-29761-1-git-send-email-quic_zijuhu@quicinc.com> <2024061609-breezy-magnitude-8edf@gregkh> <4f691c9c-8882-4bd7-ba60-3fc665d0808c@quicinc.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4f691c9c-8882-4bd7-ba60-3fc665d0808c@quicinc.com> On Sun, Jun 16, 2024 at 06:10:02PM +0800, quic_zijuhu wrote: > On 6/16/2024 6:02 PM, Greg KH wrote: > > On Sun, Jun 16, 2024 at 05:54:15PM +0800, Zijun Hu wrote: > >> Kernel API devm_krealloc() calls alloc_dr() with wrong argument > >> @total_new_size, and it will cause more memory to be allocated > >> than required, fixed by using @new_size as alloc_dr()'s argument. > >> > >> Fixes: f82485722e5d ("devres: provide devm_krealloc()") > >> Signed-off-by: Zijun Hu > >> --- > >> drivers/base/devres.c | 2 +- > >> 1 file changed, 1 insertion(+), 1 deletion(-) > >> > >> diff --git a/drivers/base/devres.c b/drivers/base/devres.c > >> index 3df0025d12aa..1cf87ddf1f55 100644 > >> --- a/drivers/base/devres.c > >> +++ b/drivers/base/devres.c > >> @@ -898,7 +898,7 @@ void *devm_krealloc(struct device *dev, void *ptr, size_t new_size, gfp_t gfp) > >> * taking the lock as most probably the caller uses GFP_KERNEL. > >> */ > >> new_dr = alloc_dr(devm_kmalloc_release, > >> - total_new_size, gfp, dev_to_node(dev)); > >> + new_size, gfp, dev_to_node(dev)); > > > > Wait, are you sure? Did you properly test this? What is the actual > > savings and how do you handle the fact that you are now not allocating > > the space reserved for the devres structure itself? > > > sure even if i did not test it. > beware that alloc_dr() will call check_dr_size() to reserve space for > devres structure. > > it will reserve the devres structure two times if using wrong > @total_new_size. it is obvious wrong. Ah, I missed that, that is tricky. Please document this better when you resubmit a new version. And of course, please test it. thanks, greg k-h