Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-14.8 required=3.0 tests=BAYES_00, DATE_IN_FUTURE_06_12,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E8884C433E0 for ; Wed, 23 Dec 2020 07:04:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AA52A207B5 for ; Wed, 23 Dec 2020 07:04:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727063AbgLWHEz (ORCPT ); Wed, 23 Dec 2020 02:04:55 -0500 Received: from mga04.intel.com ([192.55.52.120]:26597 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727050AbgLWHEz (ORCPT ); Wed, 23 Dec 2020 02:04:55 -0500 IronPort-SDR: gzOaimxExpuG9ROUzlOPB7F0CUvgoDvuL5DPzyruPDwrfJ5X6PQSK4M2vXY8/wYFSzMQqosQaI RTffoOAorwjg== X-IronPort-AV: E=McAfee;i="6000,8403,9843"; a="173405187" X-IronPort-AV: E=Sophos;i="5.78,441,1599548400"; d="scan'208";a="173405187" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Dec 2020 23:03:40 -0800 IronPort-SDR: L+k5kTF7Wky5McdxkK5WdBPsZT8keYZALEXmEA1cyHegvpvERBtEX7OwVF92nNp5oJk+A/t+RB Sb7pDojFXTWg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.78,441,1599548400"; d="scan'208";a="565083493" Received: from ceph.sh.intel.com ([10.239.241.176]) by fmsmga005.fm.intel.com with ESMTP; 22 Dec 2020 23:03:39 -0800 From: Qiaowei Ren To: Coly Li Cc: Jianpeng Ma , linux-bcache@vger.kernel.org, Qiaowei Ren Subject: [RFC PATCH v2 3/8] bcache: initialization of the buddy Date: Wed, 23 Dec 2020 09:44:57 -0500 Message-Id: <20201223144502.25029-4-qiaowei.ren@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20201223144502.25029-1-qiaowei.ren@intel.com> References: <20201223144502.25029-1-qiaowei.ren@intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-bcache@vger.kernel.org This nvm pages allocator will implement the simple buddy to manage the nvm address space. This patch initializes this buddy for new namespace. the unit of alloc/free of the buddy is page. DAX device has their struct page(in dram or PMEM). struct { /* ZONE_DEVICE pages */ /** @pgmap: Points to the hosting device page map. */ struct dev_pagemap *pgmap; void *zone_device_data; /* * ZONE_DEVICE private pages are counted as being * mapped so the next 3 words hold the mapping, index, * and private fields from the source anonymous or * page cache page while the page is migrated to device * private memory. * ZONE_DEVICE MEMORY_DEVICE_FS_DAX pages also * use the mapping, index, and private fields when * pmem backed DAX files are mapped. */ }; ZONE_DEVICE pages only use pgmap. Other 4 words[16/32 bytes] don't use. So the second/third word will be used as 'struct list_head ' which list in buddy. The fourth word(that is normal struct page::index) store pgoff which the page-offset in the dax device. And the fifth word (that is normal struct page::private) store order of buddy. page_type will be used to store buddy flags. Signed-off-by: Jianpeng Ma Signed-off-by: Qiaowei Ren --- drivers/md/bcache/nvm-pages.c | 82 +++++++++++++++++++++++++++++++++-- drivers/md/bcache/nvm-pages.h | 3 ++ 2 files changed, 82 insertions(+), 3 deletions(-) diff --git a/drivers/md/bcache/nvm-pages.c b/drivers/md/bcache/nvm-pages.c index 29ee4dbc504d..ea36994b5b00 100644 --- a/drivers/md/bcache/nvm-pages.c +++ b/drivers/md/bcache/nvm-pages.c @@ -74,8 +74,10 @@ static void release_nvm_namespaces(struct bch_nvm_set *nvm_set) { int i; - for (i = 0; i < nvm_set->total_namespaces_nr; i++) + for (i = 0; i < nvm_set->total_namespaces_nr; i++) { + kfree(nvm_set->nss[i]->pages_bitmap); kfree(nvm_set->nss[i]); + } kfree(nvm_set->nss); } @@ -92,6 +94,17 @@ static void *nvm_pgoff_to_vaddr(struct bch_nvm_namespace *ns, pgoff_t pgoff) return ns->kaddr + ns->pages_offset + (pgoff << PAGE_SHIFT); } +static struct page *nvm_vaddr_to_page(struct bch_nvm_namespace *ns, void *addr) +{ + return virt_to_page(addr); +} + +static inline void remove_owner_space(struct bch_nvm_namespace *ns, + pgoff_t pgoff, u32 nr) +{ + bitmap_set(ns->pages_bitmap, pgoff, nr); +} + static int init_owner_info(struct bch_nvm_namespace *ns) { struct owner_list_head *owner_list_head; @@ -146,6 +159,8 @@ static int init_owner_info(struct bch_nvm_namespace *ns) extent->kaddr = nvm_pgoff_to_vaddr(extents->ns, rec->pgoff); extent->nr = rec->nr; list_add_tail(&extent->list, &extents->extent_head); + /*remove already alloced space*/ + remove_owner_space(extents->ns, rec->pgoff, rec->nr); extents->ns->free -= rec->nr; } @@ -166,6 +181,54 @@ static int init_owner_info(struct bch_nvm_namespace *ns) return 0; } +static void init_nvm_free_space(struct bch_nvm_namespace *ns) +{ + unsigned int start, end, i; + struct page *page; + unsigned int pages; + pgoff_t pgoff_start; + + bitmap_for_each_clear_region(ns->pages_bitmap, start, end, 0, ns->pages_total) { + pgoff_start = start; + pages = end - start; + + while (pages) { + for (i = MAX_ORDER - 1; i >= 0 ; i--) { + if ((start % (1 << i) == 0) && (pages >= (1 << i))) + break; + } + + page = nvm_vaddr_to_page(ns, nvm_pgoff_to_vaddr(ns, pgoff_start)); + page->index = pgoff_start; + set_page_private(page, i); + __SetPageBuddy(page); + list_add((struct list_head *)&page->zone_device_data, &ns->free_area[i]); + + pgoff_start += 1 << i; + pages -= 1 << i; + } + } + + bitmap_for_each_set_region(ns->pages_bitmap, start, end, 0, ns->pages_total) { + pages = end - start; + pgoff_start = start; + + while (pages) { + for (i = MAX_ORDER - 1; i >= 0 ; i--) { + if ((start % (1 << i) == 0) && (pages >= (1 << i))) + break; + } + + page = nvm_vaddr_to_page(ns, nvm_pgoff_to_vaddr(ns, pgoff_start)); + page->index = pgoff_start; + page->private = i; + + pgoff_start += 1 << i; + pages -= 1 << i; + } + } +} + static bool dev_dax_supported(struct block_device *bdev) { char buf[BDEVNAME_SIZE]; @@ -234,7 +297,7 @@ static bool attach_nvm_set(struct bch_nvm_namespace *ns) struct bch_nvm_namespace *bch_register_namespace(const char *dev_path) { struct bch_nvm_namespace *ns; - int err; + int i, err; pgoff_t pgoff; char buf[BDEVNAME_SIZE]; struct block_device *bdev; @@ -292,6 +355,15 @@ struct bch_nvm_namespace *bch_register_namespace(const char *dev_path) ns->bdev = bdev; ns->nvm_set = only_set; + ns->pages_bitmap = bitmap_zalloc(ns->pages_total, GFP_KERNEL); + if (!ns->pages_bitmap) { + err = -ENOMEM; + goto free_ns; + } + + for (i = 0; i < MAX_ORDER; i++) + INIT_LIST_HEAD(&ns->free_area[i]); + mutex_init(&ns->lock); if (ns->sb->this_namespace_nr == 0) { @@ -299,12 +371,16 @@ struct bch_nvm_namespace *bch_register_namespace(const char *dev_path) err = init_owner_info(ns); if (err < 0) { pr_info("init_owner_info met error %d\n", err); - goto free_ns; + goto free_bitmap; } + /* init buddy allocator */ + init_nvm_free_space(ns); } return ns; +free_bitmap: + kfree(ns->pages_bitmap); free_ns: kfree(ns); bdput: diff --git a/drivers/md/bcache/nvm-pages.h b/drivers/md/bcache/nvm-pages.h index 8850d00a4612..15aa0f15760f 100644 --- a/drivers/md/bcache/nvm-pages.h +++ b/drivers/md/bcache/nvm-pages.h @@ -44,6 +44,9 @@ struct bch_nvm_namespace { u64 pages_total; pfn_t start_pfn; + unsigned long *pages_bitmap; + struct list_head free_area[MAX_ORDER]; + struct dax_device *dax_dev; struct block_device *bdev; struct nvm_pages_sb *sb; -- 2.17.1