| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (c) 2000-2004 Silicon Graphics, Inc.  All Rights Reserved. | 
|  | 3 | * | 
|  | 4 | * This program is free software; you can redistribute it and/or modify it | 
|  | 5 | * under the terms of version 2 of the GNU General Public License as | 
|  | 6 | * published by the Free Software Foundation. | 
|  | 7 | * | 
|  | 8 | * This program is distributed in the hope that it would be useful, but | 
|  | 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | 
|  | 11 | * | 
|  | 12 | * Further, this software is distributed without any warranty that it is | 
|  | 13 | * free of the rightful claim of any third person regarding infringement | 
|  | 14 | * or the like.  Any license provided herein, whether implied or | 
|  | 15 | * otherwise, applies only to this software file.  Patent licenses, if | 
|  | 16 | * any, provided herein do not apply to combinations of this program with | 
|  | 17 | * other software, or any other product whatsoever. | 
|  | 18 | * | 
|  | 19 | * You should have received a copy of the GNU General Public License along | 
|  | 20 | * with this program; if not, write the Free Software Foundation, Inc., 59 | 
|  | 21 | * Temple Place - Suite 330, Boston MA 02111-1307, USA. | 
|  | 22 | * | 
|  | 23 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, | 
|  | 24 | * Mountain View, CA  94043, or: | 
|  | 25 | * | 
|  | 26 | * http://www.sgi.com | 
|  | 27 | * | 
|  | 28 | * For further information regarding this notice, see: | 
|  | 29 | * | 
|  | 30 | * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/ | 
|  | 31 | */ | 
|  | 32 |  | 
|  | 33 | #include <linux/sched.h> | 
|  | 34 | #include <linux/mm.h> | 
|  | 35 | #include <linux/vmalloc.h> | 
|  | 36 | #include <linux/highmem.h> | 
|  | 37 | #include <linux/swap.h> | 
|  | 38 | #include <linux/blkdev.h> | 
|  | 39 |  | 
|  | 40 | #include "time.h" | 
|  | 41 | #include "kmem.h" | 
|  | 42 |  | 
|  | 43 | #define MAX_VMALLOCS	6 | 
|  | 44 | #define MAX_SLAB_SIZE	0x20000 | 
|  | 45 |  | 
|  | 46 |  | 
|  | 47 | void * | 
|  | 48 | kmem_alloc(size_t size, int flags) | 
|  | 49 | { | 
|  | 50 | int	retries = 0; | 
|  | 51 | int	lflags = kmem_flags_convert(flags); | 
|  | 52 | void	*ptr; | 
|  | 53 |  | 
|  | 54 | do { | 
|  | 55 | if (size < MAX_SLAB_SIZE || retries > MAX_VMALLOCS) | 
|  | 56 | ptr = kmalloc(size, lflags); | 
|  | 57 | else | 
|  | 58 | ptr = __vmalloc(size, lflags, PAGE_KERNEL); | 
|  | 59 | if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP))) | 
|  | 60 | return ptr; | 
|  | 61 | if (!(++retries % 100)) | 
|  | 62 | printk(KERN_ERR "XFS: possible memory allocation " | 
|  | 63 | "deadlock in %s (mode:0x%x)\n", | 
|  | 64 | __FUNCTION__, lflags); | 
|  | 65 | blk_congestion_wait(WRITE, HZ/50); | 
|  | 66 | } while (1); | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | void * | 
|  | 70 | kmem_zalloc(size_t size, int flags) | 
|  | 71 | { | 
|  | 72 | void	*ptr; | 
|  | 73 |  | 
|  | 74 | ptr = kmem_alloc(size, flags); | 
|  | 75 | if (ptr) | 
|  | 76 | memset((char *)ptr, 0, (int)size); | 
|  | 77 | return ptr; | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | void | 
|  | 81 | kmem_free(void *ptr, size_t size) | 
|  | 82 | { | 
|  | 83 | if (((unsigned long)ptr < VMALLOC_START) || | 
|  | 84 | ((unsigned long)ptr >= VMALLOC_END)) { | 
|  | 85 | kfree(ptr); | 
|  | 86 | } else { | 
|  | 87 | vfree(ptr); | 
|  | 88 | } | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | void * | 
|  | 92 | kmem_realloc(void *ptr, size_t newsize, size_t oldsize, int flags) | 
|  | 93 | { | 
|  | 94 | void	*new; | 
|  | 95 |  | 
|  | 96 | new = kmem_alloc(newsize, flags); | 
|  | 97 | if (ptr) { | 
|  | 98 | if (new) | 
|  | 99 | memcpy(new, ptr, | 
|  | 100 | ((oldsize < newsize) ? oldsize : newsize)); | 
|  | 101 | kmem_free(ptr, oldsize); | 
|  | 102 | } | 
|  | 103 | return new; | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | void * | 
|  | 107 | kmem_zone_alloc(kmem_zone_t *zone, int flags) | 
|  | 108 | { | 
|  | 109 | int	retries = 0; | 
|  | 110 | int	lflags = kmem_flags_convert(flags); | 
|  | 111 | void	*ptr; | 
|  | 112 |  | 
|  | 113 | do { | 
|  | 114 | ptr = kmem_cache_alloc(zone, lflags); | 
|  | 115 | if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP))) | 
|  | 116 | return ptr; | 
|  | 117 | if (!(++retries % 100)) | 
|  | 118 | printk(KERN_ERR "XFS: possible memory allocation " | 
|  | 119 | "deadlock in %s (mode:0x%x)\n", | 
|  | 120 | __FUNCTION__, lflags); | 
|  | 121 | blk_congestion_wait(WRITE, HZ/50); | 
|  | 122 | } while (1); | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 | void * | 
|  | 126 | kmem_zone_zalloc(kmem_zone_t *zone, int flags) | 
|  | 127 | { | 
|  | 128 | void	*ptr; | 
|  | 129 |  | 
|  | 130 | ptr = kmem_zone_alloc(zone, flags); | 
|  | 131 | if (ptr) | 
|  | 132 | memset((char *)ptr, 0, kmem_cache_size(zone)); | 
|  | 133 | return ptr; | 
|  | 134 | } |