From patchwork Mon Jul 22 05:43:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 13738278 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 1DC53C3DA5D for ; Mon, 22 Jul 2024 05:43:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E89EB6B0082; Mon, 22 Jul 2024 01:43:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E391C6B0083; Mon, 22 Jul 2024 01:43:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CD9E36B0088; Mon, 22 Jul 2024 01:43:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id AE1A66B0083 for ; Mon, 22 Jul 2024 01:43:32 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id EAE711A1422 for ; Mon, 22 Jul 2024 05:43:31 +0000 (UTC) X-FDA: 82366296222.02.F31DD75 Received: from out30-100.freemail.mail.aliyun.com (out30-100.freemail.mail.aliyun.com [115.124.30.100]) by imf25.hostedemail.com (Postfix) with ESMTP id 6D74AA001A for ; Mon, 22 Jul 2024 05:43:29 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=Dt2KRBPF; spf=pass (imf25.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.100 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com; dmarc=pass (policy=none) header.from=linux.alibaba.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721626975; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=J5dcVB7jyKKfrkmzqLedgx6fPLHuGxUqgTPplR6Z9pA=; b=Hwk5rMIoQhZs6AEOjCq7uekfRBVtzyKrj5+xArhxtQBXZARfsLMs6KyF2i/vy0qQiTJ4aJ /GNm5iPfDA5lIw/NlKE0/ZZgC/WrySK/koaU/LFNs5uLEHcLv19QuWf84Ho9OIGgg9EX2K pYkqcmqyK8v8h/0TkpKWt7/A+d4aO14= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=Dt2KRBPF; spf=pass (imf25.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.100 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com; dmarc=pass (policy=none) header.from=linux.alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721626975; a=rsa-sha256; cv=none; b=sFoKaJJ8LBMJjngaf1vPCxwYjbGRq9uXdEUmUrxAekbS/Iu89pVOtpRSpefaBbFbvcHFkr e2uks44FKV0+8KZtPe04HO5jqS99SrGeDpZfiMt64E+LyV1J9anqjuLrI32onl7izcPPN5 TaVmRyfP51NB4ll0GxaboHvj21r1X5w= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1721627007; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=J5dcVB7jyKKfrkmzqLedgx6fPLHuGxUqgTPplR6Z9pA=; b=Dt2KRBPFFzjXQiniXmtD4/4SntKWSTF+Kg0sF+BfgF4WyXTmpX+vC6ujNyGIwbiC9t7q5cg2WiXEohzAujYTDwQ6wCuVSWj/unON+vUPY2E74JM6rFiux7vPxK33C6xSOnhRn9KXL2XKcZ130QjaGFrs4TFiVJRvtp5a8Lhpby0= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R101e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033068173054;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=11;SR=0;TI=SMTPD_---0WAyvGdp_1721627004; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0WAyvGdp_1721627004) by smtp.aliyun-inc.com; Mon, 22 Jul 2024 13:43:25 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com Cc: willy@infradead.org, david@redhat.com, 21cnbao@gmail.com, ryan.roberts@arm.com, ziy@nvidia.com, ioworker0@gmail.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 1/3] mm: shmem: simplify the suitable huge orders validation for tmpfs Date: Mon, 22 Jul 2024 13:43:17 +0800 Message-Id: <965985dd6d322929d78a0beee0dafa1c2a1b81e2.1721626645.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: References: MIME-Version: 1.0 X-Stat-Signature: 6k11hkozps3wekytfroxcgc367xr9nqr X-Rspam-User: X-Rspamd-Queue-Id: 6D74AA001A X-Rspamd-Server: rspam02 X-HE-Tag: 1721627009-812105 X-HE-Meta: U2FsdGVkX19g+lI0NO/pCfC7A+q7a3wQVezvS1dQWwdcDfWS6zrF9XPkF1RvB+SIXCoHadosJ8XeEvXEvpD/0Ebn1UiAf5FNax0PChpL3k41HjZJaBSahUEWkz1NQozfKoukBdh/X/Oj/Rr6lNH/lCJe0FKezGlUuBJctRhggeeTeozROindsll7f9puGhkR/GsnX5VhCtmXYMDAfxHzSqk9C+3yowmXnz754uKtn18GMZyW8bZqmRZcww+Rdpc6DKsOIsuXPMbH5QVWcunYkXzakxVXOgaBufLtB2JHnZ1Bh9gl8f87D77oufJJfAXicR/tn8vdRuZNWRMe9n8NcPAecmGWFNaAxw6AJqBe2i42bUXMlb2cfzt+dz0lE84xg3tLeHXa1Bq06riBQx0Qba/l8TCtjFpgiNNNHiwfzt2UcYrCiAl09ELgdHjnSJ5HAl7M0A3yRYDkHSHkbZhx0V8p3VHk5LlesyFIX5biBoOR4mSwoHbTXHMy2Pfvihf3e3cpFdknYwVVovgCfKP1n7udXBcD5EmIJUdRUFOhT5FvcWsXVaQQqP0z+JE35NsKt8CrF2SwmnAwxGkViN3PKGKA/SX319MGcyMffforndox6wDn9s/tvv9gHrRNEIQ/VErrsb06JejKF8S0j0jOIFV/BurrOJLQf29+S9wgoo7d5vUzSHv27jrntJKcl9T+PWFIEM1xQk3hF4z2werygw7jP8mabXvjVcUArHKE860hxBMG9V76eAZN2a8YPVPXjK0UUXatdppHO1RAmBE8XE7R5l7CD01joUoEAthqWtKqkSscbAdtbFuq+QSB5wYWeixmlIdGkDQ4vuZ8Cah+CUnT1YYsH9KFRBkWpkwqtVH2XTUOL43T1aJfuJTbbsmlKB7cMc43syG240cZ6zRBhtrjwzhSNWsC+PQ8oy+EjJrFDrz/lFXWize4qHwEC0OEgrQ2/GVOIAuzZle+eUb aQ8zvZ24 R8XZ1rIxdjc67jKG03RyH05ev15kR6WiHMHkrITM6Osz9mz4tckN98Q4cuAb9EEpP4ZJPyOnn2E3yKXBTrZxID7dwh8qWiHUd4cM+luf3xw1cvCZhfrqGAE+8bVhNpe35p+Gtm8Okn8v4wgQEHWh2EhrSMgx/43w+79ATKIpTD9BERNBQMz4PkKsGlZOlG9mQ92C2GNvFPuy8pQ6tTKRE7GJaO0l9AliFLdpVsG4GNLjlI1utHl46xfd1VIQwbsuITcr/ X-Bogosity: Ham, tests=bogofilter, spamicity=0.000010, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Move the suitable huge orders validation into shmem_suitable_orders() for tmpfs, which can reuse some code to simplify the logic. In addition, we don't have special handling for the error code -E2BIG when checking for conflicts with PMD sized THP in the pagecache for tmpfs, instead, it will just fallback to order-0 allocations like this patch does, so this simplification will not add functional changes. Signed-off-by: Baolin Wang Reviewed-by: Ryan Roberts --- mm/shmem.c | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index f24dfbd387ba..db7e9808830f 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1685,19 +1685,29 @@ static unsigned long shmem_suitable_orders(struct inode *inode, struct vm_fault struct address_space *mapping, pgoff_t index, unsigned long orders) { - struct vm_area_struct *vma = vmf->vma; + struct vm_area_struct *vma = vmf ? vmf->vma : NULL; unsigned long pages; int order; - orders = thp_vma_suitable_orders(vma, vmf->address, orders); - if (!orders) - return 0; + if (vma) { + orders = thp_vma_suitable_orders(vma, vmf->address, orders); + if (!orders) + return 0; + } /* Find the highest order that can add into the page cache */ order = highest_order(orders); while (orders) { pages = 1UL << order; index = round_down(index, pages); + /* + * Check for conflict before waiting on a huge allocation. + * Conflict might be that a huge page has just been allocated + * and added to page cache by a racing thread, or that there + * is already at least one small page in the huge extent. + * Be careful to retry when appropriate, but not forever! + * Elsewhere -EEXIST would be the right code, but not here. + */ if (!xa_find(&mapping->i_pages, &index, index + pages - 1, XA_PRESENT)) break; @@ -1735,7 +1745,6 @@ static struct folio *shmem_alloc_and_add_folio(struct vm_fault *vmf, { struct address_space *mapping = inode->i_mapping; struct shmem_inode_info *info = SHMEM_I(inode); - struct vm_area_struct *vma = vmf ? vmf->vma : NULL; unsigned long suitable_orders = 0; struct folio *folio = NULL; long pages; @@ -1745,26 +1754,8 @@ static struct folio *shmem_alloc_and_add_folio(struct vm_fault *vmf, orders = 0; if (orders > 0) { - if (vma && vma_is_anon_shmem(vma)) { - suitable_orders = shmem_suitable_orders(inode, vmf, + suitable_orders = shmem_suitable_orders(inode, vmf, mapping, index, orders); - } else if (orders & BIT(HPAGE_PMD_ORDER)) { - pages = HPAGE_PMD_NR; - suitable_orders = BIT(HPAGE_PMD_ORDER); - index = round_down(index, HPAGE_PMD_NR); - - /* - * Check for conflict before waiting on a huge allocation. - * Conflict might be that a huge page has just been allocated - * and added to page cache by a racing thread, or that there - * is already at least one small page in the huge extent. - * Be careful to retry when appropriate, but not forever! - * Elsewhere -EEXIST would be the right code, but not here. - */ - if (xa_find(&mapping->i_pages, &index, - index + HPAGE_PMD_NR - 1, XA_PRESENT)) - return ERR_PTR(-E2BIG); - } order = highest_order(suitable_orders); while (suitable_orders) {