blob: 4b184559f23155bf4cec7d529b1561ea804b8bf3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
47void *
Christoph Hellwig760dea62005-09-02 16:56:02 +100048kmem_alloc(size_t size, unsigned int __nocast flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Christoph Hellwig760dea62005-09-02 16:56:02 +100050 int retries = 0;
51 unsigned int lflags = kmem_flags_convert(flags);
52 void *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
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
69void *
Christoph Hellwig760dea62005-09-02 16:56:02 +100070kmem_zalloc(size_t size, unsigned int __nocast flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
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
80void
81kmem_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
91void *
Christoph Hellwig760dea62005-09-02 16:56:02 +100092kmem_realloc(void *ptr, size_t newsize, size_t oldsize,
93 unsigned int __nocast flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 void *new;
96
97 new = kmem_alloc(newsize, flags);
98 if (ptr) {
99 if (new)
100 memcpy(new, ptr,
101 ((oldsize < newsize) ? oldsize : newsize));
102 kmem_free(ptr, oldsize);
103 }
104 return new;
105}
106
107void *
Christoph Hellwig760dea62005-09-02 16:56:02 +1000108kmem_zone_alloc(kmem_zone_t *zone, unsigned int __nocast flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Christoph Hellwig760dea62005-09-02 16:56:02 +1000110 int retries = 0;
111 unsigned int lflags = kmem_flags_convert(flags);
112 void *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 do {
115 ptr = kmem_cache_alloc(zone, lflags);
116 if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP)))
117 return ptr;
118 if (!(++retries % 100))
119 printk(KERN_ERR "XFS: possible memory allocation "
120 "deadlock in %s (mode:0x%x)\n",
121 __FUNCTION__, lflags);
122 blk_congestion_wait(WRITE, HZ/50);
123 } while (1);
124}
125
126void *
Christoph Hellwig760dea62005-09-02 16:56:02 +1000127kmem_zone_zalloc(kmem_zone_t *zone, unsigned int __nocast flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 void *ptr;
130
131 ptr = kmem_zone_alloc(zone, flags);
132 if (ptr)
133 memset((char *)ptr, 0, kmem_cache_size(zone));
134 return ptr;
135}