Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp3713980pxk; Mon, 7 Sep 2020 23:46:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwRkBpev9NlNBM6suzCOMvReh+FQVt3QIIas2zLE5lqPqPVKM6+TW0ymOAYK7LXfAD2X6dm X-Received: by 2002:a17:907:104c:: with SMTP id oy12mr24827383ejb.450.1599547593983; Mon, 07 Sep 2020 23:46:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1599547593; cv=none; d=google.com; s=arc-20160816; b=Tn7+iTU2iq69tjt2uVh/LGeQomJhDErrTSld4S8KbqrZ96Npl6IxTtdkHkcN053PXm KbAmeVbJGaXdEsSQyNVROh4T+vJDFiL7zAMUMeWhkQPOT+mX0GaDZNN1/xsPXVG31bzx cnfczBClsCgXi6RxKEhJAY2t7H1Oqyz0fhfv6BEe08p0bH/yAk7EOBT2OfSUjx4dx2PM 5nCuLqI484U7OnLRtUMOZFreq3C1HQSzhOYPV7ozR5EUhGKpVlP50/ykf+AqehQLVwye 5zpiUMnRJFoV5ULLyfmRSYcoDqfVwJq5N6Qy/zIc01ratEL4QOIPO8epZTUcYmux6Z/L UWNg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=TyNRBuWY59fAAW981frH1nJrr68oTQ2mzH+bkIc5r4E=; b=WQT9YTJTdTAq0pnkibiwxWrBmv2Y+scDpu7XqrrIzaQxmTVtuiyFTp3PB7lq9i2Uz9 lzAC7OYuFHar8DuBXHZQ2ZakXA0ptZQWhidrWm/BP3vjV5BnXiX13eEnMX59rbVYIcAz lAZon1sGXfU/5mob1YHdymTJ8IJcQoPpe7JScvCN0zAN5f5BtsEIueMbe33WmkIe68ah gew+BFxgnSKnY3AuRVzWKXTCUbubZQCQZlpAuUkmoHz+HHEchqEhJjEwPbItoeBPxVQX QbDsWbbTHf2cP48dPvFBB811LtYoXZ2xu/yMfeM8ijx1kDTp8SHL798n5dbmFSQimE4d MXlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=X2eCgyA8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c24si11405276edw.144.2020.09.07.23.46.11; Mon, 07 Sep 2020 23:46:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=X2eCgyA8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729123AbgIHGmw (ORCPT + 99 others); Tue, 8 Sep 2020 02:42:52 -0400 Received: from mail.kernel.org ([198.145.29.99]:58842 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728759AbgIHGmw (ORCPT ); Tue, 8 Sep 2020 02:42:52 -0400 Received: from kernel.org (unknown [87.71.73.56]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 2975621532; Tue, 8 Sep 2020 06:42:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1599547371; bh=96sp3WmwyYCFUlY6cW+eRDxY6WE1Mn3q/76AW2FR2FA=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=X2eCgyA8r7jPkplTBABQK5Y00jq1fwvxQ4LayCz94jKomHbK5in0FChsHs77eB1r0 PhkB+tctp/lSR71to+m4BxSNKDIoEs8F3LKkU6R5GGeq5d7HMYEu08VJsvKvMMoZWb AyRX5nS6Sh+797ZUBTjsdcF8pxTeQc0JFItOBWLc= Date: Tue, 8 Sep 2020 09:42:45 +0300 From: Mike Rapoport To: Mateusz Nosek Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, akpm@linux-foundation.org Subject: Re: [PATCH] mm/mmu_notifier.c: micro-optimization substitute kzalloc with kmalloc Message-ID: <20200908064245.GE1976319@kernel.org> References: <20200906114321.16493-1-mateusznosek0@gmail.com> <20200906142645.GA1976319@kernel.org> <39c79454-9a97-2c06-3186-939c1f3a2b27@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <39c79454-9a97-2c06-3186-939c1f3a2b27@gmail.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Sep 06, 2020 at 06:06:39PM +0200, Mateusz Nosek wrote: > Hi, > > I performed simple benchmarks using custom kernel module with the code > fragment in question 'copy-pasted' in there in both versions. In case of 1k, > 10k and 100k iterations the average time for kzalloc version was 5.1 and for > kmalloc 3.9, for each iterations number. > The time was measured using 'ktime_get(void)' function and the results given > here are in ktime_t units. > The machine I use has 4 core Intel(R) Core(TM) i5-3470 CPU @ 3.20GHz CPU. > > The performance increase happens, but as you wrote it is probably not really > noticeable. I don't think that saving a few cylces of memset() in a function that called only on the initialization path in very particular cases is worth risking uninitialized variables when somebody will add a new field to the 'struct mmu_notifier_subscriptions' and will forget to explicitly set it. > I have found 3 other places in kernel code with similar kzalloc related > issues, none of which seems to be 'hot' code. > I leave the decision if this patch and potential others I would send > regarding this issue, are worth applying to the community and maintainers. > > Best regards, > Mateusz Nosek > > On 9/6/2020 4:26 PM, Mike Rapoport wrote: > > Hi, > > > > On Sun, Sep 06, 2020 at 01:43:21PM +0200, mateusznosek0@gmail.com wrote: > > > From: Mateusz Nosek > > > > > > Most fields in struct pointed by 'subscriptions' are initialized explicitly > > > after the allocation. By changing kzalloc to kmalloc the call to memset > > > is avoided. As the only new code consists of 2 simple memory accesses, > > > the performance is increased. > > > > Is there a measurable performance increase? > > > > The __mmu_notifier_register() is not used that frequently to trade off > > robustness of kzalloc() for slight (if visible at all) performance gain. > > > > > Signed-off-by: Mateusz Nosek > > > --- > > > mm/mmu_notifier.c | 4 +++- > > > 1 file changed, 3 insertions(+), 1 deletion(-) > > > > > > diff --git a/mm/mmu_notifier.c b/mm/mmu_notifier.c > > > index 4fc918163dd3..190e198dc5be 100644 > > > --- a/mm/mmu_notifier.c +++ b/mm/mmu_notifier.c @@ -625,7 +625,7 > > > @@ int __mmu_notifier_register(struct mmu_notifier *subscription, > > > * know that mm->notifier_subscriptions can't change while we * > > > hold the write side of the mmap_lock. */ > > > - subscriptions = kzalloc( > > > + subscriptions = kmalloc( > > > sizeof(struct mmu_notifier_subscriptions), GFP_KERNEL); > > > if (!subscriptions) > > > return -ENOMEM; > > > @@ -636,6 +636,8 @@ int __mmu_notifier_register(struct mmu_notifier *subscription, > > > subscriptions->itree = RB_ROOT_CACHED; > > > init_waitqueue_head(&subscriptions->wq); > > > INIT_HLIST_HEAD(&subscriptions->deferred_list); > > > + subscriptions->active_invalidate_ranges = 0; > > > + subscriptions->has_itree = false; > > > } > > > ret = mm_take_all_locks(mm); > > > -- > > > 2.20.1 > > > > > > > > -- Sincerely yours, Mike.