Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp38445873rwd; Wed, 12 Jul 2023 07:48:45 -0700 (PDT) X-Google-Smtp-Source: APBJJlFefXTbjT/Arsn3d4mAJweu0yvaFB/+elTZbP9AulIdYrptqLSGXJrlq2voYcv1TtFChLak X-Received: by 2002:a17:90a:9743:b0:262:df1d:4535 with SMTP id i3-20020a17090a974300b00262df1d4535mr14162504pjw.31.1689173325085; Wed, 12 Jul 2023 07:48:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689173325; cv=none; d=google.com; s=arc-20160816; b=cALQYW1xlyyLDEPmppH0zc0jTCwIRypuyEYtKl4TxCeG86QtMx56b0lxEJNHfgj/mZ PnGIyIoK2ucCjKVZslisXNZvbgyTlu3qHfxj3/uyFlKilGvuR1n6hCWV/AqbK8SmsAOD Xsmvg13S3zw9kWoVi25+xtUTH1W5B6qCqziuxw3+bqlqUlgxnNI1PXtYgUkD82YvLxaa /aa3Cdlf0hnH2tlDRXYccd+Ul36SsMxAk+OZzrmNsHDyONqvyf/ZXgnLZsoqtMdsDFvL s23cLYCXjkkKWye+IZWb7CmVs/FXbvhQGMSKIhHWRvH8cbk8Ia0ZJkOed61wS+IBXyc0 2SbQ== 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:date:subject:cc:to:from :dkim-signature; bh=AB5+NLcCXnzaRvbPgiz6n5N6dMyIeHmGAZh6vW2zkuk=; fh=am9XynIKpYTive19BhlfWZJ6JAuHmOId3TjoGoTGAM8=; b=HLjkQVHQBWfy833EBMhif2o90voTDzck9/lcq3trybkNvtYDPNW1Hw/GYcgBf1Vlzz 1Z+vsU2HZaoNKoPAa0bQLGptS+NkcGyY8+Mn2paxSJNXepCmHbTZWkGyNObEnkkmOH3v mcXT37sZGBRWX+sQRi1YbNi+gZgzuCEugmQEl85N8LMAkANSjGFtnXUSux3AuJztg+UN rqpiwLKLrZNH6nLLT5PcJq/pPvEhNfIdB2EGN101MBM1XR2IG7ztEBGQ85uVvmVOYVtz E9k9SONN9G5ClRoxpRtwg6hv6/JMnDd1qLtL9uIW1ofpBawEDIdnhZPyV9GdJVsp2ziB ErVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=cKpWKxtP; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z12-20020a17090a7b8c00b00262f798b614si3462617pjc.51.2023.07.12.07.48.30; Wed, 12 Jul 2023 07:48:45 -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=@intel.com header.s=Intel header.b=cKpWKxtP; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233050AbjGLOa5 (ORCPT + 99 others); Wed, 12 Jul 2023 10:30:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35432 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233011AbjGLOax (ORCPT ); Wed, 12 Jul 2023 10:30:53 -0400 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 175751BE8 for ; Wed, 12 Jul 2023 07:30:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1689172245; x=1720708245; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=5HuHECwAhlLAZmEKECs99DXvcfxOeVUXcj3HB1kzxlY=; b=cKpWKxtPpPKfS845FMITUcckeAGgLNQ9c9t7f8VH4V0i6KQDoh4dUXAt teVyVRl2c3MQuxLocjvYek64ydFQClhzJqu3ruvckCPFAVsXd20yADm3l zQN7MrOd/AfNiRH4pBfAUR5eLhOkRGFK2OQPA9XS95lEO/pcFr1ap0Kup ABoE31WMp+B44z190tJOKfJ04Krc5G24te68ynBeTkbcMBNngd6/wYwYv ag4yl0fCkAR85qr+AjcBJqLvWq0iUfWCT3GMu7O7DFkAAfKJ+VEjuepbw M0iVyQMyAjSMKDrcK8Pq5qHvGUyMtTCQ5Q3bHF7AzwY4dkMiJLaea6jfH w==; X-IronPort-AV: E=McAfee;i="6600,9927,10769"; a="363768524" X-IronPort-AV: E=Sophos;i="6.01,199,1684825200"; d="scan'208";a="363768524" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jul 2023 07:30:42 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10769"; a="724893684" X-IronPort-AV: E=Sophos;i="6.01,199,1684825200"; d="scan'208";a="724893684" Received: from linux-pnp-server-20.sh.intel.com ([10.239.146.185]) by fmsmga007.fm.intel.com with ESMTP; 12 Jul 2023 07:30:39 -0700 From: Yu Ma To: akpm@linux-foundation.org, Liam.Howlett@Oracle.com, kirill@shutemov.name, yu.ma@intel.com Cc: dan.j.williams@intel.com, dave.hansen@intel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, lipeng.zhu@intel.com, pan.deng@intel.com, shakeelb@google.com, tianyou.li@intel.com, tim.c.chen@intel.com, tim.c.chen@linux.intel.com Subject: [PATCH v3] mm/mmap: move vma operations to mm_struct out of the critical section of file mapping lock Date: Wed, 12 Jul 2023 10:57:39 -0400 Message-Id: <20230712145739.604215-1-yu.ma@intel.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230711112824.e559065488b08ce649cccfd7@linux-foundation.org> References: <20230711112824.e559065488b08ce649cccfd7@linux-foundation.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE,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 UnixBench/Execl represents a class of workload where bash scripts are spawned frequently to do some short jobs. When running multiple parallel tasks, hot osq_lock is observed from do_mmap and exit_mmap. Both of them come from load_elf_binary through the call chain "execl->do_execveat_common->bprm_execve->load_elf_binary". In do_mmap,it will call mmap_region to create vma node, initialize it and insert it to vma maintain structure in mm_struct and i_mmap tree of the mapping file, then increase map_count to record the number of vma nodes used. The hot osq_lock is to protect operations on file’s i_mmap tree. For the mm_struct member change like vma insertion and map_count update, they do not affect i_mmap tree. Move those operations out of the lock's critical section, to reduce hold time on the lock. With this change, on Intel Sapphire Rapids 112C/224T platform, based on v6.0-rc6, the 160 parallel score improves by 12%. The patch has no obvious performance gain on v6.5-rc1 due to regression of this benchmark from this commit f1a7941243c102a44e8847e3b94ff4ff3ec56f25 (mm: convert mm's rss stats into percpu_counter). Related discussion and conclusion can be referred at the mail thread initiated by 0day as below: Link: https://lore.kernel.org/linux-mm/a4aa2e13-7187-600b-c628-7e8fb108def0@intel.com/ Reviewed-by: Tim Chen Signed-off-by: Yu Ma --- v2 -> v3: Rebase the patch to v6.5-rc1, which includes 1c7873e3364 (mm: lock newly mapped VMA with corrected ordering), and update commit message to status on v6.5-rc1 v1 -> v2: Update vma_link() to reduce the hold time on file mapping lock as well. Based on v6.5-rc1, vma_link() is only called by insert_vm_struct () and copy_vma(), which are both protected by mmap_lock. --- --- mm/mmap.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 3eda23c9ebe7..ce31aec82e82 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -412,14 +412,11 @@ static int vma_link(struct mm_struct *mm, struct vm_area_struct *vma) if (vma_iter_prealloc(&vmi)) return -ENOMEM; + vma_iter_store(&vmi, vma); + if (vma->vm_file) { mapping = vma->vm_file->f_mapping; i_mmap_lock_write(mapping); - } - - vma_iter_store(&vmi, vma); - - if (mapping) { __vma_link_file(vma, mapping); i_mmap_unlock_write(mapping); } @@ -2811,12 +2808,10 @@ unsigned long mmap_region(struct file *file, unsigned long addr, /* Lock the VMA since it is modified after insertion into VMA tree */ vma_start_write(vma); - if (vma->vm_file) - i_mmap_lock_write(vma->vm_file->f_mapping); - vma_iter_store(&vmi, vma); mm->map_count++; if (vma->vm_file) { + i_mmap_lock_write(vma->vm_file->f_mapping); if (vma->vm_flags & VM_SHARED) mapping_allow_writable(vma->vm_file->f_mapping); -- 2.39.3