From patchwork Thu May 30 02:04:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 13679632 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 62F17C27C44 for ; Thu, 30 May 2024 02:04:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 53E7B6B0099; Wed, 29 May 2024 22:04:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 42D4F6B009E; Wed, 29 May 2024 22:04:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 232A56B009B; Wed, 29 May 2024 22:04:42 -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 F08066B0098 for ; Wed, 29 May 2024 22:04:41 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 9D4641A0C0A for ; Thu, 30 May 2024 02:04:41 +0000 (UTC) X-FDA: 82173418362.27.6ADF545 Received: from out30-132.freemail.mail.aliyun.com (out30-132.freemail.mail.aliyun.com [115.124.30.132]) by imf19.hostedemail.com (Postfix) with ESMTP id 9468B1A000F for ; Thu, 30 May 2024 02:04:39 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=YH3mT7mk; dmarc=pass (policy=none) header.from=linux.alibaba.com; spf=pass (imf19.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.132 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1717034680; 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=r9qBGwXCngyL5anV+10XHk4WWPAHBJnN4gQCvtOnGdw=; b=8kLcEPcxHMNON3gPijOa0zSUY78JSqsygDIcxEBOlr3j5NYqlCxkcpfQZjMfW3UZTBTfnY xmNeXd+xq6iIfdToexBfmNhkb32RV6NYd3EaoBZKaR/K/joj33DsBzlQgxwGEkzjaj+egV I7dq8M9DriTd4+BDUPpT9osbiDUChmU= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=YH3mT7mk; dmarc=pass (policy=none) header.from=linux.alibaba.com; spf=pass (imf19.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.132 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717034680; a=rsa-sha256; cv=none; b=J9ODyNnedew7AkRX/y9zS24QXCmgZJ/capPcmkw9AsV7iKRzqhqZWdwymCQlJYFNqKw9cX NsMOYFH0CLbNaOXJPDlxY4IjrsvdbhKB2/ZI3fprqUmRBclksMlpF/3wxJiaGf7rTVqEQQ p+j0FaEQ7MsSOczGStRzm6Ux+lwg2+s= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1717034676; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=r9qBGwXCngyL5anV+10XHk4WWPAHBJnN4gQCvtOnGdw=; b=YH3mT7mkIWCu7V7DO7q+IM1bR5PyITEN6/wmK8uYeNfcKM7rHLANOAaFsLLqbNSo2R7KiD75S/2EHqLR73m1gpFyCSbGdPbqux4QXzIygRJVmnIJNMeHnNLtWjS3xH6RhlO+o9NE3k6RXCzMDO27GM7004NxMtJr0hi2qCvldAg= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R441e4;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_---0W7V0ZBe_1717034674; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W7V0ZBe_1717034674) by smtp.aliyun-inc.com; Thu, 30 May 2024 10:04:35 +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 v3 1/6] mm: memory: extend finish_fault() to support large folio Date: Thu, 30 May 2024 10:04:12 +0800 Message-Id: X-Mailer: git-send-email 2.39.3 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 9468B1A000F X-Stat-Signature: 9pe9fidpi1yczhi3kx36wy3zkau97sfq X-Rspam-User: X-Rspamd-Server: rspam04 X-HE-Tag: 1717034679-440258 X-HE-Meta: U2FsdGVkX191R3Dde8M/6PY8UB6wFZ/2cavNYYVlnf9NKAF6VavBusn8jOWtKvfNKLUyHHZmrNgKBszBSb/prguYIm+m/LuXyjsRjHM9fiF6r0WRY27Q0nq6xHS1nk66DsVkZU9PHrhVeGcCfX2bJ51j/6bTO79ky+t30DqjZVqGezB9Zw5YhCbBpRcUpfSfFk3XOhhz2jmsJgzh9rvp1+Al95vsvy0o4MGH1yjmxeUaT2E3k7xBcIhJ/5c1pEWIrO9IfL1mnRkvTjxZYHm0SQqe7FElBnsySBoGrs57p/EmJjYBcr3CgS4W8qznkWs0OfvWAYGiPTvIJLP+v+hMQO9JpNTZl5h7r1raddXrmkUc2DQX8Y0mgEsaAUWvkMrPXBZwwv4nYIkTRiesjP9F1872CMwT1MTEWEsloI1lRJNWYyG3hpUEOSMgdAoWa9C+09PoTZjVDPhgJfCCuwbw/0/fN6dLdsI8ygFL2Y6TZfxuGarxzeOmTfimeZs9BdqGBJtiH0STik0t6c72XsF970VQxCx4tX7cIpbZAnDYwkqkN05/ULPFyee6dIssoklnxC9Wl/7pQR3VJ+SpDdESzhRDq51Gudep8rUWcHJHH6r+g7sgKn64Ue0/FLNLwh0pHIfq1q69vYC6dZAQE8UhxSi8gH03JONDWta3kyAhCyvTpWpSGR/twdR34Xi1cJ9QUap5kwxPF/95H+qM7oWlaQVAEzJqykE+lIBUd4SoNl5EUS8bg4mAevZTaRw/I/J70vbqvxLT2N74JVrawCRHDl3Pl2tQj8ri39CCegZNmDzkcDcbeitq6TlH21xXcBB2I5rN6LIiAf6StcemdQX+ZXfQ93nzzZyHysoNeI9SmnxoIuE6rbZWdng0JDVTKSaae5f4nIMvnS9BQlSi9OcNkhb+rX4RLK242s6nOr+e5VY33uFXi1jyVKk4Z0U5TgPOgPkhy05u7+CnB9rD136 7d8Ibs6w BjJAV2RGoAhbkf2i1Pkf/IQpMYkvSEMbTOKaCQ/sJzDeHLFVCaVl3PS9EEtMyO3GeM5JTyasV3KcZ7LM02H3TFJQOp5eV1yCktinZvWhMHBgbAggKv6WpBpmnsGcvEpyOYvbqXTLCopFyROdpzgQE9e2b+v5KnfWLGZw2CTzhSdHrkQqSlDs3Uv2ZAAxZcKiI8WC18ERveOsuWsJ62qUVpzvYwm1djd33smrgGmcgXKrRnFn9APK2HhxrsgFfoqQf42lJnetWJmyrD0L4yMHQIjFlAIHpoZKZXnelDPLLfwu0KdrokXV2rWWVdekTSz0MRP5g 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. Signed-off-by: Baolin Wang --- mm/memory.c | 58 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 10 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index eef4e482c0c2..435187ff7ea4 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,59 @@ 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)) { + for (i = 0; i < nr_pages; i++) + update_mmu_tlb(vma, addr + PAGE_SIZE * i, vmf->pte + i); + 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 Thu May 30 02:04:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 13679633 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 1ED9BC25B7E for ; Thu, 30 May 2024 02:04:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CA12C6B00A4; Wed, 29 May 2024 22:04:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BF7026B00A1; Wed, 29 May 2024 22:04:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A98426B00A3; Wed, 29 May 2024 22:04:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 848FB6B009E for ; Wed, 29 May 2024 22:04:44 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 292061409E4 for ; Thu, 30 May 2024 02:04:44 +0000 (UTC) X-FDA: 82173418488.20.993C720 Received: from out30-124.freemail.mail.aliyun.com (out30-124.freemail.mail.aliyun.com [115.124.30.124]) by imf10.hostedemail.com (Postfix) with ESMTP id 24131C0020 for ; Thu, 30 May 2024 02:04:40 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=xPoZGVs5; dmarc=pass (policy=none) header.from=linux.alibaba.com; spf=pass (imf10.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.124 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1717034681; 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=ty6528dVJXEmhzcXVGrzuiLL0AndGm4IMLMhTi9OPbDpZ+8h+Owocf7TMnluGiMoWP63Tc o6/R/U0uCJ0G+ZnW6GoqtJlcaGfU074az6yiy3xHRVQTJQcz/Fp17inX+z6l4Gb69iI0fw tlaA+65u3dEj8fj1JzA+KlJ5bV5/ppE= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=xPoZGVs5; dmarc=pass (policy=none) header.from=linux.alibaba.com; spf=pass (imf10.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.124 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717034681; a=rsa-sha256; cv=none; b=5HjkLJ6L2kcFdGqsH/tM+c1kF7SPPSQSwPzaZwBktj1lcjFwjf357UlytyCPY6UlEGYu6y Xm5fzBC37ulfuRAWgkzZrXHuhJdlYcg0ucKT8/50ZNcIWQdW+VlSOLhYanmNKdeSwgzqDj GN0tFF8kiy3A7JQ8xGXVSuL9jqw4QP0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1717034677; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=sk0VCEBLx9HEPQafud6f811f0Tz6WgnTKVbuudZtsuE=; b=xPoZGVs5Ey7WTqrMjEUy10zrl3uAfbvq+XYkNgdoV1p/UeyfgzkeU2b4SckrMtbzxruo9wb+13mJC3vWZLiWvEoorKKW4+U6ZP31Qg9TLy6YjTyhKjcevBvXw/fSXeLtENQhiuTS+Vn9U6mKDDGHzSTbsrs1cLwWQ/fHbc1PfUg= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R371e4;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_---0W7V1J5D_1717034676; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W7V1J5D_1717034676) by smtp.aliyun-inc.com; Thu, 30 May 2024 10:04:36 +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 v3 2/6] mm: shmem: add THP validation for PMD-mapped THP related statistics Date: Thu, 30 May 2024 10:04:13 +0800 Message-Id: <9122368aafec9f630190da3023e01337ac49b9cd.1717033868.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: 24131C0020 X-Stat-Signature: 7qc5t1wmp67nmhbxb4rhtiko5qixh6jb X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1717034680-341969 X-HE-Meta: U2FsdGVkX1/TN6xxmExPkokcJ/rBeMeuNBpeKE/D7ri9AMF8hYwOmLEhGXnEQfznCa3D5aPNHPQDUcQHHQXKm4TI/BLJFPDe2YidF/vnfjKAZQaPxbfilw9VDnxaI+BrphLbRmItU3YhUjAwZfbmaoJrXzcsv/rKUbBCAGFm4blQTEYDATBMF63+5GxERlN2jZ8j78s3ZoDV/PkuhB1UGto+egw7OOT0EC5fDZ8pJdTHjXoGZtQLUSDnPx4DUcau/ps3QgnKXrBBpq4wXy6dOQjbi7ZwNj4rK1zqmbzoE1Aw6ALfJi8eovukq51IXsPFrGT0Bvm5KAiYUkVcAZMumt7WM5c/NV3jFVjCNMO3nuS0pMMLUburNHbKbvRYrKStNjG5hB/xZU/zIJvGBw+MIpOwYVBKqDDg0Wv5aAqKf/MbPG52/V8VgQzuG5UIaYS1tmxWzbYJM+hWM9+u1sNV7a48b/g13+gmwu+5FWEN7u2RvC33wnJP1q6ntd5txuP4YSBHwyZowBS+BqHlX6SIc10gJ+C9SWLIjsIdRFWfnHL98dEBrjKiYqS9P6jeg/lBzndSNmWi99tGq8Z4RIk6wAN0/SGaxOA9x/ZIGdvbMf+O6JGoVYj9iLObe3OYajmPQi2OaLHDPTYdQ5+9DKN+tIjOt0js7XS71tgJS0M1p8CJ+GBJl70fXBHQgHT1sOs1q1kVlVcF1p52kr4zfnrMDTf/aeJ7/BQHSUXidBAbSYNHLg3eoaZHxo20N3UJMDnhODuj1CybkDAU+dFy0ymC6b1h55mSbINC11n+uMRicsGOESwDEnSq1BvPv3y0bnivotz+2vyB10RMN6o2atbKKgmJQDDyiGcuzelQpgRqN5T2gEjMvciueTj3I/+/oJnv0sbqmJTDlmqfTSxTW7xJR5aoxI6C6K5rzfT3XK62gIcZn6N6WtpBi/K7nA54D/Rw4NLqob5sOD8oVJGwowU BcMxPOio q1DrAcLN1xUXlsme+JkJE/Ej0Pgr6ea5kjRvIAuAuAXVec/eCGdHcLfo06Iuhsj+Shrk+gsc/acFE6lY8rHrA4YkYiSVhZTGnWg7A05RftuQCOlZiA2xBbnWxZAtBx236Yqh6tT8jBiZFT78Y0cTz564E7Nrx5htbTrHb68Gj/KZZq25SVKp0i5jv98b6cOzXSZ5wEakcW9ftJ+rrFZKYRa+c3nKhPZH3k/zRarE4wP6ZI+uYUrxLecl4HaOvUtug4rP2bOYnShLQXRShgSj4I7DrjgOS3Ihsu2ioGzmk0UhlFW8= 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 Thu May 30 02:04:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 13679686 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 61633C25B7E for ; Thu, 30 May 2024 02:15:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D82266B00A7; Wed, 29 May 2024 22:15:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D32B06B00A8; Wed, 29 May 2024 22:15:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BD3016B00A9; Wed, 29 May 2024 22:15:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 9C5776B00A7 for ; Wed, 29 May 2024 22:15:01 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 4399540267 for ; Thu, 30 May 2024 02:15:01 +0000 (UTC) X-FDA: 82173444402.25.0859C08 Received: from out30-98.freemail.mail.aliyun.com (out30-98.freemail.mail.aliyun.com [115.124.30.98]) by imf14.hostedemail.com (Postfix) with ESMTP id F420F10000F for ; Thu, 30 May 2024 02:14:57 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b="w5/SiW5S"; spf=pass (imf14.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.98 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=1717035299; 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=YUWj9QiAe2+Mw0VIxQUZVOOdw6pQcRdezcn7a/MnNHI=; b=fje4ZfjnZT+8DlLjz7myu9C0fMBSC5jvXQvlwih3QkREXeytncEfL/uB6OMSU1Arf04Z4x GY8G/ZcrDuC0YLmfwDEcBue0G2H98dTiupE6V0xtbZ2NLyJ05DYBTme7DZrFLFnPxX401F 5HpiPIKG/1XGlV4lt4JArh1zDjzZGIc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717035299; a=rsa-sha256; cv=none; b=bpO30qsgz+Bk3yQ7MVJwUCDdP5+enuQyAFKk3hJEX+GU1XkplAI87zipxWCk0FU1Ih0780 esA0VeIbaRHUbiudwyqmaqJhYieqZJHaoWSERxlTBPP4ae2++zXG6Tgdytp1ptzFLQUJ0n bbzgJiKQnEgntLqKNHxBKLw+t1BMuso= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b="w5/SiW5S"; spf=pass (imf14.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.98 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=1717034678; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=YUWj9QiAe2+Mw0VIxQUZVOOdw6pQcRdezcn7a/MnNHI=; b=w5/SiW5S1XgwOqLdHjpwRx/M9GzQRfyo20KtzUSd8OblcTwNwY/Fw3ag/2T+So8EYdhVcqILHdNVsyMXaKkkz7gwpOCY/X2BIqbNt9uc97wXM5ricwRAFIWjCHL8rP3GZcDXNi0uinLa0t9sTvOGJNLvPH1YCZiQF5jfadEHL/w= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R191e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037067111;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=16;SR=0;TI=SMTPD_---0W7V1J5Y_1717034677; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W7V1J5Y_1717034677) by smtp.aliyun-inc.com; Thu, 30 May 2024 10:04:37 +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 v3 3/6] mm: shmem: add multi-size THP sysfs interface for anonymous shmem Date: Thu, 30 May 2024 10:04:14 +0800 Message-Id: <716c515156e8c891766d8fd3f1df231d289b2a37.1717033868.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-Stat-Signature: 5jdhe5p7z8s4quz37a9jfdaebwk1s38j X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: F420F10000F X-HE-Tag: 1717035297-682407 X-HE-Meta: U2FsdGVkX1+zcZ1NqOJrMRn+aZVvoJ7SZmrwz5ux6BtvEs5bcT5pmfy0V89vza+QQ9tnOx4uyh78HXXSRn81LcXisC8ztySCzdfW2+rMeUaAPHgPyrjiXg+CJxOncgEPUZbh+gPHdpm1b1DojU7v3s7Xcl2doxKgSsFRqA2izKpmmj01qMrpzU+Rn5VdZBChVK+YB6kAe+ydz6AXA5f5rYQ3GYZ93foc6f6vXGQ5Pd60I4Qafumw6ol/wDMwflSVLf+RMK33hipQqKOlao9KXG9s37Zrr3KBPg38F6ney4uLtLqxgxw7oGVCALGqryTlgBCrDsbWzeBKYqiTkW9j4ggA36mWtiYKqNW4coCw6H6k315ygtS/hB/IrD9tptJBtxF8aA0TtGd1KcG0+Gf8t4iJXfc3bOxR+VJhZ7DbfL2n+vq484fohjMvtQ36Yvf8TcAbmGLB+IRWHpEPKwfJFWSr0YjnVQrVy6sk9L9AJFOh+52WhRzBJm7mKHCtDK6vP+A2XHSOGrmKIQQtPbQICH+2JP9ADj42OwYCDJqlmg4i2v4M5gSRNigT5UIL1ltIU2uoMNtzZqiG+p5nNvDzjAgUuHy0laijFO9krg7Z9uyJBKrDd/TO7MgOPK0HBBUk3ZwuSkYa1c6pqYiVpvQae5prS02vej6tCczIPcJAqInbJAwBaRTNIZC7rT4B57Cy65+TK5QzEVjgwb7+CwlFL9rWY2/0zMbe4WmCK32de+Cl+R6KBLoZ1H1gv1jPSTgBFFG6Rwqe4PzHVQHTBkQjMf4i7YSmCKX6SBtRs8R7+nGu/An7SNf3gc9tLboIJPHPkshR6x81uw2OuMIDPXAYiOZYha1WIIk1WsJzuAveg64ixLTecBnBPW392xj6kqnwX5lUGHOL/ko0q5L9SKKalaf77X/QnQhhtDKtaUbFa0Qk0SkQ8JxTzWTL9FRXDtFyrOFgzKzHHogqrevnCBS vb3DhKw/ dO/9ees8crzPNXVil8SWpLC5O0CGX/p7rXMRrH+30INrTIBuI/pGkzXzgby656/TgQeIdZ8s86rno9uMIaaz7Ewql2TqezMyZjXF8IgbKtRlloBSwarGz5G/VwEn/gTfX9DZC1vrQg52xApwxhU9QY0lOoTcBM3EvdoRvNQU5rnaNlp1+lwsd6YNHUWeVy4C5ftoWTT7NPOrHb6KRjG7taYaCG9Jvd36/Po1/QSet0Kl8S+U= 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", "deny", "force". These values follow the same semantics as the top level, except the 'deny' is equivalent to 'never', and 'force' is equivalent to 'always' to keep compatibility. 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 | 29 +++++++ include/linux/huge_mm.h | 10 +++ mm/huge_memory.c | 11 +-- mm/shmem.c | 96 ++++++++++++++++++++++ 4 files changed, 138 insertions(+), 8 deletions(-) diff --git a/Documentation/admin-guide/mm/transhuge.rst b/Documentation/admin-guide/mm/transhuge.rst index d414d3f5592a..659459374032 100644 --- a/Documentation/admin-guide/mm/transhuge.rst +++ b/Documentation/admin-guide/mm/transhuge.rst @@ -332,6 +332,35 @@ deny force Force the huge option on for all - very useful for testing; +Anonymous 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. +Its value for each mTHP is essentially consistent with the global setting, except +for the addition of 'inherit' to ensure compatibility with the global settings. +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(); + +deny + Has the same semantics as 'never', now mTHP allocation policy is only + used for anonymous shmem and no not override tmpfs. + +force + Has the same semantics as 'always', now mTHP allocation policy is only + used for anonymous shmem and no not override tmpfs. + 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..d5ab5e211100 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_anon_shmem_orders_always __read_mostly; +static unsigned long huge_anon_shmem_orders_madvise __read_mostly; +static unsigned long huge_anon_shmem_orders_inherit __read_mostly; +static unsigned long huge_anon_shmem_orders_within_size __read_mostly; +static DEFINE_SPINLOCK(huge_anon_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, when anonymous shmem uses mTHP. + */ + huge_anon_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_anon_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_anon_shmem_orders_always)) + output = "[always] inherit within_size advise never deny [force]"; + else if (test_bit(order, &huge_anon_shmem_orders_inherit)) + output = "always [inherit] within_size advise never deny force"; + else if (test_bit(order, &huge_anon_shmem_orders_within_size)) + output = "always inherit [within_size] advise never deny force"; + else if (test_bit(order, &huge_anon_shmem_orders_madvise)) + output = "always inherit within_size [advise] never deny force"; + else + output = "always inherit within_size advise [never] [deny] force"; + + 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") || sysfs_streq(buf, "force")) { + spin_lock(&huge_anon_shmem_orders_lock); + clear_bit(order, &huge_anon_shmem_orders_inherit); + clear_bit(order, &huge_anon_shmem_orders_madvise); + clear_bit(order, &huge_anon_shmem_orders_within_size); + set_bit(order, &huge_anon_shmem_orders_always); + spin_unlock(&huge_anon_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_anon_shmem_orders_lock); + clear_bit(order, &huge_anon_shmem_orders_always); + clear_bit(order, &huge_anon_shmem_orders_madvise); + clear_bit(order, &huge_anon_shmem_orders_within_size); + set_bit(order, &huge_anon_shmem_orders_inherit); + spin_unlock(&huge_anon_shmem_orders_lock); + } else if (sysfs_streq(buf, "within_size")) { + spin_lock(&huge_anon_shmem_orders_lock); + clear_bit(order, &huge_anon_shmem_orders_always); + clear_bit(order, &huge_anon_shmem_orders_inherit); + clear_bit(order, &huge_anon_shmem_orders_madvise); + set_bit(order, &huge_anon_shmem_orders_within_size); + spin_unlock(&huge_anon_shmem_orders_lock); + } else if (sysfs_streq(buf, "madvise")) { + spin_lock(&huge_anon_shmem_orders_lock); + clear_bit(order, &huge_anon_shmem_orders_always); + clear_bit(order, &huge_anon_shmem_orders_inherit); + clear_bit(order, &huge_anon_shmem_orders_within_size); + set_bit(order, &huge_anon_shmem_orders_madvise); + spin_unlock(&huge_anon_shmem_orders_lock); + } else if (sysfs_streq(buf, "never") || sysfs_streq(buf, "deny")) { + spin_lock(&huge_anon_shmem_orders_lock); + clear_bit(order, &huge_anon_shmem_orders_always); + clear_bit(order, &huge_anon_shmem_orders_inherit); + clear_bit(order, &huge_anon_shmem_orders_within_size); + clear_bit(order, &huge_anon_shmem_orders_madvise); + spin_unlock(&huge_anon_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 Thu May 30 02:04:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 13679634 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 2146AC25B75 for ; Thu, 30 May 2024 02:04:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0D5726B009E; Wed, 29 May 2024 22:04:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EE9D86B00A3; Wed, 29 May 2024 22:04:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C944B6B009E; Wed, 29 May 2024 22:04:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 985356B009F for ; Wed, 29 May 2024 22:04:44 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 595541A0B6C for ; Thu, 30 May 2024 02:04:44 +0000 (UTC) X-FDA: 82173418488.12.B614BE1 Received: from out30-132.freemail.mail.aliyun.com (out30-132.freemail.mail.aliyun.com [115.124.30.132]) by imf04.hostedemail.com (Postfix) with ESMTP id 5214340003 for ; Thu, 30 May 2024 02:04:41 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=T1hwLpiR; spf=pass (imf04.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.132 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=1717034682; 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=/cjmkEictpYgfi5LrmgMbDpxN9fCM6bi8u7Bms91la8=; b=Uy6Z4KNEnQQKpR3mYb2lbx+waRpGIy0fNWfgRCn6yb8eVolSFNQkSu65Szc75h8zK0MFw/ vKZ1zwpgzjUtJmPIAU0/esW/kz5JCXzOathxoyCsltfwoLZPw2bt6lDV8pmVv12C3JfQbG 1ysvLuTY3nynAPJZ+PQdRGXcP2+B1kw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717034682; a=rsa-sha256; cv=none; b=VhPIvk44pFosdChjEpMsuFBfAglhbrRa1RODGx/awnw6XwRY/xIj65pZ3jvHfjtgQZ+El1 RY3mSKkpfdeXEmJtTYfos2xVY9cXLdDXoYBpK06h+eaKD9UasJAORJXUS4rWf3oPSySK0z V3JMOPF0MxuhI6RcK5FP0WzyWqv42Uo= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=T1hwLpiR; spf=pass (imf04.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.132 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=1717034679; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=/cjmkEictpYgfi5LrmgMbDpxN9fCM6bi8u7Bms91la8=; b=T1hwLpiR5m62SMbBvItxBbrdlKqSoXNfab+DmdFbS1oHcc2if7fLaGtMxY3Uc4YD/eSvREx0qUsgzCPnpEv73SIm9a47fjyolYWDd8UxuTBVIgjWFAKTGQWc9Q78uOFqHE5HFRFpE2bPflsAxcOrAxlVHgEzyNc9wq4NtLcBmbs= 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-contentspam033037067113;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=16;SR=0;TI=SMTPD_---0W7V3uPt_1717034677; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W7V3uPt_1717034677) by smtp.aliyun-inc.com; Thu, 30 May 2024 10:04:38 +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 v3 4/6] mm: shmem: add mTHP support for anonymous shmem Date: Thu, 30 May 2024 10:04:15 +0800 Message-Id: X-Mailer: git-send-email 2.39.3 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 5214340003 X-Rspam-User: X-Stat-Signature: by9yytos77xffuf13i781pd4tqf6dm7t X-HE-Tag: 1717034681-412692 X-HE-Meta: U2FsdGVkX1+Rtapd00phnkY+RQOBT49riGShXKd63qdrZGzMKtVPFYDxZpt33Sn1QefhCxpvrUtAyPL9Sor8PJAMs2YcBOadfTww5C0pEa44HEGwHPyCdI/YcsZS+KnRBcHQsM6in8C21ZUFQxINJGKfg2klFOQd5skvdX11z0q0W968VcYzVlmHo4RGfgBPnDTR9v/zZyAj9pIuXsEuXI4zk0zfOlKrarmppBtTlxbBHMJyBfwl7weLA173lN2fjyISYyur5zzkj4fbMrXK52gistis1Y32ttE9oOQmbO2n7BOoOG0R07sNfQ7aXTEz9Z+6otKHEwhomo+Gf12QLH5ozotzukgNDszGHBYTq1IUu61D7w5J2mCzF0AdjUQbtHPQ/Mkbk2vG6MHYVP18LM/j9EjIt3cgU+uhydf/NSFOxGmcVGpAwrWq7TfWeIlyrWN0/1KRlmyxnSjSARNr1BsW528IImYuT6jiT8vdaMS3TtgMLamXQ/t1A/VeAzevD+MsWJxqUfiBegLQTfXPm+L6yXrrS5kljjUzGG4JQd17/GvZpZA9nk2qVzz4ASt+xJj7Bs2krfE9khqEIwJwtthJYe0Jkh33NfE7mKvlXQkqAZybvcci4pbgPPFqZcgsJXSreevdJBUfyOap71XSwoJ1GQvF2OZBbfChXmRanmdhweBV12h01Q24wNrcJDK5cSdvzLOMmTWv+WCk3iWo51CKvLkwkTWMyRUFONIks34kj8Fkbn1yb8754OM4mhJlYEwc+cTwYrmzTH/Stbrf7ezb6f8g9qYbLLiG1HcZX0ebWNd9hlqWDeLgasBjifMDEMG2Y7S1JfElWw5HYE/k4CvbifToNcILOpGEOSwaGjFc2zmuFX5GNEJhLta+Z3oscA5jCGRyKVxCwP/SGvnSWCvYvTTg4cz98uFj9b5tdDUsTcsFYODlr88y6tjyXT9FhH9Au9MgheXigEkp3cx bITBTG2c QEqW7RmbG4/EbHZupbiFbd5+HzsSfsZySy5O9IJxcPKq8BgNr6FVxK5ljzq64FpiSNxoBRx0FU8Dga5BIisXi00ZVJponkCwX4Tmxu0Zs2erpv2Mizbf5oGt+xtV7MSvhhsumz9ee6juhDXeuSFr0Mrt4PgzkvKs8566vZzVz1apKYohc5AQHH/sE4fKy+9ZRzonxsW1mmGa6djdqChl3GxLHeyEyxU5y0FMyh0NqDjF3VfU= 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 share pages 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 share pages, 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. The primary strategy is similar to supporting anonymous mTHP. Introduce a new interface '/mm/transparent_hugepage/hugepage-XXkb/shmem_enabled', which can have all the same values as the top-level '/sys/kernel/mm/transparent_hugepage/shmem_enabled', with adding a new additional "inherit" option. 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 d5ab5e211100..493873d7246c 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 anon_shmem_allowable_huge_orders(struct inode *inode, + struct vm_area_struct *vma, pgoff_t index, + bool global_huge) +{ + unsigned long mask = READ_ONCE(huge_anon_shmem_orders_always); + unsigned long within_size_orders = READ_ONCE(huge_anon_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_anon_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_anon_shmem_orders_madvise); + + if (global_huge) + mask |= READ_ONCE(huge_anon_shmem_orders_inherit); + + return orders & mask; +} + +static unsigned long anon_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 anon_shmem_allowable_huge_orders(struct inode *inode, + struct vm_area_struct *vma, pgoff_t index, + bool global_huge) +{ + return 0; +} + +static unsigned long anon_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; + 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 = anon_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 = anon_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 Thu May 30 02:04:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 13679635 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 BC640C25B7E for ; Thu, 30 May 2024 02:04:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A23E96B009F; Wed, 29 May 2024 22:04:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9D33D6B00A1; Wed, 29 May 2024 22:04:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8003B6B00A3; Wed, 29 May 2024 22:04:45 -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 5D5F16B009F for ; Wed, 29 May 2024 22:04:45 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 0E9ECA0E70 for ; Thu, 30 May 2024 02:04:45 +0000 (UTC) X-FDA: 82173418530.16.0F94771 Received: from out30-98.freemail.mail.aliyun.com (out30-98.freemail.mail.aliyun.com [115.124.30.98]) by imf25.hostedemail.com (Postfix) with ESMTP id DA9BFA000E for ; Thu, 30 May 2024 02:04:42 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=YuHhpIIJ; spf=pass (imf25.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.98 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=1717034683; 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=OpKbZmXRrURMX+/QsaD7+TkYK79RF0tdTg/q8PuO7h4=; b=mLBztUrVQc3yCbzcIINrHuN+q7AtL2HD9tCK8nqacWQCtfARrzVSYQWwj0a1dJgpTAV0Wp 8nybS9afNWTntFFB72whg+Uj5wuZIU9TIV1MbfJJ9HjVMQCC209sDcfdV+4HRAVxTz6FAg yp6W0EYmOd+7Fktyne2PTBfS78fvyFc= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=YuHhpIIJ; spf=pass (imf25.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.98 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=1717034683; a=rsa-sha256; cv=none; b=k7L9oNTTHLZelxn9qDjHuVhZoQRearEEN2RBIWCyR0JM1ANvvKOrVVdUyXDG6yeKOarb2H o64XNG4pUAJjAhoh9GX9l18cs9zRhIec1KcHPmSL5CGzGFEOQFlL9k7MsviLa68Pojghjq UJN7a0kLYD6zKTuQ08zkZR4JmFGRYxE= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1717034680; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=OpKbZmXRrURMX+/QsaD7+TkYK79RF0tdTg/q8PuO7h4=; b=YuHhpIIJoMVLAOPeznpleGZHsDvkg+6jnktDZDigNmAHgWj69xaV5VN/g5vzr9729yeI1qKsN5UW9pIvvhefIh7T3RSEfnegrIzVWFk5JyYb3CXOEaniyx0ZyhAWV1i+jjLOD8i/B0wWewxgqPrPZ5o/xhbRkKrAVLdlU5LRdqY= 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_---0W7V1J68_1717034679; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W7V1J68_1717034679) by smtp.aliyun-inc.com; Thu, 30 May 2024 10:04:39 +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 v3 5/6] mm: shmem: add mTHP size alignment in shmem_get_unmapped_area Date: Thu, 30 May 2024 10:04:16 +0800 Message-Id: <4d0b226f2a14f93d156a01855db32ff562547bcc.1717033868.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: 95im3ssmczmruaihgbfsdcfcqnnwo9ye X-Rspamd-Queue-Id: DA9BFA000E X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1717034682-97835 X-HE-Meta: U2FsdGVkX18v1liCvAXtHGdBaBxVLU9Xa1msWWx5C0fWPHfiel+9JcdpLwkcgRdQIST88XIW0kWVwjC/w0fxL/j17RVe0WW4L5UUKSoLLDLU1qdTznB6x6kxA70/s9upjwvaErqdCGljTrYHsVnfaRMTCxqt5Haz322ye5tPG3vTIXnISc8s8tGmPPHor8vkKeKGavjvXwAKuiW1JEFDyC/OEdoXKyMRKAIkcELs4WiWz7g+MAkKsKmoYbso8SxzdFIAomREdC2qKH3n2uIIY0phT4WsqOofKf9IRvgMGYLm4a850Pjk0FzSfj5W2i1gODOMj52W5fZRwRmXdFcYNwXZt+EzOLW++tFUy+ZtiCLaDxzARVcM4uX15JA3FhonagFcuNVVw/qrkWqsScObxKvRjP3qHwKZoJ0shlmQ5j/gLkDmvAVDtUd2kL9khvA/Uduj50iUt5HmbE0M3xd25opwJvJGjv1tTddqqmEhx/DjF0FMI2QDik2caDemXKejooUzXnGdSv9HkJXdAI0ZgLxqB6uBIkoybSW4AkjWfiQRWnp95w6gVhPZCZV6AIqUSEMRxDLS7MAQVZuDbyo3AiknM/9aycIruajHSHCtFc3tUdq3OWZnI+Qb4h/jSKpRGl3PLSjUk2rcoBRjI06QEXH/0DukXwzkQTCVRGGrXs0wPAeQATWUpBi7csc4sgaP8Bz+KL1hgLVyBf2Fv56UQiG3UpxdwO7stfcyP5C7+GQu2NyH6htyoajp5b4F3DmZ5dmjLl0ZTyceZiamHRA409H2qUgYOx2wBYd3TbkStAvXmCpK75HRdB5Cdg/5TxclgUMlBTVkXLrmhIslbK6EWHetiwmCkXiZ2mH8GcWinIX8LtpX5F/XkNsMxsRFF8yyp3blGTe781qGf/Oh4kXSVjXyZuWmB7bf/dWJ+/4piy/5ywIegekhVGmsIQ0KGeuI9tm65etNKGc+KtwuJy4 hNEeJN/C 1pXWEQGeuSAlvCcyeg1wMHNR79BquuHA0j7pJCznZeErFdJLHTl7pYoA9c30aFvwON3QOg37VnSRxdHA8dsCexsEdbTkipR8pNWq4nyyHJV6gyrFz+QCOuSyypdKdmTklKsoXpAwTYF6ft6mm5RyTTZqknpvcHEk14stN6t3BZ4O1IW5JLW1MRKtfnErpicru/dv+KgTTFCi/1Q4zBb/DSALNgD9agkWO3a5lkWlI9HumC0MmPYxINaZhzkadWiK1G+Be5xAzZx8RTGRH6ol/IYRqGQSgLRzFdduXCx9VlBzgC3wbrhWQkqxekw== 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 | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 493873d7246c..efc093ee7d4d 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,34 @@ 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_anon_shmem_orders_always); + hpage_orders |= READ_ONCE(huge_anon_shmem_orders_within_size); + hpage_orders |= READ_ONCE(huge_anon_shmem_orders_madvise); + hpage_orders |= READ_ONCE(huge_anon_shmem_orders_inherit); + 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 +2481,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 Thu May 30 02:04:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 13679636 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 2F461C25B75 for ; Thu, 30 May 2024 02:04:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 542486B00A1; Wed, 29 May 2024 22:04:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4F2F26B00A3; Wed, 29 May 2024 22:04:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2D0826B00A5; Wed, 29 May 2024 22:04:47 -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 0BBDF6B00A1 for ; Wed, 29 May 2024 22:04:47 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id AF83E40B82 for ; Thu, 30 May 2024 02:04:46 +0000 (UTC) X-FDA: 82173418572.16.BC77374 Received: from out30-124.freemail.mail.aliyun.com (out30-124.freemail.mail.aliyun.com [115.124.30.124]) by imf22.hostedemail.com (Postfix) with ESMTP id A3AE5C0002 for ; Thu, 30 May 2024 02:04:44 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=chj+aDAX; dmarc=pass (policy=none) header.from=linux.alibaba.com; spf=pass (imf22.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.124 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1717034685; 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=ptAk81AGNsYnCqrfWiyusE+CUxd4N7JUTOqMyRP+zl4=; b=1CgsyHe5G/BpVvcWB4YwYQ9u+oBd8iRaKHutLzkbcA2hidzRMpiu2phPwG4usyNuatCB/Y hsB5kTcpEm5a+tDRl97vRTJCTY+4EeC6ZsuoDksnIAxQiZKzShrB9b6qvN4RmBN+4nUiOa 9tEWRIhmFFfgR3OhKxFc9UepIGKeTDU= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=chj+aDAX; dmarc=pass (policy=none) header.from=linux.alibaba.com; spf=pass (imf22.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.124 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717034685; a=rsa-sha256; cv=none; b=eYJH+bQe+Hhu83/5A+N5A9M1JjuM3DPk0RUZ+CvCbjsvf1XfNGHsCHj6iftYYR2WJTRKal ICKj0FTTO3jZ4mm/gYeti0Z8J5gbSHrvhJSc682Nrxrxw2wIZT+sPCJ+ETFYonkjKBDDR0 rwtgxVAqbdaQ4hUEGRFdrDzvOJ0sf2A= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1717034681; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=ptAk81AGNsYnCqrfWiyusE+CUxd4N7JUTOqMyRP+zl4=; b=chj+aDAXZTS7mDUOgsmGZO28i95jAOvEBzKpdeUXB5WSSlTNT8RUkAAI1n3wtCD97oh5EL22a56UVXzDOKfQY9GemhAffXcwvOmgHU5vH6wfi3A35m37xME5b/pP0Pw/LKdvzMBNF91XMq2AWsIhQW9lvbE6gMbTZYo8RS9nsBI= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R201e4;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_---0W7V3uQV_1717034680; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W7V3uQV_1717034680) by smtp.aliyun-inc.com; Thu, 30 May 2024 10:04:40 +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 v3 6/6] mm: shmem: add mTHP counters for anonymous shmem Date: Thu, 30 May 2024 10:04:17 +0800 Message-Id: X-Mailer: git-send-email 2.39.3 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: A3AE5C0002 X-Stat-Signature: yer84kprssetaicxj36ejmjqtua4i6ri X-Rspam-User: X-Rspamd-Server: rspam04 X-HE-Tag: 1717034684-62039 X-HE-Meta: U2FsdGVkX1+rRN0TqVut5WG2b1yRPsXXVH4OQDGHSBXwK/PFb3/tNgD6dctw0bRTiFuHAhXf/tsEMvnQacenbatjuDBakE7p3KWkzauoxxgJLL4bb8yP3PXbxV9+5GjS5xcw7YfDxkIcgqxkCwZpSqN7/fXF5DNxVVWxTNRkO+Qz7AW0SnajxSQufWXWz7W+rWtpIwnctaOfdVimfUfASffVjX8GVASH2PtKWqljlZUAp+iYvp/ogHFxhMryIz1oANPI1uFNwA693vc28DMitKD9iF8LONFJdnKoSnloKLMmd45Gjp5drK7UR8k+dQlNX3SYC8LDYl9sVyzN4pzVHoJNVEdbhbLGOHEI2eM+vtq2DT3V9tDQZ7mg6ZqAvhISIhXer+Afdx5cBaW7g9S8T4vIC+nU++IbCV1z4151dCdAwipH02Wj7btQs2aixyC/Ycu4QhsnE8kbCRYwPny09J4cgMoxrvt60ktEoTNxtBfEPADRauJLW9onURJ0gOr8ZvnrBcCRYhxnz44wBvjVIp11On3PE0hdULtKPlNcCMJz31CaVuwtPEJj9aTlCUELKY4GHzhOksBB3W7ONxRH/G02aJRZGJPL+a1kO7q4GzcoLUfR9rPgmRybmBs4ZDTZ37PxjowVza5bqJSY/pM+gunj0kBdil1bvnU85xtXhNxydE1XJrx1LwiI91Vu7Vjl0sSlELoKklwi9yS0HWovNoiNYechRfdpO4zKodSRPg/L6C71mTiYGYIu1bfW5Kbynu74wnUzP9lNPIFdN1fQENxySe/CAe898vHtsEkVSRrd7kcYGAax68U89A8eAzrUGTvoVeymDDLxHjS5NGN1kAZQHNxDRbYEOEM6wVBv98cxTVyBqBfueF0GSY/2x/1Jtwv51chDFFJCouNDj88Nhv0aYdEoaTcY0maou3Sft1ESeT7jRAmDvnIihszNxUhMAluewonX7UkVw5oJ9Mf D4elntRD 2zNu7J75oTM/rYnjnPGP2CXNVfFARXREegX+xdJ3yuF+QT0M27JurRukNZ+LpFQHuDfC/6uvY0U4dsUXWuLW7tN8MjP/u3WMEYSIQ69QmLsZ88D+zcj4tx5W4Kjp8oI9eDIls7WrtBLTW/eI6DqNpAQ39I4nEQG3semgK9gwUWjnJ+AqSK3anFXPOf6QVuhbZM8lhWM7h1h/K2N21xPaf7ihCNVXj5Ftn9QkJyP8ZakWZgEE= 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 --- 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 efc093ee7d4d..cdcc3b919bca 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)