From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=198.175.65.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796134775; b=dErXb1v4zqt4fwZmn+iKHQaFYxG7b8fhHspfmFI7Gua0BvoEgWv/Lr2FahZGV4A8Iy75 4utYGiqo4zrGiRjfNE/GwoBzTzHdrkKuX2Wa0u/11IWKL3lluKRXNJiy983RJXITIYIW+ 9+7+GEeVSy6zmyPTXpsx/v9dge7ouCwWwpOnyHcmVqCLfpKhBWBaKdK5NDLumLyCF5MpE TgXOfb9mbKE+S9E2/EHIkQ8EVJuR+Sfj/OrOX4RcPQinbCboHGnCtuG0bbBcBQKqJQOJ5 qk71l5e3M/eKX+3I/yXC9DjIcayx2keh10obCIZtflr3nYzgFNMr5wIGPq2wRnJREoQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796134775; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=IIgfU6nXdfJh8BjZkXWxyzzPVstL0YxXqihVtJYt2iA=; b=bSSpxdwE5GiIu4TCAAXrjaWFVGbfFvblHok44/iaqBVJYZq17KlNWdzXxRME56Oy9Qky K6ZBRxOCXsR/JGiKLS8Bxr2hPVBMfpr52c7gPzq3p6x02L08bngQGbEp6G1051GZydGBL qrS7Yg6aLIhyZkUpmNwJnaS4Pmran8RzMeRsVMFjX3Uet61Wn3T+Egk4JoH1GMfEKWMPT 86j4qo2MIkYDm8hOOsy0HYG5IVbM4Q9QJ4hMiKB4sZAo/YP4/6EmoToR//9YpCVaI1zH5 fMzXeIiwg1z7UIWxB4M6LjFSJZFQcz7jsk+SBOauKdZ+bKIyW30RJENQZq75lg5t2Ow== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=UYTdX+4y; arc=none smtp.remote-ip=198.175.65.18 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1EBEF606A320; Tue, 1 Dec 2026 14:19:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 1EBEF606A320 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.a=rsa-sha256 header.s=Intel header.b=UYTdX+4y DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796134775; x=1827670775; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=IIgfU6nXdfJh8BjZkXWxyzzPVstL0YxXqihVtJYt2iA=; b=UYTdX+4ytetSJbUdUc/n1ofLk5N2PHWwUuDRnxhxoAS2nXlQu7yYfSM6 /D0aJcyukiGrAIFjsc5k3RB0CmBiB0oL2xHitevVKMbJaP3tgvfqd/j3A QCMK8GH7y0H/izqITh/wJFkkGSw9L1Q2oR1j+//3o0etvTD/BmK/7A9TI 2NH5PxrBS/HZvzrnxKKehpKTucEY2hZbYwstHLViG6G0uuywJSowV0wDT 27duZ+uR3nFklojIeAZd1QOeYQnaaxmBW8VCwbddMEr9AobR3xtnDnImE vw31lBf6tIPpPaE5YgqmQHLfEB8lJy1ujUpb088UGOTkCGvXqvKqUUzCq g==; X-CSE-ConnectionGUID: FTDVi4xQRy2MgpxCpIo2iQ== X-CSE-MsgGUID: 5mHIGvTkRSa9Vlz38myfZQ== X-IronPort-AV: E=McAfee;i="6800,10657,11983"; a="5019357" X-IronPort-AV: E=Sophos;i="6.27,244,1787036400"; d="scan'208";a="5019357" Received: from fmviesa005.fm.intel.com ([10.60.135.145]) by orvoesa110.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Dec 2026 06:19:28 -0800 X-CSE-ConnectionGUID: fIfMGrgCR5aNVAsEnxa0rw== X-CSE-MsgGUID: +/UcXratQP29EpIMjPKolw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,244,1787036400"; d="scan'208";a="13380796" Received: from jf5300-15a101203tc.jf.intel.com (HELO meta-crater2c.jf.intel.com) ([10.54.113.49]) by fmviesa005.fm.intel.com with ESMTP; 01 Dec 2026 06:19:26 -0800 From: Daniel Byrne To: Shivank Garg , Ravi Jonnalagadda Cc: Andrew Morton , David Hildenbrand , Zi Yan , Matthew Brost , Joshua Hahn , Rakie Kim , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Karim Manaouil , Frank van der Linden , Teja Vojjala , Pravin Tamkhane , Kinsey Ho , Wei Xu , Matthew Wilcox , Davidlohr Bueso , Vinod Koul , Bharata B Rao , SeongJae Park , David Rientjes , Xuezheng Chu , Yiannis Nikolakopoulos , Dave Hansen , Johannes Weiner , John Hubbard , Peter Xu , Rik van Riel , Shakeel Butt , Tejun Heo , Fan Ni , Jonathan Cameron , "Aneesh Kumar K.V" , Nathan Lynch , Frank Li , Dan Williams , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Mike Day , dmaengine@vger.kernel.org, Vinicius Costa Gomes , Frank Li , Dave Jiang , Daniel Byrne Subject: [RFC PATCH 10/10] drivers/migrate_offload: make dcbm correct on shared, out-of-order engines Date: Tue, 1 Dec 2026 06:12:59 -0800 Message-ID: <20261201141259.2124188-11-daniel1.byrne@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261201141259.2124188-1-daniel1.byrne@intel.com> References: <20261201141259.2124188-1-daniel1.byrne@intel.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit dcbm set a callback on the last descriptor submitted to each channel, requested its channels on every batch, mapped scatterlists whose segments had to merge identically on both sides, timed out after ten seconds and terminated the channel, and submitted more descriptors than a work queue holds. Each of these is wrong on Intel DSA. Completion. A work queue served by a group with several engines completes descriptors out of order, so the last descriptor's callback does not mean the slice has landed, and the batch could be marked FOLIO_CONTENT_COPIED early. Give every descriptor its own completion, counted down on a per-slice pending counter that starts with a submission reference. Use the result-aware callback and fail the slice on any error result, so a rejected or aborted descriptor sends its folio to the CPU copy instead of being reported as copied. Channels. Acquire the channels once when offloading is enabled, group them by DMA device with the device's NUMA node, and try groups on the destination node first: a cross-socket copy pays remote link bandwidth on every written line. Concurrent batches share the pool through per-channel trylocks; when every channel is busy the batch is refused with -EBUSY (counted in batches_refused) and the move phase copies on the CPU instead of queueing. Mapping. A folio is physically contiguous and so one DMA segment. Map source and destination of each copy with dma_map_page_attrs() and pair them by folio, which removes the scatterlist allocations and the IOMMU merge asymmetry that forced CPU fallbacks. On a provider that advertises DMA_MEMCPY_SG, hand the slice over as scatter-gather transactions of up to sg_elems (default 32) folios, which DSA executes as one hardware batch descriptor; the scatterlists only carry the per-folio DMA addresses. Waiting. A timeout cannot be acted upon safely: the in-flight descriptors reference the folios and the on-stack work, and dmaengine has no per-descriptor abort. Wait for the completion; a stuck engine is the device driver's to recover, and it completes the descriptors with an error. Never terminate a channel: terminate_all is channel-wide and kills other batches' descriptors. Throttling. A work queue owns a fixed descriptor pool and the non-blocking prep returns NULL when it is exhausted. Submit at most max_inflight (default 32) descriptors per channel and wait for completions before queueing more. Request DMA_PREP_CACHE_CONTROL on the copies, so the first access after remapping hits the LLC instead of DRAM; cache_ctrl turns it off. Reported-by: Karim Manaouil Link: https://lore.kernel.org/linux-mm/20260619160725.lfcxrbj5go67qy6u@wrangler/ Suggested-by: Vinod Koul Signed-off-by: Daniel Byrne --- The idxd side of DMA_MEMCPY_SG is not in this series, so on DSA this uses one descriptor per folio here. drivers/migrate_offload/dcbm/dcbm.c | 630 +++++++++++++++++++++------- 1 file changed, 467 insertions(+), 163 deletions(-) diff --git a/drivers/migrate_offload/dcbm/dcbm.c b/drivers/migrate_offload/dcbm/dcbm.c index 3df9232586ff..03785a822161 100644 --- a/drivers/migrate_offload/dcbm/dcbm.c +++ b/drivers/migrate_offload/dcbm/dcbm.c @@ -9,127 +9,290 @@ * Copyright (C) 2024-26 Advanced Micro Devices, Inc. */ +#include #include #include #include #include #include +#include +#include +#include +#include #define MAX_DMA_CHANNELS 16 +/* + * Folios per scatter-gather transaction. A provider that supports + * DMA_MEMCPY_SG turns one transaction into one hardware batch, so + * this bounds the batch element count it must accept. + */ +#define DCBM_SG_ELEMS_DEFAULT 32 +/* + * Descriptors in flight per channel. A DSA work queue owns a fixed + * descriptor pool (its queue depth, 64 in a typical configuration) + * and prep returns NULL once it is exhausted, so a slice submits at + * most this many descriptors before waiting for completions. + */ +#define DCBM_MAX_INFLIGHT_DEFAULT 32 static atomic_long_t folios_migrated; static atomic_long_t folios_failures; +static atomic_long_t batches_refused; static bool offloading_enabled; static unsigned int nr_dma_channels = 1; +static unsigned int sg_elems = DCBM_SG_ELEMS_DEFAULT; +static unsigned int max_inflight = DCBM_MAX_INFLIGHT_DEFAULT; +static bool cache_ctrl = true; static DEFINE_MUTEX(dcbm_mutex); +/* + * Channels are acquired once when offloading is enabled and shared by + * concurrent batches through per-channel trylocks. When every channel + * is busy the batch is refused and the move phase copies on the CPU, + * instead of queueing behind other batches. + */ +static struct dcbm_chan { + struct dma_chan *chan; + struct mutex lock; +} channels[MAX_DMA_CHANNELS]; +static unsigned int nr_channels; + +/* + * Channels grouped by DMA device. One batch maps its folios against a + * single device and uses only that group's channels. Concurrent + * batches rotate over the groups so every device contributes. + */ +static struct dcbm_group { + struct device *dev; + int node; + unsigned int first; + unsigned int nr; +} groups[MAX_DMA_CHANNELS]; +static unsigned int nr_groups; +static atomic_t group_cursor; + +struct dcbm_copy { + dma_addr_t src; + dma_addr_t dst; + size_t len; +}; + struct dma_work { struct dma_chan *chan; - struct completion done; + struct device *dev; + wait_queue_head_t waitq; atomic_t pending; - struct sg_table *src_sgt; - struct sg_table *dst_sgt; - bool mapped; + atomic_t error; + struct dcbm_copy *copies; + unsigned int nr_copies; + bool submitted; }; -static void dma_completion_callback(void *data) +/* + * Every descriptor carries its own completion. Engines such as Intel + * DSA complete the descriptors of one channel out of order when the + * work queue is served by several engines, so a callback on the last + * submitted descriptor does not mean the earlier ones have landed. + * The pending count starts at one for the submitter, so the work + * cannot complete before every descriptor has been submitted. + * + * A descriptor the hardware rejected or aborted completes with an + * error result; it must fail the batch, or a folio that was never + * written would be reported as copied. + */ +static void dma_completion_callback(void *data, + const struct dmaengine_result *result) { struct dma_work *work = data; - if (atomic_dec_and_test(&work->pending)) - complete(&work->done); + if (!result || result->result != DMA_TRANS_NOERROR) + atomic_set(&work->error, -EIO); + + atomic_dec(&work->pending); + wake_up(&work->waitq); } -static int setup_sg_tables(struct dma_work *work, struct list_head **src_pos, - struct list_head **dst_pos, int nr) +static void dma_work_init(struct dma_work *work) { - struct scatterlist *sg_src, *sg_dst; - struct device *dev; - int i, ret; + init_waitqueue_head(&work->waitq); + /* Submission reference, dropped by dma_work_done_submitting(). */ + atomic_set(&work->pending, 1); + atomic_set(&work->error, 0); +} - work->src_sgt = kmalloc_obj(*work->src_sgt, GFP_KERNEL); - if (!work->src_sgt) - return -ENOMEM; - work->dst_sgt = kmalloc_obj(*work->dst_sgt, GFP_KERNEL); - if (!work->dst_sgt) { - ret = -ENOMEM; - goto err_free_src; +static void dma_work_done_submitting(struct dma_work *work) +{ + atomic_dec(&work->pending); + wake_up(&work->waitq); +} + +/* + * Throttle submission to max_inflight descriptors per channel, so a + * slice never outruns the channel's descriptor pool. + */ +static void dma_work_throttle(struct dma_work *work) +{ + unsigned int limit = READ_ONCE(max_inflight) + 1; + + wait_event(work->waitq, atomic_read(&work->pending) < limit); +} + +static unsigned long dcbm_claim_group(struct dcbm_group *grp, + unsigned int want) +{ + unsigned long mask = 0; + unsigned int i, got = 0; + + for (i = 0; i < grp->nr && got < want; i++) { + unsigned int idx = grp->first + i; + + if (mutex_trylock(&channels[idx].lock)) { + mask |= BIT(idx); + got++; + } } + return mask; +} - ret = sg_alloc_table(work->src_sgt, nr, GFP_KERNEL); - if (ret) - goto err_free_dst; - ret = sg_alloc_table(work->dst_sgt, nr, GFP_KERNEL); - if (ret) - goto err_free_src_table; +/* + * One selection pass: collect the groups whose node does (or does + * not) match @nid and try them starting from a rotating offset within + * that selection, so rotation is fair however the eligible groups are + * laid out in the global array. + */ +static unsigned long dcbm_claim_pass(int nid, bool match_node, + unsigned int want, + struct dcbm_group **grpp) +{ + unsigned int sel[MAX_DMA_CHANNELS]; + unsigned int n = 0, g, i; + + for (g = 0; g < nr_groups; g++) + if ((groups[g].node == nid) == match_node) + sel[n++] = g; + if (!n) + return 0; + + g = (unsigned int)atomic_inc_return(&group_cursor) % n; + for (i = 0; i < n; i++) { + struct dcbm_group *grp = &groups[sel[(g + i) % n]]; + unsigned long mask = dcbm_claim_group(grp, want); + + if (mask) { + *grpp = grp; + return mask; + } + } + return 0; +} + +/* + * Claim up to @want channels from one device group. Groups on the + * destination node are tried first: a cross-socket copy pays + * remote-link bandwidth on every written line plus a remote + * completion interrupt, so locality beats spreading. The first group + * with any free channel wins. If every channel of every group is busy + * the batch is refused. + */ +static unsigned long dcbm_claim_channels(unsigned int want, int nid, + struct dcbm_group **grpp) +{ + unsigned long mask = dcbm_claim_pass(nid, true, want, grpp); + + if (!mask) + mask = dcbm_claim_pass(nid, false, want, grpp); + return mask; +} + +static void dcbm_release_channels(unsigned long mask) +{ + unsigned int i; + + for_each_set_bit(i, &mask, nr_channels) + mutex_unlock(&channels[i].lock); +} + +/* + * Map each folio of the slice on its own. A folio is physically + * contiguous, so it is one DMA segment, and the source and + * destination of a copy pair up by folio rather than by whatever + * segments an IOMMU would merge two scatterlists into. + */ +static int map_folios(struct dma_work *work, struct list_head **src_pos, + struct list_head **dst_pos, unsigned int nr) +{ + struct device *dev = work->dev; + unsigned int i; + + work->copies = kcalloc(nr, sizeof(*work->copies), GFP_KERNEL); + if (!work->copies) + return -ENOMEM; - sg_src = work->src_sgt->sgl; - sg_dst = work->dst_sgt->sgl; for (i = 0; i < nr; i++) { struct folio *src = list_entry(*src_pos, struct folio, lru); struct folio *dst = list_entry(*dst_pos, struct folio, lru); - - sg_set_folio(sg_src, src, folio_size(src), 0); - sg_set_folio(sg_dst, dst, folio_size(dst), 0); + struct dcbm_copy *copy = &work->copies[i]; + + copy->len = folio_size(src); + copy->src = dma_map_page_attrs(dev, folio_page(src, 0), 0, + copy->len, DMA_TO_DEVICE, 0); + if (dma_mapping_error(dev, copy->src)) + goto err; + copy->dst = dma_map_page_attrs(dev, folio_page(dst, 0), 0, + copy->len, DMA_FROM_DEVICE, 0); + if (dma_mapping_error(dev, copy->dst)) { + dma_unmap_page_attrs(dev, copy->src, copy->len, + DMA_TO_DEVICE, 0); + goto err; + } + work->nr_copies++; *src_pos = (*src_pos)->next; *dst_pos = (*dst_pos)->next; - - if (i < nr - 1) { - sg_src = sg_next(sg_src); - sg_dst = sg_next(sg_dst); - } } + return 0; +err: + /* The mapped prefix is undone by cleanup_dma_work(). */ + return -EIO; +} - dev = dmaengine_get_dma_device(work->chan); - if (!dev) { - ret = -ENODEV; - goto err_free_dst_table; - } - ret = dma_map_sgtable(dev, work->src_sgt, DMA_TO_DEVICE, - DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_NO_KERNEL_MAPPING); - if (ret) - goto err_free_dst_table; - ret = dma_map_sgtable(dev, work->dst_sgt, DMA_FROM_DEVICE, - DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_NO_KERNEL_MAPPING); - if (ret) - goto err_unmap_src; +static void unmap_folios(struct dma_work *work) +{ + unsigned int i; - /* - * TODO: IOMMU may merge segments unevenly on the two sides, fall back - * bail to CPU copy. In practice, I have not observed merging in tests. - * Handling unequal nents is left for follow-up. - */ - if (work->src_sgt->nents != work->dst_sgt->nents) { - ret = -EINVAL; - goto err_unmap_dst; + for (i = 0; i < work->nr_copies; i++) { + struct dcbm_copy *copy = &work->copies[i]; + + dma_unmap_page_attrs(work->dev, copy->dst, copy->len, + DMA_FROM_DEVICE, 0); + dma_unmap_page_attrs(work->dev, copy->src, copy->len, + DMA_TO_DEVICE, 0); } - work->mapped = true; - return 0; + work->nr_copies = 0; + kfree(work->copies); + work->copies = NULL; +} -err_unmap_dst: - dma_unmap_sgtable(dev, work->dst_sgt, DMA_FROM_DEVICE, - DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_NO_KERNEL_MAPPING); -err_unmap_src: - dma_unmap_sgtable(dev, work->src_sgt, DMA_TO_DEVICE, - DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_NO_KERNEL_MAPPING); -err_free_dst_table: - sg_free_table(work->dst_sgt); -err_free_src_table: - sg_free_table(work->src_sgt); -err_free_dst: - kfree(work->dst_sgt); - work->dst_sgt = NULL; -err_free_src: - kfree(work->src_sgt); - work->src_sgt = NULL; - return ret; +/* + * Wait for every descriptor of a slice that was handed to the engine. + * Nothing is ever terminated. The descriptors reference the folio + * mappings and the on-stack work until they complete, dmaengine has + * no per-descriptor abort, and a terminate ends every transaction that + * is out on the channel, whoever submitted it, and calls none of them + * back. Anything submitted runs to completion, and a failure only + * decides what the caller does afterwards. + */ +static int dma_work_wait(struct dma_work *work) +{ + if (!work->submitted) + return 0; + wait_event(work->waitq, !atomic_read(&work->pending)); + return atomic_read(&work->error); } static void cleanup_dma_work(struct dma_work *works, int actual_channels) { - struct device *dev; int i; if (!works) @@ -138,71 +301,121 @@ static void cleanup_dma_work(struct dma_work *works, int actual_channels) for (i = 0; i < actual_channels; i++) { if (!works[i].chan) continue; - - dev = dmaengine_get_dma_device(works[i].chan); - - if (works[i].mapped) - dmaengine_terminate_sync(works[i].chan); - - if (dev && works[i].mapped) { - if (works[i].src_sgt) { - dma_unmap_sgtable(dev, works[i].src_sgt, - DMA_TO_DEVICE, - DMA_ATTR_SKIP_CPU_SYNC | - DMA_ATTR_NO_KERNEL_MAPPING); - sg_free_table(works[i].src_sgt); - kfree(works[i].src_sgt); - } - if (works[i].dst_sgt) { - dma_unmap_sgtable(dev, works[i].dst_sgt, - DMA_FROM_DEVICE, - DMA_ATTR_SKIP_CPU_SYNC | - DMA_ATTR_NO_KERNEL_MAPPING); - sg_free_table(works[i].dst_sgt); - kfree(works[i].dst_sgt); - } - } - dma_release_channel(works[i].chan); + unmap_folios(&works[i]); } kfree(works); } -static int submit_dma_transfers(struct dma_work *work) +static int submit_one(struct dma_work *work, struct dma_async_tx_descriptor *tx) { - struct scatterlist *sg_src, *sg_dst; - struct dma_async_tx_descriptor *tx; - unsigned long flags = DMA_CTRL_ACK; dma_cookie_t cookie; - int i; - atomic_set(&work->pending, 1); + tx->callback_result = dma_completion_callback; + tx->callback_param = work; + atomic_inc(&work->pending); - sg_src = work->src_sgt->sgl; - sg_dst = work->dst_sgt->sgl; - for_each_sgtable_dma_sg(work->src_sgt, sg_src, i) { - if (i == work->src_sgt->nents - 1) - flags |= DMA_PREP_INTERRUPT; + cookie = dmaengine_submit(tx); + if (dma_submit_error(cookie)) { + atomic_dec(&work->pending); + return -EIO; + } + /* + * Start it now. The throttle sleeps for completions before the + * next descriptor is prepared, and on a provider whose submit only + * queues (a virt-dma driver such as ptdma) nothing completes until + * issue_pending: deferring it to the end of the slice would wait + * on descriptors that never ran. The descriptor's own channel, + * because a work may spread its descriptors over several. + */ + dma_async_issue_pending(tx->chan); + return 0; +} - tx = dmaengine_prep_dma_memcpy(work->chan, - sg_dma_address(sg_dst), - sg_dma_address(sg_src), - sg_dma_len(sg_src), flags); - if (!tx) { - atomic_set(&work->pending, 0); - return -EIO; +/* + * Hand the copies to the provider as scatter-gather transactions of + * up to sg_elems folios each. A batch-capable engine such as DSA + * executes one transaction as one hardware batch descriptor, so the + * submission cost is paid once per sg_elems folios instead of once + * per folio. The scatterlists carry the DMA addresses mapped by + * map_folios(); they are not mapped again. + */ +static int submit_sg_transfers(struct dma_work *work, unsigned long flags) +{ + struct scatterlist *src_sg, *dst_sg; + unsigned int elems = READ_ONCE(sg_elems); + unsigned int done = 0; + int ret = 0; + + src_sg = kmalloc_array(2 * elems, sizeof(*src_sg), GFP_KERNEL); + if (!src_sg) + return -ENOMEM; + dst_sg = src_sg + elems; + + while (done < work->nr_copies) { + unsigned int n = min(elems, work->nr_copies - done); + struct dma_async_tx_descriptor *tx; + unsigned int i; + + sg_init_table(src_sg, n); + sg_init_table(dst_sg, n); + for (i = 0; i < n; i++) { + struct dcbm_copy *copy = &work->copies[done + i]; + + sg_dma_address(&src_sg[i]) = copy->src; + sg_dma_len(&src_sg[i]) = copy->len; + sg_dma_address(&dst_sg[i]) = copy->dst; + sg_dma_len(&dst_sg[i]) = copy->len; } - if (i == work->src_sgt->nents - 1) { - tx->callback = dma_completion_callback; - tx->callback_param = work; + dma_work_throttle(work); + tx = dmaengine_prep_dma_memcpy_sg(work->chan, dst_sg, n, + src_sg, n, flags); + if (!tx) { + ret = -EIO; + break; } + ret = submit_one(work, tx); + if (ret) + break; + done += n; + } + + kfree(src_sg); + return ret; +} - cookie = dmaengine_submit(tx); - if (dma_submit_error(cookie)) { - atomic_set(&work->pending, 0); +static int submit_dma_transfers(struct dma_work *work) +{ + struct dma_async_tx_descriptor *tx; + unsigned long flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT; + unsigned int i; + int ret; + + /* + * Cache-allocating writes leave the copied data LLC-warm for the + * first access after remapping, at the cost of cache footprint + * for folios that are not touched soon. + */ + if (READ_ONCE(cache_ctrl)) + flags |= DMA_PREP_CACHE_CONTROL; + + dma_work_init(work); + + if (dma_has_cap(DMA_MEMCPY_SG, work->chan->device->cap_mask)) + return submit_sg_transfers(work, flags); + + for (i = 0; i < work->nr_copies; i++) { + struct dcbm_copy *copy = &work->copies[i]; + + dma_work_throttle(work); + tx = dmaengine_prep_dma_memcpy(work->chan, copy->dst, copy->src, + copy->len, flags); + if (!tx) return -EIO; - } - sg_dst = sg_next(sg_dst); + + ret = submit_one(work, tx); + if (ret) + return ret; } return 0; } @@ -220,34 +433,35 @@ static int folios_copy_dma(struct list_head *dst_list, { struct folio *dst; struct dma_work *works; + struct dcbm_group *grp; struct list_head *src_pos = src_list->next; struct list_head *dst_pos = dst_list->next; + unsigned long chan_mask; int i, folios_per_chan, ret; - dma_cap_mask_t mask; int actual_channels = 0; - unsigned int max_channels; + unsigned int max_channels, idx; max_channels = min3(READ_ONCE(nr_dma_channels), nr_folios, (unsigned int)MAX_DMA_CHANNELS); - works = kcalloc(max_channels, sizeof(*works), GFP_KERNEL); - if (!works) - return -ENOMEM; - - dma_cap_zero(mask); - dma_cap_set(DMA_MEMCPY, mask); + /* Prefer the device closest to where the copies are written. */ + dst = list_first_entry(dst_list, struct folio, lru); + chan_mask = dcbm_claim_channels(max_channels, folio_nid(dst), &grp); + if (!chan_mask) { + atomic_long_inc(&batches_refused); + return -EBUSY; + } - for (i = 0; i < max_channels; i++) { - works[actual_channels].chan = dma_request_chan_by_mask(&mask); - if (IS_ERR(works[actual_channels].chan)) - break; - init_completion(&works[actual_channels].done); - actual_channels++; + works = kcalloc(hweight_long(chan_mask), sizeof(*works), GFP_KERNEL); + if (!works) { + dcbm_release_channels(chan_mask); + return -ENOMEM; } - if (actual_channels == 0) { - kfree(works); - return -ENODEV; + for_each_set_bit(idx, &chan_mask, nr_channels) { + works[actual_channels].chan = channels[idx].chan; + works[actual_channels].dev = grp->dev; + actual_channels++; } for (i = 0; i < actual_channels; i++) { @@ -256,34 +470,27 @@ static int folios_copy_dma(struct list_head *dst_list, if (folios_per_chan == 0) continue; - ret = setup_sg_tables(&works[i], &src_pos, &dst_pos, - folios_per_chan); + ret = map_folios(&works[i], &src_pos, &dst_pos, + folios_per_chan); if (ret) goto err_cleanup; } for (i = 0; i < actual_channels; i++) { - if (!works[i].mapped) + if (!works[i].copies) continue; ret = submit_dma_transfers(&works[i]); + dma_work_done_submitting(&works[i]); + works[i].submitted = true; if (ret) - goto err_cleanup; + goto err_wait; } - for (i = 0; i < actual_channels; i++) { - if (atomic_read(&works[i].pending) > 0) - dma_async_issue_pending(works[i].chan); - } - - for (i = 0; i < actual_channels; i++) { - if (atomic_read(&works[i].pending) == 0) - continue; - if (!wait_for_completion_timeout(&works[i].done, - msecs_to_jiffies(10000))) { - ret = -ETIMEDOUT; - goto err_cleanup; - } - } + ret = 0; + for (i = 0; i < actual_channels; i++) + ret |= dma_work_wait(&works[i]); + if (ret) + goto err_cleanup; /* * All folios copied; mark each destination so the move phase @@ -293,19 +500,76 @@ static int folios_copy_dma(struct list_head *dst_list, folio_set_migrate_copied(dst); cleanup_dma_work(works, actual_channels); + dcbm_release_channels(chan_mask); atomic_long_add(nr_folios, &folios_migrated); return 0; +err_wait: + /* Whatever was queued before the failure runs out. */ + for (i = 0; i < actual_channels; i++) + dma_work_wait(&works[i]); err_cleanup: pr_warn_ratelimited("dcbm: DMA copy failed (%d), falling back to CPU\n", ret); cleanup_dma_work(works, actual_channels); + dcbm_release_channels(chan_mask); atomic_long_add(nr_folios, &folios_failures); return ret; } +static void dcbm_put_channels(void) +{ + while (nr_channels) { + nr_channels--; + dma_release_channel(channels[nr_channels].chan); + channels[nr_channels].chan = NULL; + } + nr_groups = 0; +} + +static int dcbm_get_channels(void) +{ + dma_cap_mask_t mask; + + dma_cap_zero(mask); + dma_cap_set(DMA_MEMCPY, mask); + + while (nr_channels < nr_dma_channels) { + struct dma_chan *chan = dma_request_chan_by_mask(&mask); + struct device *dev; + + if (IS_ERR(chan)) + break; + + /* + * The descriptors of one batch may spread over several + * channels but its folios are mapped only once, so a + * batch must stay within one DMA device: group the + * channels by device as they enumerate. + */ + dev = dmaengine_get_dma_device(chan); + if (!dev) { + dma_release_channel(chan); + break; + } + if (!nr_groups || groups[nr_groups - 1].dev != dev) { + groups[nr_groups].dev = dev; + groups[nr_groups].node = dev_to_node(dev); + groups[nr_groups].first = nr_channels; + groups[nr_groups].nr = 0; + nr_groups++; + } + groups[nr_groups - 1].nr++; + + channels[nr_channels].chan = chan; + mutex_init(&channels[nr_channels].lock); + nr_channels++; + } + return nr_channels ? 0 : -ENODEV; +} + static const struct migrator dma_migrator = { .name = "DCBM", .offload_copy = folios_copy_dma, @@ -330,15 +594,23 @@ static int offloading_param_set(const char *val, const struct kernel_param *kp) return 0; } if (enable) { + ret = dcbm_get_channels(); + if (ret) { + mutex_unlock(&dcbm_mutex); + return ret; + } ret = migrate_offload_register(&dma_migrator, READ_ONCE(dcbm_reason_mask)); if (ret) { + dcbm_put_channels(); mutex_unlock(&dcbm_mutex); return ret; } WRITE_ONCE(offloading_enabled, true); } else { migrate_offload_unregister(&dma_migrator); + /* No batch is in flight past unregister; channels are idle. */ + dcbm_put_channels(); WRITE_ONCE(offloading_enabled, false); } mutex_unlock(&dcbm_mutex); @@ -370,6 +642,10 @@ static int nr_dma_chan_param_set(const char *val, const struct kernel_param *kp) return -EINVAL; mutex_lock(&dcbm_mutex); + if (offloading_enabled) { + mutex_unlock(&dcbm_mutex); + return -EBUSY; + } WRITE_ONCE(nr_dma_channels, new_val); mutex_unlock(&dcbm_mutex); return 0; @@ -385,7 +661,7 @@ static const struct kernel_param_ops nr_dma_chan_param_ops = { .get = nr_dma_chan_param_get, }; module_param_cb(nr_dma_chan, &nr_dma_chan_param_ops, NULL, 0644); -MODULE_PARM_DESC(nr_dma_chan, "Max DMA channels to use (1..16)"); +MODULE_PARM_DESC(nr_dma_chan, "DMA channels to acquire when enabling (1..16)"); /* reason_mask: set of MR_* reasons this migrator handles */ static int reason_mask_param_set(const char *val, const struct kernel_param *kp) @@ -455,6 +731,33 @@ static const struct kernel_param_ops folios_failures_param_ops = { module_param_cb(folios_failures, &folios_failures_param_ops, NULL, 0644); MODULE_PARM_DESC(folios_failures, "DMA-copy failure count (write to reset)"); +static int batches_refused_param_set(const char *val, const struct kernel_param *kp) +{ + atomic_long_set(&batches_refused, 0); + return 0; +} + +static int batches_refused_param_get(char *buffer, const struct kernel_param *kp) +{ + return sysfs_emit(buffer, "%ld\n", atomic_long_read(&batches_refused)); +} + +static const struct kernel_param_ops batches_refused_param_ops = { + .set = batches_refused_param_set, + .get = batches_refused_param_get, +}; +module_param_cb(batches_refused, &batches_refused_param_ops, NULL, 0644); +MODULE_PARM_DESC(batches_refused, "Batches refused because all channels were busy (write to reset)"); + +module_param(cache_ctrl, bool, 0644); +MODULE_PARM_DESC(cache_ctrl, "Request cache-allocating writes (DMA_PREP_CACHE_CONTROL)"); + +module_param(max_inflight, uint, 0644); +MODULE_PARM_DESC(max_inflight, "Descriptors in flight per channel before waiting for completions"); + +module_param(sg_elems, uint, 0644); +MODULE_PARM_DESC(sg_elems, "Folios per scatter-gather transaction on DMA_MEMCPY_SG providers"); + static int __init dcbm_init(void) { pr_info("dcbm: DMA Core Batch Migrator initialized\n"); @@ -466,6 +769,7 @@ static void __exit dcbm_exit(void) mutex_lock(&dcbm_mutex); if (offloading_enabled) { migrate_offload_unregister(&dma_migrator); + dcbm_put_channels(); offloading_enabled = false; } mutex_unlock(&dcbm_mutex); -- 2.43.0