Skip to contentSkip to conversation

arm: spinlock: const qualify read-only functions

PATCH New Ian Coolidge 3 participants 8 messages Oct 28, 2015 – Oct 31, 2015

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

PATCH 1/1 arm: spinlock: const qualify read-only functions

This allows assert_spin_locked() to be used against spinlocks that are const qualified.

---
 arch/arm/include/asm/spinlock.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/arm/include/asm/spinlock.h +3 -3
index 0fa4184..3512d2d 100644
@@ -113,17 +113,17 @@ static inline void arch_spin_unlock(arch_spinlock_t *lock)
 	dsb_sev();
 }
 
-static inline int arch_spin_value_unlocked(arch_spinlock_t lock)
+static inline int arch_spin_value_unlocked(const arch_spinlock_t lock)
 {
 	return lock.tickets.owner == lock.tickets.next;
 }
 
-static inline int arch_spin_is_locked(arch_spinlock_t *lock)
+static inline int arch_spin_is_locked(const arch_spinlock_t *lock)
 {
 	return !arch_spin_value_unlocked(READ_ONCE(*lock));
 }
 
-static inline int arch_spin_is_contended(arch_spinlock_t *lock)
+static inline int arch_spin_is_contended(const arch_spinlock_t *lock)
 {
 	struct __raw_tickets tickets = READ_ONCE(lock->tickets);
 	return (tickets.next - tickets.owner) > 1;
Signature · 1 line
2.6.0.rc2.230.g3dd15c0
#1 lore raw

PATCH 1/1 arm64: spinlock: const qualify read-only functions

This allows assert_spin_locked() to be used against spinlocks that are const qualified.

---
 arch/arm64/include/asm/spinlock.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/arm64/include/asm/spinlock.h +3 -3
index c85e96d..72a7f33 100644
@@ -122,17 +122,17 @@ static inline void arch_spin_unlock(arch_spinlock_t *lock)
 	: "memory");
 }
 
-static inline int arch_spin_value_unlocked(arch_spinlock_t lock)
+static inline int arch_spin_value_unlocked(const arch_spinlock_t lock)
 {
 	return lock.owner == lock.next;
 }
 
-static inline int arch_spin_is_locked(arch_spinlock_t *lock)
+static inline int arch_spin_is_locked(const arch_spinlock_t *lock)
 {
 	return !arch_spin_value_unlocked(READ_ONCE(*lock));
 }
 
-static inline int arch_spin_is_contended(arch_spinlock_t *lock)
+static inline int arch_spin_is_contended(const arch_spinlock_t *lock)
 {
 	arch_spinlock_t lockval = READ_ONCE(*lock);
 	return (lockval.next - lockval.owner) > 1;
Signature · 1 line
2.6.0.rc2.230.g3dd15c0
#2 lore raw

PATCH 1/1 arm: spinlock: const qualify read-only functions

In reply to Ian Coolidge
Quoted 5 lines · On Wednesday 28 October 2015 12:44:54 Ian Coolidge wrote:

This allows assert_spin_locked() to be used against spinlocks that are const qualified.

Can you give an example in the changelog where this might be useful? Constant spinlocks seem counterintuitive.

Arnd

#3 lore raw

PATCH 1/1 arm: spinlock: const qualify read-only functions.

In reply to Ian Coolidge

This allows assert_spin_locked() to be used against spinlocks that are const qualified.

For example:

struct instance_t {
	int data;
	spinlock_t lock;
};

/*
* foo does not mutate instance.
* foo must be called while the lock is held.
*/

int foo(const instance_t *instance) {
	assert_spin_locked(&instance->lock);

	/* Code that assumes the lock is held */

	return 0;
}
---
 arch/arm/include/asm/spinlock.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm/include/asm/spinlock.h +2 -2
index 0fa4184..274ceb1 100644
@@ -118,12 +118,12 @@ static inline int arch_spin_value_unlocked(arch_spinlock_t lock)
 	return lock.tickets.owner == lock.tickets.next;
 }
 
-static inline int arch_spin_is_locked(arch_spinlock_t *lock)
+static inline int arch_spin_is_locked(const arch_spinlock_t *lock)
 {
 	return !arch_spin_value_unlocked(READ_ONCE(*lock));
 }
 
-static inline int arch_spin_is_contended(arch_spinlock_t *lock)
+static inline int arch_spin_is_contended(const arch_spinlock_t *lock)
 {
 	struct __raw_tickets tickets = READ_ONCE(lock->tickets);
 	return (tickets.next - tickets.owner) > 1;
Signature · 1 line
2.6.0.rc2.230.g3dd15c0
#4 lore raw

PATCH 1/1 arm: spinlock: const qualify read-only functions.

Quoted 47 lines · Ian Coolidge

This allows assert_spin_locked() to be used against spinlocks that are const qualified.

For example:

struct instance_t {
        int data;
        spinlock_t lock;
};

/*
* foo does not mutate instance.
* foo must be called while the lock is held.
*/

int foo(const instance_t *instance) {
        assert_spin_locked(&instance->lock);

        /* Code that assumes the lock is held */

        return 0;
}
---
 arch/arm/include/asm/spinlock.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm/include/asm/spinlock.h +2 -3
index 0fa4184..274ceb1 100644
@@ -118,12 +118,12 @@ static inline int arch_spin_value_unlocked(arch_spinlock_t lock)
        return lock.tickets.owner == lock.tickets.next;
 }
 
-static inline int arch_spin_is_locked(arch_spinlock_t *lock)
+static inline int arch_spin_is_locked(const arch_spinlock_t *lock)
 {
        return !arch_spin_value_unlocked(READ_ONCE(*lock));
 }
 
-static inline int arch_spin_is_contended(arch_spinlock_t *lock)
+static inline int arch_spin_is_contended(const arch_spinlock_t *lock)
 {
        struct __raw_tickets tickets = READ_ONCE(lock->tickets);
        return (tickets.next - tickets.owner) > 1;
--
Quoted 2 lines

2.6.0.rc2.230.g3dd15c0

Arnd,

Please see this patch for an updated version. I should have noted it is PATCH v2 and replied to the old thread, sorry about that. Anyways, I provided an example of a const spinlock. Here, in that example, the spinlock is not const itself, but it is part of a structure that may be const.
I believe the example is a good one.

Also, with regards to your feedback, I don't think the length of the function body is a good argument against promising not to mutate it.

Thanks for taking a look,
Ian

#5 lore raw

PATCH 1/1 arm: spinlock: const qualify read-only functions.

Quoted 60 lines · Ian Coolidge
Quoted 47 lines · Ian Coolidge

This allows assert_spin_locked() to be used against spinlocks that are const qualified.

For example:

struct instance_t {
        int data;
        spinlock_t lock;
};

/*
* foo does not mutate instance.
* foo must be called while the lock is held.
*/

int foo(const instance_t *instance) {
        assert_spin_locked(&instance->lock);

        /* Code that assumes the lock is held */

        return 0;
}
---
 arch/arm/include/asm/spinlock.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm/include/asm/spinlock.h +2 -3
index 0fa4184..274ceb1 100644
@@ -118,12 +118,12 @@ static inline int arch_spin_value_unlocked(arch_spinlock_t lock)
        return lock.tickets.owner == lock.tickets.next;
 }
 
-static inline int arch_spin_is_locked(arch_spinlock_t *lock)
+static inline int arch_spin_is_locked(const arch_spinlock_t *lock)
 {
        return !arch_spin_value_unlocked(READ_ONCE(*lock));
 }
 
-static inline int arch_spin_is_contended(arch_spinlock_t *lock)
+static inline int arch_spin_is_contended(const arch_spinlock_t *lock)
 {
        struct __raw_tickets tickets = READ_ONCE(lock->tickets);
        return (tickets.next - tickets.owner) > 1;
--
Quoted 2 lines

2.6.0.rc2.230.g3dd15c0

Arnd,

Please see this patch for an updated version. I should have noted it is PATCH v2 and replied to the old thread, sorry about that. Anyways, I provided an example of a const spinlock. Here, in that example, the spinlock is not const itself, but it is part of a structure that may be const.
I believe the example is a good one.

Quoted 3 lines

Also, with regards to your feedback, I don't think the length of the function body is a good argument against promising not to mutate it.

Hi,

Note that Ard (me) and Arnd are two different persons.

I think your patch makes sense in itself, I just wonder if there are any real examples in the current codebase that would require it. Which struct is it that you are looking to constify, and in which context?

Regards,
Ard.

#6 lore raw

PATCH 1/1 arm: spinlock: const qualify read-only functions.

Hi Ard,

:) D'oh, yes, I had assumed you were the same person.

It is for a driver that I would eventually like to upstream.

I see that there are many cases where assert_spin_lock is used, but perhaps driver developers are underutilizing const,
or compiling the kernel with warnings tolerated.

Quoted 75 lines · Ard Biesheuvel
Quoted 60 lines · Ian Coolidge
Quoted 47 lines · Ian Coolidge

This allows assert_spin_locked() to be used against spinlocks that are const qualified.

For example:

struct instance_t {
        int data;
        spinlock_t lock;
};

/*
* foo does not mutate instance.
* foo must be called while the lock is held.
*/

int foo(const instance_t *instance) {
        assert_spin_locked(&instance->lock);

        /* Code that assumes the lock is held */

        return 0;
}
---
 arch/arm/include/asm/spinlock.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm/include/asm/spinlock.h +2 -3
index 0fa4184..274ceb1 100644
@@ -118,12 +118,12 @@ static inline int arch_spin_value_unlocked(arch_spinlock_t lock)
        return lock.tickets.owner == lock.tickets.next;
 }
 
-static inline int arch_spin_is_locked(arch_spinlock_t *lock)
+static inline int arch_spin_is_locked(const arch_spinlock_t *lock)
 {
        return !arch_spin_value_unlocked(READ_ONCE(*lock));
 }
 
-static inline int arch_spin_is_contended(arch_spinlock_t *lock)
+static inline int arch_spin_is_contended(const arch_spinlock_t *lock)
 {
        struct __raw_tickets tickets = READ_ONCE(lock->tickets);
        return (tickets.next - tickets.owner) > 1;
--
Quoted 2 lines

2.6.0.rc2.230.g3dd15c0

Arnd,

Please see this patch for an updated version. I should have noted it is PATCH v2 and replied to the old thread, sorry about that. Anyways, I provided an example of a const spinlock. Here, in that example, the spinlock is not const itself, but it is part of a structure that may be const.
I believe the example is a good one.

Quoted 3 lines

Also, with regards to your feedback, I don't think the length of the function body is a good argument against promising not to mutate it.

Hi,

Note that Ard (me) and Arnd are two different persons.

I think your patch makes sense in itself, I just wonder if there are any real examples in the current codebase that would require it. Which struct is it that you are looking to constify, and in which context?

Regards,
Ard.

#7 lore raw

PATCH 1/1 arm: spinlock: const qualify read-only functions.

Quoted 18 lines · Ian Coolidge

Hi Ard,

:) D'oh, yes, I had assumed you were the same person.

It is for a driver that I would eventually like to upstream.

I see that there are many cases where assert_spin_lock is used, but perhaps driver developers are underutilizing const,
or compiling the kernel with warnings tolerated.

Quoted 8 lines

Note that Ard (me) and Arnd are two different persons.

I think your patch makes sense in itself, I just wonder if there are any real examples in the current codebase that would require it. Which struct is it that you are looking to constify, and in which context?

Regards,
Ard.

Sorry for the top-post, and I meant 'assert_spin_locked'.