From patchwork Sat Jul 13 13:24:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 13732410 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 C54EEC3DA42 for ; Sat, 13 Jul 2024 13:24:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CCB816B007B; Sat, 13 Jul 2024 09:24:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BDF4C6B008A; Sat, 13 Jul 2024 09:24:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 79A126B007B; Sat, 13 Jul 2024 09:24:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 434B66B0089 for ; Sat, 13 Jul 2024 09:24:40 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id DC8EE40F3A for ; Sat, 13 Jul 2024 13:24:39 +0000 (UTC) X-FDA: 82334799078.01.D274F95 Received: from out30-119.freemail.mail.aliyun.com (out30-119.freemail.mail.aliyun.com [115.124.30.119]) by imf03.hostedemail.com (Postfix) with ESMTP id 8DEB620016 for ; Sat, 13 Jul 2024 13:24:34 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b="hI/dD1bA"; spf=pass (imf03.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.119 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=1720877042; 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=sinsckyfDrmoitGcxiS3VkelD+HV4MU/yUGCp8rV5USnXJwU3fkuXGy75p0NW+SA2S+KKZ Xybz1TBQdWRcvMo9/DeAoqUD+EAv7EvAEtucyUSN1VomYKQMuHgwA2rocBrdImqLbcAohq MHiy3skXGg9nmf/kCbkc1qEWd2fP81c= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720877042; a=rsa-sha256; cv=none; b=oIrVqT7p92TJfApj5n8hzuOwxCtJo5l84n/Rq9pGPye+BtIB9kpSnV/U4UmGaQUYzpfLFB Aik+D29L65BbJQV+Nefj+smS1oL3OQPKHMVjE26AvQppBt+ICo2U56W2vwfbc6RyOmIyI1 kAHFz0rFEiwg/alybsAb6iJpfAaDMfw= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b="hI/dD1bA"; spf=pass (imf03.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.119 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com; dmarc=pass (policy=none) header.from=linux.alibaba.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1720877071; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=Djj9GEjNq9Gnbpe2Z/mIcDmYI8xxQmETjEn+X/PnVoU=; b=hI/dD1bAmCsu7NU26ZIi5BEn65UvoiDvt6vLXlS54SdCJnY1jlLHy6C8Ap1ao2Qo5Dta936a910Tu94g5mFklZoLjYGHWHIawcBPVnWbY1ZajvGy3EI8hGSBArhN36+dIHtKHQpUHo2PK2yn1JBcIQGppgdqgd8lGSIx7jtH8hY= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R171e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033032014031;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=11;SR=0;TI=SMTPD_---0WAR0qHL_1720877068; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0WAR0qHL_1720877068) by smtp.aliyun-inc.com; Sat, 13 Jul 2024 21:24:29 +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 v2 1/3] mm: shmem: simplify the suitable huge orders validation for tmpfs Date: Sat, 13 Jul 2024 21:24:20 +0800 Message-Id: X-Mailer: git-send-email 2.39.3 In-Reply-To: References: MIME-Version: 1.0 X-Stat-Signature: tfeprgrxbaza8dyfd37b9f4bdbct1x9h X-Rspamd-Queue-Id: 8DEB620016 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1720877074-980367 X-HE-Meta: U2FsdGVkX192jk0CQQThR1bJiX6bpYlrbdTWJ78hmbuUWLb4r/ikCfTCex7LZ2yPaFvqivJaYJDR9372onduWFqSQPUXms4KqGFQuAaFxvRtc3ya1rlyV6U4aY1PD/ZUHthZp6XytQH9BBPWkJuV9p32Sa5pEWSV+UJF+rR3YCR2/+VJcnxc1+XEgxiw3A+iCOetiRf7+2UV/ad+8wH3mkqhMVFM5pkcsvmr3/Tcl9wzyDZlk/D1uk2UyuIoIlzaCoBBs353MqRYdnCs8eH/eztMX6ZQOXLEmqqA+dtk9P8M9U1ygCdU1O+aC8vY7p4OSrfKYcQmIH7BERnjbNqAJEsnMyXI6fJDtkrfo7Ux+HZoB0FDA9TuxRcdcQNJKm8kZSjG9mJqnnC5sUPDCDcch8dyvAwxLBqjpf2zIpWr1+Z4Kq8noSymF1FIkDZMmIClQoutSXNLmyUWPSD+ILTOl56CbWDnyZnuVtQWugWiIywVFrMG67WRKs7JfWwf7C/khKg5x5hl3VJGnb5XHSc9/iG/icIdut2u5pI4qGxOMZi8yP47nwMxquAjUxuUVblx5fslhIaUmZjV+drCx0eBzLnQPSHEoQn2N//VxfbWTuNrlRyFPzLj87HSoIuvWi+HGfKuZDSPGbh37FELbN+EnYghTf11VkbsZE8ik4ouHO74eu1rGhHCsdaRSz2Vkk4pHdnMzs3zQP32LeOCs+ri7bI9WvZFXANyFrwlw2uECKQ1ydHSHYcFbYktXrr4uKQmtS2DVI2hPM3I8A3YM/kIrgOKWj/Qe8KVOyb0nugmbhN82JUJ9+3RZD8n7GJfmPZPDedG/eqV8743Y4DIo63c0QcYi10t8+89GtagzM0A+jy/NImCLZbMI3nczGImrP9xIvBUkdcDZi1uobzOvpaDfr2DWsLlSwy1Mtf2G/1oQXKK1hNgjwaDQ36GGeiLgHdD2CtMzuSeGabH+afOlQa p46R/m/o FXydWkS15v1S40JlikOm1YAaU14pYK3/lQXliBX+jhV23iGqJeKoVf7l/x5DzkvjQjbr5/wWazFQQo/wVDrzdg9aqinb5VNMop9SHPOc4WyXGPLZVRlbxc6KgNgJtl+AivY0xC3xCuE6+kf55/DjFK0a+1ceEdZbQUYJILGMOws4xkl80/JccWhOCB69hktoDby8AK23XtYF0AaQ= 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 Reviewed-by: Ryan Roberts Acked-by: David Hildenbrand --- 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) {