From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1F35128725B for ; Wed, 15 Jul 2026 16:32:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=209.85.210.169 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784133170; cv=pass; b=IbtoSSmcQydVFVu63dTAU8Qm3PxEKnV6Jy+DB6JLGEAbI/KenFGZQBVG/7owoChz+8VtOA1vGwhZ2nVEfvwPs58HsZ7lva24EJ9L0uNtiT5KK5DNJ9c06oDAfbNtBDC5jYkD8+x7PQNGUzaYY2i+QraYlyuATQRMu3ikG0gyAiM= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784133170; c=relaxed/simple; bh=hrg1ytC5Zi5NfmZBT3q8rLT/Rqkzlss0YUPLQLhBTCQ=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=OnNJ3axZSA32vaGKrtAX67J+wX4vmuwfzDze4d3voj4yoYPimY33oXoaClO9y2fv3znDmUvUxUWwFGYe1goLiXzliPM4QOc3njv8qyM9BuP0nlOgwmxZpRBDqvaG4rMoQ+VkGWlj4Nb0dg9gV9pCQy/WnvBn1lFgK2c5GCSCGCI= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=QxlcPBbD; arc=pass smtp.client-ip=209.85.210.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="QxlcPBbD" Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-848761b5897so2941223b3a.3 for ; Wed, 15 Jul 2026 09:32:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1784133168; cv=none; d=google.com; s=arc-20260327; b=jqvrf7ZSJg0Y/D24Yi2AudLNBS1kHRv8P/qtaxAVdYGKVFxUJ2VRhDl8aqyQi+kZFO GUM6FEj4u40Bfd1zrg6XJ7qJVUcIhdImbgVIZg+Md2ShfrKr+pmpHCUaV5gHd8FD2oe7 tyxBwJNoeUh0k6Jwb0v8+WiR7QztoWPSeJCrQjpSC8CrZECfafHf3Ub/EK9KAxSsafXN oPA4ZC6XWiAxPF5qv6IdA7Bynynf91bEbnL/r+/FjXD1f6Yu4ui+5O0yVmHL25IFSD74 Qn11Hpfnyub5u09P1S7DiqbrImx5nD7ayNC9EXOSIjuuVCN9tN8a+N4DhqlQeetXzxnz P0dg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=E6I7Lcnt8DLXO66ZlyLhnbgPF/Qt7CMHhIJMJAFFWN4=; fh=S07CWSE+ZjN3n25ZLHoTDAoD8BLGk4s121a9zUF4aT4=; b=FKawAuUPvUlf+R0Rl8lPvjVGfrvB+57UVs9mnDuSKdAvWTmaI6uYBgykr5TUacJY0N YfxSBpt/EtkB4j7AFMIb7LwG5UHRCySaKFh47B421OwvWVySJ+OFI2IzC4OH8G/0rfaF j1GZHiPkGY6kqbOCynIp1mez2bmZ7QSl8Yy3VipOegZDOtVNb8+ndX/esvAMYHL2t1kd rAxhCQ+z0nMy6cSlfnhvvGOZOmEc94vRitzo0C9cHJ763+JXbbPhyPbmJRuLJvmolB8S koGHZYbHcBndK48ruxJFYsAs94wJ0DaU+bxRxd4urM5l+WMWRyDxMssfFoPJ7H+mkoKV 07Bw==; darn=vger.kernel.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784133168; x=1784737968; darn=vger.kernel.org; h=content-transfer-encoding:content-type:cc:to:subject:message-id :date:from:in-reply-to:references:mime-version:from:to:cc:subject :date:message-id:reply-to:content-type; bh=E6I7Lcnt8DLXO66ZlyLhnbgPF/Qt7CMHhIJMJAFFWN4=; b=QxlcPBbD0AYCbSN9dKCZdGZlx5OqhR+Q/C4AvqjqwBfLN8K3/dCsN78vI+VcyGM/25 mKaunq2NNGKgoUCYGE/zvWiVIKroJUkh2eJ5CS8+uPvJqUB9JCCTDeCZVQowZyA2nV18 Ugk8bl2ZD1HGkT0CCVk5mBEtErO0scxmMwscsoPhqitVnBVd346BOa8x/SE3MjSoPpJV /CjIDVSBXtyuShxngJZ7zHfK+3/Bc2nkjAngX/L6P2UgLFqCnjoSy6/i46ipS0v5A3d3 vufBUvpSD6BUX1PD5icKdhXsCsxgscWzmG1hlh63oLX21mVJGTwLUw+YGUdxT0oHA+5x mX8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784133168; x=1784737968; h=content-transfer-encoding:content-type:cc:to:subject:message-id :date:from:in-reply-to:references:mime-version:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=E6I7Lcnt8DLXO66ZlyLhnbgPF/Qt7CMHhIJMJAFFWN4=; b=LjdYqrStGKa/jL0zsAFwlshEobri05UyxIO/FkknWaAkvi5hN0MRQnc3husZoIsTWc rQoyt5D/yToaoJck2dQ9B07sMJAyffCOch0bp/3u1sOD1iAMIHTjkP0VxkReach3DbpC jHmioStvz8qSeWg/jMGx0UogFYH/vHoo64IZPT95ydYB3+3NZ2LrqWaxcKeT7JY7gksx bQFBTLk74iJ9qc3Gj2RcJjJAPCCY2cV8F6UQJE5ixq7mtyKCYY7r23fLfFuXekHdCWM0 dtEDYN4z7FAJ3T65DrduM09V/+NIKAM60LXLOcp+6wnkrw+J9/SoMNC5dZ+OCWYkTi2Z wf3A== X-Forwarded-Encrypted: i=1; AHgh+RpeXIKwkHaHso17eAjKdDG3lBNyExdXTj3+9yT/eNTMP0Qo7fJ6gnpCb2aecvz6zR4bCQuEFLToOPVLtTQ=@vger.kernel.org X-Gm-Message-State: AOJu0YxBzkIYS4WZB6EdLIeI9hVCvOumVh4SNYpRBZqZgIMQpTmeQ6MY yJ1L1A2mkPuWuUmX9bycQUJ0Lo3dkQiXnZiYpn5Mr6RA8vV8mx+VU5HG5nbcDUskCYmYT3cZys+ GZIHhfaffxGowbQYpuCXc8yRsp+MPjZtKMBOn9+k= X-Gm-Gg: AfdE7cnNJN73zzakoWfJVUyycE9vOzFFONRjuwU4g7X1C3OfRpc8EdPhwzRa/uOJIRy eW973y8kTMw2zUbE6o+J+6s0WixqKTa3VY1jQFKfWnOnRJ9aYqdwwyZJbzggv33UbM/Rcr4DdsM HYhx2G8tDPT0jSqZRtfNEx43qrQP3TndNK4NrJeFPWuIAh8XDX+/mqmMBfpvdieQFj7ISCcAFur wVfsn/oJ0AENY98VbnP+28XoXJXqhZ34Wz1G0nNC76r8b5/nXeX6/4HnlgvRUwtGtiqsaYlC6AE MXumII558t7qdDXEvVULgLV8LXN0mplgpxfO5dtC X-Received: by 2002:a05:6a00:9467:b0:845:e3e4:9702 with SMTP id d2e1a72fcca58-84a55820631mr5794824b3a.47.1784133168281; Wed, 15 Jul 2026 09:32:48 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20260714-swap-pcp-priq-v1-0-de9b164ed419@tencent.com> <20260714-swap-pcp-priq-v1-9-de9b164ed419@tencent.com> In-Reply-To: From: Kairui Song Date: Thu, 16 Jul 2026 00:32:09 +0800 X-Gm-Features: AUfX_mw8TfHkfSXVyb7VtLod2hgHDmoWUPZ9aGTZS-wnm7EzXF8K42xCziSMuxs Message-ID: Subject: Re: [PATCH RFC 09/13] mm/swap: add priority queue for swap device allocation To: Youngjun Park Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton , Chris Li , Nhat Pham , Baoquan He , Barry Song , Kemeng Shi Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Jul 15, 2026 at 2:10=E2=80=AFAM Youngjun Park wrote: > > On Tue, Jul 14, 2026 at 01:25:47AM +0800, Kairui Song via B4 Relay wrote: > > From: Kairui Song > > Hi Kairui, > > Thanks for the patch :) > I like the idea. A few questions and comments on the core implementation = below. > > I'll share my higher-level thoughts (e.g. on a tunable interface for the = round-robin policy) > separately in a follow-up. Thanks for the review! > > +/* > > + * All available swap_info_structs are grouped by priority rings, the = rings > > + * are ordered in a queue by priority (lower prio value =3D higher pri= ority). > > looks like comment updated needed :) Nice catch :), I got confused by the priority defination again, will fix it= . > [...] > > + > > +/* > > + * The ring is protected by swapon_rwsem so updating it is costly. To = make > > + * the allocator and other users skip full devices faster, the lowest = bit of > > + * a device pointer is used to mark it disabled (temporarily unavailab= le). > > + * This relies on struct swap_info_struct being sufficiently > > + * aligned (guaranteed by kmalloc). > > + */ > > +#define SWAP_DEVICE_MASKED_SHIFT 0 > > +#define SWAP_DEVICE_MASKED_BIT BIT(SWAP_DEVICE_MASKED_SH= IFT) > > Unlike the old plist, full devices stay in the ring, so every > allocation keeps walking past them. When the higher priority > devices are all full, each swap out pays one masked pointer check > per full device before reaching a usable one. That is also the > steady state for swap tiering, so I want to be sure it stays cheap. > It looks like a deliberate trade-off of the static ring design. > What do you think? Right, this should be super cheap as modern processors are really good at walking arrays. We have at most 32 swap devices in most cases, only 4 cache lines if they are in the same top ring and I think the CPU will only spend a dozen cycles on them. And rings are mostly read-only; they will stay cached for hot access. If they fallback to secondary rings... I think these devices are mostly performance insensitive anyway, and CPUs are still pretty good at reading the readonly arrays. I did plan to add a ring->avail to skip completely empty rings, or use cmpxchg & a seq style lock (don't shrink the ring, just shift the content on full device removal) to update the ring content, to archive 0 overhead if we got more full devices, it works but that's ends up super complex with no measurable gain. Maybe we can implement these if this percpu reader queue can be used as a generic infrastructure for other components, or if we grow to support hundreds of swaps someday, not really a issue for now I think. > > [...] > > > +static struct swap_info_struct *swap_queue_get_device(long nr_alloc, i= nt nr_iter) > > +{ > > + bool rotate =3D false; > > + struct swap_info_struct *si; > > + struct swap_ring_iterator *ri; > > + struct swap_prio_ring *ring; > > + unsigned int queue_idx; > > + > > + if (!swap_queue_len) > > + return ERR_PTR(-ENOENT); > > + > > + queue_idx =3D 0; > > + while (nr_iter >=3D swap_queue[queue_idx]->size) { > > + nr_iter -=3D swap_queue[queue_idx]->size; > > + if (++queue_idx >=3D swap_queue_len) > > + return ERR_PTR(-ENOENT); > > + } > > + > > + ring =3D swap_queue[queue_idx]; > > + local_lock(&swap_queue_readers->lock); > > + ri =3D this_cpu_ptr(&swap_queue_readers->ri[queue_idx]); > > + /* Rotate while iterating the ring, just not on the first try */ > > + if (nr_iter) > > + rotate =3D true; > > + else if (ri->rr_counter < nr_alloc) > > + rotate =3D true; > > + else if (ri->offset >=3D ring->size) > > + rotate =3D true; > > + if (rotate) { > > + ri->offset++; > > + ri->offset %=3D ring->size; > > + ri->rr_counter =3D SWAP_ROUND_ROBIN_QUOTA; > > + } > > + ri->rr_counter -=3D nr_alloc; > > + si =3D READ_ONCE(ring->dev[ri->offset]); > > + local_unlock(&swap_queue_readers->lock); > > + > > + if (swap_device_masked(si)) > > + return ERR_PTR(-EBUSY); > > + > > + si =3D swap_device_unmask_ptr(si); > > + return si; > > +} > > Tasks interleaving on the same CPU can rotate the ring more than > once for a single failure, since the lock is dropped between two > get_device() calls of one allocation. A retry walk can then visit > one device twice and skip another. It is transient and looks > harmless, and every fix I could think of costs more than it is > worth.... You mean dropping the local lock? Rotating it a bit more might be harmless I think, if that happens randomly, then the rotation is still fair. I did think about keep holding the local lock during the whole process, but making this unsleepable is a trade-off and reduce future flexibility... the local lock of the cluster below is already causing some trouble for swap table allocation so I hoped we could avoid that. So far, stress test didn't show any fragmentation issue. > > Unless you have a better idea, how about noting it in a > comment? Yeah, we can definitely doc that more cleanly. > > -/* Rotate the device and switch to a new cluster */ > > -static void swap_alloc_entry(struct folio *folio) > > +static int swap_alloc_entry(struct folio *folio) > > { > > - struct swap_info_struct *si, *next; > > + long nr_pages =3D folio_nr_pages(folio); > > + struct swap_info_struct *si; > > + int nr_iter, ret; > > > > - spin_lock(&swap_avail_lock); > > -start_over: > > - plist_for_each_entry_safe(si, next, &swap_avail_head, avail_list)= { > > - /* Rotate the device and switch to a new cluster */ > > - plist_requeue(&si->avail_list, &swap_avail_head); > > - spin_unlock(&swap_avail_lock); > > - if (get_swap_device_info(si)) { > > - cluster_alloc_swap_entry(si, folio); > > - put_swap_device(si); > > - if (folio_test_swapcache(folio)) > > - return; > > - if (folio_test_large(folio)) > > - return; > > + percpu_down_read(&swapon_rwsem); > > The local lock is only held inside swap_queue_get_device() now, so > the task can migrate between device selection and > cluster_alloc_swap_entry(). Then rr_counter is charged on one CPU > while the pages land in another CPU's cluster, so the pacing of > one cluster per visit does not hold anymore. > > Since you want the loop to stay sleepable, how about > migrate_disable() around the loop? That sounds like a good idea to avoid a potential race. I think these races are harmless though. > > > + for (nr_iter =3D 0;; nr_iter++) { > > + si =3D swap_queue_get_device(nr_pages, nr_iter); > > When a ring has a single device, which is probably the most common > setup, the iterator does nothing useful. Would a fast path that > skips the local_lock and just reads ring->dev[0] for size =3D=3D 1 > rings sequentially be worth it? swapon_rwsem keeps the queue stable, so c= hoosing > between the two paths should be race free, I think. Ah, that's a good idea indeed. > > del_from_avail_list(si, true); > > + percpu_up_write(&swapon_rwsem); > > > > /* > > * Swap allocator doesn't touch si lock, so looping through all > > @@ -3042,6 +3375,9 @@ SYSCALL_DEFINE1(swapoff, const char __user *, spe= cialfile) > > return err; > > } > > > > + percpu_down_write(&swapon_rwsem); > > + swap_queue_del(p); > > + > > /* > > * Wait for swap operations protected by get/put_swap_device() > > * to complete. Because of synchronize_rcu() here, all swap > > @@ -3062,7 +3398,6 @@ SYSCALL_DEFINE1(swapoff, const char __user *, spe= cialfile) > > if (!(p->flags & SWP_SOLIDSTATE)) > > atomic_dec(&nr_rotate_swap); > > > > - percpu_down_write(&swapon_rwsem); > > spin_lock(&p->lock); > > drain_mmlist(); > > synchronize_rcu() can take a while and it runs here with the write > lock held, together with wait_for_completion() and flush_work(), so > a single swapoff can stall all swap allocation during a little bit long t= ime. > > Only swap_queue_del() and the final teardown seem to need the write > lock. If my assumption is right, how about dropping it after > swap_queue_del() and re-taking it for the teardown? Good observation! I noticed we could tidy the locking scope, I skipped that optimization for the RFC :P. Swapoff is very performance insensitive IMO, it will put a huge pressure to the system and IO, so an RCU sync / flush hardly matters. The design is heavily optimized toward readers (ordinary swapin/swapout). But a tidier implementation and optimizations are definitely great to have. Thanks again for the detailed review! I think this can be rebased on top of your tiering work, there are some conflicts but nothing fundamental, and I think the ring design actually fits well. I've been experiement this for a while so I think better send it out to plan out the next steps, and this series can be used to optimize the tiering alloc as follow up.