Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752434AbcLIV6D (ORCPT ); Fri, 9 Dec 2016 16:58:03 -0500 Received: from smtprelay0198.hostedemail.com ([216.40.44.198]:41529 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751121AbcLIV6C (ORCPT ); Fri, 9 Dec 2016 16:58:02 -0500 X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,joe@perches.com,:::::::::::,RULES_HIT:41:355:379:541:599:960:966:973:988:989:1042:1260:1277:1311:1313:1314:1345:1359:1373:1437:1515:1516:1518:1534:1543:1593:1594:1605:1711:1730:1747:1777:1792:2194:2196:2198:2199:2200:2201:2393:2553:2559:2562:2828:3138:3139:3140:3141:3142:3622:3865:3866:3867:3868:3870:3871:3872:3873:3874:4321:4385:4605:5007:6119:6691:7576:7875:7903:8660:10004:10400:10848:10967:11026:11232:11473:11658:11914:12043:12266:12295:12438:12555:12679:12740:12760:13138:13148:13230:13231:13439:14093:14096:14097:14659:14721:21080:21095:21212:21451:21524:30012:30041:30054:30070:30079:30090:30091,0,RBL:none,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:2,LUA_SUMMARY:none X-HE-Tag: coil66_62d743f4f0c18 X-Filterd-Recvd-Size: 4335 Message-ID: <1481320677.5946.44.camel@perches.com> Subject: Re: [PATCH] md: Combine two kmalloc() calls into one in sb_equal() From: Joe Perches To: Al Viro Cc: SF Markus Elfring , linux-raid@vger.kernel.org, Shaohua Li , LKML , kernel-janitors@vger.kernel.org Date: Fri, 09 Dec 2016 13:57:57 -0800 In-Reply-To: <20161209213030.GC1555@ZenIV.linux.org.uk> References: <1481310314.5946.40.camel@perches.com> <20161209213030.GC1555@ZenIV.linux.org.uk> Content-Type: text/plain; charset="ISO-8859-1" X-Mailer: Evolution 3.22.1-0ubuntu2 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3264 Lines: 94 On Fri, 2016-12-09 at 21:30 +0000, Al Viro wrote: > On Fri, Dec 09, 2016 at 11:05:14AM -0800, Joe Perches wrote: > > On Fri, 2016-12-09 at 19:30 +0100, SF Markus Elfring wrote: > > > From: Markus Elfring > > > Date: Fri, 9 Dec 2016 19:09:13 +0100 > > > > > > The function "kmalloc" was called in one case by the function "sb_equal" > > > without checking immediately if it failed. > > > This issue was detected by using the Coccinelle software. > > > > > > Perform the desired memory allocation (and release at the end) > > > by a single function call instead. > > > > > > Fixes: 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 ("Linux-2.6.12-rc2") > > > > Making a change does not mean fixes. > > > > There's nothing particularly _wrong_ with the code as-is. > > > > 2 kmemdup calls might make the code more obvious. > > > > There's a small optimization possible in that only the > > first MB_SB_GENERIC_CONSTANT_WORDS of the struct are > > actually compared. Alloc and copy of both entire structs > > is inefficient and unnecessary. > > > > Perhaps something like the below would be marginally > > better/faster, but the whole thing is dubious. > > > > static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) > > { > > int ret; > > void *tmp1, *tmp2; > > > > tmp1 = kmemdup(sb1, MD_SB_GENERIC_CONSTANT_WORDS * sizeof(__u32), GFP_KERNEL); > > tmp2 = kmemdup(sb2, MD_SB_GENERIC_CONSTANT_WORDS * sizeof(__u32), GFP_KERNEL); > > > > if (!tmp1 || !tmp2) { > > ret = 0; > > goto out; > > } > > > > /* > > * nr_disks is not constant > > */ > > ((mdp_super_t *)tmp1)->nr_disks = 0; > > ((mdp_super_t *)tmp2)->nr_disks = 0; > > > > ret = memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * sizeof(__u32)) == 0; > > > > out: > > kfree(tmp1); > > kfree(tmp2); > > return ret; > > } > > May I politely inquire if either of you has actually bothered to read the > code and figure out what it does? This is grotesque... > > For really slow: we have two objects. We want to check if anything in the > 128-byte chunks in their beginnings other than one 32bit field happens to be > different. For that we > * allocate two 128-byte pieces of memory > * *copy* our objects into those > * forcibly zero the field in question in both of those copies > * compare the fuckers > * free them > > And you two are discussing whether it's better to combine allocations of those > copies into a single 256-byte allocation? Really? No. May I suggest you read my suggestion? At no point did I suggest a single allocation. I think the single allocation is silly and just makes the code harder to read. > _IF_ it is a hot path, > the obvious optimization would be to avoid copying that crap in the first > place - simply by > return memcmp(sb1, sb2, offsetof(mdp_super_t, nr_disks)) || > memcmp(&sb1->nr_disks + 1, &sb2->nr_disks + 1, > MD_SB_GENERIC_CONSTANT_WORDS * sizeof(__u32) - > offsetof(mdp_super_t, nr_disks) - 4); That's all true, but Markus has enough trouble reading simple code without trying to explain to him what offsetof does. btw: the "- 4" should be " - sizeof(__u32)" just for consistency with the line above it. > If it is _not_ a hot path, why bother with it at all? exactly.