From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A12E6EB64D7 for ; Fri, 16 Jun 2023 20:16:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3D42F8E0002; Fri, 16 Jun 2023 16:16:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 35D648E0001; Fri, 16 Jun 2023 16:16:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1FEF38E0002; Fri, 16 Jun 2023 16:16:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 0B6598E0001 for ; Fri, 16 Jun 2023 16:16:44 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id CA5D1160C4D for ; Fri, 16 Jun 2023 20:16:43 +0000 (UTC) X-FDA: 80909719086.19.F68652E Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf22.hostedemail.com (Postfix) with ESMTP id 005E5C0008 for ; Fri, 16 Jun 2023 20:16:41 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=CQQ0nM2W; dmarc=none; spf=pass (imf22.hostedemail.com: domain of akpm@linux-foundation.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1686946602; a=rsa-sha256; cv=none; b=u3Dku/WZlALLehSuuy8ZB/WEWxGvLf9PbGvm91uvau0zw0KJj0QaXLxmwHaZELh93T44We VvTZveDa3v+vpV/GlmiKRJNGtyvJ3z2XpU1swyhJQ6q3H2KqmRKWmsXYZ47fMuYP+3PXKU BptOFCTOGQTU2Uyru8S+bPcCgxo7o5E= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=CQQ0nM2W; dmarc=none; spf=pass (imf22.hostedemail.com: domain of akpm@linux-foundation.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686946602; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=PA4tB3ejt9FbkLswrgAJyYcw7mzvb1/1jPt9avoqYwA=; b=XlD+1r7ClB8IIEk1GDvLyr32Kfbci2IGn1nKGQdvdowFoZRyGzi8HRH6f8ByWuu/J+iCTF jf9arRZJStf+YmokNoRCxrRsqrYqIbTTBV1Hd1OpwRY4n2QvwPPRsTUMCbXr/vFEIjutTz hOwrFLuvEkbTgeKJsYF6B0XaFNHDt6A= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 0C99963D9D; Fri, 16 Jun 2023 20:16:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0B24DC433C8; Fri, 16 Jun 2023 20:16:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1686946600; bh=r1/rE7q7B9G5wovZrruNQFK13c8rxM+QTwTPz01sOds=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=CQQ0nM2W/E3WihKX0NzuD7GwsVcjouhHPg/g91HOOpEfR7CnzsBas+4HZEvy4671e L+BXCS4lhiz7qZGBJ841n+xMxZC3dky2IZ6SDYvZx/4RE0HtVzklxm0m/DMa7Ioflm OOeF/lkyACPF9a2h3KdaEN907rAdHS83g0MrjlFY= Date: Fri, 16 Jun 2023 13:16:39 -0700 From: Andrew Morton To: Mathieu Desnoyers Cc: Peter Zijlstra , linux-kernel@vger.kernel.org, kernel test robot , Aaron Lu , Olivier Dion , michael.christie@oracle.com, Feng Tang , John Hubbard , Jason Gunthorpe , Peter Xu , linux-mm@kvack.org Subject: Re: [PATCH] mm: Move mm_count into its own cache line Message-Id: <20230616131639.992998157fe696eb0e0589aa@linux-foundation.org> In-Reply-To: <20230515143536.114960-1-mathieu.desnoyers@efficios.com> References: <20230515143536.114960-1-mathieu.desnoyers@efficios.com> X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 005E5C0008 X-Stat-Signature: 7ne6gj9epcanohgog9rxzoerue5f7qnr X-HE-Tag: 1686946601-355046 X-HE-Meta: U2FsdGVkX187ONBdrHoZ+ILpG9BoStbmbRzusQ79pfSb2mjrDRnewvRUuakTQ/1Uy0wAQ5LOl69SWpoP2DgRHQfoto5Zl4GONJ7GtDXJv3oPTdkR2HBqB2vBTYIUHkFus0bvvRR//X95OAEbEN81e7YvpExwudYeGhdtAJkq+XfDGm+ZchR/MhKRoIgUI+wUodHwpbeMQtE8IREfyOq3skAw0gdSiYgJdvuA1x9hsfiaYTvtz1yg3W/KHNHpDFGdjBPxp1taaxEIGz8HYE5MgLbAwNvJKMPVCKsZDds+hUj6hukqmtgsZBLpTksyJQi80M0z6koL0pBPbUiGNRcHjxUhj5kD5rs8cnSy1oH0qLYgYF62lh4H39t8Z7YD7F4cefLqX/DuZd/TTMfZyqm6ltNZT7H+T9Z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.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Mon, 15 May 2023 10:35:36 -0400 Mathieu Desnoyers wrote: > The mm_struct mm_count field is frequently updated by mmgrab/mmdrop > performed by context switch. This causes false-sharing for surrounding > mm_struct fields which are read-mostly. > > This has been observed on a 2sockets/112core/224cpu Intel Sapphire > Rapids server running hackbench, and by the kernel test robot > will-it-scale testcase. > > Move the mm_count field into its own cache line to prevent false-sharing > with other mm_struct fields. > > Move mm_count to the first field of mm_struct to minimize the amount of > padding required: rather than adding padding before and after the > mm_count field, padding is only added after mm_count. > > Note that I noticed this odd comment in mm_struct: > > commit 2e3025434a6b ("mm: relocate 'write_protect_seq' in struct mm_struct") > > /* > * With some kernel config, the current mmap_lock's offset > * inside 'mm_struct' is at 0x120, which is very optimal, as > * its two hot fields 'count' and 'owner' sit in 2 different > * cachelines, and when mmap_lock is highly contended, both > * of the 2 fields will be accessed frequently, current layout > * will help to reduce cache bouncing. > * > * So please be careful with adding new fields before > * mmap_lock, which can easily push the 2 fields into one > * cacheline. > */ > struct rw_semaphore mmap_lock; > > This comment is rather odd for a few reasons: > > - It requires addition/removal of mm_struct fields to carefully consider > field alignment of _other_ fields, > - It expresses the wish to keep an "optimal" alignment for a specific > kernel config. > > I suspect that the author of this comment may want to revisit this topic > and perhaps introduce a split-struct approach for struct rw_semaphore, > if the need is to place various fields of this structure in different > cache lines. > > ... > > --- a/include/linux/mm_types.h > +++ b/include/linux/mm_types.h > @@ -583,6 +583,21 @@ struct mm_cid { > struct kioctx_table; > struct mm_struct { > struct { > + /* > + * Fields which are often written to are placed in a separate > + * cache line. > + */ > + struct { > + /** > + * @mm_count: The number of references to &struct > + * mm_struct (@mm_users count as 1). > + * > + * Use mmgrab()/mmdrop() to modify. When this drops to > + * 0, the &struct mm_struct is freed. > + */ > + atomic_t mm_count; > + } ____cacheline_aligned_in_smp; > + Why add the anonymous struct? atomic_t mm_count ____cacheline_aligned_in_smp; would suffice? Secondly, the ____cacheline_aligned_in_smp doesn't actually do anything? mm_count is at offset 0 which is cacheline aligned anyway. The next field (mm_mt) will share a cacheline with mm_count. If the plan is to put mm_count in "its own" cacheline then padding will be needed?