From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (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 52765388397 for ; Wed, 15 Jul 2026 15:58:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784131089; cv=none; b=P3ZVPRjwGmnTjNJUj7nKR0K4y+msiOya2O44jlIWJQaCBn2zCH+014sXKr4nsXOafaNlxWK6OWS55QX0Cc7ToEhw/A2VG51nFV5SAukOkCA36ybroSypod6j5lYJ/D15feCIVD4+Se1sbwWSIKywwU/tSmFwqlQk/0APfwqWw5M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784131089; c=relaxed/simple; bh=yfxuS+IdhJhDXHueQtKaHAJrJqSskaHYaro0qYJ7sW8=; h=Message-ID:Date:MIME-Version:From:Subject:To:Cc:References: In-Reply-To:Content-Type; b=AaJUpgw+PGR7jJKW0/tn4sdxG1w4t230DML4Heb4+vf4XvscvF3l0hYua5zZZrMdRSyHrimlro28m1hqlVF28GgaCjeWNxuQhcWcFuCVE10BfhcqPfOJ3KK2H5kYe57t103oGviscoAcvjsrLxwuHyhtj4noCKluxxgITr5h6IU= ARC-Authentication-Results:i=1; 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=iwzC6tI3; arc=none smtp.client-ip=209.85.214.178 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="iwzC6tI3" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-2cad4170e8eso70366545ad.3 for ; Wed, 15 Jul 2026 08:58:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784131087; x=1784735887; darn=vger.kernel.org; h=content-transfer-encoding:content-type:in-reply-to:references:cc:to :subject:from:user-agent:mime-version:date:message-id:from:to:cc :subject:date:message-id:reply-to:content-type; bh=5jqq9tpaX/pC62ALfZOmXiDQ0y4ecS0xGD3vn+9oows=; b=iwzC6tI35trDBKB5a6Eopwi+fDoB9YosXofsvxgi87ne6Fsldx7+tMAdrng4XJjJ2M URxuvqgLKSAnQCESEbS73m2FxVimedW7gdkwoXFEyUMDjjk9TqAGZcXn0/uYM8KcAvjj ESpLpRccE91XGwDgUTDCVx8pkPuZNh4I2QWGP41V/0Uq1sj9iomno/okA9TmNk4SuJCG jtporRgtRo/h6dBdo4BEoFRRuhF0djq31/dHAMtdmhrvJQuCjxfWDKXduOjsmaIddEWe JQHXWh4dksnm1oRBSFPSr7+B7vQbmiJ1GjCoKSPMhyc/E54TrUmXX1LSPWgx9hkIgjY7 M8wQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784131087; x=1784735887; h=content-transfer-encoding:content-type:in-reply-to:references:cc:to :subject:from:user-agent:mime-version:date:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=5jqq9tpaX/pC62ALfZOmXiDQ0y4ecS0xGD3vn+9oows=; b=USdZsMmVHLQYiWkzRs9+hpN8vG0yFiAV7ywXGepOG8WqrIl1y7S34sSxefVI5Y/pxo vOB/yH6+i/WvK05TF8p/et9WmRKSFHEBFJJo20PI6t7Dvm/+FoKx4plawcey81g+SLj0 aQ59U8XVballKqhk33u+BZiTunJeHbo0jb/w05NxhExy2Lby5RDVwRAG4F2WrA6SzZut YCuWVLCR6cHTiB/KHTDRULHNMlDruLHXmpXEb/pegYZetTThvMETOB/lvmXtYvcVU3NX Oa0ELCPZGGGV/oz2Szh6STN/kveQ6A0JGRs1dtGHd0HsTUz1w5MNvV7c9ifml2LPPksV Upjg== X-Gm-Message-State: AOJu0Yzxy6T6AGYmjq6HSUb+6w+XNrv1SVEpSgbVtPSp0qlXZXJ2BNfK EXP3qrQp+OLAfFETqa++HAd5TluftXFDmNEkxYMj4T5qtMOIvAMwkGby X-Gm-Gg: AfdE7clCpZ6i/g20TwZxaR6hsZJ77loQJrwpFhvcPes3ldFllLoSnqDyg8P3lcqzM0u 7QcX5aM/YjzcfEsBi5wUnHkKbrmPNsADSQA1RF+BIPK5UbXV8rBqzej72X5xRmIEqt9dkBucdyh oZwZl/UWlfYyNTdADp29WmW6TZA0aFgBEWvGlqG+YuVug2tzmAFSSUiBRBxW/cosB3l/gvMYCQZ iVUGe7dMA/+bhCi5UZ9KcxsrhOD1YdjbrIGLcdGhnh11bRiOfar8KtG9dWIaiZxl4uALrHdmmmY 64t7D0slpJC+sEVS8HnkT+MdI8N3SYej4Msu19bVddvBJanYAY0WRa40N4vAK7LNU70gEi2c8NA WKmr1HxyCpQxeH9DRiQfOkUdv59tbqxcOqeFd/AaONwjnkyY66a9URXzr0VyJknNP9YJLsCH0bI 2lA6qOQNM4V8K6qrLHsbTknpM= X-Received: by 2002:a17:903:38c4:b0:2c9:97a7:71b1 with SMTP id d9443c01a7336-2cef14bcb92mr73814315ad.44.1784131087288; Wed, 15 Jul 2026 08:58:07 -0700 (PDT) Received: from [127.0.0.1] ([178.208.188.113]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2cf100e3920sm5130685ad.5.2026.07.15.08.58.03 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 15 Jul 2026 08:58:06 -0700 (PDT) Message-ID: <77d6da3d-10af-49a1-a356-72aa8b462e85@gmail.com> Date: Wed, 15 Jul 2026 23:57:58 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: =?UTF-8?B?TW96aWxsYSBUaHVuZGVyYmlyZCDmtYvor5XniYg=?= From: Jihan LIN Subject: Re: [PATCH RFC 09/13] mm/swap: add priority queue for swap device allocation To: kasong@tencent.com, linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Andrew Morton , Chris Li , Nhat Pham , Baoquan He , Barry Song , Youngjun Park , Kemeng Shi References: <20260714-swap-pcp-priq-v1-0-de9b164ed419@tencent.com> <20260714-swap-pcp-priq-v1-9-de9b164ed419@tencent.com> In-Reply-To: <20260714-swap-pcp-priq-v1-9-de9b164ed419@tencent.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Hi Kairui, Thanks for this series! I noticed a few issues in this patch: [...] >+static void swap_queue_install_readers(struct swap_queue_reader __percpu *readers) >+{ >+ int ring_idx, cpu; >+ struct swap_prio_ring *ring; >+ >+ free_percpu(swap_queue_readers); >+ swap_queue_readers = readers; >+ if (!readers) >+ return; >+ >+ /* Distribute each CPU's swap IO fairly across devices. */ >+ for_each_possible_cpu(cpu) { >+ local_lock_init(&per_cpu_ptr(readers, cpu)->lock); >+ >+ for (ring_idx = 0; ring_idx < swap_queue_len; ring_idx++) { >+ ring = swap_queue[ring_idx]; >+ per_cpu_ptr(readers, cpu)->ri[ring_idx].offset = >+ cpu % ring->size; >+ } >+ } >+} >+static struct swap_info_struct *swap_queue_get_device(long nr_alloc, int nr_iter) >+{ >+ bool rotate = 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 = 0; >+ while (nr_iter >= swap_queue[queue_idx]->size) { >+ nr_iter -= swap_queue[queue_idx]->size; >+ if (++queue_idx >= swap_queue_len) >+ return ERR_PTR(-ENOENT); >+ } >+ >+ ring = swap_queue[queue_idx]; >+ local_lock(&swap_queue_readers->lock); >+ ri = this_cpu_ptr(&swap_queue_readers->ri[queue_idx]); >+ /* Rotate while iterating the ring, just not on the first try */ >+ if (nr_iter) >+ rotate = true; >+ else if (ri->rr_counter < nr_alloc) >+ rotate = true; >+ else if (ri->offset >= ring->size) >+ rotate = true; >+ if (rotate) { >+ ri->offset++; >+ ri->offset %= ring->size; >+ ri->rr_counter = SWAP_ROUND_ROBIN_QUOTA; >+ } >+ ri->rr_counter -= nr_alloc; ri->rr_counter is left at 0 so the first allocation on each CPU hits ri->rr_counter < nr_alloc and rotates, shifting offset away from intended cpu % ring->size start. Initializing rr_counter to SWAP_ROUND_ROBIN_QUOTA would avoid this, or is this initial rotation perhaps intentional? [...] > @@ -1245,12 +1580,12 @@ static void add_to_avail_list(struct swap_info_struct *si) > unsigned long pages; > > spin_lock(&swap_avail_lock); >+ spin_lock(&swap_queue_update_lock); > > /* >- * Add the device to the avail list if SWP_WRITEOK is set and >- * SWAP_USAGE_OFFLIST_BIT is still set. Swapoff clears >- * SWP_WRITEOK first, so the device won't be re-added after >- * swapoff starts unless swap_device_enable resurrects it. >+ * Mark the device as avail if SWP_WRITEOK is set. Swapoff clears >+ * SWP_WRITEOK first, so check that first so the device won't be >+ * re-added after swapoff started. > */ > if (!(si->flags & SWP_WRITEOK)) > goto skip; >@@ -1261,9 +1596,10 @@ static void add_to_avail_list(struct swap_info_struct *si) > val = atomic_long_fetch_and_relaxed(~SWAP_USAGE_OFFLIST_BIT, &si->inuse_pages); > > /* >- * When device is full and device is on the plist, only one updater will >- * see (inuse_pages == si->pages) and will call del_from_avail_list. If >- * that updater happen to be here, just skip adding. >+ * When device is full and marked as available, one reader will see >+ * (inuse_pages == si->pages) and should mark it as unavailable and >+ * set SWAP_USAGE_OFFLIST_BIT. If that updater happens to be here, just >+ * skip the rest. > */ > pages = si->pages; > if (val == pages) { >@@ -1273,10 +1609,10 @@ static void add_to_avail_list(struct swap_info_struct *si) > goto skip; > } > >+ swap_queue_unmask(si); > plist_add(&si->avail_list, &swap_avail_head); >- > skip: >- spin_unlock(&swap_avail_lock); >+ spin_unlock(&swap_queue_update_lock); > } swap_avail_lock is acquired but never released when add_to_avail_list() returns. [...] >@@ -3700,6 +4035,12 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) > > /* Sets SWP_WRITEOK, resurrect the percpu ref, expose the swap device */ > percpu_ref_resurrect(&si->users); >+ percpu_down_write(&swapon_rwsem); >+ error = swap_queue_add(si); >+ percpu_up_write(&swapon_rwsem); >+ if (error) >+ goto free_swap_zswap; >+ > swap_device_enable(si); The swapfile is inserted into the ring unmasked before SWP_WRITEOK was set in swap_device_enable(). Allocators can pick it and get -EBUSY from get_swap_device_info(). How about adding masked and then letting swap_device_enable() unmask the new swapfile? --- Thanks! Jihan LIN