From nobody Wed Dec 17 10:47:35 2025 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 66D40213235; Fri, 28 Mar 2025 06:14:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743142497; cv=none; b=JgB1+NfUmbIhGO2gMU0Ly7wFQxcwO0C4QaFC7PhWqXtnpfMiu6FQHQD4OgmbmpesnENCwNrxcyv0zohcZZEeEfpm5AHa3qO4hvh2Hsq04UMo34LNckmsvyC+iPc/Hvw8pkzh5Sg7pKnEYODNpMFm7fqZn6cn8ua019+5KkwWtZo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743142497; c=relaxed/simple; bh=YsTUHibJzB0YAPhLPMm/21lUnHVQrv3NyQyhKOZA7nQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=WjgHtGcDRcHIcwMP13DXSGZWhTmiAQljmWFL9YKMDWAxk4EN09DJRWZ8Sl44lkNMX6U1QsqRhAg0w81OOE08dFm4R1u8d3aZowPrTBT27HIt6L5i0YePBwNUnG6BES6qgv6lhSDGO9ItmBErmOXHu5kkhyAsBxzYjrbdgYTRxaU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=none smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.216]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4ZP9Gf5tL1z4f3jZd; Fri, 28 Mar 2025 14:14:34 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id 6D57F1A1BA7; Fri, 28 Mar 2025 14:14:52 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.104.67]) by APP4 (Coremail) with SMTP id gCh0CgDHK2BSPuZnfAUtHw--.25875S14; Fri, 28 Mar 2025 14:14:52 +0800 (CST) From: Yu Kuai To: hch@lst.de, xni@redhat.com, colyli@kernel.org, axboe@kernel.dk, agk@redhat.com, snitzer@kernel.org, mpatocka@redhat.com, song@kernel.org, yukuai3@huawei.com Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, dm-devel@lists.linux.dev, linux-raid@vger.kernel.org, yukuai1@huaweicloud.com, yi.zhang@huawei.com, yangerkun@huawei.com Subject: [PATCH RFC v2 10/14] md/md-llbitmap: implement APIs to dirty bits and clear bits Date: Fri, 28 Mar 2025 14:08:49 +0800 Message-Id: <20250328060853.4124527-11-yukuai1@huaweicloud.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20250328060853.4124527-1-yukuai1@huaweicloud.com> References: <20250328060853.4124527-1-yukuai1@huaweicloud.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: gCh0CgDHK2BSPuZnfAUtHw--.25875S14 X-Coremail-Antispam: 1UD129KBjvJXoW3XrWkCF43XFW3Wr13KF4kWFg_yoWxZFy8pF 43Xw15Gr45J3yFgryUJr9rAF15tr4ktr9IqF97A34rWw1jyrsIqFW8GFyUAws5Wrn3GFnr Aw45Kr95Cw4fZFDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmS14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JF0E3s1l82xGYI kIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2 z4x0Y4vE2Ix0cI8IcVAFwI0_tr0E3s1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F 4UJwA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq 3wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7 IYx2IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4U M4x0Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2 kIc2xKxwCY1x0262kKe7AKxVWUtVW8ZwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkE bVWUJVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67 AF67kF1VAFwI0_GFv_WrylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVW8JVW5JwCI 42IY6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F4UJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF 4lIxAIcVC2z280aVAFwI0_Gr0_Cr1lIxAIcVC2z280aVCY1x0267AKxVW8Jr0_Cr1UYxBI daVFxhVjvjDU0xZFpf9x0JUQFxUUUUUU= X-CM-SenderInfo: 51xn3trlr6x35dzhxuhorxvhhfrp/ Content-Type: text/plain; charset="utf-8" From: Yu Kuai Include following APIs: - llbitmap_startwrite - llbitmap_endwrite - llbitmap_unplug - llbitmap_flush Signed-off-by: Yu Kuai --- drivers/md/md-bitmap.h | 1 + drivers/md/md-llbitmap.c | 171 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 172 insertions(+) diff --git a/drivers/md/md-bitmap.h b/drivers/md/md-bitmap.h index 9e8bc7895751..7a3cd2f70772 100644 --- a/drivers/md/md-bitmap.h +++ b/drivers/md/md-bitmap.h @@ -23,6 +23,7 @@ enum bitmap_state { BITMAP_STALE =3D 1, /* the bitmap file is out of date or had -EIO */ BITMAP_WRITE_ERROR =3D 2, /* A write error has occurred */ BITMAP_FIRST_USE =3D 3, /* llbtimap is just created */ + BITMAP_DAEMON_BUSY =3D 4, /* llbitmap daemon is still not done after daem= on_sleep */ BITMAP_HOSTENDIAN =3D15, }; =20 diff --git a/drivers/md/md-llbitmap.c b/drivers/md/md-llbitmap.c index 1452887ffc5d..982ec868ce22 100644 --- a/drivers/md/md-llbitmap.c +++ b/drivers/md/md-llbitmap.c @@ -145,6 +145,7 @@ enum llbitmap_action { struct llbitmap_barrier { struct percpu_ref active; unsigned long expire; + bool flush; wait_queue_head_t wait; } ____cacheline_aligned_in_smp; =20 @@ -182,6 +183,12 @@ struct llbitmap_bio { struct bio bio; }; =20 +struct llbitmap_unplug_work { + struct work_struct work; + struct llbitmap *llbitmap; + struct completion *done; +}; + static struct workqueue_struct *md_llbitmap_io_wq; static struct workqueue_struct *md_llbitmap_unplug_wq; =20 @@ -793,6 +800,54 @@ static int llbitmap_read_sb(struct llbitmap *llbitmap) return ret; } =20 +static void llbitmap_pending_timer_fn(struct timer_list *t) +{ + struct llbitmap *llbitmap =3D from_timer(llbitmap, t, pending_timer); + + if (work_busy(&llbitmap->daemon_work)) { + pr_warn("daemon_work not finished\n"); + set_bit(BITMAP_DAEMON_BUSY, &llbitmap->flags); + return; + } + + queue_work(md_llbitmap_io_wq, &llbitmap->daemon_work); +} + +static void md_llbitmap_daemon_fn(struct work_struct *work) +{ + struct llbitmap *llbitmap =3D + container_of(work, struct llbitmap, daemon_work); + unsigned long start =3D 0; + unsigned long end =3D min(llbitmap->chunks, PAGE_SIZE - BITMAP_SB_SIZE) -= 1; + bool restart =3D false; + int page_idx =3D 0; + + while (page_idx < llbitmap->nr_pages) { + struct llbitmap_barrier *barrier =3D &llbitmap->barrier[page_idx]; + + if (page_idx > 0) { + start =3D end + 1; + end =3D min(end + PAGE_SIZE, llbitmap->chunks - 1); + } + + if (!barrier->flush && time_before(jiffies, barrier->expire)) { + restart =3D true; + page_idx++; + continue; + } + + llbitmap_suspend(llbitmap, page_idx); + llbitmap_state_machine(llbitmap, start, end, BitmapActionDaemon); + llbitmap_resume(llbitmap, page_idx); + + page_idx++; + } + + if (test_and_clear_bit(BITMAP_DAEMON_BUSY, &llbitmap->flags) || restart) + mod_timer(&llbitmap->pending_timer, + jiffies + llbitmap->mddev->bitmap_info.daemon_sleep * HZ); +} + static int llbitmap_create(struct mddev *mddev) { struct llbitmap *llbitmap; @@ -905,6 +960,117 @@ static void llbitmap_destroy(struct mddev *mddev) mutex_unlock(&mddev->bitmap_info.mutex); } =20 +static int llbitmap_startwrite(struct mddev *mddev, sector_t offset, + unsigned long sectors, bool is_discard) +{ + struct llbitmap *llbitmap =3D mddev->bitmap; + enum llbitmap_action action; + unsigned long start; + unsigned long end; + int page_start; + int page_end; + + if (likely(!is_discard)) { + start =3D offset >> llbitmap->chunkshift; + end =3D (offset + sectors - 1) >> llbitmap->chunkshift; + action =3D BitmapActionStartwrite; + } else { + /* + * For discard, the bit can be handled only if the discard range + * cover the whole bit, hence round start up, and end down. + */ + start =3D DIV_ROUND_UP(offset, llbitmap->chunksize); + end =3D (offset + sectors - 1) >> llbitmap->chunkshift; + action =3D BitmapActionDiscard; + } + + llbitmap_state_machine(llbitmap, start, end, action); + + page_start =3D (start + BITMAP_SB_SIZE) >> PAGE_SHIFT; + page_end =3D (end + BITMAP_SB_SIZE) >> PAGE_SHIFT; + + while (page_start <=3D page_end) { + llbitmap_raise_barrier(llbitmap, page_start); + page_start++; + } + + return 0; +} + +static void llbitmap_endwrite(struct mddev *mddev, sector_t offset, + unsigned long sectors, bool is_discard) +{ + struct llbitmap *llbitmap =3D mddev->bitmap; + unsigned long start; + unsigned long end; + int page_start; + int page_end; + + if (likely(!is_discard)) { + start =3D offset >> llbitmap->chunkshift; + end =3D (offset + sectors - 1) >> llbitmap->chunkshift; + } else { + /* + * For discard, the bit can be handled only if the discard range + * cover the whole bit, hence round start up, and end down. + */ + start =3D DIV_ROUND_UP(offset, llbitmap->chunksize); + end =3D (offset + sectors - 1) >> llbitmap->chunkshift; + } + + page_start =3D (start + BITMAP_SB_SIZE) >> PAGE_SHIFT; + page_end =3D (end + BITMAP_SB_SIZE) >> PAGE_SHIFT; + + while (page_start <=3D page_end) { + llbitmap_release_barrier(llbitmap, page_start); + page_start++; + } +} + +static void llbitmap_unplug_fn(struct work_struct *work) +{ + struct llbitmap_unplug_work *unplug_work =3D + container_of(work, struct llbitmap_unplug_work, work); + struct llbitmap *llbitmap =3D unplug_work->llbitmap; + + filemap_write_and_wait_range(llbitmap->bitmap_file->f_mapping, + BITMAP_SB_SIZE, + BITMAP_SB_SIZE + llbitmap->chunks - 1); + complete(unplug_work->done); +} + +static void llbitmap_unplug(struct mddev *mddev, bool sync) +{ + DECLARE_COMPLETION_ONSTACK(done); + struct llbitmap *llbitmap =3D mddev->bitmap; + struct llbitmap_unplug_work unplug_work =3D { + .llbitmap =3D llbitmap, + .done =3D &done, + }; + + if (!mapping_tagged(llbitmap->bitmap_file->f_mapping, + PAGECACHE_TAG_DIRTY)) + return; + + INIT_WORK_ONSTACK(&unplug_work.work, llbitmap_unplug_fn); + queue_work(md_llbitmap_unplug_wq, &unplug_work.work); + wait_for_completion(&done); + destroy_work_on_stack(&unplug_work.work); +} + +static void llbitmap_flush(struct mddev *mddev) +{ + struct llbitmap *llbitmap =3D mddev->bitmap; + int i; + + for (i =3D 0; i < llbitmap->nr_pages; i++) + llbitmap->barrier[i].flush =3D true; + + del_timer_sync(&llbitmap->pending_timer); + queue_work(md_llbitmap_io_wq, &llbitmap->daemon_work); + flush_work(&llbitmap->daemon_work); +} + static struct bitmap_operations llbitmap_ops =3D { .head =3D { .type =3D MD_BITMAP, @@ -916,4 +1082,9 @@ static struct bitmap_operations llbitmap_ops =3D { .resize =3D llbitmap_resize, .load =3D llbitmap_load, .destroy =3D llbitmap_destroy, + + .startwrite =3D llbitmap_startwrite, + .endwrite =3D llbitmap_endwrite, + .unplug =3D llbitmap_unplug, + .flush =3D llbitmap_flush, }; --=20 2.39.2