123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394 |
- /*
- * Compressed RAM block device
- *
- * Copyright (C) 2008, 2009, 2010 Nitin Gupta
- * 2012, 2013 Minchan Kim
- *
- * This code is released using a dual license strategy: BSD/GPL
- * You can choose the licence that better fits your requirements.
- *
- * Released under the terms of 3-clause BSD License
- * Released under the terms of GNU General Public License Version 2.0
- *
- */
- #define KMSG_COMPONENT "zram"
- #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/bio.h>
- #include <linux/bitops.h>
- #include <linux/blkdev.h>
- #include <linux/buffer_head.h>
- #include <linux/device.h>
- #include <linux/genhd.h>
- #include <linux/highmem.h>
- #include <linux/slab.h>
- #include <linux/backing-dev.h>
- #include <linux/string.h>
- #include <linux/vmalloc.h>
- #include <linux/err.h>
- #include <linux/idr.h>
- #include <linux/sysfs.h>
- #include <linux/debugfs.h>
- #include <linux/cpuhotplug.h>
- #include <linux/fb.h>
- #include <linux/input.h>
- #include <linux/power_supply.h>
- #include "zram_drv.h"
- static DEFINE_IDR(zram_index_idr);
- /* idr index must be protected */
- static DEFINE_MUTEX(zram_index_mutex);
- static int zram_major;
- static struct zram *zram_devices;
- static const char *default_compressor = CONFIG_ZRAM_DEF_COMP;
- /* Module params (documentation at end) */
- static unsigned int num_devices = 1;
- /*
- * Pages that compress to sizes equals or greater than this are stored
- * uncompressed in memory.
- */
- static size_t huge_class_size;
- static bool screen_on = true;
- static unsigned long long total_touch_clock;
- module_param(total_touch_clock, ullong, 0644);
- static DEFINE_MUTEX(zram_wb_wakelock_mutex);
- static struct wakeup_source zram_wb_wakelock;
- /*
- * Blocks that hasn't been accessed in wb_start_mins
- * while the touch event is present will be writebacked.
- *
- * This is to prevent writebacking blocks that'll be used
- * right after the screen goes back on.
- * (e.g., using the phone after waking up in the morning)
- *
- * Note that total_touch_clock is increased only during
- * BTN_TOUCH events are received.
- * This means that wb_start_mins will work with BTN_TOUCH's
- * perspective, not system uptime.
- */
- static int wb_start_mins = 240;
- module_param(wb_start_mins, int, 0644);
- static struct work_struct zram_wb_fb_worker;
- static void zram_free_page(struct zram *zram, size_t index);
- static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
- u32 index, int offset, struct bio *bio);
- static int zram_slot_trylock(struct zram *zram, u32 index)
- {
- return bit_spin_trylock(ZRAM_LOCK, &zram->table[index].flags);
- }
- static void zram_slot_lock(struct zram *zram, u32 index)
- {
- bit_spin_lock(ZRAM_LOCK, &zram->table[index].flags);
- }
- static void zram_slot_unlock(struct zram *zram, u32 index)
- {
- bit_spin_unlock(ZRAM_LOCK, &zram->table[index].flags);
- }
- static inline bool init_done(struct zram *zram)
- {
- return zram ? zram->disksize : 0;
- }
- static inline struct zram *dev_to_zram(struct device *dev)
- {
- return (struct zram *)dev_to_disk(dev)->private_data;
- }
- static struct zram_entry *zram_get_entry(struct zram *zram, u32 index)
- {
- return zram->table[index].entry;
- }
- static void zram_set_entry(struct zram *zram, u32 index,
- struct zram_entry *entry)
- {
- zram->table[index].entry = entry;
- }
- /* flag operations require table entry bit_spin_lock() being held */
- static bool zram_test_flag(struct zram *zram, u32 index,
- enum zram_pageflags flag)
- {
- return zram->table[index].flags & BIT(flag);
- }
- static void zram_set_flag(struct zram *zram, u32 index,
- enum zram_pageflags flag)
- {
- zram->table[index].flags |= BIT(flag);
- }
- static void zram_clear_flag(struct zram *zram, u32 index,
- enum zram_pageflags flag)
- {
- zram->table[index].flags &= ~BIT(flag);
- }
- static inline void zram_set_element(struct zram *zram, u32 index,
- unsigned long element)
- {
- zram->table[index].element = element;
- }
- static unsigned long zram_get_element(struct zram *zram, u32 index)
- {
- return zram->table[index].element;
- }
- static size_t zram_get_obj_size(struct zram *zram, u32 index)
- {
- return zram->table[index].flags & (BIT(ZRAM_FLAG_SHIFT) - 1);
- }
- static void zram_set_obj_size(struct zram *zram,
- u32 index, size_t size)
- {
- unsigned long flags = zram->table[index].flags >> ZRAM_FLAG_SHIFT;
- zram->table[index].flags = (flags << ZRAM_FLAG_SHIFT) | size;
- }
- static inline bool zram_allocated(struct zram *zram, u32 index)
- {
- return zram_get_obj_size(zram, index) ||
- zram_test_flag(zram, index, ZRAM_SAME) ||
- zram_test_flag(zram, index, ZRAM_WB);
- }
- #if PAGE_SIZE != 4096
- static inline bool is_partial_io(struct bio_vec *bvec)
- {
- return bvec->bv_len != PAGE_SIZE;
- }
- #else
- static inline bool is_partial_io(struct bio_vec *bvec)
- {
- return false;
- }
- #endif
- /*
- * Check if request is within bounds and aligned on zram logical blocks.
- */
- static inline bool valid_io_request(struct zram *zram,
- sector_t start, unsigned int size)
- {
- u64 end, bound;
- /* unaligned request */
- if (unlikely(start & (ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1)))
- return false;
- if (unlikely(size & (ZRAM_LOGICAL_BLOCK_SIZE - 1)))
- return false;
- end = start + (size >> SECTOR_SHIFT);
- bound = zram->disksize >> SECTOR_SHIFT;
- /* out of range range */
- if (unlikely(start >= bound || end > bound || start > end))
- return false;
- /* I/O request is valid */
- return true;
- }
- static void update_position(u32 *index, int *offset, struct bio_vec *bvec)
- {
- *index += (*offset + bvec->bv_len) / PAGE_SIZE;
- *offset = (*offset + bvec->bv_len) % PAGE_SIZE;
- }
- static inline void update_used_max(struct zram *zram,
- const unsigned long pages)
- {
- unsigned long old_max, cur_max;
- old_max = atomic_long_read(&zram->stats.max_used_pages);
- do {
- cur_max = old_max;
- if (pages > cur_max)
- old_max = atomic_long_cmpxchg(
- &zram->stats.max_used_pages, cur_max, pages);
- } while (old_max != cur_max);
- }
- static inline void zram_fill_page(void *ptr, unsigned long len,
- unsigned long value)
- {
- WARN_ON_ONCE(!IS_ALIGNED(len, sizeof(unsigned long)));
- memset_l(ptr, value, len / sizeof(unsigned long));
- }
- static bool page_same_filled(void *ptr, unsigned long *element)
- {
- unsigned long *page;
- unsigned long val;
- unsigned int pos, last_pos = PAGE_SIZE / sizeof(*page) - 1;
- page = (unsigned long *)ptr;
- val = page[0];
- if (val != page[last_pos])
- return false;
- for (pos = 1; pos < last_pos; pos++) {
- if (val != page[pos])
- return false;
- }
- *element = val;
- return true;
- }
- static ssize_t initstate_show(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- u32 val;
- struct zram *zram = dev_to_zram(dev);
- down_read(&zram->init_lock);
- val = init_done(zram);
- up_read(&zram->init_lock);
- return scnprintf(buf, PAGE_SIZE, "%u\n", val);
- }
- static ssize_t disksize_show(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- struct zram *zram = dev_to_zram(dev);
- return scnprintf(buf, PAGE_SIZE, "%llu\n", zram->disksize);
- }
- static ssize_t mem_limit_store(struct device *dev,
- struct device_attribute *attr, const char *buf, size_t len)
- {
- u64 limit;
- char *tmp;
- struct zram *zram = dev_to_zram(dev);
- limit = memparse(buf, &tmp);
- if (buf == tmp) /* no chars parsed, invalid input */
- return -EINVAL;
- down_write(&zram->init_lock);
- zram->limit_pages = PAGE_ALIGN(limit) >> PAGE_SHIFT;
- up_write(&zram->init_lock);
- return len;
- }
- static ssize_t mem_used_max_store(struct device *dev,
- struct device_attribute *attr, const char *buf, size_t len)
- {
- int err;
- unsigned long val;
- struct zram *zram = dev_to_zram(dev);
- err = kstrtoul(buf, 10, &val);
- if (err || val != 0)
- return -EINVAL;
- down_read(&zram->init_lock);
- if (init_done(zram)) {
- atomic_long_set(&zram->stats.max_used_pages,
- zs_get_total_pages(zram->mem_pool));
- }
- up_read(&zram->init_lock);
- return len;
- }
- // Returns the number of marked blocks
- static int mark_idle(struct zram *zram, unsigned long long threshold)
- {
- unsigned long nr_pages = zram->disksize >> PAGE_SHIFT;
- int index, ret = 0;
- down_read(&zram->init_lock);
- if (!init_done(zram)) {
- up_read(&zram->init_lock);
- return -EINVAL;
- }
- for (index = 0; index < nr_pages; index++) {
- /*
- * Do not mark ZRAM_UNDER_WB slot as ZRAM_IDLE to close race.
- * See the comment in writeback_store.
- */
- zram_slot_lock(zram, index);
- #ifdef CONFIG_ZRAM_MEMORY_TRACKING
- if (zram_allocated(zram, index) &&
- !zram_test_flag(zram, index, ZRAM_UNDER_WB) &&
- !zram_test_flag(zram, index, ZRAM_WB) &&
- !zram_test_flag(zram, index, ZRAM_IDLE) &&
- !zram_test_flag(zram, index, ZRAM_SAME) &&
- !zram_test_flag(zram, index, ZRAM_DEDUPED) &&
- threshold >= zram->table[index].ac_time) {
- zram_set_flag(zram, index, ZRAM_IDLE);
- ret++;
- }
- #endif
- zram_slot_unlock(zram, index);
- }
- up_read(&zram->init_lock);
- return ret;
- }
- #ifdef CONFIG_ZRAM_WRITEBACK
- static void reset_bdev(struct zram *zram)
- {
- struct block_device *bdev;
- if (!zram->backing_dev)
- return;
- bdev = zram->bdev;
- blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
- /* hope filp_close flush all of IO */
- filp_close(zram->backing_dev, NULL);
- zram->backing_dev = NULL;
- zram->bdev = NULL;
- zram->disk->queue->backing_dev_info->capabilities |=
- BDI_CAP_SYNCHRONOUS_IO;
- kvfree(zram->bitmap);
- zram->bitmap = NULL;
- }
- static ssize_t backing_dev_show(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- struct file *file;
- struct zram *zram = dev_to_zram(dev);
- char *p;
- ssize_t ret;
- down_read(&zram->init_lock);
- file = zram->backing_dev;
- if (!file) {
- memcpy(buf, "none\n", 5);
- up_read(&zram->init_lock);
- return 5;
- }
- p = file_path(file, buf, PAGE_SIZE - 1);
- if (IS_ERR(p)) {
- ret = PTR_ERR(p);
- goto out;
- }
- ret = strlen(p);
- memmove(buf, p, ret);
- buf[ret++] = '\n';
- out:
- up_read(&zram->init_lock);
- return ret;
- }
- static ssize_t backing_dev_store(struct device *dev,
- struct device_attribute *attr, const char *buf, size_t len)
- {
- char *file_name;
- size_t sz;
- struct file *backing_dev = NULL;
- struct inode *inode;
- struct address_space *mapping;
- unsigned int bitmap_sz;
- unsigned long nr_pages, nr_size, *bitmap = NULL;
- struct block_device *bdev = NULL;
- int err;
- struct zram *zram = dev_to_zram(dev);
- file_name = kmalloc(PATH_MAX, GFP_KERNEL);
- if (!file_name)
- return -ENOMEM;
- down_write(&zram->init_lock);
- if (init_done(zram)) {
- pr_info("Can't setup backing device for initialized device\n");
- err = -EBUSY;
- goto out;
- }
- strlcpy(file_name, buf, PATH_MAX);
- /* ignore trailing newline */
- sz = strlen(file_name);
- if (sz > 0 && file_name[sz - 1] == '\n')
- file_name[sz - 1] = 0x00;
- backing_dev = filp_open(file_name, O_RDWR|O_LARGEFILE, 0);
- if (IS_ERR(backing_dev)) {
- err = PTR_ERR(backing_dev);
- backing_dev = NULL;
- goto out;
- }
- mapping = backing_dev->f_mapping;
- inode = mapping->host;
- /* Support only block device in this moment */
- if (!S_ISBLK(inode->i_mode)) {
- err = -ENOTBLK;
- goto out;
- }
- bdev = blkdev_get_by_dev(inode->i_rdev,
- FMODE_READ | FMODE_WRITE | FMODE_EXCL, zram);
- if (IS_ERR(bdev)) {
- err = PTR_ERR(bdev);
- bdev = NULL;
- goto out;
- }
- nr_size = i_size_read(inode);
- nr_pages = nr_size >> PAGE_SHIFT;
- bitmap_sz = BITS_TO_LONGS(nr_pages) * sizeof(long);
- bitmap = kvzalloc(bitmap_sz, GFP_KERNEL);
- if (!bitmap) {
- err = -ENOMEM;
- goto out;
- }
- // Trim the device
- pr_info("discarding backing device\n");
- err = blkdev_issue_discard(bdev, 0, nr_size >> 9, GFP_KERNEL, 0);
- if (err)
- pr_warn("failed to discard device: %d\n", err);
- reset_bdev(zram);
- zram->bdev = bdev;
- zram->backing_dev = backing_dev;
- zram->bitmap = bitmap;
- zram->nr_pages = nr_pages;
- /*
- * With writeback feature, zram does asynchronous IO so it's no longer
- * synchronous device so let's remove synchronous io flag. Othewise,
- * upper layer(e.g., swap) could wait IO completion rather than
- * (submit and return), which will cause system sluggish.
- * Furthermore, when the IO function returns(e.g., swap_readpage),
- * upper layer expects IO was done so it could deallocate the page
- * freely but in fact, IO is going on so finally could cause
- * use-after-free when the IO is really done.
- */
- zram->disk->queue->backing_dev_info->capabilities &=
- ~BDI_CAP_SYNCHRONOUS_IO;
- up_write(&zram->init_lock);
- pr_info("setup backing device %s\n", file_name);
- kfree(file_name);
- return len;
- out:
- kvfree(bitmap);
- if (bdev)
- blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
- if (backing_dev)
- filp_close(backing_dev, NULL);
- up_write(&zram->init_lock);
- kfree(file_name);
- return err;
- }
- static unsigned long alloc_block_bdev(struct zram *zram)
- {
- unsigned long blk_idx = 1;
- retry:
- /* skip 0 bit to confuse zram.handle = 0 */
- blk_idx = find_next_zero_bit(zram->bitmap, zram->nr_pages, blk_idx);
- if (blk_idx == zram->nr_pages)
- return 0;
- if (test_and_set_bit(blk_idx, zram->bitmap))
- goto retry;
- atomic64_inc(&zram->stats.bd_count);
- return blk_idx;
- }
- static void free_block_bdev(struct zram *zram, unsigned long blk_idx)
- {
- int was_set;
- was_set = test_and_clear_bit(blk_idx, zram->bitmap);
- WARN_ON_ONCE(!was_set);
- atomic64_dec(&zram->stats.bd_count);
- }
- static void zram_page_end_io(struct bio *bio)
- {
- struct page *page = bio->bi_io_vec[0].bv_page;
- page_endio(page, op_is_write(bio_op(bio)),
- blk_status_to_errno(bio->bi_status));
- bio_put(bio);
- }
- /*
- * Returns 1 if the submission is successful.
- */
- static int read_from_bdev_async(struct zram *zram, struct bio_vec *bvec,
- unsigned long entry, struct bio *parent)
- {
- struct bio *bio;
- bio = bio_alloc(GFP_ATOMIC, 1);
- if (!bio)
- return -ENOMEM;
- bio->bi_iter.bi_sector = entry * (PAGE_SIZE >> 9);
- bio_set_dev(bio, zram->bdev);
- if (!bio_add_page(bio, bvec->bv_page, bvec->bv_len, bvec->bv_offset)) {
- bio_put(bio);
- return -EIO;
- }
- if (!parent) {
- bio->bi_opf = REQ_OP_READ;
- bio->bi_end_io = zram_page_end_io;
- } else {
- bio->bi_opf = parent->bi_opf;
- bio_chain(bio, parent);
- }
- submit_bio(bio);
- return 1;
- }
- #define FOUR_K(x) ((x) * (1 << (PAGE_SHIFT - 12)))
- static int do_writeback(struct zram *zram)
- {
- unsigned long nr_pages = zram->disksize >> PAGE_SHIFT;
- unsigned long index = 0;
- struct bio bio;
- struct bio_vec bio_vec;
- struct page *page;
- int ret = 0;
- int err;
- unsigned long blk_idx = 0;
- down_read(&zram->init_lock);
- if (!init_done(zram)) {
- ret = -EINVAL;
- goto release_init_lock;
- }
- if (!zram->backing_dev) {
- ret = -ENODEV;
- goto release_init_lock;
- }
- page = alloc_page(GFP_KERNEL);
- if (!page) {
- ret = -ENOMEM;
- goto release_init_lock;
- }
- for (; nr_pages != 0; index++, nr_pages--) {
- struct bio_vec bvec;
- if (!blk_idx) {
- blk_idx = alloc_block_bdev(zram);
- if (!blk_idx) {
- ret = -ENOSPC;
- break;
- }
- }
- zram_slot_lock(zram, index);
- if (!zram_allocated(zram, index))
- goto next;
- if (zram_test_flag(zram, index, ZRAM_WB) ||
- zram_test_flag(zram, index, ZRAM_SAME) ||
- zram_test_flag(zram, index, ZRAM_DEDUPED) ||
- zram_test_flag(zram, index, ZRAM_UNDER_WB))
- goto next;
- if (!zram_test_flag(zram, index, ZRAM_IDLE))
- goto next;
- /*
- * Clearing ZRAM_UNDER_WB is duty of caller.
- * IOW, zram_free_page never clear it.
- */
- zram_set_flag(zram, index, ZRAM_UNDER_WB);
- /* Need for hugepage writeback racing */
- zram_set_flag(zram, index, ZRAM_IDLE);
- zram_slot_unlock(zram, index);
- bvec.bv_page = page;
- bvec.bv_len = PAGE_SIZE;
- bvec.bv_offset = 0;
- if (zram_bvec_read(zram, &bvec, index, 0, NULL)) {
- zram_slot_lock(zram, index);
- zram_clear_flag(zram, index, ZRAM_UNDER_WB);
- zram_clear_flag(zram, index, ZRAM_IDLE);
- zram_slot_unlock(zram, index);
- continue;
- }
- bio_init(&bio, &bio_vec, 1);
- bio_set_dev(&bio, zram->bdev);
- bio.bi_iter.bi_sector = blk_idx * (PAGE_SIZE >> 9);
- bio.bi_opf = REQ_OP_WRITE | REQ_SYNC;
- bio_add_page(&bio, bvec.bv_page, bvec.bv_len,
- bvec.bv_offset);
- /*
- * XXX: A single page IO would be inefficient for write
- * but it would be not bad as starter.
- */
- err = submit_bio_wait(&bio);
- if (err) {
- zram_slot_lock(zram, index);
- zram_clear_flag(zram, index, ZRAM_UNDER_WB);
- zram_clear_flag(zram, index, ZRAM_IDLE);
- zram_slot_unlock(zram, index);
- /*
- * Return last IO error unless every IO were
- * not suceeded.
- */
- ret = err;
- continue;
- }
- atomic64_inc(&zram->stats.bd_writes);
- /*
- * We released zram_slot_lock so need to check if the slot was
- * changed. If there is freeing for the slot, we can catch it
- * easily by zram_allocated.
- * A subtle case is the slot is freed/reallocated/marked as
- * ZRAM_IDLE again. To close the race, idle_store doesn't
- * mark ZRAM_IDLE once it found the slot was ZRAM_UNDER_WB.
- * Thus, we could close the race by checking ZRAM_IDLE bit.
- */
- zram_slot_lock(zram, index);
- if (!zram_allocated(zram, index) ||
- !zram_test_flag(zram, index, ZRAM_IDLE)) {
- zram_clear_flag(zram, index, ZRAM_UNDER_WB);
- zram_clear_flag(zram, index, ZRAM_IDLE);
- goto next;
- }
- zram_free_page(zram, index);
- zram_clear_flag(zram, index, ZRAM_UNDER_WB);
- zram_set_flag(zram, index, ZRAM_WB);
- zram_set_element(zram, index, blk_idx);
- blk_idx = 0;
- atomic64_inc(&zram->stats.pages_stored);
- next:
- zram_slot_unlock(zram, index);
- }
- if (blk_idx)
- free_block_bdev(zram, blk_idx);
- __free_page(page);
- release_init_lock:
- pr_info("writeback: C: %llu MiB, R: %llu MiB, W: %llu MiB\n",
- FOUR_K((u64)atomic64_read(&zram->stats.bd_count)) / 256,
- FOUR_K((u64)atomic64_read(&zram->stats.bd_reads)) / 256,
- FOUR_K((u64)atomic64_read(&zram->stats.bd_writes)) / 256);
- up_read(&zram->init_lock);
- return ret;
- }
- struct zram_work {
- struct work_struct work;
- struct zram *zram;
- unsigned long entry;
- struct bio *bio;
- struct bio_vec bvec;
- };
- #if PAGE_SIZE != 4096
- static void zram_sync_read(struct work_struct *work)
- {
- struct zram_work *zw = container_of(work, struct zram_work, work);
- struct zram *zram = zw->zram;
- unsigned long entry = zw->entry;
- struct bio *bio = zw->bio;
- read_from_bdev_async(zram, &zw->bvec, entry, bio);
- }
- /*
- * Block layer want one ->make_request_fn to be active at a time
- * so if we use chained IO with parent IO in same context,
- * it's a deadlock. To avoid, it, it uses worker thread context.
- */
- static int read_from_bdev_sync(struct zram *zram, struct bio_vec *bvec,
- unsigned long entry, struct bio *bio)
- {
- struct zram_work work;
- work.bvec = *bvec;
- work.zram = zram;
- work.entry = entry;
- work.bio = bio;
- INIT_WORK_ONSTACK(&work.work, zram_sync_read);
- queue_work(system_unbound_wq, &work.work);
- flush_work(&work.work);
- destroy_work_on_stack(&work.work);
- return 1;
- }
- #else
- static int read_from_bdev_sync(struct zram *zram, struct bio_vec *bvec,
- unsigned long entry, struct bio *bio)
- {
- WARN_ON(1);
- return -EIO;
- }
- #endif
- static int read_from_bdev(struct zram *zram, struct bio_vec *bvec,
- unsigned long entry, struct bio *parent, bool sync)
- {
- atomic64_inc(&zram->stats.bd_reads);
- if (sync)
- return read_from_bdev_sync(zram, bvec, entry, parent);
- else
- return read_from_bdev_async(zram, bvec, entry, parent);
- }
- #else
- static inline void reset_bdev(struct zram *zram) {};
- static int read_from_bdev(struct zram *zram, struct bio_vec *bvec,
- unsigned long entry, struct bio *parent, bool sync)
- {
- return -EIO;
- }
- static void free_block_bdev(struct zram *zram, unsigned long blk_idx) {};
- #endif
- #ifdef CONFIG_ZRAM_MEMORY_TRACKING
- static struct dentry *zram_debugfs_root;
- static void zram_debugfs_create(void)
- {
- zram_debugfs_root = debugfs_create_dir("zram", NULL);
- }
- static void zram_debugfs_destroy(void)
- {
- debugfs_remove_recursive(zram_debugfs_root);
- }
- static void zram_accessed(struct zram *zram, u32 index)
- {
- zram_clear_flag(zram, index, ZRAM_IDLE);
- zram->table[index].ac_time = total_touch_clock;
- }
- static ssize_t read_block_state(struct file *file, char __user *buf,
- size_t count, loff_t *ppos)
- {
- char *kbuf;
- ssize_t index, written = 0;
- struct zram *zram = file->private_data;
- unsigned long nr_pages = zram->disksize >> PAGE_SHIFT;
- unsigned long long *clock;
- kbuf = kvmalloc(count, GFP_KERNEL);
- if (!kbuf)
- return -ENOMEM;
- down_read(&zram->init_lock);
- if (!init_done(zram)) {
- up_read(&zram->init_lock);
- kvfree(kbuf);
- return -EINVAL;
- }
- for (index = *ppos; index < nr_pages; index++) {
- int copied;
- zram_slot_lock(zram, index);
- if (!zram_allocated(zram, index))
- goto next;
- clock = &zram->table[index].ac_time;
- copied = snprintf(kbuf + written, count,
- "%12zd %12llu.%06llu %c%c%c%c\n",
- index,
- *clock / NSEC_PER_SEC,
- *clock / NSEC_PER_USEC % USEC_PER_SEC,
- zram_test_flag(zram, index, ZRAM_SAME) ? 's' : '.',
- zram_test_flag(zram, index, ZRAM_WB) ? 'w' : '.',
- zram_test_flag(zram, index, ZRAM_IDLE) ? 'i' : '.',
- zram_test_flag(zram, index, ZRAM_DEDUPED) ? 'd' : '.');
- if (count < copied) {
- zram_slot_unlock(zram, index);
- break;
- }
- written += copied;
- count -= copied;
- next:
- zram_slot_unlock(zram, index);
- *ppos += 1;
- }
- up_read(&zram->init_lock);
- if (copy_to_user(buf, kbuf, written))
- written = -EFAULT;
- kvfree(kbuf);
- return written;
- }
- static const struct file_operations proc_zram_block_state_op = {
- .open = simple_open,
- .read = read_block_state,
- .llseek = default_llseek,
- };
- static void zram_debugfs_register(struct zram *zram)
- {
- if (!zram_debugfs_root)
- return;
- zram->debugfs_dir = debugfs_create_dir(zram->disk->disk_name,
- zram_debugfs_root);
- debugfs_create_file("block_state", 0400, zram->debugfs_dir,
- zram, &proc_zram_block_state_op);
- }
- static void zram_debugfs_unregister(struct zram *zram)
- {
- debugfs_remove_recursive(zram->debugfs_dir);
- }
- #else
- static void zram_debugfs_create(void) {};
- static void zram_debugfs_destroy(void) {};
- static void zram_accessed(struct zram *zram, u32 index)
- {
- zram_clear_flag(zram, index, ZRAM_IDLE);
- };
- static void zram_debugfs_register(struct zram *zram) {};
- static void zram_debugfs_unregister(struct zram *zram) {};
- #endif
- /*
- * We switched to per-cpu streams and this attr is not needed anymore.
- * However, we will keep it around for some time, because:
- * a) we may revert per-cpu streams in the future
- * b) it's visible to user space and we need to follow our 2 years
- * retirement rule; but we already have a number of 'soon to be
- * altered' attrs, so max_comp_streams need to wait for the next
- * layoff cycle.
- */
- static ssize_t max_comp_streams_show(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- return scnprintf(buf, PAGE_SIZE, "%d\n", num_online_cpus());
- }
- static ssize_t max_comp_streams_store(struct device *dev,
- struct device_attribute *attr, const char *buf, size_t len)
- {
- return len;
- }
- static ssize_t comp_algorithm_show(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- size_t sz;
- struct zram *zram = dev_to_zram(dev);
- down_read(&zram->init_lock);
- sz = zcomp_available_show(zram->compressor, buf);
- up_read(&zram->init_lock);
- return sz;
- }
- static ssize_t comp_algorithm_store(struct device *dev,
- struct device_attribute *attr, const char *buf, size_t len)
- {
- struct zram *zram = dev_to_zram(dev);
- char compressor[ARRAY_SIZE(zram->compressor)];
- size_t sz;
- strlcpy(compressor, buf, sizeof(compressor));
- /* ignore trailing newline */
- sz = strlen(compressor);
- if (sz > 0 && compressor[sz - 1] == '\n')
- compressor[sz - 1] = 0x00;
- if (!zcomp_available_algorithm(compressor))
- return -EINVAL;
- down_write(&zram->init_lock);
- if (init_done(zram)) {
- up_write(&zram->init_lock);
- pr_info("Can't change algorithm for initialized device\n");
- return -EBUSY;
- }
- strcpy(zram->compressor, compressor);
- up_write(&zram->init_lock);
- return len;
- }
- static ssize_t use_dedup_show(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- bool val;
- struct zram *zram = dev_to_zram(dev);
- down_read(&zram->init_lock);
- val = zram->use_dedup;
- up_read(&zram->init_lock);
- return scnprintf(buf, PAGE_SIZE, "%d\n", (int)val);
- }
- #ifdef CONFIG_ZRAM_DEDUP
- static ssize_t use_dedup_store(struct device *dev,
- struct device_attribute *attr, const char *buf, size_t len)
- {
- int val;
- struct zram *zram = dev_to_zram(dev);
- if (kstrtoint(buf, 10, &val) || (val != 0 && val != 1))
- return -EINVAL;
- down_write(&zram->init_lock);
- if (init_done(zram)) {
- up_write(&zram->init_lock);
- pr_info("Can't change dedup usage for initialized device\n");
- return -EBUSY;
- }
- zram->use_dedup = val;
- up_write(&zram->init_lock);
- return len;
- }
- #endif
- static ssize_t compact_store(struct device *dev,
- struct device_attribute *attr, const char *buf, size_t len)
- {
- struct zram *zram = dev_to_zram(dev);
- down_read(&zram->init_lock);
- if (!init_done(zram)) {
- up_read(&zram->init_lock);
- return -EINVAL;
- }
- zs_compact(zram->mem_pool);
- up_read(&zram->init_lock);
- return len;
- }
- static ssize_t io_stat_show(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- struct zram *zram = dev_to_zram(dev);
- ssize_t ret;
- down_read(&zram->init_lock);
- ret = scnprintf(buf, PAGE_SIZE,
- "%8llu %8llu %8llu %8llu\n",
- (u64)atomic64_read(&zram->stats.failed_reads),
- (u64)atomic64_read(&zram->stats.failed_writes),
- (u64)atomic64_read(&zram->stats.invalid_io),
- (u64)atomic64_read(&zram->stats.notify_free));
- up_read(&zram->init_lock);
- return ret;
- }
- static ssize_t mm_stat_show(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- struct zram *zram = dev_to_zram(dev);
- struct zs_pool_stats pool_stats;
- u64 orig_size, mem_used = 0;
- long max_used;
- ssize_t ret;
- memset(&pool_stats, 0x00, sizeof(struct zs_pool_stats));
- down_read(&zram->init_lock);
- if (init_done(zram)) {
- mem_used = zs_get_total_pages(zram->mem_pool);
- zs_pool_stats(zram->mem_pool, &pool_stats);
- }
- orig_size = atomic64_read(&zram->stats.pages_stored);
- max_used = atomic_long_read(&zram->stats.max_used_pages);
- ret = scnprintf(buf, PAGE_SIZE,
- "%8llu %8llu %8llu %8lu %8ld %8llu %8lu %8llu %8llu\n",
- (orig_size << PAGE_SHIFT) / 1048576,
- ((u64)atomic64_read(&zram->stats.compr_data_size)) / 1048576,
- (mem_used << PAGE_SHIFT) / 1048576,
- (zram->limit_pages << PAGE_SHIFT) / 1048576,
- (max_used << PAGE_SHIFT) / 1048576,
- ((u64)atomic64_read(&zram->stats.same_pages)) / 256,
- (atomic_long_read(&pool_stats.pages_compacted)) / 256,
- (zram_dedup_dup_size(zram)) / 1048576,
- (zram_dedup_meta_size(zram)) / 1048576);
- up_read(&zram->init_lock);
- return ret;
- }
- #ifdef CONFIG_ZRAM_WRITEBACK
- static ssize_t bd_stat_show(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- struct zram *zram = dev_to_zram(dev);
- ssize_t ret;
- down_read(&zram->init_lock);
- ret = scnprintf(buf, PAGE_SIZE,
- "%8llu %8llu %8llu\n",
- FOUR_K((u64)atomic64_read(&zram->stats.bd_count)) / 256,
- FOUR_K((u64)atomic64_read(&zram->stats.bd_reads)) / 256,
- FOUR_K((u64)atomic64_read(&zram->stats.bd_writes)) / 256);
- up_read(&zram->init_lock);
- return ret;
- }
- #endif
- static ssize_t debug_stat_show(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- int version = 1;
- struct zram *zram = dev_to_zram(dev);
- ssize_t ret;
- down_read(&zram->init_lock);
- ret = scnprintf(buf, PAGE_SIZE,
- "version: %d\n%8llu %8llu\n",
- version,
- (u64)atomic64_read(&zram->stats.writestall),
- (u64)atomic64_read(&zram->stats.miss_free));
- up_read(&zram->init_lock);
- return ret;
- }
- static DEVICE_ATTR_RO(io_stat);
- static DEVICE_ATTR_RO(mm_stat);
- #ifdef CONFIG_ZRAM_WRITEBACK
- static DEVICE_ATTR_RO(bd_stat);
- #endif
- static DEVICE_ATTR_RO(debug_stat);
- static unsigned long zram_entry_handle(struct zram *zram,
- struct zram_entry *entry)
- {
- if (zram_dedup_enabled(zram))
- return entry->handle;
- else
- return (unsigned long)entry;
- }
- static struct zram_entry *zram_entry_alloc(struct zram *zram,
- unsigned int len, gfp_t flags)
- {
- struct zram_entry *entry;
- unsigned long handle;
- handle = zs_malloc(zram->mem_pool, len, flags);
- if (!handle)
- return NULL;
- if (!zram_dedup_enabled(zram))
- return (struct zram_entry *)handle;
- entry = kzalloc(sizeof(*entry),
- flags & ~(__GFP_HIGHMEM|__GFP_MOVABLE|__GFP_CMA));
- if (!entry) {
- zs_free(zram->mem_pool, handle);
- return NULL;
- }
- zram_dedup_init_entry(zram, entry, handle, len);
- atomic64_add(sizeof(*entry), &zram->stats.meta_data_size);
- return entry;
- }
- void zram_entry_free(struct zram *zram, struct zram_entry *entry)
- {
- if (!zram_dedup_put_entry(zram, entry))
- return;
- zs_free(zram->mem_pool, zram_entry_handle(zram, entry));
- if (!zram_dedup_enabled(zram))
- return;
- kfree(entry);
- atomic64_sub(sizeof(*entry), &zram->stats.meta_data_size);
- }
- static void zram_meta_free(struct zram *zram, u64 disksize)
- {
- size_t num_pages = disksize >> PAGE_SHIFT;
- size_t index;
- /* Free all pages that are still in this zram device */
- for (index = 0; index < num_pages; index++)
- zram_free_page(zram, index);
- zs_destroy_pool(zram->mem_pool);
- zram_dedup_fini(zram);
- vfree(zram->table);
- }
- static bool zram_meta_alloc(struct zram *zram, u64 disksize)
- {
- size_t num_pages;
- num_pages = disksize >> PAGE_SHIFT;
- zram->table = vzalloc(num_pages * sizeof(*zram->table));
- if (!zram->table)
- return false;
- zram->mem_pool = zs_create_pool(zram->disk->disk_name);
- if (!zram->mem_pool) {
- vfree(zram->table);
- return false;
- }
- if (!huge_class_size)
- huge_class_size = zs_huge_class_size(zram->mem_pool);
- if (zram_dedup_init(zram, num_pages)) {
- vfree(zram->table);
- zs_destroy_pool(zram->mem_pool);
- return false;
- }
- return true;
- }
- /*
- * To protect concurrent access to the same index entry,
- * caller should hold this table index entry's bit_spinlock to
- * indicate this index entry is accessing.
- */
- static void zram_free_page(struct zram *zram, size_t index)
- {
- struct zram_entry *entry;
- #ifdef CONFIG_ZRAM_MEMORY_TRACKING
- zram->table[index].ac_time = 0;
- #endif
- if (zram_test_flag(zram, index, ZRAM_IDLE))
- zram_clear_flag(zram, index, ZRAM_IDLE);
- if (zram_test_flag(zram, index, ZRAM_DEDUPED))
- zram_clear_flag(zram, index, ZRAM_DEDUPED);
- if (zram_test_flag(zram, index, ZRAM_WB)) {
- zram_clear_flag(zram, index, ZRAM_WB);
- free_block_bdev(zram, zram_get_element(zram, index));
- goto out;
- }
- /*
- * No memory is allocated for same element filled pages.
- * Simply clear same page flag.
- */
- if (zram_test_flag(zram, index, ZRAM_SAME)) {
- zram_clear_flag(zram, index, ZRAM_SAME);
- atomic64_dec(&zram->stats.same_pages);
- goto out;
- }
- entry = zram_get_entry(zram, index);
- if (!entry)
- return;
- zram_entry_free(zram, entry);
- atomic64_sub(zram_get_obj_size(zram, index),
- &zram->stats.compr_data_size);
- out:
- atomic64_dec(&zram->stats.pages_stored);
- zram_set_entry(zram, index, NULL);
- zram_set_obj_size(zram, index, 0);
- WARN_ON_ONCE(zram->table[index].flags &
- ~(1UL << ZRAM_LOCK | 1UL << ZRAM_UNDER_WB));
- }
- static int __zram_bvec_read(struct zram *zram, struct page *page, u32 index,
- struct bio *bio, bool partial_io)
- {
- struct zcomp_strm *zstrm;
- struct zram_entry *entry;
- unsigned int size;
- void *src, *dst;
- int ret;
- zram_slot_lock(zram, index);
- if (zram_test_flag(zram, index, ZRAM_WB)) {
- struct bio_vec bvec;
- zram_slot_unlock(zram, index);
- bvec.bv_page = page;
- bvec.bv_len = PAGE_SIZE;
- bvec.bv_offset = 0;
- return read_from_bdev(zram, &bvec,
- zram_get_element(zram, index),
- bio, partial_io);
- }
- entry = zram_get_entry(zram, index);
- if (!entry || zram_test_flag(zram, index, ZRAM_SAME)) {
- unsigned long value;
- void *mem;
- value = entry ? zram_get_element(zram, index) : 0;
- mem = kmap_atomic(page);
- zram_fill_page(mem, PAGE_SIZE, value);
- kunmap_atomic(mem);
- zram_slot_unlock(zram, index);
- return 0;
- }
- size = zram_get_obj_size(zram, index);
- if (size != PAGE_SIZE)
- zstrm = zcomp_stream_get(zram->comp);
- src = zs_map_object(zram->mem_pool,
- zram_entry_handle(zram, entry), ZS_MM_RO);
- if (size == PAGE_SIZE) {
- dst = kmap_atomic(page);
- memcpy(dst, src, PAGE_SIZE);
- kunmap_atomic(dst);
- ret = 0;
- } else {
- dst = kmap_atomic(page);
- ret = zcomp_decompress(zstrm, src, size, dst);
- kunmap_atomic(dst);
- zcomp_stream_put(zram->comp);
- }
- zs_unmap_object(zram->mem_pool, zram_entry_handle(zram, entry));
- zram_slot_unlock(zram, index);
- /* Should NEVER happen. Return bio error if it does. */
- if (WARN_ON(ret))
- pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
- return ret;
- }
- static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
- u32 index, int offset, struct bio *bio)
- {
- int ret;
- struct page *page;
- page = bvec->bv_page;
- if (is_partial_io(bvec)) {
- /* Use a temporary buffer to decompress the page */
- page = alloc_page(GFP_NOIO|__GFP_HIGHMEM);
- if (!page)
- return -ENOMEM;
- }
- ret = __zram_bvec_read(zram, page, index, bio, is_partial_io(bvec));
- if (unlikely(ret))
- goto out;
- if (is_partial_io(bvec)) {
- void *dst = kmap_atomic(bvec->bv_page);
- void *src = kmap_atomic(page);
- memcpy(dst + bvec->bv_offset, src + offset, bvec->bv_len);
- kunmap_atomic(src);
- kunmap_atomic(dst);
- }
- out:
- if (is_partial_io(bvec))
- __free_page(page);
- return ret;
- }
- static int __zram_bvec_write(struct zram *zram, struct bio_vec *bvec,
- u32 index, struct bio *bio)
- {
- int ret = 0;
- unsigned long alloced_pages;
- struct zram_entry *entry = NULL;
- unsigned int comp_len = 0;
- void *src, *dst, *mem;
- struct zcomp_strm *zstrm;
- struct page *page = bvec->bv_page;
- u32 checksum;
- unsigned long element = 0;
- enum zram_pageflags flags = 0;
- mem = kmap_atomic(page);
- if (page_same_filled(mem, &element)) {
- kunmap_atomic(mem);
- /* Free memory associated with this sector now. */
- flags = ZRAM_SAME;
- atomic64_inc(&zram->stats.same_pages);
- goto out;
- }
- kunmap_atomic(mem);
- entry = zram_dedup_find(zram, page, &checksum);
- if (entry) {
- flags = ZRAM_DEDUPED;
- comp_len = entry->len;
- goto out;
- }
- compress_again:
- zstrm = zcomp_stream_get(zram->comp);
- src = kmap_atomic(page);
- ret = zcomp_compress(zstrm, src, &comp_len);
- kunmap_atomic(src);
- if (unlikely(ret)) {
- zcomp_stream_put(zram->comp);
- pr_err("Compression failed! err=%d\n", ret);
- if (entry)
- zram_entry_free(zram, entry);
- return ret;
- }
- if (comp_len >= huge_class_size)
- comp_len = PAGE_SIZE;
- /*
- * entry allocation has 2 paths:
- * a) fast path is executed with preemption disabled (for
- * per-cpu streams) and has __GFP_DIRECT_RECLAIM bit clear,
- * since we can't sleep;
- * b) slow path enables preemption and attempts to allocate
- * the page with __GFP_DIRECT_RECLAIM bit set. we have to
- * put per-cpu compression stream and, thus, to re-do
- * the compression once entry is allocated.
- *
- * if we have a 'non-null' entry here then we are coming
- * from the slow path and entry has already been allocated.
- */
- if (!entry)
- entry = zram_entry_alloc(zram, comp_len,
- __GFP_KSWAPD_RECLAIM |
- __GFP_NOWARN |
- __GFP_HIGHMEM |
- __GFP_MOVABLE);
- if (!entry) {
- zcomp_stream_put(zram->comp);
- atomic64_inc(&zram->stats.writestall);
- entry = zram_entry_alloc(zram, comp_len,
- GFP_NOIO | __GFP_HIGHMEM |
- __GFP_MOVABLE);
- if (entry)
- goto compress_again;
- return -ENOMEM;
- }
- alloced_pages = zs_get_total_pages(zram->mem_pool);
- update_used_max(zram, alloced_pages);
- if (zram->limit_pages && alloced_pages > zram->limit_pages) {
- zcomp_stream_put(zram->comp);
- zram_entry_free(zram, entry);
- return -ENOMEM;
- }
- dst = zs_map_object(zram->mem_pool,
- zram_entry_handle(zram, entry), ZS_MM_WO);
- src = zstrm->buffer;
- if (comp_len == PAGE_SIZE)
- src = kmap_atomic(page);
- memcpy(dst, src, comp_len);
- if (comp_len == PAGE_SIZE)
- kunmap_atomic(src);
- zcomp_stream_put(zram->comp);
- zs_unmap_object(zram->mem_pool, zram_entry_handle(zram, entry));
- atomic64_add(comp_len, &zram->stats.compr_data_size);
- zram_dedup_insert(zram, entry, checksum);
- out:
- /*
- * Free memory associated with this sector
- * before overwriting unused sectors.
- */
- zram_slot_lock(zram, index);
- zram_free_page(zram, index);
- switch (flags) {
- case ZRAM_SAME:
- zram_set_flag(zram, index, flags);
- zram_set_element(zram, index, element);
- break;
- case ZRAM_DEDUPED:
- zram_set_flag(zram, index, flags);
- // Fallthrough
- default:
- zram_set_entry(zram, index, entry);
- zram_set_obj_size(zram, index, comp_len);
- }
- zram_slot_unlock(zram, index);
- /* Update stats */
- atomic64_inc(&zram->stats.pages_stored);
- return ret;
- }
- static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec,
- u32 index, int offset, struct bio *bio)
- {
- int ret;
- struct page *page = NULL;
- void *src;
- struct bio_vec vec;
- vec = *bvec;
- if (is_partial_io(bvec)) {
- void *dst;
- /*
- * This is a partial IO. We need to read the full page
- * before to write the changes.
- */
- page = alloc_page(GFP_NOIO|__GFP_HIGHMEM);
- if (!page)
- return -ENOMEM;
- ret = __zram_bvec_read(zram, page, index, bio, true);
- if (ret)
- goto out;
- src = kmap_atomic(bvec->bv_page);
- dst = kmap_atomic(page);
- memcpy(dst + offset, src + bvec->bv_offset, bvec->bv_len);
- kunmap_atomic(dst);
- kunmap_atomic(src);
- vec.bv_page = page;
- vec.bv_len = PAGE_SIZE;
- vec.bv_offset = 0;
- }
- ret = __zram_bvec_write(zram, &vec, index, bio);
- out:
- if (is_partial_io(bvec))
- __free_page(page);
- return ret;
- }
- /*
- * zram_bio_discard - handler on discard request
- * @index: physical block index in PAGE_SIZE units
- * @offset: byte offset within physical block
- */
- static void zram_bio_discard(struct zram *zram, u32 index,
- int offset, struct bio *bio)
- {
- size_t n = bio->bi_iter.bi_size;
- /*
- * zram manages data in physical block size units. Because logical block
- * size isn't identical with physical block size on some arch, we
- * could get a discard request pointing to a specific offset within a
- * certain physical block. Although we can handle this request by
- * reading that physiclal block and decompressing and partially zeroing
- * and re-compressing and then re-storing it, this isn't reasonable
- * because our intent with a discard request is to save memory. So
- * skipping this logical block is appropriate here.
- */
- if (offset) {
- if (n <= (PAGE_SIZE - offset))
- return;
- n -= (PAGE_SIZE - offset);
- index++;
- }
- while (n >= PAGE_SIZE) {
- zram_slot_lock(zram, index);
- zram_free_page(zram, index);
- zram_slot_unlock(zram, index);
- atomic64_inc(&zram->stats.notify_free);
- index++;
- n -= PAGE_SIZE;
- }
- }
- /*
- * Returns errno if it has some problem. Otherwise return 0 or 1.
- * Returns 0 if IO request was done synchronously
- * Returns 1 if IO request was successfully submitted.
- */
- static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,
- int offset, unsigned int op, struct bio *bio)
- {
- unsigned long start_time = jiffies;
- struct request_queue *q = zram->disk->queue;
- int ret;
- generic_start_io_acct(q, op, bvec->bv_len >> SECTOR_SHIFT,
- &zram->disk->part0);
- if (!op_is_write(op)) {
- atomic64_inc(&zram->stats.num_reads);
- ret = zram_bvec_read(zram, bvec, index, offset, bio);
- flush_dcache_page(bvec->bv_page);
- } else {
- atomic64_inc(&zram->stats.num_writes);
- ret = zram_bvec_write(zram, bvec, index, offset, bio);
- }
- generic_end_io_acct(q, op, &zram->disk->part0, start_time);
- zram_slot_lock(zram, index);
- zram_accessed(zram, index);
- zram_slot_unlock(zram, index);
- if (unlikely(ret < 0)) {
- if (!op_is_write(op))
- atomic64_inc(&zram->stats.failed_reads);
- else
- atomic64_inc(&zram->stats.failed_writes);
- }
- return ret;
- }
- static void zram_wb_input_event(struct input_handle *handle,
- unsigned int type, unsigned int code, int value)
- {
- static unsigned int prev_value = UINT_MAX;
- static unsigned long long prev;
- unsigned long long delta;
- if (code != BTN_TOUCH)
- return;
- if (prev_value == value)
- return;
- if (!screen_on)
- return;
- if (value == 1) {
- prev = sched_clock();
- } else {
- delta = sched_clock() - prev;
- total_touch_clock += delta;
- }
- prev_value = value;
- }
- static int zram_wb_input_connect(struct input_handler *handler,
- struct input_dev *dev, const struct input_device_id *id)
- {
- struct input_handle *handle;
- int error;
- handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
- if (!handle)
- return -ENOMEM;
- handle->dev = dev;
- handle->handler = handler;
- handle->name = "zram_wb";
- // Initialize zram_wb_input_event()'s prev
- zram_wb_input_event(NULL, 1, BTN_TOUCH, UINT_MAX);
- error = input_register_handle(handle);
- if (error)
- goto err2;
- error = input_open_device(handle);
- if (error)
- goto err1;
- pr_info("%s found and connected!\n", dev->name);
- return 0;
- err1:
- input_unregister_handle(handle);
- err2:
- kfree(handle);
- return error;
- }
- static void zram_wb_input_disconnect(struct input_handle *handle)
- {
- input_close_device(handle);
- input_unregister_handle(handle);
- kfree(handle);
- }
- static const struct input_device_id zram_wb_input_ids[] = {
- {
- .flags = INPUT_DEVICE_ID_MATCH_KEYBIT,
- .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
- },
- { },
- };
- static struct input_handler zram_wb_input_handler = {
- .event = zram_wb_input_event,
- .connect = zram_wb_input_connect,
- .disconnect = zram_wb_input_disconnect,
- .name = "zram_wb_input_handler",
- .id_table = zram_wb_input_ids,
- };
- static void zram_wb_fb_work(struct work_struct *work)
- {
- struct blk_plug plug;
- struct zram *zram;
- int ret, id;
- if (screen_on)
- return;
- // Force finger-up to zram_wb_input_event()
- zram_wb_input_event(NULL, 1, BTN_TOUCH, 0);
- // Work only if charger is connected
- if (power_supply_is_system_supplied() <= 0)
- return;
- if (total_touch_clock <= (wb_start_mins * NSEC_PER_SEC * 60))
- return;
- mutex_lock(&zram_wb_wakelock_mutex);
- __pm_stay_awake(&zram_wb_wakelock);
- blk_start_plug(&plug);
- idr_for_each_entry(&zram_index_idr, zram, id) {
- // Start marking blocks if they are old enough
- ret = mark_idle(zram, total_touch_clock - (wb_start_mins * NSEC_PER_SEC * 60));
- pr_err("mark_idle() returned %d\n", ret);
- if (ret <= 0)
- continue;
- // Start writeback
- pr_info("do_writeback()++\n");
- ret = do_writeback(zram);
- pr_info("do_writeback()--\n");
- }
- blk_finish_plug(&plug);
- __pm_relax(&zram_wb_wakelock);
- mutex_unlock(&zram_wb_wakelock_mutex);
- }
- static int fb_notifier_callback(struct notifier_block *self,
- unsigned long event, void *data)
- {
- struct fb_event *evdata = data;
- int *blank;
- if (event != FB_EVENT_BLANK && event != FB_EARLY_EVENT_BLANK)
- goto out;
- blank = evdata->data;
- switch (*blank) {
- case FB_BLANK_POWERDOWN:
- case FB_BLANK_HSYNC_SUSPEND:
- case FB_BLANK_VSYNC_SUSPEND:
- case FB_BLANK_NORMAL:
- if (!screen_on)
- goto out;
- screen_on = false;
- queue_work(system_power_efficient_wq, &zram_wb_fb_worker);
- break;
- case FB_BLANK_UNBLANK:
- if (screen_on)
- goto out;
- screen_on = true;
- queue_work(system_power_efficient_wq, &zram_wb_fb_worker);
- break;
- }
- out:
- return NOTIFY_OK;
- }
- static struct notifier_block fb_notifier_block = {
- .notifier_call = fb_notifier_callback,
- };
- static void __init init_zram_wb(void)
- {
- int i;
- INIT_WORK(&zram_wb_fb_worker, zram_wb_fb_work);
- wakeup_source_init(&zram_wb_wakelock, "zram_wb_wakelock");
- fb_register_client(&fb_notifier_block);
- i = input_register_handler(&zram_wb_input_handler);
- }
- static void __exit destroy_zram_wb(void)
- {
- input_unregister_handler(&zram_wb_input_handler);
- fb_unregister_client(&fb_notifier_block);
- wakeup_source_trash(&zram_wb_wakelock);
- }
- static void __zram_make_request(struct zram *zram, struct bio *bio)
- {
- int offset;
- u32 index;
- struct bio_vec bvec;
- struct bvec_iter iter;
- index = bio->bi_iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
- offset = (bio->bi_iter.bi_sector &
- (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT;
- switch (bio_op(bio)) {
- case REQ_OP_DISCARD:
- case REQ_OP_WRITE_ZEROES:
- zram_bio_discard(zram, index, offset, bio);
- bio_endio(bio);
- return;
- default:
- break;
- }
- bio_for_each_segment(bvec, bio, iter) {
- struct bio_vec bv = bvec;
- unsigned int unwritten = bvec.bv_len;
- do {
- bv.bv_len = min_t(unsigned int, PAGE_SIZE - offset,
- unwritten);
- if (zram_bvec_rw(zram, &bv, index, offset,
- bio_op(bio), bio) < 0)
- goto out;
- bv.bv_offset += bv.bv_len;
- unwritten -= bv.bv_len;
- update_position(&index, &offset, &bv);
- } while (unwritten);
- }
- bio_endio(bio);
- return;
- out:
- bio_io_error(bio);
- }
- /*
- * Handler function for all zram I/O requests.
- */
- static blk_qc_t zram_make_request(struct request_queue *queue, struct bio *bio)
- {
- struct zram *zram = queue->queuedata;
- if (!valid_io_request(zram, bio->bi_iter.bi_sector,
- bio->bi_iter.bi_size)) {
- atomic64_inc(&zram->stats.invalid_io);
- goto error;
- }
- __zram_make_request(zram, bio);
- return BLK_QC_T_NONE;
- error:
- bio_io_error(bio);
- return BLK_QC_T_NONE;
- }
- static void zram_slot_free_notify(struct block_device *bdev,
- unsigned long index)
- {
- struct zram *zram;
- zram = bdev->bd_disk->private_data;
- atomic64_inc(&zram->stats.notify_free);
- if (!zram_slot_trylock(zram, index)) {
- atomic64_inc(&zram->stats.miss_free);
- return;
- }
- zram_free_page(zram, index);
- zram_slot_unlock(zram, index);
- }
- static int zram_rw_page(struct block_device *bdev, sector_t sector,
- struct page *page, unsigned int op)
- {
- int offset, ret;
- u32 index;
- struct zram *zram;
- struct bio_vec bv;
- if (PageTransHuge(page))
- return -ENOTSUPP;
- zram = bdev->bd_disk->private_data;
- if (!valid_io_request(zram, sector, PAGE_SIZE)) {
- atomic64_inc(&zram->stats.invalid_io);
- ret = -EINVAL;
- goto out;
- }
- index = sector >> SECTORS_PER_PAGE_SHIFT;
- offset = (sector & (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT;
- bv.bv_page = page;
- bv.bv_len = PAGE_SIZE;
- bv.bv_offset = 0;
- ret = zram_bvec_rw(zram, &bv, index, offset, op, NULL);
- out:
- /*
- * If I/O fails, just return error(ie, non-zero) without
- * calling page_endio.
- * It causes resubmit the I/O with bio request by upper functions
- * of rw_page(e.g., swap_readpage, __swap_writepage) and
- * bio->bi_end_io does things to handle the error
- * (e.g., SetPageError, set_page_dirty and extra works).
- */
- if (unlikely(ret < 0))
- return ret;
- switch (ret) {
- case 0:
- page_endio(page, op_is_write(op), 0);
- break;
- case 1:
- ret = 0;
- break;
- default:
- WARN_ON(1);
- }
- return ret;
- }
- static void zram_reset_device(struct zram *zram)
- {
- struct zcomp *comp;
- u64 disksize;
- down_write(&zram->init_lock);
- zram->limit_pages = 0;
- if (!init_done(zram)) {
- up_write(&zram->init_lock);
- return;
- }
- comp = zram->comp;
- disksize = zram->disksize;
- zram->disksize = 0;
- set_capacity(zram->disk, 0);
- part_stat_set_all(&zram->disk->part0, 0);
- up_write(&zram->init_lock);
- /* I/O operation under all of CPU are done so let's free */
- zram_meta_free(zram, disksize);
- memset(&zram->stats, 0, sizeof(zram->stats));
- zcomp_destroy(comp);
- reset_bdev(zram);
- }
- static ssize_t disksize_store(struct device *dev,
- struct device_attribute *attr, const char *buf, size_t len)
- {
- u64 disksize;
- struct zcomp *comp;
- struct zram *zram = dev_to_zram(dev);
- int err;
- disksize = memparse(buf, NULL);
- if (!disksize)
- return -EINVAL;
- down_write(&zram->init_lock);
- if (init_done(zram)) {
- pr_info("Cannot change disksize for initialized device\n");
- err = -EBUSY;
- goto out_unlock;
- }
- disksize = PAGE_ALIGN(disksize);
- if (!zram_meta_alloc(zram, disksize)) {
- err = -ENOMEM;
- goto out_unlock;
- }
- comp = zcomp_create(zram->compressor);
- if (IS_ERR(comp)) {
- pr_err("Cannot initialise %s compressing backend\n",
- zram->compressor);
- err = PTR_ERR(comp);
- goto out_free_meta;
- }
- zram->comp = comp;
- zram->disksize = disksize;
- barrier();
- set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
- revalidate_disk(zram->disk);
- up_write(&zram->init_lock);
- return len;
- out_free_meta:
- zram_meta_free(zram, disksize);
- out_unlock:
- up_write(&zram->init_lock);
- return err;
- }
- static ssize_t reset_store(struct device *dev,
- struct device_attribute *attr, const char *buf, size_t len)
- {
- int ret;
- unsigned short do_reset;
- struct zram *zram;
- struct block_device *bdev;
- ret = kstrtou16(buf, 10, &do_reset);
- if (ret)
- return ret;
- if (!do_reset)
- return -EINVAL;
- zram = dev_to_zram(dev);
- bdev = bdget_disk(zram->disk, 0);
- if (!bdev)
- return -ENOMEM;
- mutex_lock(&bdev->bd_mutex);
- /* Do not reset an active device or claimed device */
- if (bdev->bd_openers || zram->claim) {
- mutex_unlock(&bdev->bd_mutex);
- bdput(bdev);
- return -EBUSY;
- }
- /* From now on, anyone can't open /dev/zram[0-9] */
- zram->claim = true;
- mutex_unlock(&bdev->bd_mutex);
- /* Make sure all the pending I/O are finished */
- fsync_bdev(bdev);
- zram_reset_device(zram);
- revalidate_disk(zram->disk);
- bdput(bdev);
- mutex_lock(&bdev->bd_mutex);
- zram->claim = false;
- mutex_unlock(&bdev->bd_mutex);
- return len;
- }
- static int zram_open(struct block_device *bdev, fmode_t mode)
- {
- int ret = 0;
- struct zram *zram;
- WARN_ON(!mutex_is_locked(&bdev->bd_mutex));
- zram = bdev->bd_disk->private_data;
- /* zram was claimed to reset so open request fails */
- if (zram->claim)
- ret = -EBUSY;
- return ret;
- }
- static const struct block_device_operations zram_devops = {
- .open = zram_open,
- .swap_slot_free_notify = zram_slot_free_notify,
- .rw_page = zram_rw_page,
- .owner = THIS_MODULE
- };
- static DEVICE_ATTR_WO(compact);
- static DEVICE_ATTR_RW(disksize);
- static DEVICE_ATTR_RO(initstate);
- static DEVICE_ATTR_WO(reset);
- static DEVICE_ATTR_WO(mem_limit);
- static DEVICE_ATTR_WO(mem_used_max);
- static DEVICE_ATTR_RW(max_comp_streams);
- static DEVICE_ATTR_RW(comp_algorithm);
- #ifdef CONFIG_ZRAM_WRITEBACK
- static DEVICE_ATTR_RW(backing_dev);
- #endif
- #ifdef CONFIG_ZRAM_DEDUP
- static DEVICE_ATTR_RW(use_dedup);
- #else
- static DEVICE_ATTR_RO(use_dedup);
- #endif
- static struct attribute *zram_disk_attrs[] = {
- &dev_attr_disksize.attr,
- &dev_attr_initstate.attr,
- &dev_attr_reset.attr,
- &dev_attr_compact.attr,
- &dev_attr_mem_limit.attr,
- &dev_attr_mem_used_max.attr,
- &dev_attr_max_comp_streams.attr,
- &dev_attr_comp_algorithm.attr,
- #ifdef CONFIG_ZRAM_WRITEBACK
- &dev_attr_backing_dev.attr,
- #endif
- &dev_attr_use_dedup.attr,
- &dev_attr_io_stat.attr,
- &dev_attr_mm_stat.attr,
- #ifdef CONFIG_ZRAM_WRITEBACK
- &dev_attr_bd_stat.attr,
- #endif
- &dev_attr_debug_stat.attr,
- NULL,
- };
- static const struct attribute_group zram_disk_attr_group = {
- .attrs = zram_disk_attrs,
- };
- static const struct attribute_group *zram_disk_attr_groups[] = {
- &zram_disk_attr_group,
- NULL,
- };
- /*
- * Allocate and initialize new zram device. the function returns
- * '>= 0' device_id upon success, and negative value otherwise.
- */
- static int zram_add(void)
- {
- struct zram *zram;
- struct request_queue *queue;
- int ret, device_id;
- zram = kzalloc(sizeof(struct zram), GFP_KERNEL);
- if (!zram)
- return -ENOMEM;
- if (!zram_devices)
- zram_devices = zram;
- ret = idr_alloc(&zram_index_idr, zram, 0, 0, GFP_KERNEL);
- if (ret < 0)
- goto out_free_dev;
- device_id = ret;
- init_rwsem(&zram->init_lock);
- #ifdef CONFIG_ZRAM_DEDUP
- zram->use_dedup = true;
- #endif
- queue = blk_alloc_queue(GFP_KERNEL);
- if (!queue) {
- pr_err("Error allocating disk queue for device %d\n",
- device_id);
- ret = -ENOMEM;
- goto out_free_idr;
- }
- blk_queue_make_request(queue, zram_make_request);
- /* gendisk structure */
- zram->disk = alloc_disk(1);
- if (!zram->disk) {
- pr_err("Error allocating disk structure for device %d\n",
- device_id);
- ret = -ENOMEM;
- goto out_free_queue;
- }
- zram->disk->major = zram_major;
- zram->disk->first_minor = device_id;
- zram->disk->fops = &zram_devops;
- zram->disk->queue = queue;
- zram->disk->queue->queuedata = zram;
- zram->disk->private_data = zram;
- snprintf(zram->disk->disk_name, 16, "zram%d", device_id);
- /* Actual capacity set using syfs (/sys/block/zram<id>/disksize */
- set_capacity(zram->disk, 0);
- /* zram devices sort of resembles non-rotational disks */
- queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zram->disk->queue);
- queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, zram->disk->queue);
- /*
- * To ensure that we always get PAGE_SIZE aligned
- * and n*PAGE_SIZED sized I/O requests.
- */
- blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE);
- blk_queue_logical_block_size(zram->disk->queue,
- ZRAM_LOGICAL_BLOCK_SIZE);
- blk_queue_io_min(zram->disk->queue, PAGE_SIZE);
- blk_queue_io_opt(zram->disk->queue, PAGE_SIZE);
- zram->disk->queue->limits.discard_granularity = PAGE_SIZE;
- blk_queue_max_discard_sectors(zram->disk->queue, UINT_MAX);
- queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zram->disk->queue);
- /*
- * zram_bio_discard() will clear all logical blocks if logical block
- * size is identical with physical block size(PAGE_SIZE). But if it is
- * different, we will skip discarding some parts of logical blocks in
- * the part of the request range which isn't aligned to physical block
- * size. So we can't ensure that all discarded logical blocks are
- * zeroed.
- */
- if (ZRAM_LOGICAL_BLOCK_SIZE == PAGE_SIZE)
- blk_queue_max_write_zeroes_sectors(zram->disk->queue, UINT_MAX);
- zram->disk->queue->backing_dev_info->capabilities |=
- (BDI_CAP_STABLE_WRITES | BDI_CAP_SYNCHRONOUS_IO);
- disk_to_dev(zram->disk)->groups = zram_disk_attr_groups;
- add_disk(zram->disk);
- strlcpy(zram->compressor, default_compressor, sizeof(zram->compressor));
- zram_debugfs_register(zram);
- if (!zram_devices)
- zram_devices = zram;
- pr_info("Added device: %s\n", zram->disk->disk_name);
- return device_id;
- out_free_queue:
- blk_cleanup_queue(queue);
- out_free_idr:
- idr_remove(&zram_index_idr, device_id);
- out_free_dev:
- kfree(zram);
- return ret;
- }
- static int zram_remove(struct zram *zram)
- {
- struct block_device *bdev;
- bdev = bdget_disk(zram->disk, 0);
- if (!bdev)
- return -ENOMEM;
- mutex_lock(&bdev->bd_mutex);
- if (bdev->bd_openers || zram->claim) {
- mutex_unlock(&bdev->bd_mutex);
- bdput(bdev);
- return -EBUSY;
- }
- zram->claim = true;
- mutex_unlock(&bdev->bd_mutex);
- zram_debugfs_unregister(zram);
- /* Make sure all the pending I/O are finished */
- fsync_bdev(bdev);
- zram_reset_device(zram);
- bdput(bdev);
- pr_info("Removed device: %s\n", zram->disk->disk_name);
- del_gendisk(zram->disk);
- blk_cleanup_queue(zram->disk->queue);
- put_disk(zram->disk);
- if (zram_devices == zram)
- zram_devices = NULL;
- kfree(zram);
- return 0;
- }
- /* zram-control sysfs attributes */
- /*
- * NOTE: hot_add attribute is not the usual read-only sysfs attribute. In a
- * sense that reading from this file does alter the state of your system -- it
- * creates a new un-initialized zram device and returns back this device's
- * device_id (or an error code if it fails to create a new device).
- */
- static ssize_t hot_add_show(struct class *class,
- struct class_attribute *attr,
- char *buf)
- {
- int ret;
- mutex_lock(&zram_index_mutex);
- ret = zram_add();
- mutex_unlock(&zram_index_mutex);
- if (ret < 0)
- return ret;
- return scnprintf(buf, PAGE_SIZE, "%d\n", ret);
- }
- static struct class_attribute class_attr_hot_add =
- __ATTR(hot_add, 0400, hot_add_show, NULL);
- static ssize_t hot_remove_store(struct class *class,
- struct class_attribute *attr,
- const char *buf,
- size_t count)
- {
- struct zram *zram;
- int ret, dev_id;
- /* dev_id is gendisk->first_minor, which is `int' */
- ret = kstrtoint(buf, 10, &dev_id);
- if (ret)
- return ret;
- if (dev_id < 0)
- return -EINVAL;
- mutex_lock(&zram_index_mutex);
- zram = idr_find(&zram_index_idr, dev_id);
- if (zram) {
- ret = zram_remove(zram);
- if (!ret)
- idr_remove(&zram_index_idr, dev_id);
- } else {
- ret = -ENODEV;
- }
- mutex_unlock(&zram_index_mutex);
- return ret ? ret : count;
- }
- static CLASS_ATTR_WO(hot_remove);
- static struct attribute *zram_control_class_attrs[] = {
- &class_attr_hot_add.attr,
- &class_attr_hot_remove.attr,
- NULL,
- };
- ATTRIBUTE_GROUPS(zram_control_class);
- static struct class zram_control_class = {
- .name = "zram-control",
- .owner = THIS_MODULE,
- .class_groups = zram_control_class_groups,
- };
- static int zram_remove_cb(int id, void *ptr, void *data)
- {
- zram_remove(ptr);
- return 0;
- }
- static void destroy_devices(void)
- {
- class_unregister(&zram_control_class);
- idr_for_each(&zram_index_idr, &zram_remove_cb, NULL);
- zram_debugfs_destroy();
- idr_destroy(&zram_index_idr);
- unregister_blkdev(zram_major, "zram");
- cpuhp_remove_multi_state(CPUHP_ZCOMP_PREPARE);
- }
- unsigned long zram_mlog(void)
- {
- #define P2K(x) (((unsigned long)x) << (PAGE_SHIFT - 10))
- if (num_devices == 0 && init_done(zram_devices))
- return P2K(zs_get_total_pages(zram_devices->mem_pool));
- #undef P2K
- return 0;
- }
- static int __init zram_init(void)
- {
- int ret;
- ret = cpuhp_setup_state_multi(CPUHP_ZCOMP_PREPARE, "block/zram:prepare",
- zcomp_cpu_up_prepare, zcomp_cpu_dead);
- if (ret < 0)
- return ret;
- ret = class_register(&zram_control_class);
- if (ret) {
- pr_err("Unable to register zram-control class\n");
- cpuhp_remove_multi_state(CPUHP_ZCOMP_PREPARE);
- return ret;
- }
- zram_debugfs_create();
- zram_major = register_blkdev(0, "zram");
- if (zram_major <= 0) {
- pr_err("Unable to get major number\n");
- class_unregister(&zram_control_class);
- cpuhp_remove_multi_state(CPUHP_ZCOMP_PREPARE);
- return -EBUSY;
- }
- while (num_devices != 0) {
- mutex_lock(&zram_index_mutex);
- ret = zram_add();
- mutex_unlock(&zram_index_mutex);
- if (ret < 0)
- goto out_error;
- num_devices--;
- }
- init_zram_wb();
- return 0;
- out_error:
- destroy_devices();
- return ret;
- }
- static void __exit zram_exit(void)
- {
- destroy_zram_wb();
- destroy_devices();
- }
- module_init(zram_init);
- module_exit(zram_exit);
- module_param(num_devices, uint, 0);
- MODULE_PARM_DESC(num_devices, "Number of pre-created zram devices");
- MODULE_LICENSE("Dual BSD/GPL");
- MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");
- MODULE_DESCRIPTION("Compressed RAM Block Device");
|