linux: defer vcache evictions when sleep would be needed
[openafs.git] / src / afs / afs_osi_alloc.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include "afs/param.h"
12
13
14
15
16 #include "afs/sysincludes.h"    /* Standard vendor system headers */
17 #include "afsincludes.h"        /* Afs-based standard headers */
18 #include "afs/afs_stats.h"      /* afs statistics */
19
20
21
22 #ifdef AFS_AIX41_ENV
23 #include "sys/lockl.h"
24 #include "sys/sleep.h"
25 #include "sys/syspest.h"
26 #include "sys/lock_def.h"
27 #endif
28
29 #ifndef AFS_PRIVATE_OSI_ALLOCSPACES
30
31 afs_lock_t osi_fsplock;
32 afs_lock_t osi_flplock;
33
34 static struct osi_packet {
35     struct osi_packet *next;
36 } *freePacketList = NULL, *freeSmallList = NULL;
37
38 #endif /* AFS_PRIVATE_OSI_ALLOCSPACES */
39
40 static char memZero;            /* address of 0 bytes for kmem_alloc */
41
42 #if !defined(AFS_NBSD_ENV) || defined(AFS_NBSD50_ENV)
43 void *
44 afs_osi_Alloc(size_t size)
45 {
46     AFS_STATCNT(osi_Alloc);
47     /* 0-length allocs may return NULL ptr from AFS_KALLOC, so we special-case
48      * things so that NULL returned iff an error occurred */
49     if (size == 0)
50         return &memZero;
51
52     AFS_STATS(afs_stats_cmperf.OutStandingAllocs++);
53     AFS_STATS(afs_stats_cmperf.OutStandingMemUsage += size);
54 #ifdef AFS_LINUX20_ENV
55     return osi_linux_alloc(size, 1);
56 #elif defined(AFS_FBSD_ENV)
57     return osi_fbsd_alloc(size, 1);
58 #else
59     return AFS_KALLOC(size);
60 #endif
61 }
62
63 void
64 afs_osi_Free(void *x, size_t asize)
65 {
66     AFS_STATCNT(osi_Free);
67     if (x == &memZero)
68         return;                 /* check for putting memZero back */
69
70     AFS_STATS(afs_stats_cmperf.OutStandingAllocs--);
71     AFS_STATS(afs_stats_cmperf.OutStandingMemUsage -= asize);
72 #if defined(AFS_LINUX20_ENV)
73     osi_linux_free(x);
74 #elif defined(AFS_FBSD_ENV)
75     osi_fbsd_free(x);
76 #else
77     AFS_KFREE(x, asize);
78 #endif
79 }
80
81 void
82 afs_osi_FreeStr(char *x)
83 {
84     afs_osi_Free(x, strlen(x) + 1);
85 }
86
87 #endif /* !AFS_NBSD_ENV && !defined(AFS_NBSD50_ENV) */
88
89 #ifndef AFS_PRIVATE_OSI_ALLOCSPACES
90
91 /* free space allocated by AllocLargeSpace.  Also called by mclput when freeing
92  * a packet allocated by osi_NetReceive. */
93
94 void
95 osi_FreeLargeSpace(void *adata)
96 {
97
98     AFS_ASSERT_GLOCK();
99
100     AFS_STATCNT(osi_FreeLargeSpace);
101     afs_stats_cmperf.LargeBlocksActive--;
102     ObtainWriteLock(&osi_flplock, 322);
103     ((struct osi_packet *)adata)->next = freePacketList;
104     freePacketList = adata;
105     ReleaseWriteLock(&osi_flplock);
106 }
107
108 void
109 osi_FreeSmallSpace(void *adata)
110 {
111
112     AFS_ASSERT_GLOCK();
113
114     AFS_STATCNT(osi_FreeSmallSpace);
115     afs_stats_cmperf.SmallBlocksActive--;
116     ObtainWriteLock(&osi_fsplock, 323);
117     ((struct osi_packet *)adata)->next = freeSmallList;
118     freeSmallList = adata;
119     ReleaseWriteLock(&osi_fsplock);
120 }
121
122
123 /* allocate space for sender */
124 void *
125 osi_AllocLargeSpace(size_t size)
126 {
127     struct osi_packet *tp;
128
129     AFS_ASSERT_GLOCK();
130
131     AFS_STATCNT(osi_AllocLargeSpace);
132     if (size > AFS_LRALLOCSIZ)
133         osi_Panic("osi_AllocLargeSpace: size=%d\n", (int)size);
134     afs_stats_cmperf.LargeBlocksActive++;
135     if (!freePacketList) {
136         char *p;
137
138         afs_stats_cmperf.LargeBlocksAlloced++;
139         p = afs_osi_Alloc(AFS_LRALLOCSIZ);
140 #ifdef  KERNEL_HAVE_PIN
141         /*
142          * Need to pin this memory since under heavy conditions this memory
143          * could be swapped out; the problem is that we could inside rx where
144          * interrupts are disabled and thus we would panic if we don't pin it.
145          */
146         pin(p, AFS_LRALLOCSIZ);
147 #endif
148         return p;
149     }
150     ObtainWriteLock(&osi_flplock, 324);
151     tp = freePacketList;
152     if (tp)
153         freePacketList = tp->next;
154     ReleaseWriteLock(&osi_flplock);
155     return (char *)tp;
156 }
157
158
159 /* allocate space for sender */
160 void *
161 osi_AllocSmallSpace(size_t size)
162 {
163     struct osi_packet *tp;
164
165     AFS_STATCNT(osi_AllocSmallSpace);
166     if (size > AFS_SMALLOCSIZ)
167         osi_Panic("osi_AllocSmallS: size=%d\n", (int)size);
168
169     if (!freeSmallList) {
170         afs_stats_cmperf.SmallBlocksAlloced++;
171         afs_stats_cmperf.SmallBlocksActive++;
172         tp = afs_osi_Alloc(AFS_SMALLOCSIZ);
173 #ifdef KERNEL_HAVE_PIN
174         pin((char *)tp, AFS_SMALLOCSIZ);
175 #endif
176         return (char *)tp;
177     }
178     afs_stats_cmperf.SmallBlocksActive++;
179     ObtainWriteLock(&osi_fsplock, 327);
180     tp = freeSmallList;
181     if (tp)
182         freeSmallList = tp->next;
183     ReleaseWriteLock(&osi_fsplock);
184     return (char *)tp;
185 }
186 #endif /* AFS_PRIVATE_OSI_ALLOCSPACES */
187
188 void
189 shutdown_osinet(void)
190 {
191     AFS_STATCNT(shutdown_osinet);
192 #ifndef AFS_PRIVATE_OSI_ALLOCSPACES
193     if (afs_cold_shutdown) {
194         struct osi_packet *tp;
195
196         while ((tp = freePacketList)) {
197             freePacketList = tp->next;
198             afs_osi_Free(tp, AFS_LRALLOCSIZ);
199 #ifdef  KERNEL_HAVE_PIN
200             unpin(tp, AFS_LRALLOCSIZ);
201 #endif
202         }
203
204         while ((tp = freeSmallList)) {
205             freeSmallList = tp->next;
206             afs_osi_Free(tp, AFS_SMALLOCSIZ);
207 #ifdef  KERNEL_HAVE_PIN
208             unpin(tp, AFS_SMALLOCSIZ);
209 #endif
210         }
211         LOCK_INIT(&osi_fsplock, "osi_fsplock");
212         LOCK_INIT(&osi_flplock, "osi_flplock");
213     }
214 #endif /* AFS_PRIVATE_OSI_ALLOCSPACES */
215     if (afs_stats_cmperf.LargeBlocksActive ||
216         afs_stats_cmperf.SmallBlocksActive)
217     {
218         afs_warn("WARNING: not all blocks freed: large %d small %d\n",
219                  afs_stats_cmperf.LargeBlocksActive,
220                  afs_stats_cmperf.SmallBlocksActive);
221     }
222 }
223