From mboxrd@z Thu Jan 1 00:00:00 1970 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 CAF41C4167B for ; Tue, 5 Dec 2023 16:58:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6101E6B0088; Tue, 5 Dec 2023 11:58:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 598926B0089; Tue, 5 Dec 2023 11:58:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 39D496B008A; Tue, 5 Dec 2023 11:58:07 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 23B016B0088 for ; Tue, 5 Dec 2023 11:58:07 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id EF43FA01C7 for ; Tue, 5 Dec 2023 16:58:06 +0000 (UTC) X-FDA: 81533372172.24.EC770CB Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf14.hostedemail.com (Postfix) with ESMTP id 905F2100014 for ; Tue, 5 Dec 2023 16:58:04 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=IZe1hik5; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf14.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1701795484; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ELf8MFiiC+U22XjQQS3STGR+Op06anfDzFy1HDxkZQ0=; b=kxNl6e8BBrRD3QhqeJ2Cnhih/suppMnGUf+jcjcrWFMlxM1AWqFYICjoqxkE9fzh57NbJR KgrjMeA9PmyLvMZAq5JuqS79ghZYhfaITpa8iHfdUwxCcDMffaXZ+jk/j9FCa0wk+De9qO RPW0whYK6i5STJs1Skm4ITTcSZGlXVU= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=IZe1hik5; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf14.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701795484; a=rsa-sha256; cv=none; b=W9afOOAS8lDi843QjPwy1fQIyzBIjRHBiatJveYvh6VDWfFKmDg/XIixiVxDbrChGggGqE POvOnSEs4XpKOl76t/ojxAk8SxqX0Iz1qu8HPOWDTlaZm1ByAKeSh3xXJoRAU2ID5oe8q7 nJpYaTKVrmvtPPTXqdHrFy2RqwTgTPc= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701795483; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:autocrypt:autocrypt; bh=ELf8MFiiC+U22XjQQS3STGR+Op06anfDzFy1HDxkZQ0=; b=IZe1hik5SGB5fP464kIXKKMry4Qf9kyUzSwYdLQdgxwNLMqdAc4X/quzuTQNskc4O4ZXhr XiaJSdUN3Z0pZVbPbw4tGDp79tq5P+I+ElTrEFVUEQ1s2P4byJh90AJZJ8FX2Ixq8Pma94 aXnTqbgWh8nYAdBj8tOGR5/11iShVLQ= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-325-eUfH3aDzNliZhQSfFdKByQ-1; Tue, 05 Dec 2023 11:58:02 -0500 X-MC-Unique: eUfH3aDzNliZhQSfFdKByQ-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-40b40234bd5so40785915e9.0 for ; Tue, 05 Dec 2023 08:58:02 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701795481; x=1702400281; h=content-transfer-encoding:in-reply-to:organization:autocrypt:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=ELf8MFiiC+U22XjQQS3STGR+Op06anfDzFy1HDxkZQ0=; b=oiUWDa/sAS8pNx4IjbPIjwq26gcrubsZBOScMyyD5SAf0P6d2ZzJedjh73Gl7Gtwp0 YhfEVRlgPbgCrPVoSMenmMx/X9gNxuT+VhYBW7SV+tBdv+T/+CJfm4H1mgApeajU4eQH fVMaF9Iwb3pJlVgIQkC6xhE8VEJmQHYp5uoJCCWdM+UkYNRohFjDKtnTuKDqMHm4uQxs cix0fQx2FrVIdXdop/NbeKYRIkV4FcG8Yp5R7Iz3c9W6mWX5g8rWE3mOX7ztTvozZrMJ J9w1kzf5ytuCYGOkiivitEwxi263a7LKA+IZI6tZGbOCkxu/A4f8BzKY/4Mdl7rQ4rJX 4wyg== X-Gm-Message-State: AOJu0Ywj8qkRv41KsT0xznPrH75eB3/lxGI1xM7pwkYv11+G0Qrzx2ae zuuWRAkVFGjeOve24V2fSb8mPY6/CVBH/S2ipr9yyvrFdN84T4DamjGKah/ATteOIdg0+NwF4R/ Gs1q40cnMycc= X-Received: by 2002:a1c:7214:0:b0:40b:5e21:d346 with SMTP id n20-20020a1c7214000000b0040b5e21d346mr742436wmc.79.1701795480969; Tue, 05 Dec 2023 08:58:00 -0800 (PST) X-Google-Smtp-Source: AGHT+IFZLZ5WaCMtpImiv8y3olD07A0P1y1RYRKjS3FNUCmZ0vK2nZcqPGFt9+yACNOL+6egHrVA6Q== X-Received: by 2002:a1c:7214:0:b0:40b:5e21:d346 with SMTP id n20-20020a1c7214000000b0040b5e21d346mr742409wmc.79.1701795480460; Tue, 05 Dec 2023 08:58:00 -0800 (PST) Received: from ?IPV6:2003:cb:c72f:300:1ec7:2962:8889:ef6? (p200300cbc72f03001ec7296288890ef6.dip0.t-ipconnect.de. [2003:cb:c72f:300:1ec7:2962:8889:ef6]) by smtp.gmail.com with ESMTPSA id z3-20020a05600c0a0300b0040839fcb217sm19586724wmp.8.2023.12.05.08.57.59 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 05 Dec 2023 08:58:00 -0800 (PST) Message-ID: <004ed23d-5571-4474-b7fe-7bc08817c165@redhat.com> Date: Tue, 5 Dec 2023 17:57:58 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v8 03/10] mm: thp: Introduce multi-size THP sysfs interface To: Ryan Roberts , Andrew Morton , Matthew Wilcox , Yin Fengwei , Yu Zhao , Catalin Marinas , Anshuman Khandual , Yang Shi , "Huang, Ying" , Zi Yan , Luis Chamberlain , Itaru Kitayama , "Kirill A. Shutemov" , John Hubbard , David Rientjes , Vlastimil Babka , Hugh Dickins , Kefeng Wang , Barry Song <21cnbao@gmail.com>, Alistair Popple Cc: linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org References: <20231204102027.57185-1-ryan.roberts@arm.com> <20231204102027.57185-4-ryan.roberts@arm.com> From: David Hildenbrand Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoU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i+rugUI+w6DABEBAAHCwXwEGAEIACYCGwwWIQQb2cqtc1xMOkYN/MpN3hD3 AP+DWgUCXw7HsgUJEqkpoQAKCRBN3hD3AP+DWrrpD/4qS3dyVRxDcDHIlmguXjC1Q5tZTwNB boaBTPHSy/Nksu0eY7x6HfQJ3xajVH32Ms6t1trDQmPx2iP5+7iDsb7OKAb5eOS8h+BEBDeq 3ecsQDv0fFJOA9ag5O3LLNk+3x3q7e0uo06XMaY7UHS341ozXUUI7wC7iKfoUTv03iO9El5f XpNMx/YrIMduZ2+nd9Di7o5+KIwlb2mAB9sTNHdMrXesX8eBL6T9b+MZJk+mZuPxKNVfEQMQ a5SxUEADIPQTPNvBewdeI80yeOCrN+Zzwy/Mrx9EPeu59Y5vSJOx/z6OUImD/GhX7Xvkt3kq Er5KTrJz3++B6SH9pum9PuoE/k+nntJkNMmQpR4MCBaV/J9gIOPGodDKnjdng+mXliF3Ptu6 3oxc2RCyGzTlxyMwuc2U5Q7KtUNTdDe8T0uE+9b8BLMVQDDfJjqY0VVqSUwImzTDLX9S4g/8 kC4HRcclk8hpyhY2jKGluZO0awwTIMgVEzmTyBphDg/Gx7dZU1Xf8HFuE+UZ5UDHDTnwgv7E th6RC9+WrhDNspZ9fJjKWRbveQgUFCpe1sa77LAw+XFrKmBHXp9ZVIe90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: <20231204102027.57185-4-ryan.roberts@arm.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 905F2100014 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 33og3mwyngeapzdpg4st5cnw84ethmqm X-HE-Tag: 1701795484-956165 X-HE-Meta: U2FsdGVkX1/mjErF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z7Z1NJQwhvFiMr2arlfr+SI4ZradaNWlm2TXWx3ML8toosrBsvwFB0HKHPrEJc/VPaffuhipOWyjiVqkkeB6Ej1Gtmxf3r9x3XyREv+U10IMSlHRXL4iqKWtAqnZtDFWekTkDVAH+M2IRktT2ilvap9xXfGVq1sbo0oMBXrQyYzrGVEHYvJ7jCDGAD+pEp9zMeHN4OHpY1A7BgafNNOYA6aVC/zmYg3knAfqg0uU9VuBg5X2gwgm2k2Nxx1A8fwum8Wd7mxyUWOW/n16QWTvwJOE0L3xEsanrLDtAhgsMe+pSFFESpvzSYy1knQgAoqR2P5vRxZnezsZtWdGcV4ZZ2hiKQ0VP8CKkCMUUjxyFcYFJ5fh7Oz+0nBcBi+/TuNsvw8UFmZGc/ejA6GIOBddksjwP+ra5R64+cz8+sUoyJpwDYxb0h53up47ZB8/U+soN6wU= 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: On 04.12.23 11:20, Ryan Roberts wrote: > In preparation for adding support for anonymous multi-size THP, > introduce new sysfs structure that will be used to control the new > behaviours. A new directory is added under transparent_hugepage for each > supported THP size, and contains an `enabled` file, which can be set to > "inherit" (to inherit the global setting), "always", "madvise" or > "never". For now, the kernel still only supports PMD-sized anonymous > THP, so only 1 directory is populated. > > The first half of the change converts transhuge_vma_suitable() and > hugepage_vma_check() so that they take a bitfield of orders for which > the user wants to determine support, and the functions filter out all > the orders that can't be supported, given the current sysfs > configuration and the VMA dimensions. If there is only 1 order set in > the input then the output can continue to be treated like a boolean; > this is the case for most call sites. The resulting functions are > renamed to thp_vma_suitable_orders() and thp_vma_allowable_orders() > respectively. > > The second half of the change implements the new sysfs interface. It has > been done so that each supported THP size has a `struct thpsize`, which > describes the relevant metadata and is itself a kobject. This is pretty > minimal for now, but should make it easy to add new per-thpsize files to > the interface if needed in future (e.g. per-size defrag). Rather than > keep the `enabled` state directly in the struct thpsize, I've elected to > directly encode it into huge_anon_orders_[always|madvise|inherit] > bitfields since this reduces the amount of work required in > thp_vma_allowable_orders() which is called for every page fault. > > See Documentation/admin-guide/mm/transhuge.rst, as modified by this > commit, for details of how the new sysfs interface works. > > Signed-off-by: Ryan Roberts > --- Some comments mostly regarding thp_vma_allowable_orders and friends. In general, LGTM. I'll have to go over the order logic once again, I got a bit lost once we started mixing anon and file orders. [...] Doc updates all looked good to me, skimming over them. > diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h > index fa0350b0812a..bd0eadd3befb 100644 > --- a/include/linux/huge_mm.h > +++ b/include/linux/huge_mm.h [...] > +static inline unsigned long thp_vma_suitable_orders(struct vm_area_struct *vma, > + unsigned long addr, unsigned long orders) > +{ > + int order; > + > + /* > + * Iterate over orders, highest to lowest, removing orders that don't > + * meet alignment requirements from the set. Exit loop at first order > + * that meets requirements, since all lower orders must also meet > + * requirements. > + */ > + > + order = first_order(orders); nit: "highest_order" or "largest_order" would be more expressive regarding the actual semantics. > + > + while (orders) { > + unsigned long hpage_size = PAGE_SIZE << order; > + unsigned long haddr = ALIGN_DOWN(addr, hpage_size); > + > + if (haddr >= vma->vm_start && > + haddr + hpage_size <= vma->vm_end) { > + if (!vma_is_anonymous(vma)) { > + if (IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - > + vma->vm_pgoff, > + hpage_size >> PAGE_SHIFT)) > + break; > + } else > + break; Comment: Codying style wants you to use if () {} else {} But I'd recommend for the conditions: if (haddr < vma->vm_start || haddr + hpage_size > vma->vm_end) continue; /* Don't have to check pgoff for anonymous vma */ if (!vma_is_anonymous(vma)) break; if (IS_ALIGNED((... break; [...] > +/** > + * thp_vma_allowable_orders - determine hugepage orders that are allowed for vma > + * @vma: the vm area to check > + * @vm_flags: use these vm_flags instead of vma->vm_flags > + * @smaps: whether answer will be used for smaps file > + * @in_pf: whether answer will be used by page fault handler > + * @enforce_sysfs: whether sysfs config should be taken into account > + * @orders: bitfield of all orders to consider > + * > + * Calculates the intersection of the requested hugepage orders and the allowed > + * hugepage orders for the provided vma. Permitted orders are encoded as a set > + * bit at the corresponding bit position (bit-2 corresponds to order-2, bit-3 > + * corresponds to order-3, etc). Order-0 is never considered a hugepage order. > + * > + * Return: bitfield of orders allowed for hugepage in the vma. 0 if no hugepage > + * orders are allowed. > + */ > +unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma, > + unsigned long vm_flags, bool smaps, > + bool in_pf, bool enforce_sysfs, > + unsigned long orders) > +{ > + /* Check the intersection of requested and supported orders. */ > + orders &= vma_is_anonymous(vma) ? > + THP_ORDERS_ALL_ANON : THP_ORDERS_ALL_FILE; > + if (!orders) > + return 0; Comment: if this is called from some hot path, we might want to move as much as possible into a header, so we can avoid this function call here when e.g., THP are completely disabled etc. > + > if (!vma->vm_mm) /* vdso */ > - return false; > + return 0; > > /* > * Explicitly disabled through madvise or prctl, or some > @@ -88,16 +141,16 @@ bool hugepage_vma_check(struct vm_area_struct *vma, unsigned long vm_flags, > * */ > if ((vm_flags & VM_NOHUGEPAGE) || > test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags)) > - return false; > + return 0; > /* > * If the hardware/firmware marked hugepage support disabled. > */ > if (transparent_hugepage_flags & (1 << TRANSPARENT_HUGEPAGE_UNSUPPORTED)) > - return false; > + return 0; > > /* khugepaged doesn't collapse DAX vma, but page fault is fine. */ > if (vma_is_dax(vma)) > - return in_pf; > + return in_pf ? orders : 0; > > /* > * khugepaged special VMA and hugetlb VMA. > @@ -105,17 +158,29 @@ bool hugepage_vma_check(struct vm_area_struct *vma, unsigned long vm_flags, > * VM_MIXEDMAP set. > */ > if (!in_pf && !smaps && (vm_flags & VM_NO_KHUGEPAGED)) > - return false; > + return 0; > > /* > - * Check alignment for file vma and size for both file and anon vma. > + * Check alignment for file vma and size for both file and anon vma by > + * filtering out the unsuitable orders. > * > * Skip the check for page fault. Huge fault does the check in fault > - * handlers. And this check is not suitable for huge PUD fault. > + * handlers. > */ > - if (!in_pf && > - !transhuge_vma_suitable(vma, (vma->vm_end - HPAGE_PMD_SIZE))) > - return false; > + if (!in_pf) { > + int order = first_order(orders); > + unsigned long addr; > + > + while (orders) { > + addr = vma->vm_end - (PAGE_SIZE << order); > + if (thp_vma_suitable_orders(vma, addr, BIT(order))) > + break; Comment: you'd want a "thp_vma_suitable_order" helper here. But maybe the compiler is smart enough to optimize the loop and everyything else out. [...] > + > +static ssize_t thpsize_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")) { > + set_bit(order, &huge_anon_orders_always); > + clear_bit(order, &huge_anon_orders_inherit); > + clear_bit(order, &huge_anon_orders_madvise); > + } else if (sysfs_streq(buf, "inherit")) { > + set_bit(order, &huge_anon_orders_inherit); > + clear_bit(order, &huge_anon_orders_always); > + clear_bit(order, &huge_anon_orders_madvise); > + } else if (sysfs_streq(buf, "madvise")) { > + set_bit(order, &huge_anon_orders_madvise); > + clear_bit(order, &huge_anon_orders_always); > + clear_bit(order, &huge_anon_orders_inherit); > + } else if (sysfs_streq(buf, "never")) { > + clear_bit(order, &huge_anon_orders_always); > + clear_bit(order, &huge_anon_orders_inherit); > + clear_bit(order, &huge_anon_orders_madvise); Note: I was wondering for a second if some concurrent cames could lead to an inconsistent state. I think in the worst case we'll simply end up with "never" on races. [...] > static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj) > { > int err; > + struct thpsize *thpsize; > + unsigned long orders; > + int order; > + > + /* > + * Default to setting PMD-sized THP to inherit the global setting and > + * disable all other sizes. powerpc's PMD_ORDER isn't a compile-time > + * constant so we have to do this here. > + */ > + huge_anon_orders_inherit = BIT(PMD_ORDER); > > *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj); > if (unlikely(!*hugepage_kobj)) { > @@ -434,8 +631,24 @@ static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj) > goto remove_hp_group; > } > > + orders = THP_ORDERS_ALL_ANON; > + order = first_order(orders); > + while (orders) { > + thpsize = thpsize_create(order, *hugepage_kobj); > + if (IS_ERR(thpsize)) { > + pr_err("failed to create thpsize for order %d\n", order); > + err = PTR_ERR(thpsize); > + goto remove_all; > + } > + list_add(&thpsize->node, &thpsize_list); > + order = next_order(&orders, order); > + } > + > return 0; > [...] > page = compound_head(page); > @@ -5116,7 +5116,8 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma, > return VM_FAULT_OOM; > retry_pud: > if (pud_none(*vmf.pud) && > - hugepage_vma_check(vma, vm_flags, false, true, true)) { > + thp_vma_allowable_orders(vma, vm_flags, false, true, true, > + BIT(PUD_ORDER))) { > ret = create_huge_pud(&vmf); > if (!(ret & VM_FAULT_FALLBACK)) > return ret; > @@ -5150,7 +5151,8 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma, > goto retry_pud; > > if (pmd_none(*vmf.pmd) && > - hugepage_vma_check(vma, vm_flags, false, true, true)) { > + thp_vma_allowable_orders(vma, vm_flags, false, true, true, > + BIT(PMD_ORDER))) { Comment: A helper like "thp_vma_allowable_order(vma, PMD_ORDER)" might make this easier to read -- and the implemenmtation will be faster. > ret = create_huge_pmd(&vmf); > if (!(ret & VM_FAULT_FALLBACK)) > return ret; > diff --git a/mm/page_vma_mapped.c b/mm/page_vma_mapped.c > index e0b368e545ed..64da127cc267 100644 > --- a/mm/page_vma_mapped.c > +++ b/mm/page_vma_mapped.c > @@ -268,7 +268,8 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) > * cleared *pmd but not decremented compound_mapcount(). > */ > if ((pvmw->flags & PVMW_SYNC) && > - transhuge_vma_suitable(vma, pvmw->address) && > + thp_vma_suitable_orders(vma, pvmw->address, > + BIT(PMD_ORDER)) && Comment: Similarly, a helper like "thp_vma_suitable_order(vma, PMD_ORDER)" might make this easier to read. > (pvmw->nr_pages >= HPAGE_PMD_NR)) { > spinlock_t *ptl = pmd_lock(mm, pvmw->pmd); > -- Cheers, David / dhildenb