Skip to contentSkip to conversation

coredump: Replace opencoded set_mask_bits()

PATCH In review Vineet Gupta 4 participants 8 messages Aug 7, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 coredump: Replace opencoded set_mask_bits()

Cc Alexander Viro <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc Peter Zijlstra (Intel) <[email protected]>
---
 fs/exec.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
fs/exec.c +1 -6
index 1977c2a553ac..25078627f048 100644
@@ -1690,15 +1690,10 @@ EXPORT_SYMBOL(set_binfmt);
  */
 void set_dumpable(struct mm_struct *mm, int value)
 {
-	unsigned long old, new;
-
 	if (WARN_ON((unsigned)value > SUID_DUMP_ROOT))
 		return;
 
-	do {
-		old = ACCESS_ONCE(mm->flags);
-		new = (old & ~MMF_DUMPABLE_MASK) | value;
-	} while (cmpxchg(&mm->flags, old, new) != old);
+	set_mask_bits(&mm->flags, MMF_DUMPABLE_MASK, value);
 }
 
 SYSCALL_DEFINE3(execve,
Signature · 1 line
1.9.1
Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · Vineet Gupta
fs/exec.c +0 -1
@@ -1690,15 +1690,10 @@ EXPORT_SYMBOL(set_binfmt);
  */
 void set_dumpable(struct mm_struct *mm, int value)
 {
-	unsigned long old, new;

fs/exec.c EXPORT_SYMBOL(set_binfmt); · line 1697

Quoted 10 lines
fs/exec.c +1 -5
-
 	if (WARN_ON((unsigned)value > SUID_DUMP_ROOT))
 		return;
 
-	do {
-		old = ACCESS_ONCE(mm->flags);
-		new = (old & ~MMF_DUMPABLE_MASK) | value;
-	} while (cmpxchg(&mm->flags, old, new) != old);
+	set_mask_bits(&mm->flags, MMF_DUMPABLE_MASK, value);
 }
#2 lore raw
Quoted 13 lines · Oleg Nesterov
Quoted 12 lines · Vineet Gupta
fs/exec.c +0 -2
@@ -1690,15 +1690,10 @@ EXPORT_SYMBOL(set_binfmt);
  */
 void set_dumpable(struct mm_struct *mm, int value)
 {
-	unsigned long old, new;
-
 	if (WARN_ON((unsigned)value > SUID_DUMP_ROOT))
 		return;
 
Quoted 8 lines

fs/exec.c EXPORT_SYMBOL(set_binfmt); · line 1697

Quoted 6 lines
fs/exec.c +1 -4
-	do {
-		old = ACCESS_ONCE(mm->flags);
-		new = (old & ~MMF_DUMPABLE_MASK) | value;
-	} while (cmpxchg(&mm->flags, old, new) != old);
+	set_mask_bits(&mm->flags, MMF_DUMPABLE_MASK, value);
 }

I have a fundamental question though, perhaps stupid, do use cases like these warrant the data to be atomic_t in first place. Do API like set_mask_bits() make sense at all - or shd they be moved to atomic_* (after changing the underlying data)

See, I have such a cmpxchg loop in ARC code - originally from Peter :-) arch/arc/kernel/smp.c. @ipi_data_ptr is NOT atomic_t

	do {
		new = old = ACCESS_ONCE(*ipi_data_ptr);
		new |= 1U << msg;
	} while (cmpxchg(ipi_data_ptr, old, new) != old);

Given that ARC (and some other RISC cores) lack native cmpxchg, we use LLSC instructions to implement atomics including cpmxchg - the implementation itself ensures loop is builtin making the outer loping superfluous and waste of cycles (see e.g. cover letter @ http://www.spinics.net/lists/kernel/msg2029217.html)

So I wanted to convert that loop (and similar other cases to "some" API which could be built conditionally based on cmpxchg or llsc. None such exist and I was thinking of converting my case to atomic_t. Is that the right approach ?

Thx,
-Vineet

#3 lore raw
Quoted 8 lines · Vineet Gupta

See, I have such a cmpxchg loop in ARC code - originally from Peter :-) arch/arc/kernel/smp.c. @ipi_data_ptr is NOT atomic_t

	do {
		new = old = ACCESS_ONCE(*ipi_data_ptr);
		new |= 1U << msg;
	} while (cmpxchg(ipi_data_ptr, old, new) != old);

Well, you'll have atomic_or() real soon now.

#4 lore raw
Quoted 11 lines · Peter Zijlstra
Quoted 8 lines · Vineet Gupta
Quoted 8 lines

See, I have such a cmpxchg loop in ARC code - originally from Peter :-) arch/arc/kernel/smp.c. @ipi_data_ptr is NOT atomic_t

	do {
		new = old = ACCESS_ONCE(*ipi_data_ptr);
		new |= 1U << msg;
	} while (cmpxchg(ipi_data_ptr, old, new) != old);

Well, you'll have atomic_or() real soon now.

Doesn't help my cause - ipi_data_ptr is not atomic_t - hence my prev question in this thread

#5 lore raw
Quoted 11 lines · Vineet Gupta
Quoted 10 lines · Peter Zijlstra
Quoted 8 lines · Vineet Gupta
Quoted 8 lines

See, I have such a cmpxchg loop in ARC code - originally from Peter :-) arch/arc/kernel/smp.c. @ipi_data_ptr is NOT atomic_t

	do {
		new = old = ACCESS_ONCE(*ipi_data_ptr);
		new |= 1U << msg;
	} while (cmpxchg(ipi_data_ptr, old, new) != old);
Quoted 4 lines
Quoted 1 line

Well, you'll have atomic_or() real soon now.

Doesn't help my cause - ipi_data_ptr is not atomic_t - hence my prev question in this thread

A cast will work :-)

But yes, ideally everything will be type safe because of those archs that cannot have atomic RmW ops like !ARC_HAS_LLSC.

Mixing cmpxchg()/xchg() with regular stores is broken on those.

Fwiw, you might want to set ARCH_SUPPORTS_ATOMIC_RMW for ARC_HAS_LLSC.

#6 lore raw
Quoted 12 lines · Peter Zijlstra
Quoted 11 lines · Vineet Gupta
Quoted 10 lines · Peter Zijlstra
Quoted 8 lines · Vineet Gupta
Quoted 8 lines

See, I have such a cmpxchg loop in ARC code - originally from Peter :-) arch/arc/kernel/smp.c. @ipi_data_ptr is NOT atomic_t

	do {
		new = old = ACCESS_ONCE(*ipi_data_ptr);
		new |= 1U << msg;
	} while (cmpxchg(ipi_data_ptr, old, new) != old);
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Well, you'll have atomic_or() real soon now.

Doesn't help my cause - ipi_data_ptr is not atomic_t - hence my prev question in this thread

A cast will work :-)

How ? We have

typedef struct {
int counter;
} atomic_t;

Quoted 2 lines

But yes, ideally everything will be type safe because of those archs that cannot have atomic RmW ops like !ARC_HAS_LLSC.

Type safe - how / what ?

Quoted 2 lines

Mixing cmpxchg()/xchg() with regular stores is broken on those.

Right, but how does that relate to this discussion - perhaps I shd stop talking - long friday already :-)

Quoted 3 lines

Fwiw, you might want to set ARCH_SUPPORTS_ATOMIC_RMW for ARC_HAS_LLSC.

Sure I will !

#7 lore raw
Quoted 18 lines · Vineet Gupta
Quoted 17 lines · Peter Zijlstra
Quoted 15 lines · Vineet Gupta
Quoted 11 lines · Peter Zijlstra
Quoted 8 lines · Vineet Gupta
Quoted 8 lines

See, I have such a cmpxchg loop in ARC code - originally from Peter :-) arch/arc/kernel/smp.c. @ipi_data_ptr is NOT atomic_t

	do {
		new = old = ACCESS_ONCE(*ipi_data_ptr);
		new |= 1U << msg;
	} while (cmpxchg(ipi_data_ptr, old, new) != old);

Well, you'll have atomic_or() real soon now.

Doesn't help my cause - ipi_data_ptr is not atomic_t - hence my prev question in this thread

Quoted 8 lines
Quoted 2 lines

A cast will work :-)

How ? We have

typedef struct {
int counter;
} atomic_t;

ARC is 32bit, right? So int and unsigned long are of the same size. Therefore:

	atomic_or(1 << msg, (atomic_t *)ipi_data_ptr);

Ugly, yes, but it should DTRT.

Quoted 4 lines
Quoted 2 lines

But yes, ideally everything will be type safe because of those archs that cannot have atomic RmW ops like !ARC_HAS_LLSC.

Type safe - how / what ?

All atomic stuff restricted to atomic*t and bitmap functions (and ideally we'd also have bitmap_t to avoid passing random unsigned long * into bitmap functions and praying it all works, we do, and it doesn't, well mostly :-).

Quoted 4 lines
Quoted 1 line

Mixing cmpxchg()/xchg() with regular stores is broken on those.

Right, but how does that relate to this discussion - perhaps I shd stop talking - long friday already :-)

:-)

Well, its a very good argument for why we should not use cmpxchg/xchg on !atomic*t types, and therefore why the function at hand (set_mask_bit) should really be on an atomic_t.

That said, it will probably make the fs code fugly for having to use atomic_t and all its accessors all over the place.