Web lists-archives.com

Re: set_bit() + down_write()




On Wed, Dec 06, 2017 at 01:48:17AM -0800, David Rientjes wrote:
> Hi Paul,
> 
> I have a question about whether this code in exit_mmap() could be racy:
> 
> 	set_bit(MMF_OOM_SKIP, &mm->flags);
> 	if (unlikely(tsk_is_oom_victim(current))) {
> 		/*
> 		 * Wait for oom_reap_task() to stop working on this
> 		 * mm. Because MMF_OOM_SKIP is already set before
> 		 * calling down_read(), oom_reap_task() will not run
> 		 * on this "mm" post up_write().
> 		 *
> 		 * tsk_is_oom_victim() cannot be set from under us
> 		 * either because current->mm is already set to NULL
> 		 * under task_lock before calling mmput and oom_mm is
> 		 * set not NULL by the OOM killer only if current->mm
> 		 * is found not NULL while holding the task_lock.
> 		 */
> 		down_write(&mm->mmap_sem);
> 		up_write(&mm->mmap_sem);
> 	}
> 
> This is supposed to serialize __oom_reap_task_mm() from operating on an mm 
> with MMF_OOM_SKIP set while under the protection of 
> down_read(&mm->mmap_sem).
> 
> Is it possible that MMF_OOM_SKIP above actually gets set after up_write()?
> 
> If so, that would explain why we see the oom reaper operating on mm's with 
> MMF_OOM_SKIP set.

Well, set_bit() has no ordering semantics, but up_write() does provide
some ordering, but is not fully ordered.  So it all depends on what
the other end is doing.

If the other end is this same task, then it will see things fully ordered.

If the other end holds ->mmap_sem, then it will see things fully ordered.

If the other end does not hold ->mmap_sem, things are more complicated,
and it depends on exactly what the other end is doing.  As an example
(not directly related to your example above), here is something that
would -not- be guaranteed to be ordered:

Task 0:
	...

	WRITE_ONCE(x, 1);
	WRITE_ONCE(y, 1);
	up_write(&mm->mmap_sem);

Task 1:

	down_write(&mm->mmap_sem);
	r1 = READ_ONCE(z);
	r2 = READ_ONCE(y);
	...

Task 2:

	WRITE_ONCE(z, 1);
	smp_mb();
	r3 = READ_ONCE(x);

It really is possible on some architectures to end up with r1==0,
r2==1, and r3==0.

But what exactly was the other end doing?  What architecture were you
running on?  And what version of Linux were you using?

							Thanx, Paul