Skip to contentSkip to conversation

arm_mpam: make the mon_sel guard conditional only

PATCH Superseded Sasha Levin 5 participants 9 messages Oct 3 – Oct 12 MPAM driver

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm_mpam: make the mon_sel guard conditional only

Building arm64 allmodconfig with clang fails:

drivers/resctrl/mpam_internal.h:207:7: error: ignoring return value of function declared with 'warn_unused_result' attribute [-Werror,-Wunused-result]

    207 |              mpam_mon_sel_lock(_T), mpam_mon_sel_unlock(_T));

mpam_mon_sel_lock() is __must_check and can fail: for an MSC accessed through the MPAM-Fb firmware interface it returns false when called from a context that cannot sleep. The mon_sel guard was defined with DEFINE_GUARD(), which calls it unconditionally and discards the result, so a guard(mon_sel) user would carry on without the lock and release it on scope exit. The unconditional guard only exists as the base for the conditional mon_sel_lock variant, which is the one actually used.

Define mon_sel_lock directly as a conditional guard class instead, as posix-timers does for lock_timer: the constructor returns NULL when the lock cannot be taken, and the destructor only releases a lock that was taken. ACQUIRE(mon_sel_lock, ...) works as before, including ACQUIRE_ERR() returning -EBUSY on failure, and there is no unconditional variant left to misuse.

---
 drivers/resctrl/mpam_internal.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/resctrl/mpam_internal.h +4 -3
index ee7c3953a9b6c..be1795b29aca2 100644
@@ -203,9 +203,10 @@ static inline int mpam_mon_sel_lock_init(struct device *dev,
 	return devm_mutex_init(dev, &msc->mon_sel_mutex);
 }
 
-DEFINE_GUARD(mon_sel, struct mpam_msc *,
-	     mpam_mon_sel_lock(_T), mpam_mon_sel_unlock(_T));
-DEFINE_GUARD_COND(mon_sel, _lock, mpam_mon_sel_lock(_T), _RET);
+DEFINE_CLASS(mon_sel_lock, struct mpam_msc *,
+	     _T ? mpam_mon_sel_unlock(_T) : (void)0,
+	     mpam_mon_sel_lock(msc) ? msc : NULL, struct mpam_msc *msc);
+DEFINE_CLASS_IS_COND_GUARD(mon_sel_lock);
 
 /* Bits for mpam features bitmaps */
 enum mpam_device_features {
Signature · 1 line
2.53.0
Tested-by on v1 1/1
#1 lore raw
Quoted 5 lines · Sasha Levin

Building arm64 allmodconfig with clang fails:

drivers/resctrl/mpam_internal.h:207:7: error: ignoring return value of function declared with 'warn_unused_result' attribute [-Werror,-Wunused-result]

Quoted 8 lines
    207 |              mpam_mon_sel_lock(_T), mpam_mon_sel_unlock(_T));

mpam_mon_sel_lock() is __must_check and can fail: for an MSC accessed through the MPAM-Fb firmware interface it returns false when called from a context that cannot sleep. The mon_sel guard was defined with DEFINE_GUARD(), which calls it unconditionally and discards the result, so a guard(mon_sel) user would carry on without the lock and release it on scope exit.

I wondered why we do not see the same warning with GCC since it seems like it should also warn on the same construct...

Quoted 2 lines

The unconditional guard only exists as the base for the conditional mon_sel_lock variant, which is the one actually used.

but this is why: GCC may not emit warnings for dead code due to some warnings being emitted from the middle end after optimizations happen but clang will always warn because its semantic analysis runs in the front end before optimizations occur.

Quoted 6 lines

Define mon_sel_lock directly as a conditional guard class instead, as posix-timers does for lock_timer: the constructor returns NULL when the lock cannot be taken, and the destructor only releases a lock that was taken. ACQUIRE(mon_sel_lock, ...) works as before, including ACQUIRE_ERR() returning -EBUSY on failure, and there is no unconditional variant left to misuse.

These macros make my head hurt but this seems correct to me (though not enough to give a formal review tag).

Quoted 6 lines

Thanks, this fixes the build for me as well.

Quoted 25 lines
---
 drivers/resctrl/mpam_internal.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/resctrl/mpam_internal.h +4 -3
index ee7c3953a9b6c..be1795b29aca2 100644
@@ -203,9 +203,10 @@ static inline int mpam_mon_sel_lock_init(struct device *dev,
 	return devm_mutex_init(dev, &msc->mon_sel_mutex);
 }
 
-DEFINE_GUARD(mon_sel, struct mpam_msc *,
-	     mpam_mon_sel_lock(_T), mpam_mon_sel_unlock(_T));
-DEFINE_GUARD_COND(mon_sel, _lock, mpam_mon_sel_lock(_T), _RET);
+DEFINE_CLASS(mon_sel_lock, struct mpam_msc *,
+	     _T ? mpam_mon_sel_unlock(_T) : (void)0,
+	     mpam_mon_sel_lock(msc) ? msc : NULL, struct mpam_msc *msc);
+DEFINE_CLASS_IS_COND_GUARD(mon_sel_lock);
 
 /* Bits for mpam features bitmaps */
 enum mpam_device_features {
Signature · 1 line
2.53.0
Signature · 2 lines
Cheers,
Nathan
Tested-by on v1 1/1
#2 lore raw
In reply to Sasha Levin

Hi Sasha,

many thanks for doing this!

Quoted 6 lines · Sasha Levin

Building arm64 allmodconfig with clang fails:

drivers/resctrl/mpam_internal.h:207:7: error: ignoring return value of function declared with 'warn_unused_result' attribute [-Werror,-Wunused-result]

     207 |              mpam_mon_sel_lock(_T), mpam_mon_sel_unlock(_T));

Ooops, I indeed tested this only with GCC.

Quoted 8 lines

mpam_mon_sel_lock() is __must_check and can fail: for an MSC accessed through the MPAM-Fb firmware interface it returns false when called from a context that cannot sleep. The mon_sel guard was defined with DEFINE_GUARD(), which calls it unconditionally and discards the result, so a guard(mon_sel) user would carry on without the lock and release it on scope exit. The unconditional guard only exists as the base for the conditional mon_sel_lock variant, which is the one actually used.

Yes, that was the idea I had to model this particular lock, though I wasn't entirely happy with it, but didn't dare to create a whole new class. Turns out my solution was not only slightly less elegant, but also broken ;-) So many thanks for pointing this out and providing the proper solution.

Quoted 22 lines

Define mon_sel_lock directly as a conditional guard class instead, as posix-timers does for lock_timer: the constructor returns NULL when the lock cannot be taken, and the destructor only releases a lock that was taken. ACQUIRE(mon_sel_lock, ...) works as before, including ACQUIRE_ERR() returning -EBUSY on failure, and there is no unconditional variant left to misuse.

---
  drivers/resctrl/mpam_internal.h | 7 ++++---
  1 file changed, 4 insertions(+), 3 deletions(-)
drivers/resctrl/mpam_internal.h +0 -0
index ee7c3953a9b6c..be1795b29aca2 100644

drivers/resctrl/mpam_internal.h static inline int mpam_mon_sel_lock_init(struct device *dev, · line 209

Quoted 10 lines
drivers/resctrl/mpam_internal.h +4 -3
@@ -203,9 +203,10 @@ static inline int mpam_mon_sel_lock_init(struct device *dev,
  	return devm_mutex_init(dev, &msc->mon_sel_mutex);
  }
 
-DEFINE_GUARD(mon_sel, struct mpam_msc *,
-	     mpam_mon_sel_lock(_T), mpam_mon_sel_unlock(_T));
-DEFINE_GUARD_COND(mon_sel, _lock, mpam_mon_sel_lock(_T), _RET);
+DEFINE_CLASS(mon_sel_lock, struct mpam_msc *,
+	     _T ? mpam_mon_sel_unlock(_T) : (void)0,
+	     mpam_mon_sel_lock(msc) ? msc : NULL, struct mpam_msc *msc);
+DEFINE_CLASS_IS_COND_GUARD(mon_sel_lock);

Ah, that's ... nice, I guess, though I share that headache argument with Nathan ;-)
So that looks superficially right, but the details go a bit above my head, although I tested it to both still work and to fix the LLVM build error, so:

Thanks,
Andre

Quoted 3 lines
drivers/resctrl/mpam_internal.h +0 -0
  /* Bits for mpam features bitmaps */
  enum mpam_device_features {
maintainer
#3 lore raw
In reply to Sasha Levin

Hi Sasha,

Thanks for the report and fix.

Quoted 38 lines · Sasha Levin

Building arm64 allmodconfig with clang fails:

drivers/resctrl/mpam_internal.h:207:7: error: ignoring return value of function declared with 'warn_unused_result' attribute [-Werror,-Wunused-result]

    207 |              mpam_mon_sel_lock(_T), mpam_mon_sel_unlock(_T));

mpam_mon_sel_lock() is __must_check and can fail: for an MSC accessed through the MPAM-Fb firmware interface it returns false when called from a context that cannot sleep. The mon_sel guard was defined with DEFINE_GUARD(), which calls it unconditionally and discards the result, so a guard(mon_sel) user would carry on without the lock and release it on scope exit. The unconditional guard only exists as the base for the conditional mon_sel_lock variant, which is the one actually used.

Define mon_sel_lock directly as a conditional guard class instead, as posix-timers does for lock_timer: the constructor returns NULL when the lock cannot be taken, and the destructor only releases a lock that was taken. ACQUIRE(mon_sel_lock, ...) works as before, including ACQUIRE_ERR() returning -EBUSY on failure, and there is no unconditional variant left to misuse.

---
 drivers/resctrl/mpam_internal.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/resctrl/mpam_internal.h +0 -0
index ee7c3953a9b6c..be1795b29aca2 100644

drivers/resctrl/mpam_internal.h static inline int mpam_mon_sel_lock_init(struct device *dev, · line 207

Quoted 8 lines
drivers/resctrl/mpam_internal.h +2 -3
@@ -203,9 +203,10 @@ static inline int mpam_mon_sel_lock_init(struct device *dev,
 	return devm_mutex_init(dev, &msc->mon_sel_mutex);
 }
 
-DEFINE_GUARD(mon_sel, struct mpam_msc *,
-	     mpam_mon_sel_lock(_T), mpam_mon_sel_unlock(_T));
-DEFINE_GUARD_COND(mon_sel, _lock, mpam_mon_sel_lock(_T), _RET);
+DEFINE_CLASS(mon_sel_lock, struct mpam_msc *,
+	     _T ? mpam_mon_sel_unlock(_T) : (void)0,

Could this line be an if statement? That looks to be common in other users of DEFINE_CLASS().

if (_T) mpam_mon_sel_unlock(_T)

Thanks,

Ben

Quoted 5 lines
drivers/resctrl/mpam_internal.h +2 -0
+	     mpam_mon_sel_lock(msc) ? msc : NULL, struct mpam_msc *msc);
+DEFINE_CLASS_IS_COND_GUARD(mon_sel_lock);
 
 /* Bits for mpam features bitmaps */
 enum mpam_device_features {
#4 lore raw

PATCH v2 1/1 arm_mpam: make the mon_sel guard conditional only

Building arm64 allmodconfig with clang fails:

drivers/resctrl/mpam_internal.h:207:7: error: ignoring return value of function declared with 'warn_unused_result' attribute [-Werror,-Wunused-result]

    207 |              mpam_mon_sel_lock(_T), mpam_mon_sel_unlock(_T));

mpam_mon_sel_lock() is __must_check and can fail: for an MSC accessed through the MPAM-Fb firmware interface it returns false when called from a context that cannot sleep. The mon_sel guard was defined with DEFINE_GUARD(), which calls it unconditionally and discards the result, so a guard(mon_sel) user would carry on without the lock and release it on scope exit. The unconditional guard only exists as the base for the conditional mon_sel_lock variant, which is the one actually used.

Define mon_sel_lock directly as a conditional guard class instead, as posix-timers does for lock_timer: the constructor returns NULL when the lock cannot be taken, and the destructor only releases a lock that was taken. ACQUIRE(mon_sel_lock, ...) works as before, including ACQUIRE_ERR() returning -EBUSY on failure, and there is no unconditional variant left to misuse.

---
Changes in v2:
- Use an if statement in the destructor, as suggested by Ben Horgan.

 drivers/resctrl/mpam_internal.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/resctrl/mpam_internal.h +4 -3
index ee7c3953a9b6c..1f86cc158100b 100644
@@ -203,9 +203,10 @@ static inline int mpam_mon_sel_lock_init(struct device *dev,
 	return devm_mutex_init(dev, &msc->mon_sel_mutex);
 }
 
-DEFINE_GUARD(mon_sel, struct mpam_msc *,
-	     mpam_mon_sel_lock(_T), mpam_mon_sel_unlock(_T));
-DEFINE_GUARD_COND(mon_sel, _lock, mpam_mon_sel_lock(_T), _RET);
+DEFINE_CLASS(mon_sel_lock, struct mpam_msc *,
+	     if (_T) mpam_mon_sel_unlock(_T),
+	     mpam_mon_sel_lock(msc) ? msc : NULL, struct mpam_msc *msc);
+DEFINE_CLASS_IS_COND_GUARD(mon_sel_lock);
 
 /* Bits for mpam features bitmaps */
 enum mpam_device_features {
Signature · 1 line
2.53.0
maintainer Reviewed-by on v2 1/1
#5 lore raw

Hi Sasha,

Quoted 23 lines · Sasha Levin

Building arm64 allmodconfig with clang fails:

drivers/resctrl/mpam_internal.h:207:7: error: ignoring return value of function declared with 'warn_unused_result' attribute [-Werror,-Wunused-result]

    207 |              mpam_mon_sel_lock(_T), mpam_mon_sel_unlock(_T));

mpam_mon_sel_lock() is __must_check and can fail: for an MSC accessed through the MPAM-Fb firmware interface it returns false when called from a context that cannot sleep. The mon_sel guard was defined with DEFINE_GUARD(), which calls it unconditionally and discards the result, so a guard(mon_sel) user would carry on without the lock and release it on scope exit. The unconditional guard only exists as the base for the conditional mon_sel_lock variant, which is the one actually used.

Define mon_sel_lock directly as a conditional guard class instead, as posix-timers does for lock_timer: the constructor returns NULL when the lock cannot be taken, and the destructor only releases a lock that was taken. ACQUIRE(mon_sel_lock, ...) works as before, including ACQUIRE_ERR() returning -EBUSY on failure, and there is no unconditional variant left to misuse.

Quoted 6 lines

Looks good to me.

Thanks,

Ben

Quoted 25 lines

---
Changes in v2:
- Use an if statement in the destructor, as suggested by Ben Horgan.

 drivers/resctrl/mpam_internal.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/resctrl/mpam_internal.h +4 -3
index ee7c3953a9b6c..1f86cc158100b 100644
@@ -203,9 +203,10 @@ static inline int mpam_mon_sel_lock_init(struct device *dev,
 	return devm_mutex_init(dev, &msc->mon_sel_mutex);
 }
 
-DEFINE_GUARD(mon_sel, struct mpam_msc *,
-	     mpam_mon_sel_lock(_T), mpam_mon_sel_unlock(_T));
-DEFINE_GUARD_COND(mon_sel, _lock, mpam_mon_sel_lock(_T), _RET);
+DEFINE_CLASS(mon_sel_lock, struct mpam_msc *,
+	     if (_T) mpam_mon_sel_unlock(_T),
+	     mpam_mon_sel_lock(msc) ? msc : NULL, struct mpam_msc *msc);
+DEFINE_CLASS_IS_COND_GUARD(mon_sel_lock);
 
 /* Bits for mpam features bitmaps */
 enum mpam_device_features {
maintainer
#6 lore raw

+CC: Catalin, Will

Hi Catalin,

This is the clang build fix I mentioned. Just adding a note here as I missed to CC you before.

Thanks,
Ben

Quoted 67 lines · Ben Horgan

Hi Sasha,

Quoted 23 lines · Sasha Levin

Building arm64 allmodconfig with clang fails:

drivers/resctrl/mpam_internal.h:207:7: error: ignoring return value of function declared with 'warn_unused_result' attribute [-Werror,-Wunused-result]

    207 |              mpam_mon_sel_lock(_T), mpam_mon_sel_unlock(_T));

mpam_mon_sel_lock() is __must_check and can fail: for an MSC accessed through the MPAM-Fb firmware interface it returns false when called from a context that cannot sleep. The mon_sel guard was defined with DEFINE_GUARD(), which calls it unconditionally and discards the result, so a guard(mon_sel) user would carry on without the lock and release it on scope exit. The unconditional guard only exists as the base for the conditional mon_sel_lock variant, which is the one actually used.

Define mon_sel_lock directly as a conditional guard class instead, as posix-timers does for lock_timer: the constructor returns NULL when the lock cannot be taken, and the destructor only releases a lock that was taken. ACQUIRE(mon_sel_lock, ...) works as before, including ACQUIRE_ERR() returning -EBUSY on failure, and there is no unconditional variant left to misuse.

Quoted 6 lines

Looks good to me.

Thanks,

Ben

Quoted 25 lines

---
Changes in v2:
- Use an if statement in the destructor, as suggested by Ben Horgan.

 drivers/resctrl/mpam_internal.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/resctrl/mpam_internal.h +4 -3
index ee7c3953a9b6c..1f86cc158100b 100644
@@ -203,9 +203,10 @@ static inline int mpam_mon_sel_lock_init(struct device *dev,
 	return devm_mutex_init(dev, &msc->mon_sel_mutex);
 }
 
-DEFINE_GUARD(mon_sel, struct mpam_msc *,
-	     mpam_mon_sel_lock(_T), mpam_mon_sel_unlock(_T));
-DEFINE_GUARD_COND(mon_sel, _lock, mpam_mon_sel_lock(_T), _RET);
+DEFINE_CLASS(mon_sel_lock, struct mpam_msc *,
+	     if (_T) mpam_mon_sel_unlock(_T),
+	     mpam_mon_sel_lock(msc) ? msc : NULL, struct mpam_msc *msc);
+DEFINE_CLASS_IS_COND_GUARD(mon_sel_lock);
 
 /* Bits for mpam features bitmaps */
 enum mpam_device_features {
#7 lore raw
Quoted 1 line · Ben Horgan

This is the clang build fix I mentioned. Just adding a note here as I missed to CC you before.

I queued it already, just haven't got around to run b4 ty yet.

Signature · 1 line
Catalin
#8 lore raw
In reply to Sasha Levin
Quoted 8 lines · Sasha Levin

Building arm64 allmodconfig with clang fails:

drivers/resctrl/mpam_internal.h:207:7: error: ignoring return value of function declared with 'warn_unused_result' attribute [-Werror,-Wunused-result]

    207 |              mpam_mon_sel_lock(_T), mpam_mon_sel_unlock(_T));

[...]

Applied to arm64 (for-next/mpam), thanks!

[1/1] arm_mpam: make the mon_sel guard conditional only

      https://git.kernel.org/arm64/c/8ea457f746d9