From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from sg-1-101.ptr.blmpb.com (sg-1-101.ptr.blmpb.com [118.26.132.101]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 011F730FC3F for ; Mon, 29 Dec 2025 12:26:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=118.26.132.101 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767011177; cv=none; b=nYdZNaHjY53hvbLUu+L5UF6a8M2ynVm8WBUlTjg5UQm6tLFs+j/L72LPpFOIUxIemPRkdna5M2ciAaAYTkTeDpGuL7Rlk9iH0Ry3zpwKrJTUId815VKonI3P/LSpFlAgDaVTKUYUK2TB/bsc+f4m0pWVnvx0X5GQ2IYjvqeDOk8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767011177; c=relaxed/simple; bh=sgK8f3QNYfar1gezKOlCjhQ48pEwkkOULYvVWSbFjVg=; h=Date:Mime-Version:In-Reply-To:Content-Type:To:From:Cc:References: Subject:Message-Id; b=d9KlR/MJo8D5NUxW2jW1f+5udQfhFZOhWaBE0JFPp9LEUc/oWUYqr5oRJ0cD59yvkx99aTOk4dZIz+Bc58+POnfcpo0BOy+T5YNwn15pZcbDZ1bAyunCML8UTVDSKja0jHy9KwWuOcWp7D72ND/tDJCFtma1zRuQbfDVEB0u2lM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=DB4Mq7z8; arc=none smtp.client-ip=118.26.132.101 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="DB4Mq7z8" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=2212171451; d=bytedance.com; t=1767011161; h=from:subject: mime-version:from:date:message-id:subject:to:cc:reply-to:content-type: mime-version:in-reply-to:message-id; bh=+W6DGY7qIcPDVlcv8bt7iI6QjbF6AUeFe1xm8LEWDtY=; b=DB4Mq7z8+o8OzcpUo48XK0VHzln1/I+kYCEMhl5Se6Zb8wjKI9i61lvccRcqZMDQbwy4HF +ZK3SwIeOZYwKBJTLx1ZxpV9rqJGGb1BOyO8uOq2lJO2H940KRYh4vhGzg2yQoEqN9+k4M Kj69DktgMeSlLRhxUzJ5CNPO4g75D1WjNW3L7W9HKzh4zF/pUx6l3phFmYckEF8IbQpw2L 3F7npi3wVIo7PsnV8rFbekvDtKWXupvt15fgzr69sUZKcJn01nr7VLo0PxG1j9qpW0d115 frj2QQFzoUqUjXdAavfLNYjl1XMFGVE9L4kp/SVn0q4HAy/x2EExLYkXknCYIQ== Date: Mon, 29 Dec 2025 20:25:37 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=UTF-8 To: From: "Li Zhe" X-Original-From: Li Zhe Cc: , , , , , , Content-Transfer-Encoding: 7bit References: X-Lms-Return-Path: Subject: Re: [PATCH 4/8] mm/hugetlb: introduce per-node sysfs interface "zeroable_hugepages" Message-Id: <20251229122537.6903-1-lizhe.67@bytedance.com> X-Mailer: git-send-email 2.45.2 On Fri, 26 Dec 2025 10:51:01 -0800, fvdl@google.com wrote: > > +static ssize_t zeroable_hugepages_show(struct kobject *kobj, > > + struct kobj_attribute *attr, char *buf) > > +{ > > + struct hstate *h; > > + unsigned long free_huge_pages_zero; > > + int nid; > > + > > + h = kobj_to_hstate(kobj, &nid); > > + if (WARN_ON(nid == NUMA_NO_NODE)) > > + return -EPERM; > > + > > + free_huge_pages_zero = h->free_huge_pages_node[nid] - > > + h->free_huge_pages_zero_node[nid]; > > + > > + return sprintf(buf, "%lu\n", free_huge_pages_zero); > > +} > > + > > +static inline bool zero_should_abort(struct hstate *h, int nid) > > +{ > > + return (h->free_huge_pages_zero_node[nid] == > > + h->free_huge_pages_node[nid]) || > > + list_empty(&h->hugepage_freelists[nid]); > > +} > > + > > +static void zero_free_hugepages_nid(struct hstate *h, > > + int nid, unsigned int nr_zero) > > +{ > > + struct list_head *freelist = &h->hugepage_freelists[nid]; > > + unsigned int nr_zerod = 0; > > + struct folio *folio; > > + > > + if (zero_should_abort(h, nid)) > > + return; > > + > > + spin_lock_irq(&hugetlb_lock); > > + > > + while (nr_zerod < nr_zero) { > > + > > + if (zero_should_abort(h, nid) || fatal_signal_pending(current)) > > + break; > > + > > + freelist = freelist->prev; > > + if (unlikely(list_is_head(freelist, &h->hugepage_freelists[nid]))) > > + break; > > + folio = list_entry(freelist, struct folio, lru); > > + > > + if (folio_test_hugetlb_zeroed(folio) || > > + folio_test_hugetlb_zeroing(folio)) > > + continue; > > + > > + folio_set_hugetlb_zeroing(folio); > > + > > + /* > > + * Incrementing this here is a bit of a fib, since > > + * the page hasn't been cleared yet (it will be done > > + * immediately after dropping the lock below). But > > + * it keeps the count consistent with the overall > > + * free count in case the page gets taken off the > > + * freelist while we're working on it. > > + */ > > + h->free_huge_pages_zero_node[nid]++; > > + spin_unlock_irq(&hugetlb_lock); > > + > > + /* > > + * HWPoison pages may show up on the freelist. > > + * Don't try to zero it out, but do set the flag > > + * and counts, so that we don't consider it again. > > + */ > > + if (!folio_test_hwpoison(folio)) > > + folio_zero_user(folio, 0); > > + > > + cond_resched(); > > + > > + spin_lock_irq(&hugetlb_lock); > > + folio_set_hugetlb_zeroed(folio); > > + folio_clear_hugetlb_zeroing(folio); > > + > > + /* > > + * If the page is still on the free list, move > > + * it to the head. > > + */ > > + if (folio_test_hugetlb_freed(folio)) > > + list_move(&folio->lru, &h->hugepage_freelists[nid]); > > + > > + /* > > + * If someone was waiting for the zero to > > + * finish, wake them up. > > + */ > > + if (waitqueue_active(&h->dqzero_wait[nid])) > > + wake_up(&h->dqzero_wait[nid]); > > + nr_zerod++; > > + freelist = &h->hugepage_freelists[nid]; > > + } > > + spin_unlock_irq(&hugetlb_lock); > > +} > > Nit: s/nr_zerod/nr_zeroed/ Thank you for the reminder. I will address this issue in v2. > Feels like the list logic can be cleaned up a bit here. Since the > zeroed folios are at the head of the list, and the dirty ones at the > tail, and you start walking from the tail, you don't need to check if > you circled back to the head - just stop if you encounter a prezeroed > folio. If you encounter a prezeroed folio while walking from the tail, > that means that all other folios from that one to the head will also > be prezeroed already. Thank you for the thoughtful suggestion. Your line of reasoning is, in most situations, perfectly valid. Under extreme concurrency, however, a corner case can still appear. Imagine two processes simultaneously zeroing huge pages: Process A enters zero_free_hugepages_nid(), completes the zeroing of one huge page, and marks the folio in the list as pre-zeroed. Should Process B enter the same function moments later and decide to exit as soon as it meets a prezeroed folio, the intended parallel zeroing would quietly fall back to a single-threaded pace. Thanks, Zhe