All of lore.kernel.org
 help / color / mirror / Atom feed
From: Oleg Nesterov <oleg@redhat.com>
To: Jason Wang <jasowang@redhat.com>
Cc: axboe@kernel.dk, brauner@kernel.org, mst@redhat.com,
	linux@leemhuis.info, linux-kernel@vger.kernel.org,
	ebiederm@xmission.com, stefanha@redhat.com,
	nicolas.dichtel@6wind.com,
	virtualization@lists.linux-foundation.org,
	torvalds@linux-foundation.org
Subject: Re: [PATCH 3/3] fork, vhost: Use CLONE_THREAD to fix freezer/ps regression
Date: Thu, 1 Jun 2023 09:43:16 +0200	[thread overview]
Message-ID: <20230601074315.GA13133@redhat.com> (raw)
In-Reply-To: <CACGkMEvNrC5gc4ppp0QG-SNSbs_snrqwPkNBotffRRDJA1VJjQ@mail.gmail.com>

On 06/01, Jason Wang wrote:
>
> On Wed, May 31, 2023 at 5:14 PM Oleg Nesterov <oleg@redhat.com> wrote:
> >
> > > > I don't understand you. OK, to simplify, suppose we have 2 global vars
> > > >
> > > >         void *PTR = something_non_null;
> > > >         unsigned long FLAGS = -1ul;
> > > >
> > > > Now I think this code
> > > >
> > > >         CPU_0                           CPU_1
> > > >
> > > >         void *ptr = PTR;                if (!test_and_set_bit(0, FLAGS))
> > > >         clear_bit(0, FLAGS);                    PTR = NULL;
> > > >         BUG_ON(!ptr);
> > > >
> > > > is racy and can hit the BUG_ON(!ptr).
> > >
> > > This seems different to the above case?
> >
> > not sure,
> >
> > > And you can hit BUG_ON with
> > > the following execution sequence:
> > >
> > > [cpu 0] clear_bit(0, FLAGS);
> > > [cpu 1] if (!test_and_set_bit(0, FLAGS))
> > > [cpu 1] PTR = NULL;
> > > [cpu 0] BUG_ON(!ptr)
> >
> > I don't understand this part... yes, we can hit this BUG_ON() without mb in
> > between, this is what I tried to say.
>
> I may miss something,

Or me... note that CPU_0 loads the global "PTR" into the local "ptr" before clear_bit.
Since you have mentioned the program order: yes this lacks READ_ONCE() or barrier(),
but the same is true for the code in vhost_worker(). So I still don't understand.

> but the above is the sequence that is executed
> by the processor (for each CPU, it's just the program order). So where
> do you expect to place an mb can help?

before clear_bit:

	CPU_0

	void *ptr = PTR;
	mb();			// implies compiler barrier as well
	clear_bit(0, FLAGS);
	BUG_ON(!ptr);

just in case... mb() in the code above is only for illustration, we can use
smp_mb__before_atomic() + clear_bit(). Or just clear_bit_unlock(), iiuc the
one-way barrier is fine in this case.


> > > In vhost code, there's a condition before the clear_bit() which sits
> > > inside llist_for_each_entry_safe():
> > >
> > > #define llist_for_each_entry_safe(pos, n, node, member)                        \
> > >         for (pos = llist_entry((node), typeof(*pos), member);                  \
> > >              member_address_is_nonnull(pos, member) &&                         \
> > >                 (n = llist_entry(pos->member.next, typeof(*n), member), true); \
> > >              pos = n)
> > >
> > > The clear_bit() is a store which is not speculated, so there's a
> > > control dependency, the store can't be executed until the condition
> > > expression is evaluated which requires pos->member.next
> > > (work->node.next) to be loaded.
> >
> > But llist_for_each_entry_safe() doesn't check "n", I mean, it is not that we have
> > something like
> >
> >         n = llist_entry(...);
> >         if (n)
> >                 clear_bit(...);
> >
> > so I do not see how can we rely on the load-store control dependency.
>
> Just to make sure we are on the same page, the condition expression is
>
> member_address_is_nonnull(pos, member) && (n =
> llist_entry(pos->member.next, typeof(*n), member), true)
>
> So it's something like:
>
> if (work->node && (work_next = work->node->next, true))
>     clear_bit(&work->flags);
>
> So two loads from both work->node and work->node->next, and there's a
> store which is clear_bit, then it's a load-store control dependencies?

I guess you missed the comma expression... Let me rewrite your pseudo-code
above, it is equivalent to

	if (work->node) {
		if ((work_next = work->node->next, true))
			clear_bit(&work->flags);
	}

another rewrite:
	
	if (work->node) {
		work_next = work->node->next;
		if ((work, true))
			clear_bit(&work->flags);
	}

and the final rewrite:

	if (work->node) {
		work_next = work->node->next;
		if (true)
			clear_bit(&work->flags);
	}

so again, I do not see the load-store control dependency. Not to mention this
code lacks READ_ONCE().


If we had something like

	if (work->node) {
		work_next = READ_ONCE(work->node->next);
		if (work_next)
			clear_bit(&work->flags);
	}

instead, then yes, we could rely on the LOAD-STORE dependency.

Oleg.

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

WARNING: multiple messages have this Message-ID (diff)
From: Oleg Nesterov <oleg@redhat.com>
To: Jason Wang <jasowang@redhat.com>
Cc: Mike Christie <michael.christie@oracle.com>,
	linux@leemhuis.info, nicolas.dichtel@6wind.com, axboe@kernel.dk,
	ebiederm@xmission.com, torvalds@linux-foundation.org,
	linux-kernel@vger.kernel.org,
	virtualization@lists.linux-foundation.org, mst@redhat.com,
	sgarzare@redhat.com, stefanha@redhat.com, brauner@kernel.org
Subject: Re: [PATCH 3/3] fork, vhost: Use CLONE_THREAD to fix freezer/ps regression
Date: Thu, 1 Jun 2023 09:43:16 +0200	[thread overview]
Message-ID: <20230601074315.GA13133@redhat.com> (raw)
In-Reply-To: <CACGkMEvNrC5gc4ppp0QG-SNSbs_snrqwPkNBotffRRDJA1VJjQ@mail.gmail.com>

On 06/01, Jason Wang wrote:
>
> On Wed, May 31, 2023 at 5:14 PM Oleg Nesterov <oleg@redhat.com> wrote:
> >
> > > > I don't understand you. OK, to simplify, suppose we have 2 global vars
> > > >
> > > >         void *PTR = something_non_null;
> > > >         unsigned long FLAGS = -1ul;
> > > >
> > > > Now I think this code
> > > >
> > > >         CPU_0                           CPU_1
> > > >
> > > >         void *ptr = PTR;                if (!test_and_set_bit(0, FLAGS))
> > > >         clear_bit(0, FLAGS);                    PTR = NULL;
> > > >         BUG_ON(!ptr);
> > > >
> > > > is racy and can hit the BUG_ON(!ptr).
> > >
> > > This seems different to the above case?
> >
> > not sure,
> >
> > > And you can hit BUG_ON with
> > > the following execution sequence:
> > >
> > > [cpu 0] clear_bit(0, FLAGS);
> > > [cpu 1] if (!test_and_set_bit(0, FLAGS))
> > > [cpu 1] PTR = NULL;
> > > [cpu 0] BUG_ON(!ptr)
> >
> > I don't understand this part... yes, we can hit this BUG_ON() without mb in
> > between, this is what I tried to say.
>
> I may miss something,

Or me... note that CPU_0 loads the global "PTR" into the local "ptr" before clear_bit.
Since you have mentioned the program order: yes this lacks READ_ONCE() or barrier(),
but the same is true for the code in vhost_worker(). So I still don't understand.

> but the above is the sequence that is executed
> by the processor (for each CPU, it's just the program order). So where
> do you expect to place an mb can help?

before clear_bit:

	CPU_0

	void *ptr = PTR;
	mb();			// implies compiler barrier as well
	clear_bit(0, FLAGS);
	BUG_ON(!ptr);

just in case... mb() in the code above is only for illustration, we can use
smp_mb__before_atomic() + clear_bit(). Or just clear_bit_unlock(), iiuc the
one-way barrier is fine in this case.


> > > In vhost code, there's a condition before the clear_bit() which sits
> > > inside llist_for_each_entry_safe():
> > >
> > > #define llist_for_each_entry_safe(pos, n, node, member)                        \
> > >         for (pos = llist_entry((node), typeof(*pos), member);                  \
> > >              member_address_is_nonnull(pos, member) &&                         \
> > >                 (n = llist_entry(pos->member.next, typeof(*n), member), true); \
> > >              pos = n)
> > >
> > > The clear_bit() is a store which is not speculated, so there's a
> > > control dependency, the store can't be executed until the condition
> > > expression is evaluated which requires pos->member.next
> > > (work->node.next) to be loaded.
> >
> > But llist_for_each_entry_safe() doesn't check "n", I mean, it is not that we have
> > something like
> >
> >         n = llist_entry(...);
> >         if (n)
> >                 clear_bit(...);
> >
> > so I do not see how can we rely on the load-store control dependency.
>
> Just to make sure we are on the same page, the condition expression is
>
> member_address_is_nonnull(pos, member) && (n =
> llist_entry(pos->member.next, typeof(*n), member), true)
>
> So it's something like:
>
> if (work->node && (work_next = work->node->next, true))
>     clear_bit(&work->flags);
>
> So two loads from both work->node and work->node->next, and there's a
> store which is clear_bit, then it's a load-store control dependencies?

I guess you missed the comma expression... Let me rewrite your pseudo-code
above, it is equivalent to

	if (work->node) {
		if ((work_next = work->node->next, true))
			clear_bit(&work->flags);
	}

another rewrite:
	
	if (work->node) {
		work_next = work->node->next;
		if ((work, true))
			clear_bit(&work->flags);
	}

and the final rewrite:

	if (work->node) {
		work_next = work->node->next;
		if (true)
			clear_bit(&work->flags);
	}

so again, I do not see the load-store control dependency. Not to mention this
code lacks READ_ONCE().


If we had something like

	if (work->node) {
		work_next = READ_ONCE(work->node->next);
		if (work_next)
			clear_bit(&work->flags);
	}

instead, then yes, we could rely on the LOAD-STORE dependency.

Oleg.


  reply	other threads:[~2023-06-01  7:43 UTC|newest]

Thread overview: 103+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-22  2:51 [PATCH 0/3] vhost: Fix freezer/ps regressions Mike Christie
2023-05-22  2:51 ` Mike Christie
2023-05-22  2:51 ` [PATCH 1/3] signal: Don't always put SIGKILL in shared_pending Mike Christie
2023-05-22  2:51   ` Mike Christie
2023-05-23 15:30   ` Eric W. Biederman
2023-05-23 15:30     ` Eric W. Biederman
2023-05-22  2:51 ` [PATCH 2/3] signal: Don't exit for PF_USER_WORKER tasks Mike Christie
2023-05-22  2:51   ` Mike Christie
2023-05-22  2:51 ` [PATCH 3/3] fork, vhost: Use CLONE_THREAD to fix freezer/ps regression Mike Christie
2023-05-22  2:51   ` Mike Christie
2023-05-22 12:30   ` Oleg Nesterov
2023-05-22 12:30     ` Oleg Nesterov
2023-05-22 17:00     ` Mike Christie
2023-05-22 17:00       ` Mike Christie
2023-05-22 17:47       ` Oleg Nesterov
2023-05-22 17:47         ` Oleg Nesterov
2023-05-23 12:15         ` Oleg Nesterov
2023-05-23 12:15           ` Oleg Nesterov
2023-05-23 15:57           ` Eric W. Biederman
2023-05-23 15:57             ` Eric W. Biederman
2023-05-24 14:10             ` Oleg Nesterov
2023-05-24 14:10               ` Oleg Nesterov
2023-05-24 14:44               ` Eric W. Biederman
2023-05-24 14:44                 ` Eric W. Biederman
2023-05-25 11:55                 ` Oleg Nesterov
2023-05-25 11:55                   ` Oleg Nesterov
2023-05-25 15:30                   ` Eric W. Biederman
2023-05-25 15:30                     ` Eric W. Biederman
2023-05-25 16:20                     ` Linus Torvalds
2023-05-25 16:20                       ` Linus Torvalds
2023-05-27  9:49                       ` Eric W. Biederman
2023-05-27  9:49                         ` Eric W. Biederman
2023-05-27 16:12                         ` Linus Torvalds
2023-05-27 16:12                           ` Linus Torvalds
2023-05-28  1:17                           ` Eric W. Biederman
2023-05-28  1:17                             ` Eric W. Biederman
2023-05-28  1:21                             ` Linus Torvalds
2023-05-28  1:21                               ` Linus Torvalds
2023-05-29 11:19                             ` Oleg Nesterov
2023-05-29 11:19                               ` Oleg Nesterov
2023-05-29 16:09                               ` michael.christie
2023-05-29 16:09                                 ` michael.christie
2023-05-29 17:46                                 ` Oleg Nesterov
2023-05-29 17:46                                   ` Oleg Nesterov
2023-05-29 17:54                                   ` Oleg Nesterov
2023-05-29 17:54                                     ` Oleg Nesterov
2023-05-29 19:03                                     ` Mike Christie
2023-05-29 19:03                                       ` Mike Christie
2023-05-29 19:35                                   ` Mike Christie
2023-05-29 19:35                                     ` Mike Christie
2023-05-29 19:46                                     ` michael.christie
2023-05-29 19:46                                       ` michael.christie
2023-05-30  2:48                                       ` Eric W. Biederman
2023-05-30  2:48                                         ` Eric W. Biederman
2023-05-30  2:38                                 ` Eric W. Biederman
2023-05-30  2:38                                   ` Eric W. Biederman
2023-05-30 15:34                                   ` Mike Christie
2023-05-30 15:34                                     ` Mike Christie
2023-05-31  3:30                                   ` Mike Christie
2023-05-31  3:30                                     ` Mike Christie
2023-05-29 16:11                               ` michael.christie
2023-05-29 16:11                                 ` michael.christie
2023-05-30 14:15                               ` Christian Brauner
2023-05-30 17:55                                 ` Oleg Nesterov
2023-05-30 17:55                                   ` Oleg Nesterov
2023-05-30 15:01                         ` Eric W. Biederman
2023-05-30 15:01                           ` Eric W. Biederman
2023-05-31  5:22             ` Jason Wang
2023-05-31  5:22               ` Jason Wang
2023-05-24  0:02           ` Mike Christie
2023-05-24  0:02             ` Mike Christie
2023-05-25 16:15           ` Mike Christie
2023-05-25 16:15             ` Mike Christie
2023-05-28  1:41             ` Eric W. Biederman
2023-05-28  1:41               ` Eric W. Biederman
2023-05-28 19:29               ` Mike Christie
2023-05-28 19:29                 ` Mike Christie
2023-05-31  5:22           ` Jason Wang
2023-05-31  5:22             ` Jason Wang
2023-05-31  7:25             ` Oleg Nesterov
2023-05-31  7:25               ` Oleg Nesterov
2023-05-31  8:17               ` Jason Wang
2023-05-31  8:17                 ` Jason Wang
2023-05-31  9:14                 ` Oleg Nesterov
2023-05-31  9:14                   ` Oleg Nesterov
2023-06-01  2:44                   ` Jason Wang
2023-06-01  2:44                     ` Jason Wang
2023-06-01  7:43                     ` Oleg Nesterov [this message]
2023-06-01  7:43                       ` Oleg Nesterov
2023-06-02  5:03                       ` Jason Wang
2023-06-02  5:03                         ` Jason Wang
2023-06-02 17:58                         ` Oleg Nesterov
2023-06-02 17:58                           ` Oleg Nesterov
2023-06-02 20:07                           ` Linus Torvalds
2023-06-02 20:07                             ` Linus Torvalds
2023-06-05 14:20                             ` Oleg Nesterov
2023-06-05 14:20                               ` Oleg Nesterov
2023-05-22 19:40   ` Michael S. Tsirkin
2023-05-22 19:40     ` Michael S. Tsirkin
2023-05-23 15:39     ` Eric W. Biederman
2023-05-23 15:39       ` Eric W. Biederman
2023-05-23 15:48     ` Mike Christie
2023-05-23 15:48       ` Mike Christie

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230601074315.GA13133@redhat.com \
    --to=oleg@redhat.com \
    --cc=axboe@kernel.dk \
    --cc=brauner@kernel.org \
    --cc=ebiederm@xmission.com \
    --cc=jasowang@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@leemhuis.info \
    --cc=mst@redhat.com \
    --cc=nicolas.dichtel@6wind.com \
    --cc=stefanha@redhat.com \
    --cc=torvalds@linux-foundation.org \
    --cc=virtualization@lists.linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.