Skip to contentSkip to conversation

powerpc/mm, pseries, powernv: Enable clang context analysis

PATCH New Ritesh Harjani (IBM) 1 participant 20 messages Nov 20

Subsystems (2)
lore
Share & tools
lore

Ritesh Harjani (IBM)

author reviewer
#0 lore raw

v3 cover powerpc/mm, pseries, powernv: Enable clang context analysis

Enable Clang context analysis checking for most of powerpc/mm, pseries & powernv platforms.

Lock acquire and release checking used to be done with Sparse. That support was removed [1] and replaced with Clang context analysis [2].

Note that enabling this caught a real locking rcu bug in arch/powerpc/mm (Patch-1). Note, more invasive checkers to check what each lock protects is deferred for later enablement.

The series was build tested with LLVM/Clang 23.x

[1] 5b63d0ae94cc ("compiler-context-analysis: Remove Sparse support")
[2] 3269701cb256 ("compiler-context-analysis: Add infrastructure for Context Analysis with Clang")

v2 -> v3:
=========
1. Added patch-3 to remove -Wunused-but-set-variable on 4K with Hash (reported by Sashiko).

v1 -> v2:
===========
1. Dropped RFC tag, since this series is looking in shape to be included for v7.5
2. Clubbed both RFC series {pseries|powernv} and mm into one.
2. Patch-3 changed the type of i from "unsigned long i" to "int i"
3. Fixed commit msg for patch-14 - as pointed by Sashiko

Ritesh Harjani (IBM) (19):
powerpc/mm/book3s64: unlock RCU before returning from mm_iommu_is_devmem() powerpc/mm/book3s64: Rework tlbie() to remove conditional locking powerpc/64s/hash: Reference all arguments in the 4K pte_iterate_hashed_subpages() powerpc/mm/book3s64: Rework native_flush_hash_range() to remove conditional locking powerpc/mm/book3s64: Annotate the subpage-protection PTE lock powerpc/mm: Rework nohash TLB broadcast locking
powerpc/mm: Enable context analysis
powerpc/pseries: Drop inline from hugepage invalidate powerpc/pseries: Rework hugepage invalidate to remove conditional locking powerpc/pseries: Rework hash flush to remove conditional locking powerpc/pseries: Add context annotations for the DTL worker lock powerpc/pseries: Annotate the VPD sequence lock context powerpc/pseries: Annotate the RTAS indices sequence lock context powerpc/pseries: Annotate the RTAS physical-attestation sequence lock context powerpc/pseries: Annotate the DTL debugfs read lock context powerpc/pseries: Disable context analysis for the CMM hotplug notifier powerpc/pseries: Enable context analysis
powerpc/powernv: Rework OPAL console write to remove conditional locking powerpc/powernv: Enable context analysis

 arch/powerpc/include/asm/book3s/64/hash-4k.h  |   4 +
 arch/powerpc/mm/Makefile                      |   2 +
 arch/powerpc/mm/book3s32/Makefile             |   2 +
 arch/powerpc/mm/book3s64/Makefile             |   2 +
 arch/powerpc/mm/book3s64/hash_native.c        | 122 +++++++++++-------
 arch/powerpc/mm/book3s64/iommu_api.c          |   6 +-
 arch/powerpc/mm/book3s64/subpage_prot.c       |   2 +
 arch/powerpc/mm/nohash/Makefile               |   2 +
 arch/powerpc/mm/nohash/mmu_context.c          |   1 +
 arch/powerpc/mm/nohash/tlb.c                  |   8 +-
 arch/powerpc/platforms/powernv/Makefile       |   2 +
 arch/powerpc/platforms/powernv/opal.c         |  30 +++--
 arch/powerpc/platforms/pseries/Makefile       |   3 +
 arch/powerpc/platforms/pseries/cmm.c          |   1 +
 arch/powerpc/platforms/pseries/dtl.c          |   8 ++
 arch/powerpc/platforms/pseries/lpar.c         |  59 +++++----
 arch/powerpc/platforms/pseries/papr-indices.c |   3 +
 .../platforms/pseries/papr-phy-attest.c       |   4 +
 arch/powerpc/platforms/pseries/papr-vpd.c     |   3 +
 19 files changed, 183 insertions(+), 81 deletions(-)

--
2.39.5

author reviewer
#1 lore raw

PATCH v3 1/19 powerpc/mm/book3s64: unlock RCU before returning from mm_iommu_is_devmem()

Clang's context analysis reports:
iommu_api.c:380:1: error: __ctx_lock_RCU 'RCU' is not held on every path through here [-Werror,-Wthread-safety-analysis]

mm_iommu_is_devmem() takes the RCU read lock to walk the preregistered device-memory list. On a match it returned true from inside the loop and skipped rcu_read_unlock(). Hence fix it.

Fixes b5952f8125ae ("powerpc/mm/book3s64/iommu: fix some RCU-list locks")
---
 arch/powerpc/mm/book3s64/iommu_api.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
arch/powerpc/mm/book3s64/iommu_api.c +4 -2
index 60d00c9e7f19..56d9aa6cc8e2 100644
@@ -356,6 +356,7 @@ bool mm_iommu_is_devmem(struct mm_struct *mm, unsigned long hpa,
 {
 	struct mm_iommu_table_group_mem_t *mem;
 	unsigned long end;
+	bool ret = false;
 
 	rcu_read_lock();
 	list_for_each_entry_rcu(mem, &mm->context.iommu_group_mem_list, next) {
@@ -371,12 +372,13 @@ bool mm_iommu_is_devmem(struct mm_struct *mm, unsigned long hpa,
 			 * and the caller needs to distinguish this situation.
 			 */
 			*size = min(1UL << pageshift, end - hpa);
-			return true;
+			ret = true;
+			break;
 		}
 	}
 	rcu_read_unlock();
 
-	return false;
+	return ret;
 }
 EXPORT_SYMBOL_GPL(mm_iommu_is_devmem);
 
Signature · 1 line
2.39.5
author reviewer
#2 lore raw

PATCH v3 2/19 powerpc/mm/book3s64: Rework tlbie() to remove conditional locking

In reply to Ritesh Harjani (IBM)

Clang's context analysis reports:
hash_native.c:235:2: error: raw_spinlock 'native_tlbie_lock' is not held on every path through here [-Werror,-Wthread-safety-analysis] hash_native.c:245:3: error: releasing raw_spinlock 'native_tlbie_lock' that was not held [-Werror,-Wthread-safety-analysis]

This patch reworks the tlbie() function to remove the conditional locking. It fixes the clang context analysis reported errors. However, this also simplifies the code around tlbie[l] - which is also a win.

No functional change intended.

---
 arch/powerpc/mm/book3s64/hash_native.c | 34 ++++++++++++++++++--------
 1 file changed, 24 insertions(+), 10 deletions(-)
arch/powerpc/mm/book3s64/hash_native.c +24 -10
index ab2a80e59011..6bad0545b57a 100644
@@ -222,6 +222,23 @@ static inline void __tlbiel(unsigned long vpn, int psize, int apsize, int ssize)
 
 }
 
+static inline void __tlbie_sequence(unsigned long vpn, int psize, int apsize,
+				    int ssize)
+{
+	asm volatile("ptesync": : :"memory");
+	__tlbie(vpn, psize, apsize, ssize);
+	fixup_tlbie_vpn(vpn, psize, apsize, ssize);
+	asm volatile("eieio; tlbsync; ptesync": : :"memory");
+}
+
+static inline void __tlbiel_sequence(unsigned long vpn, int psize, int apsize,
+				     int ssize)
+{
+	asm volatile("ptesync": : :"memory");
+	__tlbiel(vpn, psize, apsize, ssize);
+	ppc_after_tlbiel_barrier();
+}
+
 static inline void tlbie(unsigned long vpn, int psize, int apsize,
 			 int ssize, int local)
 {
@@ -230,19 +247,16 @@ static inline void tlbie(unsigned long vpn, int psize, int apsize,
 
 	if (use_local)
 		use_local = mmu_psize_defs[psize].tlbiel;
-	if (lock_tlbie && !use_local)
-		raw_spin_lock(&native_tlbie_lock);
-	asm volatile("ptesync": : :"memory");
+
 	if (use_local) {
-		__tlbiel(vpn, psize, apsize, ssize);
-		ppc_after_tlbiel_barrier();
+		__tlbiel_sequence(vpn, psize, apsize, ssize);
+	} else if (lock_tlbie) {
+		raw_spin_lock(&native_tlbie_lock);
+		__tlbie_sequence(vpn, psize, apsize, ssize);
+		raw_spin_unlock(&native_tlbie_lock);
 	} else {
-		__tlbie(vpn, psize, apsize, ssize);
-		fixup_tlbie_vpn(vpn, psize, apsize, ssize);
-		asm volatile("eieio; tlbsync; ptesync": : :"memory");
+		__tlbie_sequence(vpn, psize, apsize, ssize);
 	}
-	if (lock_tlbie && !use_local)
-		raw_spin_unlock(&native_tlbie_lock);
 }
 
 static inline void native_lock_hpte(struct hash_pte *hptep)
Signature · 1 line
2.39.5
author reviewer
#3 lore raw

PATCH v3 3/19 powerpc/64s/hash: Reference all arguments in the 4K pte_iterate_hashed_subpages()

In reply to Ritesh Harjani (IBM)

With 4K pages there is exactly one HPTE per PTE, so pte_iterate_hashed_subpages() only sets index and shift and does not look at rpte at all, whereas 64K version reads all three.

Reference all variables in pte_iterate_hashed_subpages() even for the 4K version, to avoid -Wunused-but-set-variable warning.

No functional change.

---
 arch/powerpc/include/asm/book3s/64/hash-4k.h | 4 ++++
 1 file changed, 4 insertions(+)
arch/powerpc/include/asm/book3s/64/hash-4k.h +4 -0
index 8e5bd9902bed..179181870e71 100644
@@ -89,10 +89,14 @@ static inline unsigned long __rpte_to_hidx(real_pte_t rpte, unsigned long index)
 	return pte_val(__rpte_to_pte(rpte)) >> H_PAGE_F_GIX_SHIFT;
 }
 
+/* references all variables to avoid -Wunused-but-set-variable warning */
 #define pte_iterate_hashed_subpages(rpte, psize, va, index, shift)       \
 	do {							         \
 		index = 0;					         \
 		shift = mmu_psize_defs[psize].shift;		         \
+		(void)(rpte);					         \
+		(void)(index);					         \
+		(void)(shift);					         \
 
 #define pte_iterate_hashed_end() } while(0)
 
Signature · 1 line
2.39.5
author reviewer
#4 lore raw

PATCH v3 4/19 powerpc/mm/book3s64: Rework native_flush_hash_range() to remove conditional locking

In reply to Ritesh Harjani (IBM)

Clang's context analysis reports:
hash_native.c:851:2: error: raw_spinlock 'native_tlbie_lock' is not held on every path through here [-Werror,-Wthread-safety-analysis] hash_native.c:868:3: error: releasing raw_spinlock 'native_tlbie_lock' that was not held [-Werror,-Wthread-safety-analysis]

This patch reworks the native_flush_hash_range() function to remove the conditional locking. It fixes the clang context analysis reported errors. However, this also simplifies the code in native_flush_hash_range() due to various if conditions.

No functional change intended.

---
 arch/powerpc/mm/book3s64/hash_native.c | 88 ++++++++++++++++----------
 1 file changed, 54 insertions(+), 34 deletions(-)
arch/powerpc/mm/book3s64/hash_native.c +54 -34
index 6bad0545b57a..2a852dd5049f 100644
@@ -792,6 +792,54 @@ static notrace void native_hpte_clear(void)
 	asm volatile("eieio; tlbsync; ptesync":::"memory");
 }
 
+static void native_flush_hash_range_tlbiel(struct ppc64_tlb_batch *batch,
+					   unsigned long number)
+{
+	unsigned long vpn = 0, index, shift;
+	real_pte_t pte;
+	unsigned long psize = batch->psize;
+	int ssize = batch->ssize;
+	int i;
+
+	asm volatile("ptesync":::"memory");
+	for (i = 0; i < number; i++) {
+		vpn = batch->vpn[i];
+		pte = batch->pte[i];
+
+		pte_iterate_hashed_subpages(pte, psize,
+					    vpn, index, shift) {
+			__tlbiel(vpn, psize, psize, ssize);
+		} pte_iterate_hashed_end();
+	}
+	ppc_after_tlbiel_barrier();
+}
+
+static void native_flush_hash_range_tlbie(struct ppc64_tlb_batch *batch,
+					  unsigned long number)
+{
+	unsigned long vpn = 0, index, shift;
+	real_pte_t pte;
+	unsigned long psize = batch->psize;
+	int ssize = batch->ssize;
+	int i;
+
+	asm volatile("ptesync":::"memory");
+	for (i = 0; i < number; i++) {
+		vpn = batch->vpn[i];
+		pte = batch->pte[i];
+
+		pte_iterate_hashed_subpages(pte, psize,
+					    vpn, index, shift) {
+			__tlbie(vpn, psize, psize, ssize);
+		} pte_iterate_hashed_end();
+	}
+	/*
+	 * Just do one more with the last used values.
+	 */
+	fixup_tlbie_vpn(vpn, psize, psize, ssize);
+	asm volatile("eieio; tlbsync; ptesync":::"memory");
+}
+
 /*
  * Batched hash table flush, we batch the tlbie's to avoid taking/releasing
  * the lock all the time
@@ -845,41 +893,13 @@ static void native_flush_hash_range(unsigned long number, int local)
 
 	if (mmu_has_feature(MMU_FTR_TLBIEL) &&
 	    mmu_psize_defs[psize].tlbiel && local) {
-		asm volatile("ptesync":::"memory");
-		for (i = 0; i < number; i++) {
-			vpn = batch->vpn[i];
-			pte = batch->pte[i];
-
-			pte_iterate_hashed_subpages(pte, psize,
-						    vpn, index, shift) {
-				__tlbiel(vpn, psize, psize, ssize);
-			} pte_iterate_hashed_end();
-		}
-		ppc_after_tlbiel_barrier();
+		native_flush_hash_range_tlbiel(batch, number);
+	} else if (!mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE)) {
+		raw_spin_lock(&native_tlbie_lock);
+		native_flush_hash_range_tlbie(batch, number);
+		raw_spin_unlock(&native_tlbie_lock);
 	} else {
-		int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
-
-		if (lock_tlbie)
-			raw_spin_lock(&native_tlbie_lock);
-
-		asm volatile("ptesync":::"memory");
-		for (i = 0; i < number; i++) {
-			vpn = batch->vpn[i];
-			pte = batch->pte[i];
-
-			pte_iterate_hashed_subpages(pte, psize,
-						    vpn, index, shift) {
-				__tlbie(vpn, psize, psize, ssize);
-			} pte_iterate_hashed_end();
-		}
-		/*
-		 * Just do one more with the last used values.
-		 */
-		fixup_tlbie_vpn(vpn, psize, psize, ssize);
-		asm volatile("eieio; tlbsync; ptesync":::"memory");
-
-		if (lock_tlbie)
-			raw_spin_unlock(&native_tlbie_lock);
+		native_flush_hash_range_tlbie(batch, number);
 	}
 
 	local_irq_restore(flags);
Signature · 1 line
2.39.5
author reviewer
#5 lore raw

PATCH v3 5/19 powerpc/mm/book3s64: Annotate the subpage-protection PTE lock

In reply to Ritesh Harjani (IBM)

Clang's context analysis reports:
subpage_prot.c:83:2: error: releasing spinlock 'ptl' that was not held [-Werror,-Wthread-safety-analysis]

pte_offset_map_lock() holds ptl when it returns a PTE and pte_unmap_unlock() drops it. However since the lock is taken inside the helper, which is not annotated, so the unlock in hpte_flush_range() looks unheld.

This patch fixes it in hpte_flush_range() by adding the needed annotation here. IMO, should ideally be added to pte_offset_map_lock(), once that happens (since it might need larger enablement), we can remove it from here.

---
 arch/powerpc/mm/book3s64/subpage_prot.c | 2 ++
 1 file changed, 2 insertions(+)
arch/powerpc/mm/book3s64/subpage_prot.c +2 -0
index 37d47282c368..d641c35c408d 100644
@@ -73,6 +73,8 @@ static void hpte_flush_range(struct mm_struct *mm, unsigned long addr,
 	pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
 	if (!pte)
 		return;
+	/* pte_offset_map_lock on success returns with ptl lock held */
+	__acquire(ptl);
 	lazy_mmu_mode_enable();
 	for (; npages > 0; --npages) {
 		pte_update(mm, addr, pte, 0, 0, 0);
Signature · 1 line
2.39.5
author reviewer
#6 lore raw

PATCH v3 6/19 powerpc/mm: Rework nohash TLB broadcast locking

In reply to Ritesh Harjani (IBM)

Clang's context analysis reports:
mmu_context.c:141:2: error: releasing raw_spinlock 'context_lock' that was not held [-Werror,-Wthread-safety-analysis] mmu_context.c:147:1: error: raw_spinlock 'context_lock' is not held on every path through here [-Werror,-Wthread-safety-analysis]

tlb.c:257:4: error: raw_spinlock 'tlbivax_lock' is not held on every path through here [-Werror,-Wthread-safety-analysis] tlb.c:259:5: error: releasing raw_spinlock 'tlbivax_lock' that was not held [-Werror,-Wthread-safety-analysis]

This reworks the locking in __flush_tlb_page() a bit to satisfy clang context analyzer requirement.
Also steal_context_smp() drops the context_lock, so it needs to be annotated with __must_hold to inform the clang analyzer that this function holds the context_lock.

---
 arch/powerpc/mm/nohash/mmu_context.c | 1 +
 arch/powerpc/mm/nohash/tlb.c         | 8 +++++---
 2 files changed, 6 insertions(+), 3 deletions(-)
arch/powerpc/mm/nohash/mmu_context.c +1 -0
index 28a96a10c907..bdbe1f55f388 100644
@@ -95,6 +95,7 @@ static DEFINE_RAW_SPINLOCK(context_lock);
  *  -- benh
  */
 static unsigned int steal_context_smp(unsigned int id)
+			__must_hold(&context_lock)
 {
 	struct mm_struct *mm;
 	unsigned int cpu, max, i;
arch/powerpc/mm/nohash/tlb.c +5 -3
index 07a2db16c2b1..9297549aef41 100644
@@ -252,11 +252,13 @@ void __flush_tlb_page(struct mm_struct *mm, unsigned long vmaddr,
 		/* If broadcast tlbivax is supported, use it */
 		if (mmu_has_feature(MMU_FTR_USE_TLBIVAX_BCAST)) {
 			int lock = mmu_has_feature(MMU_FTR_LOCK_BCAST_INVAL);
-			if (lock)
+			if (lock) {
 				raw_spin_lock(&tlbivax_lock);
-			_tlbivax_bcast(vmaddr, pid, tsize, ind);
-			if (lock)
+				_tlbivax_bcast(vmaddr, pid, tsize, ind);
 				raw_spin_unlock(&tlbivax_lock);
+			} else {
+				_tlbivax_bcast(vmaddr, pid, tsize, ind);
+			}
 			goto bail;
 		} else {
 			struct tlb_flush_param p = {
Signature · 1 line
2.39.5
author reviewer
#7 lore raw

PATCH v3 7/19 powerpc/mm: Enable context analysis

In reply to Ritesh Harjani (IBM)

powerpc/mm/ objects now build with Clang context analysis enabled. Turn it on for book3s32, book3s64 and nohash mmu.

---
 arch/powerpc/mm/Makefile          | 2 ++
 arch/powerpc/mm/book3s32/Makefile | 2 ++
 arch/powerpc/mm/book3s64/Makefile | 2 ++
 arch/powerpc/mm/nohash/Makefile   | 2 ++
 4 files changed, 8 insertions(+)
arch/powerpc/mm/Makefile +2 -0
index 8c1582b2987d..33e51abb5aac 100644
@@ -3,6 +3,8 @@
 # Makefile for the linux ppc-specific parts of the memory manager.
 #
 
+CONTEXT_ANALYSIS		:= y
+
 obj-y				:= fault.o mem.o pgtable.o maccess.o pageattr.o \
 				   init_$(BITS).o pgtable_$(BITS).o \
 				   pgtable-frag.o ioremap.o ioremap_$(BITS).o \
arch/powerpc/mm/book3s32/Makefile +2 -0
index 50dd8f6bdf46..d5069a586c7b 100644
@@ -1,5 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0
 
+CONTEXT_ANALYSIS	:= y
+
 KASAN_SANITIZE_mmu.o := n
 
 ifdef CONFIG_KASAN
arch/powerpc/mm/book3s64/Makefile +2 -0
index 33af5795856a..c5d8d2ef72ac 100644
@@ -1,5 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0
 
+CONTEXT_ANALYSIS		:= y
+
 obj-y				+= mmu_context.o pgtable.o trace.o
 ifdef CONFIG_PPC_64S_HASH_MMU
 CFLAGS_REMOVE_slb.o = $(CC_FLAGS_FTRACE)
arch/powerpc/mm/nohash/Makefile +2 -0
index cf60c776c883..7d7409d2cf97 100644
@@ -1,5 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0
 
+CONTEXT_ANALYSIS		:= y
+
 obj-y				+= mmu_context.o tlb.o tlb_low.o kup.o
 obj-$(CONFIG_PPC_BOOK3E_64)  	+= tlb_64e.o tlb_low_64e.o book3e_pgtable.o
 obj-$(CONFIG_44x)		+= 44x.o
Signature · 1 line
2.39.5
author reviewer
#8 lore raw

PATCH v3 8/19 powerpc/pseries: Drop inline from hugepage invalidate

In reply to Ritesh Harjani (IBM)

Leave __pSeries_lpar_hugepage_invalidate() static and let the compiler decide whether to inline it.

---
 arch/powerpc/platforms/pseries/lpar.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/platforms/pseries/lpar.c +1 -1
index 6a415febc53b..94cd30e9f6e1 100644
@@ -1225,7 +1225,7 @@ static void hugepage_bulk_invalidate(unsigned long *slot, unsigned long *vpn,
 	}
 }
 
-static inline void __pSeries_lpar_hugepage_invalidate(unsigned long *slot,
+static void __pSeries_lpar_hugepage_invalidate(unsigned long *slot,
 						      unsigned long *vpn,
 						      int count, int psize,
 						      int ssize)
Signature · 1 line
2.39.5
author reviewer
#9 lore raw

PATCH v3 9/19 powerpc/pseries: Rework hugepage invalidate to remove conditional locking

In reply to Ritesh Harjani (IBM)

Clang's compiler based static context analysis does not work with locks that are conditionally taken like in __pSeries_lpar_hugepage_invalidate().

lpar.c:1240:6: error: spinlock 'pSeries_lpar_tlbie_lock' is not held on every path through here [-Werror,-Wthread-safety-analysis]

But this also simplifies the function definition in my opinion - so this is also a win.

No functional change intended.

---
 arch/powerpc/platforms/pseries/lpar.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
arch/powerpc/platforms/pseries/lpar.c +16 -9
index 94cd30e9f6e1..abd77c45cbf4 100644
@@ -1224,26 +1224,33 @@ static void hugepage_bulk_invalidate(unsigned long *slot, unsigned long *vpn,
 		BUG_ON(rc != H_SUCCESS);
 	}
 }
-
-static void __pSeries_lpar_hugepage_invalidate(unsigned long *slot,
+static void __pSeries_lpar_hugepage_invalidate_unlocked(unsigned long *slot,
 						      unsigned long *vpn,
 						      int count, int psize,
 						      int ssize)
 {
-	unsigned long flags = 0;
-	int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
-
-	if (lock_tlbie)
-		spin_lock_irqsave(&pSeries_lpar_tlbie_lock, flags);
-
 	/* Assuming THP size is 16M */
 	if (is_supported_hlbkrm(psize, MMU_PAGE_16M))
 		hugepage_block_invalidate(slot, vpn, count, psize, ssize);
 	else
 		hugepage_bulk_invalidate(slot, vpn, count, psize, ssize);
+}
 
-	if (lock_tlbie)
+static void __pSeries_lpar_hugepage_invalidate(unsigned long *slot,
+						      unsigned long *vpn,
+						      int count, int psize,
+						      int ssize)
+{
+	unsigned long flags = 0;
+
+	if (!mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE)) {
+		spin_lock_irqsave(&pSeries_lpar_tlbie_lock, flags);
+		__pSeries_lpar_hugepage_invalidate_unlocked(slot, vpn, count, psize, ssize);
 		spin_unlock_irqrestore(&pSeries_lpar_tlbie_lock, flags);
+		return;
+	}
+
+	__pSeries_lpar_hugepage_invalidate_unlocked(slot, vpn, count, psize, ssize);
 }
 
 static void pSeries_lpar_hugepage_invalidate(unsigned long vsid,
Signature · 1 line
2.39.5
author reviewer
#10 lore raw

PATCH v3 10/19 powerpc/pseries: Rework hash flush to remove conditional locking

In reply to Ritesh Harjani (IBM)

Clang's compiler based static context analysis does not work with locks that are conditionally taken like in pSeries_lpar_flush_hash_range().

This also simplifies the function definition in my opinion - so this is also a win.

No functional change intended.

---
 arch/powerpc/platforms/pseries/lpar.c | 31 ++++++++++++++++-----------
 1 file changed, 18 insertions(+), 13 deletions(-)
arch/powerpc/platforms/pseries/lpar.c +18 -13
index abd77c45cbf4..5dbf447d3a83 100644
@@ -1526,28 +1526,19 @@ void __init pseries_lpar_read_hblkrm_characteristics(void)
 					bpsize, idx, hblkrm_size[bpsize][idx]);
 }
 
-/*
- * Take a spinlock around flushes to avoid bouncing the hypervisor tlbie
- * lock.
- */
-static void pSeries_lpar_flush_hash_range(unsigned long number, int local)
+static void pSeries_lpar_flush_hash_range_unlocked(unsigned long number, int local)
 {
 	unsigned long vpn;
 	unsigned long i, pix, rc;
-	unsigned long flags = 0;
 	struct ppc64_tlb_batch *batch = this_cpu_ptr(&ppc64_tlb_batch);
-	int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
 	unsigned long param[PLPAR_HCALL9_BUFSIZE];
 	unsigned long index, shift, slot;
 	real_pte_t pte;
 	int psize, ssize;
 
-	if (lock_tlbie)
-		spin_lock_irqsave(&pSeries_lpar_tlbie_lock, flags);
-
 	if (is_supported_hlbkrm(batch->psize, batch->psize)) {
 		do_block_remove(number, batch, param);
-		goto out;
+		return;
 	}
 
 	psize = batch->psize;
@@ -1587,10 +1578,24 @@ static void pSeries_lpar_flush_hash_range(unsigned long number, int local)
 				  param[6], param[7]);
 		BUG_ON(rc != H_SUCCESS);
 	}
+}
 
-out:
-	if (lock_tlbie)
+/*
+ * Take a spinlock around flushes to avoid bouncing the hypervisor tlbie
+ * lock. Firmware with MMU_FTR_LOCKLESS_TLBIE does not need it.
+ */
+static void pSeries_lpar_flush_hash_range(unsigned long number, int local)
+{
+	unsigned long flags;
+
+	if (!mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE)) {
+		spin_lock_irqsave(&pSeries_lpar_tlbie_lock, flags);
+		pSeries_lpar_flush_hash_range_unlocked(number, local);
 		spin_unlock_irqrestore(&pSeries_lpar_tlbie_lock, flags);
+		return;
+	}
+
+	pSeries_lpar_flush_hash_range_unlocked(number, local);
 }
 
 static int __init disable_bulk_remove(char *str)
Signature · 1 line
2.39.5
author reviewer
#11 lore raw

PATCH v3 11/19 powerpc/pseries: Add context annotations for the DTL worker lock

In reply to Ritesh Harjani (IBM)

dtl_worker_{enable,disable}() needs annotations since they acquire and release locks. So this adds conditional annotation based on return value.

Let's also disable context analysis for vcpudispatch_stats_write(), since based on received input, it can leave the lock held until a later write of 0 unlocks it. So we need to mark it with __context_unsafe().

Otherwise we get following error on enabling CONTEXT_ANALYSIS on this
file:
lpar.c:490:9: error: rw_semaphore 'dtl_access_lock' is not held on every path through here [-Werror,-Wthread-safety-analysis] lpar.c:498:2: error: releasing rw_semaphore 'dtl_access_lock' that was not held [-Werror,-Wthread-safety-analysis]

---
 arch/powerpc/platforms/pseries/lpar.c | 3 +++
 1 file changed, 3 insertions(+)
arch/powerpc/platforms/pseries/lpar.c +3 -0
index 5dbf447d3a83..9965ead1e93c 100644
@@ -461,6 +461,7 @@ static void reset_global_dtl_mask(void)
 }
 
 static int dtl_worker_enable(unsigned long *time_limit)
+		__cond_acquires(0, &dtl_access_lock)
 {
 	int rc = 0, state;
 
@@ -491,6 +492,7 @@ static int dtl_worker_enable(unsigned long *time_limit)
 }
 
 static void dtl_worker_disable(unsigned long *time_limit)
+		__releases(&dtl_access_lock)
 {
 	cpuhp_remove_state(dtl_worker_state);
 	free_dtl_buffers(time_limit);
@@ -500,6 +502,7 @@ static void dtl_worker_disable(unsigned long *time_limit)
 
 static ssize_t vcpudispatch_stats_write(struct file *file, const char __user *p,
 		size_t count, loff_t *ppos)
+		__context_unsafe(/* dtl_access_lock is held from enable write until disable write */)
 {
 	unsigned long time_limit = jiffies + HZ;
 	struct vcpu_dispatch_data *disp;
Signature · 1 line
2.39.5
author reviewer
#12 lore raw

PATCH v3 12/19 powerpc/pseries: Annotate the VPD sequence lock context

In reply to Ritesh Harjani (IBM)

Clang's context analysis reports:
papr-vpd.c:151:1: error: mutex 'rtas_ibm_get_vpd_lock' is still held at the end of function [-Werror,-Wthread-safety-analysis] papr-vpd.c:165:2: error: releasing mutex 'rtas_ibm_get_vpd_lock' that was not held [-Werror,-Wthread-safety-analysis]

Add the necessary annotations to in vpd_sequence_{begin|end}(), to fix these errors for enabling Clang context analysis.

---
 arch/powerpc/platforms/pseries/papr-vpd.c | 3 +++
 1 file changed, 3 insertions(+)
arch/powerpc/platforms/pseries/papr-vpd.c +3 -0
index f38c188fc4a1..8d210454cff1 100644
@@ -123,8 +123,10 @@ static int rtas_ibm_get_vpd(struct rtas_ibm_get_vpd_params *params)
  * @seq: vpd call parameters from sequence struct
  *
  * Context: May sleep.
+ * Returns with rtas_ibm_get_vpd_lock held. vpd_sequence_end() releases it.
  */
 static void vpd_sequence_begin(struct papr_rtas_sequence *seq)
+		__acquires(&rtas_ibm_get_vpd_lock)
 {
 	struct rtas_ibm_get_vpd_params *vpd_params;
 	/*
@@ -157,6 +159,7 @@ static void vpd_sequence_begin(struct papr_rtas_sequence *seq)
  * Releases resources obtained by vpd_sequence_begin().
  */
 static void vpd_sequence_end(struct papr_rtas_sequence *seq)
+		__releases(&rtas_ibm_get_vpd_lock)
 {
 	struct rtas_ibm_get_vpd_params *vpd_params;
 
Signature · 1 line
2.39.5
author reviewer
#13 lore raw

PATCH v3 13/19 powerpc/pseries: Annotate the RTAS indices sequence lock context

In reply to Ritesh Harjani (IBM)

Clang's context analysis reports:
papr-indices.c:145:1: error: mutex 'rtas_ibm_get_indices_lock' is still held at the end of function [-Werror,-Wthread-safety-analysis] papr-indices.c:158:2: error: releasing mutex 'rtas_ibm_get_indices_lock' that was not held [-Werror,-Wthread-safety-analysis]

Add the necessary annotations to indices_sequence_{begin|end}(), to fix these errors for enabling Clang context analysis.

---
 arch/powerpc/platforms/pseries/papr-indices.c | 3 +++
 1 file changed, 3 insertions(+)
arch/powerpc/platforms/pseries/papr-indices.c +3 -0
index 3c7545591c45..3f3b88f708a3 100644
@@ -126,8 +126,10 @@ static int rtas_ibm_get_indices(struct rtas_get_indices_params *params)
  * indices_sequence_begin() - Begin a indices retrieval sequence.
  *
  * Context: May sleep.
+ * Returns with rtas_ibm_get_indices_lock held. indices_sequence_end() releases it.
  */
 static void indices_sequence_begin(struct papr_rtas_sequence *seq)
+		__acquires(&rtas_ibm_get_indices_lock)
 {
 	struct rtas_get_indices_params  *param;
 
@@ -150,6 +152,7 @@ static void indices_sequence_begin(struct papr_rtas_sequence *seq)
  * Releases resources obtained by indices_sequence_begin().
  */
 static void indices_sequence_end(struct papr_rtas_sequence *seq)
+		__releases(&rtas_ibm_get_indices_lock)
 {
 	struct rtas_get_indices_params *param;
 
Signature · 1 line
2.39.5
author reviewer
#14 lore raw

PATCH v3 14/19 powerpc/pseries: Annotate the RTAS physical-attestation sequence lock context

In reply to Ritesh Harjani (IBM)

Clang's context analysis reports:
papr-phy-attest.c:154:1: error: mutex 'rtas_ibm_physical_attestation_lock' is still held at the end of function [-Werror,-Wthread-safety-analysis] papr-phy-attest.c:169:2: error: releasing mutex 'rtas_ibm_physical_attestation_lock' that was not held [-Werror,-Wthread-safety-analysis]

Add the necessary annotations to phy_attest_sequence_{begin|end}(), to fix these errors for enabling Clang context analysis.

---
 arch/powerpc/platforms/pseries/papr-phy-attest.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/powerpc/platforms/pseries/papr-phy-attest.c +4 -0
index 350ba26e5962..d2bc5c0da422 100644
@@ -133,8 +133,11 @@ static int rtas_physical_attestation(struct rtas_phy_attest_params *params)
  * @seq: user specified parameters for RTAS call from seq struct.
  *
  * Context: May sleep.
+ * Returns with rtas_ibm_physical_attestation_lock held.
+ * phy_attest_sequence_end() releases it.
  */
 static void phy_attest_sequence_begin(struct papr_rtas_sequence *seq)
+		__acquires(&rtas_ibm_physical_attestation_lock)
 {
 	struct rtas_phy_attest_params *param;
 
@@ -161,6 +164,7 @@ static void phy_attest_sequence_begin(struct papr_rtas_sequence *seq)
  * Releases resources obtained by phy_attest_sequence_begin().
  */
 static void phy_attest_sequence_end(struct papr_rtas_sequence *seq)
+		__releases(&rtas_ibm_physical_attestation_lock)
 {
 	struct rtas_phy_attest_params *param;
 
Signature · 1 line
2.39.5
author reviewer
#15 lore raw

PATCH v3 15/19 powerpc/pseries: Annotate the DTL debugfs read lock context

In reply to Ritesh Harjani (IBM)

Clang's context analysis reports:
dtl.c: error: rw_semaphore 'dtl_access_lock' is still held at the end of function [-Werror,-Wthread-safety-analysis] dtl.c: error: releasing rw_semaphore 'dtl_access_lock' that was not held [-Werror,-Wthread-safety-analysis]

Add the necessary annotations to

	dtl_{enable|disable}(), dtl_file{open|release}(),

to fix these errors for enabling Clang context analysis.

---
 arch/powerpc/platforms/pseries/dtl.c | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/powerpc/platforms/pseries/dtl.c +8 -0
index f293588b8c7b..18c597210103 100644
@@ -177,7 +177,12 @@ static u64 dtl_current_index(struct dtl *dtl)
 }
 #endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
 
+/*
+ * On success the read lock stays held until dtl_disable(). That covers
+ * the whole time the debugfs file is open.
+ */
 static int dtl_enable(struct dtl *dtl)
+		__cond_acquires_shared(0, &dtl_access_lock)
 {
 	long int n_entries;
 	long int rc;
@@ -225,6 +230,7 @@ static int dtl_enable(struct dtl *dtl)
 }
 
 static void dtl_disable(struct dtl *dtl)
+		__releases_shared(&dtl_access_lock)
 {
 	spin_lock(&dtl->lock);
 	dtl_stop(dtl);
@@ -238,6 +244,7 @@ static void dtl_disable(struct dtl *dtl)
 /* file interface */
 
 static int dtl_file_open(struct inode *inode, struct file *filp)
+		__cond_acquires_shared(0, &dtl_access_lock)
 {
 	struct dtl *dtl = inode->i_private;
 	int rc;
@@ -251,6 +258,7 @@ static int dtl_file_open(struct inode *inode, struct file *filp)
 }
 
 static int dtl_file_release(struct inode *inode, struct file *filp)
+		__releases_shared(&dtl_access_lock)
 {
 	struct dtl *dtl = inode->i_private;
 	dtl_disable(dtl);
Signature · 1 line
2.39.5
author reviewer
#16 lore raw

PATCH v3 16/19 powerpc/pseries: Disable context analysis for the CMM hotplug notifier

In reply to Ritesh Harjani (IBM)

Clang's context analysis reports:
cmm.c: error: mutex 'hotplug_mutex' is still held at the end of function [-Werror,-Wthread-safety-analysis] cmm.c: error: releasing mutex 'hotplug_mutex' that was not held [-Werror,-Wthread-safety-analysis]

cmm_memory_cb() takes hotplug_mutex on MEM_GOING_OFFLINE and drops it on a later MEM_OFFLINE or MEM_CANCEL_OFFLINE call.

Add __context_unsafe() annotation for cmm_memory_cb() callback to avoid the reported build error in order to enable Clang context analysis.

---
 arch/powerpc/platforms/pseries/cmm.c | 1 +
 1 file changed, 1 insertion(+)
arch/powerpc/platforms/pseries/cmm.c +1 -0
index 38e22125b96f..159efe734785 100644
@@ -470,6 +470,7 @@ static struct notifier_block cmm_reboot_nb = {
  **/
 static int cmm_memory_cb(struct notifier_block *self,
 			unsigned long action, void *arg)
+		__context_unsafe(/* Conditional locking */)
 {
 	switch (action) {
 	case MEM_GOING_OFFLINE:
Signature · 1 line
2.39.5
author reviewer
#17 lore raw

PATCH v3 17/19 powerpc/pseries: Enable context analysis

In reply to Ritesh Harjani (IBM)

pseries objects now build with Clang context analysis enabled. Turn it on for the directory.

This checks that each lock taken in this code is dropped on every path.

---
 arch/powerpc/platforms/pseries/Makefile | 3 +++
 1 file changed, 3 insertions(+)
arch/powerpc/platforms/pseries/Makefile +3 -0
index 3ced289a675b..d5f9beec33cc 100644
@@ -1,4 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0
+
+CONTEXT_ANALYSIS			:= y
+
 ccflags-$(CONFIG_PPC_PSERIES_DEBUG)	+= -DDEBUG
 
 obj-y			:= lpar.o hvCall.o nvram.o reconfig.o \
Signature · 1 line
2.39.5
author reviewer
#18 lore raw

PATCH v3 18/19 powerpc/powernv: Rework OPAL console write to remove conditional locking

In reply to Ritesh Harjani (IBM)

Clang's context analysis reports:
opal.c:454:7: error: spinlock 'opal_write_lock' is not held on every path through here [-Werror,-Wthread-safety-analysis] opal.c:493:3: error: releasing spinlock 'opal_write_lock' that was not held [-Werror,-Wthread-safety-analysis]

This patch rework the OPAL console write function to remove this conditional locking to fix the reported build errors while enabling Clang context analysis. We could have marked __context_unsafe(), but this anyways simplifies the locking a bit.

---
 arch/powerpc/platforms/powernv/opal.c | 30 ++++++++++++++++++---------
 1 file changed, 20 insertions(+), 10 deletions(-)
arch/powerpc/platforms/powernv/opal.c +20 -10
index 1946dbdc9fa1..aea83c4be76d 100644
@@ -438,19 +438,13 @@ ssize_t opal_get_chars(uint32_t vtermno, u8 *buf, size_t count)
 	return 0;
 }
 
-static ssize_t __opal_put_chars(uint32_t vtermno, const u8 *data,
-				size_t total_len, bool atomic)
+static ssize_t __opal_put_chars_unlocked(uint32_t vtermno, const u8 *data,
+					 size_t total_len, bool atomic)
 {
-	unsigned long flags = 0 /* shut up gcc */;
 	ssize_t written;
 	__be64 olen;
 	s64 rc;
 
-	if (!opal.entry)
-		return -ENODEV;
-
-	if (atomic)
-		spin_lock_irqsave(&opal_write_lock, flags);
 	rc = opal_console_write_buffer_space(vtermno, &olen);
 	if (rc || be64_to_cpu(olen) < total_len) {
 		/* Closed -> drop characters */
@@ -489,10 +483,26 @@ static ssize_t __opal_put_chars(uint32_t vtermno, const u8 *data,
 	}
 
 out:
-	if (atomic)
+	return written;
+}
+
+static ssize_t __opal_put_chars(uint32_t vtermno, const u8 *data,
+				size_t total_len, bool atomic)
+{
+	unsigned long flags = 0;
+	ssize_t written;
+
+	if (!opal.entry)
+		return -ENODEV;
+
+	if (atomic) {
+		spin_lock_irqsave(&opal_write_lock, flags);
+		written = __opal_put_chars_unlocked(vtermno, data, total_len, true);
 		spin_unlock_irqrestore(&opal_write_lock, flags);
+		return written;
+	}
 
-	return written;
+	return __opal_put_chars_unlocked(vtermno, data, total_len, false);
 }
 
 ssize_t opal_put_chars(uint32_t vtermno, const u8 *data, size_t total_len)
Signature · 1 line
2.39.5
author reviewer
#19 lore raw

PATCH v3 19/19 powerpc/powernv: Enable context analysis

In reply to Ritesh Harjani (IBM)

powernv objects now build with Clang context analysis enabled. Turn it on for the directory.

This checks that each lock taken in this code is dropped on every path.

---
 arch/powerpc/platforms/powernv/Makefile | 2 ++
 1 file changed, 2 insertions(+)
arch/powerpc/platforms/powernv/Makefile +2 -0
index 9e5d0c847ee2..834a56122aaa 100644
@@ -1,5 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0
 
+CONTEXT_ANALYSIS	:= y
+
 # nothing that deals with real mode is safe to KASAN
 # in particular, idle code runs a bunch of things in real mode
 KASAN_SANITIZE_idle.o := n
Signature · 1 line
2.39.5