Skip to contentSkip to conversation

R3k and swap: Still no go?

PATCH New Jan-Benedict Glaw 2 participants 3 messages Feb 22, 2002 – Mar 4, 2002 MIPS

lore
Share & tools
lore
#0 lore raw

Hi!

A year or two ago we've had a thread about r3k and swap. Currently, the box will oops... There's a patch changing some magic numbers in include/asm-mips/pgtable.h (attached against current CVS), but it is not really acceptable: some numbers are changed but nobody seems to know *what* this does mean. Ralf even mentioned that there might be some bug in the whole paging mechanism hiding behind this.

Flo looked into this and his resume was that even the current (oopsing) values *should* do the job IIRC. Maciej, do you have a hint on this? I'm now using this patch, because it does make the kernel useable, but I don't understand why...

MfG, JBG

Signature · 3 lines
Jan-Benedict Glaw   .   [email protected]   .   +49-172-7608481
	 -- New APT-Proxy written in shell script --
	   http://lug-owl.de/~jbglaw/software/ap2/
include/asm-mips/pgtable.h +10 -3
@@ -518,9 +518,16 @@
 extern void update_mmu_cache(struct vm_area_struct *vma,
 				unsigned long address, pte_t pte);
 
-#define SWP_TYPE(x)		(((x).val >> 1) & 0x3f)
-#define SWP_OFFSET(x)		((x).val >> 8)
-#define SWP_ENTRY(type,offset)	((swp_entry_t) { ((type) << 1) | ((offset) << 8) })
+#ifndef CONFIG_CPU_R3000
+#	define SWP_TYPE(x)		(((x).val >> 1) & 0x3f)
+#	define SWP_OFFSET(x)		((x).val >> 8)
+#	define SWP_ENTRY(type,offset)	((swp_entry_t) { ((type) << 1) | ((offset) << 8) })
+#else /* CONFIG_CPU_R3000 */
+#	define SWP_TYPE(x)			(((x).val >> 8) & 0x7f)
+#	define SWP_OFFSET(x)			((x).val >> 15)
+#	define define SWP_ENTRY(type,offset)	((swp_entry_t) { ((type) << 8) | ((offset) << 15) })
+#endif
+
 #define pte_to_swp_entry(pte)	((swp_entry_t) { pte_val(pte) })
 #define swp_entry_to_pte(x)	((pte_t) { (x).val })
 
#1 lore raw
Quoted 6 lines · Jan-Benedict Glaw

A year or two ago we've had a thread about r3k and swap. Currently, the box will oops... There's a patch changing some magic numbers in include/asm-mips/pgtable.h (attached against current CVS), but it is not really acceptable: some numbers are changed but nobody seems to know *what* this does mean. Ralf even mentioned that there might be some bug in the whole paging mechanism hiding behind this.

Indeed, for good code you need to inspect why things work after an
uncertain fix (for commercial one, you probably don't).

Quoted 4 lines

Flo looked into this and his resume was that even the current (oopsing) values *should* do the job IIRC. Maciej, do you have a hint on this? I'm now using this patch, because it does make the kernel useable, but I don't understand why...

I browsed through the kernel and it seems the fix is twiddling in the
right area, although a minor clean up seems to be needed.

include/asm-mips/pgtable.h line 529

Quoted 5 lines
include/asm-mips/pgtable.h +5 -0
+#else /* CONFIG_CPU_R3000 */
+#	define SWP_TYPE(x)			(((x).val >> 8) & 0x7f)
+#	define SWP_OFFSET(x)			((x).val >> 15)
+#	define define SWP_ENTRY(type,offset)	((swp_entry_t) { ((type) << 8) | ((offset) << 15) })
+#endif

This is an overkill -- you limit yourself to 128kB of swap per
device/file. ;-) Except that there is a typo in the SWP_ENTRY definition, so it can't work anyway.

Quoted 1 line

[ Part 1.3: "Flo's statement" ]

[...]

include/asm-mips/pgtable.h

Quoted 4 lines
include/asm-mips/pgtable.h +2 -2
-#define SWP_OFFSET(x)		((x).val >> 8)
-#define SWP_ENTRY(type,offset)	((swp_entry_t) { ((type) << 1) | ((offset) << 8) })
+#define SWP_OFFSET(x)		((x).val >> 10)
+#define SWP_ENTRY(type,offset)	((swp_entry_t) { ((type) << 1) | ((offset) << 10) })

This is a move in the right direction. I'd shift bits and mask them a
bit differently, though. The point is to have _PAGE_VALID cleared for paged-out entries.

Quoted 7 lines

The R3k machine (Decstation 5000/120) does survive swapping. The idea is that the bits 9 & 10 on the R3k are

#define _PAGE_VALID                 (1<<9)
#define _PAGE_SILENT_READ           (1<<9)  /* synonym                 */
#define _PAGE_DIRTY                 (1<<10) /* The MIPS dirty bit      */
#define _PAGE_SILENT_WRITE          (1<<10)

Yep, that's the culprit.

Quoted 6 lines

Whereas this on R4k is

#define _PAGE_VALID                 (1<<7)
#define _PAGE_SILENT_READ           (1<<7)  /* synonym                 */
#define _PAGE_DIRTY                 (1<<8)  /* The MIPS dirty bit      */
#define _PAGE_SILENT_WRITE          (1<<8)

Remember these are shifted left by six.

Quoted 3 lines

OTOH when following the code paths the pte gets completely replaced with the swap entry and not bitmasked/and/ored with the original pte or the ptes flags. So i find no dependencie to the SWP_ macros.

Well, when loaded into the TLB paged-out entries absolutely must have
their _PAGE_VALID bit cleared. Otherwise you don't get a TLBL/TLBS exception upon an address dereference and a random (not quite, but still irrelevant to the page map) page address is used instead. And as I see SWP_ENTRY is used to make swap PTE entries which are to fault upon a dereference.

Quoted 2 lines

When adding debug code there i can see the flags getting restored correctly on swapping in.

Unless _PAGE_VALID is set and you don't reach the code at all.

Actually the R4k variant seems to be broken as well -- it's only
MAX_SWAPFILES that protects _PAGE_VALID from being set here as well. Anyone knows why we don't use _PAGE_VALID as a presence mark and have separate software-maintained _PAGE_PRESENT for this purpose? Other archs don't seem to need such a mess...

I should have a patch ready soon.

Signature · 3 lines
+  Maciej W. Rozycki, Technical University of Gdansk, Poland   +
+--------------------------------------------------------------+
+        e-mail: [email protected], PGP key available        +
#2 lore raw

PATCH 1/1 Critical swapping problems

Hello,

After studying dependencies I concluded the Flo's patch for R3k swapping
is OK -- it correctly zeroes the valid and global bits of swapped-out PTEs. Additionally the R4k+ version didn't zero the global bit.

Here is a patch I successfully tested using an NVRAM and a RAM-disk as
swap devices on my R3k system. I took quite some time, but weird Oopses and init crashes were distracting me. It turned out these were signal delivery bugs (or at least that's what I suspect) that were not directly related to the patch but to the test environment I created (the bugs are still a mystery and are under investigation) -- without the patch they happen as well, usually under conditions near OOM. The patch consists mostly of the Flo's changes with R4k+ bits as well as comment updates added by me.

Given enough virtual memory the patch works very well -- I've run `tar
-jxf glibc-2.2.5.tar.bz2; rm -rf glibc-2.2.5' for a few hours in a loop which with RAM clipped to 16MB and 32MB of swap gives a considerable swap activity, with no problems noticed.

Ralf, the patch is *CRITICAL* for R3k -- please apply it as soon as
possible. The R4k+ update is not so important as MAX_SWAPFILES protects us, but it should go in anyway for correctness.

Maciej

Signature · 37 lines
+  Maciej W. Rozycki, Technical University of Gdansk, Poland   +
+--------------------------------------------------------------+
+        e-mail: [email protected], PGP key available        +

patch-mips-2.4.17-20020129-swap-2
diff -up --recursive --new-file linux-mips-2.4.17-20020129.macro/include/asm-mips/pgtable.h linux-mips-2.4.17-20020129/include/asm-mips/pgtable.h
--- linux-mips-2.4.17-20020129.macro/include/asm-mips/pgtable.h	Fri Jan 25 05:27:42 2002
+++ linux-mips-2.4.17-20020129/include/asm-mips/pgtable.h	Wed Feb 27 00:55:35 2002
@@ -165,7 +165,7 @@ extern int add_temporary_entry(unsigned 
 #define _PAGE_SILENT_READ           (1<<9)  /* synonym                 */
 #define _PAGE_DIRTY                 (1<<10) /* The MIPS dirty bit      */
 #define _PAGE_SILENT_WRITE          (1<<10)
-#define _CACHE_UNCACHED             (1<<11) /* R4[0246]00              */
+#define _CACHE_UNCACHED             (1<<11)
 #define _CACHE_MASK                 (1<<11)
 #define _CACHE_CACHABLE_NONCOHERENT 0
 
@@ -518,9 +518,19 @@ extern void paging_init(void);
 extern void update_mmu_cache(struct vm_area_struct *vma,
 				unsigned long address, pte_t pte);
 
-#define SWP_TYPE(x)		(((x).val >> 1) & 0x3f)
+/* Swap entries must have VALID and GLOBAL bits cleared. */
+#if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
+
+#define SWP_TYPE(x)		(((x).val >> 1) & 0x7f)
+#define SWP_OFFSET(x)		((x).val >> 10)
+#define SWP_ENTRY(type,offset)	((swp_entry_t) { ((type) << 1) | ((offset) << 10) })
+#else
+
+#define SWP_TYPE(x)		(((x).val >> 1) & 0x1f)
 #define SWP_OFFSET(x)		((x).val >> 8)
 #define SWP_ENTRY(type,offset)	((swp_entry_t) { ((type) << 1) | ((offset) << 8) })
+#endif
+
 #define pte_to_swp_entry(pte)	((swp_entry_t) { pte_val(pte) })
 #define swp_entry_to_pte(x)	((pte_t) { (x).val })