On Tue, May 24, 2022 at 12:48:31PM -0300, Jason Gunthorpe wrote: > On Tue, May 24, 2022 at 08:43:27AM -0700, Minchan Kim wrote: > > On Tue, May 24, 2022 at 11:19:37AM -0300, Jason Gunthorpe wrote: > > > On Mon, May 23, 2022 at 10:16:58PM -0700, Minchan Kim wrote: > > > > On Mon, May 23, 2022 at 07:55:25PM -0700, John Hubbard wrote: > > > > > On 5/23/22 09:33, Minchan Kim wrote: > > > > > ... > > > > > > > So then: > > > > > > > > > > > > > > diff --git a/mm/page_alloc.c b/mm/page_alloc.c > > > > > > > index 0e42038382c1..b404f87e2682 100644 > > > > > > > +++ b/mm/page_alloc.c > > > > > > > @@ -482,7 +482,12 @@ unsigned long __get_pfnblock_flags_mask(const struct page *page, > > > > > > > word_bitidx = bitidx / BITS_PER_LONG; > > > > > > > bitidx &= (BITS_PER_LONG-1); > > > > > > > > > > > > > > - word = bitmap[word_bitidx]; > > > > > > > + /* > > > > > > > + * This races, without locks, with set_pageblock_migratetype(). Ensure > > > > > > set_pfnblock_flags_mask would be better? > > > > > > > + * a consistent (non-tearing) read of the memory array, so that results, > > > > > > > > > > > > Thanks for proceeding and suggestion, John. > > > > > > > > > > > > IIUC, the load tearing wouldn't be an issue since [1] fixed the issue. > > > > > > > > > > Did it? [1] fixed something, but I'm not sure we can claim that that > > > > > code is now safe against tearing in all possible cases, especially given > > > > > the recent discussion here. Specifically, having this code do a read, > > > > > then follow that up with calculations, seems correct. Anything else is > > > > > > > > The load tearing you are trying to explain in the comment would be > > > > solved by [1] since the bits will always align on a word and accessing > > > > word size based on word aligned address is always atomic so there is > > > > no load tearing problem IIUC. > > > > > > That is not technically true. It is exactly the sort of thing > > > READ_ONCE is intended to guard against. > > > > Oh, does word access based on the aligned address still happen > > load tearing? > > > > I just referred to > > https://elixir.bootlin.com/linux/latest/source/Documentation/memory-barriers.txt#L1759 > > I read that as saying load tearing is technically allowed but doesn't > happen in gcc, and so must use the _ONCE macros. This is in fact the intent, except... And as that passage goes on to state, there really are compilers (such as GCC) that tear stores of constants to machine aligned/sized locations. In short, use of the _ONCE() macros can save you a lot of pain. > > I didn't say it doesn't refetch the value without the READ_ONCE. > > > > What I am saying is READ_ONCE(bitmap_word_bitidx] prevents "refetching" > > issue rather than "tearing" issue in specific __get_pfnblock_flags_mask > > context because I though there is no load-tearing issue there since > > bitmap is word-aligned/accessed. No? > > It does both. AFAIK our memory model has no guarentees on what naked C > statements will do. Tearing, multi-load, etc - it is all technically > permitted. Use the proper accessors. I am with Jason on this one. In fact, I believe that any naked C-language access to mutable shared variables should have a comment stating why the compiler cannot mangle that access. Thanx, Paul