mirror of
https://github.com/torvalds/linux.git
synced 2024-11-30 08:01:59 +00:00
3822a7c409
F_SEAL_EXEC") which permits the setting of the memfd execute bit at memfd creation time, with the option of sealing the state of the X bit. - Peter Xu adds a patch series ("mm/hugetlb: Make huge_pte_offset() thread-safe for pmd unshare") which addresses a rare race condition related to PMD unsharing. - Several folioification patch serieses from Matthew Wilcox, Vishal Moola, Sidhartha Kumar and Lorenzo Stoakes - Johannes Weiner has a series ("mm: push down lock_page_memcg()") which does perform some memcg maintenance and cleanup work. - SeongJae Park has added DAMOS filtering to DAMON, with the series "mm/damon/core: implement damos filter". These filters provide users with finer-grained control over DAMOS's actions. SeongJae has also done some DAMON cleanup work. - Kairui Song adds a series ("Clean up and fixes for swap"). - Vernon Yang contributed the series "Clean up and refinement for maple tree". - Yu Zhao has contributed the "mm: multi-gen LRU: memcg LRU" series. It adds to MGLRU an LRU of memcgs, to improve the scalability of global reclaim. - David Hildenbrand has added some userfaultfd cleanup work in the series "mm: uffd-wp + change_protection() cleanups". - Christoph Hellwig has removed the generic_writepages() library function in the series "remove generic_writepages". - Baolin Wang has performed some maintenance on the compaction code in his series "Some small improvements for compaction". - Sidhartha Kumar is doing some maintenance work on struct page in his series "Get rid of tail page fields". - David Hildenbrand contributed some cleanup, bugfixing and generalization of pte management and of pte debugging in his series "mm: support __HAVE_ARCH_PTE_SWP_EXCLUSIVE on all architectures with swap PTEs". - Mel Gorman and Neil Brown have removed the __GFP_ATOMIC allocation flag in the series "Discard __GFP_ATOMIC". - Sergey Senozhatsky has improved zsmalloc's memory utilization with his series "zsmalloc: make zspage chain size configurable". - Joey Gouly has added prctl() support for prohibiting the creation of writeable+executable mappings. The previous BPF-based approach had shortcomings. See "mm: In-kernel support for memory-deny-write-execute (MDWE)". - Waiman Long did some kmemleak cleanup and bugfixing in the series "mm/kmemleak: Simplify kmemleak_cond_resched() & fix UAF". - T.J. Alumbaugh has contributed some MGLRU cleanup work in his series "mm: multi-gen LRU: improve". - Jiaqi Yan has provided some enhancements to our memory error statistics reporting, mainly by presenting the statistics on a per-node basis. See the series "Introduce per NUMA node memory error statistics". - Mel Gorman has a second and hopefully final shot at fixing a CPU-hog regression in compaction via his series "Fix excessive CPU usage during compaction". - Christoph Hellwig does some vmalloc maintenance work in the series "cleanup vfree and vunmap". - Christoph Hellwig has removed block_device_operations.rw_page() in ths series "remove ->rw_page". - We get some maple_tree improvements and cleanups in Liam Howlett's series "VMA tree type safety and remove __vma_adjust()". - Suren Baghdasaryan has done some work on the maintainability of our vm_flags handling in the series "introduce vm_flags modifier functions". - Some pagemap cleanup and generalization work in Mike Rapoport's series "mm, arch: add generic implementation of pfn_valid() for FLATMEM" and "fixups for generic implementation of pfn_valid()" - Baoquan He has done some work to make /proc/vmallocinfo and /proc/kcore better represent the real state of things in his series "mm/vmalloc.c: allow vread() to read out vm_map_ram areas". - Jason Gunthorpe rationalized the GUP system's interface to the rest of the kernel in the series "Simplify the external interface for GUP". - SeongJae Park wishes to migrate people from DAMON's debugfs interface over to its sysfs interface. To support this, we'll temporarily be printing warnings when people use the debugfs interface. See the series "mm/damon: deprecate DAMON debugfs interface". - Andrey Konovalov provided the accurately named "lib/stackdepot: fixes and clean-ups" series. - Huang Ying has provided a dramatic reduction in migration's TLB flush IPI rates with the series "migrate_pages(): batch TLB flushing". - Arnd Bergmann has some objtool fixups in "objtool warning fixes". -----BEGIN PGP SIGNATURE----- iHUEABYIAB0WIQTTMBEPP41GrTpTJgfdBJ7gKXxAjgUCY/PoPQAKCRDdBJ7gKXxA jlvpAPsFECUBBl20qSue2zCYWnHC7Yk4q9ytTkPB/MMDrFEN9wD/SNKEm2UoK6/K DmxHkn0LAitGgJRS/W9w81yrgig9tAQ= =MlGs -----END PGP SIGNATURE----- Merge tag 'mm-stable-2023-02-20-13-37' of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Pull MM updates from Andrew Morton: - Daniel Verkamp has contributed a memfd series ("mm/memfd: add F_SEAL_EXEC") which permits the setting of the memfd execute bit at memfd creation time, with the option of sealing the state of the X bit. - Peter Xu adds a patch series ("mm/hugetlb: Make huge_pte_offset() thread-safe for pmd unshare") which addresses a rare race condition related to PMD unsharing. - Several folioification patch serieses from Matthew Wilcox, Vishal Moola, Sidhartha Kumar and Lorenzo Stoakes - Johannes Weiner has a series ("mm: push down lock_page_memcg()") which does perform some memcg maintenance and cleanup work. - SeongJae Park has added DAMOS filtering to DAMON, with the series "mm/damon/core: implement damos filter". These filters provide users with finer-grained control over DAMOS's actions. SeongJae has also done some DAMON cleanup work. - Kairui Song adds a series ("Clean up and fixes for swap"). - Vernon Yang contributed the series "Clean up and refinement for maple tree". - Yu Zhao has contributed the "mm: multi-gen LRU: memcg LRU" series. It adds to MGLRU an LRU of memcgs, to improve the scalability of global reclaim. - David Hildenbrand has added some userfaultfd cleanup work in the series "mm: uffd-wp + change_protection() cleanups". - Christoph Hellwig has removed the generic_writepages() library function in the series "remove generic_writepages". - Baolin Wang has performed some maintenance on the compaction code in his series "Some small improvements for compaction". - Sidhartha Kumar is doing some maintenance work on struct page in his series "Get rid of tail page fields". - David Hildenbrand contributed some cleanup, bugfixing and generalization of pte management and of pte debugging in his series "mm: support __HAVE_ARCH_PTE_SWP_EXCLUSIVE on all architectures with swap PTEs". - Mel Gorman and Neil Brown have removed the __GFP_ATOMIC allocation flag in the series "Discard __GFP_ATOMIC". - Sergey Senozhatsky has improved zsmalloc's memory utilization with his series "zsmalloc: make zspage chain size configurable". - Joey Gouly has added prctl() support for prohibiting the creation of writeable+executable mappings. The previous BPF-based approach had shortcomings. See "mm: In-kernel support for memory-deny-write-execute (MDWE)". - Waiman Long did some kmemleak cleanup and bugfixing in the series "mm/kmemleak: Simplify kmemleak_cond_resched() & fix UAF". - T.J. Alumbaugh has contributed some MGLRU cleanup work in his series "mm: multi-gen LRU: improve". - Jiaqi Yan has provided some enhancements to our memory error statistics reporting, mainly by presenting the statistics on a per-node basis. See the series "Introduce per NUMA node memory error statistics". - Mel Gorman has a second and hopefully final shot at fixing a CPU-hog regression in compaction via his series "Fix excessive CPU usage during compaction". - Christoph Hellwig does some vmalloc maintenance work in the series "cleanup vfree and vunmap". - Christoph Hellwig has removed block_device_operations.rw_page() in ths series "remove ->rw_page". - We get some maple_tree improvements and cleanups in Liam Howlett's series "VMA tree type safety and remove __vma_adjust()". - Suren Baghdasaryan has done some work on the maintainability of our vm_flags handling in the series "introduce vm_flags modifier functions". - Some pagemap cleanup and generalization work in Mike Rapoport's series "mm, arch: add generic implementation of pfn_valid() for FLATMEM" and "fixups for generic implementation of pfn_valid()" - Baoquan He has done some work to make /proc/vmallocinfo and /proc/kcore better represent the real state of things in his series "mm/vmalloc.c: allow vread() to read out vm_map_ram areas". - Jason Gunthorpe rationalized the GUP system's interface to the rest of the kernel in the series "Simplify the external interface for GUP". - SeongJae Park wishes to migrate people from DAMON's debugfs interface over to its sysfs interface. To support this, we'll temporarily be printing warnings when people use the debugfs interface. See the series "mm/damon: deprecate DAMON debugfs interface". - Andrey Konovalov provided the accurately named "lib/stackdepot: fixes and clean-ups" series. - Huang Ying has provided a dramatic reduction in migration's TLB flush IPI rates with the series "migrate_pages(): batch TLB flushing". - Arnd Bergmann has some objtool fixups in "objtool warning fixes". * tag 'mm-stable-2023-02-20-13-37' of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm: (505 commits) include/linux/migrate.h: remove unneeded externs mm/memory_hotplug: cleanup return value handing in do_migrate_range() mm/uffd: fix comment in handling pte markers mm: change to return bool for isolate_movable_page() mm: hugetlb: change to return bool for isolate_hugetlb() mm: change to return bool for isolate_lru_page() mm: change to return bool for folio_isolate_lru() objtool: add UACCESS exceptions for __tsan_volatile_read/write kmsan: disable ftrace in kmsan core code kasan: mark addr_has_metadata __always_inline mm: memcontrol: rename memcg_kmem_enabled() sh: initialize max_mapnr m68k/nommu: add missing definition of ARCH_PFN_OFFSET mm: percpu: fix incorrect size in pcpu_obj_full_size() maple_tree: reduce stack usage with gcc-9 and earlier mm: page_alloc: call panic() when memoryless node allocation fails mm: multi-gen LRU: avoid futile retries migrate_pages: move THP/hugetlb migration support check to simplify code migrate_pages: batch flushing TLB migrate_pages: share more code between _unmap and _move ...
518 lines
12 KiB
C
518 lines
12 KiB
C
// SPDX-License-Identifier: GPL-2.0-only
|
|
/*
|
|
* Ram backed block device driver.
|
|
*
|
|
* Copyright (C) 2007 Nick Piggin
|
|
* Copyright (C) 2007 Novell Inc.
|
|
*
|
|
* Parts derived from drivers/block/rd.c, and drivers/block/loop.c, copyright
|
|
* of their respective owners.
|
|
*/
|
|
|
|
#include <linux/init.h>
|
|
#include <linux/initrd.h>
|
|
#include <linux/module.h>
|
|
#include <linux/moduleparam.h>
|
|
#include <linux/major.h>
|
|
#include <linux/blkdev.h>
|
|
#include <linux/bio.h>
|
|
#include <linux/highmem.h>
|
|
#include <linux/mutex.h>
|
|
#include <linux/pagemap.h>
|
|
#include <linux/radix-tree.h>
|
|
#include <linux/fs.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/backing-dev.h>
|
|
#include <linux/debugfs.h>
|
|
|
|
#include <linux/uaccess.h>
|
|
|
|
/*
|
|
* Each block ramdisk device has a radix_tree brd_pages of pages that stores
|
|
* the pages containing the block device's contents. A brd page's ->index is
|
|
* its offset in PAGE_SIZE units. This is similar to, but in no way connected
|
|
* with, the kernel's pagecache or buffer cache (which sit above our block
|
|
* device).
|
|
*/
|
|
struct brd_device {
|
|
int brd_number;
|
|
struct gendisk *brd_disk;
|
|
struct list_head brd_list;
|
|
|
|
/*
|
|
* Backing store of pages and lock to protect it. This is the contents
|
|
* of the block device.
|
|
*/
|
|
spinlock_t brd_lock;
|
|
struct radix_tree_root brd_pages;
|
|
u64 brd_nr_pages;
|
|
};
|
|
|
|
/*
|
|
* Look up and return a brd's page for a given sector.
|
|
*/
|
|
static struct page *brd_lookup_page(struct brd_device *brd, sector_t sector)
|
|
{
|
|
pgoff_t idx;
|
|
struct page *page;
|
|
|
|
/*
|
|
* The page lifetime is protected by the fact that we have opened the
|
|
* device node -- brd pages will never be deleted under us, so we
|
|
* don't need any further locking or refcounting.
|
|
*
|
|
* This is strictly true for the radix-tree nodes as well (ie. we
|
|
* don't actually need the rcu_read_lock()), however that is not a
|
|
* documented feature of the radix-tree API so it is better to be
|
|
* safe here (we don't have total exclusion from radix tree updates
|
|
* here, only deletes).
|
|
*/
|
|
rcu_read_lock();
|
|
idx = sector >> PAGE_SECTORS_SHIFT; /* sector to page index */
|
|
page = radix_tree_lookup(&brd->brd_pages, idx);
|
|
rcu_read_unlock();
|
|
|
|
BUG_ON(page && page->index != idx);
|
|
|
|
return page;
|
|
}
|
|
|
|
/*
|
|
* Insert a new page for a given sector, if one does not already exist.
|
|
*/
|
|
static int brd_insert_page(struct brd_device *brd, sector_t sector, gfp_t gfp)
|
|
{
|
|
pgoff_t idx;
|
|
struct page *page;
|
|
int ret = 0;
|
|
|
|
page = brd_lookup_page(brd, sector);
|
|
if (page)
|
|
return 0;
|
|
|
|
page = alloc_page(gfp | __GFP_ZERO | __GFP_HIGHMEM);
|
|
if (!page)
|
|
return -ENOMEM;
|
|
|
|
if (radix_tree_maybe_preload(gfp)) {
|
|
__free_page(page);
|
|
return -ENOMEM;
|
|
}
|
|
|
|
spin_lock(&brd->brd_lock);
|
|
idx = sector >> PAGE_SECTORS_SHIFT;
|
|
page->index = idx;
|
|
if (radix_tree_insert(&brd->brd_pages, idx, page)) {
|
|
__free_page(page);
|
|
page = radix_tree_lookup(&brd->brd_pages, idx);
|
|
if (!page)
|
|
ret = -ENOMEM;
|
|
else if (page->index != idx)
|
|
ret = -EIO;
|
|
} else {
|
|
brd->brd_nr_pages++;
|
|
}
|
|
spin_unlock(&brd->brd_lock);
|
|
|
|
radix_tree_preload_end();
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* Free all backing store pages and radix tree. This must only be called when
|
|
* there are no other users of the device.
|
|
*/
|
|
#define FREE_BATCH 16
|
|
static void brd_free_pages(struct brd_device *brd)
|
|
{
|
|
unsigned long pos = 0;
|
|
struct page *pages[FREE_BATCH];
|
|
int nr_pages;
|
|
|
|
do {
|
|
int i;
|
|
|
|
nr_pages = radix_tree_gang_lookup(&brd->brd_pages,
|
|
(void **)pages, pos, FREE_BATCH);
|
|
|
|
for (i = 0; i < nr_pages; i++) {
|
|
void *ret;
|
|
|
|
BUG_ON(pages[i]->index < pos);
|
|
pos = pages[i]->index;
|
|
ret = radix_tree_delete(&brd->brd_pages, pos);
|
|
BUG_ON(!ret || ret != pages[i]);
|
|
__free_page(pages[i]);
|
|
}
|
|
|
|
pos++;
|
|
|
|
/*
|
|
* It takes 3.4 seconds to remove 80GiB ramdisk.
|
|
* So, we need cond_resched to avoid stalling the CPU.
|
|
*/
|
|
cond_resched();
|
|
|
|
/*
|
|
* This assumes radix_tree_gang_lookup always returns as
|
|
* many pages as possible. If the radix-tree code changes,
|
|
* so will this have to.
|
|
*/
|
|
} while (nr_pages == FREE_BATCH);
|
|
}
|
|
|
|
/*
|
|
* copy_to_brd_setup must be called before copy_to_brd. It may sleep.
|
|
*/
|
|
static int copy_to_brd_setup(struct brd_device *brd, sector_t sector, size_t n,
|
|
gfp_t gfp)
|
|
{
|
|
unsigned int offset = (sector & (PAGE_SECTORS-1)) << SECTOR_SHIFT;
|
|
size_t copy;
|
|
int ret;
|
|
|
|
copy = min_t(size_t, n, PAGE_SIZE - offset);
|
|
ret = brd_insert_page(brd, sector, gfp);
|
|
if (ret)
|
|
return ret;
|
|
if (copy < n) {
|
|
sector += copy >> SECTOR_SHIFT;
|
|
ret = brd_insert_page(brd, sector, gfp);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* Copy n bytes from src to the brd starting at sector. Does not sleep.
|
|
*/
|
|
static void copy_to_brd(struct brd_device *brd, const void *src,
|
|
sector_t sector, size_t n)
|
|
{
|
|
struct page *page;
|
|
void *dst;
|
|
unsigned int offset = (sector & (PAGE_SECTORS-1)) << SECTOR_SHIFT;
|
|
size_t copy;
|
|
|
|
copy = min_t(size_t, n, PAGE_SIZE - offset);
|
|
page = brd_lookup_page(brd, sector);
|
|
BUG_ON(!page);
|
|
|
|
dst = kmap_atomic(page);
|
|
memcpy(dst + offset, src, copy);
|
|
kunmap_atomic(dst);
|
|
|
|
if (copy < n) {
|
|
src += copy;
|
|
sector += copy >> SECTOR_SHIFT;
|
|
copy = n - copy;
|
|
page = brd_lookup_page(brd, sector);
|
|
BUG_ON(!page);
|
|
|
|
dst = kmap_atomic(page);
|
|
memcpy(dst, src, copy);
|
|
kunmap_atomic(dst);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Copy n bytes to dst from the brd starting at sector. Does not sleep.
|
|
*/
|
|
static void copy_from_brd(void *dst, struct brd_device *brd,
|
|
sector_t sector, size_t n)
|
|
{
|
|
struct page *page;
|
|
void *src;
|
|
unsigned int offset = (sector & (PAGE_SECTORS-1)) << SECTOR_SHIFT;
|
|
size_t copy;
|
|
|
|
copy = min_t(size_t, n, PAGE_SIZE - offset);
|
|
page = brd_lookup_page(brd, sector);
|
|
if (page) {
|
|
src = kmap_atomic(page);
|
|
memcpy(dst, src + offset, copy);
|
|
kunmap_atomic(src);
|
|
} else
|
|
memset(dst, 0, copy);
|
|
|
|
if (copy < n) {
|
|
dst += copy;
|
|
sector += copy >> SECTOR_SHIFT;
|
|
copy = n - copy;
|
|
page = brd_lookup_page(brd, sector);
|
|
if (page) {
|
|
src = kmap_atomic(page);
|
|
memcpy(dst, src, copy);
|
|
kunmap_atomic(src);
|
|
} else
|
|
memset(dst, 0, copy);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Process a single bvec of a bio.
|
|
*/
|
|
static int brd_do_bvec(struct brd_device *brd, struct page *page,
|
|
unsigned int len, unsigned int off, blk_opf_t opf,
|
|
sector_t sector)
|
|
{
|
|
void *mem;
|
|
int err = 0;
|
|
|
|
if (op_is_write(opf)) {
|
|
/*
|
|
* Must use NOIO because we don't want to recurse back into the
|
|
* block or filesystem layers from page reclaim.
|
|
*/
|
|
gfp_t gfp = opf & REQ_NOWAIT ? GFP_NOWAIT : GFP_NOIO;
|
|
|
|
err = copy_to_brd_setup(brd, sector, len, gfp);
|
|
if (err)
|
|
goto out;
|
|
}
|
|
|
|
mem = kmap_atomic(page);
|
|
if (!op_is_write(opf)) {
|
|
copy_from_brd(mem + off, brd, sector, len);
|
|
flush_dcache_page(page);
|
|
} else {
|
|
flush_dcache_page(page);
|
|
copy_to_brd(brd, mem + off, sector, len);
|
|
}
|
|
kunmap_atomic(mem);
|
|
|
|
out:
|
|
return err;
|
|
}
|
|
|
|
static void brd_submit_bio(struct bio *bio)
|
|
{
|
|
struct brd_device *brd = bio->bi_bdev->bd_disk->private_data;
|
|
sector_t sector = bio->bi_iter.bi_sector;
|
|
struct bio_vec bvec;
|
|
struct bvec_iter iter;
|
|
|
|
bio_for_each_segment(bvec, bio, iter) {
|
|
unsigned int len = bvec.bv_len;
|
|
int err;
|
|
|
|
/* Don't support un-aligned buffer */
|
|
WARN_ON_ONCE((bvec.bv_offset & (SECTOR_SIZE - 1)) ||
|
|
(len & (SECTOR_SIZE - 1)));
|
|
|
|
err = brd_do_bvec(brd, bvec.bv_page, len, bvec.bv_offset,
|
|
bio->bi_opf, sector);
|
|
if (err) {
|
|
if (err == -ENOMEM && bio->bi_opf & REQ_NOWAIT) {
|
|
bio_wouldblock_error(bio);
|
|
return;
|
|
}
|
|
bio_io_error(bio);
|
|
return;
|
|
}
|
|
sector += len >> SECTOR_SHIFT;
|
|
}
|
|
|
|
bio_endio(bio);
|
|
}
|
|
|
|
static const struct block_device_operations brd_fops = {
|
|
.owner = THIS_MODULE,
|
|
.submit_bio = brd_submit_bio,
|
|
};
|
|
|
|
/*
|
|
* And now the modules code and kernel interface.
|
|
*/
|
|
static int rd_nr = CONFIG_BLK_DEV_RAM_COUNT;
|
|
module_param(rd_nr, int, 0444);
|
|
MODULE_PARM_DESC(rd_nr, "Maximum number of brd devices");
|
|
|
|
unsigned long rd_size = CONFIG_BLK_DEV_RAM_SIZE;
|
|
module_param(rd_size, ulong, 0444);
|
|
MODULE_PARM_DESC(rd_size, "Size of each RAM disk in kbytes.");
|
|
|
|
static int max_part = 1;
|
|
module_param(max_part, int, 0444);
|
|
MODULE_PARM_DESC(max_part, "Num Minors to reserve between devices");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
MODULE_ALIAS_BLOCKDEV_MAJOR(RAMDISK_MAJOR);
|
|
MODULE_ALIAS("rd");
|
|
|
|
#ifndef MODULE
|
|
/* Legacy boot options - nonmodular */
|
|
static int __init ramdisk_size(char *str)
|
|
{
|
|
rd_size = simple_strtol(str, NULL, 0);
|
|
return 1;
|
|
}
|
|
__setup("ramdisk_size=", ramdisk_size);
|
|
#endif
|
|
|
|
/*
|
|
* The device scheme is derived from loop.c. Keep them in synch where possible
|
|
* (should share code eventually).
|
|
*/
|
|
static LIST_HEAD(brd_devices);
|
|
static struct dentry *brd_debugfs_dir;
|
|
|
|
static int brd_alloc(int i)
|
|
{
|
|
struct brd_device *brd;
|
|
struct gendisk *disk;
|
|
char buf[DISK_NAME_LEN];
|
|
int err = -ENOMEM;
|
|
|
|
list_for_each_entry(brd, &brd_devices, brd_list)
|
|
if (brd->brd_number == i)
|
|
return -EEXIST;
|
|
brd = kzalloc(sizeof(*brd), GFP_KERNEL);
|
|
if (!brd)
|
|
return -ENOMEM;
|
|
brd->brd_number = i;
|
|
list_add_tail(&brd->brd_list, &brd_devices);
|
|
|
|
spin_lock_init(&brd->brd_lock);
|
|
INIT_RADIX_TREE(&brd->brd_pages, GFP_ATOMIC);
|
|
|
|
snprintf(buf, DISK_NAME_LEN, "ram%d", i);
|
|
if (!IS_ERR_OR_NULL(brd_debugfs_dir))
|
|
debugfs_create_u64(buf, 0444, brd_debugfs_dir,
|
|
&brd->brd_nr_pages);
|
|
|
|
disk = brd->brd_disk = blk_alloc_disk(NUMA_NO_NODE);
|
|
if (!disk)
|
|
goto out_free_dev;
|
|
|
|
disk->major = RAMDISK_MAJOR;
|
|
disk->first_minor = i * max_part;
|
|
disk->minors = max_part;
|
|
disk->fops = &brd_fops;
|
|
disk->private_data = brd;
|
|
strscpy(disk->disk_name, buf, DISK_NAME_LEN);
|
|
set_capacity(disk, rd_size * 2);
|
|
|
|
/*
|
|
* This is so fdisk will align partitions on 4k, because of
|
|
* direct_access API needing 4k alignment, returning a PFN
|
|
* (This is only a problem on very small devices <= 4M,
|
|
* otherwise fdisk will align on 1M. Regardless this call
|
|
* is harmless)
|
|
*/
|
|
blk_queue_physical_block_size(disk->queue, PAGE_SIZE);
|
|
|
|
/* Tell the block layer that this is not a rotational device */
|
|
blk_queue_flag_set(QUEUE_FLAG_NONROT, disk->queue);
|
|
blk_queue_flag_set(QUEUE_FLAG_SYNCHRONOUS, disk->queue);
|
|
blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, disk->queue);
|
|
blk_queue_flag_set(QUEUE_FLAG_NOWAIT, disk->queue);
|
|
err = add_disk(disk);
|
|
if (err)
|
|
goto out_cleanup_disk;
|
|
|
|
return 0;
|
|
|
|
out_cleanup_disk:
|
|
put_disk(disk);
|
|
out_free_dev:
|
|
list_del(&brd->brd_list);
|
|
kfree(brd);
|
|
return err;
|
|
}
|
|
|
|
static void brd_probe(dev_t dev)
|
|
{
|
|
brd_alloc(MINOR(dev) / max_part);
|
|
}
|
|
|
|
static void brd_cleanup(void)
|
|
{
|
|
struct brd_device *brd, *next;
|
|
|
|
debugfs_remove_recursive(brd_debugfs_dir);
|
|
|
|
list_for_each_entry_safe(brd, next, &brd_devices, brd_list) {
|
|
del_gendisk(brd->brd_disk);
|
|
put_disk(brd->brd_disk);
|
|
brd_free_pages(brd);
|
|
list_del(&brd->brd_list);
|
|
kfree(brd);
|
|
}
|
|
}
|
|
|
|
static inline void brd_check_and_reset_par(void)
|
|
{
|
|
if (unlikely(!max_part))
|
|
max_part = 1;
|
|
|
|
/*
|
|
* make sure 'max_part' can be divided exactly by (1U << MINORBITS),
|
|
* otherwise, it is possiable to get same dev_t when adding partitions.
|
|
*/
|
|
if ((1U << MINORBITS) % max_part != 0)
|
|
max_part = 1UL << fls(max_part);
|
|
|
|
if (max_part > DISK_MAX_PARTS) {
|
|
pr_info("brd: max_part can't be larger than %d, reset max_part = %d.\n",
|
|
DISK_MAX_PARTS, DISK_MAX_PARTS);
|
|
max_part = DISK_MAX_PARTS;
|
|
}
|
|
}
|
|
|
|
static int __init brd_init(void)
|
|
{
|
|
int err, i;
|
|
|
|
brd_check_and_reset_par();
|
|
|
|
brd_debugfs_dir = debugfs_create_dir("ramdisk_pages", NULL);
|
|
|
|
for (i = 0; i < rd_nr; i++) {
|
|
err = brd_alloc(i);
|
|
if (err)
|
|
goto out_free;
|
|
}
|
|
|
|
/*
|
|
* brd module now has a feature to instantiate underlying device
|
|
* structure on-demand, provided that there is an access dev node.
|
|
*
|
|
* (1) if rd_nr is specified, create that many upfront. else
|
|
* it defaults to CONFIG_BLK_DEV_RAM_COUNT
|
|
* (2) User can further extend brd devices by create dev node themselves
|
|
* and have kernel automatically instantiate actual device
|
|
* on-demand. Example:
|
|
* mknod /path/devnod_name b 1 X # 1 is the rd major
|
|
* fdisk -l /path/devnod_name
|
|
* If (X / max_part) was not already created it will be created
|
|
* dynamically.
|
|
*/
|
|
|
|
if (__register_blkdev(RAMDISK_MAJOR, "ramdisk", brd_probe)) {
|
|
err = -EIO;
|
|
goto out_free;
|
|
}
|
|
|
|
pr_info("brd: module loaded\n");
|
|
return 0;
|
|
|
|
out_free:
|
|
brd_cleanup();
|
|
|
|
pr_info("brd: module NOT loaded !!!\n");
|
|
return err;
|
|
}
|
|
|
|
static void __exit brd_exit(void)
|
|
{
|
|
|
|
unregister_blkdev(RAMDISK_MAJOR, "ramdisk");
|
|
brd_cleanup();
|
|
|
|
pr_info("brd: module unloaded\n");
|
|
}
|
|
|
|
module_init(brd_init);
|
|
module_exit(brd_exit);
|
|
|