Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp3177758rwd; Fri, 16 Jun 2023 13:28:27 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6QXJoKfIoecygl3z32E8r1bGMr34tZKniEN5uObBJu7JptyvmZEaCnkW01x4TK+WYQN3JZ X-Received: by 2002:a17:902:e547:b0:1a9:433e:41e7 with SMTP id n7-20020a170902e54700b001a9433e41e7mr2523694plf.43.1686947307154; Fri, 16 Jun 2023 13:28:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686947307; cv=none; d=google.com; s=arc-20160816; b=hHalrbYZLfEiaSEDNe+n9vmg7CGS60R6YwQNNmozmW/le7wKIyaWvIyex6IUQ0oSfh u3d/Nb0csPWm9qkPkjP3NoHS1EQkp0NU4NfhpWCfcB49K2X2yp7FQhIgCO7ngejarhST iBoLv1TW98V2cYbjru+FH+J2T9t2qMvabHiBjuKQcjHTPcnZErNCr/Co8mtv829iNtyb sw2ICgsr4HQ9sTFSXXRPtcm+W8lxmnSh4uaaYolBiLNbhQvz+Ma6miDdGiseSR9j7zdZ pRgOvRs4sRdNbrBR3xoyhS+fdvfBLzBhscZlCLDIuLfYzHAt28z0NKCGD6NxJfpzSrJK PmnQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=PA4tB3ejt9FbkLswrgAJyYcw7mzvb1/1jPt9avoqYwA=; b=SEgCLqG9iI+Tm2jovJ2lb+HyBGew7cMl1gB0b++QFzhYggfxZxWW3hW4TA4qvTpjvi LPJe5ap9wWr5OZcKygnzSjpFvn1EcGZOIMOp4QXJKTylk/wvVf8djKF5tnSeNKOkSf6d MHoqeKSY0iiyBpUHXcP0S8AoBv/DzVpLqIpIh4Z/T6DeyI7k6DNDuhA3nRun28Ju53xM j684Te6KCol/i+zaWEemq5ubTUoihqK4VMj36BeWnbn9iDWJ4MaiwI/i9IMgG07vfZTZ WLWfIsP1GwdljYDSJ1deZBjYCvQe6eyvb1r31el1Tgtj6KSSodbz6KX8QVfkrHRepqz9 rGcQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=korg header.b=CQQ0nM2W; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z18-20020a170903019200b001ab089f7329si13330175plg.73.2023.06.16.13.28.12; Fri, 16 Jun 2023 13:28:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=korg header.b=CQQ0nM2W; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232930AbjFPUQn (ORCPT + 99 others); Fri, 16 Jun 2023 16:16:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49692 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230022AbjFPUQm (ORCPT ); Fri, 16 Jun 2023 16:16:42 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7EE2635BD for ; Fri, 16 Jun 2023 13:16:41 -0700 (PDT) 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 1171463E33 for ; 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-Spam-Status: No, score=-7.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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?