Blame view

fs/xfs/kmem.c 2.86 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
  /*
7b7187698   Nathan Scott   [XFS] Update lice...
2
3
   * Copyright (c) 2000-2005 Silicon Graphics, Inc.
   * All Rights Reserved.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4
   *
7b7187698   Nathan Scott   [XFS] Update lice...
5
6
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License as
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
7
8
   * published by the Free Software Foundation.
   *
7b7187698   Nathan Scott   [XFS] Update lice...
9
10
11
12
   * This program is distributed in the hope that it would be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
   *
7b7187698   Nathan Scott   [XFS] Update lice...
14
15
16
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write the Free Software Foundation,
   * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
17
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
  #include <linux/mm.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19
  #include <linux/highmem.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
20
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21
22
  #include <linux/swap.h>
  #include <linux/blkdev.h>
3fcfab16c   Andrew Morton   [PATCH] separate ...
23
  #include <linux/backing-dev.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
24
25
  #include "time.h"
  #include "kmem.h"
4f10700a2   Dave Chinner   xfs: Convert linu...
26
  #include "xfs_message.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
27

bdfb04301   Christoph Hellwig   xfs: replace KM_L...
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  /*
   * Greedy allocation.  May fail and may return vmalloced memory.
   *
   * Must be freed using kmem_free_large.
   */
  void *
  kmem_zalloc_greedy(size_t *size, size_t minsize, size_t maxsize)
  {
  	void		*ptr;
  	size_t		kmsize = maxsize;
  
  	while (!(ptr = kmem_zalloc_large(kmsize))) {
  		if ((kmsize >>= 1) <= minsize)
  			kmsize = minsize;
  	}
  	if (ptr)
  		*size = kmsize;
  	return ptr;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
47

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
48
  void *
27496a8c6   Al Viro   [PATCH] gfp_t: fs/*
49
  kmem_alloc(size_t size, unsigned int __nocast flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
50
  {
27496a8c6   Al Viro   [PATCH] gfp_t: fs/*
51
52
53
  	int	retries = 0;
  	gfp_t	lflags = kmem_flags_convert(flags);
  	void	*ptr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
54
55
  
  	do {
bdfb04301   Christoph Hellwig   xfs: replace KM_L...
56
  		ptr = kmalloc(size, lflags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
57
58
59
  		if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP)))
  			return ptr;
  		if (!(++retries % 100))
4f10700a2   Dave Chinner   xfs: Convert linu...
60
61
  			xfs_err(NULL,
  		"possible memory allocation deadlock in %s (mode:0x%x)",
34a622b2e   Harvey Harrison   [XFS] replace rem...
62
  					__func__, lflags);
8aa7e847d   Jens Axboe   Fix congestion_wa...
63
  		congestion_wait(BLK_RW_ASYNC, HZ/50);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
64
65
66
67
  	} while (1);
  }
  
  void *
27496a8c6   Al Viro   [PATCH] gfp_t: fs/*
68
  kmem_zalloc(size_t size, unsigned int __nocast flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
69
70
71
72
73
74
75
76
77
78
  {
  	void	*ptr;
  
  	ptr = kmem_alloc(size, flags);
  	if (ptr)
  		memset((char *)ptr, 0, (int)size);
  	return ptr;
  }
  
  void
d3689d768   Barry Naujok   [XFS] kmem_free a...
79
  kmem_free(const void *ptr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
80
  {
9e2779fa2   Christoph Lameter   is_vmalloc_addr()...
81
  	if (!is_vmalloc_addr(ptr)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
82
83
84
85
86
87
88
  		kfree(ptr);
  	} else {
  		vfree(ptr);
  	}
  }
  
  void *
d3689d768   Barry Naujok   [XFS] kmem_free a...
89
  kmem_realloc(const void *ptr, size_t newsize, size_t oldsize,
27496a8c6   Al Viro   [PATCH] gfp_t: fs/*
90
  	     unsigned int __nocast flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
91
92
93
94
95
96
97
98
  {
  	void	*new;
  
  	new = kmem_alloc(newsize, flags);
  	if (ptr) {
  		if (new)
  			memcpy(new, ptr,
  				((oldsize < newsize) ? oldsize : newsize));
f0e2d93c2   Denys Vlasenko   [XFS] Remove unus...
99
  		kmem_free(ptr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
100
101
102
103
104
  	}
  	return new;
  }
  
  void *
27496a8c6   Al Viro   [PATCH] gfp_t: fs/*
105
  kmem_zone_alloc(kmem_zone_t *zone, unsigned int __nocast flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
106
  {
27496a8c6   Al Viro   [PATCH] gfp_t: fs/*
107
108
109
  	int	retries = 0;
  	gfp_t	lflags = kmem_flags_convert(flags);
  	void	*ptr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
110
111
112
113
114
115
  
  	do {
  		ptr = kmem_cache_alloc(zone, lflags);
  		if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP)))
  			return ptr;
  		if (!(++retries % 100))
4f10700a2   Dave Chinner   xfs: Convert linu...
116
117
  			xfs_err(NULL,
  		"possible memory allocation deadlock in %s (mode:0x%x)",
34a622b2e   Harvey Harrison   [XFS] replace rem...
118
  					__func__, lflags);
8aa7e847d   Jens Axboe   Fix congestion_wa...
119
  		congestion_wait(BLK_RW_ASYNC, HZ/50);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
120
121
122
123
  	} while (1);
  }
  
  void *
27496a8c6   Al Viro   [PATCH] gfp_t: fs/*
124
  kmem_zone_zalloc(kmem_zone_t *zone, unsigned int __nocast flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
125
126
127
128
129
130
131
132
  {
  	void	*ptr;
  
  	ptr = kmem_zone_alloc(zone, flags);
  	if (ptr)
  		memset((char *)ptr, 0, kmem_cache_size(zone));
  	return ptr;
  }