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 8E8A7C3DA59 for ; Mon, 22 Jul 2024 06:45:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 06FEC6B0083; Mon, 22 Jul 2024 02:45:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E9DB26B0085; Mon, 22 Jul 2024 02:45:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CC90E6B0088; Mon, 22 Jul 2024 02:45:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id AF12E6B0083 for ; Mon, 22 Jul 2024 02:45:02 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 70D9F121520 for ; Mon, 22 Jul 2024 06:45:02 +0000 (UTC) X-FDA: 82366451244.27.8F54EB6 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf16.hostedemail.com (Postfix) with ESMTP id AC50918000B for ; Mon, 22 Jul 2024 06:44:59 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=pZchxHqd; spf=pass (imf16.hostedemail.com: domain of vbabka@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=vbabka@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721630677; a=rsa-sha256; cv=none; b=ezynORrF7C2USURYMErWKSZcYQpeGyJUNXr675e4HqumwqL0bWrjRPx8a6eM1NXH9bYOry DziPXeQC3Ua93jugxRgCewh676L/PWDBH1uhRUKl7vg9/hyt1UKh9k0P4jW1VPTp4eDvF6 lJDW25vhtxQ4SJqbng5AtJInOAE5H6U= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=pZchxHqd; spf=pass (imf16.hostedemail.com: domain of vbabka@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=vbabka@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721630677; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=hqbLzWBG08nxbnF45qSkfGYEmY/EhoDzJsyBcArqN3k=; b=l4YnIMGNyrljTjYVYc9k+Drdwhvx9rcPlhdPU5vs5qwjTjM9qp5mu5VZwfiH5yXjrxiMp7 655fJSn75Wr66RjQPMOIJw+8GUi9cWXZBOz78T6Snr8NJFQwHhLRutM15RZWI58mkhljZO Bf/RBf7V0ElRcBU+l3p8i2IeaWQNsyY= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 7765F60ADE; Mon, 22 Jul 2024 06:44:58 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E2E6FC116B1; Mon, 22 Jul 2024 06:44:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721630698; bh=imk9qeSKssMP43EivBkJbYhgn7TTBeSWRGr8uYPHarE=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=pZchxHqdqBUFU9p0ToSXaXNKrQ04OA3kBhc70bnnMvMvAJCCDRgY2zmKIDIVAVm7y eXPKKDpImJ+ItIfLET+7e5Ts4AIjyzuRve/+juai4oRscrh99YpcH25x+J6+jQIhXQ kYad+nN1HaDiQklfucwBG4kDcKi959669FZul7rpldzfCc9lS2OoH+WHxsJ/gGWVXw p3JV/nVTvt0QY/IIIYTGBpgGkhgkj+MH4oWD5jOT6hzRCWNrcmXTqMYRo9XvWYllRb 13zxqdCICGJNgA5DZHXOTvIad6zCmnJpSfdCRhtWzCtz9YIKa2e+PAzHpNRg4CSmzN TxBUmtkrcYiJw== Message-ID: <8323327f-3386-48ba-8554-10a5a6d12a04@kernel.org> Date: Mon, 22 Jul 2024 08:44:54 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2] mm/page_alloc: Fix pcp->count race between drain_pages_zone() vs __rmqueue_pcplist() To: Li Zhijian , linux-mm@kvack.org Cc: akpm@linux-foundation.org, linux-kernel@vger.kernel.org, Yasunori Gotou , David Hildenbrand , Yao Xingtao References: <20240722021059.1076399-1-lizhijian@fujitsu.com> Content-Language: en-US From: "Vlastimil Babka (SUSE)" In-Reply-To: <20240722021059.1076399-1-lizhijian@fujitsu.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Stat-Signature: yp63zqpyjf6snrdsc5i4nz791ys4d14d X-Rspamd-Queue-Id: AC50918000B X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1721630699-677914 X-HE-Meta: U2FsdGVkX18N+1yrjYemEJGxiBhKamkxVtFXDXZ2FmqNGdigLtfC2cJ0E1plzzH0kQMIo+I0R+0DwBR8/L8e6xia/1djUBmPdwOESUvRv1d8DNpzjiJDHwYjMBIU1dSr51Qw+BL6GYiSvaJnHdfR7eT/5sUgMmI8HUU7vm7yHDL1CsMMAVO0xmWWNhbQsrlKyCOscurRbDH9daen1SpGVbCbUo+3Kex3SINea5DLE6v2L/NZRGH6dScEamR4rhPiMoSY+reeFL8G7Rw+lRudnpAtucV81j7mJ/Rb0VjbxFNw2uShUQeHfa9K33xAZMuS6R9++Pgrq8LbT4mvL2erN14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m@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On 7/22/24 4:10 AM, Li Zhijian wrote: > It's expected that no page should be left in pcp_list after calling > zone_pcp_disable() in offline_pages(). Previously, it's observed that > offline_pages() gets stuck [1] due to some pages remaining in pcp_list. > > Cause: > There is a race condition between drain_pages_zone() and __rmqueue_pcplist() > involving the pcp->count variable. See below scenario: > > CPU0 CPU1 > ---------------- --------------- > spin_lock(&pcp->lock); > __rmqueue_pcplist() { > zone_pcp_disable() { > /* list is empty */ > if (list_empty(list)) { > /* add pages to pcp_list */ > alloced = rmqueue_bulk() > mutex_lock(&pcp_batch_high_lock) > ... > __drain_all_pages() { > drain_pages_zone() { > /* read pcp->count, it's 0 here */ > count = READ_ONCE(pcp->count) > /* 0 means nothing to drain */ > /* update pcp->count */ > pcp->count += alloced << order; > ... > ... > spin_unlock(&pcp->lock); > > In this case, after calling zone_pcp_disable() though, there are still some > pages in pcp_list. And these pages in pcp_list are neither movable nor > isolated, offline_pages() gets stuck as a result. > > Solution: > Expand the scope of the pcp->lock to also protect pcp->count in > drain_pages_zone(), to ensure no pages are left in the pcp list after > zone_pcp_disable() > > [1] https://lore.kernel.org/linux-mm/6a07125f-e720-404c-b2f9-e55f3f166e85@fujitsu.com/ > > Cc: David Hildenbrand > Cc: Vlastimil Babka (SUSE) > Reported-by: Yao Xingtao > Signed-off-by: Li Zhijian Can we find a breaking commit for Fixes: ? > --- > V2: > - Narrow down the scope of the spin_lock() to limit the draining latency. # Vlastimil and David > - In above scenario, it's sufficient to read pcp->count once with lock held, and it fully fixed > my issue[1] in thounds runs(It happened in more than 5% before). That should be ok indeed, but... > RFC: > https://lore.kernel.org/linux-mm/20240716073929.843277-1-lizhijian@fujitsu.com/ > --- > mm/page_alloc.c | 5 ++++- > 1 file changed, 4 insertions(+), 1 deletion(-) > > diff --git a/mm/page_alloc.c b/mm/page_alloc.c > index 9ecf99190ea2..5388a35c4e9c 100644 > --- a/mm/page_alloc.c > +++ b/mm/page_alloc.c > @@ -2323,8 +2323,11 @@ void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp) > static void drain_pages_zone(unsigned int cpu, struct zone *zone) > { > struct per_cpu_pages *pcp = per_cpu_ptr(zone->per_cpu_pageset, cpu); > - int count = READ_ONCE(pcp->count); > + int count; > > + spin_lock(&pcp->lock); > + count = pcp->count; > + spin_unlock(&pcp->lock); > while (count) { > int to_drain = min(count, pcp->batch << CONFIG_PCP_BATCH_SCALE_MAX); > count -= to_drain; It's wasteful to do a lock/unlock cycle just to read the count. It could rather look something like this: while (true) spin_lock(&pcp->lock); count = pcp->count; ... count -= to_drain; if (to_drain) drain_zone_pages(...) ... spin_unlock(&pcp->lock); if (count) break;