From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796221199; b=P7tTsLXlZir/wseYZIO+OOXDF4G2jeSXBuadqrEUsMnxC7ZGBUc6gM5en4hURdAd3yHZ wYu5FJ+jEz0mS5Bp5312LWR21TNglAdKfn4PhJvbqmMrf1XxZA0uPqdS7OI1xT1cBSQxa 3nPGcyrOO1Li+NwFec2rlzHD8gruHxbJdTzxoO09s50g1aKLpR5OdeHsg8v+OzTXfaO2X DgE6ek18qe44dbnn94pC1Rh/ZKin2KkLpintIsECb3cAzRJQFCVL0xuUyaj+acdc89oJ9 73MDsYBcDA9JTYKL/ePi/qec46Vd6BAbA0UkXXUS6WxnfHjpTGnNfJwRGlVvMqHCnRQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796221199; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=Lit+/XKQNZImHC+ONu0vaOGgmqurhU7Xz2ps6OYmDS8=; b=T6UoHdCJkUnYeFm9KcKJHKQ4I3O22iYS/rTIQZvMri4blRqPLGtbaQ0ujHPMf4IcVACB JXbg0tZypYGfXzEx0/z0CQqshb2on77AYnwIZf6hP6ZwYg+jEJ4P02eVqn5j6x5+fSkA9 N4IbR8ICM3+tiZVs2EYwVeGR4zwzS4G0h/pUfPvl7FLfIpd+mhTxmMZKQw4D0h2Au8qTD Z6lVkvsM4pBW/9PLiPrq9q2KYDO9esSWz7hBnNWoFghNBlRGHZQigskvYwiNF1bOKcwyK dGJ3oK3lEGCl5i4foZX19xWXFsmxJGufgx7RfYsZfjmlAIitsHHGMr7qVxkn733VL3A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=IzvbP6m+; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 62397606A337 for ; Wed, 2 Dec 2026 14:19:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=IzvbP6m+ Received: by smtp.kernel.org (Postfix) with ESMTPSA id D8CBB1F00899; Wed, 2 Dec 2026 14:19:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796221199; bh=Lit+/XKQNZImHC+ONu0vaOGgmqurhU7Xz2ps6OYmDS8=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=IzvbP6m+qTgr7xUycPsZYtKwBoPnjVm51Skx+v3WHGJzIT/yOCosyPYKMNFZJWKz5 szA8gDKC4XKE5eiJgNE15ni/RBi7SXtHKd+JPZNwtu4cayhJrEiTZSPsTKUvwvVTD0 99VEKQn5wO0LXTF1bYZPqC+yCqpdFryHI0SwMmzMyLTSo6+e4Wq9JrFLmRD7GmDVcu +XqFKcaiJJQYjLAvy1ewZ+9L+DniplsQWQp5w1IWwf3HPo03p/3tyJctXIQ1zpeM9J jy8T+3y4aWUDdo9Aw7Jcy5q3wVgTc3AbrRN8Jnnkpp0djhGX1LkW1I5mQ0s4CaaTu+ 4wUmHHrsNhHZQ== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [RFC PATCH 10/10] drivers/migrate_offload: make dcbm correct on shared, out-of-order engines Reply-To: sashiko-reviews@lists.linux.dev To: "Daniel Byrne" , "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, "Mike Day" , dmaengine@vger.kernel.org, "Vinicius Costa Gomes" , "Frank Li" , "Dave Jiang" In-Reply-To: <20261201141259.2124188-11-daniel1.byrne@intel.com> References: <20261201141259.2124188-1-daniel1.byrne@intel.com> <20261201141259.2124188-11-daniel1.byrne@intel.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Wed, 02 Dec 2026 14:19:56 +0000 X-Mailing-List: dmaengine@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [High] drivers/migrate_offload: UAF on work->waitq in dma_completion_callback() - [Medium] drivers/migrate_offload: deadlock in dma_work_throttle() on bad max_inflight - [Medium] drivers/migrate_offload: OOB write in submit_sg_transfers() on bad sg_elems - [Medium] drivers/migrate_offload: UAF and channel leak on module param parse failure - [Medium] drivers/migrate_offload: folios_copy_dma() stalls claimed channels on throttle -- commit 3a2193077cc84a75579aba88a063d111f5f2b975 Author: Daniel Byrne drivers/migrate_offload: make dcbm correct on shared, out-of-order engines This commit reworks DCBM to track completion per descriptor, pool and group DMA channels by NUMA node, map folios individually with optional DMA_MEMCPY_SG batching, and throttle in-flight descriptors per channel. Link: https://lore.kernel.org/linux-mm/20260619160725.lfcxrbj5go67qy6u@wran= gler/ > diff --git a/drivers/migrate_offload/dcbm/dcbm.c b/drivers/migrate_offloa= d/dcbm/dcbm.c > index 3df9232586ff2..03785a8221610 100644 > --- a/drivers/migrate_offload/dcbm/dcbm.c > +++ b/drivers/migrate_offload/dcbm/dcbm.c [ ... ] > @@ -9,127 +9,290 @@ > +static void dma_completion_callback(void *data, > + const struct dmaengine_result *result) > { > struct dma_work *work =3D data; > =20 > - if (atomic_dec_and_test(&work->pending)) > - complete(&work->done); > + if (!result || result->result !=3D DMA_TRANS_NOERROR) > + atomic_set(&work->error, -EIO); > + > + atomic_dec(&work->pending); > + wake_up(&work->waitq); > } > =20 > -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); > +} > =20 > - work->src_sgt =3D kmalloc_obj(*work->src_sgt, GFP_KERNEL); > - if (!work->src_sgt) > - return -ENOMEM; > - work->dst_sgt =3D kmalloc_obj(*work->dst_sgt, GFP_KERNEL); > - if (!work->dst_sgt) { > - ret =3D -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 =3D READ_ONCE(max_inflight) + 1; > + > + wait_event(work->waitq, atomic_read(&work->pending) < limit); > +} [Severity: High] Can dma_completion_callback() access work->waitq after cleanup_dma_work() has freed works? Unlike complete(), which holds the waitqueue lock across marking the completion done and waking waiters, dma_completion_callback() decrements work->pending before acquiring work->waitq.lock in wake_up(): CPU0 CPU1 folios_copy_dma() ... dma_completion_callback() atomic_dec(&work->pending) dma_work_wait() wait_event(work->waitq, !atomic_read(&work->pending)) cleanup_dma_work() kfree(works) wake_up(&work->waitq) spin_lock_irqsave(&work->waitq.lo= ck) <-- use-after-free [Severity: Medium] Can dma_work_throttle() hang forever if max_inflight is set to 0 or UINT_MAX? max_inflight is exposed as an unvalidated uint module parameter. Because dma_work_init() sets work->pending to 1 for the submitter reference until submit_dma_transfers() returns, setting max_inflight to 0 makes limit 1 before any descriptor is submitted, and setting it to UINT_MAX wraps limit to 0. In either case atomic_read(&work->pending) < limit is false with no descriptors in flight to wake work->waitq, deadlocking the migrating task with the folio locks and channel mutexes held. [ ... ] > @@ -138,71 +301,121 @@ static void cleanup_dma_work(struct dma_work *work= s, int actual_channels) > +static int submit_sg_transfers(struct dma_work *work, unsigned long flag= s) > +{ > + struct scatterlist *src_sg, *dst_sg; > + unsigned int elems =3D READ_ONCE(sg_elems); > + unsigned int done =3D 0; > + int ret =3D 0; > + > + src_sg =3D kmalloc_array(2 * elems, sizeof(*src_sg), GFP_KERNEL); > + if (!src_sg) > + return -ENOMEM; > + dst_sg =3D src_sg + elems; > + > + while (done < work->nr_copies) { > + unsigned int n =3D 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); [Severity: Medium] Can an invalid sg_elems module parameter cause an out-of-bounds write in submit_sg_transfers()? sg_elems is also an unvalidated uint module parameter. If sg_elems is 0, kmalloc_array(0, ...) returns ZERO_SIZE_PTR, n is 0, and sg_init_table(src_sg, 0) writes to src_sg[-1]. If sg_elems is at least 0x80000000, 2 * elems overflows in 32-bit unsigned arithmetic before kmalloc_array() widens it to size_t, allocating a small buffer that sg_init_table() and the loop below overrun. [ ... ] > @@ -256,34 +470,27 @@ static int folios_copy_dma(struct list_head *dst_li= st, > for (i =3D 0; i < actual_channels; i++) { > - if (!works[i].mapped) > + if (!works[i].copies) > continue; > ret =3D submit_dma_transfers(&works[i]); > + dma_work_done_submitting(&works[i]); > + works[i].submitted =3D true; > if (ret) > - goto err_cleanup; > + goto err_wait; > } [Severity: Medium] Does calling submit_dma_transfers() sequentially across works[] stall the remaining claimed channels whenever a slice exceeds max_inflight descriptors? folios_copy_dma() locks all claimed channels up front, but submit_dma_transfers(&works[i]) blocks in dma_work_throttle() once max_inflight descriptors are queued on channel i. On a 512-folio batch split across 4 channels without DMA_MEMCPY_SG, channel 0 must complete 96 of its 128 descriptors in hardware before any descriptors are submitted to channels 1..3, while channels 1..3 sit idle with their locks held and cause concurrent batches to fail dcbm_claim_channels() with -EBUSY. [ ... ] > @@ -330,15 +594,23 @@ static int offloading_param_set(const char *val, co= nst struct kernel_param *kp) > return 0; > } > if (enable) { > + ret =3D dcbm_get_channels(); > + if (ret) { > + mutex_unlock(&dcbm_mutex); > + return ret; > + } > ret =3D 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, co= nst struct kernel_param *kp) > return -EINVAL; > =20 > 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; [Severity: Medium] What happens if the module is loaded with "offloading=3D1 nr_dma_chan=3D4" = on the command line? parse_args() runs module parameter setters in command-line order before dcbm_init(). offloading_param_set() acquires a channel and registers dma_migrator, and then nr_dma_chan_param_set() sees offloading_enabled and returns -EBUSY. load_module() then aborts and frees the module without running dcbm_exit(), leaking the DMA channel and leaving dma_migrator registered in freed module memory. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261201141259.2124= 188-1-daniel1.byrne@intel.com?part=3D10