From patchwork Tue Jun 11 10:11:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 13693370 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 B2E65C25B76 for ; Tue, 11 Jun 2024 10:11:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 37D496B0092; Tue, 11 Jun 2024 06:11:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 32C6C6B0096; Tue, 11 Jun 2024 06:11:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1F49B6B00A0; Tue, 11 Jun 2024 06:11:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 00DF76B0092 for ; Tue, 11 Jun 2024 06:11:23 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 736F0140A93 for ; Tue, 11 Jun 2024 10:11:23 +0000 (UTC) X-FDA: 82218190446.05.370645C Received: from out30-101.freemail.mail.aliyun.com (out30-101.freemail.mail.aliyun.com [115.124.30.101]) by imf10.hostedemail.com (Postfix) with ESMTP id 7144FC0010 for ; Tue, 11 Jun 2024 10:11:20 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=keGRBWiM; spf=pass (imf10.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.101 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=1718100681; a=rsa-sha256; cv=none; b=OGKOrTbKDmbY0R0929+E50WQSENw0QVqXyIYrYDqIyBId9jTNNYN1hr/m2BCPlxnIEoxVo BZCtMXBTZFEHN/zaaZG4JCLlaKc2NSRqfKBLGCA2GWjOoz/RR/J+AoQsdjLi69Xnutd8tP 7XJFWquUDtp1t0YSBNZ/k3dJz9KN8LQ= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=keGRBWiM; spf=pass (imf10.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.101 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=1718100681; 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=W4Bbp1+23OsISWkX2qQa4Pc7burV6pvce6vC6TkTWEc=; b=k+mWhC2cvQDJZ31RWyqK0QScEcdqHKtTTmP4r7lSBSTelolZsUJYQ5Lj3r37wnD1hayB3G pWCmgyWyuY40EU8zJjpMKjpYzUot+tSWtvMZC12Pqy/K5RqOX+0Rj4b0p4nyq4Ledw/si3 l8GHqiIQ4Lqk5Q/i0l+JjyGB1F1AzFc= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1718100677; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=W4Bbp1+23OsISWkX2qQa4Pc7burV6pvce6vC6TkTWEc=; b=keGRBWiMU/XZpK/a7q6sIrhjChl5HCxfUvUrYZ6CpusVWZcyUAH2aTdjDk/TvjA+KmY/HYNGpMrB0e2cIGHsG/nK96LPKYLUAduKKqkmyFc6m+iaEfaoMFhIXPpcPrpvZzSOV5H4qDRUurbhiQ4/ypbecNQRa0TTmg4sYVjc7wQ= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R671e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033045075189;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=16;SR=0;TI=SMTPD_---0W8G7ELN_1718100676; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W8G7ELN_1718100676) by smtp.aliyun-inc.com; Tue, 11 Jun 2024 18:11:16 +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 v5 1/6] mm: memory: extend finish_fault() to support large folio Date: Tue, 11 Jun 2024 18:11:05 +0800 Message-Id: <3a190892355989d42f59cf9f2f98b94694b0d24d.1718090413.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 7144FC0010 X-Rspam-User: X-Rspamd-Server: rspam12 X-Stat-Signature: xq67htspyxipk45zh4xnij5sizatyzfu X-HE-Tag: 1718100680-510218 X-HE-Meta: U2FsdGVkX1/1hWxQ4H8i1qfUmzXvs0Q/28lY34sNtyAoaX0e+sj5V2FhmKn1E7SHSZQFA5mmoWBCoNc6aVyMvGIgMRyFf4ImENOzN/7B/rD5JgAbp346nFENVtT2a0cFugVYmphkVKv4oU0pjkGSH1qlnKQ7h2MhcBDPV98jY8vuVW6VvPv/sGgnoURRsvMB1KQfa6lN+e0CcP9fAODCkUmDm//kv7h41ipubAiyP3aV514n5tRfQahBr0/8jnUgQbjSOs4XdB30sqpRcWsLm7mGp6+Qg6rFjgrcVHUg7TTD8Jf/YP3DD3NS/gKt1embeHyA4l+Xauxx3KfwSyaraIFwv/pvQujqtJAESJqtHihjLLq2hO7NkPWlAbg6Q+hf99zz2mxSbZnLA1BKzC2tUWzJlCrdoJfv6Qyz+GOfL5ye2DxyTbwnyzsmQLM6UJf66kvv6wnE6chkjuq7fT+19uACENcLCXcCI6f0W2dkAIj5xCPxTiBfaPhfCd3ejdLjwDDQxL6i+bMo92xgg0oFAQ48RiqE1AGO0dEbSifOP2WWek0cvDFgPxwgeGvYcqBQD+U4vU9atooMW2yBtOw5vZd8Mxi4F52D3OeXdpzJ4b073RDMXLbwq8TwNihZVAic6hY4dOKKiCe19rLq9wzStN4rJEtj+ZUnX1w3F4G2rhlOAgqtnXvU8gZr6GWaalq9/jbWFnJKNP2xCvTDkIIwKhi6nUcZ+FLsCViItpbsr2EB+kL/unXYCYap98bC8DD+ytFTA7FxKWYsa1W7IR4aEwlyR6It8onTrKuKnJjTGrHOEcpUsUW2mtJ2a6f7HLHdh9/Z7a7y/C6uh1MRkRUZfeq7QDxCIPebaAm7ruo22pePJCbugc7Pur9P0lJxL+tpcNl1D7N3rM6x0udO+qXeXIaha9pR3qcPF6oYSmEc2Rf/zVgm4fPzk2GFphGQ5T0rOPU+lbVO6c1mofSZX4V 9jhai9+8 Ds9hgTVoCaJM+eshZW+VnLjlDpABZsMYtXLi6AuYZqsNMvMG9FR6R7vJ1FymyM+Z1q9sYficrlPiShxWZ4tvKv/TGybnDf+MEO2lBd05pAS+VHFCDXphXbMfdnY8yL3xfeSOVVsNN3IAaQEwJ8E/LLEzbCc0VYGe9brKEwldZrtbFKbArZQB2sGVWleirY1A2OTQ9Yhjnhhmu7YV/RYFx9IWGXt4gZBxEpVk1Sw7srCfVsDc8qVRzlFtyskv/J1nibUdRkdlB4KrB6AUgB/6NT1Z7+g== 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 Reviewed-by: Kefeng Wang --- mm/memory.c | 57 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 10 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index eef4e482c0c2..72775ee99ff3 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; + 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; } From patchwork Tue Jun 11 10:11:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 13693371 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 B4D67C27C75 for ; Tue, 11 Jun 2024 10:11:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DB7826B0096; Tue, 11 Jun 2024 06:11:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D40816B00A0; Tue, 11 Jun 2024 06:11:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BDF696B00A3; Tue, 11 Jun 2024 06:11:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 9F91A6B0096 for ; Tue, 11 Jun 2024 06:11:24 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 4065F1412F9 for ; Tue, 11 Jun 2024 10:11:24 +0000 (UTC) X-FDA: 82218190488.04.ACE6BC4 Received: from out30-112.freemail.mail.aliyun.com (out30-112.freemail.mail.aliyun.com [115.124.30.112]) by imf24.hostedemail.com (Postfix) with ESMTP id 1F48D180006 for ; Tue, 11 Jun 2024 10:11:21 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=G+S8m3CJ; spf=pass (imf24.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.112 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=1718100682; a=rsa-sha256; cv=none; b=vz0UAGhO/yU41/l6gi4LAVSZ5nDKGLdR5AjbVyuAZM/juECyVyEimNVCGq74khktBtjqNh hh7LH8vd+LBXNujrzLoA2KomAYZXV7I5M7mC+iln3kAHstxhSUHKCuT3xwSBYwIn/NNviT uBIkTrIlRMKus3BFTz7yeoDBhMonb0k= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=G+S8m3CJ; spf=pass (imf24.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.112 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=1718100682; 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=sk0VCEBLx9HEPQafud6f811f0Tz6WgnTKVbuudZtsuE=; b=Vd81aBtDGyBcZQr/0gysF2x4hO+2By0P0Np3h/yuy7gMOc970P6IHyGlZgrz5PN6dIpKDE JGVbLL1BFtIx4v4JI/rzFwNjE2LpY4xhmTMcAanMp8QWkbfWhd0omLEGIHtnZGyzr8Tnyq eOxC2vfQYSYchmxbhZbQ/XQiJ3jQGis= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1718100679; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=sk0VCEBLx9HEPQafud6f811f0Tz6WgnTKVbuudZtsuE=; b=G+S8m3CJeaGO/N3L6M/HIQFECjbieubXUYafz8SHfbvukr+zHxZjC7+yrkmKRvRxj22CHcIrRDKBMQ1s1tSrCP813AzIOuhW/3RFu71gmbwIj3oy7wu2P/gk7y44omrPCbRGc3Gjwa5QeAqP4+yMB1F0ycSZD6O1ExuD5yGIZBw= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R111e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037067113;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=16;SR=0;TI=SMTPD_---0W8G1Jqm_1718100677; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W8G1Jqm_1718100677) by smtp.aliyun-inc.com; Tue, 11 Jun 2024 18:11:17 +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 v5 2/6] mm: shmem: add THP validation for PMD-mapped THP related statistics Date: Tue, 11 Jun 2024 18:11:06 +0800 Message-Id: X-Mailer: git-send-email 2.39.3 In-Reply-To: References: MIME-Version: 1.0 X-Stat-Signature: fr6yybabeitr5mjx54xkgcrcxwdbhphz X-Rspamd-Queue-Id: 1F48D180006 X-Rspamd-Server: rspam04 X-Rspam-User: X-HE-Tag: 1718100681-890824 X-HE-Meta: U2FsdGVkX19YY9i9Z5DwRllRGYUTIE1TFe1yGudjwZmQp+FvNlNKzfQpvfzXkNJQBraPl9pRTETS3e/0rlJCwQKq5XlIb+XI+mS4shP8SEVxkc3spbvzKxbsZifvYK2jRZN+kI9tT4VuJaqDRsVe8YHiCM6dI2agjW8urB138ebUCu4DrBzc6qSoipkkiTnQWTPanrod5OkQnzXQspn9x0aBdWv8O+1kCy5v7gW6/ysATrbWnf8EZRMibqMgJBEC6IbpUI7a6NAx6RmqYAKXfmh/A1sQrZ9f+B7K5+gQnSIXNIaHUprE7ac8MJRr1yhAB49RRUwfZ48exA4zvAlTrh14199Ezr6sDS9IV1P0AznNTz4tQGQeiEAh/BX9QRDIDl2d1PXE2ehs8kl2mfONzCinnxZim/aLxYP5hPWnZ9xMFyO5yxXjDjtwa1aiLCLAHuoDW67iO0BkpsUSnXnzNBcXjYJ2rCX43bL6eF9bC1+VVjsLkNOrkUOLvhGUxujogaSsb0hsKmnR6J9z4oqPPELY3wOG14ry+/CRvx6jIu7JURJwlnQ1pqKVFiETfx8t6pkR0Whx4xHcR3GeP3ljMKPTkPMTz4GsjtdVS3vMvdA+LxOJmjsziTt4syAaJQw1kclgYjQRToDy52epPTA506WBo3zrub+GBYokHYB5HzSFbTnSGp0snwCPKkW+DqRKaU5RSKtZeD5nn6hYN5CueEwl9hK4JBggspQTb/tNWakM0c5z8ZJIUWCv7FiW3N5lAvtx1WMbMBrozZed+CTZ+tQHs+NExjQMaA9hwyHMDmIhkdEvo91WyOiB7L7UNDlj0kDsRn+iXhK6YY/Lhghy6HD0nHXQsS4sLeMK6/zWsKKBEncNbqOZshA4H14+5/MFfs/L0Z/myEoqocPqFcsMYE3IWaNpGKVuPT8WlBHBxsSVlOV1NqHLXyekZN/qUPPJN2H8akiLWE3e12uz/TD upuquYF1 JANtriOrZr03GlyI/MGJBfga5md2lVdeHWaZjhUdVDkoyNjXchhksCERW+jEPmen0xFuIc4xo+e2XkzgKYmWLU8nMjKsoSAsBqBeGq6GgXmpazTiHzm4omtvAqEgGsEDsro39Kc9uXS55OhdBhn4jhygbTX7GayHzsvR/hodYmR32svbAh/1sxEGRmwYIhTHaKLaR5btaLPys8jRc3bNneCIpBPlmaH6QhCurJ/zE57KGzLMcN706INLDW+tulL2OLkgs58nw+Mj1S3RSdvOAZ9ej+e4NUlYgEmydsD7VQcyXgGQ= 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: In order to extend support for mTHP, add THP validation for PMD-mapped THP related statistics to avoid statistical confusion. Signed-off-by: Baolin Wang Reviewed-by: Barry Song --- mm/shmem.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 6868c0af3a69..ae358efc397a 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1647,7 +1647,7 @@ static struct folio *shmem_alloc_and_add_folio(gfp_t gfp, return ERR_PTR(-E2BIG); folio = shmem_alloc_folio(gfp, HPAGE_PMD_ORDER, info, index); - if (!folio) + if (!folio && pages == HPAGE_PMD_NR) count_vm_event(THP_FILE_FALLBACK); } else { pages = 1; @@ -1665,7 +1665,7 @@ static struct folio *shmem_alloc_and_add_folio(gfp_t gfp, if (xa_find(&mapping->i_pages, &index, index + pages - 1, XA_PRESENT)) { error = -EEXIST; - } else if (huge) { + } else if (pages == HPAGE_PMD_NR) { count_vm_event(THP_FILE_FALLBACK); count_vm_event(THP_FILE_FALLBACK_CHARGE); } @@ -2031,7 +2031,8 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index, folio = shmem_alloc_and_add_folio(huge_gfp, inode, index, fault_mm, true); if (!IS_ERR(folio)) { - count_vm_event(THP_FILE_ALLOC); + if (folio_test_pmd_mappable(folio)) + count_vm_event(THP_FILE_ALLOC); goto alloced; } if (PTR_ERR(folio) == -EEXIST) From patchwork Tue Jun 11 10:11:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 13693374 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 012CEC25B76 for ; Tue, 11 Jun 2024 10:11:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5C8D66B00A4; Tue, 11 Jun 2024 06:11:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 551336B00A5; Tue, 11 Jun 2024 06:11:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 330E56B00A6; Tue, 11 Jun 2024 06:11:26 -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 041666B00A5 for ; Tue, 11 Jun 2024 06:11:25 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id A652F8128F for ; Tue, 11 Jun 2024 10:11:25 +0000 (UTC) X-FDA: 82218190530.13.D142ED8 Received: from out30-110.freemail.mail.aliyun.com (out30-110.freemail.mail.aliyun.com [115.124.30.110]) by imf06.hostedemail.com (Postfix) with ESMTP id 53A34180020 for ; Tue, 11 Jun 2024 10:11:22 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=kELhQeLy; spf=pass (imf06.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.110 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=1718100684; 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=6yjfO47qpLPy3B0qP6pgcF4Lahv9FnYDIgEE8QuEsvQ=; b=J4y1oQ3WOsbVg3Ek7oZKYXP8/ybCgXsMBCYQx+cC+Id67Qeda91/4RB1d2K1iyFsNsUtRb E3D8xZetNdt9tDJLXpr4dc5Fte2iBKq7pp95uwsPvS1SruIdYotsjj3p1DWkzdeWVDyY+o GeS2rWbW8nYzyr+hpSN+H+L6QY5Q0nA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718100684; a=rsa-sha256; cv=none; b=Mr7rVASaEsUYwD/HQLYHRycUALjeGNLdUXfAOU7U2muWwziCNvBgsKaNRbgXg0yR4LN9Vx 2uHJk0/g31WJTeMYWLJzJYsk54iq6yaF8Fh5J2VjWi0SKD0GwSsWEXwFMUQpXqVtQfQD0f N0BFy9+p3yOjVMMldnhvpighQ+HX23Y= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=kELhQeLy; spf=pass (imf06.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.110 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=1718100680; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=6yjfO47qpLPy3B0qP6pgcF4Lahv9FnYDIgEE8QuEsvQ=; b=kELhQeLyevC/o92f4vGX68LKn2+y3E7y8Eld+WmD5IB/xALZeG7ocm2f44taAs6pGKydCRByQBA4LqDhp060U18Bezk4yRz+xd+GpODUoCi2y/Xa5X0xhGtVezTkoMgoKClxHpPSJGjuKL1SWnFgSl5f2fLUhQWMabCh7wz9Qbg= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R211e4;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=16;SR=0;TI=SMTPD_---0W8G7EMK_1718100678; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W8G7EMK_1718100678) by smtp.aliyun-inc.com; Tue, 11 Jun 2024 18:11:18 +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 v5 3/6] mm: shmem: add multi-size THP sysfs interface for anonymous shmem Date: Tue, 11 Jun 2024 18:11:07 +0800 Message-Id: X-Mailer: git-send-email 2.39.3 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Stat-Signature: 44cw56didn7gcjzkzxg7yyq3wcjrdupy X-Rspamd-Queue-Id: 53A34180020 X-Rspam-User: X-HE-Tag: 1718100682-566770 X-HE-Meta: U2FsdGVkX1+6vcbiJGyMYCeMjFLIy1EenouzGqyU0PPIIMlHwg1yMBF/3mTA2FIQIqL7hDN+su8wMVp/X4nezxjLJkL4sNMNM6msqrrcdytij9L0OBW26fzzz/iNqMQpCsgcOrRNFv3dj/JjdgZCLeyZ6l3EzHRBVk0Rla1cuHV8QwLW7/3aZ6xUmir0F3WKC+WknCXb1ND3J8vvq9fz8rmm+Rx5Ji9JzNtdCcs0ZrDvao0v2HPImyql7Vgdxw2c6DoDQZyiYrWs4odHuPda0aoq645/fR1TaywZBGwOKXYC3woCw/Z5jOlc1BQ8r0M3NMi9UlTh9li6hGe0G8yWe6gePGVccZ9R62eaHqSOrGCMd2+7EHMVVkr36STMdKy9Wyic6c0KFwRAJQKjGwikD0UqzNWEI+LtZfn1q+70+yNb5EVMrN68I9UsrCCT92a0xjltK8bK2vWRXh6ML5MmQAHwgTKTnsysI2qbPdWhhraWDYUkc6kpIuBsGyjNJggaHXwetNpLHfgp/566/JDWAJuErNZG9nAjRodZXk0jhAFpXyAgRN7pxOVGw5LDkSgY8tsXiC9Gi7RXuGHeYiA3kJcn7yhrHR7GW17188Ny9jvgaYT8kzapO300o5OYTfjsoLjymDV+EmZaj5OeTBQQtmh1FIiuzQwdnRnATI23MgMCAh+LLuyWbTrivPMVxJQISS539WlPgxgmvbrIWROHBi7T9cYLaxclkfRZGipeI3r2wdc9kWHv+p1Ao2ICwx2StNi17xxAByO3nvI3LFxXQA620h7zFe+hyddfmCnF2Ch5blktLV+M8+TcZcepZqujjB/yAlE0YsObBlO7a0oB+2Q1B/HkRi9yid4WXr7ghyzTEPb78GdEpYrXkMR0jBq0pM0hVX6ZPZhRTaDpor6Bs0WMvPgRnx3i2rMvLjjK89/5bb6436oeJ6J834bDoq0+q05pfCgiVDmlJ1RbsU6 nGKNitAe 9HTU1R+apakJbKIgisOSPFNSjCmAbuHCFDjV7w47WMIAiceYI9rLI80ouRzxzaFqCP5AFIxsiZKyTH81FHw9e/boZj9HP9gvd75jiNdpWvJV3SS8gNHrhQLau/7HkFOmm0s0MagwwPor/gUye/vzwg1jRoAIirVlXAvvGyM60XjEcHMUJC9ah++06aG0Agt8m6QN8qRZSUCzHRk6FqNlQ/BePgnV1Ht4j5DHdFYJpnxkjzbs= 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: To support the use of mTHP with anonymous shmem, add a new sysfs interface 'shmem_enabled' in the '/sys/kernel/mm/transparent_hugepage/hugepages-kB/' directory for each mTHP to control whether shmem is enabled for that mTHP, with a value similar to the top level 'shmem_enabled', which can be set to: "always", "inherit (to inherit the top level setting)", "within_size", "advise", "never". An 'inherit' option is added to ensure compatibility with these global settings, and the options 'force' and 'deny' are dropped, which are rather testing artifacts from the old ages. By default, PMD-sized hugepages have enabled="inherit" and all other hugepage sizes have enabled="never" for '/sys/kernel/mm/transparent_hugepage/hugepages-xxkB/shmem_enabled'. In addition, if top level value is 'force', then only PMD-sized hugepages have enabled="inherit", otherwise configuration will be failed and vice versa. That means now we will avoid using non-PMD sized THP to override the global huge allocation. Signed-off-by: Baolin Wang --- Documentation/admin-guide/mm/transhuge.rst | 23 ++++++ include/linux/huge_mm.h | 10 +++ mm/huge_memory.c | 11 +-- mm/shmem.c | 96 ++++++++++++++++++++++ 4 files changed, 132 insertions(+), 8 deletions(-) diff --git a/Documentation/admin-guide/mm/transhuge.rst b/Documentation/admin-guide/mm/transhuge.rst index d414d3f5592a..b76d15e408b3 100644 --- a/Documentation/admin-guide/mm/transhuge.rst +++ b/Documentation/admin-guide/mm/transhuge.rst @@ -332,6 +332,29 @@ deny force Force the huge option on for all - very useful for testing; +Shmem can also use "multi-size THP" (mTHP) by adding a new sysfs knob to control +mTHP allocation: '/sys/kernel/mm/transparent_hugepage/hugepages-kB/shmem_enabled', +and its value for each mTHP is essentially consistent with the global setting. +An 'inherit' option is added to ensure compatibility with these global settings. +Conversely, the options 'force' and 'deny' are dropped, which are rather testing +artifacts from the old ages. +always + Attempt to allocate huge pages every time we need a new page; + +inherit + Inherit the top-level "shmem_enabled" value. By default, PMD-sized hugepages + have enabled="inherit" and all other hugepage sizes have enabled="never"; + +never + Do not allocate huge pages; + +within_size + Only allocate huge page if it will be fully within i_size. + Also respect fadvise()/madvise() hints; + +advise + Only allocate huge pages if requested with fadvise()/madvise(); + Need of application restart =========================== diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index 020e2344eb86..fac21548c5de 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -6,6 +6,7 @@ #include #include /* only for vma_is_dax() */ +#include vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf); int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm, @@ -63,6 +64,7 @@ ssize_t single_hugepage_flag_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf, enum transparent_hugepage_flag flag); extern struct kobj_attribute shmem_enabled_attr; +extern struct kobj_attribute thpsize_shmem_enabled_attr; /* * Mask of all large folio orders supported for anonymous THP; all orders up to @@ -265,6 +267,14 @@ unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma, return __thp_vma_allowable_orders(vma, vm_flags, tva_flags, orders); } +struct thpsize { + struct kobject kobj; + struct list_head node; + int order; +}; + +#define to_thpsize(kobj) container_of(kobj, struct thpsize, kobj) + enum mthp_stat_item { MTHP_STAT_ANON_FAULT_ALLOC, MTHP_STAT_ANON_FAULT_FALLBACK, diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 8e49f402d7c7..1360a1903b66 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -449,14 +449,6 @@ static void thpsize_release(struct kobject *kobj); static DEFINE_SPINLOCK(huge_anon_orders_lock); static LIST_HEAD(thpsize_list); -struct thpsize { - struct kobject kobj; - struct list_head node; - int order; -}; - -#define to_thpsize(kobj) container_of(kobj, struct thpsize, kobj) - static ssize_t thpsize_enabled_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { @@ -517,6 +509,9 @@ static struct kobj_attribute thpsize_enabled_attr = static struct attribute *thpsize_attrs[] = { &thpsize_enabled_attr.attr, +#ifdef CONFIG_SHMEM + &thpsize_shmem_enabled_attr.attr, +#endif NULL, }; diff --git a/mm/shmem.c b/mm/shmem.c index ae358efc397a..bb19ea2770e3 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -131,6 +131,14 @@ struct shmem_options { #define SHMEM_SEEN_QUOTA 32 }; +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +static unsigned long huge_shmem_orders_always __read_mostly; +static unsigned long huge_shmem_orders_madvise __read_mostly; +static unsigned long huge_shmem_orders_inherit __read_mostly; +static unsigned long huge_shmem_orders_within_size __read_mostly; +static DEFINE_SPINLOCK(huge_shmem_orders_lock); +#endif + #ifdef CONFIG_TMPFS static unsigned long shmem_default_max_blocks(void) { @@ -4672,6 +4680,12 @@ void __init shmem_init(void) SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; else shmem_huge = SHMEM_HUGE_NEVER; /* just in case it was patched */ + + /* + * Default to setting PMD-sized THP to inherit the global setting and + * disable all other multi-size THPs. + */ + huge_shmem_orders_inherit = BIT(HPAGE_PMD_ORDER); #endif return; @@ -4731,6 +4745,11 @@ static ssize_t shmem_enabled_store(struct kobject *kobj, huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY) return -EINVAL; + /* Do not override huge allocation policy with non-PMD sized mTHP */ + if (huge == SHMEM_HUGE_FORCE && + huge_shmem_orders_inherit != BIT(HPAGE_PMD_ORDER)) + return -EINVAL; + shmem_huge = huge; if (shmem_huge > SHMEM_HUGE_DENY) SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; @@ -4738,6 +4757,83 @@ static ssize_t shmem_enabled_store(struct kobject *kobj, } struct kobj_attribute shmem_enabled_attr = __ATTR_RW(shmem_enabled); + +static ssize_t thpsize_shmem_enabled_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + int order = to_thpsize(kobj)->order; + const char *output; + + if (test_bit(order, &huge_shmem_orders_always)) + output = "[always] inherit within_size advise never"; + else if (test_bit(order, &huge_shmem_orders_inherit)) + output = "always [inherit] within_size advise never"; + else if (test_bit(order, &huge_shmem_orders_within_size)) + output = "always inherit [within_size] advise never"; + else if (test_bit(order, &huge_shmem_orders_madvise)) + output = "always inherit within_size [advise] never"; + else + output = "always inherit within_size advise [never]"; + + return sysfs_emit(buf, "%s\n", output); +} + +static ssize_t thpsize_shmem_enabled_store(struct kobject *kobj, + struct kobj_attribute *attr, + const char *buf, size_t count) +{ + int order = to_thpsize(kobj)->order; + ssize_t ret = count; + + if (sysfs_streq(buf, "always")) { + spin_lock(&huge_shmem_orders_lock); + clear_bit(order, &huge_shmem_orders_inherit); + clear_bit(order, &huge_shmem_orders_madvise); + clear_bit(order, &huge_shmem_orders_within_size); + set_bit(order, &huge_shmem_orders_always); + spin_unlock(&huge_shmem_orders_lock); + } else if (sysfs_streq(buf, "inherit")) { + /* Do not override huge allocation policy with non-PMD sized mTHP */ + if (shmem_huge == SHMEM_HUGE_FORCE && + order != HPAGE_PMD_ORDER) + return -EINVAL; + + spin_lock(&huge_shmem_orders_lock); + clear_bit(order, &huge_shmem_orders_always); + clear_bit(order, &huge_shmem_orders_madvise); + clear_bit(order, &huge_shmem_orders_within_size); + set_bit(order, &huge_shmem_orders_inherit); + spin_unlock(&huge_shmem_orders_lock); + } else if (sysfs_streq(buf, "within_size")) { + spin_lock(&huge_shmem_orders_lock); + clear_bit(order, &huge_shmem_orders_always); + clear_bit(order, &huge_shmem_orders_inherit); + clear_bit(order, &huge_shmem_orders_madvise); + set_bit(order, &huge_shmem_orders_within_size); + spin_unlock(&huge_shmem_orders_lock); + } else if (sysfs_streq(buf, "madvise")) { + spin_lock(&huge_shmem_orders_lock); + clear_bit(order, &huge_shmem_orders_always); + clear_bit(order, &huge_shmem_orders_inherit); + clear_bit(order, &huge_shmem_orders_within_size); + set_bit(order, &huge_shmem_orders_madvise); + spin_unlock(&huge_shmem_orders_lock); + } else if (sysfs_streq(buf, "never")) { + spin_lock(&huge_shmem_orders_lock); + clear_bit(order, &huge_shmem_orders_always); + clear_bit(order, &huge_shmem_orders_inherit); + clear_bit(order, &huge_shmem_orders_within_size); + clear_bit(order, &huge_shmem_orders_madvise); + spin_unlock(&huge_shmem_orders_lock); + } else { + ret = -EINVAL; + } + + return ret; +} + +struct kobj_attribute thpsize_shmem_enabled_attr = + __ATTR(shmem_enabled, 0644, thpsize_shmem_enabled_show, thpsize_shmem_enabled_store); #endif /* CONFIG_TRANSPARENT_HUGEPAGE && CONFIG_SYSFS */ #else /* !CONFIG_SHMEM */ From patchwork Tue Jun 11 10:11:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 13693373 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 16B1DC25B76 for ; Tue, 11 Jun 2024 10:11:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EA0C36B00A3; Tue, 11 Jun 2024 06:11:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E504B6B00A4; Tue, 11 Jun 2024 06:11:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CA2DF6B00A5; Tue, 11 Jun 2024 06:11:25 -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 A77E26B00A3 for ; Tue, 11 Jun 2024 06:11:25 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 5DCD58129C for ; Tue, 11 Jun 2024 10:11:25 +0000 (UTC) X-FDA: 82218190530.27.68759D9 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 50F6FA0013 for ; Tue, 11 Jun 2024 10:11:22 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=NdtoTnyF; 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=1718100683; 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=d0UcBrIAWPKoJXUZ8qfKGS/skuBxjMLJJmhrq6myW0M=; b=dnu8efPuuMIbSGoaOVmFAeIwodr0ci/kZi5ZPRXyTXBeEVc6V2/Zq7AeEY+GGB4Go6GU9e ecgh95ZYErYDTbd4QuWs0ar2LP0whlZtB2IP2L6eed7wRiQjTXr3+MfpmOrWOCKqxwtj9d STBK/VHefhZcWViFZ3VRBuEKIlUdhKQ= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=NdtoTnyF; 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=1718100683; a=rsa-sha256; cv=none; b=xw2SSZalTgLaF16IKBzEcqumHcAuQHE8UbpdhEDdgVDpdn5VFdifMMHhh6+GlOW0Iy5bv3 P9l56mTwA+8MaQczpKNgqgwpK0DK0deOiz6XEy2WjvXdOgtlPOnzLtGcYD/BRSVQa4OcGm YmLGP4Lmiosp2HzC1xMeWh0XuymLpvQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1718100680; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=d0UcBrIAWPKoJXUZ8qfKGS/skuBxjMLJJmhrq6myW0M=; b=NdtoTnyF5a5EaOLi04Ey2+y6PRbCgFFhmy7tuwR/MxHOeu0LOfrPejZuTPzSfRl29j3e6aQ+I4hXYSyO7DgOFVQz408mGL6S0PvGfKbOkCQhu8uanuLUvjAVLJtgJVhkPq1XHQriIf4SxG81QeH5Gvq5SxyAFc8taQsMYQ2k0Ao= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R151e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037067110;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=16;SR=0;TI=SMTPD_---0W8G5NWO_1718100679; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W8G5NWO_1718100679) by smtp.aliyun-inc.com; Tue, 11 Jun 2024 18:11:19 +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 v5 4/6] mm: shmem: add mTHP support for anonymous shmem Date: Tue, 11 Jun 2024 18:11:08 +0800 Message-Id: <65796c1e72e51e15f3410195b5c2d5b6c160d411.1718090413.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: References: MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 50F6FA0013 X-Stat-Signature: o7qm5mpibmi6rf1sfx1dpox1h5jqzkdf X-HE-Tag: 1718100682-706930 X-HE-Meta: U2FsdGVkX18kwMUxGk93n4EKbB7h91ExS3pA8ivzs/B6f2dn70tqxDmBdYdRGliEaOXrkcqRk+W6cdx/BJ5hF+0unNhXw/9Up8Xq2C4Rhm9o5gKgW7ren0CMs8XLjSbgTi4FHAeXwjQVw12a4YVE4FkxFDgI9L7zYYqcqSLkX0Ue53r9w9Ocm+nxvUmvAxqhpcb9mHiSuUN/KSuNr/q2FgeliHZ0GGYLKpmsaEF6NVVcybN5UypQg6pmc3jQmgeZaJUQ0+Qqh1mSWJ37MrU9AfWcb9MprvL9skkmXbz8F6pE9jlgQ3ufWEoTIGfm7XcMdZdscrrecls5xWs24iYw3pNATATiScf8+kudofOPYa3YqertSLqZlZMEvSnzZlrdkVvFv2HomZwR34YxagI2bUiR26N32XA/i359CebCip8gDihgMcOoLYnANk2F8vU9lBIDk/6UzlrPmfab3rVGkYXx/o07MnooHc+sbieei2t+S04/FpML/Y2liiOEzWUNv7kzWBbLQxo0GmTWBGkMuZliY13JvOgSBsSS3EBuedAx2PYfzcNMw6OB4izryoY6h8tYTBj2reGcZxwgTLd0OrFxPdMz443sxmLm/LGy6jWtZOyXIzxpXT3+A25l4PyEsRNhnB8rikfKrgIBF1c88ZTL4/KVpzg+oDoqTelLMnvRLHesV0njoR3DJdd58ESlOGkP1+uIkBXl5fmCUENsHcGotuXLn1HdGGIbb6mU6lGZ0UOzvkMXmO4u4CMpeibv2adZn20b3d81G4CMOXQs1X/NG74E+dh2U5IQIvRgKilujoX+RbJr85kcEzJLS32FPKu75LGPLIX9AiiSeAnvWqYSkVHYERVERePqlwQ3G5+GrH00cH4C/bcK75qQ7QpuQYAkOZGJflRKCVi4QmPyq3Y8NyROJCEpwOsc6g5TNVEFMuFNfnSHvjXN6PnpGWa+eCXOv79+/DUku/p1rwP zKwDL6GL QDxdISjt8o5ZjBWF7VmGRmKALb/7whdHRCwpa7kOQqbhrH8yMFb3+aOAfOBVgyZWIveivvlcuEWVihSRyr4NgRNEb0eYnSjbaTNtGdUp9a9AbvN7ud7uWRLX2yrcY2MHpluJa4qkZIdEutBC/3L+AqUWLDXzeC7xjvR9WRQevEm3rzCGJespBw5WfyfyjW4YpPjXFF2VtldrD0JjvJHon+XsRnzVe8XJ/La+UY8WdSxcQhUs= 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: Commit 19eaf44954df adds multi-size THP (mTHP) for anonymous pages, that can allow THP to be configured through the sysfs interface located at '/sys/kernel/mm/transparent_hugepage/hugepage-XXkb/enabled'. However, the anonymous shmem will ignore the anonymous mTHP rule configured through the sysfs interface, and can only use the PMD-mapped THP, that is not reasonable. Users expect to apply the mTHP rule for all anonymous pages, including the anonymous shmem, in order to enjoy the benefits of mTHP. For example, lower latency than PMD-mapped THP, smaller memory bloat than PMD-mapped THP, contiguous PTEs on ARM architecture to reduce TLB miss etc. In addition, the mTHP interfaces can be extended to support all shmem/tmpfs scenarios in the future, especially for the shmem mmap() case. The primary strategy is similar to supporting anonymous mTHP. Introduce a new interface '/mm/transparent_hugepage/hugepage-XXkb/shmem_enabled', which can have almost the same values as the top-level '/sys/kernel/mm/transparent_hugepage/shmem_enabled', with adding a new additional "inherit" option and dropping the testing options 'force' and 'deny'. By default all sizes will be set to "never" except PMD size, which is set to "inherit". This ensures backward compatibility with the anonymous shmem enabled of the top level, meanwhile also allows independent control of anonymous shmem enabled for each mTHP. Signed-off-by: Baolin Wang --- include/linux/huge_mm.h | 10 +++ mm/shmem.c | 187 +++++++++++++++++++++++++++++++++------- 2 files changed, 167 insertions(+), 30 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index fac21548c5de..909cfc67521d 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -575,6 +575,16 @@ static inline bool thp_migration_supported(void) { return false; } + +static inline int highest_order(unsigned long orders) +{ + return 0; +} + +static inline int next_order(unsigned long *orders, int prev) +{ + return 0; +} #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ static inline int split_folio_to_list_to_order(struct folio *folio, diff --git a/mm/shmem.c b/mm/shmem.c index bb19ea2770e3..e849c88452b2 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1611,6 +1611,107 @@ static gfp_t limit_gfp_mask(gfp_t huge_gfp, gfp_t limit_gfp) return result; } +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +static unsigned long shmem_allowable_huge_orders(struct inode *inode, + struct vm_area_struct *vma, pgoff_t index, + bool global_huge) +{ + unsigned long mask = READ_ONCE(huge_shmem_orders_always); + unsigned long within_size_orders = READ_ONCE(huge_shmem_orders_within_size); + unsigned long vm_flags = vma->vm_flags; + /* + * Check all the (large) orders below HPAGE_PMD_ORDER + 1 that + * are enabled for this vma. + */ + unsigned long orders = BIT(PMD_ORDER + 1) - 1; + loff_t i_size; + int order; + + if ((vm_flags & VM_NOHUGEPAGE) || + test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags)) + return 0; + + /* If the hardware/firmware marked hugepage support disabled. */ + if (transparent_hugepage_flags & (1 << TRANSPARENT_HUGEPAGE_UNSUPPORTED)) + return 0; + + /* + * Following the 'deny' semantics of the top level, force the huge + * option off from all mounts. + */ + if (shmem_huge == SHMEM_HUGE_DENY) + return 0; + + /* + * Only allow inherit orders if the top-level value is 'force', which + * means non-PMD sized THP can not override 'huge' mount option now. + */ + if (shmem_huge == SHMEM_HUGE_FORCE) + return READ_ONCE(huge_shmem_orders_inherit); + + /* Allow mTHP that will be fully within i_size. */ + order = highest_order(within_size_orders); + while (within_size_orders) { + index = round_up(index + 1, order); + i_size = round_up(i_size_read(inode), PAGE_SIZE); + if (i_size >> PAGE_SHIFT >= index) { + mask |= within_size_orders; + break; + } + + order = next_order(&within_size_orders, order); + } + + if (vm_flags & VM_HUGEPAGE) + mask |= READ_ONCE(huge_shmem_orders_madvise); + + if (global_huge) + mask |= READ_ONCE(huge_shmem_orders_inherit); + + return orders & mask; +} + +static unsigned long shmem_suitable_orders(struct inode *inode, struct vm_fault *vmf, + struct address_space *mapping, pgoff_t index, + unsigned long orders) +{ + struct vm_area_struct *vma = vmf->vma; + unsigned long pages; + int order; + + 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); + if (!xa_find(&mapping->i_pages, &index, + index + pages - 1, XA_PRESENT)) + break; + order = next_order(&orders, order); + } + + return orders; +} +#else +static unsigned long shmem_allowable_huge_orders(struct inode *inode, + struct vm_area_struct *vma, pgoff_t index, + bool global_huge) +{ + return 0; +} + +static unsigned long shmem_suitable_orders(struct inode *inode, struct vm_fault *vmf, + struct address_space *mapping, pgoff_t index, + unsigned long orders) +{ + return 0; +} +#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ + static struct folio *shmem_alloc_folio(gfp_t gfp, int order, struct shmem_inode_info *info, pgoff_t index) { @@ -1625,38 +1726,55 @@ static struct folio *shmem_alloc_folio(gfp_t gfp, int order, return folio; } -static struct folio *shmem_alloc_and_add_folio(gfp_t gfp, - struct inode *inode, pgoff_t index, - struct mm_struct *fault_mm, bool huge) +static struct folio *shmem_alloc_and_add_folio(struct vm_fault *vmf, + gfp_t gfp, struct inode *inode, pgoff_t index, + struct mm_struct *fault_mm, unsigned long orders) { struct address_space *mapping = inode->i_mapping; struct shmem_inode_info *info = SHMEM_I(inode); - struct folio *folio; + struct vm_area_struct *vma = vmf ? vmf->vma : NULL; + unsigned long suitable_orders = 0; + struct folio *folio = NULL; long pages; - int error; + int error, order; if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) - huge = false; + orders = 0; - if (huge) { - pages = HPAGE_PMD_NR; - index = round_down(index, HPAGE_PMD_NR); + if (orders > 0) { + if (vma && vma_is_anon_shmem(vma)) { + 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); + /* + * 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); + } - folio = shmem_alloc_folio(gfp, HPAGE_PMD_ORDER, info, index); - if (!folio && pages == HPAGE_PMD_NR) - count_vm_event(THP_FILE_FALLBACK); + order = highest_order(suitable_orders); + while (suitable_orders) { + pages = 1UL << order; + index = round_down(index, pages); + folio = shmem_alloc_folio(gfp, order, info, index); + if (folio) + goto allocated; + + if (pages == HPAGE_PMD_NR) + count_vm_event(THP_FILE_FALLBACK); + order = next_order(&suitable_orders, order); + } } else { pages = 1; folio = shmem_alloc_folio(gfp, 0, info, index); @@ -1664,6 +1782,7 @@ static struct folio *shmem_alloc_and_add_folio(gfp_t gfp, if (!folio) return ERR_PTR(-ENOMEM); +allocated: __folio_set_locked(folio); __folio_set_swapbacked(folio); @@ -1958,7 +2077,8 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index, struct mm_struct *fault_mm; struct folio *folio; int error; - bool alloced; + bool alloced, huge; + unsigned long orders = 0; if (WARN_ON_ONCE(!shmem_mapping(inode->i_mapping))) return -EINVAL; @@ -2030,14 +2150,21 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index, return 0; } - if (shmem_is_huge(inode, index, false, fault_mm, - vma ? vma->vm_flags : 0)) { + huge = shmem_is_huge(inode, index, false, fault_mm, + vma ? vma->vm_flags : 0); + /* Find hugepage orders that are allowed for anonymous shmem. */ + if (vma && vma_is_anon_shmem(vma)) + orders = shmem_allowable_huge_orders(inode, vma, index, huge); + else if (huge) + orders = BIT(HPAGE_PMD_ORDER); + + if (orders > 0) { gfp_t huge_gfp; huge_gfp = vma_thp_gfp_mask(vma); huge_gfp = limit_gfp_mask(huge_gfp, gfp); - folio = shmem_alloc_and_add_folio(huge_gfp, - inode, index, fault_mm, true); + folio = shmem_alloc_and_add_folio(vmf, huge_gfp, + inode, index, fault_mm, orders); if (!IS_ERR(folio)) { if (folio_test_pmd_mappable(folio)) count_vm_event(THP_FILE_ALLOC); @@ -2047,7 +2174,7 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index, goto repeat; } - folio = shmem_alloc_and_add_folio(gfp, inode, index, fault_mm, false); + folio = shmem_alloc_and_add_folio(vmf, gfp, inode, index, fault_mm, 0); if (IS_ERR(folio)) { error = PTR_ERR(folio); if (error == -EEXIST) @@ -2058,7 +2185,7 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index, alloced: alloced = true; - if (folio_test_pmd_mappable(folio) && + if (folio_test_large(folio) && DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) < folio_next_index(folio) - 1) { struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); From patchwork Tue Jun 11 10:11:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 13693378 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 B5C46C25B76 for ; Tue, 11 Jun 2024 10:11:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 776C16B00A8; Tue, 11 Jun 2024 06:11:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 701806B00A9; Tue, 11 Jun 2024 06:11:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 506316B00AA; Tue, 11 Jun 2024 06:11:29 -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 286D76B00A8 for ; Tue, 11 Jun 2024 06:11:29 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id D76EB12130C for ; Tue, 11 Jun 2024 10:11:28 +0000 (UTC) X-FDA: 82218190656.03.7F4A81F Received: from out30-99.freemail.mail.aliyun.com (out30-99.freemail.mail.aliyun.com [115.124.30.99]) by imf27.hostedemail.com (Postfix) with ESMTP id 849B440029 for ; Tue, 11 Jun 2024 10:11:26 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=Jptw3rzn; spf=pass (imf27.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.99 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=1718100687; 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=ihHol0Lo4aQL92vXhUE0q0HG3Xsyoz8HlIbQSl3GlRA=; b=ZbMOdVhKglfelgyHmVDv5/yr3xhmLwUc/vIcoqBB6ZMyaR+CwuWpM5O/epWAAOHYsebBcK IqxonnVFIdNXqOrozIbLz5NC09X7tKZKMS2VIKUWzcv8sG4gEvbELEehCX/qfD2XMQRI7T 77l19mpMUOQUjbCSJBkevs8qW8KWcW0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718100687; a=rsa-sha256; cv=none; b=3/HtKyOBbluGFWwN/5IvU+S7m8OvwUPUG+mLC9sTnybZ1EpObYD4bzrM0Wt7c1mfU+GYmn 4Be90xuMWx6XfXFDw3J8NPqdAA1kbQs7j2JRWzZwYgJi2jxZKKfxWbKzaOEDHN4mIyicT+ tsJYdfrCh34xu0eh8NUUV3DhMruVD4o= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=Jptw3rzn; spf=pass (imf27.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.99 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=1718100682; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=ihHol0Lo4aQL92vXhUE0q0HG3Xsyoz8HlIbQSl3GlRA=; b=Jptw3rznsqoHQxQkCYMKVYim7AsECX9znu/4R94NJp8vLd4WcIzqq+a0yaHYvsN5Bwt54eTffUbUeq1WyXmROKAbiVoPr2290bOnpEePq7Cx23kngnfjzAdFNZycFGJFwpb2enBjzGuxIGdknmyd9dla+hIkBGFUeBLOFw+eu1I= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R161e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033045075189;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=16;SR=0;TI=SMTPD_---0W8G1Jrq_1718100680; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W8G1Jrq_1718100680) by smtp.aliyun-inc.com; Tue, 11 Jun 2024 18:11:21 +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 v5 5/6] mm: shmem: add mTHP size alignment in shmem_get_unmapped_area Date: Tue, 11 Jun 2024 18:11:09 +0800 Message-Id: <0c549b57cf7db07503af692d8546ecfad0fcce52.1718090413.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: ot71ssk4nursrbgx679177se4gtxx37t X-Rspamd-Queue-Id: 849B440029 X-Rspam-User: X-Rspamd-Server: rspam05 X-HE-Tag: 1718100686-848679 X-HE-Meta: U2FsdGVkX186+qZ+MVDNc/GVXRV9ybdFoyJLrfCg5LnvJnJOL3P2V+q+bcUC9zNAZy8vpAi+w7z9WCh1kZM64yrQmCk9SHqDXWOy4/qnUS6DMO4+8ummOrc5GaD2LGUI/dlH1G1Pl8NWOrsHvnMtBKtNzeRzcFhYSGpFPzAi9ruHIUV2g3UDXrBddSDyQC+8wBA/GVJlNhM15Fls/vnodtNKm6/pEge85F9DPOUy3RUH3vV9MIXpcFjbuQN8SRT1LWUECoJ1AwvZccmWMtEwvMe79P8OAJVqTnRPbX2h48pNB/VPZ7r2FiSehMESAoGTGNBV3zBue9iNjoMpVV12bjz5ZaV/6qc92TnuLnoAadp4vRlyf5z+2jr72QzuQ1/2kx72n1oVCgTSUDQS0KjdQV4KzmH4f5oREtLXli7190nC9j5lGnX2mS34K6pqN6ZTF7nv4GxoHh/hO4tI2ElJwLMWt36B5bXe1klKvSBsW6W1lF5mG53IHkp5NT3zFVc1cNGk77bqE3vKGMK5pyd7DsresrVxnmJx8q6xXRInr6Gux4oN20Nkwiy9vDFDTDN5/P+JRE1I2a5+jqHXdfFuDCapq7vTKQhk3XDC9HsB3OvXbvg1ENPnWUdgPsJpGhG8rVLpOT2l+6XkVJWLtfvh5+Wre3bq92n/Bst67YlxYw9rIGDUBd83yGk72z5W1f4nl6AW8YCuVV6cZXozjpg20cBD3bfWWUY27zl5pvB8Gpnzpl0R4zH2Y0APEJ6loS5htP5pawEtmcqR0fg3Vd+BxIe6vlU1B6I2fj2389oRwSi5/59KpWttYVCtHIK5MguOX3FD4Ke5/AMDNPWV5FibaOB7/CxZl2Qs7vbImMnRw5Kv/Bd1L3KJfprIoUlG4byIuPKWfHIWt8T0ImyyweiuIKA/CICcGFys7KlAjm0xyZZbF+z2w4zqse1sl2NlGFpwdjasm1m0OU3LzeMWQJb 0eWUwBt/ Z/uFI36sNVWV7qZGLl6rJAeTY2eMBNSfiWAAj5EcbjWEq30RS6t9RfGxP2iGb8ZBK0JQek0P0dY7gyucuyWayqFacpIyZFMS1oqEUbXRZqZrT2fhI1+IJvqEzv4cNbPs+ys8Mcr1urOMh4d0RslIAdTl6Pha5/4EMSWFUnwA82OrAgLXk5uaKQKbAxQT6cuFsEE8E7d1ZUY+IndY+F2UHHtNNMan+azXkyBeitucKZBQOikK6ryD7QarzXfhizC9wAX7stsSdZFkjKiOrc9qBqr6q+dKIKNrbzcguzRiP6XdFd9gZ8nQ55IkkGg== 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: Although the top-level hugepage allocation can be turned off, anonymous shmem can still use mTHP by configuring the sysfs interface located at '/sys/kernel/mm/transparent_hugepage/hugepage-XXkb/shmem_enabled'. Therefore, add alignment for mTHP size to provide a suitable alignment address in shmem_get_unmapped_area(). Signed-off-by: Baolin Wang Tested-by: Lance Yang --- mm/shmem.c | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index e849c88452b2..f5469c357be6 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2394,6 +2394,7 @@ unsigned long shmem_get_unmapped_area(struct file *file, unsigned long inflated_len; unsigned long inflated_addr; unsigned long inflated_offset; + unsigned long hpage_size; if (len > TASK_SIZE) return -ENOMEM; @@ -2412,8 +2413,6 @@ unsigned long shmem_get_unmapped_area(struct file *file, if (shmem_huge == SHMEM_HUGE_DENY) return addr; - if (len < HPAGE_PMD_SIZE) - return addr; if (flags & MAP_FIXED) return addr; /* @@ -2425,8 +2424,11 @@ unsigned long shmem_get_unmapped_area(struct file *file, if (uaddr == addr) return addr; + hpage_size = HPAGE_PMD_SIZE; if (shmem_huge != SHMEM_HUGE_FORCE) { struct super_block *sb; + unsigned long __maybe_unused hpage_orders; + int order = 0; if (file) { VM_BUG_ON(file->f_op != &shmem_file_operations); @@ -2439,18 +2441,38 @@ unsigned long shmem_get_unmapped_area(struct file *file, if (IS_ERR(shm_mnt)) return addr; sb = shm_mnt->mnt_sb; + + /* + * Find the highest mTHP order used for anonymous shmem to + * provide a suitable alignment address. + */ +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + hpage_orders = READ_ONCE(huge_shmem_orders_always); + hpage_orders |= READ_ONCE(huge_shmem_orders_within_size); + hpage_orders |= READ_ONCE(huge_shmem_orders_madvise); + if (SHMEM_SB(sb)->huge != SHMEM_HUGE_NEVER) + hpage_orders |= READ_ONCE(huge_shmem_orders_inherit); + + if (hpage_orders > 0) { + order = highest_order(hpage_orders); + hpage_size = PAGE_SIZE << order; + } +#endif } - if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER) + if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER && !order) return addr; } - offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1); - if (offset && offset + len < 2 * HPAGE_PMD_SIZE) + if (len < hpage_size) + return addr; + + offset = (pgoff << PAGE_SHIFT) & (hpage_size - 1); + if (offset && offset + len < 2 * hpage_size) return addr; - if ((addr & (HPAGE_PMD_SIZE-1)) == offset) + if ((addr & (hpage_size - 1)) == offset) return addr; - inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE; + inflated_len = len + hpage_size - PAGE_SIZE; if (inflated_len > TASK_SIZE) return addr; if (inflated_len < len) @@ -2463,10 +2485,10 @@ unsigned long shmem_get_unmapped_area(struct file *file, if (inflated_addr & ~PAGE_MASK) return addr; - inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1); + inflated_offset = inflated_addr & (hpage_size - 1); inflated_addr += offset - inflated_offset; if (inflated_offset > offset) - inflated_addr += HPAGE_PMD_SIZE; + inflated_addr += hpage_size; if (inflated_addr > TASK_SIZE - len) return addr; From patchwork Tue Jun 11 10:11:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 13693377 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 E60EAC27C5E for ; Tue, 11 Jun 2024 10:11:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4DD826B00A5; Tue, 11 Jun 2024 06:11:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 466AC6B00A7; Tue, 11 Jun 2024 06:11:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2DF2F6B00A8; Tue, 11 Jun 2024 06:11:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 0ACFB6B00A5 for ; Tue, 11 Jun 2024 06:11:28 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id C396A1C1E48 for ; Tue, 11 Jun 2024 10:11:27 +0000 (UTC) X-FDA: 82218190614.09.BDDBC62 Received: from out30-112.freemail.mail.aliyun.com (out30-112.freemail.mail.aliyun.com [115.124.30.112]) by imf08.hostedemail.com (Postfix) with ESMTP id B778F160016 for ; Tue, 11 Jun 2024 10:11:25 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=jI6eHc3s; spf=pass (imf08.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.112 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=1718100686; 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=nLj8yUsK2lei34HSlxIopppsBC+abcyB+V/ZUwcOo4A=; b=qi97HcDGsFiFMdsyWoBKtEaW2CNzAEvTa4BlJb6qEF41k9++VSswkH2Ov4EckbfUoparRE 771RYFDF84cL3mg1X/FmDyD+/Sa6HiE7mvbEhuq51Dq4rWqgm04Im0AYbMnk/WdNGQob/P N/gUKlWBoTwj48GjCnqFhQykiQTkGlY= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=jI6eHc3s; spf=pass (imf08.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.112 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=1718100686; a=rsa-sha256; cv=none; b=UY6njvFHZ4T/N0CCnb9vtCJyRiEKwYTzy4vIBT6ACmjIPDbXB7ceQ8VXU7cDxPB9/P9AE8 sir+meA2HtRCQWV/tgErmqitaTKRm4iqpjYBwDFsP2VMnBYFsgvvNiYNNZb/y0E7xbyyWi 3upydUJkm0ptF/aXQHufdBE7RYUNvpw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1718100682; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=nLj8yUsK2lei34HSlxIopppsBC+abcyB+V/ZUwcOo4A=; b=jI6eHc3s7Dh200K72Vp2xKk9UjuQgAiiwwnHGqgadNMokNpBfiy12gYTzTcNhkpDibiqeIscxgAx42TLdN1NYekzkrqbB17CFARjGG9mql7wWyxLYxLq3tsWOu11gSe30DXWkBx9F/CaZr2VfoSv6AyvT594tEiswDJnVQT6ChM= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R161e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037067112;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=16;SR=0;TI=SMTPD_---0W8G7ENT_1718100681; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W8G7ENT_1718100681) by smtp.aliyun-inc.com; Tue, 11 Jun 2024 18:11:22 +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 v5 6/6] mm: shmem: add mTHP counters for anonymous shmem Date: Tue, 11 Jun 2024 18:11:10 +0800 Message-Id: <4fd9e467d49ae4a747e428bcd821c7d13125ae67.1718090413.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: References: MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: B778F160016 X-Rspamd-Server: rspam01 X-Stat-Signature: ybcsypd7gznhrnu8de49xkpdf1mtym59 X-HE-Tag: 1718100685-127468 X-HE-Meta: U2FsdGVkX1+EalnkX4KU0UPkXgCHksG1+7nYmRNN0eJjDBeJDKLGkWhOgHN4gd9+itX9mdBNCl6Vb1ArsVlVTsr3xyJcSDrQEneLisVtdJz4WSQV4P35yx3rTSUgib9lO2vKy4A4XfxAIdtCNdIZvlxVSyf80MkQ6PvH8R7W/YbvabEBdeN0P/q2X13IU1pYt7L5AAmmwyq8M0vC231MfNtUFsDDe3qNriivcPaPl3nDo/fJK847Akt7FQc+D4YotWneROnQrGh3nn+F9te7bZPHX4QjZNebMU3JkgSv5fZ8RGuGaPYmHj63nf895poJsmnLCWCyBgw3RQh/JAS0zJkjhpkH1PQsKknPNlb31s/bUExfWcDMFd/6YgbAAymFeJtKajdopqIKy9tiaPtaL3FdGFLdCsSrd+Pcl+65Qri05v8XdH9Fde7DFF7baeMqSG5U/+i8AjJCJ4Un0JCJT6CdDGcZ7gUaBjztqlkP8V+ENRbSkShXSvDKm+T2Dlee+cMG6KWsij9XS4EoFrXgWaUM2EHhlREH4c7L3vjO8IchqfO1JHqyxffp0waCc5VCPk/0CVROSKfavGB4xwBPhmoBiC0g+p4kuJxt4lNwtsED/BD0g/DuZeVvuMBlNuibLrg0ll4G0hGjFeAVMa3m0Yx0JFRBpQugfbOwfWpVl1Tyj6NJlxW5xQoot1LpaL/faXgPoxuxllNUTjvdezaZriwK0HC8Oxzr61w+9K2Zart2sAsWJd6dwKngjTWWAUcevqTi+FpZTLx/jjgpMsa2wzkeTk3TDQwMBVIDFn5fROJhFYaNbub7+mfbDVrf9T2VNfdKc+lGVJ+LlYBw0Ea72D1px2dIDRgyKV6+bwiuF3OVyyhLnOLTFlDof9WTuF5KJLmPHONGUDywi63MAvROm3Loykciwq1FLMiyWXd7J3/KhswWU5THwlEzfp5UZxvybyyAcM9tuKAate1CYh5 dkaTKbFB w9xC5FY0WUAfAECRSdvk00aCa1jN5lz1BmtrGKDIWxUqPN9jrOuyHSkIz6PIZjeAOSUA+aaJh7xGiXI+oCGN7I2Bmv9V89IWBOvEkp6ytzmrq/vyL927gbLQ7KGZFVNMjaugFS33UBO6NaOM39/n+pB2QQbcPmi56CA3QPuGMblfRxsIgpVGNgu8nMHbNBubMi+633LIpxc8G8Gy8xwYGCy9l9rVC6OBITq5IjL9xF15lbP4= 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 mTHP counters for anonymous shmem. Signed-off-by: Baolin Wang Reviewed-by: Lance Yang --- include/linux/huge_mm.h | 3 +++ mm/huge_memory.c | 6 ++++++ mm/shmem.c | 18 +++++++++++++++--- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index 909cfc67521d..212cca384d7e 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -281,6 +281,9 @@ enum mthp_stat_item { MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE, MTHP_STAT_SWPOUT, MTHP_STAT_SWPOUT_FALLBACK, + MTHP_STAT_FILE_ALLOC, + MTHP_STAT_FILE_FALLBACK, + MTHP_STAT_FILE_FALLBACK_CHARGE, __MTHP_STAT_COUNT }; diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 1360a1903b66..3fbcd77f5957 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -555,6 +555,9 @@ DEFINE_MTHP_STAT_ATTR(anon_fault_fallback, MTHP_STAT_ANON_FAULT_FALLBACK); DEFINE_MTHP_STAT_ATTR(anon_fault_fallback_charge, MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE); DEFINE_MTHP_STAT_ATTR(swpout, MTHP_STAT_SWPOUT); DEFINE_MTHP_STAT_ATTR(swpout_fallback, MTHP_STAT_SWPOUT_FALLBACK); +DEFINE_MTHP_STAT_ATTR(file_alloc, MTHP_STAT_FILE_ALLOC); +DEFINE_MTHP_STAT_ATTR(file_fallback, MTHP_STAT_FILE_FALLBACK); +DEFINE_MTHP_STAT_ATTR(file_fallback_charge, MTHP_STAT_FILE_FALLBACK_CHARGE); static struct attribute *stats_attrs[] = { &anon_fault_alloc_attr.attr, @@ -562,6 +565,9 @@ static struct attribute *stats_attrs[] = { &anon_fault_fallback_charge_attr.attr, &swpout_attr.attr, &swpout_fallback_attr.attr, + &file_alloc_attr.attr, + &file_fallback_attr.attr, + &file_fallback_charge_attr.attr, NULL, }; diff --git a/mm/shmem.c b/mm/shmem.c index f5469c357be6..99bd3c34f0fb 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1773,6 +1773,9 @@ static struct folio *shmem_alloc_and_add_folio(struct vm_fault *vmf, if (pages == HPAGE_PMD_NR) count_vm_event(THP_FILE_FALLBACK); +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + count_mthp_stat(order, MTHP_STAT_FILE_FALLBACK); +#endif order = next_order(&suitable_orders, order); } } else { @@ -1792,9 +1795,15 @@ static struct folio *shmem_alloc_and_add_folio(struct vm_fault *vmf, if (xa_find(&mapping->i_pages, &index, index + pages - 1, XA_PRESENT)) { error = -EEXIST; - } else if (pages == HPAGE_PMD_NR) { - count_vm_event(THP_FILE_FALLBACK); - count_vm_event(THP_FILE_FALLBACK_CHARGE); + } else if (pages > 1) { + if (pages == HPAGE_PMD_NR) { + count_vm_event(THP_FILE_FALLBACK); + count_vm_event(THP_FILE_FALLBACK_CHARGE); + } +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + count_mthp_stat(folio_order(folio), MTHP_STAT_FILE_FALLBACK); + count_mthp_stat(folio_order(folio), MTHP_STAT_FILE_FALLBACK_CHARGE); +#endif } goto unlock; } @@ -2168,6 +2177,9 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index, if (!IS_ERR(folio)) { if (folio_test_pmd_mappable(folio)) count_vm_event(THP_FILE_ALLOC); +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + count_mthp_stat(folio_order(folio), MTHP_STAT_FILE_ALLOC); +#endif goto alloced; } if (PTR_ERR(folio) == -EEXIST)