Skip to content

Commit a2721e9

Browse files
airliedDave Airlie
authored andcommitted
AGP fix race condition between unmapping and freeing pages
With Andi's clflush fixup, we were getting hangs on server exit, flushing the mappings after freeing each page helped. This showed up a race condition where the pages after being freed could be reused before the agp mappings had been flushed. Flushing after each single page is a bad thing for future drm work, so make the page destroy a two pass unmapping all the pages, flushing the mappings, and then destroying the pages. Signed-off-by: Dave Airlie <airlied@linux.ie> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 23fd504 commit a2721e9

6 files changed

Lines changed: 48 additions & 27 deletions

File tree

‎drivers/char/agp/agp.h‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ struct gatt_mask {
5858
* devices this will probably be ignored */
5959
};
6060

61+
#define AGP_PAGE_DESTROY_UNMAP 1
62+
#define AGP_PAGE_DESTROY_FREE 2
63+
6164
struct aper_size_info_8 {
6265
int size;
6366
int num_entries;
@@ -113,7 +116,7 @@ struct agp_bridge_driver {
113116
struct agp_memory *(*alloc_by_type) (size_t, int);
114117
void (*free_by_type)(struct agp_memory *);
115118
void *(*agp_alloc_page)(struct agp_bridge_data *);
116-
void (*agp_destroy_page)(void *);
119+
void (*agp_destroy_page)(void *, int flags);
117120
int (*agp_type_to_mask_type) (struct agp_bridge_data *, int);
118121
};
119122

@@ -267,7 +270,7 @@ int agp_generic_remove_memory(struct agp_memory *mem, off_t pg_start, int type);
267270
struct agp_memory *agp_generic_alloc_by_type(size_t page_count, int type);
268271
void agp_generic_free_by_type(struct agp_memory *curr);
269272
void *agp_generic_alloc_page(struct agp_bridge_data *bridge);
270-
void agp_generic_destroy_page(void *addr);
273+
void agp_generic_destroy_page(void *addr, int flags);
271274
void agp_free_key(int key);
272275
int agp_num_entries(void);
273276
u32 agp_collect_device_status(struct agp_bridge_data *bridge, u32 mode, u32 command);

‎drivers/char/agp/ali-agp.c‎

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -156,29 +156,34 @@ static void *m1541_alloc_page(struct agp_bridge_data *bridge)
156156
return addr;
157157
}
158158

159-
static void ali_destroy_page(void * addr)
159+
static void ali_destroy_page(void * addr, int flags)
160160
{
161161
if (addr) {
162-
global_cache_flush(); /* is this really needed? --hch */
163-
agp_generic_destroy_page(addr);
164-
global_flush_tlb();
162+
if (flags & AGP_PAGE_DESTROY_UNMAP) {
163+
global_cache_flush(); /* is this really needed? --hch */
164+
agp_generic_destroy_page(addr, flags);
165+
global_flush_tlb();
166+
} else
167+
agp_generic_destroy_page(addr, flags);
165168
}
166169
}
167170

168-
static void m1541_destroy_page(void * addr)
171+
static void m1541_destroy_page(void * addr, int flags)
169172
{
170173
u32 temp;
171174

172175
if (addr == NULL)
173176
return;
174177

175-
global_cache_flush();
178+
if (flags & AGP_PAGE_DESTROY_UNMAP) {
179+
global_cache_flush();
176180

177-
pci_read_config_dword(agp_bridge->dev, ALI_CACHE_FLUSH_CTRL, &temp);
178-
pci_write_config_dword(agp_bridge->dev, ALI_CACHE_FLUSH_CTRL,
179-
(((temp & ALI_CACHE_FLUSH_ADDR_MASK) |
180-
virt_to_gart(addr)) | ALI_CACHE_FLUSH_EN));
181-
agp_generic_destroy_page(addr);
181+
pci_read_config_dword(agp_bridge->dev, ALI_CACHE_FLUSH_CTRL, &temp);
182+
pci_write_config_dword(agp_bridge->dev, ALI_CACHE_FLUSH_CTRL,
183+
(((temp & ALI_CACHE_FLUSH_ADDR_MASK) |
184+
virt_to_gart(addr)) | ALI_CACHE_FLUSH_EN));
185+
}
186+
agp_generic_destroy_page(addr, flags);
182187
}
183188

184189

‎drivers/char/agp/backend.c‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,11 @@ static int agp_backend_initialize(struct agp_bridge_data *bridge)
189189

190190
err_out:
191191
if (bridge->driver->needs_scratch_page) {
192-
bridge->driver->agp_destroy_page(
193-
gart_to_virt(bridge->scratch_page_real));
192+
bridge->driver->agp_destroy_page(gart_to_virt(bridge->scratch_page_real),
193+
AGP_PAGE_DESTROY_UNMAP);
194194
flush_agp_mappings();
195+
bridge->driver->agp_destroy_page(gart_to_virt(bridge->scratch_page_real),
196+
AGP_PAGE_DESTROY_FREE);
195197
}
196198
if (got_gatt)
197199
bridge->driver->free_gatt_table(bridge);
@@ -215,9 +217,11 @@ static void agp_backend_cleanup(struct agp_bridge_data *bridge)
215217

216218
if (bridge->driver->agp_destroy_page &&
217219
bridge->driver->needs_scratch_page) {
218-
bridge->driver->agp_destroy_page(
219-
gart_to_virt(bridge->scratch_page_real));
220+
bridge->driver->agp_destroy_page(gart_to_virt(bridge->scratch_page_real),
221+
AGP_PAGE_DESTROY_UNMAP);
220222
flush_agp_mappings();
223+
bridge->driver->agp_destroy_page(gart_to_virt(bridge->scratch_page_real),
224+
AGP_PAGE_DESTROY_FREE);
221225
}
222226
}
223227

‎drivers/char/agp/generic.c‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,12 @@ void agp_free_memory(struct agp_memory *curr)
195195
}
196196
if (curr->page_count != 0) {
197197
for (i = 0; i < curr->page_count; i++) {
198-
curr->bridge->driver->agp_destroy_page(gart_to_virt(curr->memory[i]));
198+
curr->bridge->driver->agp_destroy_page(gart_to_virt(curr->memory[i]), AGP_PAGE_DESTROY_UNMAP);
199199
}
200200
flush_agp_mappings();
201+
for (i = 0; i < curr->page_count; i++) {
202+
curr->bridge->driver->agp_destroy_page(gart_to_virt(curr->memory[i]), AGP_PAGE_DESTROY_FREE);
203+
}
201204
}
202205
agp_free_key(curr->key);
203206
agp_free_page_array(curr);
@@ -1176,18 +1179,22 @@ void *agp_generic_alloc_page(struct agp_bridge_data *bridge)
11761179
EXPORT_SYMBOL(agp_generic_alloc_page);
11771180

11781181

1179-
void agp_generic_destroy_page(void *addr)
1182+
void agp_generic_destroy_page(void *addr, int flags)
11801183
{
11811184
struct page *page;
11821185

11831186
if (addr == NULL)
11841187
return;
11851188

11861189
page = virt_to_page(addr);
1187-
unmap_page_from_agp(page);
1188-
put_page(page);
1189-
free_page((unsigned long)addr);
1190-
atomic_dec(&agp_bridge->current_memory_agp);
1190+
if (flags & AGP_PAGE_DESTROY_UNMAP)
1191+
unmap_page_from_agp(page);
1192+
1193+
if (flags & AGP_PAGE_DESTROY_FREE) {
1194+
put_page(page);
1195+
free_page((unsigned long)addr);
1196+
atomic_dec(&agp_bridge->current_memory_agp);
1197+
}
11911198
}
11921199
EXPORT_SYMBOL(agp_generic_destroy_page);
11931200

‎drivers/char/agp/i460-agp.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,10 +536,10 @@ static void *i460_alloc_page (struct agp_bridge_data *bridge)
536536
return page;
537537
}
538538

539-
static void i460_destroy_page (void *page)
539+
static void i460_destroy_page (void *page, int flags)
540540
{
541541
if (I460_IO_PAGE_SHIFT <= PAGE_SHIFT) {
542-
agp_generic_destroy_page(page);
542+
agp_generic_destroy_page(page, flags);
543543
global_flush_tlb();
544544
}
545545
}

‎drivers/char/agp/intel-agp.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,11 @@ static void intel_i810_free_by_type(struct agp_memory *curr)
400400
if (curr->page_count == 4)
401401
i8xx_destroy_pages(gart_to_virt(curr->memory[0]));
402402
else {
403-
agp_bridge->driver->agp_destroy_page(
404-
gart_to_virt(curr->memory[0]));
403+
agp_bridge->driver->agp_destroy_page(gart_to_virt(curr->memory[0]),
404+
AGP_PAGE_DESTROY_UNMAP);
405405
global_flush_tlb();
406+
agp_bridge->driver->agp_destroy_page(gart_to_virt(curr->memory[0]),
407+
AGP_PAGE_DESTROY_FREE);
406408
}
407409
agp_free_page_array(curr);
408410
}

0 commit comments

Comments
 (0)