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 329AF1F669F for ; Tue, 7 Jan 2025 20:40:17 +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=1736282420; cv=none; b=gx4+Jho1ZUhvM/NjIcYBBApDrFAmcu1YdZtHK8H8biwYzk14B5bUPdF7ftI/oIaa9AGf7GsWswaQRXlrNhlrXodPq4v/nLGG0VIXpMSK854Chc4HOl5RRGl+YAl6gF2TKK/2ktKHs7ea8DoqNWW8nbSTrIxU6KdX5TZAZEbutBA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736282420; c=relaxed/simple; bh=miJnVFlp+8lFYrNECV1q4UvY9H2N0nPtMDXK6Osx2j0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GXlVVqvPjmq/e8hayxPVW4oGzn3g9jYHQhLLDqoZgWdoMz/WFcUruL5Z3CQWMW4C7z51wcOBIXiCkdpV5Fufrg3x17c7NIrRmYKc8s1uURfaPEYwfkA5lOdymvICkmdVpjSH6XmdyBDj4fo671XCORZ4ddTHqdr0rL5ne0of2K4= 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=MqWiJpnR; 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="MqWiJpnR" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736282417; 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=FeRSwArockADk7XuGGq52WKujX3Jyjof3FZOrhLMA7I=; b=MqWiJpnRWgTE70hM91MbTq/w91PVMzfcAHnUpGhX610BLb0f7lJCKTjCDz9GrnFHj3szyj EgbEGtG1w/3t4nFe3Hw1txh9kXJUK5VdaelDQGBgV81k+IMT1BAIpANPxpi8KsWGXu9R3G Y4fbyROrqeuYua+I5tH6uABqF1IbPLI= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-437-maJF6XPaOfCarlWQw9x9Lw-1; Tue, 07 Jan 2025 15:40:16 -0500 X-MC-Unique: maJF6XPaOfCarlWQw9x9Lw-1 X-Mimecast-MFC-AGG-ID: maJF6XPaOfCarlWQw9x9Lw Received: by mail-qv1-f69.google.com with SMTP id 6a1803df08f44-6d8f0b1023bso179410986d6.0 for ; Tue, 07 Jan 2025 12:40:16 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736282415; x=1736887215; 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=FeRSwArockADk7XuGGq52WKujX3Jyjof3FZOrhLMA7I=; b=foggBA15z2A4CNCAcOKFaYVkKYpVbsWq7ds23LO33BbIrb2dr9WyoHS72ExWY2D51O 3Q7juVl+LahOQpTrxafKOOMmyEm5PUkBY9V9AH6z5yoOK8LjCO/l8Kuxp20o3ous0W3Q qZjX0j5AnwQFDwhEv9AHo/9kgU/5670MfjO4cSCgIP2Cl5nezUq/MMqVEVjWfVnxxqnt Y0/hM+/UmZWrgu2Qh5a1/rXUBqvVQvtpt1wo4PSgUiZiT9PBA16U+/+4xMRdNObgVm1Z w3reBQMWZOYvAXrmMCRg+6sCavxkXUksIlTx4DzLG/yBdIkR+uHQ/vPfJxCTNF8xE9G2 yfeA== X-Forwarded-Encrypted: i=1; AJvYcCXL9DTZcKf91DYDL9lvKdRiiJdDViyfwDDRmRq4nkXp3jbPkM8hjPnUZIiLfR2erDHairsb19W3yg0m7Jk=@vger.kernel.org X-Gm-Message-State: AOJu0YwykqTsYmtq5i6xbQLXWXf8tUOLlLBZPCRYa8IX6fbYup62dDcl BJlDMfEgK4B24DIQhAJm0iDmEXt8mOUyiyDFACuHCioLnqWIkenzvGYWZsJZKFxLjePCglAGJzB hkrFt3A6cfGMbTr7otCre+8rYxMPsI6yN7lbNlPjchn9CyfdRYYtyJPj3/kt3og== X-Gm-Gg: ASbGncuXwQ3V4ZAN4krAkA/K71VzBfbTUa30Ugw9bLvB/7kPqJwAHjPUYG16r3EzQmV /jptEHYYaVCb3y8WVaGXO8MDJ+smcknyz6cKDZ5LW8Cwdnffmq5csc8GV5uZ1Ls0BQqEQAzPuWY OSzieEb8jrzr+RTpa5aDZzRrJVTo4eh5abpYozpqzpgKjKZZMxPEkiJNZYU4pZ3NTCMjzvrU3aY PnGUHo0SzSYGvAiTV0bV7Uc9sCNM4KysuCNOuWjRcVx9QJCo9dNoUv3sNNHzsfyhzGhf7Wy3e5v er/WQkxSZuNeYNsVGQY2BVVToNG8uO54 X-Received: by 2002:a05:6214:434a:b0:6d8:cff9:f373 with SMTP id 6a1803df08f44-6df9b2d1a40mr7580956d6.30.1736282415440; Tue, 07 Jan 2025 12:40:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IE3MtP6+dWvdpGsDlM1wGqTR7s1w0fblu3/xx93mfYpEOm6U5pVW/zHRBSeKXgOBBACpJiRAA== X-Received: by 2002:a05:6214:434a:b0:6d8:cff9:f373 with SMTP id 6a1803df08f44-6df9b2d1a40mr7580606d6.30.1736282415027; Tue, 07 Jan 2025 12:40:15 -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.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jan 2025 12:40:13 -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 5/7] mm/hugetlb: Simplify vma_has_reserves() Date: Tue, 7 Jan 2025 15:40:00 -0500 Message-ID: <20250107204002.2683356-6-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 vma_has_reserves() is a helper "trying" to know whether the vma should consume one reservation when allocating the hugetlb folio. However it's not clear on why we need such complexity, as such information is already represented in the "chg" variable. >From alloc_hugetlb_folio() context, "chg" (or in the function's context, "gbl_chg") is defined as: - If gbl_chg=1, the allocation cannot reuse an existing reservation - If gbl_chg=0, the allocation should reuse an existing reservation Firstly, map_chg is defined as following, to cover all cases of hugetlb reservation scenarios (mostly, via vma_needs_reservation(), but cow_from_owner is an outlier): CONDITION HAS RESERVATION? ========= ================ - SHARED: always check against per-inode resv_map (ignore NONRESERVE) - If resv exists ==> YES [1] - If not ==> NO [2] - PRIVATE: complicated... - Request came from a CoW from owner resv map ==> NO [3] (when cow_from_owner==true) - If does not own a resv_map at all.. ==> NO [4] (examples: VM_NORESERVE, private fork()) - If owns a resv_map, but resv donsn't exists ==> NO [5] - If owns a resv_map, and resv exists ==> YES [6] Further on, gbl_chg considered spool setup, so that is a decision based on all the context. If we look at vma_has_reserves(), it almost does check that has already been processed by map_chg accounting (I marked each return value to the case above): static bool vma_has_reserves(struct vm_area_struct *vma, long chg) { if (vma->vm_flags & VM_NORESERVE) { if (vma->vm_flags & VM_MAYSHARE && chg == 0) return true; ==> [1] else return false; ==> [2] or [4] } if (vma->vm_flags & VM_MAYSHARE) { if (chg) return false; ==> [2] else return true; ==> [1] } if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) { if (chg) return false; ==> [5] else return true; ==> [6] } return false; ==> [4] } It didn't check [3], but [3] case was actually already covered now by the "chg" / "gbl_chg" / "map_chg" calculations. In short, vma_has_reserves() doesn't provide anything more than return "!chg".. so just simplify all the things. There're a lot of comments describing truncation races, IIUC there should have no race as long as map_chg is properly done. Signed-off-by: Peter Xu --- mm/hugetlb.c | 67 ++++++---------------------------------------------- 1 file changed, 7 insertions(+), 60 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index b8a849fe1531..5ec079f32f44 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -1247,66 +1247,13 @@ void clear_vma_resv_huge_pages(struct vm_area_struct *vma) } /* Returns true if the VMA has associated reserve pages */ -static bool vma_has_reserves(struct vm_area_struct *vma, long chg) +static bool vma_has_reserves(long chg) { - if (vma->vm_flags & VM_NORESERVE) { - /* - * This address is already reserved by other process(chg == 0), - * so, we should decrement reserved count. Without decrementing, - * reserve count remains after releasing inode, because this - * allocated page will go into page cache and is regarded as - * coming from reserved pool in releasing step. Currently, we - * don't have any other solution to deal with this situation - * properly, so add work-around here. - */ - if (vma->vm_flags & VM_MAYSHARE && chg == 0) - return true; - else - return false; - } - - /* Shared mappings always use reserves */ - if (vma->vm_flags & VM_MAYSHARE) { - /* - * We know VM_NORESERVE is not set. Therefore, there SHOULD - * be a region map for all pages. The only situation where - * there is no region map is if a hole was punched via - * fallocate. In this case, there really are no reserves to - * use. This situation is indicated if chg != 0. - */ - if (chg) - return false; - else - return true; - } - /* - * Only the process that called mmap() has reserves for - * private mappings. + * Now "chg" has all the conditions considered for whether we + * should use an existing reservation. */ - if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) { - /* - * Like the shared case above, a hole punch or truncate - * could have been performed on the private mapping. - * Examine the value of chg to determine if reserves - * actually exist or were previously consumed. - * Very Subtle - The value of chg comes from a previous - * call to vma_needs_reserves(). The reserve map for - * private mappings has different (opposite) semantics - * than that of shared mappings. vma_needs_reserves() - * has already taken this difference in semantics into - * account. Therefore, the meaning of chg is the same - * as in the shared case above. Code could easily be - * combined, but keeping it separate draws attention to - * subtle differences. - */ - if (chg) - return false; - else - return true; - } - - return false; + return chg == 0; } static void enqueue_hugetlb_folio(struct hstate *h, struct folio *folio) @@ -1407,7 +1354,7 @@ static struct folio *dequeue_hugetlb_folio_vma(struct hstate *h, * have no page reserves. This check ensures that reservations are * not "stolen". The child may still get SIGKILLed */ - if (!vma_has_reserves(vma, chg) && !available_huge_pages(h)) + if (!vma_has_reserves(chg) && !available_huge_pages(h)) goto err; gfp_mask = htlb_alloc_mask(h); @@ -1425,7 +1372,7 @@ static struct folio *dequeue_hugetlb_folio_vma(struct hstate *h, folio = dequeue_hugetlb_folio_nodemask(h, gfp_mask, nid, nodemask); - if (folio && vma_has_reserves(vma, chg)) { + if (folio && vma_has_reserves(chg)) { folio_set_hugetlb_restore_reserve(folio); h->resv_huge_pages--; } @@ -3116,7 +3063,7 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, if (!folio) goto out_uncharge_cgroup; spin_lock_irq(&hugetlb_lock); - if (vma_has_reserves(vma, gbl_chg)) { + if (vma_has_reserves(gbl_chg)) { folio_set_hugetlb_restore_reserve(folio); h->resv_huge_pages--; } -- 2.47.0