From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-189.mta0.migadu.com (out-189.mta0.migadu.com [91.218.175.189]) (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 5197218892D for ; Tue, 7 Jan 2025 08:41:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.189 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736239309; cv=none; b=cRs9KccrtcjSG2fqgnKK12AAZkWM44Jt9hDypWRghxeE21bOhn4vOlyoXPAjp8jmewIW6IEhBmz8JlpPqfycsbip5QTCD3KoIDWrVR3KRH+fDQRsgi5KaCXaPpx3qj+F5Hf61th9Dmqp6rqOlHwwZLrCJJGCTPu0rprwuRYfn/4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736239309; c=relaxed/simple; bh=0ootUSWNnYNgqzEBtfwmW/RF7XHSEBYsy4Qyub2Xm4k=; h=Content-Type:Mime-Version:Subject:From:In-Reply-To:Date:Cc: Message-Id:References:To; b=VWOUjiF6BcwnbGMWT00DpIzSPf23/KlLTP4rD7BguiquUVwr9wCY1aCpe97Ov5a0XnvY6pi6+44/E9fMqyZvWn/T7QQVNssRGkGLyoUM2Dbto6wBlmYu8uKR+UyiK7k+xG6fNjGRinfa2gte2TZWdK4FI+TniRGAq6E687sb1Y8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=ocL3Jk21; arc=none smtp.client-ip=91.218.175.189 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="ocL3Jk21" Content-Type: text/plain; charset=us-ascii DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1736239304; 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; bh=Ws7s9ot9g9RMuZ5YQDWC51kDruEfARKXBMGZD+KjBBg=; b=ocL3Jk21HZpGt/qPSjFcXLVZc+5NvDtg3tKYtuV36L3xThRy2w4auDzI/WeWnnDSJtRqL+ 7QAHpP5tFfSbC7ZHjpnQ8zTzd93lgIyZn2LrAbHlpzJq1nJPZRRoye0VyLrizsYXcuG+Lg b6X18YnGkHvgV8tcRT+Lq8p/ABNkMKM= Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.200.121\)) Subject: Re: [PATCH mm-unstable v1] mm/hugetlb_vmemmap: fix memory loads ordering X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Muchun Song In-Reply-To: <20250107043505.351925-1-yuzhao@google.com> Date: Tue, 7 Jan 2025 16:41:03 +0800 Cc: Andrew Morton , David Hildenbrand , Mateusz Guzik , "Matthew Wilcox (Oracle)" , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Will Deacon Content-Transfer-Encoding: quoted-printable Message-Id: References: <20250107043505.351925-1-yuzhao@google.com> To: Yu Zhao X-Migadu-Flow: FLOW_OUT > On Jan 7, 2025, at 12:35, Yu Zhao wrote: >=20 > Using x86_64 as an example, for a 32KB struct page[] area describing a > 2MB hugeTLB, HVO reduces the area to 4KB by the following steps: > 1. Split the (r/w vmemmap) PMD mapping the area into 512 (r/w) PTEs; > 2. For the 8 PTEs mapping the area, remap PTE 1-7 to the page mapped > by PTE 0, and at the same time change the permission from r/w to > r/o; > 3. Free the pages PTE 1-7 used to map, hence the reduction from 32KB > to 4KB. >=20 > However, the following race can happen due to improperly memory loads > ordering: > CPU 1 (HVO) CPU 2 (speculative PFN walker) >=20 > page_ref_freeze() > synchronize_rcu() > rcu_read_lock() > page_is_fake_head() is false > vmemmap_remap_pte() > XXX: struct page[] becomes r/o >=20 > page_ref_unfreeze() > page_ref_count() is not zero >=20 > atomic_add_unless(&page->_refcount) > XXX: try to modify r/o struct page[] >=20 > Specifically, page_is_fake_head() must be ordered after > page_ref_count() on CPU 2 so that it can only return true for this > case, to avoid the later attempt to modify r/o struct page[]. >=20 > This patch adds the missing memory barrier and makes the tests on > page_is_fake_head() and page_ref_count() done in the proper order. >=20 > Fixes: bd225530a4c7 ("mm/hugetlb_vmemmap: fix race with speculative = PFN walkers") > Reported-by: Will Deacon > Closes: = https://lore.kernel.org/20241128142028.GA3506@willie-the-truck/ > Signed-off-by: Yu Zhao > --- > include/linux/page-flags.h | 2 +- > include/linux/page_ref.h | 8 ++++++-- > 2 files changed, 7 insertions(+), 3 deletions(-) >=20 > diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h > index 691506bdf2c5..6b8ecf86f1b6 100644 > --- a/include/linux/page-flags.h > +++ b/include/linux/page-flags.h > @@ -212,7 +212,7 @@ static __always_inline const struct page = *page_fixed_fake_head(const struct page > * cold cacheline in some cases. > */ > if (IS_ALIGNED((unsigned long)page, PAGE_SIZE) && > - test_bit(PG_head, &page->flags)) { > + test_bit_acquire(PG_head, &page->flags)) { > /* > * We can safely access the field of the @page[1] with = PG_head > * because the @page is a compound page composed with at = least > diff --git a/include/linux/page_ref.h b/include/linux/page_ref.h > index 8c236c651d1d..5becea98bd79 100644 > --- a/include/linux/page_ref.h > +++ b/include/linux/page_ref.h > @@ -233,8 +233,12 @@ static inline bool page_ref_add_unless(struct = page *page, int nr, int u) > bool ret =3D false; >=20 > rcu_read_lock(); > - /* avoid writing to the vmemmap area being remapped */ > - if (!page_is_fake_head(page) && page_ref_count(page) !=3D u) > + /* > + * To avoid writing to the vmemmap area remapped into r/o in = parallel, > + * the page_ref_count() test must precede the = page_is_fake_head() test > + * so that test_bit_acquire() in the latter is ordered after the = former. > + */ > + if (page_ref_count(page) !=3D u && !page_is_fake_head(page)) IIUC, we need to insert a memory barrier between page_ref_count() and = page_is_fake_head(). Specifically, accessing between page->_refcount and page->flags. So we = should insert a read memory barrier here, right? But I saw you added an acquire barrier = in page_fixed_fake_head(), I don't understand why an acquire barrier could stop the CPU reordering = the accessing between them. What am I missing here? Muchun, Thanks. > ret =3D atomic_add_unless(&page->_refcount, nr, u); > rcu_read_unlock(); >=20 > --=20 > 2.47.1.613.gc27f4b7a9f-goog >=20