From patchwork Tue Jun 4 10:17:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 13685017 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 039E6C25B7E for ; Tue, 4 Jun 2024 10:18:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DABFA6B00B5; Tue, 4 Jun 2024 06:18:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9393C6B00B4; Tue, 4 Jun 2024 06:18:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6EE146B00BA; Tue, 4 Jun 2024 06:18:14 -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 0F0706B00B4 for ; Tue, 4 Jun 2024 06:18:14 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 922651210C6 for ; Tue, 4 Jun 2024 10:18:11 +0000 (UTC) X-FDA: 82192805982.08.FB90DD5 Received: from out30-118.freemail.mail.aliyun.com (out30-118.freemail.mail.aliyun.com [115.124.30.118]) by imf10.hostedemail.com (Postfix) with ESMTP id 71371C0022 for ; Tue, 4 Jun 2024 10:18:09 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=j0H9zn4a; spf=pass (imf10.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.118 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=1717496289; 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=guoh/QGJfMTFO1luBsme9MpL+CeGbbXVEAqgA5Cl58c=; b=8FcHjOZ2PsHzwlKd4wEIcuxo23H++z+WQHpZ8HqEA016C81LMdA+XOv+umKHt1rfI6zQT9 f/+d/CjtkFPxsdFYo7eKW0iFjTKmYmhiOohWYM72w8oQyOf/tg8Y+MFvG/f3Vr+U3+Hm76 SHGXuKNrbzL2uKy1Dc9QkV8evmqkK98= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717496289; a=rsa-sha256; cv=none; b=PiT4IBAwnlcbcLH+E3aMBtu8J7O7zGfp/I22hhGwZyuzEM0Kfw/AMt+3jTPXw+5Y4w3IDM dE96k6XHrRpcX2Sz4xzf30j5V/ptD5pmnMGCoN6iYpUR8QAjYvZhAmFAoc/93kit2F5fz3 5ZnVxsWulCsRzPYpb17yIjAqNtoV9u8= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=j0H9zn4a; spf=pass (imf10.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.118 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=1717496286; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=guoh/QGJfMTFO1luBsme9MpL+CeGbbXVEAqgA5Cl58c=; b=j0H9zn4aUzodZDSL+2M/DWjU07FGxDDpvnIdV39pFW4WBmi9JKRcZB7joAj8LNIeZwqcD5aWI+FBXA/fknfMAa7rc/6kUA95A0kgkju2hShuF66N/coDe3EuuSyloL1p84KX4r+6OlAoHk6kMMpeWw+kTC0yF60w4CogLgXxSHs= 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-contentspam033045046011;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=16;SR=0;TI=SMTPD_---0W7qooHn_1717496284; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W7qooHn_1717496284) by smtp.aliyun-inc.com; Tue, 04 Jun 2024 18:18:05 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com Cc: willy@infradead.org, david@redhat.com, wangkefeng.wang@huawei.com, ying.huang@intel.com, 21cnbao@gmail.com, ryan.roberts@arm.com, shy828301@gmail.com, ziy@nvidia.com, ioworker0@gmail.com, da.gomez@samsung.com, p.raghav@samsung.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 1/6] mm: memory: extend finish_fault() to support large folio Date: Tue, 4 Jun 2024 18:17:45 +0800 Message-Id: X-Mailer: git-send-email 2.39.3 In-Reply-To: References: MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: iwor3f9rjy4ioe1fkbr8njusgtqjbfze X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 71371C0022 X-HE-Tag: 1717496289-381085 X-HE-Meta: U2FsdGVkX1/kSulS8nHmZTlwIYNdeT8FE6vdtfOPnxiwMlOdbNovZUFvSwCV0/cTta06dcDmiydMbWcSEGw7OigvEDVdPHjdMwEjI5vRtav/r0XPrvPfKBAxpb9TLH+3Xx6dGxnow0AcJfyYLIadk0mxBACnNHq9+9l2tHDvlE57vfoSSGLxT0lEbnAXzqz7KFrGSVlgqQ0gxcPSKUJ3BVOKOcKvyUjLNKtb52GYnVxmgbSkWxkfijNPI29dP+cGW6LtX2j6jivHMtpjb8gJq5cZyUe41rvY3bxvG71mhKZCuncomSDBm83o43GS0eZy5MipMeAHP0gl0ET/3elim++Owzvsl0DBxHvseUY4pEaEwphmB0tPRy57QwI8CvOQ/6SxoLH49WGkJpqoN9ABl7NO7/6PVidAv0LTIGcRL2tVbDTIniC3+cRGB+eBO7Fmu2ar5KOv09cWML4Guq2BUSpIStJHUl9qZzYYxTbTpj42I8BqOawGazgr4YdI7e8vuYi27/4kKm0WYqI0ZpgcVUvlR4Zbt/2yRQLlr7I0i4LxHpTB2Bvi7+h28AW2p4JpL11Tf7ga5QGme1AVZx14zkNH/xc8qp9C7Xt2o5Nj4Fma2iR+FVWRcIcXoOlKxx5M09kVTjAP0yo+NEBg2v65l57+i77JYl1UI/KvsqzZCSMGkpppf6r9tDsJEDrBtjsoQA98m5fCyrxKcglhaOdJapgihnuCdbBIqWV+NMZ13pD+5hf6bP5Lh3S/vkkrsViwtXks44zWz5QfoxEGlCx/SAmTJAyXe8U2TrYou1mBByn8yYyanibv0nwMubU/5CtziH/8WZ8wccCDwWO6JD7+NTTGh0WmkMmwrJ1UEMxwkcTgowtghaFVBi2utywIHnDCTPgUuI1olg9rxyf/QP+KAjBrCyXRg6OlT/m1YSzUlwNcm1cgKluW83ZQF2bPh1j2yTwz2Y2JuYTpakPjmP5 n2ySx488 0lVd2n0oX3/ii3SKymNpqxMRSPHqh9r7IgQFa1wCoQDfvXTjU50P2IKusv8lrWFKgTQ6vpaKsbclBnHzTcaAMQ1muGKcUMpCUhjycT0DQsA6eX5pWTEES0Wu+U2C8r8QbqcpKE+oGA5xLu+rZBBWPIWHiW5mHDnL1eyk8I0HiDdmofHW+Frb9fThtJ104JcRNQ6Oq9a6XhwVrTOn6O+jDEv0Y7Bm65w0fUOzTKdyRGw9O2NLrtIjNVZYPrzbxLG9BryxkQPBjPzJPdA/FVVvOjN6RuWGDbsJcOLDK 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: List-Subscribe: List-Unsubscribe: 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. Keep the same behavior (per-page fault) for non-anon shmem to avoid inflating the RSS unintentionally, and we can discuss what size of mapping to build when extending mTHP to control non-anon shmem in the future. Signed-off-by: Baolin Wang --- mm/memory.c | 57 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 10 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index eef4e482c0c2..1f7be4c6aac4 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4831,9 +4831,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) @@ -4864,24 +4867,58 @@ vm_fault_t finish_fault(struct vm_fault *vmf) return VM_FAULT_OOM; } + folio = page_folio(page); + nr_pages = folio_nr_pages(folio); + + /* + * Using per-page fault to maintain the uffd semantics, and same + * approach also applies to non-anonymous-shmem faults to avoid + * inflating the RSS of the process. + */ + if (!vma_is_anon_shmem(vma) || unlikely(userfaultfd_armed(vma))) { + nr_pages = 1; + } else if (nr_pages > 1) { + pgoff_t idx = folio_page_idx(folio, page); + /* The page offset of vmf->address within the VMA. */ + pgoff_t vma_off = vmf->pgoff - vmf->vma->vm_pgoff; + + /* + * Fallback to per-page fault in case the folio size in page + * cache beyond the VMA limits. + */ + if (unlikely(vma_off < idx || + vma_off + (nr_pages - idx) > vma_pages(vma))) { + nr_pages = 1; + } else { + /* Now we can set mappings for the whole large folio. */ + addr = vmf->address - idx * PAGE_SIZE; + page = &folio->page; + } + } + 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)) { + update_mmu_tlb_range(vma, addr, vmf->pte, nr_pages); ret = VM_FAULT_NOPAGE; + goto unlock; } + folio_ref_add(folio, nr_pages - 1); + 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; }