summaryrefslogtreecommitdiff
blob: 4c6ab5407e7e52a0c5f67bfe3f18fe6c67d197e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
diff -ru NVIDIA_kernel-1.0-3123-2.5/nv-linux.h NVIDIA_kernel-1.0-3123-2.5-pa/nv-linux.h
--- NVIDIA_kernel-1.0-3123-2.5/nv-linux.h	Sat Oct 26 07:38:02 2002
+++ NVIDIA_kernel-1.0-3123-2.5-pa/nv-linux.h	Sat Oct 26 07:07:06 2002
@@ -61,7 +61,7 @@
 
 #include <linux/pci.h>              /* pci_find_class, etc              */
 #include <linux/wrapper.h>          /* mem_map_reserve                  */
-#include <linux/interrupt.h>        /* tasklets, interrupt helpers      */
+#include <linux/interrupt.h>        /* early kernels: bh; newer: irq    */
 
 #include <asm/system.h>             /* cli, sli, save_flags             */
 #include <asm/io.h>                 /* ioremap, virt_to_phys            */
@@ -122,6 +122,7 @@
 #  define MEM_MAP_READ_COUNT(map_nr)    (atomic_read(&mem_map[map_nr].count))
 #  define MEM_MAP_INC_COUNT(map_nr)     (atomic_inc(&mem_map[map_nr].count))
 #  define MEM_MAP_DEC_COUNT(map_nr)     (atomic_dec(&mem_map[map_nr].count))
+#  define VMA_PRIVATE(vma)              ((void*)((vma)->vm_pte))
 #else
 #  define LINUX_VMA_OFFS(vma)           (((vma)->vm_pgoff) << PAGE_SHIFT)
 #  define GET_MODULE_SYMBOL(mod,sym)	(const void *) inter_module_get(sym)
@@ -130,6 +131,7 @@
 #  define MEM_MAP_READ_COUNT(map_nr)    (atomic_read(&(map_nr)->count))
 #  define MEM_MAP_INC_COUNT(map_nr)     (atomic_inc(&(map_nr)->count))
 #  define MEM_MAP_DEC_COUNT(map_nr)     (atomic_dec(&(map_nr)->count))
+#  define VMA_PRIVATE(vma)              ((vma)->vm_private_data)
 #endif
 
 #ifdef KERNEL_2_5
@@ -202,6 +204,7 @@
 typedef struct nv_alloc_s {
     struct nv_alloc_s *next;    
     struct vm_area_struct *vma;
+    unsigned int   usage_count;
     unsigned int   process_id;
     unsigned int   thread_gid;
     unsigned int   num_pages;
diff -ru NVIDIA_kernel-1.0-3123-2.5/nv.c NVIDIA_kernel-1.0-3123-2.5-pa/nv.c
--- NVIDIA_kernel-1.0-3123-2.5/nv.c	Sat Oct 26 07:20:31 2002
+++ NVIDIA_kernel-1.0-3123-2.5-pa/nv.c	Sat Oct 26 07:04:46 2002
@@ -74,11 +74,21 @@
 static int      nvos_is_nv_device(struct pci_dev *dev);
 static int      nvos_set_primary_card(nv_ioctl_primary_card_t *info);
 static int      nvos_probe_devices(void);
-static void *   nvos_malloc(unsigned long);
-static void     nvos_free(void **);
-
 static void     nvos_proc_create(void);
 static void     nvos_proc_remove(void);
+static void *   nvos_malloc_pages(unsigned long);
+static void     nvos_unlock_pages(void **);
+static void     nvos_free_pages(void **);
+
+#define nvos_unlock_and_free_pages(count, page_list) \
+    if (page_list) {                                 \
+        if (count == 0)                              \
+            nvos_unlock_pages(page_list);            \
+        nvos_free_pages(page_list);                  \
+    }
+
+static nv_alloc_t  *nvos_create_alloc();
+static int          nvos_free_alloc(nv_alloc_t *);
 
 /* nvl_ functions.. take a linux state device pointer */
 static nv_alloc_t   *nvl_find_alloc(nv_linux_state_t *, unsigned long, nv_alloc_t **);
@@ -364,18 +374,39 @@
  * memory on systems with high memory support enabled.
  */
 
-static void *nvos_malloc(unsigned long size)
+/* note that there's a subtle kernel interaction with regards to bookkeeping
+ * on these pages. So long as the pages are marked reserved, the kernel won't
+ * touch them (alter the usage count on them). this leads to a subtle problem
+ * with mmap. Normally, allocating the pages would set the count to 1, then 
+ * mmaping them would bump the count up to 2. The order of unmapping and freeing
+ * the pages wouldn't matter, as they wouldn't really be considered free by the
+ * kernel until the count dropped back to 0. Since the kernel won't touch the
+ * count when the page is reserved, we need to be careful about this order and
+ * unreserving the pages. if we unreserve the pages while freeing them, and the
+ * munmap comes later, the munmap code path will attempt a second free on the 
+ * same pages. We also don't have a lot of control over which comes first, 
+ * sometimes we'll get called to free the pages first, sometimes we'll get called
+ * to munmap them first. Oh, and we'll get vma open/close calls every time the
+ * process is cloned, then execv'd, and munmap == vma close.
+ * sooo, we keep our own count of the allocation usage, and don't unreserve the
+ * pages until our count drops to 0. this should currently happen in either
+ * vma_release or nvos_free, both of which will be followed by a kernel attempt
+ * to free the page. Since the page fill finally be unreserved, the kernel will
+ * reduce the count to 0 and successfully free the page for us, only once.
+ * sigh... you have to love s&%*^y interfaces that force you to *know* too much
+ * about kernel internals. 
+ */
+
+static void *nvos_malloc_pages(unsigned long pages_needed)
 {
     unsigned long *page_list = NULL;
     unsigned long *page_ptr  = NULL;
-    unsigned int   pages_needed;
     unsigned int   page_list_size;
 
     /*
      * allocate a pointer for each physical page and an
      * integer to hold the number of pages allocated
      */
-    pages_needed = (size >> PAGE_SHIFT);
     page_list_size = (pages_needed + 1) * sizeof(unsigned long *);
 
     page_list = vmalloc(page_list_size);
@@ -424,11 +455,15 @@
     return NULL;
 }
 
-static void nvos_free(void **page_list)
+// unlock the pages we've locked down for dma purposes
+static void nvos_unlock_pages(void **page_list)
 {
     unsigned long *page_ptr;
     unsigned int   pages_left;
 
+    if (page_list == NULL)
+        return;
+
     page_ptr = (unsigned long *) page_list;
 
     /* retrieve the number of pages allocated */
@@ -436,11 +471,71 @@
 
     while (pages_left) {
         mem_map_unreserve(GET_MAP_NR(*page_ptr));
+        page_ptr++;
+        pages_left--;
+    }
+}
+
+static void nvos_free_pages(void **page_list)
+{
+    unsigned long *page_ptr;
+    unsigned int   pages_left;
+
+    if (page_list == NULL)
+        return;
+
+    page_ptr = (unsigned long *) page_list;
+
+    /* retrieve the number of pages allocated */
+    pages_left = *(unsigned int *) (page_list - 1);
+
+    while (pages_left) {
         free_page((unsigned long) phys_to_virt(*page_ptr++));
         pages_left--;
     }
+}
 
-    vfree(page_list);
+static 
+nv_alloc_t *nvos_create_alloc(void)
+{
+    nv_alloc_t *at;
+
+    NV_KMALLOC(at, sizeof(nv_alloc_t));
+    if (at == NULL)
+        return NULL;
+
+    memset(at, 0, sizeof(nv_alloc_t));
+
+    at->process_id = current->pid;
+#if !defined (KERNEL_2_2)
+    at->thread_gid = current->tgid;
+#else
+    at->thread_gid = -1;
+#endif
+
+    return at;
+}
+
+static 
+int nvos_free_alloc(
+    nv_alloc_t *at
+)
+{
+    if (at == NULL)
+        return -1;
+
+    if (at->usage_count)
+        return 1;
+
+    // we keep the page_table around after freeing the pages
+    // for bookkeeping reasons. Free the page_table and assume
+    // the underlying pages are already unlocked and freed.
+    if (at->page_table)
+        vfree(at->page_table - 1);
+
+    NV_KFREE(at);
+
+    return 0;
 }
 
 static u8 nvos_find_agp_capability(struct pci_dev *dev)
@@ -981,6 +1076,12 @@
 void
 nv_kern_vma_open(struct vm_area_struct *vma)
 {
+    if (VMA_PRIVATE(vma))
+    {
+        nv_alloc_t *at = (nv_alloc_t *) VMA_PRIVATE(vma);
+        at->usage_count++;
+    }
+
     MOD_INC_USE_COUNT;
 }
 
@@ -988,6 +1089,25 @@
 void
 nv_kern_vma_release(struct vm_area_struct *vma)
 {
+    if (VMA_PRIVATE(vma))
+    {
+        nv_alloc_t *at = (nv_alloc_t *) VMA_PRIVATE(vma);
+
+        at->usage_count--;
+
+        // if usage_count is down to 0, the kernel virtual mapping was freed
+        // but the underlying physical pages were not, due to the reserved bit
+        // being set. We need to clear the reserved bit, then munmap will
+        // zap the pages and free the physical pages.
+        if (at->usage_count == 0)
+        {
+            if (at->page_table)
+                nvos_unlock_pages(at->page_table);
+            nvos_free_alloc(at);
+            VMA_PRIVATE(vma) = NULL;
+        }
+    }
+
     MOD_DEC_USE_COUNT;
 }
 
@@ -1125,6 +1245,7 @@
         nvl->tl.data = (unsigned long) nv->pdev;
         tasklet_enable(&nvl->tl);
 
+        memset(&nvl->wq, 0, sizeof(wait_queue_head_t));
         nv->flags |= NV_FLAG_OPEN;
     }
 
@@ -1310,6 +1431,8 @@
         }
 
         at->vma = vma;
+        VMA_PRIVATE(vma) = at;
+        at->usage_count++;
 
         start = vma->vm_start;
         while (pages--)
@@ -1344,6 +1467,8 @@
         }
 
         at->vma = vma;
+        VMA_PRIVATE(vma) = at;
+        at->usage_count++;
 
         if (NV_OSAGP_ENABLED(nv))
         {
@@ -2163,20 +2288,14 @@
     int rm_status = 0;
     nv_linux_state_t *nvl = (nv_linux_state_t *) nv;
 
-    NV_KMALLOC(at, sizeof(nv_alloc_t));
+    at = nvos_create_alloc();
     if (at == NULL)
         return RM_ERROR;
 
-    memset(at, 0, sizeof(nv_alloc_t));
-
     page_count = RM_PAGES_TO_OS_PAGES(page_count);
     at->num_pages = page_count;
-
-    at->process_id = current->pid;
-    at->thread_gid = current->tgid;
-
     at->class = class;
-    at->vma = NULL;
+    at->usage_count++;
 
     if (at->class == NV01_ROOT)
     {
@@ -2222,7 +2341,7 @@
             NV_ADD_AT(nvl, at);
         } else {
             /* use nvidia's nvagp support */
-            at->page_table = nvos_malloc(page_count << PAGE_SHIFT);
+            at->page_table = nvos_malloc_pages(page_count);
             if (at->page_table == NULL)
                 goto failed;
 
@@ -2246,7 +2365,7 @@
         nv->agp_buffers++;
     } else {
         /* allocate general system memory */
-        at->page_table = nvos_malloc(page_count << PAGE_SHIFT);
+        at->page_table = nvos_malloc_pages(page_count);
         if (at->page_table == NULL)
             goto failed;
 
@@ -2259,10 +2378,10 @@
 failed:
     /* free any pages we may have allocated */
     if (at->page_table)
-        nvos_free(at->page_table);
+        nvos_unlock_and_free_pages(at->usage_count, at->page_table);
+
+    nvos_free_alloc(at);
 
-    /* free it */
-    NV_KFREE(at);
     return -1;
 }
 
@@ -2300,17 +2419,19 @@
         NV_REMOVE_AT_FROM_LIST(nvl, at, prev);
         nv_unlock_at(nv);
 
+        at->usage_count--;
+
         if (NV_OSAGP_ENABLED(nv))
         {
             rmStatus = KernFreeAGPPages(pAddress, priv_data);
         } else {
             rmStatus = rm_free_agp_pages(nv, pAddress, priv_data);
-            if (rmStatus == 0x0)
-                nvos_free(at->page_table);
+            if (rmStatus == RM_OK)
+                nvos_unlock_and_free_pages(at->usage_count, at->page_table);
         }
 
         /* we may hold off on disabling agp until all buffers are freed */
-        if (rmStatus == 0x0)
+        if (rmStatus == RM_OK)
         {
             nv->agp_buffers--;
             if (!nv->agp_buffers && nv->agp_teardown)
@@ -2325,6 +2446,8 @@
         NV_REMOVE_AT_FROM_LIST(nvl, at, prev);
         nv_unlock_at(nv);
 
+        at->usage_count--;
+
         if (at->class == NV01_ROOT)
         {
             int order, i;
@@ -2342,11 +2465,13 @@
         }
         else
         {
-            nvos_free(at->page_table);
+            nvos_unlock_and_free_pages(at->usage_count, at->page_table);
         }
     }
 
-    NV_KFREE(at);
+    if (at->usage_count == 0)
+        nvos_free_alloc(at);
+
     return rmStatus;
 }
 
diff -ru NVIDIA_kernel-1.0-3123-2.5/nv.h NVIDIA_kernel-1.0-3123-2.5-pa/nv.h
--- NVIDIA_kernel-1.0-3123-2.5/nv.h	Sat Oct 26 07:21:10 2002
+++ NVIDIA_kernel-1.0-3123-2.5-pa/nv.h	Thu Oct 17 05:30:51 2002
@@ -195,8 +195,10 @@
     U032 agp_buffers;
     U032 agp_teardown;
 
+#ifndef KERNEL_2_5
     /* keep track of any pending bottom-halves */
     int bh_count;
+#endif
 
     /* copy of the video bios in system memory */
     /* used by general resman code to query bios-set values */