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=-13.8 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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 757F2C43462 for ; Mon, 19 Apr 2021 10:14:41 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 1DD7661165 for ; Mon, 19 Apr 2021 10:14:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1DD7661165 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 978CA6B0036; Mon, 19 Apr 2021 06:14:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 926E66B006E; Mon, 19 Apr 2021 06:14:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 77AEB6B0070; Mon, 19 Apr 2021 06:14:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0139.hostedemail.com [216.40.44.139]) by kanga.kvack.org (Postfix) with ESMTP id 5913D6B0036 for ; Mon, 19 Apr 2021 06:14:40 -0400 (EDT) Received: from smtpin26.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 11DFA8249980 for ; Mon, 19 Apr 2021 10:14:40 +0000 (UTC) X-FDA: 78048707520.26.789F595 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf29.hostedemail.com (Postfix) with ESMTP id 4E866E4 for ; Mon, 19 Apr 2021 10:14:37 +0000 (UTC) Received: by mail.kernel.org (Postfix) with ESMTPSA id E554261157; Mon, 19 Apr 2021 10:14:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1618827278; bh=5M87hSd1tRHqwQ2MYCWGR3C48wQgQUhK50aj8D9UnJc=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=evAt2Kl5D6T9Cd0vvsQr+HGnZL0bjqZw0RQERf99o/a8ARSQONHlK5WsXoqQNk5HA UuhF/Rrt5Zhrq6yRbbpHUSqFJnrvW93kg/buM/tDCb4j7nNpeebnLT19q5XvGHG0e1 49YiudzNJhVJYWtrWp69yUsf7ZicdxeuaJvb2o26MproyIrJfelmI05dnFpYTA1S+3 KPevq2k+QCdmg92H0H+J4u9YW1dfsBAUkpesDwdo/uZea8b1mg9JI4Syb8VXhzzjqw IZ0ev45rLME8l/aMAEZNl8hE0npfiMbGut76Y1J7ozBko7t6Q5l7vUjPndFu66yHeo n/6L17YFihFDg== Date: Mon, 19 Apr 2021 13:14:19 +0300 From: Mike Rapoport To: David Hildenbrand 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 Subject: Re: [PATCH] secretmem: optimize page_is_secretmem() Message-ID: References: <20210419084218.7466-1-rppt@kernel.org> <3b30ac54-8a92-5f54-28f0-f110a40700c7@redhat.com> <56d8b80c-ce2c-ed86-0eda-253768d8d463@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline In-Reply-To: <56d8b80c-ce2c-ed86-0eda-253768d8d463@redhat.com> X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 4E866E4 X-Stat-Signature: 7artr6fjjunx98bh3dkxr8ud7pombf8e Received-SPF: none (kernel.org>: No applicable sender policy available) receiver=imf29; identity=mailfrom; envelope-from=""; helo=mail.kernel.org; client-ip=198.145.29.99 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1618827277-303331 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 Mon, Apr 19, 2021 at 11:40:56AM +0200, David Hildenbrand wrote: > 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 > > > > >=20 > > > > > Kernel test robot reported -4.2% regression of will-it-scale.pe= r_thread_ops > > > > > due to commit "mm: introduce memfd_secret system call to create= "secret" > > > > > memory areas". > > > > >=20 > > > > > The perf profile of the test indicated that the regression is c= aused by > > > > > page_is_secretmem() called from gup_pte_range() (inlined by gup= _pgd_range): > > > > >=20 > > > > > 27.76 +2.5 30.23 perf-profile.children.cycles-pp.gu= p_pgd_range > > > > > 0.00 +3.2 3.19 =B1 2% perf-profile.children.cycles-pp.= page_mapping > > > > > 0.00 +3.7 3.66 =B1 2% perf-profile.children.cycles-pp.= page_is_secretmem > > > > >=20 > > > > > Further analysis showed that the slow down happens because neit= her > > > > > page_is_secretmem() nor page_mapping() are not inline and moreo= ver, > > > > > multiple page flags checks in page_mapping() involve calling > > > > > compound_head() several times for the same page. > > > > >=20 > > > > > Make page_is_secretmem() inline and replace page_mapping() with= page flag > > > > > checks that do not imply page-to-head conversion. > > > > >=20 > > > > > Reported-by: kernel test robot > > > > > Signed-off-by: Mike Rapoport > > > > > --- > > > > >=20 > > > > > @Andrew, > > > > > The patch is vs v5.12-rc7-mmots-2021-04-15-16-28, I'd appreciat= e if it would > > > > > be added as a fixup to the memfd_secret series. > > > > >=20 > > > > > include/linux/secretmem.h | 26 +++++++++++++++++++++++++- > > > > > mm/secretmem.c | 12 +----------- > > > > > 2 files changed, 26 insertions(+), 12 deletions(-) > > > > >=20 > > > > > diff --git a/include/linux/secretmem.h b/include/linux/secretme= m.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 c= all > > > > > + * instruction and repeated compound_head(page) inside the > > > > > + * page_mapping() function. > > > > > + * We know that secretmem pages are not compound and LRU so w= e can > > > > > + * save a couple of cycles here. > > > > > + */ > > > > > + if (PageCompound(page) || !PageLRU(page)) > > > > > + return false; > > > >=20 > > > > I'd assume secretmem pages are rare in basically every setup out = there. So > > > > maybe throwing in a couple of likely()/unlikely() might make sens= e. > > >=20 > > > I'd say we could do unlikely(page_is_secretmem()) at call sites. He= re I can > > > hardly estimate which pages are going to be checked. > > > > > + > > > > > + mapping =3D (struct address_space *) > > > > > + ((unsigned long)page->mapping & ~PAGE_MAPPING_FLAGS); > > > > > + > > > >=20 > > > > 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 cents. > > >=20 > > > Well, most if the -4.2% of the performance regression kbuild report= ed were > > > due to repeated compount_head(page) in page_mapping(). So the whole= point > > > 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 c= ases. >=20 > (and I do wonder if a generic page_mapping() optimization would make se= nse > instead) Not sure. Replacing page_mapping() with page_file_mapping() at the call sites at fs/ and mm/ increased the defconfig image by nearly 2k and page_file_mapping() is way simpler than page_mapping() add/remove: 1/0 grow/shrink: 35/0 up/down: 1960/0 (1960) Function old new delta shrink_page_list 3414 3670 +256 __set_page_dirty_nobuffers 242 349 +107 check_move_unevictable_pages 904 987 +83 move_to_new_page 591 671 +80 shrink_active_list 912 970 +58 move_pages_to_lru 911 965 +54 migrate_pages 2500 2554 +54 shmem_swapin_page 1145 1197 +52 shmem_undo_range 1669 1719 +50 __test_set_page_writeback 620 670 +50 __set_page_dirty_buffers 187 237 +50 __pagevec_lru_add 757 807 +50 __munlock_pagevec 1155 1205 +50 __dump_page 1101 1151 +50 __cancel_dirty_page 182 232 +50 __remove_mapping 461 510 +49 rmap_walk_file 402 449 +47 isolate_movable_page 240 287 +47 test_clear_page_writeback 668 714 +46 page_cache_pipe_buf_try_steal 171 217 +46 page_endio 246 290 +44 page_file_mapping - 43 +43 __isolate_lru_page_prepare 254 297 +43 hugetlb_page_mapping_lock_write 39 81 +42 iomap_set_page_dirty 110 151 +41 clear_page_dirty_for_io 324 364 +40 wait_on_page_writeback_killable 118 157 +39 wait_on_page_writeback 105 144 +39 set_page_dirty 159 198 +39 putback_movable_page 32 71 +39 page_mkclean 172 211 +39 mark_buffer_dirty 176 215 +39 invalidate_inode_page 122 161 +39 delete_from_page_cache 139 178 +39 PageMovable 49 86 +37 isolate_migratepages_block 2843 2872 +29 Total: Before=3D17068648, After=3D17070608, chg +0.01% =20 > Willy can most probably give the best advise here :) I think that's what folios are for :) --=20 Sincerely yours, Mike.