From patchwork Mon Jan 27 07:29:12 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13951021 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 38B38C0218C for ; Mon, 27 Jan 2025 07:29:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BF7A1280120; Mon, 27 Jan 2025 02:29:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BA7842800DA; Mon, 27 Jan 2025 02:29:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A6F91280120; Mon, 27 Jan 2025 02:29:52 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id E65432800DA for ; Mon, 27 Jan 2025 02:29:50 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 4B24081FF9 for ; Mon, 27 Jan 2025 07:29:50 +0000 (UTC) X-FDA: 83052407340.03.E6072D4 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) by imf24.hostedemail.com (Postfix) with ESMTP id 5069A180004 for ; Mon, 27 Jan 2025 07:29:47 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=PfnO+aPv; spf=pass (imf24.hostedemail.com: domain of senozhatsky@chromium.org designates 209.85.214.177 as permitted sender) smtp.mailfrom=senozhatsky@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737962987; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=q566LymSR3yLFFaT+LJFKvYojLsB2mtA0so6QzTzlYU=; b=whX5i62oQ7N8AhWeFc9KBAaUeH9m4FI8njhhcI5HE4rFqUAktegXWhS4JOlcuiKFOGCd5Z yDX60AAFjJgTYRqS08QoO2+sETGceZ/Kap4Qsd5EE0vj5naBgmkJUpyE/ZSRBGC3/jO3mT X10nflTtIrtOpSnGZYqnhkzknVld4uY= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=PfnO+aPv; spf=pass (imf24.hostedemail.com: domain of senozhatsky@chromium.org designates 209.85.214.177 as permitted sender) smtp.mailfrom=senozhatsky@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737962987; a=rsa-sha256; cv=none; b=HoE5VvM/ZpNW6NQFvRBcmuCtqSHBSjp5IiQhd16cks6B/kpk0lKgIU9U+yYSJ18hTnrrib 9HhX7FzMEh30b5L51Aibu0JQWfEIMrLFw9ZusRpiZiVfichpdpeuoe1Z6gvnr+ZNzQIW8a iX5q260ihMiyRUSg0YiWkocpkGQ7yuE= Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-215770613dbso49459295ad.2 for ; Sun, 26 Jan 2025 23:29:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1737962986; x=1738567786; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=q566LymSR3yLFFaT+LJFKvYojLsB2mtA0so6QzTzlYU=; b=PfnO+aPvX3vb0jEH8TXjkLjVK7EMVKv8CSSzWmn3Tqzz/kD4Q9UWmRpwEFj7iDk7wE fp8YCPOK4D/N14bjaSnSGmZwX4C0fFlBKiCrbQNwHWmc5xF07PmjXix+nXF9q0S+3KW2 Hu/+WMh0a7N8iY1NkS6ABFCkjkviElfh+uh6k= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737962986; x=1738567786; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=q566LymSR3yLFFaT+LJFKvYojLsB2mtA0so6QzTzlYU=; b=IV0nNNuRs/PAWM0GI8/mEKtZaOyfYxIpCFmNEfL8cc1S3rauAawpa0K58IWKSie+4q uR0ZlTSDU7MSdRMHCjwMoaSqNjQoQx+Vum3l/WjLWa7V4JlMjjqZTwBYB7m/afH1yinR s1QfMtB8r99XzDMi1ol1HMpib0n4WXws+B7ZUA+/pgpO6m5O/rZKrhPIa5CnJIfof+2D AuIhzdw5MOTdCvKVShJnYdiQAPtR9ILxRCN/HDLqspoKrzN/TRMD87kx4DoamDY/ql1e tDh7fjVhKGP6hv9hpIzXdL18T8tjEh2chhpnW9IOHNYFR/EurPn66SPzxINPvXEmOcGQ nWOg== X-Gm-Message-State: AOJu0Yze3CWAvfLxmoPMydUi8enmlHm3XV4qH/16immDGHtw2qDP/SqT Zymh0XfymQwr7SNvdO6m1cDb+ORo+P3b96/QQQywb++D6lRKFXRXjgURIHMZNg== X-Gm-Gg: ASbGncuuBdlT95jlOQAwiY2RC8duifkKP79uNonvW/QSd/buM04bS3AXB0mZfY6tNDm XetV99jm6EWCj2lgctaE7hAEIc0aAT/dAdIfGKxl3GGrtOxt2fpDSgUDz1GxMttUBwsR+rw0SPf fcBuepWQ3SGVVUapGvxwiUan3S9BBMH4fVkJc17575cGxuIr/uH+rx+fUItUUmaMrC5ui/U30IB S4SpsZhCSVncffUJ4GjMRHrLE95o7ErifUsP+YvgHSOmbKAgC4HZMnCFlVavTaKpvv2Xoy23iOJ Z5lonSA= X-Google-Smtp-Source: AGHT+IHWGcaKLW0ar/9axezARL9DLtAwfcGy5WchQ6brciU9TbvtbSpVRLYsS13AacDLWO3sj1eyFw== X-Received: by 2002:a05:6a00:8013:b0:725:8c0f:6fa3 with SMTP id d2e1a72fcca58-72dafbaae38mr52843068b3a.22.1737962986053; Sun, 26 Jan 2025 23:29:46 -0800 (PST) Received: from localhost ([2401:fa00:8f:203:566d:6152:c049:8d3a]) by smtp.gmail.com with UTF8SMTPSA id d2e1a72fcca58-72f8a69f41esm6319379b3a.31.2025.01.26.23.29.44 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 26 Jan 2025 23:29:45 -0800 (PST) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv2 1/9] zram: switch to non-atomic entry locking Date: Mon, 27 Jan 2025 16:29:12 +0900 Message-ID: <20250127072932.1289973-2-senozhatsky@chromium.org> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog In-Reply-To: <20250127072932.1289973-1-senozhatsky@chromium.org> References: <20250127072932.1289973-1-senozhatsky@chromium.org> MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 5069A180004 X-Stat-Signature: raabz6yinm4pfrxz9wf6w5mhnjduckm5 X-Rspam-User: X-HE-Tag: 1737962987-904273 X-HE-Meta: U2FsdGVkX18uJnOzoFPqPv0kDvuFR7lL3Nym1Ie+z5UniC9k20eNXoaeYuIkzvBl2UcR0zV4+49zZOHXXyiR+830p+3t9Fnig4QcGnfm5y8HOkFlcdjaqgptzHLFVmpQyutDeg04D1Sx6eos19tDLQ4fUF2vxGJvqVHoz+7zA+VBCdOc0Tu7xZ3EdGTykYZMJRi3iU2H197YU9eBNz3I/bNmris6Dq/2kxuUUFhzYJrbzrriPel9Q0kky7KAzpPLDyONhxQQWTdvoeVY3gVu8fNlW7+IU6qIci7AaPnbpGFR7k4zZYS2RXn9cXqGsS01gJOcKiVZgaAW8j/9Kvg8WAGY/dkJH9InawaiYBXrSWiSspi7UNXQyWeipM1EMTOI2E1DzAQwU1A57nWk0gLLrHqzVBzn0Z3rl4VBkJ8CxG7GaBehH0G2LzTQmrKiXkSIofngecWZqjNgHQeEXSEwYuIpQuYGJ1r6rzXlzOKSNwggC8BTV/Oz5VDK7PNdIItcFR7dapiCVxx1ezzmGCCNt1FzDZ6JPwEi5U+raInV9La2NorUUrjbc0gfG7nAdoXJNRW6oOsNfkRzhYmwzx7scCmugoapDIEEXLoYu1/rMcIGY/pN45iixPvdYS7lYIG89qdh0gphB8ZeQcdiMIxAnY5ECiPtDNDOxWgiHamSU/WHbK6NjfY2Orfx1wklcUURUjuDGh/jXR2bgA874StWxcL6xU0MjINTrmaPytNMjYXD7Gvi8Q6mdBxC0PBzzp6WujNMf0mUl76FnzMVmmTiE1vLTb5SU43F7o6r8jbw47K6bnrZ93XTRdt747UkW0I1onv0Ly1IJ1gTifaR+BrD5qo2G985FvZ7Y27S3EVlpmqktVMHJRUBMcgZkYV0lZaPZUT1RgmV/eooyUOki2/wN1qMrH81oALIdn9gW667HeqnLI8NAG5nECi6stmXpoyll8w3VENq/lT0YOX6RLW n/Zwp9oW sIzsOLr6653R02CXP153o88ReJShgiseWKJJTPXHaHheYKu9PJkpdrnr3R3p1BhPYa2fNVj+6QKy8rZbsDazZhgVw/1MrRwSqXC/HUwjXPtWqHxhzaTJjhLsvtxwrqVCgd+pqy7f22PyVCqGrpl2WlZp/c2bfnFreMDkxiOo6sv66Y+REISkN0pldF26nWt5YNEnWUziNtjtIXGQQIqbenO1dpbwu4vEZjhQcxq5Q79qPRtXFzBGTAvhIsOwT3GAHvmqgAxbuk4wkcsA3tNIWWUfS1pxBdC5up83eXxrUHcGINVfMcT3S5HCsrA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Concurrent modifications of meta table entries is now handled by per-entry spin-lock. This has a number of shortcomings. First, this imposes atomic requirements on compression backends. zram can call both zcomp_compress() and zcomp_decompress() under entry spin-lock, which implies that we can use only compression algorithms that don't schedule/sleep/wait during compression and decompression. This, for instance, makes it impossible to use some of the ASYNC compression algorithms (H/W compression, etc.) implementations. Second, this can potentially trigger watchdogs. For example, entry re-compression with secondary algorithms is performed under entry spin-lock. Given that we chain secondary compression algorithms and that some of them can be configured for best compression ratio (and worst compression speed) zram can stay under spin-lock for quite some time. Do not use per-entry spin-locks and instead convert it to an atomic_t variable which open codes reader-writer type of lock. This permits preemption from slot_lock section. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/zram_drv.c | 155 +++++++++++++++++++++------------- drivers/block/zram/zram_drv.h | 7 +- 2 files changed, 98 insertions(+), 64 deletions(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 9f5020b077c5..14859bd2611f 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -58,19 +58,57 @@ static void zram_free_page(struct zram *zram, size_t index); static int zram_read_from_zspool(struct zram *zram, struct page *page, u32 index); -static int zram_slot_trylock(struct zram *zram, u32 index) +static int zram_slot_write_trylock(struct zram *zram, u32 index) { - return spin_trylock(&zram->table[index].lock); + int old; + + old = atomic_cmpxchg(&zram->table[index].lock, ZRAM_ENTRY_UNLOCKED, + ZRAM_ENTRY_WRLOCKED); + return old == ZRAM_ENTRY_UNLOCKED; } -static void zram_slot_lock(struct zram *zram, u32 index) +static void zram_slot_write_lock(struct zram *zram, u32 index) { - spin_lock(&zram->table[index].lock); + atomic_t *lock = &zram->table[index].lock; + int old; + + while (1) { + old = atomic_cmpxchg(lock, ZRAM_ENTRY_UNLOCKED, + ZRAM_ENTRY_WRLOCKED); + if (old == ZRAM_ENTRY_UNLOCKED) + return; + + cond_resched(); + } } -static void zram_slot_unlock(struct zram *zram, u32 index) +static void zram_slot_write_unlock(struct zram *zram, u32 index) { - spin_unlock(&zram->table[index].lock); + atomic_set(&zram->table[index].lock, ZRAM_ENTRY_UNLOCKED); +} + +static void zram_slot_read_lock(struct zram *zram, u32 index) +{ + atomic_t *lock = &zram->table[index].lock; + int old; + + while (1) { + old = atomic_read(lock); + if (old == ZRAM_ENTRY_WRLOCKED) { + cond_resched(); + continue; + } + + if (atomic_cmpxchg(lock, old, old + 1) == old) + return; + + cond_resched(); + } +} + +static void zram_slot_read_unlock(struct zram *zram, u32 index) +{ + atomic_dec(&zram->table[index].lock); } static inline bool init_done(struct zram *zram) @@ -93,7 +131,6 @@ static void zram_set_handle(struct zram *zram, u32 index, unsigned long handle) zram->table[index].handle = handle; } -/* flag operations require table entry bit_spin_lock() being held */ static bool zram_test_flag(struct zram *zram, u32 index, enum zram_pageflags flag) { @@ -229,9 +266,9 @@ static void release_pp_slot(struct zram *zram, struct zram_pp_slot *pps) { list_del_init(&pps->entry); - zram_slot_lock(zram, pps->index); + zram_slot_write_lock(zram, pps->index); zram_clear_flag(zram, pps->index, ZRAM_PP_SLOT); - zram_slot_unlock(zram, pps->index); + zram_slot_write_unlock(zram, pps->index); kfree(pps); } @@ -394,11 +431,11 @@ static void mark_idle(struct zram *zram, ktime_t cutoff) * * And ZRAM_WB slots simply cannot be ZRAM_IDLE. */ - zram_slot_lock(zram, index); + zram_slot_write_lock(zram, index); if (!zram_allocated(zram, index) || zram_test_flag(zram, index, ZRAM_WB) || zram_test_flag(zram, index, ZRAM_SAME)) { - zram_slot_unlock(zram, index); + zram_slot_write_unlock(zram, index); continue; } @@ -410,7 +447,7 @@ static void mark_idle(struct zram *zram, ktime_t cutoff) zram_set_flag(zram, index, ZRAM_IDLE); else zram_clear_flag(zram, index, ZRAM_IDLE); - zram_slot_unlock(zram, index); + zram_slot_write_unlock(zram, index); } } @@ -709,7 +746,7 @@ static int scan_slots_for_writeback(struct zram *zram, u32 mode, INIT_LIST_HEAD(&pps->entry); - zram_slot_lock(zram, index); + zram_slot_write_lock(zram, index); if (!zram_allocated(zram, index)) goto next; @@ -731,7 +768,7 @@ static int scan_slots_for_writeback(struct zram *zram, u32 mode, place_pp_slot(zram, ctl, pps); pps = NULL; next: - zram_slot_unlock(zram, index); + zram_slot_write_unlock(zram, index); } kfree(pps); @@ -822,7 +859,7 @@ static ssize_t writeback_store(struct device *dev, } index = pps->index; - zram_slot_lock(zram, index); + zram_slot_read_lock(zram, index); /* * scan_slots() sets ZRAM_PP_SLOT and relases slot lock, so * slots can change in the meantime. If slots are accessed or @@ -833,7 +870,7 @@ static ssize_t writeback_store(struct device *dev, goto next; if (zram_read_from_zspool(zram, page, index)) goto next; - zram_slot_unlock(zram, index); + zram_slot_read_unlock(zram, index); bio_init(&bio, zram->bdev, &bio_vec, 1, REQ_OP_WRITE | REQ_SYNC); @@ -860,7 +897,7 @@ static ssize_t writeback_store(struct device *dev, } atomic64_inc(&zram->stats.bd_writes); - zram_slot_lock(zram, index); + zram_slot_write_lock(zram, index); /* * Same as above, we release slot lock during writeback so * slot can change under us: slot_free() or slot_free() and @@ -882,7 +919,7 @@ static ssize_t writeback_store(struct device *dev, zram->bd_wb_limit -= 1UL << (PAGE_SHIFT - 12); spin_unlock(&zram->wb_limit_lock); next: - zram_slot_unlock(zram, index); + zram_slot_write_unlock(zram, index); release_pp_slot(zram, pps); cond_resched(); @@ -1001,7 +1038,7 @@ static ssize_t read_block_state(struct file *file, char __user *buf, for (index = *ppos; index < nr_pages; index++) { int copied; - zram_slot_lock(zram, index); + zram_slot_read_lock(zram, index); if (!zram_allocated(zram, index)) goto next; @@ -1019,13 +1056,13 @@ static ssize_t read_block_state(struct file *file, char __user *buf, ZRAM_INCOMPRESSIBLE) ? 'n' : '.'); if (count <= copied) { - zram_slot_unlock(zram, index); + zram_slot_read_unlock(zram, index); break; } written += copied; count -= copied; next: - zram_slot_unlock(zram, index); + zram_slot_read_unlock(zram, index); *ppos += 1; } @@ -1455,33 +1492,31 @@ static void zram_meta_free(struct zram *zram, u64 disksize) static bool zram_meta_alloc(struct zram *zram, u64 disksize) { - size_t num_pages, index; + size_t num_ents, index; - num_pages = disksize >> PAGE_SHIFT; - zram->table = vzalloc(array_size(num_pages, sizeof(*zram->table))); + num_ents = disksize >> PAGE_SHIFT; + zram->table = vzalloc(array_size(num_ents, sizeof(*zram->table))); if (!zram->table) - return false; + goto error; zram->mem_pool = zs_create_pool(zram->disk->disk_name); - if (!zram->mem_pool) { - vfree(zram->table); - zram->table = NULL; - return false; - } + if (!zram->mem_pool) + goto error; if (!huge_class_size) huge_class_size = zs_huge_class_size(zram->mem_pool); - for (index = 0; index < num_pages; index++) - spin_lock_init(&zram->table[index].lock); + for (index = 0; index < num_ents; index++) + atomic_set(&zram->table[index].lock, ZRAM_ENTRY_UNLOCKED); + return true; + +error: + vfree(zram->table); + zram->table = NULL; + return false; } -/* - * 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) { unsigned long handle; @@ -1602,17 +1637,17 @@ static int zram_read_page(struct zram *zram, struct page *page, u32 index, { int ret; - zram_slot_lock(zram, index); + zram_slot_read_lock(zram, index); if (!zram_test_flag(zram, index, ZRAM_WB)) { /* Slot should be locked through out the function call */ ret = zram_read_from_zspool(zram, page, index); - zram_slot_unlock(zram, index); + zram_slot_read_unlock(zram, index); } else { /* * The slot should be unlocked before reading from the backing * device. */ - zram_slot_unlock(zram, index); + zram_slot_read_unlock(zram, index); ret = read_from_bdev(zram, page, zram_get_handle(zram, index), parent); @@ -1655,10 +1690,10 @@ static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec, static int write_same_filled_page(struct zram *zram, unsigned long fill, u32 index) { - zram_slot_lock(zram, index); + zram_slot_write_lock(zram, index); zram_set_flag(zram, index, ZRAM_SAME); zram_set_handle(zram, index, fill); - zram_slot_unlock(zram, index); + zram_slot_write_unlock(zram, index); atomic64_inc(&zram->stats.same_pages); atomic64_inc(&zram->stats.pages_stored); @@ -1693,11 +1728,11 @@ static int write_incompressible_page(struct zram *zram, struct page *page, kunmap_local(src); zs_unmap_object(zram->mem_pool, handle); - zram_slot_lock(zram, index); + zram_slot_write_lock(zram, index); zram_set_flag(zram, index, ZRAM_HUGE); zram_set_handle(zram, index, handle); zram_set_obj_size(zram, index, PAGE_SIZE); - zram_slot_unlock(zram, index); + zram_slot_write_unlock(zram, index); atomic64_add(PAGE_SIZE, &zram->stats.compr_data_size); atomic64_inc(&zram->stats.huge_pages); @@ -1718,9 +1753,9 @@ static int zram_write_page(struct zram *zram, struct page *page, u32 index) bool same_filled; /* First, free memory allocated to this slot (if any) */ - zram_slot_lock(zram, index); + zram_slot_write_lock(zram, index); zram_free_page(zram, index); - zram_slot_unlock(zram, index); + zram_slot_write_unlock(zram, index); mem = kmap_local_page(page); same_filled = page_same_filled(mem, &element); @@ -1790,10 +1825,10 @@ static int zram_write_page(struct zram *zram, struct page *page, u32 index) zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP]); zs_unmap_object(zram->mem_pool, handle); - zram_slot_lock(zram, index); + zram_slot_write_lock(zram, index); zram_set_handle(zram, index, handle); zram_set_obj_size(zram, index, comp_len); - zram_slot_unlock(zram, index); + zram_slot_write_unlock(zram, index); /* Update stats */ atomic64_inc(&zram->stats.pages_stored); @@ -1850,7 +1885,7 @@ static int scan_slots_for_recompress(struct zram *zram, u32 mode, INIT_LIST_HEAD(&pps->entry); - zram_slot_lock(zram, index); + zram_slot_write_lock(zram, index); if (!zram_allocated(zram, index)) goto next; @@ -1871,7 +1906,7 @@ static int scan_slots_for_recompress(struct zram *zram, u32 mode, place_pp_slot(zram, ctl, pps); pps = NULL; next: - zram_slot_unlock(zram, index); + zram_slot_write_unlock(zram, index); } kfree(pps); @@ -2162,7 +2197,7 @@ static ssize_t recompress_store(struct device *dev, if (!num_recomp_pages) break; - zram_slot_lock(zram, pps->index); + zram_slot_write_lock(zram, pps->index); if (!zram_test_flag(zram, pps->index, ZRAM_PP_SLOT)) goto next; @@ -2170,7 +2205,7 @@ static ssize_t recompress_store(struct device *dev, &num_recomp_pages, threshold, prio, prio_max); next: - zram_slot_unlock(zram, pps->index); + zram_slot_write_unlock(zram, pps->index); release_pp_slot(zram, pps); if (err) { @@ -2217,9 +2252,9 @@ static void zram_bio_discard(struct zram *zram, struct bio *bio) } while (n >= PAGE_SIZE) { - zram_slot_lock(zram, index); + zram_slot_write_lock(zram, index); zram_free_page(zram, index); - zram_slot_unlock(zram, index); + zram_slot_write_unlock(zram, index); atomic64_inc(&zram->stats.notify_free); index++; n -= PAGE_SIZE; @@ -2248,9 +2283,9 @@ static void zram_bio_read(struct zram *zram, struct bio *bio) } flush_dcache_page(bv.bv_page); - zram_slot_lock(zram, index); + zram_slot_write_lock(zram, index); zram_accessed(zram, index); - zram_slot_unlock(zram, index); + zram_slot_write_unlock(zram, index); bio_advance_iter_single(bio, &iter, bv.bv_len); } while (iter.bi_size); @@ -2278,9 +2313,9 @@ static void zram_bio_write(struct zram *zram, struct bio *bio) break; } - zram_slot_lock(zram, index); + zram_slot_write_lock(zram, index); zram_accessed(zram, index); - zram_slot_unlock(zram, index); + zram_slot_write_unlock(zram, index); bio_advance_iter_single(bio, &iter, bv.bv_len); } while (iter.bi_size); @@ -2321,13 +2356,13 @@ static void zram_slot_free_notify(struct block_device *bdev, zram = bdev->bd_disk->private_data; atomic64_inc(&zram->stats.notify_free); - if (!zram_slot_trylock(zram, index)) { + if (!zram_slot_write_trylock(zram, index)) { atomic64_inc(&zram->stats.miss_free); return; } zram_free_page(zram, index); - zram_slot_unlock(zram, index); + zram_slot_write_unlock(zram, index); } static void zram_comp_params_reset(struct zram *zram) diff --git a/drivers/block/zram/zram_drv.h b/drivers/block/zram/zram_drv.h index db78d7c01b9a..3436ddf8ab23 100644 --- a/drivers/block/zram/zram_drv.h +++ b/drivers/block/zram/zram_drv.h @@ -15,7 +15,6 @@ #ifndef _ZRAM_DRV_H_ #define _ZRAM_DRV_H_ -#include #include #include @@ -28,7 +27,6 @@ #define ZRAM_SECTOR_PER_LOGICAL_BLOCK \ (1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT)) - /* * ZRAM is mainly used for memory efficiency so we want to keep memory * footprint small and thus squeeze size and zram pageflags into a flags @@ -58,13 +56,14 @@ enum zram_pageflags { __NR_ZRAM_PAGEFLAGS, }; -/*-- Data structures */ +#define ZRAM_ENTRY_UNLOCKED 0 +#define ZRAM_ENTRY_WRLOCKED (-1) /* Allocated for each disk page */ struct zram_table_entry { unsigned long handle; unsigned int flags; - spinlock_t lock; + atomic_t lock; #ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME ktime_t ac_time; #endif