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 CA4EEC54E64 for ; Fri, 22 Mar 2024 23:39:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4F3CF6B0088; Fri, 22 Mar 2024 19:39:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 47D1B6B0089; Fri, 22 Mar 2024 19:39:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2F6376B0092; Fri, 22 Mar 2024 19:39:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 18BFE6B0088 for ; Fri, 22 Mar 2024 19:39:01 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id DAD8D14065C for ; Fri, 22 Mar 2024 23:39:00 +0000 (UTC) X-FDA: 81926292840.16.377EB8C Received: from mail-vk1-f174.google.com (mail-vk1-f174.google.com [209.85.221.174]) by imf08.hostedemail.com (Postfix) with ESMTP id 1992A160013 for ; Fri, 22 Mar 2024 23:38:58 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=hKEaNDRM; spf=pass (imf08.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.221.174 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711150739; 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=/2oZiuP4eJFJqX5LDfAjGop+aH7uad0slclSBu+40bY=; b=oAlhnYElKKeMqqWDciCrwGcev51FaddcMbAWUFrDDoDquB64mqZr/rty+sGLyYxqFgCgkM zw3O9nZQchmVlZfJ2yPqYPRxhkiF2ixW1w/04a/ZWCGhB4/KADqVH11TtfhYHW7CClR/cL om1/G424gKZR1wdjRtm/gQmri2MtGLM= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=hKEaNDRM; spf=pass (imf08.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.221.174 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711150739; a=rsa-sha256; cv=none; b=8PfS/GmwrkStQxgSvWQcfHx3Xbjn95yx7fnt5oqszpMLb4Yx4wb66uDa4gqh2lQ467FzDM AwsQDEFXX3lFPVMLBWkRD2FI7X2V30udfaqTV0ikglJmiZkhRbEFCMrfnC5Bq94ECcXWaB 9CxAQB4VZIR6miJ3ai79BgHOVVmZasA= Received: by mail-vk1-f174.google.com with SMTP id 71dfb90a1353d-4d44beade3cso1164607e0c.1 for ; Fri, 22 Mar 2024 16:38:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711150738; x=1711755538; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=/2oZiuP4eJFJqX5LDfAjGop+aH7uad0slclSBu+40bY=; b=hKEaNDRMDaCMZm597jxFX/uLxax9DYmk5dAZjo8/CBXFMb3AogKQv1CLQNIXrmnw2o viJ19upWZhMH6aS7TDrJqak/FLoq4NSiyxQyFYM0OAQIk4E7erIavvWfvw5VYfmQacDL X8+eITHzFAW1F0QewDkDHTQUbsskzNcNBzHL3IAIUeJ3NYNPF8mZnCz6z8CuGZQ3s7Sq rpb1tCYOb64650M4DsAS/XuLD7lQWp0UlAmOP6Km2lo3gRen0E7fpLbPEdop+uWrm3Wf mn14/byyhi+O5VDJbdHSxrOft4gJatmg0ANxQazq8/O4tz543iCd8u7bd6iKtcvfRJsN 4blw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711150738; x=1711755538; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/2oZiuP4eJFJqX5LDfAjGop+aH7uad0slclSBu+40bY=; b=SB3a1PijxeGAdftbgLW/Xt5VT9O0i1YjV5OQm0OkwoAndmYKbUDaD1iNa1a+PUsEkj x5c7THZTIrI0ODmhyEk2VmLvAqRLKkBWLbTTuFQjlReLjG9ph/MVyRqFLIFHUjUg84O6 loz6zvee1IASubc/B3DdHNX7DS2vw050qysFGrNiIfO/JQa0NYU59mssDaVjq1XMjHVA EFsd/meTdps54QIywP++JZJGi0IIsH9j9HrcwsQu4iSUdVU1/y4H8Y213NmxFj1xpnqh vAYG+L+WeNHhnII+MV4UcsuntMKMrO7Fn9ipQofOSwiHGaNRyg2mQ2x5pC9Z16e5zPi4 J/gg== X-Forwarded-Encrypted: i=1; AJvYcCUYNZnuSzLtygaoMsCrJ37KBPhD2dcgUCwz79ab/i1PUklj4IBgQJiGEaaUk+oiYLcXqiC68RLizBK3ug/4Sa8fBYo= X-Gm-Message-State: AOJu0YyvVolKAElGHwvCehMpMGGoCg7NGuhd+RETkS5C9PXBMV+ZAvBx XgJz9oN2Lzt+fCcACF6imFWzFDRFKvWiGFxRSZ5qt+BKmI1ZvFm/SVcNrl7uIXcrnMSOSbnct+z mxyvjjvUkHQxK/A0Op2qsqhpEtj8= X-Google-Smtp-Source: AGHT+IFIwc6UVKQK7agHQWdizVXgFmBzEUsAxPOYgSTUyo0phNdEn5cbytslxNKLflQb8cn/v4cbB+Aq0ZHHcZkVVh0= X-Received: by 2002:a05:6122:4d1a:b0:4d3:34b1:7211 with SMTP id fi26-20020a0561224d1a00b004d334b17211mr1285910vkb.3.1711150737989; Fri, 22 Mar 2024 16:38:57 -0700 (PDT) MIME-Version: 1.0 References: <01b0b8e8-af1d-4fbe-951e-278e882283fd@linux.dev> In-Reply-To: From: Barry Song <21cnbao@gmail.com> Date: Sat, 23 Mar 2024 12:38:46 +1300 Message-ID: Subject: Re: [External] Re: [bug report] mm/zswap :memory corruption after zswap_load(). To: Yosry Ahmed Cc: Zhongkun He , Chengming Zhou , Johannes Weiner , Andrew Morton , linux-mm , wuyun.abel@bytedance.com, zhouchengming@bytedance.com, Nhat Pham , Kairui Song , Minchan Kim , David Hildenbrand , Chris Li , Ying Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 1992A160013 X-Rspam-User: X-Stat-Signature: ngt6k5sy67a1ccdmhsytrt47aw5o7kif X-Rspamd-Server: rspam01 X-HE-Tag: 1711150738-143436 X-HE-Meta: U2FsdGVkX18t8mshUYs0bFMfI/HL/nR4NsTgKdnJ6zc0aQxxQm+a/lwww9+jJPiGQs9tbVYGZ+VRPqj+5T6X4vZPdpYtTEF/TKG5NYFjFiLyaw1C3HnPnZjAlAJLhtvlpy/buIH9A2FtcyATek03fsoqJzy/ae/Hzv98VkjtjKucI9t58bsX/WK98oN1Jc/yRJ7AXBZVNVLlSNZEj7TegctpHl4fb8Do+o7RvRF26WH65CTMPQduo3mGmzRfUtaQX3iVKKX2QrT/bWf0uUESZQKmXPR6jHqNkq+xsplQCsH6SbQB6YOJ/O9dQRIXRne0IiuA12ihOOsLvyH5B6N8P+ssVyO9ZAFgjxi+Zc3DAtQ4KUVYjYCNVzfhLV8l/J1OR7g9o1QISFaPuezpDnYHGlcsxYUC1w26sc0kqSfRjQNTBDG8VNzMpv+axn3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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 Sat, Mar 23, 2024 at 12:35=E2=80=AFPM Yosry Ahmed wrote: > > On Fri, Mar 22, 2024 at 4:32=E2=80=AFPM Barry Song <21cnbao@gmail.com> wr= ote: > > > > On Sat, Mar 23, 2024 at 12:23=E2=80=AFPM Yosry Ahmed wrote: > > > > > > On Fri, Mar 22, 2024 at 4:18=E2=80=AFPM Barry Song <21cnbao@gmail.com= > wrote: > > > > > > > > On Sat, Mar 23, 2024 at 12:09=E2=80=AFPM Yosry Ahmed wrote: > > > > > > > > > > On Fri, Mar 22, 2024 at 4:04=E2=80=AFPM Barry Song <21cnbao@gmail= .com> wrote: > > > > > > > > > > > > On Sat, Mar 23, 2024 at 8:35=E2=80=AFAM Yosry Ahmed wrote: > > > > > > > > > > > > > > On Thu, Mar 21, 2024 at 8:04=E2=80=AFPM Zhongkun He > > > > > > > wrote: > > > > > > > > > > > > > > > > On Thu, Mar 21, 2024 at 5:29=E2=80=AFPM Chengming Zhou wrote: > > > > > > > > > > > > > > > > > > On 2024/3/21 14:36, Zhongkun He wrote: > > > > > > > > > > On Thu, Mar 21, 2024 at 1:24=E2=80=AFPM Chengming Zhou = wrote: > > > > > > > > > >> > > > > > > > > > >> On 2024/3/21 13:09, Zhongkun He wrote: > > > > > > > > > >>> On Thu, Mar 21, 2024 at 12:42=E2=80=AFPM Chengming Zh= ou > > > > > > > > > >>> wrote: > > > > > > > > > >>>> > > > > > > > > > >>>> On 2024/3/21 12:34, Zhongkun He wrote: > > > > > > > > > >>>>> Hey folks, > > > > > > > > > >>>>> > > > > > > > > > >>>>> Recently, I tested the zswap with memory reclaiming= in the mainline > > > > > > > > > >>>>> (6.8) and found a memory corruption issue related t= o exclusive loads. > > > > > > > > > >>>> > > > > > > > > > >>>> Is this fix included? 13ddaf26be32 ("mm/swap: fix ra= ce when skipping swapcache") > > > > > > > > > >>>> This fix avoids concurrent swapin using the same swa= p entry. > > > > > > > > > >>>> > > > > > > > > > >>> > > > > > > > > > >>> Yes, This fix avoids concurrent swapin from different= cpu, but the > > > > > > > > > >>> reported issue occurs > > > > > > > > > >>> on the same cpu. > > > > > > > > > >> > > > > > > > > > >> I think you may misunderstand the race description in = this fix changelog, > > > > > > > > > >> the CPU0 and CPU1 just mean two concurrent threads, no= t real two CPUs. > > > > > > > > > >> > > > > > > > > > >> Could you verify if the problem still exists with this= fix? > > > > > > > > > > > > > > > > > > > > Yes=EF=BC=8CI'm sure the problem still exists with this= patch. > > > > > > > > > > There is some debug info, not mainline. > > > > > > > > > > > > > > > > > > > > bpftrace -e'k:swap_readpage {printf("%lld, %lld,%ld,%ld= ,%ld\n%s", > > > > > > > > > > ((struct page *)arg0)->private,nsecs,tid,pid,cpu,kstack= )}' --include > > > > > > > > > > linux/mm_types.h > > > > > > > > > > > > > > > > > > Ok, this problem seems only happen on SWP_SYNCHRONOUS_IO = swap backends, > > > > > > > > > which now include zram, ramdisk, pmem, nvdimm. > > > > > > > > > > > > > > > > Yes. > > > > > > > > > > > > > > > > > > > > > > > > > > It maybe not good to use zswap on these swap backends? > > > > > > > > > > > > > > > > > > The problem here is the page fault handler tries to skip = swapcache to > > > > > > > > > swapin the folio (swap entry count =3D=3D 1), but then it= can't install folio > > > > > > > > > to pte entry since some changes happened such as concurre= nt fork of entry. > > > > > > > > > > > > > > > > > > > > > > > > > The first page fault returned VM_FAULT_RETRY because > > > > > > > > folio_lock_or_retry() failed. > > > > > > > > > > > > > > How so? The folio is newly allocated and not visible to any o= ther > > > > > > > threads or CPUs. swap_read_folio() unlocks it and then return= s and we > > > > > > > immediately try to lock it again with folio_lock_or_retry(). = How does > > > > > > > this fail? > > > > > > > > > > > > > > Let's go over what happens after swap_read_folio(): > > > > > > > - The 'if (!folio)' code block will be skipped. > > > > > > > - folio_lock_or_retry() should succeed as I mentioned earlier= . > > > > > > > - The 'if (swapcache)' code block will be skipped. > > > > > > > - The pte_same() check should succeed on first look because o= ther > > > > > > > concurrent faulting threads should be held off by the newly i= ntroduced > > > > > > > swapcache_prepare() logic. But looking deeper I think this on= e may > > > > > > > fail due to a concurrent MADV_WILLNEED. > > > > > > > - The 'if (unlikely(!folio_test_uptodate(folio)))` part will = be > > > > > > > skipped because swap_read_folio() marks the folio up-to-date. > > > > > > > - After that point there is no possible failure until we inst= all the > > > > > > > pte, at which point concurrent faults will fail on !pte_same(= ) and > > > > > > > retry. > > > > > > > > > > > > > > So the only failure I think is possible is the pte_same() che= ck. I see > > > > > > > how a concurrent MADV_WILLNEED could cause that check to fail= . A > > > > > > > concurrent MADV_WILLNEED will block on swapcache_prepare(), b= ut once > > > > > > > the fault resolves it will go ahead and read the folio again = into the > > > > > > > swapcache. It seems like we will end up with two copies of th= e same > > > > > > > > > > > > but zswap has freed the object when the do_swap_page finishes s= wap_read_folio > > > > > > due to exclusive load feature of zswap? > > > > > > > > > > > > so WILLNEED will get corrupted data and put it into swapcache. > > > > > > some other concurrent new forked process might get the new data > > > > > > from the swapcache WILLNEED puts when the new-forked process > > > > > > goes into do_swap_page. > > > > > > > > > > Oh I was wondering how synchronization with WILLNEED happens with= out > > > > > zswap. It seems like we could end up with two copies of the same = folio > > > > > and one of them will be leaked unless I am missing something. > > > > > > > > > > > > > > > > > so very likely a new process is forked right after do_swap_page= finishes > > > > > > swap_read_folio and before swapcache_clear. > > > > > > > > > > > > > folio? Maybe this is harmless because the folio in the swacac= he will > > > > > > > never be used, but it is essentially leaked at that point, ri= ght? > > > > > > > > > > > > > > I feel like I am missing something. Adding other folks that w= ere > > > > > > > involved in the recent swapcache_prepare() synchronization th= read. > > > > > > > > > > > > > > Anyway, I agree that at least in theory the data corruption c= ould > > > > > > > happen because of exclusive loads when skipping the swapcache= , and we > > > > > > > should fix that. > > > > > > > > > > > > > > Perhaps the right thing to do may be to write the folio again= to zswap > > > > > > > before unlocking it and before calling swapcache_clear(). The= need for > > > > > > > the write can be detected by checking if the folio is dirty, = I think > > > > > > > this will only be true if the folio was loaded from zswap. > > > > > > > > > > > > we only need to write when we know swap_read_folio() gets data > > > > > > from zswap but not swapfile. is there a quick way to do this? > > > > > > > > > > The folio will be dirty when loaded from zswap, so we can check i= f the > > > > > folio is dirty and write the page if fail after swap_read_folio()= . > > > > > > > > Is it actually a bug of swapin_walk_pmd_entry? it only check pte > > > > before read_swap_cache_async. but when read_swap_cache_async > > > > is blocked by swapcache_prepare, after it gets the swapcache_prepar= e > > > > successfully , someone else should have already set the pte and fre= ed > > > > the swap slot even if this is not zswap? > > > > > > If someone freed the swap slot then swapcache_prepare() should fail, > > > but the swap entry could have been recycled after we dropped the pte > > > lock, right? > > > > > > Anyway, yeah, I think there might be a bug here irrelevant to zswap. > > > > > > > > > > > static int swapin_walk_pmd_entry(pmd_t *pmd, unsigned long start, > > > > unsigned long end, struct mm_walk *walk) > > > > { > > > > struct vm_area_struct *vma =3D walk->private; > > > > struct swap_iocb *splug =3D NULL; > > > > pte_t *ptep =3D NULL; > > > > spinlock_t *ptl; > > > > unsigned long addr; > > > > > > > > for (addr =3D start; addr < end; addr +=3D PAGE_SIZE) { > > > > pte_t pte; > > > > swp_entry_t entry; > > > > struct folio *folio; > > > > > > > > if (!ptep++) { > > > > ptep =3D pte_offset_map_lock(vma->vm_mm, pm= d, addr, &ptl); > > > > if (!ptep) > > > > break; > > > > } > > > > > > > > pte =3D ptep_get(ptep); > > > > if (!is_swap_pte(pte)) > > > > continue; > > > > entry =3D pte_to_swp_entry(pte); > > > > if (unlikely(non_swap_entry(entry))) > > > > continue; > > > > > > > > pte_unmap_unlock(ptep, ptl); > > > > ptep =3D NULL; > > > > > > > > folio =3D read_swap_cache_async(entry, GFP_HIGHUSER= _MOVABLE, > > > > vma, addr, &splug); > > > > if (folio) > > > > folio_put(folio); > > > > } > > > > > > > > if (ptep)c > > > > pte_unmap_unlock(ptep, ptl); > > > > swap_read_unplug(splug); > > > > cond_resched(); > > > > > > > > return 0; > > > > } > > > > > > > > I mean pte can become non-swap within read_swap_cache_async(), > > > > so no matter if it is zswap, we have the bug. > > > > checked again, probably still a zswap issue, as swapcache_prepare can = detect > > real swap slot free :-) > > > > /* > > * Swap entry may have been freed since our caller obse= rved it. > > */ > > err =3D swapcache_prepare(entry); > > if (!err) > > break; > > > > > > zswap exslusive load isn't a real swap free. > > > > But probably we have found the timing which causes the issue at least := -) > > The problem I was referring to is with the swapin fault path that > skips the swapcache vs. MADV_WILLNEED. The fault path could swapin the > page and skip the swapcache, and MADV_WILLNEED could swap it in again > into the swapcache. We would end up with two copies of the folio. right. i feel like we have to re-check pte is not changed within __read_swap_cache_async after swapcache_prepare succeed after being blocked for a while as the previous entry could have been freed and re-allocted by someone else - a completely different process. then we get read other processes' data.