From patchwork Thu Jul 11 05:42:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 13730032 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 EE52CC3DA41 for ; Thu, 11 Jul 2024 05:43:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0BA2C6B0098; Thu, 11 Jul 2024 01:43:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 065786B009C; Thu, 11 Jul 2024 01:43:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E223D6B009A; Thu, 11 Jul 2024 01:43:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id C133B6B0093 for ; Thu, 11 Jul 2024 01:43:13 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 2DA7A403DB for ; Thu, 11 Jul 2024 05:43:13 +0000 (UTC) X-FDA: 82326378666.10.DC50DCF Received: from out30-124.freemail.mail.aliyun.com (out30-124.freemail.mail.aliyun.com [115.124.30.124]) by imf08.hostedemail.com (Postfix) with ESMTP id 8E9B8160002 for ; Thu, 11 Jul 2024 05:43:09 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b="E/HyPgbx"; spf=pass (imf08.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.124 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=1720676565; 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=Djj9GEjNq9Gnbpe2Z/mIcDmYI8xxQmETjEn+X/PnVoU=; b=uF6EtFeqpmORQgYlmFzfHpGKHoszQmeqLE3b2dK1upwb69PEHpTtDfKA9hEnZj3oAxyy1P StNiuCYXUwSJ5lZpjN7cPDlpzbKygteWVWw+s5FAx4LiQVBT8g2QYl2Wis9IunqDZ8aIMm syeI4/sKR8dXyCZhDiHbR0VpR4mMlTg= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b="E/HyPgbx"; spf=pass (imf08.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.124 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=1720676565; a=rsa-sha256; cv=none; b=PSdi+BAJCLKxOj5R7qM9enL5H/KvXDYwLQqAkSZjfYOjy4YQzHHXz1hbadkg25+kCc0/92 7BD/2eO650gl4G6JPL+JIL6ch3o9YYGtXegiGBoxha+eHKG6o5yP/wGhoZucvlaq8guPW7 r9/sYvGVEPuR/UZy26fMEYhjRg+Ay3w= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1720676586; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=Djj9GEjNq9Gnbpe2Z/mIcDmYI8xxQmETjEn+X/PnVoU=; b=E/HyPgbxT2tqaJkMD7lot699FN5JX4U0B0jR3QkIpBH4QudtHj4OpgSRujeGpXl7VVTemRgoy3B7fU8jMDdE99K/XgpHzIrRh7eKtSxqyxFqOWqrlhOmx5qd8wdtBMRCZ1vjLDXHMpKAf/KiUVgwmgg+Tdp4UvmWH7HD4/45eYw= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R621e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033045046011;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=11;SR=0;TI=SMTPD_---0WAIiL2T_1720676582; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0WAIiL2T_1720676582) by smtp.aliyun-inc.com; Thu, 11 Jul 2024 13:43:02 +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 1/3] mm: shmem: simplify the suitable huge orders validation for tmpfs Date: Thu, 11 Jul 2024 13:42:48 +0800 Message-Id: X-Mailer: git-send-email 2.39.3 In-Reply-To: References: MIME-Version: 1.0 X-Stat-Signature: stg5694wfq8fyr1jdc7egg6gd5cwq5xx X-Rspam-User: X-Rspamd-Queue-Id: 8E9B8160002 X-Rspamd-Server: rspam02 X-HE-Tag: 1720676589-349200 X-HE-Meta: U2FsdGVkX195OJkiMeYr1QMSlZFvQr53NXmtVV3T6w1ObHhhfxcCP8E6+d/Ra6CgLNPwH2dJWBBC6GGULjLJibQoh+Fp0b/xv3gliqQTLYHpGFO0rNA/RDHFTRhsvCpbxY/qE8MPszayPWnPh0G6iXc/5dp2aC39ouj6Yap+EHzOh4ILs0KaH/Jumdu3MTRA230k6YK67dhfN7PngnAacxQmbjfUjU4Zko7hkzuUiwqtc9w2rKbJmGco7oa2Y+H0x/mysY2750ksfo6KVj6ujbw/DhKmtQvZ2+yFEJ0IWgbrAlSNMbZ49j/+HTPKjI614cQZblhxmYnLsLnJCQrtyRMxDtrFBuTkNbxRggKB/WVz57EUw278O9YNKtwj0mqimdltf8wZH6z0QKwQORRb4Xztsg/I06AMIX6d7zfyjuBwUJwwwribN4uI4RQBUPZGObhyft9atFb2hS5zS9i2or7Qd7VshCT/MpKPl8V046qT8C4xA1ih8U+yWW3hTB4DyIMmE1kpHPkPyuIR7FXbX10I43zPAO8mKTQs4MJ2ZtdTOGNTlGMULtNcNLmTmqmYM7VyiywUG8UezfV5n4LpQOH14TSxaRGJmLo6j9d1waBj6CXzsOCO4xebqYWCHDDJAf5faWTemQ6dVg5K3jLpsUv4iQW891eUz+mflji7TMFDfMMhA5UOXogHP5tCJiId8IcL2ro6bfT4kkxIk3UUGIQfiLQ0bQMgLluJqOxj2bW8AaJdP+3HViiPQsu647Q0caqPpsfSH/UafX4hHvOwPbXRqFKi2o5oNIlTdUJloQHblYKceud0zXwpkzLn8KpgImLGnCvSfqmX/zMuT6YCc3Wy7VYsNy+9sj8Dvkg8R7lME6KelpfJ1vVctRi1Q4lwMOW5z02tYZkmwDgs7Dwtyd5TnbB/b6EAeQTOsLNyrn+y5QmD+wczjvXZ58Ra+bmO+oiXdZDyx7ESOrkMwUY WzHS7Qeq 0yC4fu0TGUi3cszQObV5xjMqwm3H0r2RvdDsULLAJ5xK+HFinLgUfgir9W4jKwdWGmlQKpFmgkZZuyip2lc3k8mJAyW0R/fAdYVLMN1BOKF8mtqfwo0B0wvnjNh9doJxPj9Nakvd42W4NimlhSgH9KrL3Z+0QyBa9FDNZPWLvhVpo6gZZO2MYSmiPujQdRZ+JDjQyYXCFW0O48FnIfmQdgQFnHmFJHmmAXsRVrWNbgSCKXEA= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000031, 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 --- 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) {