All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Vineet Gupta <Vineet.Gupta1@synopsys.com>
Cc: "linux-arch@vger.kernel.org" <linux-arch@vger.kernel.org>,
	"linux-snps-arc@lists.infradead.org"
	<linux-snps-arc@lists.infradead.org>,
	Alexey Brodkin <Alexey.Brodkin@synopsys.com>,
	lkml <linux-kernel@vger.kernel.org>
Subject: Re: arc_usr_cmpxchg and preemption
Date: Wed, 14 Mar 2018 18:53:52 +0100	[thread overview]
Message-ID: <20180314175352.GP4064@hirez.programming.kicks-ass.net> (raw)
In-Reply-To: <fdf30e76-f75d-9dda-26ca-ac011c41ee64@synopsys.com>

On Wed, Mar 14, 2018 at 09:58:19AM -0700, Vineet Gupta wrote:

> Well it is broken wrt the semantics the syscall is supposed to provide.
> Preemption disabling is what prevents a concurrent thread from coming in and
> modifying the same location (Imagine a variable which is being cmpxchg
> concurrently by 2 threads).
> 
> One approach is to do it the MIPS way, emulate the llsc flag - set it under
> preemption disabled section and clear it in switch_to

*shudder*... just catch the -EFAULT, force the write fault and retry.

Something like:

int sys_cmpxchg(u32 __user *user_ptr, u32 old, u32 new)
{
	u32 val;
	int ret;

again:
	ret = 0;

	preempt_disable();
	val = get_user(user_ptr);
	if (val == old)
		ret = put_user(new, user_ptr);
	preempt_enable();

	if (ret == -EFAULT) {
		struct page *page;
		ret = get_user_pages_fast((unsigned long)user_ptr, 1, 1, &page);
		if (ret < 0)
			return ret;
		put_page(page);
		goto again;
	}

	return ret;
}

WARNING: multiple messages have this Message-ID (diff)
From: Peter Zijlstra <peterz@infradead.org>
To: Vineet Gupta <Vineet.Gupta1@synopsys.com>
Cc: Alexey Brodkin <Alexey.Brodkin@synopsys.com>,
	"linux-snps-arc@lists.infradead.org"
	<linux-snps-arc@lists.infradead.org>,
	lkml <linux-kernel@vger.kernel.org>,
	"linux-arch@vger.kernel.org" <linux-arch@vger.kernel.org>
Subject: Re: arc_usr_cmpxchg and preemption
Date: Wed, 14 Mar 2018 18:53:52 +0100	[thread overview]
Message-ID: <20180314175352.GP4064@hirez.programming.kicks-ass.net> (raw)
Message-ID: <20180314175352.5Qu8bmTIFKvKsES5MaBqyRXAkPNXMLGnaZxADscUCq0@z> (raw)
In-Reply-To: <fdf30e76-f75d-9dda-26ca-ac011c41ee64@synopsys.com>

On Wed, Mar 14, 2018 at 09:58:19AM -0700, Vineet Gupta wrote:

> Well it is broken wrt the semantics the syscall is supposed to provide.
> Preemption disabling is what prevents a concurrent thread from coming in and
> modifying the same location (Imagine a variable which is being cmpxchg
> concurrently by 2 threads).
> 
> One approach is to do it the MIPS way, emulate the llsc flag - set it under
> preemption disabled section and clear it in switch_to

*shudder*... just catch the -EFAULT, force the write fault and retry.

Something like:

int sys_cmpxchg(u32 __user *user_ptr, u32 old, u32 new)
{
	u32 val;
	int ret;

again:
	ret = 0;

	preempt_disable();
	val = get_user(user_ptr);
	if (val == old)
		ret = put_user(new, user_ptr);
	preempt_enable();

	if (ret == -EFAULT) {
		struct page *page;
		ret = get_user_pages_fast((unsigned long)user_ptr, 1, 1, &page);
		if (ret < 0)
			return ret;
		put_page(page);
		goto again;
	}

	return ret;
}

WARNING: multiple messages have this Message-ID (diff)
From: peterz@infradead.org (Peter Zijlstra)
To: linux-snps-arc@lists.infradead.org
Subject: arc_usr_cmpxchg and preemption
Date: Wed, 14 Mar 2018 18:53:52 +0100	[thread overview]
Message-ID: <20180314175352.GP4064@hirez.programming.kicks-ass.net> (raw)
In-Reply-To: <fdf30e76-f75d-9dda-26ca-ac011c41ee64@synopsys.com>

On Wed, Mar 14, 2018@09:58:19AM -0700, Vineet Gupta wrote:

> Well it is broken wrt the semantics the syscall is supposed to provide.
> Preemption disabling is what prevents a concurrent thread from coming in and
> modifying the same location (Imagine a variable which is being cmpxchg
> concurrently by 2 threads).
> 
> One approach is to do it the MIPS way, emulate the llsc flag - set it under
> preemption disabled section and clear it in switch_to

*shudder*... just catch the -EFAULT, force the write fault and retry.

Something like:

int sys_cmpxchg(u32 __user *user_ptr, u32 old, u32 new)
{
	u32 val;
	int ret;

again:
	ret = 0;

	preempt_disable();
	val = get_user(user_ptr);
	if (val == old)
		ret = put_user(new, user_ptr);
	preempt_enable();

	if (ret == -EFAULT) {
		struct page *page;
		ret = get_user_pages_fast((unsigned long)user_ptr, 1, 1, &page);
		if (ret < 0)
			return ret;
		put_page(page);
		goto again;
	}

	return ret;
}

  reply	other threads:[~2018-03-14 17:53 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-14 16:36 arc_usr_cmpxchg and preemption Alexey Brodkin
2018-03-14 16:58 ` Vineet Gupta
2018-03-14 16:58   ` Vineet Gupta
2018-03-14 17:53   ` Peter Zijlstra [this message]
2018-03-14 17:53     ` Peter Zijlstra
2018-03-14 17:53     ` Peter Zijlstra
2018-03-14 18:20     ` Peter Zijlstra
2018-03-14 18:20       ` Peter Zijlstra
2018-03-14 18:20       ` Peter Zijlstra
2018-03-14 20:38     ` Alexey Brodkin
2018-03-14 20:38       ` Alexey Brodkin
2018-03-14 20:55       ` Vineet Gupta
2018-03-14 20:55         ` Vineet Gupta
2018-03-15  8:18       ` Peter Zijlstra
2018-03-15  8:18         ` Peter Zijlstra
2018-03-15  8:18         ` Peter Zijlstra
2018-03-15  9:12         ` Alexey Brodkin
2018-03-15  9:12           ` Alexey Brodkin
2018-03-15 11:28           ` Peter Zijlstra
2018-03-15 11:28             ` Peter Zijlstra
2018-03-15 11:28             ` Peter Zijlstra
2018-03-15 19:03             ` Alexey Brodkin
2018-03-15 19:03               ` Alexey Brodkin
2018-03-15 19:03               ` Alexey Brodkin
2018-03-16  7:55               ` Peter Zijlstra
2018-03-16  7:55                 ` Peter Zijlstra
2018-03-16  7:55                 ` Peter Zijlstra
2018-03-16 18:12               ` Max Filippov
2018-03-16 18:12                 ` Max Filippov
2018-03-16 17:33     ` Alexey Brodkin
2018-03-16 17:33       ` Alexey Brodkin
2018-03-16 17:54       ` Vineet Gupta
2018-03-16 17:54         ` Vineet Gupta
2018-03-16 17:58         ` Peter Zijlstra
2018-03-16 17:58           ` Peter Zijlstra

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=20180314175352.GP4064@hirez.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=Alexey.Brodkin@synopsys.com \
    --cc=Vineet.Gupta1@synopsys.com \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-snps-arc@lists.infradead.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.