scsi: scsi_debug: Improve lock context annotations

Prepare the scsi_debug driver for enabling compiler-based lock context
analysis:

 - Annotate the lock and unlock helper functions with __acquires,
   __releases, __acquires_shared, and __releases_shared.

 - Add __acquire() and __release() calls in the paths that do not match
   the __acquires() / __releases() annotations.

 - Annotate sdeb_meta_{read,write}_{,un}lock() with __context_unsafe()
   because adding __acquire() and __release() calls in these functions
   is not possible without triggering undefined behavior (taking the
   address of a member of a structure with a NULL pointer as base
   address).

Signed-off-by: Bart Van Assche <bvanassche@acm.org>
Link: https://patch.msgid.link/1775809e31b535cc42f3f79e75f28666e74a98f7.1791924924.git.bvanassche@acm.org
Signed-off-by: Martin K. Petersen (Oracle) <mkp@kernel.org>
This commit is contained in:
Bart Van Assche 2026-10-13 15:10:08 -07:00 committed by Martin K. Petersen (Oracle)
parent 20a26ca89e
commit 0f81d5ea52

View File

@ -4052,24 +4052,27 @@ static inline struct sdeb_store_info *devip2sip(struct sdebug_dev_info *devip,
static inline void
sdeb_read_lock(rwlock_t *lock)
__acquires_shared(lock)
{
if (sdebug_no_rwlock)
__acquire(lock);
__acquire_shared(lock);
else
read_lock(lock);
}
static inline void
sdeb_read_unlock(rwlock_t *lock)
__releases_shared(lock)
{
if (sdebug_no_rwlock)
__release(lock);
__release_shared(lock);
else
read_unlock(lock);
}
static inline void
sdeb_write_lock(rwlock_t *lock)
__acquires(lock)
{
if (sdebug_no_rwlock)
__acquire(lock);
@ -4079,6 +4082,7 @@ sdeb_write_lock(rwlock_t *lock)
static inline void
sdeb_write_unlock(rwlock_t *lock)
__releases(lock)
{
if (sdebug_no_rwlock)
__release(lock);
@ -4088,6 +4092,7 @@ sdeb_write_unlock(rwlock_t *lock)
static inline void
sdeb_data_read_lock(struct sdeb_store_info *sip)
__acquires_shared(&sip->macc_data_lck)
{
BUG_ON(!sip);
@ -4096,6 +4101,7 @@ sdeb_data_read_lock(struct sdeb_store_info *sip)
static inline void
sdeb_data_read_unlock(struct sdeb_store_info *sip)
__releases_shared(&sip->macc_data_lck)
{
BUG_ON(!sip);
@ -4104,6 +4110,7 @@ sdeb_data_read_unlock(struct sdeb_store_info *sip)
static inline void
sdeb_data_write_lock(struct sdeb_store_info *sip)
__acquires(&sip->macc_data_lck)
{
BUG_ON(!sip);
@ -4112,6 +4119,7 @@ sdeb_data_write_lock(struct sdeb_store_info *sip)
static inline void
sdeb_data_write_unlock(struct sdeb_store_info *sip)
__releases(&sip->macc_data_lck)
{
BUG_ON(!sip);
@ -4120,6 +4128,7 @@ sdeb_data_write_unlock(struct sdeb_store_info *sip)
static inline void
sdeb_data_sector_read_lock(struct sdeb_store_info *sip)
__acquires_shared(&sip->macc_sector_lck)
{
BUG_ON(!sip);
@ -4128,6 +4137,7 @@ sdeb_data_sector_read_lock(struct sdeb_store_info *sip)
static inline void
sdeb_data_sector_read_unlock(struct sdeb_store_info *sip)
__releases_shared(&sip->macc_sector_lck)
{
BUG_ON(!sip);
@ -4136,6 +4146,7 @@ sdeb_data_sector_read_unlock(struct sdeb_store_info *sip)
static inline void
sdeb_data_sector_write_lock(struct sdeb_store_info *sip)
__acquires(&sip->macc_sector_lck)
{
BUG_ON(!sip);
@ -4144,6 +4155,7 @@ sdeb_data_sector_write_lock(struct sdeb_store_info *sip)
static inline void
sdeb_data_sector_write_unlock(struct sdeb_store_info *sip)
__releases(&sip->macc_sector_lck)
{
BUG_ON(!sip);
@ -4165,50 +4177,67 @@ sdeb_data_sector_write_unlock(struct sdeb_store_info *sip)
static inline void
sdeb_data_lock(struct sdeb_store_info *sip, bool atomic)
__acquires(&sip->macc_data_lck)
{
if (atomic)
if (atomic) {
sdeb_data_write_lock(sip);
else
} else {
sdeb_data_read_lock(sip);
__release_shared(&sip->macc_data_lck);
__acquire(&sip->macc_data_lck);
}
}
static inline void
sdeb_data_unlock(struct sdeb_store_info *sip, bool atomic)
__releases(&sip->macc_data_lck)
{
if (atomic)
if (atomic) {
sdeb_data_write_unlock(sip);
else
} else {
__release(&sip->macc_data_lck);
__acquire_shared(&sip->macc_data_lck);
sdeb_data_read_unlock(sip);
}
}
/* Allow many reads but only 1x write per sector */
static inline void
sdeb_data_sector_lock(struct sdeb_store_info *sip, bool do_write)
__acquires(&sip->macc_sector_lck)
{
if (do_write)
if (do_write) {
sdeb_data_sector_write_lock(sip);
else
} else {
sdeb_data_sector_read_lock(sip);
__release_shared(&sip->macc_sector_lck);
__acquire(&sip->macc_sector_lck);
}
}
static inline void
sdeb_data_sector_unlock(struct sdeb_store_info *sip, bool do_write)
__releases(&sip->macc_sector_lck)
{
if (do_write)
if (do_write) {
sdeb_data_sector_write_unlock(sip);
else
} else {
__release(&sip->macc_sector_lck);
__acquire_shared(&sip->macc_sector_lck);
sdeb_data_sector_read_unlock(sip);
}
}
static inline void
sdeb_meta_read_lock(struct sdeb_store_info *sip)
__acquires_shared(&sip->macc_meta_lck)
__context_unsafe(/*
* Can't be annotated with
* __{acquire,release}(,_shared}() without triggering
* undefined behavior.
*/)
{
if (sdebug_no_rwlock) {
if (sip)
__acquire(&sip->macc_meta_lck);
else
__acquire(&sdeb_fake_rw_lck);
} else {
if (!sdebug_no_rwlock) {
if (sip)
read_lock(&sip->macc_meta_lck);
else
@ -4218,13 +4247,14 @@ sdeb_meta_read_lock(struct sdeb_store_info *sip)
static inline void
sdeb_meta_read_unlock(struct sdeb_store_info *sip)
__releases_shared(&sip->macc_meta_lck)
__context_unsafe(/*
* Can't be annotated with
* __{acquire,release}(,_shared}() without triggering
* undefined behavior.
*/)
{
if (sdebug_no_rwlock) {
if (sip)
__release(&sip->macc_meta_lck);
else
__release(&sdeb_fake_rw_lck);
} else {
if (!sdebug_no_rwlock) {
if (sip)
read_unlock(&sip->macc_meta_lck);
else
@ -4234,13 +4264,14 @@ sdeb_meta_read_unlock(struct sdeb_store_info *sip)
static inline void
sdeb_meta_write_lock(struct sdeb_store_info *sip)
__acquires(&sip->macc_meta_lck)
__context_unsafe(/*
* Can't be annotated with
* __{acquire,release}(,_shared}() without triggering
* undefined behavior.
*/)
{
if (sdebug_no_rwlock) {
if (sip)
__acquire(&sip->macc_meta_lck);
else
__acquire(&sdeb_fake_rw_lck);
} else {
if (!sdebug_no_rwlock) {
if (sip)
write_lock(&sip->macc_meta_lck);
else
@ -4250,13 +4281,14 @@ sdeb_meta_write_lock(struct sdeb_store_info *sip)
static inline void
sdeb_meta_write_unlock(struct sdeb_store_info *sip)
__releases(&sip->macc_meta_lck)
__context_unsafe(/*
* Can't be annotated with
* __{acquire,release}(,_shared}() without triggering
* undefined behavior.
*/)
{
if (sdebug_no_rwlock) {
if (sip)
__release(&sip->macc_meta_lck);
else
__release(&sdeb_fake_rw_lck);
} else {
if (!sdebug_no_rwlock) {
if (sip)
write_unlock(&sip->macc_meta_lck);
else