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 X-Spam-Level: X-Spam-Status: No, score=-15.1 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 71A31C433ED for ; Mon, 19 Apr 2021 09:41:04 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 0913B60FF3 for ; Mon, 19 Apr 2021 09:41:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0913B60FF3 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 815A96B0036; Mon, 19 Apr 2021 05:41:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7EC3C6B006E; Mon, 19 Apr 2021 05:41:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 617056B0070; Mon, 19 Apr 2021 05:41:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0221.hostedemail.com [216.40.44.221]) by kanga.kvack.org (Postfix) with ESMTP id 435186B0036 for ; Mon, 19 Apr 2021 05:41:03 -0400 (EDT) Received: from smtpin33.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 0B811180AD837 for ; Mon, 19 Apr 2021 09:41:03 +0000 (UTC) X-FDA: 78048622806.33.401984E Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf17.hostedemail.com (Postfix) with ESMTP id B326B40002DF for ; Mon, 19 Apr 2021 09:40:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618825262; h=from:from: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; bh=GeFgKAw6h+mPcXVcc4bWq1BMFLAt/JiFop1ZWmN9P3g=; b=blr3qaMcaPKptqAW2dsrPdu12MDl7piaeHIkesm5NN/Lq2z/+17F5ftkGJZYyfylE91JNa 1E/9oK3IXvjT4146yDwp5+LMYz7N05gtn/OBk3GgQGZRCvVCUhWYMxZ/bAv1TojmNdmZNJ dtW6BdIZiSxr+3Sw7pvfvuwiDOHbqn0= Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-474-wV8cgupBNBiwGWAMuZiGYA-1; Mon, 19 Apr 2021 05:41:00 -0400 X-MC-Unique: wV8cgupBNBiwGWAMuZiGYA-1 Received: by mail-ed1-f69.google.com with SMTP id cz7-20020a0564021ca7b02903853d41d8adso1566412edb.17 for ; Mon, 19 Apr 2021 02:40:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:references:organization :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=GeFgKAw6h+mPcXVcc4bWq1BMFLAt/JiFop1ZWmN9P3g=; b=iKF3/IrE1j9/ZSLpfUS6tgxhU6+zoCLYowLPHsvXMzzRynIRpVhXww7iZBBnbBtQ2r vUHN/KK/4nhDh1MR7ON9voMTtOLQc0hT0k5rchARQZDrm+PVrf00CIi463HrVTGrqyhV aexv8MPoSv+qKeiNXu6y0jk+Tw1GLPdlsSD/WDep4kVoKVCA85KBk9lVXB2xyUFLxLRH dXhwnGRrhBWi8P51djDDAbLII72sceDEScmuBO5/J3K5Q1N6nNr5vLk/4l2Mt3Smtepu uTfcaFVPCNI1RG1UkbrHq5bwC0VnK/ahkRf9mtvAWskHlc1BR8NTixJJOMdalRQbjQqt NDYA== X-Gm-Message-State: AOAM532qM5o6Z32vaSXJCGxs0wRE2j3RZXl5Kiy61iF7WCjtVVHAAtgg qccsI1xUX3hBibaZjwH0nKf9Jn1Euo5n+z+a4TOVgwUYWcaefSmagpuXI5x+SLDTbm7Up2/Mxl9 mLFwh5VG95V4= X-Received: by 2002:aa7:dcd3:: with SMTP id w19mr404822edu.157.1618825258854; Mon, 19 Apr 2021 02:40:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwUep/F2sYsfc/GbdpC874xEruErysvjZiJSGRDsYz4g5yz9xbr6J9pJm9MKGKGsnTSZWCpUQ== X-Received: by 2002:aa7:dcd3:: with SMTP id w19mr404773edu.157.1618825258627; Mon, 19 Apr 2021 02:40:58 -0700 (PDT) Received: from [192.168.3.132] (p5b0c69b8.dip0.t-ipconnect.de. [91.12.105.184]) by smtp.gmail.com with ESMTPSA id b6sm1276048edd.18.2021.04.19.02.40.56 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 19 Apr 2021 02:40:58 -0700 (PDT) Subject: Re: [PATCH] secretmem: optimize page_is_secretmem() From: David Hildenbrand To: Mike Rapoport Cc: Andrew Morton , Alexander Viro , Andy Lutomirski , Arnd Bergmann , Borislav Petkov , Catalin Marinas , Christopher Lameter , Dan Williams , Dave Hansen , Elena Reshetova , "H. Peter Anvin" , Ingo Molnar , James Bottomley , "Kirill A. Shutemov" , Matthew Wilcox , Matthew Garrett , Mark Rutland , Michal Hocko , Mike Rapoport , Michael Kerrisk , Palmer Dabbelt , Paul Walmsley , Peter Zijlstra , "Rafael J. Wysocki" , Rick Edgecombe , Roman Gushchin , Shakeel Butt , Shuah Khan , Thomas Gleixner , Tycho Andersen , Will Deacon , Yury Norov , linux-api@vger.kernel.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-nvdimm@lists.01.org, linux-riscv@lists.infradead.org, x86@kernel.org, kernel test robot References: <20210419084218.7466-1-rppt@kernel.org> <3b30ac54-8a92-5f54-28f0-f110a40700c7@redhat.com> Organization: Red Hat Message-ID: <56d8b80c-ce2c-ed86-0eda-253768d8d463@redhat.com> Date: Mon, 19 Apr 2021 11:40:56 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=david@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US X-Stat-Signature: 4173wz6xohg69fafpddc665e1w6wf8f3 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: B326B40002DF Received-SPF: none (redhat.com>: No applicable sender policy available) receiver=imf17; identity=mailfrom; envelope-from=""; helo=us-smtp-delivery-124.mimecast.com; client-ip=170.10.133.124 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1618825259-69907 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On 19.04.21 11:38, David Hildenbrand wrote: > On 19.04.21 11:36, Mike Rapoport wrote: >> On Mon, Apr 19, 2021 at 11:15:02AM +0200, David Hildenbrand wrote: >>> On 19.04.21 10:42, Mike Rapoport wrote: >>>> From: Mike Rapoport >>>> >>>> Kernel test robot reported -4.2% regression of will-it-scale.per_thr= ead_ops >>>> due to commit "mm: introduce memfd_secret system call to create "sec= ret" >>>> memory areas". >>>> >>>> The perf profile of the test indicated that the regression is caused= by >>>> page_is_secretmem() called from gup_pte_range() (inlined by gup_pgd_= range): >>>> >>>> 27.76 +2.5 30.23 perf-profile.children.cycles-pp.gup_pgd= _range >>>> 0.00 +3.2 3.19 =C2=B1 2% perf-profile.children.cycles-pp.pa= ge_mapping >>>> 0.00 +3.7 3.66 =C2=B1 2% perf-profile.children.cycles-pp.pa= ge_is_secretmem >>>> >>>> Further analysis showed that the slow down happens because neither >>>> page_is_secretmem() nor page_mapping() are not inline and moreover, >>>> multiple page flags checks in page_mapping() involve calling >>>> compound_head() several times for the same page. >>>> >>>> Make page_is_secretmem() inline and replace page_mapping() with page= flag >>>> checks that do not imply page-to-head conversion. >>>> >>>> Reported-by: kernel test robot >>>> Signed-off-by: Mike Rapoport >>>> --- >>>> >>>> @Andrew, >>>> The patch is vs v5.12-rc7-mmots-2021-04-15-16-28, I'd appreciate if = it would >>>> be added as a fixup to the memfd_secret series. >>>> >>>> include/linux/secretmem.h | 26 +++++++++++++++++++++++++- >>>> mm/secretmem.c | 12 +----------- >>>> 2 files changed, 26 insertions(+), 12 deletions(-) >>>> >>>> diff --git a/include/linux/secretmem.h b/include/linux/secretmem.h >>>> index 907a6734059c..b842b38cbeb1 100644 >>>> --- a/include/linux/secretmem.h >>>> +++ b/include/linux/secretmem.h >>>> @@ -4,8 +4,32 @@ >>>> #ifdef CONFIG_SECRETMEM >>>> +extern const struct address_space_operations secretmem_aops; >>>> + >>>> +static inline bool page_is_secretmem(struct page *page) >>>> +{ >>>> + struct address_space *mapping; >>>> + >>>> + /* >>>> + * Using page_mapping() is quite slow because of the actual call >>>> + * instruction and repeated compound_head(page) inside the >>>> + * page_mapping() function. >>>> + * We know that secretmem pages are not compound and LRU so we can >>>> + * save a couple of cycles here. >>>> + */ >>>> + if (PageCompound(page) || !PageLRU(page)) >>>> + return false; >>> >>> I'd assume secretmem pages are rare in basically every setup out ther= e. So >>> maybe throwing in a couple of likely()/unlikely() might make sense. >> >> I'd say we could do unlikely(page_is_secretmem()) at call sites. Here = I can >> hardly estimate which pages are going to be checked. >> =20 >>>> + >>>> + mapping =3D (struct address_space *) >>>> + ((unsigned long)page->mapping & ~PAGE_MAPPING_FLAGS); >>>> + >>> >>> Not sure if open-coding page_mapping is really a good idea here -- or= even >>> necessary after the fast path above is in place. Anyhow, just my 2 ce= nts. >> >> Well, most if the -4.2% of the performance regression kbuild reported = were >> due to repeated compount_head(page) in page_mapping(). So the whole po= int >> of this patch is to avoid calling page_mapping(). >=20 > I would have thought the fast path "(PageCompound(page) || > !PageLRU(page))" would already avoid calling page_mapping() in many cas= es. (and I do wonder if a generic page_mapping() optimization would make=20 sense instead) Willy can most probably give the best advise here :) --=20 Thanks, David / dhildenb