Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

[PATCH] mm: m68k kill stram swap

Please, please now delete the Atari CONFIG_STRAM_SWAP code. It may be
excellent and ingenious code, but its reference to swap_vfsmnt betrays that it
hasn't been built since 2.5.1 (four years old come December), it's delving
deep into matters which are the preserve of core mm code, its only purpose is
to give the more conscientious mm guys an anxiety attack from time to time;
yet we keep on breaking it more and more.

If you want to use RAM for swap, then if the MTD driver does not already
provide just what you need, I'm sure David could be persuaded to add the
extra. But you'd also like to be able to allocate extents of that swap for
other use: we can give you a core interface for that if you need. But unbuilt
for four years suggests to me that there's no need at all.

I cannot swear the patch below won't break your build, but believe so.

Signed-off-by: Hugh Dickins <hugh@veritas.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>

authored by

Hugh Dickins and committed by
Linus Torvalds
f9c98d02 147efea8

+17 -951
-2
Documentation/kernel-parameters.txt
··· 1460 1460 stifb= [HW] 1461 1461 Format: bpp:<bpp1>[:<bpp2>[:<bpp3>...]] 1462 1462 1463 - stram_swap= [HW,M68k] 1464 - 1465 1463 swiotlb= [IA-64] Number of I/O TLB slabs 1466 1464 1467 1465 switches= [HW,M68k]
+1 -23
Documentation/m68k/kernel-options.txt
··· 626 626 can be performed in optimal order. Not all SCSI devices support 627 627 tagged queuing (:-(). 628 628 629 - 4.6 switches= 629 + 4.5 switches= 630 630 ------------- 631 631 632 632 Syntax: switches=<list of switches> ··· 660 660 If you give an option both, with and without the "ov_" prefix, the 661 661 earlier initialization ("ov_"-less) takes precedence. But the 662 662 switching-off on reset still happens in this case. 663 - 664 - 4.5) stram_swap= 665 - ---------------- 666 - 667 - Syntax: stram_swap=<do_swap>[,<max_swap>] 668 - 669 - This option is available only if the kernel has been compiled with 670 - CONFIG_STRAM_SWAP enabled. Normally, the kernel then determines 671 - dynamically whether to actually use ST-RAM as swap space. (Currently, 672 - the fraction of ST-RAM must be less or equal 1/3 of total memory to 673 - enable this swapping.) You can override the kernel's decision by 674 - specifying this option. 1 for <do_swap> means always enable the swap, 675 - even if you have less alternate RAM. 0 stands for never swap to 676 - ST-RAM, even if it's small enough compared to the rest of memory. 677 - 678 - If ST-RAM swapping is enabled, the kernel usually uses all free 679 - ST-RAM as swap "device". If the kernel resides in ST-RAM, the region 680 - allocated by it is obviously never used for swapping :-) You can also 681 - limit this amount by specifying the second parameter, <max_swap>, if 682 - you want to use parts of ST-RAM as normal system memory. <max_swap> is 683 - in kBytes and the number should be a multiple of 4 (otherwise: rounded 684 - down). 685 663 686 664 5) Options for Amiga Only: 687 665 ==========================
+1 -23
arch/m68k/Kconfig
··· 388 388 Include support in the kernel for pcmcia on Amiga 1200 and Amiga 389 389 600. If you intend to use pcmcia cards say Y; otherwise say N. 390 390 391 - config STRAM_SWAP 392 - bool "Support for ST-RAM as swap space" 393 - depends on ATARI && BROKEN 394 - ---help--- 395 - Some Atari 68k machines (including the 520STF and 1020STE) divide 396 - their addressable memory into ST and TT sections. The TT section 397 - (up to 512MB) is the main memory; the ST section (up to 4MB) is 398 - accessible to the built-in graphics board, runs slower, and is 399 - present mainly for backward compatibility with older machines. 400 - 401 - This enables support for using (parts of) ST-RAM as swap space, 402 - instead of as normal system memory. This can first enhance system 403 - performance if you have lots of alternate RAM (compared to the size 404 - of ST-RAM), because executable code always will reside in faster 405 - memory. ST-RAM will remain as ultra-fast swap space. On the other 406 - hand, it allows much improved dynamic allocations of ST-RAM buffers 407 - for device driver modules (e.g. floppy, ACSI, SLM printer, DMA 408 - sound). The probability that such allocations at module load time 409 - fail is drastically reduced. 410 - 411 391 config STRAM_PROC 412 392 bool "ST-RAM statistics in /proc" 413 393 depends on ATARI 414 394 help 415 - Say Y here to report ST-RAM usage statistics in /proc/stram. See 416 - the help for CONFIG_STRAM_SWAP for discussion of ST-RAM and its 417 - uses. 395 + Say Y here to report ST-RAM usage statistics in /proc/stram. 418 396 419 397 config HEARTBEAT 420 398 bool "Use power LED as a heartbeat" if AMIGA || APOLLO || ATARI || MAC ||Q40
+15 -903
arch/m68k/atari/stram.c
··· 15 15 #include <linux/kdev_t.h> 16 16 #include <linux/major.h> 17 17 #include <linux/init.h> 18 - #include <linux/swap.h> 19 18 #include <linux/slab.h> 20 19 #include <linux/vmalloc.h> 21 20 #include <linux/pagemap.h> 22 - #include <linux/shm.h> 23 21 #include <linux/bootmem.h> 24 22 #include <linux/mount.h> 25 23 #include <linux/blkdev.h> ··· 30 32 #include <asm/atari_stram.h> 31 33 #include <asm/io.h> 32 34 #include <asm/semaphore.h> 33 - 34 - #include <linux/swapops.h> 35 35 36 36 #undef DEBUG 37 37 ··· 45 49 #include <linux/proc_fs.h> 46 50 #endif 47 51 48 - /* Pre-swapping comments: 49 - * 52 + /* 50 53 * ++roman: 51 54 * 52 55 * New version of ST-Ram buffer allocation. Instead of using the ··· 70 75 * 71 76 */ 72 77 73 - /* 74 - * New Nov 1997: Use ST-RAM as swap space! 75 - * 76 - * In the past, there were often problems with modules that require ST-RAM 77 - * buffers. Such drivers have to use __get_dma_pages(), which unfortunately 78 - * often isn't very successful in allocating more than 1 page :-( [1] The net 79 - * result was that most of the time you couldn't insmod such modules (ataflop, 80 - * ACSI, SCSI on Falcon, Atari internal framebuffer, not to speak of acsi_slm, 81 - * which needs a 1 MB buffer... :-). 82 - * 83 - * To overcome this limitation, ST-RAM can now be turned into a very 84 - * high-speed swap space. If a request for an ST-RAM buffer comes, the kernel 85 - * now tries to unswap some pages on that swap device to make some free (and 86 - * contiguous) space. This works much better in comparison to 87 - * __get_dma_pages(), since used swap pages can be selectively freed by either 88 - * moving them to somewhere else in swap space, or by reading them back into 89 - * system memory. Ok, there operation of unswapping isn't really cheap (for 90 - * each page, one has to go through the page tables of all processes), but it 91 - * doesn't happen that often (only when allocation ST-RAM, i.e. when loading a 92 - * module that needs ST-RAM). But it at least makes it possible to load such 93 - * modules! 94 - * 95 - * It could also be that overall system performance increases a bit due to 96 - * ST-RAM swapping, since slow ST-RAM isn't used anymore for holding data or 97 - * executing code in. It's then just a (very fast, compared to disk) back 98 - * storage for not-so-often needed data. (But this effect must be compared 99 - * with the loss of total memory...) Don't know if the effect is already 100 - * visible on a TT, where the speed difference between ST- and TT-RAM isn't 101 - * that dramatic, but it should on machines where TT-RAM is really much faster 102 - * (e.g. Afterburner). 103 - * 104 - * [1]: __get_free_pages() does a fine job if you only want one page, but if 105 - * you want more (contiguous) pages, it can give you such a block only if 106 - * there's already a free one. The algorithm can't try to free buffers or swap 107 - * out something in order to make more free space, since all that page-freeing 108 - * mechanisms work "target-less", i.e. they just free something, but not in a 109 - * specific place. I.e., __get_free_pages() can't do anything to free 110 - * *adjacent* pages :-( This situation becomes even worse for DMA memory, 111 - * since the freeing algorithms are also blind to DMA capability of pages. 112 - */ 113 - 114 - /* 1998-10-20: ++andreas 115 - unswap_by_move disabled because it does not handle swapped shm pages. 116 - */ 117 - 118 - /* 2000-05-01: ++andreas 119 - Integrated with bootmem. Remove all traces of unswap_by_move. 120 - */ 121 - 122 - #ifdef CONFIG_STRAM_SWAP 123 - #define ALIGN_IF_SWAP(x) PAGE_ALIGN(x) 124 - #else 125 - #define ALIGN_IF_SWAP(x) (x) 126 - #endif 127 - 128 - /* get index of swap page at address 'addr' */ 129 - #define SWAP_NR(addr) (((addr) - swap_start) >> PAGE_SHIFT) 130 - 131 - /* get address of swap page #'nr' */ 132 - #define SWAP_ADDR(nr) (swap_start + ((nr) << PAGE_SHIFT)) 133 - 134 - /* get number of pages for 'n' bytes (already page-aligned) */ 135 - #define N_PAGES(n) ((n) >> PAGE_SHIFT) 136 - 137 - /* The following two numbers define the maximum fraction of ST-RAM in total 138 - * memory, below that the kernel would automatically use ST-RAM as swap 139 - * space. This decision can be overridden with stram_swap= */ 140 - #define MAX_STRAM_FRACTION_NOM 1 141 - #define MAX_STRAM_FRACTION_DENOM 3 142 - 143 78 /* Start and end (virtual) of ST-RAM */ 144 79 static void *stram_start, *stram_end; 145 80 ··· 89 164 } BLOCK; 90 165 91 166 /* values for flags field */ 92 - #define BLOCK_FREE 0x01 /* free structure in the BLOCKs pool */ 167 + #define BLOCK_FREE 0x01 /* free structure in the BLOCKs pool */ 93 168 #define BLOCK_KMALLOCED 0x02 /* structure allocated by kmalloc() */ 94 - #define BLOCK_GFP 0x08 /* block allocated with __get_dma_pages() */ 95 - #define BLOCK_INSWAP 0x10 /* block allocated in swap space */ 169 + #define BLOCK_GFP 0x08 /* block allocated with __get_dma_pages() */ 96 170 97 171 /* list of allocated blocks */ 98 172 static BLOCK *alloc_list; ··· 103 179 #define N_STATIC_BLOCKS 20 104 180 static BLOCK static_blocks[N_STATIC_BLOCKS]; 105 181 106 - #ifdef CONFIG_STRAM_SWAP 107 - /* max. number of bytes to use for swapping 108 - * 0 = no ST-RAM swapping 109 - * -1 = do swapping (to whole ST-RAM) if it's less than MAX_STRAM_FRACTION of 110 - * total memory 111 - */ 112 - static int max_swap_size = -1; 113 - 114 - /* start and end of swapping area */ 115 - static void *swap_start, *swap_end; 116 - 117 - /* The ST-RAM's swap info structure */ 118 - static struct swap_info_struct *stram_swap_info; 119 - 120 - /* The ST-RAM's swap type */ 121 - static int stram_swap_type; 122 - 123 - /* Semaphore for get_stram_region. */ 124 - static DECLARE_MUTEX(stram_swap_sem); 125 - 126 - /* major and minor device number of the ST-RAM device; for the major, we use 127 - * the same as Amiga z2ram, which is really similar and impossible on Atari, 128 - * and for the minor a relatively odd number to avoid the user creating and 129 - * using that device. */ 130 - #define STRAM_MAJOR Z2RAM_MAJOR 131 - #define STRAM_MINOR 13 132 - 133 - /* Some impossible pointer value */ 134 - #define MAGIC_FILE_P (struct file *)0xffffdead 135 - 136 - #ifdef DO_PROC 137 - static unsigned stat_swap_read; 138 - static unsigned stat_swap_write; 139 - static unsigned stat_swap_force; 140 - #endif /* DO_PROC */ 141 - 142 - #endif /* CONFIG_STRAM_SWAP */ 143 - 144 182 /***************************** Prototypes *****************************/ 145 183 146 - #ifdef CONFIG_STRAM_SWAP 147 - static int swap_init(void *start_mem, void *swap_data); 148 - static void *get_stram_region( unsigned long n_pages ); 149 - static void free_stram_region( unsigned long offset, unsigned long n_pages 150 - ); 151 - static int in_some_region(void *addr); 152 - static unsigned long find_free_region( unsigned long n_pages, unsigned long 153 - *total_free, unsigned long 154 - *region_free ); 155 - static void do_stram_request(request_queue_t *); 156 - static int stram_open( struct inode *inode, struct file *filp ); 157 - static int stram_release( struct inode *inode, struct file *filp ); 158 - static void reserve_region(void *start, void *end); 159 - #endif 160 184 static BLOCK *add_region( void *addr, unsigned long size ); 161 185 static BLOCK *find_region( void *addr ); 162 186 static int remove_region( BLOCK *block ); ··· 151 279 */ 152 280 void __init atari_stram_reserve_pages(void *start_mem) 153 281 { 154 - #ifdef CONFIG_STRAM_SWAP 155 - /* if max_swap_size is negative (i.e. no stram_swap= option given), 156 - * determine at run time whether to use ST-RAM swapping */ 157 - if (max_swap_size < 0) 158 - /* Use swapping if ST-RAM doesn't make up more than MAX_STRAM_FRACTION 159 - * of total memory. In that case, the max. size is set to 16 MB, 160 - * because ST-RAM can never be bigger than that. 161 - * Also, never use swapping on a Hades, there's no separate ST-RAM in 162 - * that machine. */ 163 - max_swap_size = 164 - (!MACH_IS_HADES && 165 - (N_PAGES(stram_end-stram_start)*MAX_STRAM_FRACTION_DENOM <= 166 - ((unsigned long)high_memory>>PAGE_SHIFT)*MAX_STRAM_FRACTION_NOM)) ? 16*1024*1024 : 0; 167 - DPRINTK( "atari_stram_reserve_pages: max_swap_size = %d\n", max_swap_size ); 168 - #endif 169 - 170 282 /* always reserve first page of ST-RAM, the first 2 kB are 171 283 * supervisor-only! */ 172 284 if (!kernel_in_stram) 173 285 reserve_bootmem (0, PAGE_SIZE); 174 286 175 - #ifdef CONFIG_STRAM_SWAP 176 - { 177 - void *swap_data; 178 - 179 - start_mem = (void *) PAGE_ALIGN ((unsigned long) start_mem); 180 - /* determine first page to use as swap: if the kernel is 181 - in TT-RAM, this is the first page of (usable) ST-RAM; 182 - otherwise just use the end of kernel data (= start_mem) */ 183 - swap_start = !kernel_in_stram ? stram_start + PAGE_SIZE : start_mem; 184 - /* decrement by one page, rest of kernel assumes that first swap page 185 - * is always reserved and maybe doesn't handle swp_entry == 0 186 - * correctly */ 187 - swap_start -= PAGE_SIZE; 188 - swap_end = stram_end; 189 - if (swap_end-swap_start > max_swap_size) 190 - swap_end = swap_start + max_swap_size; 191 - DPRINTK( "atari_stram_reserve_pages: swapping enabled; " 192 - "swap=%p-%p\n", swap_start, swap_end); 193 - 194 - /* reserve some amount of memory for maintainance of 195 - * swapping itself: one page for each 2048 (PAGE_SIZE/2) 196 - * swap pages. (2 bytes for each page) */ 197 - swap_data = start_mem; 198 - start_mem += ((SWAP_NR(swap_end) + PAGE_SIZE/2 - 1) 199 - >> (PAGE_SHIFT-1)) << PAGE_SHIFT; 200 - /* correct swap_start if necessary */ 201 - if (swap_start + PAGE_SIZE == swap_data) 202 - swap_start = start_mem - PAGE_SIZE; 203 - 204 - if (!swap_init( start_mem, swap_data )) { 205 - printk( KERN_ERR "ST-RAM swap space initialization failed\n" ); 206 - max_swap_size = 0; 207 - return; 208 - } 209 - /* reserve region for swapping meta-data */ 210 - reserve_region(swap_data, start_mem); 211 - /* reserve swapping area itself */ 212 - reserve_region(swap_start + PAGE_SIZE, swap_end); 213 - 214 - /* 215 - * If the whole ST-RAM is used for swapping, there are no allocatable 216 - * dma pages left. But unfortunately, some shared parts of the kernel 217 - * (particularly the SCSI mid-level) call __get_dma_pages() 218 - * unconditionally :-( These calls then fail, and scsi.c even doesn't 219 - * check for NULL return values and just crashes. The quick fix for 220 - * this (instead of doing much clean up work in the SCSI code) is to 221 - * pretend all pages are DMA-able by setting mach_max_dma_address to 222 - * ULONG_MAX. This doesn't change any functionality so far, since 223 - * get_dma_pages() shouldn't be used on Atari anyway anymore (better 224 - * use atari_stram_alloc()), and the Atari SCSI drivers don't need DMA 225 - * memory. But unfortunately there's now no kind of warning (even not 226 - * a NULL return value) if you use get_dma_pages() nevertheless :-( 227 - * You just will get non-DMA-able memory... 228 - */ 229 - mach_max_dma_address = 0xffffffff; 230 - } 231 - #endif 232 287 } 233 288 234 289 void atari_stram_mem_init_hook (void) ··· 166 367 167 368 /* 168 369 * This is main public interface: somehow allocate a ST-RAM block 169 - * There are three strategies: 170 370 * 171 371 * - If we're before mem_init(), we have to make a static allocation. The 172 372 * region is taken in the kernel data area (if the kernel is in ST-RAM) or ··· 173 375 * rsvd_stram_* region. The ST-RAM is somewhere in the middle of kernel 174 376 * address space in the latter case. 175 377 * 176 - * - If mem_init() already has been called and ST-RAM swapping is enabled, 177 - * try to get the memory from the (pseudo) swap-space, either free already 178 - * or by moving some other pages out of the swap. 179 - * 180 - * - If mem_init() already has been called, and ST-RAM swapping is not 181 - * enabled, the only possibility is to try with __get_dma_pages(). This has 182 - * the disadvantage that it's very hard to get more than 1 page, and it is 183 - * likely to fail :-( 378 + * - If mem_init() already has been called, try with __get_dma_pages(). 379 + * This has the disadvantage that it's very hard to get more than 1 page, 380 + * and it is likely to fail :-( 184 381 * 185 382 */ 186 383 void *atari_stram_alloc(long size, const char *owner) ··· 186 393 187 394 DPRINTK("atari_stram_alloc(size=%08lx,owner=%s)\n", size, owner); 188 395 189 - size = ALIGN_IF_SWAP(size); 190 - DPRINTK( "atari_stram_alloc: rounded size = %08lx\n", size ); 191 - #ifdef CONFIG_STRAM_SWAP 192 - if (max_swap_size) { 193 - /* If swapping is active: make some free space in the swap 194 - "device". */ 195 - DPRINTK( "atari_stram_alloc: after mem_init, swapping ok, " 196 - "calling get_region\n" ); 197 - addr = get_stram_region( N_PAGES(size) ); 198 - flags = BLOCK_INSWAP; 199 - } 200 - else 201 - #endif 202 396 if (!mem_init_done) 203 397 return alloc_bootmem_low(size); 204 398 else { 205 - /* After mem_init() and no swapping: can only resort to 206 - * __get_dma_pages() */ 399 + /* After mem_init(): can only resort to __get_dma_pages() */ 207 400 addr = (void *)__get_dma_pages(GFP_KERNEL, get_order(size)); 208 401 flags = BLOCK_GFP; 209 - DPRINTK( "atari_stram_alloc: after mem_init, swapping off, " 402 + DPRINTK( "atari_stram_alloc: after mem_init, " 210 403 "get_pages=%p\n", addr ); 211 404 } 212 405 ··· 201 422 /* out of memory for BLOCK structure :-( */ 202 423 DPRINTK( "atari_stram_alloc: out of mem for BLOCK -- " 203 424 "freeing again\n" ); 204 - #ifdef CONFIG_STRAM_SWAP 205 - if (flags == BLOCK_INSWAP) 206 - free_stram_region( SWAP_NR(addr), N_PAGES(size) ); 207 - else 208 - #endif 209 - free_pages((unsigned long)addr, get_order(size)); 425 + free_pages((unsigned long)addr, get_order(size)); 210 426 return( NULL ); 211 427 } 212 428 block->owner = owner; ··· 225 451 DPRINTK( "atari_stram_free: found block (%p): size=%08lx, owner=%s, " 226 452 "flags=%02x\n", block, block->size, block->owner, block->flags ); 227 453 228 - #ifdef CONFIG_STRAM_SWAP 229 - if (!max_swap_size) { 230 - #endif 231 - if (block->flags & BLOCK_GFP) { 232 - DPRINTK("atari_stram_free: is kmalloced, order_size=%d\n", 233 - get_order(block->size)); 234 - free_pages((unsigned long)addr, get_order(block->size)); 235 - } 236 - else 237 - goto fail; 238 - #ifdef CONFIG_STRAM_SWAP 239 - } 240 - else if (block->flags & BLOCK_INSWAP) { 241 - DPRINTK( "atari_stram_free: is swap-alloced\n" ); 242 - free_stram_region( SWAP_NR(block->start), N_PAGES(block->size) ); 243 - } 244 - else 454 + if (!(block->flags & BLOCK_GFP)) 245 455 goto fail; 246 - #endif 456 + 457 + DPRINTK("atari_stram_free: is kmalloced, order_size=%d\n", 458 + get_order(block->size)); 459 + free_pages((unsigned long)addr, get_order(block->size)); 247 460 remove_region( block ); 248 461 return; 249 462 ··· 238 477 printk( KERN_ERR "atari_stram_free: cannot free block at %p " 239 478 "(called from %p)\n", addr, __builtin_return_address(0) ); 240 479 } 241 - 242 - 243 - #ifdef CONFIG_STRAM_SWAP 244 - 245 - 246 - /* ------------------------------------------------------------------------ */ 247 - /* Main Swapping Functions */ 248 - /* ------------------------------------------------------------------------ */ 249 - 250 - 251 - /* 252 - * Initialize ST-RAM swap device 253 - * (lots copied and modified from sys_swapon() in mm/swapfile.c) 254 - */ 255 - static int __init swap_init(void *start_mem, void *swap_data) 256 - { 257 - static struct dentry fake_dentry; 258 - static struct vfsmount fake_vfsmnt; 259 - struct swap_info_struct *p; 260 - struct inode swap_inode; 261 - unsigned int type; 262 - void *addr; 263 - int i, j, k, prev; 264 - 265 - DPRINTK("swap_init(start_mem=%p, swap_data=%p)\n", 266 - start_mem, swap_data); 267 - 268 - /* need at least one page for swapping to (and this also isn't very 269 - * much... :-) */ 270 - if (swap_end - swap_start < 2*PAGE_SIZE) { 271 - printk( KERN_WARNING "stram_swap_init: swap space too small\n" ); 272 - return( 0 ); 273 - } 274 - 275 - /* find free slot in swap_info */ 276 - for( p = swap_info, type = 0; type < nr_swapfiles; type++, p++ ) 277 - if (!(p->flags & SWP_USED)) 278 - break; 279 - if (type >= MAX_SWAPFILES) { 280 - printk( KERN_WARNING "stram_swap_init: max. number of " 281 - "swap devices exhausted\n" ); 282 - return( 0 ); 283 - } 284 - if (type >= nr_swapfiles) 285 - nr_swapfiles = type+1; 286 - 287 - stram_swap_info = p; 288 - stram_swap_type = type; 289 - 290 - /* fake some dir cache entries to give us some name in /dev/swaps */ 291 - fake_dentry.d_parent = &fake_dentry; 292 - fake_dentry.d_name.name = "stram (internal)"; 293 - fake_dentry.d_name.len = 16; 294 - fake_vfsmnt.mnt_parent = &fake_vfsmnt; 295 - 296 - p->flags = SWP_USED; 297 - p->swap_file = &fake_dentry; 298 - p->swap_vfsmnt = &fake_vfsmnt; 299 - p->swap_map = swap_data; 300 - p->cluster_nr = 0; 301 - p->next = -1; 302 - p->prio = 0x7ff0; /* a rather high priority, but not the higest 303 - * to give the user a chance to override */ 304 - 305 - /* call stram_open() directly, avoids at least the overhead in 306 - * constructing a dummy file structure... */ 307 - swap_inode.i_rdev = MKDEV( STRAM_MAJOR, STRAM_MINOR ); 308 - stram_open( &swap_inode, MAGIC_FILE_P ); 309 - p->max = SWAP_NR(swap_end); 310 - 311 - /* initialize swap_map: set regions that are already allocated or belong 312 - * to kernel data space to SWAP_MAP_BAD, otherwise to free */ 313 - j = 0; /* # of free pages */ 314 - k = 0; /* # of already allocated pages (from pre-mem_init stram_alloc()) */ 315 - p->lowest_bit = 0; 316 - p->highest_bit = 0; 317 - for( i = 1, addr = SWAP_ADDR(1); i < p->max; 318 - i++, addr += PAGE_SIZE ) { 319 - if (in_some_region( addr )) { 320 - p->swap_map[i] = SWAP_MAP_BAD; 321 - ++k; 322 - } 323 - else if (kernel_in_stram && addr < start_mem ) { 324 - p->swap_map[i] = SWAP_MAP_BAD; 325 - } 326 - else { 327 - p->swap_map[i] = 0; 328 - ++j; 329 - if (!p->lowest_bit) p->lowest_bit = i; 330 - p->highest_bit = i; 331 - } 332 - } 333 - /* first page always reserved (and doesn't really belong to swap space) */ 334 - p->swap_map[0] = SWAP_MAP_BAD; 335 - 336 - /* now swapping to this device ok */ 337 - p->pages = j + k; 338 - swap_list_lock(); 339 - nr_swap_pages += j; 340 - p->flags = SWP_WRITEOK; 341 - 342 - /* insert swap space into swap_list */ 343 - prev = -1; 344 - for (i = swap_list.head; i >= 0; i = swap_info[i].next) { 345 - if (p->prio >= swap_info[i].prio) { 346 - break; 347 - } 348 - prev = i; 349 - } 350 - p->next = i; 351 - if (prev < 0) { 352 - swap_list.head = swap_list.next = p - swap_info; 353 - } else { 354 - swap_info[prev].next = p - swap_info; 355 - } 356 - swap_list_unlock(); 357 - 358 - printk( KERN_INFO "Using %dk (%d pages) of ST-RAM as swap space.\n", 359 - p->pages << 2, p->pages ); 360 - return( 1 ); 361 - } 362 - 363 - 364 - /* 365 - * The swap entry has been read in advance, and we return 1 to indicate 366 - * that the page has been used or is no longer needed. 367 - * 368 - * Always set the resulting pte to be nowrite (the same as COW pages 369 - * after one process has exited). We don't know just how many PTEs will 370 - * share this swap entry, so be cautious and let do_wp_page work out 371 - * what to do if a write is requested later. 372 - */ 373 - static inline void unswap_pte(struct vm_area_struct * vma, unsigned long 374 - address, pte_t *dir, swp_entry_t entry, 375 - struct page *page) 376 - { 377 - pte_t pte = *dir; 378 - 379 - if (pte_none(pte)) 380 - return; 381 - if (pte_present(pte)) { 382 - /* If this entry is swap-cached, then page must already 383 - hold the right address for any copies in physical 384 - memory */ 385 - if (pte_page(pte) != page) 386 - return; 387 - /* We will be removing the swap cache in a moment, so... */ 388 - set_pte(dir, pte_mkdirty(pte)); 389 - return; 390 - } 391 - if (pte_val(pte) != entry.val) 392 - return; 393 - 394 - DPRINTK("unswap_pte: replacing entry %08lx by new page %p", 395 - entry.val, page); 396 - set_pte(dir, pte_mkdirty(mk_pte(page, vma->vm_page_prot))); 397 - swap_free(entry); 398 - get_page(page); 399 - inc_mm_counter(vma->vm_mm, rss); 400 - } 401 - 402 - static inline void unswap_pmd(struct vm_area_struct * vma, pmd_t *dir, 403 - unsigned long address, unsigned long size, 404 - unsigned long offset, swp_entry_t entry, 405 - struct page *page) 406 - { 407 - pte_t * pte; 408 - unsigned long end; 409 - 410 - if (pmd_none(*dir)) 411 - return; 412 - if (pmd_bad(*dir)) { 413 - pmd_ERROR(*dir); 414 - pmd_clear(dir); 415 - return; 416 - } 417 - pte = pte_offset_kernel(dir, address); 418 - offset += address & PMD_MASK; 419 - address &= ~PMD_MASK; 420 - end = address + size; 421 - if (end > PMD_SIZE) 422 - end = PMD_SIZE; 423 - do { 424 - unswap_pte(vma, offset+address-vma->vm_start, pte, entry, page); 425 - address += PAGE_SIZE; 426 - pte++; 427 - } while (address < end); 428 - } 429 - 430 - static inline void unswap_pgd(struct vm_area_struct * vma, pgd_t *dir, 431 - unsigned long address, unsigned long size, 432 - swp_entry_t entry, struct page *page) 433 - { 434 - pmd_t * pmd; 435 - unsigned long offset, end; 436 - 437 - if (pgd_none(*dir)) 438 - return; 439 - if (pgd_bad(*dir)) { 440 - pgd_ERROR(*dir); 441 - pgd_clear(dir); 442 - return; 443 - } 444 - pmd = pmd_offset(dir, address); 445 - offset = address & PGDIR_MASK; 446 - address &= ~PGDIR_MASK; 447 - end = address + size; 448 - if (end > PGDIR_SIZE) 449 - end = PGDIR_SIZE; 450 - do { 451 - unswap_pmd(vma, pmd, address, end - address, offset, entry, 452 - page); 453 - address = (address + PMD_SIZE) & PMD_MASK; 454 - pmd++; 455 - } while (address < end); 456 - } 457 - 458 - static void unswap_vma(struct vm_area_struct * vma, pgd_t *pgdir, 459 - swp_entry_t entry, struct page *page) 460 - { 461 - unsigned long start = vma->vm_start, end = vma->vm_end; 462 - 463 - do { 464 - unswap_pgd(vma, pgdir, start, end - start, entry, page); 465 - start = (start + PGDIR_SIZE) & PGDIR_MASK; 466 - pgdir++; 467 - } while (start < end); 468 - } 469 - 470 - static void unswap_process(struct mm_struct * mm, swp_entry_t entry, 471 - struct page *page) 472 - { 473 - struct vm_area_struct* vma; 474 - 475 - /* 476 - * Go through process' page directory. 477 - */ 478 - if (!mm) 479 - return; 480 - for (vma = mm->mmap; vma; vma = vma->vm_next) { 481 - pgd_t * pgd = pgd_offset(mm, vma->vm_start); 482 - unswap_vma(vma, pgd, entry, page); 483 - } 484 - } 485 - 486 - 487 - static int unswap_by_read(unsigned short *map, unsigned long max, 488 - unsigned long start, unsigned long n_pages) 489 - { 490 - struct task_struct *p; 491 - struct page *page; 492 - swp_entry_t entry; 493 - unsigned long i; 494 - 495 - DPRINTK( "unswapping %lu..%lu by reading in\n", 496 - start, start+n_pages-1 ); 497 - 498 - for( i = start; i < start+n_pages; ++i ) { 499 - if (map[i] == SWAP_MAP_BAD) { 500 - printk( KERN_ERR "get_stram_region: page %lu already " 501 - "reserved??\n", i ); 502 - continue; 503 - } 504 - 505 - if (map[i]) { 506 - entry = swp_entry(stram_swap_type, i); 507 - DPRINTK("unswap: map[i=%lu]=%u nr_swap=%ld\n", 508 - i, map[i], nr_swap_pages); 509 - 510 - swap_device_lock(stram_swap_info); 511 - map[i]++; 512 - swap_device_unlock(stram_swap_info); 513 - /* Get a page for the entry, using the existing 514 - swap cache page if there is one. Otherwise, 515 - get a clean page and read the swap into it. */ 516 - page = read_swap_cache_async(entry, NULL, 0); 517 - if (!page) { 518 - swap_free(entry); 519 - return -ENOMEM; 520 - } 521 - read_lock(&tasklist_lock); 522 - for_each_process(p) 523 - unswap_process(p->mm, entry, page); 524 - read_unlock(&tasklist_lock); 525 - shmem_unuse(entry, page); 526 - /* Now get rid of the extra reference to the 527 - temporary page we've been using. */ 528 - if (PageSwapCache(page)) 529 - delete_from_swap_cache(page); 530 - __free_page(page); 531 - #ifdef DO_PROC 532 - stat_swap_force++; 533 - #endif 534 - } 535 - 536 - DPRINTK( "unswap: map[i=%lu]=%u nr_swap=%ld\n", 537 - i, map[i], nr_swap_pages ); 538 - swap_list_lock(); 539 - swap_device_lock(stram_swap_info); 540 - map[i] = SWAP_MAP_BAD; 541 - if (stram_swap_info->lowest_bit == i) 542 - stram_swap_info->lowest_bit++; 543 - if (stram_swap_info->highest_bit == i) 544 - stram_swap_info->highest_bit--; 545 - --nr_swap_pages; 546 - swap_device_unlock(stram_swap_info); 547 - swap_list_unlock(); 548 - } 549 - 550 - return 0; 551 - } 552 - 553 - /* 554 - * reserve a region in ST-RAM swap space for an allocation 555 - */ 556 - static void *get_stram_region( unsigned long n_pages ) 557 - { 558 - unsigned short *map = stram_swap_info->swap_map; 559 - unsigned long max = stram_swap_info->max; 560 - unsigned long start, total_free, region_free; 561 - int err; 562 - void *ret = NULL; 563 - 564 - DPRINTK( "get_stram_region(n_pages=%lu)\n", n_pages ); 565 - 566 - down(&stram_swap_sem); 567 - 568 - /* disallow writing to the swap device now */ 569 - stram_swap_info->flags = SWP_USED; 570 - 571 - /* find a region of n_pages pages in the swap space including as much free 572 - * pages as possible (and excluding any already-reserved pages). */ 573 - if (!(start = find_free_region( n_pages, &total_free, &region_free ))) 574 - goto end; 575 - DPRINTK( "get_stram_region: region starts at %lu, has %lu free pages\n", 576 - start, region_free ); 577 - 578 - err = unswap_by_read(map, max, start, n_pages); 579 - if (err) 580 - goto end; 581 - 582 - ret = SWAP_ADDR(start); 583 - end: 584 - /* allow using swap device again */ 585 - stram_swap_info->flags = SWP_WRITEOK; 586 - up(&stram_swap_sem); 587 - DPRINTK( "get_stram_region: returning %p\n", ret ); 588 - return( ret ); 589 - } 590 - 591 - 592 - /* 593 - * free a reserved region in ST-RAM swap space 594 - */ 595 - static void free_stram_region( unsigned long offset, unsigned long n_pages ) 596 - { 597 - unsigned short *map = stram_swap_info->swap_map; 598 - 599 - DPRINTK( "free_stram_region(offset=%lu,n_pages=%lu)\n", offset, n_pages ); 600 - 601 - if (offset < 1 || offset + n_pages > stram_swap_info->max) { 602 - printk( KERN_ERR "free_stram_region: Trying to free non-ST-RAM\n" ); 603 - return; 604 - } 605 - 606 - swap_list_lock(); 607 - swap_device_lock(stram_swap_info); 608 - /* un-reserve the freed pages */ 609 - for( ; n_pages > 0; ++offset, --n_pages ) { 610 - if (map[offset] != SWAP_MAP_BAD) 611 - printk( KERN_ERR "free_stram_region: Swap page %lu was not " 612 - "reserved\n", offset ); 613 - map[offset] = 0; 614 - } 615 - 616 - /* update swapping meta-data */ 617 - if (offset < stram_swap_info->lowest_bit) 618 - stram_swap_info->lowest_bit = offset; 619 - if (offset+n_pages-1 > stram_swap_info->highest_bit) 620 - stram_swap_info->highest_bit = offset+n_pages-1; 621 - if (stram_swap_info->prio > swap_info[swap_list.next].prio) 622 - swap_list.next = swap_list.head; 623 - nr_swap_pages += n_pages; 624 - swap_device_unlock(stram_swap_info); 625 - swap_list_unlock(); 626 - } 627 - 628 - 629 - /* ------------------------------------------------------------------------ */ 630 - /* Utility Functions for Swapping */ 631 - /* ------------------------------------------------------------------------ */ 632 - 633 - 634 - /* is addr in some of the allocated regions? */ 635 - static int in_some_region(void *addr) 636 - { 637 - BLOCK *p; 638 - 639 - for( p = alloc_list; p; p = p->next ) { 640 - if (p->start <= addr && addr < p->start + p->size) 641 - return( 1 ); 642 - } 643 - return( 0 ); 644 - } 645 - 646 - 647 - static unsigned long find_free_region(unsigned long n_pages, 648 - unsigned long *total_free, 649 - unsigned long *region_free) 650 - { 651 - unsigned short *map = stram_swap_info->swap_map; 652 - unsigned long max = stram_swap_info->max; 653 - unsigned long head, tail, max_start; 654 - long nfree, max_free; 655 - 656 - /* first scan the swap space for a suitable place for the allocation */ 657 - head = 1; 658 - max_start = 0; 659 - max_free = -1; 660 - *total_free = 0; 661 - 662 - start_over: 663 - /* increment tail until final window size reached, and count free pages */ 664 - nfree = 0; 665 - for( tail = head; tail-head < n_pages && tail < max; ++tail ) { 666 - if (map[tail] == SWAP_MAP_BAD) { 667 - head = tail+1; 668 - goto start_over; 669 - } 670 - if (!map[tail]) { 671 - ++nfree; 672 - ++*total_free; 673 - } 674 - } 675 - if (tail-head < n_pages) 676 - goto out; 677 - if (nfree > max_free) { 678 - max_start = head; 679 - max_free = nfree; 680 - if (max_free >= n_pages) 681 - /* don't need more free pages... :-) */ 682 - goto out; 683 - } 684 - 685 - /* now shift the window and look for the area where as much pages as 686 - * possible are free */ 687 - while( tail < max ) { 688 - nfree -= (map[head++] == 0); 689 - if (map[tail] == SWAP_MAP_BAD) { 690 - head = tail+1; 691 - goto start_over; 692 - } 693 - if (!map[tail]) { 694 - ++nfree; 695 - ++*total_free; 696 - } 697 - ++tail; 698 - if (nfree > max_free) { 699 - max_start = head; 700 - max_free = nfree; 701 - if (max_free >= n_pages) 702 - /* don't need more free pages... :-) */ 703 - goto out; 704 - } 705 - } 706 - 707 - out: 708 - if (max_free < 0) { 709 - printk( KERN_NOTICE "get_stram_region: ST-RAM too full or fragmented " 710 - "-- can't allocate %lu pages\n", n_pages ); 711 - return( 0 ); 712 - } 713 - 714 - *region_free = max_free; 715 - return( max_start ); 716 - } 717 - 718 - 719 - /* setup parameters from command line */ 720 - void __init stram_swap_setup(char *str, int *ints) 721 - { 722 - if (ints[0] >= 1) 723 - max_swap_size = ((ints[1] < 0 ? 0 : ints[1]) * 1024) & PAGE_MASK; 724 - } 725 - 726 - 727 - /* ------------------------------------------------------------------------ */ 728 - /* ST-RAM device */ 729 - /* ------------------------------------------------------------------------ */ 730 - 731 - static int refcnt; 732 - 733 - static void do_stram_request(request_queue_t *q) 734 - { 735 - struct request *req; 736 - 737 - while ((req = elv_next_request(q)) != NULL) { 738 - void *start = swap_start + (req->sector << 9); 739 - unsigned long len = req->current_nr_sectors << 9; 740 - if ((start + len) > swap_end) { 741 - printk( KERN_ERR "stram: bad access beyond end of device: " 742 - "block=%ld, count=%d\n", 743 - req->sector, 744 - req->current_nr_sectors ); 745 - end_request(req, 0); 746 - continue; 747 - } 748 - 749 - if (req->cmd == READ) { 750 - memcpy(req->buffer, start, len); 751 - #ifdef DO_PROC 752 - stat_swap_read += N_PAGES(len); 753 - #endif 754 - } 755 - else { 756 - memcpy(start, req->buffer, len); 757 - #ifdef DO_PROC 758 - stat_swap_write += N_PAGES(len); 759 - #endif 760 - } 761 - end_request(req, 1); 762 - } 763 - } 764 - 765 - 766 - static int stram_open( struct inode *inode, struct file *filp ) 767 - { 768 - if (filp != MAGIC_FILE_P) { 769 - printk( KERN_NOTICE "Only kernel can open ST-RAM device\n" ); 770 - return( -EPERM ); 771 - } 772 - if (refcnt) 773 - return( -EBUSY ); 774 - ++refcnt; 775 - return( 0 ); 776 - } 777 - 778 - static int stram_release( struct inode *inode, struct file *filp ) 779 - { 780 - if (filp != MAGIC_FILE_P) { 781 - printk( KERN_NOTICE "Only kernel can close ST-RAM device\n" ); 782 - return( -EPERM ); 783 - } 784 - if (refcnt > 0) 785 - --refcnt; 786 - return( 0 ); 787 - } 788 - 789 - 790 - static struct block_device_operations stram_fops = { 791 - .open = stram_open, 792 - .release = stram_release, 793 - }; 794 - 795 - static struct gendisk *stram_disk; 796 - static struct request_queue *stram_queue; 797 - static DEFINE_SPINLOCK(stram_lock); 798 - 799 - int __init stram_device_init(void) 800 - { 801 - if (!MACH_IS_ATARI) 802 - /* no point in initializing this, I hope */ 803 - return -ENXIO; 804 - 805 - if (!max_swap_size) 806 - /* swapping not enabled */ 807 - return -ENXIO; 808 - stram_disk = alloc_disk(1); 809 - if (!stram_disk) 810 - return -ENOMEM; 811 - 812 - if (register_blkdev(STRAM_MAJOR, "stram")) { 813 - put_disk(stram_disk); 814 - return -ENXIO; 815 - } 816 - 817 - stram_queue = blk_init_queue(do_stram_request, &stram_lock); 818 - if (!stram_queue) { 819 - unregister_blkdev(STRAM_MAJOR, "stram"); 820 - put_disk(stram_disk); 821 - return -ENOMEM; 822 - } 823 - 824 - stram_disk->major = STRAM_MAJOR; 825 - stram_disk->first_minor = STRAM_MINOR; 826 - stram_disk->fops = &stram_fops; 827 - stram_disk->queue = stram_queue; 828 - sprintf(stram_disk->disk_name, "stram"); 829 - set_capacity(stram_disk, (swap_end - swap_start)/512); 830 - add_disk(stram_disk); 831 - return 0; 832 - } 833 - 834 - 835 - 836 - /* ------------------------------------------------------------------------ */ 837 - /* Misc Utility Functions */ 838 - /* ------------------------------------------------------------------------ */ 839 - 840 - /* reserve a range of pages */ 841 - static void reserve_region(void *start, void *end) 842 - { 843 - reserve_bootmem (virt_to_phys(start), end - start); 844 - } 845 - 846 - #endif /* CONFIG_STRAM_SWAP */ 847 480 848 481 849 482 /* ------------------------------------------------------------------------ */ ··· 328 1173 { 329 1174 int len = 0; 330 1175 BLOCK *p; 331 - #ifdef CONFIG_STRAM_SWAP 332 - int i; 333 - unsigned short *map = stram_swap_info->swap_map; 334 - unsigned long max = stram_swap_info->max; 335 - unsigned free = 0, used = 0, rsvd = 0; 336 - #endif 337 1176 338 - #ifdef CONFIG_STRAM_SWAP 339 - if (max_swap_size) { 340 - for( i = 1; i < max; ++i ) { 341 - if (!map[i]) 342 - ++free; 343 - else if (map[i] == SWAP_MAP_BAD) 344 - ++rsvd; 345 - else 346 - ++used; 347 - } 348 - PRINT_PROC( 349 - "Total ST-RAM: %8u kB\n" 350 - "Total ST-RAM swap: %8lu kB\n" 351 - "Free swap: %8u kB\n" 352 - "Used swap: %8u kB\n" 353 - "Allocated swap: %8u kB\n" 354 - "Swap Reads: %8u\n" 355 - "Swap Writes: %8u\n" 356 - "Swap Forced Reads: %8u\n", 357 - (stram_end - stram_start) >> 10, 358 - (max-1) << (PAGE_SHIFT-10), 359 - free << (PAGE_SHIFT-10), 360 - used << (PAGE_SHIFT-10), 361 - rsvd << (PAGE_SHIFT-10), 362 - stat_swap_read, 363 - stat_swap_write, 364 - stat_swap_force ); 365 - } 366 - else { 367 - #endif 368 - PRINT_PROC( "ST-RAM swapping disabled\n" ); 369 - PRINT_PROC("Total ST-RAM: %8u kB\n", 1177 + PRINT_PROC("Total ST-RAM: %8u kB\n", 370 1178 (stram_end - stram_start) >> 10); 371 - #ifdef CONFIG_STRAM_SWAP 372 - } 373 - #endif 374 - 375 1179 PRINT_PROC( "Allocated regions:\n" ); 376 1180 for( p = alloc_list; p; p = p->next ) { 377 1181 if (len + 50 >= PAGE_SIZE) ··· 341 1227 p->owner); 342 1228 if (p->flags & BLOCK_GFP) 343 1229 PRINT_PROC( "page-alloced)\n" ); 344 - else if (p->flags & BLOCK_INSWAP) 345 - PRINT_PROC( "in swap)\n" ); 346 1230 else 347 1231 PRINT_PROC( "??)\n" ); 348 1232 }