Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp5162833imu; Wed, 19 Dec 2018 06:40:42 -0800 (PST) X-Google-Smtp-Source: AFSGD/WGz6znzN0rVGugiYjJZAKb0VfMdJBHVh92WczRshndB+T7UvKP0wx6uqKDZnV4znrjVOWh X-Received: by 2002:a62:848d:: with SMTP id k135mr20482720pfd.47.1545230442306; Wed, 19 Dec 2018 06:40:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545230441; cv=none; d=google.com; s=arc-20160816; b=tE+wPtI0S6kb2QhacM9p66XorzgAdSyf/P5m02kbObsH2QbIMhjSYKjFS2zlsjmNTs EqsayZi78mWXAZ6FwykitnuzIHRoH3+6BxlH1K9ftyDGWMzyUuHDvsBzJtMyryD2mG5A 79/TkoWAEGtVaN4rFDZj+z+cxzSjr8yIvTttDQJrFlIs2CggFZvj/AlpVZlvv2GSJkyH UBz1BvbxoNRz4o16l0BNrVbDne1/9KXrFqcMILLxbFoveAQwHJNQo1RGiK/MGaFknU5z YEoEJZ7tBhTn+qVCfmhxepJuUeILLaHcTMYryjwC488vwe2418CzmcNJEV/kNg6KXPfA 2IeQ== 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:mime-version :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id; bh=rZMSTz9/dOnFvQzAq/HDgIvQ2nC0rgx1Zjx4gRtvLts=; b=lhXRKor15SONpDVOv0edA1/6H7epNhimyvv2EQOYCVOt1r5aW6i7jLujEMyBtY4eWb VdG98icggb0sIaeoaxfaM/EcRXh9smsR4nALsZslMlJ8MPAy2arwNoUw1f8nRjwu31wH d1aP0qehQY24dVTR/KdO0Oe34210er8N8tS5FQfxfmdnftPr6rn2xkCzKEmg+ELvLw6j YmZEvKmANDy9wDBznD1Gtucgag2yEu4KX9nATTDqg7NJ6cbTbKjuXEkxvO49l8cG+Nhq Y7Aj4frM7J4So4qpRP4yOB+uH+B8ALMWnQNbPSw1K+v0pq7pIQ9OTq85LMWzTM4aLaIi 3D4Q== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b19si17447803pfm.100.2018.12.19.06.40.24; Wed, 19 Dec 2018 06:40:41 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729139AbeLSK5I (ORCPT + 99 others); Wed, 19 Dec 2018 05:57:08 -0500 Received: from smtprelay0098.hostedemail.com ([216.40.44.98]:58465 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726725AbeLSK5I (ORCPT ); Wed, 19 Dec 2018 05:57:08 -0500 Received: from filter.hostedemail.com (clb03-v110.bra.tucows.net [216.40.38.60]) by smtprelay04.hostedemail.com (Postfix) with ESMTP id B6432180A8850; Wed, 19 Dec 2018 10:57:06 +0000 (UTC) X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,joe@perches.com,:::::::::::,RULES_HIT:41:355:379:599:800:960:966:973:988:989:1260:1277:1311:1313:1314:1345:1359:1437:1515:1516:1518:1534:1542:1593:1594:1711:1730:1747:1777:1792:2194:2196:2199:2200:2393:2553:2559:2562:2828:2899:3138:3139:3140:3141:3142:3355:3622:3865:3866:3867:3870:3871:3872:3873:3874:4250:4321:4385:5007:7875:7903:8660:8957:9108:10004:10400:10848:11026:11232:11233:11473:11658:11914:12043:12296:12740:12760:12895:13148:13230:13439:14096:14097:14181:14659:14721:21080:21627:30054:30070:30090:30091,0,RBL:47.151.153.53:@perches.com:.lbl8.mailshell.net-62.8.0.100 64.201.201.201,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:28,LUA_SUMMARY:none X-HE-Tag: women89_3b8f63be8e5b X-Filterd-Recvd-Size: 3844 Received: from XPS-9350.home (unknown [47.151.153.53]) (Authenticated sender: joe@perches.com) by omf14.hostedemail.com (Postfix) with ESMTPA; Wed, 19 Dec 2018 10:57:05 +0000 (UTC) Message-ID: <6ef8274c8fc7adb7903cf3874fbab1098881ed4c.camel@perches.com> Subject: Re: [PATCH] cifs: check kzalloc return From: Joe Perches To: Nicholas Mc Guire Cc: Nicholas Mc Guire , Steve French , linux-cifs@vger.kernel.org, samba-technical@lists.samba.org, linux-kernel@vger.kernel.org Date: Wed, 19 Dec 2018 02:57:04 -0800 In-Reply-To: <20181219072250.GA18501@osadl.at> References: <1545150439-26055-1-git-send-email-hofrat@osadl.org> <4db484684a6e9b096c7fdf57673fd208d1c4005a.camel@perches.com> <20181219072250.GA18501@osadl.at> Content-Type: text/plain; charset="ISO-8859-1" User-Agent: Evolution 3.30.1-1build1 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 2018-12-19 at 08:22 +0100, Nicholas Mc Guire wrote: > On Tue, Dec 18, 2018 at 09:33:58AM -0800, Joe Perches wrote: > > On Tue, 2018-12-18 at 17:27 +0100, Nicholas Mc Guire wrote: > > > kzalloc can return NULL so a check is needed. While there is a > > > check for ret_buf there is no check for the allocation of > > > ret_buf->crfid.fid - this check is thus added. Both call-sites > > > of tconInfoAlloc() check for NULL return of tconInfoAlloc() > > > so returning NULL on failure of kzalloc() here seems appropriate. > > > As the kzalloc() is the only thing here that can fail it is > > > moved to the beginning so as not to initialize other resources > > > on failure of kzalloc. > > > > > Perhaps use a more common style by returning early on the > > first possible failure too so the block can be unindented. [] > > yup the restructured cleanup would be the better way to go > > rather than making this two patches I guess it would be the > best to just skip the intermediate kzalloc only cleanup - > atleast I see little value in that intermediat step - so > could you take care of the kzalloc() in your refactoring > please ? I did that in the patch I proposed, which is trivial. If you want to take it, please do. If you want a sign-off: Signed-off-by: Joe Perches > > diff --git a/fs/cifs/misc.c b/fs/cifs/misc.c [] > > @@ -111,21 +111,27 @@ struct cifs_tcon * > > tconInfoAlloc(void) > > { > > struct cifs_tcon *ret_buf; > > - ret_buf = kzalloc(sizeof(struct cifs_tcon), GFP_KERNEL); > > - if (ret_buf) { > > - atomic_inc(&tconInfoAllocCount); > > - ret_buf->tidStatus = CifsNew; > > - ++ret_buf->tc_count; > > - INIT_LIST_HEAD(&ret_buf->openFileList); > > - INIT_LIST_HEAD(&ret_buf->tcon_list); > > - spin_lock_init(&ret_buf->open_file_lock); > > - mutex_init(&ret_buf->crfid.fid_mutex); > > - ret_buf->crfid.fid = kzalloc(sizeof(struct cifs_fid), > > - GFP_KERNEL); > > - spin_lock_init(&ret_buf->stat_lock); > > - atomic_set(&ret_buf->num_local_opens, 0); > > - atomic_set(&ret_buf->num_remote_opens, 0); > > + > > + ret_buf = kzalloc(sizeof(*ret_buf), GFP_KERNEL); > > + if (!ret_buf) > > + return NULL; > > + ret_buf->crfid.fid = kzalloc(sizeof(*ret_buf->crfid.fid), GFP_KERNEL); > > + if (!ret_buf->crfid.fid) { > > + kfree(ret_buf); > > + return NULL; > > } > > + > > + atomic_inc(&tconInfoAllocCount); > > + ret_buf->tidStatus = CifsNew; > > + ++ret_buf->tc_count; > > + INIT_LIST_HEAD(&ret_buf->openFileList); > > + INIT_LIST_HEAD(&ret_buf->tcon_list); > > + spin_lock_init(&ret_buf->open_file_lock); > > + mutex_init(&ret_buf->crfid.fid_mutex); > > + spin_lock_init(&ret_buf->stat_lock); > > + atomic_set(&ret_buf->num_local_opens, 0); > > + atomic_set(&ret_buf->num_remote_opens, 0); > > + > > return ret_buf; > > } > >