Received: by 2002:ab2:7b86:0:b0:1f7:5705:b850 with SMTP id q6csp1255241lqh; Mon, 6 May 2024 01:47:23 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUGPcBchPOx7urV3K4tsPV4PvpGD/JsY66W0LbCitH9N9kcPvydxx8DT1S371Rpf2RzrIDSmKx4vQANZtSlByciA3HM+H1gcKF7BRnaNA== X-Google-Smtp-Source: AGHT+IGPUPXjPegpCdo7JMUDdGDHV0b0g5Mv6UBn/s4uwVcF+fpsk31h4jFY1fct3ch9V4/8JYkM X-Received: by 2002:a05:6a20:c886:b0:1af:86e4:bc99 with SMTP id hb6-20020a056a20c88600b001af86e4bc99mr11046721pzb.10.1714985243026; Mon, 06 May 2024 01:47:23 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714985243; cv=pass; d=google.com; s=arc-20160816; b=aYzAlVOKYFKP/mcf9Q/11VrMCorTNLzAnKoAiQrNwLg4OK0RZaEMb4JYc1InoAWJ0O WiGb/h9sumZ4A/8F6DyztYnPqozV/ybMVC1vLbEVctgsPoeSQycT3VidPQQT0l01SpSP +CYUTS34NfzIvVP2pIXDB5B0D33QrCSPgHs11iBJo8w9ehMJhmBa/2wsuGd/hb2sLbQ2 x5JSF8ld2DjJdjubdjX6jFxI0iAnj3hDQ4L6iLlLoFVDQdOJEewj4JbrGNAIblmk5nVZ HbqB3w9jEl/hUm2AGQtWez1xTZ09xWt1/hDwIL8TlN6THsY2bHMv1dnl5LSg2psOoYj2 GSqA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=zEr/Eu5mgqpEki8XSyTh3V+LovJcAoeI3KW1/F4hkgg=; fh=QbwS3auWn4deePzqcQEgPh0o+FZG4BOPGyVFHXP9ySY=; b=UU7Uu33dMf4fXkL44QsyrJD3sYa+rB95+hzhQrySdB1+jwC7oOqy8qNeHzE2namxUE se/+7YHTYiZiJFYuGdFFQ1WH2w/G0pWVgsp76lhx9WMAhEZSquEZPGPnr39kjses4sxw wt2QsZ2mrjwhBByxXPwXjlCdgPmg8Qx8EzARPVctWFfjg9DCtvDepJMEkuXzeRFaE9Iv xMj3fewu3zDLWNQT1/o3dXflr86kQZCl4tF2cPPTOCscfQXTrP6wadfUT7YZWV75/Y0r +NLWRXu5lL75rg0qxnjbqEaTj9jgVVayuOlOxAin9Zx/YDmC1PCX7e/j13BpSSfUzv6w uFZg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.alibaba.com header.s=default header.b="VaC/kRe1"; arc=pass (i=1 spf=pass spfdomain=linux.alibaba.com dkim=pass dkdomain=linux.alibaba.com dmarc=pass fromdomain=linux.alibaba.com); spf=pass (google.com: domain of linux-kernel+bounces-169520-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-169520-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.alibaba.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id ey19-20020a056a0038d300b006ecfa8519b7si5235342pfb.7.2024.05.06.01.47.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 01:47:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-169520-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.alibaba.com header.s=default header.b="VaC/kRe1"; arc=pass (i=1 spf=pass spfdomain=linux.alibaba.com dkim=pass dkdomain=linux.alibaba.com dmarc=pass fromdomain=linux.alibaba.com); spf=pass (google.com: domain of linux-kernel+bounces-169520-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-169520-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.alibaba.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 7CA48281FDC for ; Mon, 6 May 2024 08:47:22 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A8628142625; Mon, 6 May 2024 08:46:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="VaC/kRe1" Received: from out30-101.freemail.mail.aliyun.com (out30-101.freemail.mail.aliyun.com [115.124.30.101]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CE1601420A6 for ; Mon, 6 May 2024 08:46:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.101 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714985212; cv=none; b=pKEkWJs4gdYXEA1NY0VBvi376OZtjzOVkpom3j98AzTBvTAgJkYEdrj617645G9mlmLHMlgB7EXYMwvfC+TBiJCzYT1GHLjT3dFNBHuO5xSacRnLyTg2ELByKdRzu5SDx5kH416IQf7rWWicaGgZLtsro8KFdOsW8+mhSvHDr+c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714985212; c=relaxed/simple; bh=TtEvthxzqcH6fibOTrZH8nZvm8Cacr0Qx1M3RVs4Sq8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kiSTuLvQvKid/P8R/UjLKIUs7OC0NsQj09MQSCMMg6pGPFbuZigDeUpvVDdxTCB5nXSKE3t0unBp/GuWOHWLQyvzCXZr7++iie4LNrjXDKg7nHwnq4f66/bB1IjQiUKcjfmtL04UUIdQYWf9DyxxVvYC5umLajgjlKvCjg7POoY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=VaC/kRe1; arc=none smtp.client-ip=115.124.30.101 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1714985207; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=zEr/Eu5mgqpEki8XSyTh3V+LovJcAoeI3KW1/F4hkgg=; b=VaC/kRe1uXXZZhETUNZOyPdATJLv9wdN9iawFkKUMls4e7lUzINH4uM0PxZENld2U6PYzjxI7QUSAKP0Az7SdKF6WA6ea1z1iMbU4a+R0Od02k2s4ZvGl6Pkke/GkaIFIoCl7ij1a0NoZcbMhyA1FELXiH5ihqdVzv3+OtK4feY= 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-contentspam033022160150;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=14;SR=0;TI=SMTPD_---0W5weiKG_1714985205; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W5weiKG_1714985205) by smtp.aliyun-inc.com; Mon, 06 May 2024 16:46:46 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com Cc: willy@infradead.org, david@redhat.com, ioworker0@gmail.com, wangkefeng.wang@huawei.com, ying.huang@intel.com, 21cnbao@gmail.com, ryan.roberts@arm.com, shy828301@gmail.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/8] mm: memory: extend finish_fault() to support large folio Date: Mon, 6 May 2024 16:46:26 +0800 Message-Id: X-Mailer: git-send-email 2.39.3 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Add large folio mapping establishment support for finish_fault() as a preparation, to support multi-size THP allocation of anonymous shmem pages in the following patches. Signed-off-by: Baolin Wang --- mm/memory.c | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index eea6e4984eae..936377220b77 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4747,9 +4747,12 @@ vm_fault_t finish_fault(struct vm_fault *vmf) { struct vm_area_struct *vma = vmf->vma; struct page *page; + struct folio *folio; vm_fault_t ret; bool is_cow = (vmf->flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED); + int type, nr_pages, i; + unsigned long addr = vmf->address; /* Did we COW the page? */ if (is_cow) @@ -4780,24 +4783,44 @@ vm_fault_t finish_fault(struct vm_fault *vmf) return VM_FAULT_OOM; } + folio = page_folio(page); + nr_pages = folio_nr_pages(folio); + + if (unlikely(userfaultfd_armed(vma))) { + nr_pages = 1; + } else if (nr_pages > 1) { + unsigned long start = ALIGN_DOWN(vmf->address, nr_pages * PAGE_SIZE); + unsigned long end = start + nr_pages * PAGE_SIZE; + + /* In case the folio size in page cache beyond the VMA limits. */ + addr = max(start, vma->vm_start); + nr_pages = (min(end, vma->vm_end) - addr) >> PAGE_SHIFT; + + page = folio_page(folio, (addr - start) >> PAGE_SHIFT); + } vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, - vmf->address, &vmf->ptl); + addr, &vmf->ptl); if (!vmf->pte) return VM_FAULT_NOPAGE; /* Re-check under ptl */ - if (likely(!vmf_pte_changed(vmf))) { - struct folio *folio = page_folio(page); - int type = is_cow ? MM_ANONPAGES : mm_counter_file(folio); - - set_pte_range(vmf, folio, page, 1, vmf->address); - add_mm_counter(vma->vm_mm, type, 1); - ret = 0; - } else { - update_mmu_tlb(vma, vmf->address, vmf->pte); + if (nr_pages == 1 && unlikely(vmf_pte_changed(vmf))) { + update_mmu_tlb(vma, addr, vmf->pte); + ret = VM_FAULT_NOPAGE; + goto unlock; + } else if (nr_pages > 1 && !pte_range_none(vmf->pte, nr_pages)) { + for (i = 0; i < nr_pages; i++) + update_mmu_tlb(vma, addr + PAGE_SIZE * i, vmf->pte + i); ret = VM_FAULT_NOPAGE; + goto unlock; } + set_pte_range(vmf, folio, page, nr_pages, addr); + type = is_cow ? MM_ANONPAGES : mm_counter_file(folio); + add_mm_counter(vma->vm_mm, type, nr_pages); + ret = 0; + +unlock: pte_unmap_unlock(vmf->pte, vmf->ptl); return ret; } -- 2.39.3