From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5AD5F1F7586 for ; Tue, 7 Jan 2025 20:40:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736282416; cv=none; b=Al6yNB0OpDcFKfgRFpKaabnWcMoYCrQ6ogrzydIoEwDJaj3YZHtsM3TT9CL5rVgPHIqgnJKTX7kSqtceKeGs9eQ6ZuOnn6aNfk4Jz+psdGYnJDPItBJIteusWuIxEdOvWWsttX0H0vRbvDHoK1pqV53S44O3GnxxeBhaLfM9CP8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736282416; c=relaxed/simple; bh=rWneKYGt7D2DFvoz8LH5TQEfbGYz4EU5IesPIUTseFs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Akc1T9hSptbxM5c08Ne90EXun3zFjoWp8jelfy90VKPzcJFKAWQGT6vcwys9F8eNzajn4pXXF15C5mtNWczenfALbEiHK69d2kV6XFB1V3mbaKaMbP9vIKRINYDCUAxgrWro0gdQlYB6a7MfVIunf4Bv7vWDMA74wQf9JxeoHwY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=JkPKe9qw; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="JkPKe9qw" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736282413; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/Z9zMvty6z22PLVpM0GzhFMnUyCY+IlYCy3OrPbUsSM=; b=JkPKe9qwGcy1QEAo9qOdU/Z9Vf8vwF4jmGk7KuosSLx8mVBU6hlp0yorx+ZXGL2Y845SGD YFbyWP55pp1JJMwbafRt/d/MU+X7MeAdR87/ZN3eJRIg8JdIuyUQyAqWMFBBJq5bKQYnzS tJnobxFePhrPRxz1x+9sdqI+b979MaE= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-549-AXqNcXecP2a_H_RILENVXQ-1; Tue, 07 Jan 2025 15:40:11 -0500 X-MC-Unique: AXqNcXecP2a_H_RILENVXQ-1 X-Mimecast-MFC-AGG-ID: AXqNcXecP2a_H_RILENVXQ Received: by mail-qk1-f200.google.com with SMTP id af79cd13be357-7b6e241d34eso2276747385a.0 for ; Tue, 07 Jan 2025 12:40:11 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736282411; x=1736887211; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/Z9zMvty6z22PLVpM0GzhFMnUyCY+IlYCy3OrPbUsSM=; b=ImUmTfe/nnNbiqEx2eNVJyKcEc9tBNCLECOfXztFiSykxj+P2QZucMPgNdXnT6576m +YvLr9VG1WrLEARE5UW2toEIzLBHsmKhQwxNki/dRpGUP0ZOd3ljzb9A11Pl8lv4R01i u2G6ilT/7NBmIi9UHZ6WQJxo3KuEkN++PHiAwjU9NlAdDzbODabaUfnRTHeIsrJiFBY0 QIJrzj2BRk8vvilxBu9PFFJllNtAePyt+f6ctPoX/6tkoeC3l6brf9kSsrDoOTKFz/YI oyqG2Zha3xn6L+Tn4QFAjf76IMMJ4rS1NmDT53VzUIjMM6E00I/x7q39qsl2EtRPnt2d RY8g== X-Forwarded-Encrypted: i=1; AJvYcCVCgtLjvj4G04kPbzaQjajt83MnH952MBSCOdNWVpsuIjV1zqEFHaud5FVOr0QZ0p6SU2IT0R/Y+XaKDq8=@vger.kernel.org X-Gm-Message-State: AOJu0YwF6Aagk+9ZPEQbSi8gjqpiu0+3faBN0HtfG+EfK12EbpakQs0e 7rJDc8Rw9bbmml+epVbob8XsZ98X50uHGoQ9So70AjJpuRNFIcgf5YfnV5I0NkbmrZjXQvUCdGy MO/Q1HXrfNWa4aUtDbaZbg4I87+wmpbGASGchtN/kAN9ecE4bH/WCeaQAP0ubdw== X-Gm-Gg: ASbGnctnggeTbox0Oe9wBZcvNwBkRHZCO2UzBIHs/V4MvAJBZPGA2KcTQCX+d2Sh3VK REz32PBIBj8/aMBgZonD5wnQM3QjWXy1KOzUui+85rx0rko/GxnMVG1vtGAoqNr23JAPUJ2codF xcLyI92SVSTqr7lwl0/GZsZDj3HTnykj06BUFp+Jns1WrL5By4Jx8vND+DGJlkG2OveA6bE8IYs /dFWKa8smPAecDXOUiBt+U+ilfo/np5hyjYaeCpiKL5AeaRVNGNvSHHTXOp6ly09M32iyvtjk7v +kMu2WOUele9wen+pX51AQho7XMexoC6 X-Received: by 2002:a05:6214:27c2:b0:6d8:b3a7:75a5 with SMTP id 6a1803df08f44-6df9b2d50d7mr8913906d6.42.1736282411351; Tue, 07 Jan 2025 12:40:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IHj1C6u68Y/+sXafpOSyDBghbB3mfdOGpS6Z/lZ5W8Owyk67iqxQBFKx0AO6ST9F8O901e9zA== X-Received: by 2002:a05:6214:27c2:b0:6d8:b3a7:75a5 with SMTP id 6a1803df08f44-6df9b2d50d7mr8913536d6.42.1736282410900; Tue, 07 Jan 2025 12:40:10 -0800 (PST) Received: from x1n.redhat.com (pool-99-254-114-190.cpe.net.cable.rogers.com. [99.254.114.190]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6dd181373f6sm184478306d6.62.2025.01.07.12.40.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jan 2025 12:40:10 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Breno Leitao , Rik van Riel , Muchun Song , Naoya Horiguchi , Roman Gushchin , Ackerley Tng , Andrew Morton , peterx@redhat.com, Oscar Salvador Subject: [PATCH v2 3/7] mm/hugetlb: Rename avoid_reserve to cow_from_owner Date: Tue, 7 Jan 2025 15:39:58 -0500 Message-ID: <20250107204002.2683356-4-peterx@redhat.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250107204002.2683356-1-peterx@redhat.com> References: <20250107204002.2683356-1-peterx@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The old name "avoid_reserve" can be too generic and can be used wrongly in the new call sites that want to allocate a hugetlb folio. It's confusing on two things: (1) whether one can opt-in to avoid global reservation, and (2) whether it should take more than one count. In reality, this flag is only used in an extremely hacky path, in an extremely hacky way in hugetlb CoW path only, and always use with 1 saying "skip global reservation". Rename the flag to avoid future abuse of this flag, making it a boolean so as to reflect its true representation that it's not a counter. To make it even harder to abuse, add a comment above the function to explain it. Signed-off-by: Peter Xu --- fs/hugetlbfs/inode.c | 2 +- include/linux/hugetlb.h | 4 ++-- mm/hugetlb.c | 33 ++++++++++++++++++++------------- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index 62fb0cbc93ab..0fc179a59830 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -814,7 +814,7 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset, * folios in these areas, we need to consume the reserves * to keep reservation accounting consistent. */ - folio = alloc_hugetlb_folio(&pseudo_vma, addr, 0); + folio = alloc_hugetlb_folio(&pseudo_vma, addr, false); if (IS_ERR(folio)) { mutex_unlock(&hugetlb_fault_mutex_table[hash]); error = PTR_ERR(folio); diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 10faf42ca96a..49ec2362ce92 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -683,7 +683,7 @@ struct huge_bootmem_page { int isolate_or_dissolve_huge_page(struct page *page, struct list_head *list); int replace_free_hugepage_folios(unsigned long start_pfn, unsigned long end_pfn); struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, - unsigned long addr, int avoid_reserve); + unsigned long addr, bool cow_from_owner); struct folio *alloc_hugetlb_folio_nodemask(struct hstate *h, int preferred_nid, nodemask_t *nmask, gfp_t gfp_mask, bool allow_alloc_fallback); @@ -1068,7 +1068,7 @@ static inline int replace_free_hugepage_folios(unsigned long start_pfn, static inline struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, unsigned long addr, - int avoid_reserve) + bool cow_from_owner) { return NULL; } diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 7be8c35d2a83..cdbc8914a9f7 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3008,8 +3008,15 @@ int replace_free_hugepage_folios(unsigned long start_pfn, unsigned long end_pfn) return ret; } +/* + * NOTE! "cow_from_owner" represents a very hacky usage only used in CoW + * faults of hugetlb private mappings on top of a non-page-cache folio (in + * which case even if there's a private vma resv map it won't cover such + * allocation). New call sites should (probably) never set it to true!! + * When it's set, the allocation will bypass all vma level reservations. + */ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, - unsigned long addr, int avoid_reserve) + unsigned long addr, bool cow_from_owner) { struct hugepage_subpool *spool = subpool_vma(vma); struct hstate *h = hstate_vma(vma); @@ -3038,7 +3045,7 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, * Allocations for MAP_NORESERVE mappings also need to be * checked against any subpool limit. */ - if (map_chg || avoid_reserve) { + if (map_chg || cow_from_owner) { gbl_chg = hugepage_subpool_get_pages(spool, 1); if (gbl_chg < 0) goto out_end_reservation; @@ -3046,7 +3053,7 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, /* If this allocation is not consuming a reservation, charge it now. */ - deferred_reserve = map_chg || avoid_reserve; + deferred_reserve = map_chg || cow_from_owner; if (deferred_reserve) { ret = hugetlb_cgroup_charge_cgroup_rsvd( idx, pages_per_huge_page(h), &h_cg); @@ -3071,7 +3078,7 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, if (!folio) goto out_uncharge_cgroup; spin_lock_irq(&hugetlb_lock); - if (!avoid_reserve && vma_has_reserves(vma, gbl_chg)) { + if (!cow_from_owner && vma_has_reserves(vma, gbl_chg)) { folio_set_hugetlb_restore_reserve(folio); h->resv_huge_pages--; } @@ -3138,7 +3145,7 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, hugetlb_cgroup_uncharge_cgroup_rsvd(idx, pages_per_huge_page(h), h_cg); out_subpool_put: - if (map_chg || avoid_reserve) + if (map_chg || cow_from_owner) hugepage_subpool_put_pages(spool, 1); out_end_reservation: vma_end_reservation(h, vma, addr); @@ -5369,7 +5376,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, spin_unlock(src_ptl); spin_unlock(dst_ptl); /* Do not use reserve as it's private owned */ - new_folio = alloc_hugetlb_folio(dst_vma, addr, 0); + new_folio = alloc_hugetlb_folio(dst_vma, addr, false); if (IS_ERR(new_folio)) { folio_put(pte_folio); ret = PTR_ERR(new_folio); @@ -5823,7 +5830,7 @@ static vm_fault_t hugetlb_wp(struct folio *pagecache_folio, struct hstate *h = hstate_vma(vma); struct folio *old_folio; struct folio *new_folio; - int outside_reserve = 0; + bool cow_from_owner = 0; vm_fault_t ret = 0; struct mmu_notifier_range range; @@ -5886,7 +5893,7 @@ static vm_fault_t hugetlb_wp(struct folio *pagecache_folio, */ if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) && old_folio != pagecache_folio) - outside_reserve = 1; + cow_from_owner = true; folio_get(old_folio); @@ -5895,7 +5902,7 @@ static vm_fault_t hugetlb_wp(struct folio *pagecache_folio, * be acquired again before returning to the caller, as expected. */ spin_unlock(vmf->ptl); - new_folio = alloc_hugetlb_folio(vma, vmf->address, outside_reserve); + new_folio = alloc_hugetlb_folio(vma, vmf->address, cow_from_owner); if (IS_ERR(new_folio)) { /* @@ -5905,7 +5912,7 @@ static vm_fault_t hugetlb_wp(struct folio *pagecache_folio, * reliability, unmap the page from child processes. The child * may get SIGKILLed if it later faults. */ - if (outside_reserve) { + if (cow_from_owner) { struct address_space *mapping = vma->vm_file->f_mapping; pgoff_t idx; u32 hash; @@ -6156,7 +6163,7 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping, goto out; } - folio = alloc_hugetlb_folio(vma, vmf->address, 0); + folio = alloc_hugetlb_folio(vma, vmf->address, false); if (IS_ERR(folio)) { /* * Returning error will result in faulting task being @@ -6622,7 +6629,7 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte, goto out; } - folio = alloc_hugetlb_folio(dst_vma, dst_addr, 0); + folio = alloc_hugetlb_folio(dst_vma, dst_addr, false); if (IS_ERR(folio)) { ret = -ENOMEM; goto out; @@ -6664,7 +6671,7 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte, goto out; } - folio = alloc_hugetlb_folio(dst_vma, dst_addr, 0); + folio = alloc_hugetlb_folio(dst_vma, dst_addr, false); if (IS_ERR(folio)) { folio_put(*foliop); ret = -ENOMEM; -- 2.47.0