Skip to content

Commit 1d5cfdb

Browse files
Tetsuo Handatorvalds
authored andcommitted
tree wide: use kvfree() than conditional kfree()/vfree()
There are many locations that do if (memory_was_allocated_by_vmalloc) vfree(ptr); else kfree(ptr); but kvfree() can handle both kmalloc()ed memory and vmalloc()ed memory using is_vmalloc_addr(). Unless callers have special reasons, we can replace this branch with kvfree(). Please check and reply if you found problems. Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Acked-by: Michal Hocko <mhocko@suse.com> Acked-by: Jan Kara <jack@suse.com> Acked-by: Russell King <rmk+kernel@arm.linux.org.uk> Reviewed-by: Andreas Dilger <andreas.dilger@intel.com> Acked-by: "Rafael J. Wysocki" <rjw@rjwysocki.net> Acked-by: David Rientjes <rientjes@google.com> Cc: "Luck, Tony" <tony.luck@intel.com> Cc: Oleg Drokin <oleg.drokin@intel.com> Cc: Boris Petkov <bp@suse.de> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent eab95db commit 1d5cfdb

17 files changed

Lines changed: 36 additions & 103 deletions

File tree

‎arch/arm/mm/dma-mapping.c‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,18 +1200,14 @@ static struct page **__iommu_alloc_buffer(struct device *dev, size_t size,
12001200
while (i--)
12011201
if (pages[i])
12021202
__free_pages(pages[i], 0);
1203-
if (array_size <= PAGE_SIZE)
1204-
kfree(pages);
1205-
else
1206-
vfree(pages);
1203+
kvfree(pages);
12071204
return NULL;
12081205
}
12091206

12101207
static int __iommu_free_buffer(struct device *dev, struct page **pages,
12111208
size_t size, struct dma_attrs *attrs)
12121209
{
12131210
int count = size >> PAGE_SHIFT;
1214-
int array_size = count * sizeof(struct page *);
12151211
int i;
12161212

12171213
if (dma_get_attr(DMA_ATTR_FORCE_CONTIGUOUS, attrs)) {
@@ -1222,10 +1218,7 @@ static int __iommu_free_buffer(struct device *dev, struct page **pages,
12221218
__free_pages(pages[i], 0);
12231219
}
12241220

1225-
if (array_size <= PAGE_SIZE)
1226-
kfree(pages);
1227-
else
1228-
vfree(pages);
1221+
kvfree(pages);
12291222
return 0;
12301223
}
12311224

‎drivers/acpi/apei/erst.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <linux/hardirq.h>
3333
#include <linux/pstore.h>
3434
#include <linux/vmalloc.h>
35+
#include <linux/mm.h> /* kvfree() */
3536
#include <acpi/apei.h>
3637

3738
#include "apei-internal.h"
@@ -532,10 +533,7 @@ static int __erst_record_id_cache_add_one(void)
532533
return -ENOMEM;
533534
memcpy(new_entries, entries,
534535
erst_record_id_cache.len * sizeof(entries[0]));
535-
if (erst_record_id_cache.size < PAGE_SIZE)
536-
kfree(entries);
537-
else
538-
vfree(entries);
536+
kvfree(entries);
539537
erst_record_id_cache.entries = entries = new_entries;
540538
erst_record_id_cache.size = new_size;
541539
}

‎drivers/block/drbd/drbd_bitmap.c‎

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -364,12 +364,9 @@ static void bm_free_pages(struct page **pages, unsigned long number)
364364
}
365365
}
366366

367-
static void bm_vk_free(void *ptr, int v)
367+
static inline void bm_vk_free(void *ptr)
368368
{
369-
if (v)
370-
vfree(ptr);
371-
else
372-
kfree(ptr);
369+
kvfree(ptr);
373370
}
374371

375372
/*
@@ -379,7 +376,7 @@ static struct page **bm_realloc_pages(struct drbd_bitmap *b, unsigned long want)
379376
{
380377
struct page **old_pages = b->bm_pages;
381378
struct page **new_pages, *page;
382-
unsigned int i, bytes, vmalloced = 0;
379+
unsigned int i, bytes;
383380
unsigned long have = b->bm_number_of_pages;
384381

385382
BUG_ON(have == 0 && old_pages != NULL);
@@ -401,7 +398,6 @@ static struct page **bm_realloc_pages(struct drbd_bitmap *b, unsigned long want)
401398
PAGE_KERNEL);
402399
if (!new_pages)
403400
return NULL;
404-
vmalloced = 1;
405401
}
406402

407403
if (want >= have) {
@@ -411,7 +407,7 @@ static struct page **bm_realloc_pages(struct drbd_bitmap *b, unsigned long want)
411407
page = alloc_page(GFP_NOIO | __GFP_HIGHMEM);
412408
if (!page) {
413409
bm_free_pages(new_pages + have, i - have);
414-
bm_vk_free(new_pages, vmalloced);
410+
bm_vk_free(new_pages);
415411
return NULL;
416412
}
417413
/* we want to know which page it is
@@ -427,11 +423,6 @@ static struct page **bm_realloc_pages(struct drbd_bitmap *b, unsigned long want)
427423
*/
428424
}
429425

430-
if (vmalloced)
431-
b->bm_flags |= BM_P_VMALLOCED;
432-
else
433-
b->bm_flags &= ~BM_P_VMALLOCED;
434-
435426
return new_pages;
436427
}
437428

@@ -469,7 +460,7 @@ void drbd_bm_cleanup(struct drbd_device *device)
469460
if (!expect(device->bitmap))
470461
return;
471462
bm_free_pages(device->bitmap->bm_pages, device->bitmap->bm_number_of_pages);
472-
bm_vk_free(device->bitmap->bm_pages, (BM_P_VMALLOCED & device->bitmap->bm_flags));
463+
bm_vk_free(device->bitmap->bm_pages);
473464
kfree(device->bitmap);
474465
device->bitmap = NULL;
475466
}
@@ -643,7 +634,6 @@ int drbd_bm_resize(struct drbd_device *device, sector_t capacity, int set_new_bi
643634
unsigned long want, have, onpages; /* number of pages */
644635
struct page **npages, **opages = NULL;
645636
int err = 0, growing;
646-
int opages_vmalloced;
647637

648638
if (!expect(b))
649639
return -ENOMEM;
@@ -656,8 +646,6 @@ int drbd_bm_resize(struct drbd_device *device, sector_t capacity, int set_new_bi
656646
if (capacity == b->bm_dev_capacity)
657647
goto out;
658648

659-
opages_vmalloced = (BM_P_VMALLOCED & b->bm_flags);
660-
661649
if (capacity == 0) {
662650
spin_lock_irq(&b->bm_lock);
663651
opages = b->bm_pages;
@@ -671,7 +659,7 @@ int drbd_bm_resize(struct drbd_device *device, sector_t capacity, int set_new_bi
671659
b->bm_dev_capacity = 0;
672660
spin_unlock_irq(&b->bm_lock);
673661
bm_free_pages(opages, onpages);
674-
bm_vk_free(opages, opages_vmalloced);
662+
bm_vk_free(opages);
675663
goto out;
676664
}
677665
bits = BM_SECT_TO_BIT(ALIGN(capacity, BM_SECT_PER_BIT));
@@ -744,7 +732,7 @@ int drbd_bm_resize(struct drbd_device *device, sector_t capacity, int set_new_bi
744732

745733
spin_unlock_irq(&b->bm_lock);
746734
if (opages != npages)
747-
bm_vk_free(opages, opages_vmalloced);
735+
bm_vk_free(opages);
748736
if (!growing)
749737
b->bm_set = bm_count_bits(b);
750738
drbd_info(device, "resync bitmap: bits=%lu words=%lu pages=%lu\n", bits, words, want);

‎drivers/block/drbd/drbd_int.h‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -536,9 +536,6 @@ struct drbd_bitmap; /* opaque for drbd_device */
536536
/* definition of bits in bm_flags to be used in drbd_bm_lock
537537
* and drbd_bitmap_io and friends. */
538538
enum bm_flag {
539-
/* do we need to kfree, or vfree bm_pages? */
540-
BM_P_VMALLOCED = 0x10000, /* internal use only, will be masked out */
541-
542539
/* currently locked for bulk operation */
543540
BM_LOCKED_MASK = 0xf,
544541

‎drivers/char/mspec.c‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,11 @@ struct vma_data {
9393
spinlock_t lock; /* Serialize access to this structure. */
9494
int count; /* Number of pages allocated. */
9595
enum mspec_page_type type; /* Type of pages allocated. */
96-
int flags; /* See VMD_xxx below. */
9796
unsigned long vm_start; /* Original (unsplit) base. */
9897
unsigned long vm_end; /* Original (unsplit) end. */
9998
unsigned long maddr[0]; /* Array of MSPEC addresses. */
10099
};
101100

102-
#define VMD_VMALLOCED 0x1 /* vmalloc'd rather than kmalloc'd */
103-
104101
/* used on shub2 to clear FOP cache in the HUB */
105102
static unsigned long scratch_page[MAX_NUMNODES];
106103
#define SH2_AMO_CACHE_ENTRIES 4
@@ -185,10 +182,7 @@ mspec_close(struct vm_area_struct *vma)
185182
"failed to zero page %ld\n", my_page);
186183
}
187184

188-
if (vdata->flags & VMD_VMALLOCED)
189-
vfree(vdata);
190-
else
191-
kfree(vdata);
185+
kvfree(vdata);
192186
}
193187

194188
/*
@@ -256,7 +250,7 @@ mspec_mmap(struct file *file, struct vm_area_struct *vma,
256250
enum mspec_page_type type)
257251
{
258252
struct vma_data *vdata;
259-
int pages, vdata_size, flags = 0;
253+
int pages, vdata_size;
260254

261255
if (vma->vm_pgoff != 0)
262256
return -EINVAL;
@@ -271,16 +265,13 @@ mspec_mmap(struct file *file, struct vm_area_struct *vma,
271265
vdata_size = sizeof(struct vma_data) + pages * sizeof(long);
272266
if (vdata_size <= PAGE_SIZE)
273267
vdata = kzalloc(vdata_size, GFP_KERNEL);
274-
else {
268+
else
275269
vdata = vzalloc(vdata_size);
276-
flags = VMD_VMALLOCED;
277-
}
278270
if (!vdata)
279271
return -ENOMEM;
280272

281273
vdata->vm_start = vma->vm_start;
282274
vdata->vm_end = vma->vm_end;
283-
vdata->flags = flags;
284275
vdata->type = type;
285276
spin_lock_init(&vdata->lock);
286277
atomic_set(&vdata->refcnt, 1);

‎drivers/gpu/drm/drm_hashtab.c‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,7 @@ EXPORT_SYMBOL(drm_ht_remove_item);
198198
void drm_ht_remove(struct drm_open_hash *ht)
199199
{
200200
if (ht->table) {
201-
if ((PAGE_SIZE / sizeof(*ht->table)) >> ht->order)
202-
kfree(ht->table);
203-
else
204-
vfree(ht->table);
201+
kvfree(ht->table);
205202
ht->table = NULL;
206203
}
207204
}

‎drivers/staging/lustre/include/linux/libcfs/libcfs_private.h‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,12 @@ do { \
151151

152152
#define LIBCFS_FREE(ptr, size) \
153153
do { \
154-
int s = (size); \
155154
if (unlikely((ptr) == NULL)) { \
156155
CERROR("LIBCFS: free NULL '" #ptr "' (%d bytes) at " \
157-
"%s:%d\n", s, __FILE__, __LINE__); \
156+
"%s:%d\n", (int)(size), __FILE__, __LINE__); \
158157
break; \
159158
} \
160-
if (unlikely(s > LIBCFS_VMALLOC_SIZE)) \
161-
vfree(ptr); \
162-
else \
163-
kfree(ptr); \
159+
kvfree(ptr); \
164160
} while (0)
165161

166162
/******************************************************************************/

‎fs/coda/coda_linux.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ void coda_sysctl_clean(void);
7272
} while (0)
7373

7474

75-
#define CODA_FREE(ptr,size) \
76-
do { if (size < PAGE_SIZE) kfree((ptr)); else vfree((ptr)); } while (0)
75+
#define CODA_FREE(ptr, size) kvfree((ptr))
7776

7877
/* inode to cnode access functions */
7978

‎fs/jffs2/build.c‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <linux/slab.h>
1818
#include <linux/vmalloc.h>
1919
#include <linux/mtd/mtd.h>
20+
#include <linux/mm.h> /* kvfree() */
2021
#include "nodelist.h"
2122

2223
static void jffs2_build_remove_unlinked_inode(struct jffs2_sb_info *,
@@ -383,12 +384,7 @@ int jffs2_do_mount_fs(struct jffs2_sb_info *c)
383384
return 0;
384385

385386
out_free:
386-
#ifndef __ECOS
387-
if (jffs2_blocks_use_vmalloc(c))
388-
vfree(c->blocks);
389-
else
390-
#endif
391-
kfree(c->blocks);
387+
kvfree(c->blocks);
392388

393389
return ret;
394390
}

‎fs/jffs2/fs.c‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -596,10 +596,7 @@ int jffs2_do_fill_super(struct super_block *sb, void *data, int silent)
596596
out_root:
597597
jffs2_free_ino_caches(c);
598598
jffs2_free_raw_node_refs(c);
599-
if (jffs2_blocks_use_vmalloc(c))
600-
vfree(c->blocks);
601-
else
602-
kfree(c->blocks);
599+
kvfree(c->blocks);
603600
out_inohash:
604601
jffs2_clear_xattr_subsystem(c);
605602
kfree(c->inocache_list);

0 commit comments

Comments
 (0)