From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D1FC0C021B2 for ; Tue, 25 Feb 2025 04:01:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 623E3280003; Mon, 24 Feb 2025 23:01:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5AC73280002; Mon, 24 Feb 2025 23:01:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 427C9280003; Mon, 24 Feb 2025 23:01:53 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 1F4A8280002 for ; Mon, 24 Feb 2025 23:01:53 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id A0B37120814 for ; Tue, 25 Feb 2025 04:01:52 +0000 (UTC) X-FDA: 83157118464.03.B0E8B62 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf10.hostedemail.com (Postfix) with ESMTP id BAC61C000D for ; Tue, 25 Feb 2025 04:01:50 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=WwDtW9Mk; spf=pass (imf10.hostedemail.com: domain of bhe@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=bhe@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1740456110; a=rsa-sha256; cv=none; b=0DquvL+ejkDaN0JBD/He5eiUSxWXtijIr5m5rOhpQQOhYGyfVg425eh9EIeVaFNZ38GfYs J+3ixL8eWn7ntaXKfjOB2nrozkuBct1nHwd35fd+VRxlVZEeDWOMEZfvygVlPJDjzD0Sfo sxjy5un3pCS1PDxDFODOMqoqlLJz3l8= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=WwDtW9Mk; spf=pass (imf10.hostedemail.com: domain of bhe@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=bhe@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1740456110; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=aJCEL/JcHod2gpRQmwl4T/ssU5PjyZ46YyekLHWBr0Y=; b=jo1IY6WA6vDxZn2HPFy5tZCxI3Q5UMjCb4azz/LkNemuuQ+z+ruCcUMujTK3aEf0LmOstq yL28SCF5axN3208Pwvx+qw6Z1AZLX/5PufyUZBYFzT4Rfz5cPszbNZ88gixZQtbAKuUgkr n8yCPnLkH8r8gtBVDGqOvUKqYg4Sfgk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740456110; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=aJCEL/JcHod2gpRQmwl4T/ssU5PjyZ46YyekLHWBr0Y=; b=WwDtW9MkzL0+vcP5nrlGvYiHN0FoZG1xXL4IU2qMVA0Fvx+kjL3swt0OsxYrP7cionlWSv omGzdYvOaLlPFeAmnTmDdDqslk4jFHD/06g/M9PbW3HbRcGZRpo4XSsa0gAusI/jBZMMJM O8t82oqqL+ITOQCUX1Z4hKMrrI2DsSQ= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-607-YMRLKxqsPpab-5VKbCcA0Q-1; Mon, 24 Feb 2025 23:01:43 -0500 X-MC-Unique: YMRLKxqsPpab-5VKbCcA0Q-1 X-Mimecast-MFC-AGG-ID: YMRLKxqsPpab-5VKbCcA0Q_1740456101 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 608F5180034A; Tue, 25 Feb 2025 04:01:40 +0000 (UTC) Received: from localhost (unknown [10.72.112.127]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id DCAFB180035E; Tue, 25 Feb 2025 04:01:37 +0000 (UTC) Date: Tue, 25 Feb 2025 12:01:32 +0800 From: Baoquan He To: Kairui Song Cc: linux-mm@kvack.org, Andrew Morton , Chris Li , Barry Song , Hugh Dickins , Yosry Ahmed , "Huang, Ying" , Nhat Pham , Johannes Weiner , Baolin Wang , Kalesh Singh , Matthew Wilcox , linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 5/7] mm, swap: use percpu cluster as allocation fast path Message-ID: References: <20250224180212.22802-1-ryncsn@gmail.com> <20250224180212.22802-6-ryncsn@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20250224180212.22802-6-ryncsn@gmail.com> X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 X-Rspamd-Queue-Id: BAC61C000D X-Stat-Signature: 6sf7zx847ie58tbdodfb1uf7pfcqfrcz X-Rsp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jP9zkhatXGWh9++kRRhdg4581mke1D8JXqIEYCzfGOhvf1JdQcS4+6104UQ9K+OtF/8M1Xm7XyEa8d331Fbn1eNHo9QFVntbBiiCK6mFDps7CZnJw+uywWOUJbn4C9E6o8YeefLclUe0ixnRjcSOgmKtCpH6TuyT+yFwp7tYRH8PCouX2mlJOnVh78lQ0MvxOHJjoKv3hhkj19TMRnCmnbFEkmbgKcry+iYtXpalng+e77kmMs8sMZPV47nUjOmxfMADXkKtNgctnhGD4+98sJR/aknXV+LQsWne+jijPj6oMp6nzVnPZL9tJIOALHb++8BBbMAu7sge8u7jl2fv2cAcgVgHKewuz2/JwmZf6PdoJrjeihxmgyEDbtQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On 02/25/25 at 02:02am, Kairui Song wrote: ......snip... > @@ -1204,19 +1252,36 @@ int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_order) > int n_ret = 0; > int node; > > + /* Fast path using percpu cluster */ > + local_lock(&percpu_swap_cluster.lock); > + n_ret = swap_alloc_fast(swp_entries, > + SWAP_HAS_CACHE, > + order, n_goal); Since we always pass in the SWAP_HAS_CACHE for the 2nd parameter, we can hard code it inside fucntion swap_alloc_fast(), unless there will be other callsite where different value is passed in for 'usage'. And the function 2nd parameter of swap_alloc_slow() too. Anyway, it's not a big deal, could be my personal peference. Other than this nit, this looks good to me. > + if (n_ret == n_goal) > + goto out; > + > + n_goal = min_t(int, n_goal - n_ret, SWAP_BATCH); > + /* Rotate the device and switch to a new cluster */ > spin_lock(&swap_avail_lock); > start_over: > node = numa_node_id(); > plist_for_each_entry_safe(si, next, &swap_avail_heads[node], avail_lists[node]) { > - /* requeue si to after same-priority siblings */ > plist_requeue(&si->avail_lists[node], &swap_avail_heads[node]); > spin_unlock(&swap_avail_lock); > if (get_swap_device_info(si)) { > - n_ret = scan_swap_map_slots(si, SWAP_HAS_CACHE, > - n_goal, swp_entries, order); > + /* > + * For order 0 allocation, try best to fill the request > + * as it's used by slot cache. > + * > + * For mTHP allocation, it always have n_goal == 1, > + * and falling a mTHP swapin will just make the caller > + * fallback to order 0 allocation, so just bail out. > + */ > + n_ret += scan_swap_map_slots(si, SWAP_HAS_CACHE, n_goal, > + swp_entries + n_ret, order); > put_swap_device(si); > if (n_ret || size > 1) > - goto check_out; > + goto out; > } > > spin_lock(&swap_avail_lock); > @@ -1234,12 +1299,10 @@ int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_order) > if (plist_node_empty(&next->avail_lists[node])) > goto start_over; > } > - > spin_unlock(&swap_avail_lock); > - > -check_out: > +out: > + local_unlock(&percpu_swap_cluster.lock); > atomic_long_sub(n_ret * size, &nr_swap_pages); > - > return n_ret; > } > > @@ -2725,8 +2788,6 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) > arch_swap_invalidate_area(p->type); > zswap_swapoff(p->type); > mutex_unlock(&swapon_mutex); > - free_percpu(p->percpu_cluster); > - p->percpu_cluster = NULL; > kfree(p->global_cluster); > p->global_cluster = NULL; > vfree(swap_map); > @@ -3125,7 +3186,7 @@ static struct swap_cluster_info *setup_clusters(struct swap_info_struct *si, > unsigned long nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER); > struct swap_cluster_info *cluster_info; > unsigned long i, j, idx; > - int cpu, err = -ENOMEM; > + int err = -ENOMEM; > > cluster_info = kvcalloc(nr_clusters, sizeof(*cluster_info), GFP_KERNEL); > if (!cluster_info) > @@ -3134,20 +3195,7 @@ static struct swap_cluster_info *setup_clusters(struct swap_info_struct *si, > for (i = 0; i < nr_clusters; i++) > spin_lock_init(&cluster_info[i].lock); > > - if (si->flags & SWP_SOLIDSTATE) { > - si->percpu_cluster = alloc_percpu(struct percpu_cluster); > - if (!si->percpu_cluster) > - goto err_free; > - > - for_each_possible_cpu(cpu) { > - struct percpu_cluster *cluster; > - > - cluster = per_cpu_ptr(si->percpu_cluster, cpu); > - for (i = 0; i < SWAP_NR_ORDERS; i++) > - cluster->next[i] = SWAP_ENTRY_INVALID; > - local_lock_init(&cluster->lock); > - } > - } else { > + if (!(si->flags & SWP_SOLIDSTATE)) { > si->global_cluster = kmalloc(sizeof(*si->global_cluster), > GFP_KERNEL); > if (!si->global_cluster) > @@ -3424,8 +3472,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) > bad_swap_unlock_inode: > inode_unlock(inode); > bad_swap: > - free_percpu(si->percpu_cluster); > - si->percpu_cluster = NULL; > kfree(si->global_cluster); > si->global_cluster = NULL; > inode = NULL; > -- > 2.48.1 >