From nobody Wed Nov 27 23:23:51 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) (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 2C46C1E00A3; Mon, 7 Oct 2024 23:17:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728343048; cv=none; b=TQprebOS3xLkrg8obWMBsDmcXeMYhCdUlGZ2O75Uxi9aW8GHP7LemlCyzd7YkGAL9F04aWl52sAshVirfOJtPboU/GZt/JTuRGmhAPln1/CyXi9rwmkcDmy74Vk2AQKH5meuHMTrf3gIn39LCKAQaN6a7JyJbE7PDeQX2OMeTfo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728343048; c=relaxed/simple; bh=4YHNYXXXwFr7ip6zqBJyoPXFRkgEec/Vj1FG2ZKMCNY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=hCXSuEUWI6BHrXCIeb/xwgmBWJ2fpOBAh/V7iZD9SQ84e/Zpg71CE6r/C1zKZHvFQ0sQMp6HSa38ID790TDM7/oKicjxqvXnu1itCNx1efLuB3MR6moj+3p/JSoq7skNZoZO9KpaPpM377N0urigiep7V+SKLy4CKVCtPJUx6sY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=XjwIdZdd; arc=none smtp.client-ip=192.198.163.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="XjwIdZdd" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1728343046; x=1759879046; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=4YHNYXXXwFr7ip6zqBJyoPXFRkgEec/Vj1FG2ZKMCNY=; b=XjwIdZddXJP0h3h5bkumn1PD0ByS7P9xx9IOakMPg9pXvaBfPi99Y7WR kcO+1/gzEHBvZRrcZvJ60q459eVnRDFOvFOvMRXxxNrY6LV3fYa8Z/4N4 jl117UWhYUDe2vQbLv1kC5OCK0ISxQkA5adMsdCG63kgxAwrGqf5LBtpE CXhhgZVZwDMHjr+ETtoAUKAvr7nFMzLP5XfbpkX7KrAaHgIlRBNbRb7r4 HusM1pl+7ZUiexcl0xhZdYRn1BqfJvZoQx9BEIbHPzVEBnxPIAFbA+1cF AYZvwvB4o5TBhPlzde0keQzByrOBw9rH9tuo5EgldOUNpvsmJn32ZVt6W A==; X-CSE-ConnectionGUID: Y3fGGwQZQpaTkDSxEEs96g== X-CSE-MsgGUID: EdkyqTNRQCCqtvF1PAl4xw== X-IronPort-AV: E=McAfee;i="6700,10204,11218"; a="26972689" X-IronPort-AV: E=Sophos;i="6.11,185,1725346800"; d="scan'208";a="26972689" Received: from fmviesa010.fm.intel.com ([10.60.135.150]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Oct 2024 16:17:25 -0700 X-CSE-ConnectionGUID: 6eQ0nFhuRDyZrtBGYPXYCQ== X-CSE-MsgGUID: mg8uU/1pRnGw2+d5inZbTw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,185,1725346800"; d="scan'208";a="76001819" Received: from ldmartin-desk2.corp.intel.com (HELO localhost) ([10.125.110.112]) by fmviesa010-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Oct 2024 16:17:22 -0700 From: ira.weiny@intel.com Date: Mon, 07 Oct 2024 18:16:30 -0500 Subject: [PATCH v4 24/28] dax/region: Create resources on sparse DAX regions Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241007-dcd-type2-upstream-v4-24-c261ee6eeded@intel.com> References: <20241007-dcd-type2-upstream-v4-0-c261ee6eeded@intel.com> In-Reply-To: <20241007-dcd-type2-upstream-v4-0-c261ee6eeded@intel.com> To: Dave Jiang , Fan Ni , Jonathan Cameron , Navneet Singh , Jonathan Corbet , Andrew Morton Cc: Dan Williams , Davidlohr Bueso , Alison Schofield , Vishal Verma , Ira Weiny , linux-btrfs@vger.kernel.org, linux-cxl@vger.kernel.org, linux-doc@vger.kernel.org, nvdimm@lists.linux.dev, linux-kernel@vger.kernel.org X-Mailer: b4 0.15-dev-37811 X-Developer-Signature: v=1; a=ed25519-sha256; t=1728342968; l=26840; i=ira.weiny@intel.com; s=20221211; h=from:subject:message-id; bh=7SkA2W4o17/Nwyqdc4QaFpp/VXVG4QO9u5sAr5tlu0o=; b=PKCxThEk6swk660szAwhhP5NP7t6pLesDhReG0pgaoiCzGBJg9UvTFLmt6aku8w3b0/lAK3BI m5IroFr6n7SD3a1/EfCJGAK0Lpaoki5clGNmM9UpVO8g41teeocmLk3 X-Developer-Key: i=ira.weiny@intel.com; a=ed25519; pk=noldbkG+Wp1qXRrrkfY1QJpDf7QsOEthbOT7vm0PqsE= From: Navneet Singh DAX regions which map dynamic capacity partitions require that memory be allowed to come and go. Recall sparse regions were created for this purpose. Now that extents can be realized within DAX regions the DAX region driver can start tracking sub-resource information. The tight relationship between DAX region operations and extent operations require memory changes to be controlled synchronously with the user of the region. Synchronize through the dax_region_rwsem and by having the region driver drive both the region device as well as the extent sub-devices. Recall requests to remove extents can happen at any time and that a host is not obligated to release the memory until it is not being used. If an extent is not used allow a release response. The DAX layer has no need for the details of the CXL memory extent devices. Expose extents to the DAX layer as device children of the DAX region device. A single callback from the driver aids the DAX layer to determine if the child device is an extent. The DAX layer also registers a devres function to automatically clean up when the device is removed from the region. There is a race between extents being surfaced and the dax_cxl driver being loaded. The driver must therefore scan for any existing extents while still under the device lock. Respond to extent notifications. Manage the DAX region resource tree based on the extents lifetime. Return the status of remove notifications to lower layers such that it can manage the hardware appropriately. Signed-off-by: Navneet Singh Co-developed-by: Ira Weiny Signed-off-by: Ira Weiny --- Changes: [djiang: clarify the use of dax_region_{add,rm}_resource() and dax_avail_size()] [djiang: use guard(device)() and clean up notify function] [Jonathan: fix briefly] [Jonathan: clarify the comment when dax_resource must be released] [Jonathan: fix bug with put_device] [Jonathan: remove available_size variable] [Jonathan: Fix error message] [Jonathan: clarify error return of cxl_dax_region_notify()] [Jonathan: Fix bracket coding style] [iweiny: s/%par/%pra/] --- drivers/cxl/core/extent.c | 74 ++++++++++++-- drivers/cxl/cxl.h | 6 ++ drivers/dax/bus.c | 243 +++++++++++++++++++++++++++++++++++++++++-= ---- drivers/dax/bus.h | 3 +- drivers/dax/cxl.c | 62 +++++++++++- drivers/dax/dax-private.h | 42 ++++++++ drivers/dax/hmem/hmem.c | 2 +- drivers/dax/pmem.c | 2 +- 8 files changed, 396 insertions(+), 38 deletions(-) diff --git a/drivers/cxl/core/extent.c b/drivers/cxl/core/extent.c index a1eb6e8e4f1a..75fb73ce2185 100644 --- a/drivers/cxl/core/extent.c +++ b/drivers/cxl/core/extent.c @@ -270,20 +270,65 @@ static void calc_hpa_range(struct cxl_endpoint_decode= r *cxled, hpa_range->end =3D hpa_range->start + range_len(dpa_range) - 1; } =20 +static int cxlr_notify_extent(struct cxl_region *cxlr, enum dc_event event, + struct region_extent *region_extent) +{ + struct device *dev =3D &cxlr->cxlr_dax->dev; + struct cxl_notify_data notify_data; + struct cxl_driver *driver; + + dev_dbg(dev, "Trying notify: type %d HPA %pra\n", + event, ®ion_extent->hpa_range); + + guard(device)(dev); + + /* + * The lack of a driver indicates a notification has failed. No user + * space coordiantion was possible. + */ + if (!dev->driver) + return 0; + driver =3D to_cxl_drv(dev->driver); + if (!driver->notify) + return 0; + + notify_data =3D (struct cxl_notify_data) { + .event =3D event, + .region_extent =3D region_extent, + }; + + dev_dbg(dev, "Notify: type %d HPA %pra\n", + event, ®ion_extent->hpa_range); + return driver->notify(dev, ¬ify_data); +} + +struct rm_data { + struct cxl_region *cxlr; + struct range *range; +}; + static int cxlr_rm_extent(struct device *dev, void *data) { struct region_extent *region_extent =3D to_region_extent(dev); - struct range *region_hpa_range =3D data; + struct rm_data *rm_data =3D data; + int rc; =20 if (!region_extent) return 0; =20 /* - * Any extent which 'touches' the released range is removed. + * Any extent which 'touches' the released range is attempted to be + * removed. */ - if (range_overlaps(region_hpa_range, ®ion_extent->hpa_range)) { + if (range_overlaps(rm_data->range, ®ion_extent->hpa_range)) { + struct cxl_region *cxlr =3D rm_data->cxlr; + dev_dbg(dev, "Remove region extent HPA %pra\n", ®ion_extent->hpa_range); + rc =3D cxlr_notify_extent(cxlr, DCD_RELEASE_CAPACITY, region_extent); + if (rc =3D=3D -EBUSY) + return 0; + /* Extent not in use or error, remove it */ region_rm_extent(region_extent); } return 0; @@ -328,8 +373,13 @@ int cxl_rm_extent(struct cxl_memdev_state *mds, struct= cxl_extent *extent) =20 calc_hpa_range(cxled, cxlr->cxlr_dax, &dpa_range, &hpa_range); =20 + struct rm_data rm_data =3D { + .cxlr =3D cxlr, + .range =3D &hpa_range, + }; + /* Remove region extents which overlap */ - return device_for_each_child(&cxlr->cxlr_dax->dev, &hpa_range, + return device_for_each_child(&cxlr->cxlr_dax->dev, &rm_data, cxlr_rm_extent); } =20 @@ -354,8 +404,20 @@ static int cxlr_add_extent(struct cxl_dax_region *cxlr= _dax, return rc; } =20 - /* device model handles freeing region_extent */ - return online_region_extent(region_extent); + rc =3D online_region_extent(region_extent); + /* device model handled freeing region_extent */ + if (rc) + return rc; + + rc =3D cxlr_notify_extent(cxlr_dax->cxlr, DCD_ADD_CAPACITY, region_extent= ); + /* + * The region device was briefly live but DAX layer ensures it was not + * used + */ + if (rc) + region_rm_extent(region_extent); + + return rc; } =20 /* Callers are expected to ensure cxled has been attached to a region */ diff --git a/drivers/cxl/cxl.h b/drivers/cxl/cxl.h index b75653e9bc32..fce007af98cf 100644 --- a/drivers/cxl/cxl.h +++ b/drivers/cxl/cxl.h @@ -918,10 +918,16 @@ bool is_cxl_region(struct device *dev); =20 extern struct bus_type cxl_bus_type; =20 +struct cxl_notify_data { + enum dc_event event; + struct region_extent *region_extent; +}; + struct cxl_driver { const char *name; int (*probe)(struct device *dev); void (*remove)(struct device *dev); + int (*notify)(struct device *dev, struct cxl_notify_data *notify_data); struct device_driver drv; int id; }; diff --git a/drivers/dax/bus.c b/drivers/dax/bus.c index f0e3f8c787df..4e19d18369de 100644 --- a/drivers/dax/bus.c +++ b/drivers/dax/bus.c @@ -183,6 +183,86 @@ static bool is_sparse(struct dax_region *dax_region) return (dax_region->res.flags & IORESOURCE_DAX_SPARSE_CAP) !=3D 0; } =20 +static void __dax_release_resource(struct dax_resource *dax_resource) +{ + struct dax_region *dax_region =3D dax_resource->region; + + lockdep_assert_held_write(&dax_region_rwsem); + dev_dbg(dax_region->dev, "Extent release resource %pr\n", + dax_resource->res); + if (dax_resource->res) + __release_region(&dax_region->res, dax_resource->res->start, + resource_size(dax_resource->res)); + dax_resource->res =3D NULL; +} + +static void dax_release_resource(void *res) +{ + struct dax_resource *dax_resource =3D res; + + guard(rwsem_write)(&dax_region_rwsem); + __dax_release_resource(dax_resource); + kfree(dax_resource); +} + +int dax_region_add_resource(struct dax_region *dax_region, + struct device *device, + resource_size_t start, resource_size_t length) +{ + struct resource *new_resource; + int rc; + + struct dax_resource *dax_resource __free(kfree) =3D + kzalloc(sizeof(*dax_resource), GFP_KERNEL); + if (!dax_resource) + return -ENOMEM; + + guard(rwsem_write)(&dax_region_rwsem); + + dev_dbg(dax_region->dev, "DAX region resource %pr\n", &dax_region->res); + new_resource =3D __request_region(&dax_region->res, start, length, "exten= t", 0); + if (!new_resource) { + dev_err(dax_region->dev, "Failed to add region s:%pa l:%pa\n", + &start, &length); + return -ENOSPC; + } + + dev_dbg(dax_region->dev, "add resource %pr\n", new_resource); + dax_resource->region =3D dax_region; + dax_resource->res =3D new_resource; + dev_set_drvdata(device, dax_resource); + rc =3D devm_add_action_or_reset(device, dax_release_resource, + no_free_ptr(dax_resource)); + /* On error; ensure driver data is cleared under semaphore */ + if (rc) + dev_set_drvdata(device, NULL); + return rc; +} +EXPORT_SYMBOL_GPL(dax_region_add_resource); + +int dax_region_rm_resource(struct dax_region *dax_region, + struct device *dev) +{ + struct dax_resource *dax_resource; + + guard(rwsem_write)(&dax_region_rwsem); + + dax_resource =3D dev_get_drvdata(dev); + if (!dax_resource) + return 0; + + if (dax_resource->use_cnt) + return -EBUSY; + + /* + * release the resource under dax_region_rwsem to avoid races with + * users trying to use the extent + */ + __dax_release_resource(dax_resource); + return 0; +} +EXPORT_SYMBOL_GPL(dax_region_rm_resource); + bool static_dev_dax(struct dev_dax *dev_dax) { return is_static(dev_dax->region); @@ -296,19 +376,44 @@ static ssize_t region_align_show(struct device *dev, static struct device_attribute dev_attr_region_align =3D __ATTR(align, 0400, region_align_show, NULL); =20 +#define for_each_child_resource(extent, res) \ + for (res =3D (extent)->child; res; res =3D res->sibling) + +resource_size_t +dax_avail_size(struct resource *dax_resource) +{ + resource_size_t rc; + struct resource *used_res; + + rc =3D resource_size(dax_resource); + for_each_child_resource(dax_resource, used_res) + rc -=3D resource_size(used_res); + return rc; +} +EXPORT_SYMBOL_GPL(dax_avail_size); + #define for_each_dax_region_resource(dax_region, res) \ for (res =3D (dax_region)->res.child; res; res =3D res->sibling) =20 static unsigned long long dax_region_avail_size(struct dax_region *dax_reg= ion) { - resource_size_t size =3D resource_size(&dax_region->res); + resource_size_t size; struct resource *res; =20 lockdep_assert_held(&dax_region_rwsem); =20 - if (is_sparse(dax_region)) - return 0; + if (is_sparse(dax_region)) { + /* + * Children of a sparse region represent available space not + * used space. + */ + size =3D 0; + for_each_dax_region_resource(dax_region, res) + size +=3D dax_avail_size(res); + return size; + } =20 + size =3D resource_size(&dax_region->res); for_each_dax_region_resource(dax_region, res) size -=3D resource_size(res); return size; @@ -449,15 +554,26 @@ EXPORT_SYMBOL_GPL(kill_dev_dax); static void trim_dev_dax_range(struct dev_dax *dev_dax) { int i =3D dev_dax->nr_range - 1; - struct range *range =3D &dev_dax->ranges[i].range; + struct dev_dax_range *dev_range =3D &dev_dax->ranges[i]; + struct range *range =3D &dev_range->range; struct dax_region *dax_region =3D dev_dax->region; + struct resource *res =3D &dax_region->res; =20 lockdep_assert_held_write(&dax_region_rwsem); dev_dbg(&dev_dax->dev, "delete range[%d]: %#llx:%#llx\n", i, (unsigned long long)range->start, (unsigned long long)range->end); =20 - __release_region(&dax_region->res, range->start, range_len(range)); + if (dev_range->dax_resource) { + res =3D dev_range->dax_resource->res; + dev_dbg(&dev_dax->dev, "Trim sparse extent %pr\n", res); + } + + __release_region(res, range->start, range_len(range)); + + if (dev_range->dax_resource) + dev_range->dax_resource->use_cnt--; + if (--dev_dax->nr_range =3D=3D 0) { kfree(dev_dax->ranges); dev_dax->ranges =3D NULL; @@ -640,7 +756,7 @@ static void dax_region_unregister(void *region) =20 struct dax_region *alloc_dax_region(struct device *parent, int region_id, struct range *range, int target_node, unsigned int align, - unsigned long flags) + unsigned long flags, struct dax_sparse_ops *sparse_ops) { struct dax_region *dax_region; =20 @@ -658,12 +774,16 @@ struct dax_region *alloc_dax_region(struct device *pa= rent, int region_id, || !IS_ALIGNED(range_len(range), align)) return NULL; =20 + if (!sparse_ops && (flags & IORESOURCE_DAX_SPARSE_CAP)) + return NULL; + dax_region =3D kzalloc(sizeof(*dax_region), GFP_KERNEL); if (!dax_region) return NULL; =20 dev_set_drvdata(parent, dax_region); kref_init(&dax_region->kref); + dax_region->sparse_ops =3D sparse_ops; dax_region->id =3D region_id; dax_region->align =3D align; dax_region->dev =3D parent; @@ -845,7 +965,8 @@ static int devm_register_dax_mapping(struct dev_dax *de= v_dax, int range_id) } =20 static int alloc_dev_dax_range(struct resource *parent, struct dev_dax *de= v_dax, - u64 start, resource_size_t size) + u64 start, resource_size_t size, + struct dax_resource *dax_resource) { struct device *dev =3D &dev_dax->dev; struct dev_dax_range *ranges; @@ -884,6 +1005,7 @@ static int alloc_dev_dax_range(struct resource *parent= , struct dev_dax *dev_dax, .start =3D alloc->start, .end =3D alloc->end, }, + .dax_resource =3D dax_resource, }; =20 dev_dbg(dev, "alloc range[%d]: %pa:%pa\n", dev_dax->nr_range - 1, @@ -966,7 +1088,8 @@ static int dev_dax_shrink(struct dev_dax *dev_dax, res= ource_size_t size) int i; =20 for (i =3D dev_dax->nr_range - 1; i >=3D 0; i--) { - struct range *range =3D &dev_dax->ranges[i].range; + struct dev_dax_range *dev_range =3D &dev_dax->ranges[i]; + struct range *range =3D &dev_range->range; struct dax_mapping *mapping =3D dev_dax->ranges[i].mapping; struct resource *adjust =3D NULL, *res; resource_size_t shrink; @@ -982,12 +1105,21 @@ static int dev_dax_shrink(struct dev_dax *dev_dax, r= esource_size_t size) continue; } =20 - for_each_dax_region_resource(dax_region, res) - if (strcmp(res->name, dev_name(dev)) =3D=3D 0 - && res->start =3D=3D range->start) { - adjust =3D res; - break; - } + if (dev_range->dax_resource) { + for_each_child_resource(dev_range->dax_resource->res, res) + if (strcmp(res->name, dev_name(dev)) =3D=3D 0 + && res->start =3D=3D range->start) { + adjust =3D res; + break; + } + } else { + for_each_dax_region_resource(dax_region, res) + if (strcmp(res->name, dev_name(dev)) =3D=3D 0 + && res->start =3D=3D range->start) { + adjust =3D res; + break; + } + } =20 if (dev_WARN_ONCE(dev, !adjust || i !=3D dev_dax->nr_range - 1, "failed to find matching resource\n")) @@ -1025,19 +1157,21 @@ static bool adjust_ok(struct dev_dax *dev_dax, stru= ct resource *res) } =20 /** - * dev_dax_resize_static - Expand the device into the unused portion of the - * region. This may involve adjusting the end of an existing resource, or - * allocating a new resource. + * __dev_dax_resize - Expand the device into the unused portion of the reg= ion. + * This may involve adjusting the end of an existing resource, or allocati= ng a + * new resource. * * @parent: parent resource to allocate this range in * @dev_dax: DAX device to be expanded * @to_alloc: amount of space to alloc; must be <=3D space available in @p= arent + * @dax_resource: if sparse; the parent resource * * Return the amount of space allocated or -ERRNO on failure */ -static ssize_t dev_dax_resize_static(struct resource *parent, - struct dev_dax *dev_dax, - resource_size_t to_alloc) +static ssize_t __dev_dax_resize(struct resource *parent, + struct dev_dax *dev_dax, + resource_size_t to_alloc, + struct dax_resource *dax_resource) { struct resource *res, *first; int rc; @@ -1045,7 +1179,8 @@ static ssize_t dev_dax_resize_static(struct resource = *parent, first =3D parent->child; if (!first) { rc =3D alloc_dev_dax_range(parent, dev_dax, - parent->start, to_alloc); + parent->start, to_alloc, + dax_resource); if (rc) return rc; return to_alloc; @@ -1059,7 +1194,8 @@ static ssize_t dev_dax_resize_static(struct resource = *parent, if (res =3D=3D first && res->start > parent->start) { alloc =3D min(res->start - parent->start, to_alloc); rc =3D alloc_dev_dax_range(parent, dev_dax, - parent->start, alloc); + parent->start, alloc, + dax_resource); if (rc) return rc; return alloc; @@ -1083,7 +1219,8 @@ static ssize_t dev_dax_resize_static(struct resource = *parent, return rc; return alloc; } - rc =3D alloc_dev_dax_range(parent, dev_dax, res->end + 1, alloc); + rc =3D alloc_dev_dax_range(parent, dev_dax, res->end + 1, alloc, + dax_resource); if (rc) return rc; return alloc; @@ -1094,6 +1231,51 @@ static ssize_t dev_dax_resize_static(struct resource= *parent, return 0; } =20 +static ssize_t dev_dax_resize_static(struct dax_region *dax_region, + struct dev_dax *dev_dax, + resource_size_t to_alloc) +{ + return __dev_dax_resize(&dax_region->res, dev_dax, to_alloc, NULL); +} + +static int find_free_extent(struct device *dev, void *data) +{ + struct dax_region *dax_region =3D data; + struct dax_resource *dax_resource; + + if (!dax_region->sparse_ops->is_extent(dev)) + return 0; + + dax_resource =3D dev_get_drvdata(dev); + if (!dax_resource || !dax_avail_size(dax_resource->res)) + return 0; + return 1; +} + +static ssize_t dev_dax_resize_sparse(struct dax_region *dax_region, + struct dev_dax *dev_dax, + resource_size_t to_alloc) +{ + struct dax_resource *dax_resource; + ssize_t alloc; + + struct device *extent_dev __free(put_device) =3D + device_find_child(dax_region->dev, dax_region, + find_free_extent); + if (!extent_dev) + return 0; + + dax_resource =3D dev_get_drvdata(extent_dev); + if (!dax_resource) + return 0; + + to_alloc =3D min(dax_avail_size(dax_resource->res), to_alloc); + alloc =3D __dev_dax_resize(dax_resource->res, dev_dax, to_alloc, dax_reso= urce); + if (alloc > 0) + dax_resource->use_cnt++; + return alloc; +} + static ssize_t dev_dax_resize(struct dax_region *dax_region, struct dev_dax *dev_dax, resource_size_t size) { @@ -1117,7 +1299,10 @@ static ssize_t dev_dax_resize(struct dax_region *dax= _region, return -ENXIO; =20 retry: - alloc =3D dev_dax_resize_static(&dax_region->res, dev_dax, to_alloc); + if (is_sparse(dax_region)) + alloc =3D dev_dax_resize_sparse(dax_region, dev_dax, to_alloc); + else + alloc =3D dev_dax_resize_static(dax_region, dev_dax, to_alloc); if (alloc <=3D 0) return alloc; to_alloc -=3D alloc; @@ -1226,7 +1411,7 @@ static ssize_t mapping_store(struct device *dev, stru= ct device_attribute *attr, to_alloc =3D range_len(&r); if (alloc_is_aligned(dev_dax, to_alloc)) rc =3D alloc_dev_dax_range(&dax_region->res, dev_dax, r.start, - to_alloc); + to_alloc, NULL); up_write(&dax_dev_rwsem); up_write(&dax_region_rwsem); =20 @@ -1494,8 +1679,14 @@ static struct dev_dax *__devm_create_dev_dax(struct = dev_dax_data *data) device_initialize(dev); dev_set_name(dev, "dax%d.%d", dax_region->id, dev_dax->id); =20 + if (is_sparse(dax_region) && data->size) { + dev_err(parent, "Sparse DAX region devices must be created initially wit= h 0 size"); + rc =3D -EINVAL; + goto err_id; + } + rc =3D alloc_dev_dax_range(&dax_region->res, dev_dax, dax_region->res.sta= rt, - data->size); + data->size, NULL); if (rc) goto err_range; =20 diff --git a/drivers/dax/bus.h b/drivers/dax/bus.h index 783bfeef42cc..ae5029ea6047 100644 --- a/drivers/dax/bus.h +++ b/drivers/dax/bus.h @@ -9,6 +9,7 @@ struct dev_dax; struct resource; struct dax_device; struct dax_region; +struct dax_sparse_ops; =20 /* dax bus specific ioresource flags */ #define IORESOURCE_DAX_STATIC BIT(0) @@ -17,7 +18,7 @@ struct dax_region; =20 struct dax_region *alloc_dax_region(struct device *parent, int region_id, struct range *range, int target_node, unsigned int align, - unsigned long flags); + unsigned long flags, struct dax_sparse_ops *sparse_ops); =20 struct dev_dax_data { struct dax_region *dax_region; diff --git a/drivers/dax/cxl.c b/drivers/dax/cxl.c index 367e86b1c22a..df979ea2cb59 100644 --- a/drivers/dax/cxl.c +++ b/drivers/dax/cxl.c @@ -5,6 +5,58 @@ =20 #include "../cxl/cxl.h" #include "bus.h" +#include "dax-private.h" + +static int __cxl_dax_add_resource(struct dax_region *dax_region, + struct region_extent *region_extent) +{ + resource_size_t start, length; + struct device *dev; + + dev =3D ®ion_extent->dev; + start =3D dax_region->res.start + region_extent->hpa_range.start; + length =3D range_len(®ion_extent->hpa_range); + return dax_region_add_resource(dax_region, dev, start, length); +} + +static int cxl_dax_add_resource(struct device *dev, void *data) +{ + struct dax_region *dax_region =3D data; + struct region_extent *region_extent; + + region_extent =3D to_region_extent(dev); + if (!region_extent) + return 0; + + dev_dbg(dax_region->dev, "Adding resource HPA %pra\n", + ®ion_extent->hpa_range); + + return __cxl_dax_add_resource(dax_region, region_extent); +} + +static int cxl_dax_region_notify(struct device *dev, + struct cxl_notify_data *notify_data) +{ + struct cxl_dax_region *cxlr_dax =3D to_cxl_dax_region(dev); + struct dax_region *dax_region =3D dev_get_drvdata(dev); + struct region_extent *region_extent =3D notify_data->region_extent; + + switch (notify_data->event) { + case DCD_ADD_CAPACITY: + return __cxl_dax_add_resource(dax_region, region_extent); + case DCD_RELEASE_CAPACITY: + return dax_region_rm_resource(dax_region, ®ion_extent->dev); + case DCD_FORCED_CAPACITY_RELEASE: + default: + dev_err(&cxlr_dax->dev, "Unknown DC event %d\n", + notify_data->event); + return -ENXIO; + } +} + +struct dax_sparse_ops sparse_ops =3D { + .is_extent =3D is_region_extent, +}; =20 static int cxl_dax_region_probe(struct device *dev) { @@ -24,15 +76,18 @@ static int cxl_dax_region_probe(struct device *dev) flags |=3D IORESOURCE_DAX_SPARSE_CAP; =20 dax_region =3D alloc_dax_region(dev, cxlr->id, &cxlr_dax->hpa_range, nid, - PMD_SIZE, flags); + PMD_SIZE, flags, &sparse_ops); if (!dax_region) return -ENOMEM; =20 - if (cxlr->mode =3D=3D CXL_REGION_DC) + if (cxlr->mode =3D=3D CXL_REGION_DC) { + device_for_each_child(&cxlr_dax->dev, dax_region, + cxl_dax_add_resource); /* Add empty seed dax device */ dev_size =3D 0; - else + } else { dev_size =3D range_len(&cxlr_dax->hpa_range); + } =20 data =3D (struct dev_dax_data) { .dax_region =3D dax_region, @@ -47,6 +102,7 @@ static int cxl_dax_region_probe(struct device *dev) static struct cxl_driver cxl_dax_region_driver =3D { .name =3D "cxl_dax_region", .probe =3D cxl_dax_region_probe, + .notify =3D cxl_dax_region_notify, .id =3D CXL_DEVICE_DAX_REGION, .drv =3D { .suppress_bind_attrs =3D true, diff --git a/drivers/dax/dax-private.h b/drivers/dax/dax-private.h index ccde98c3d4e2..e3866115243e 100644 --- a/drivers/dax/dax-private.h +++ b/drivers/dax/dax-private.h @@ -16,6 +16,14 @@ struct inode *dax_inode(struct dax_device *dax_dev); int dax_bus_init(void); void dax_bus_exit(void); =20 +/** + * struct dax_sparse_ops - Operations for sparse regions + * @is_extent: return if the device is an extent + */ +struct dax_sparse_ops { + bool (*is_extent)(struct device *dev); +}; + /** * struct dax_region - mapping infrastructure for dax devices * @id: kernel-wide unique region for a memory range @@ -27,6 +35,7 @@ void dax_bus_exit(void); * @res: resource tree to track instance allocations * @seed: allow userspace to find the first unbound seed device * @youngest: allow userspace to find the most recently created device + * @sparse_ops: operations required for sparse regions */ struct dax_region { int id; @@ -38,6 +47,7 @@ struct dax_region { struct resource res; struct device *seed; struct device *youngest; + struct dax_sparse_ops *sparse_ops; }; =20 struct dax_mapping { @@ -62,6 +72,7 @@ struct dax_mapping { * @pgoff: page offset * @range: resource-span * @mapping: device to assist in interrogating the range layout + * @dax_resource: if not NULL; dax sparse resource containing this range */ struct dev_dax { struct dax_region *region; @@ -79,6 +90,7 @@ struct dev_dax { unsigned long pgoff; struct range range; struct dax_mapping *mapping; + struct dax_resource *dax_resource; } *ranges; }; =20 @@ -89,6 +101,36 @@ struct dev_dax { */ void run_dax(struct dax_device *dax_dev); =20 +/** + * struct dax_resource - For sparse regions; an active resource + * @region: dax_region this resources is in + * @res: resource + * @use_cnt: count the number of uses of this resource + * + * Changes to the dax_reigon and the dax_resources within it are protected= by + * dax_region_rwsem + * + * dax_resource's are not intended to be used outside the dax layer. + */ +struct dax_resource { + struct dax_region *region; + struct resource *res; + unsigned int use_cnt; +}; + +/* + * Similar to run_dax() dax_region_{add,rm}_resource() and dax_avail_size(= ) are + * exported but are not intended to be generic operations outside the dax + * subsystem. They are only generic between the dax layer and the dax dri= vers. + */ +int dax_region_add_resource(struct dax_region *dax_region, struct device *= dev, + resource_size_t start, resource_size_t length); +int dax_region_rm_resource(struct dax_region *dax_region, + struct device *dev); +resource_size_t dax_avail_size(struct resource *dax_resource); + +typedef int (*match_cb)(struct device *dev, resource_size_t *size_avail); + static inline struct dev_dax *to_dev_dax(struct device *dev) { return container_of(dev, struct dev_dax, dev); diff --git a/drivers/dax/hmem/hmem.c b/drivers/dax/hmem/hmem.c index 5e7c53f18491..0eea65052874 100644 --- a/drivers/dax/hmem/hmem.c +++ b/drivers/dax/hmem/hmem.c @@ -28,7 +28,7 @@ static int dax_hmem_probe(struct platform_device *pdev) =20 mri =3D dev->platform_data; dax_region =3D alloc_dax_region(dev, pdev->id, &mri->range, - mri->target_node, PMD_SIZE, flags); + mri->target_node, PMD_SIZE, flags, NULL); if (!dax_region) return -ENOMEM; =20 diff --git a/drivers/dax/pmem.c b/drivers/dax/pmem.c index c8ebf4e281f2..f927e855f240 100644 --- a/drivers/dax/pmem.c +++ b/drivers/dax/pmem.c @@ -54,7 +54,7 @@ static struct dev_dax *__dax_pmem_probe(struct device *de= v) range.start +=3D offset; dax_region =3D alloc_dax_region(dev, region_id, &range, nd_region->target_node, le32_to_cpu(pfn_sb->align), - IORESOURCE_DAX_STATIC); + IORESOURCE_DAX_STATIC, NULL); if (!dax_region) return ERR_PTR(-ENOMEM); =20 --=20 2.46.0