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 BBA4DC54E64 for ; Fri, 22 Mar 2024 23:35:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 196086B008A; Fri, 22 Mar 2024 19:35:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 145996B008C; Fri, 22 Mar 2024 19:35:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F26E76B0092; Fri, 22 Mar 2024 19:35:27 -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 E2C1B6B008A for ; Fri, 22 Mar 2024 19:35:27 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id C011C1C14A5 for ; Fri, 22 Mar 2024 23:35:27 +0000 (UTC) X-FDA: 81926283894.07.25DF9A2 Received: from mail-lj1-f170.google.com (mail-lj1-f170.google.com [209.85.208.170]) by imf09.hostedemail.com (Postfix) with ESMTP id CB195140013 for ; Fri, 22 Mar 2024 23:35:25 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=lOYgx35l; spf=pass (imf09.hostedemail.com: domain of yosryahmed@google.com designates 209.85.208.170 as permitted sender) smtp.mailfrom=yosryahmed@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711150526; 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=BWVbgXkqT8zuvZRUiFVVMtFQRaANyf4huWv1ZPYsDbQ=; b=a6byPYzLHLpmvcuT70jVErk7qfkXH/Nlcn+kyt/6b/pp/MpXmE4zbqp6NT2UVWaEZAzLPv kYL1l9B32ZYTs1WGnB+8wjOkffcdDyNh6fJrJFxfpP0dqkt3a3BbZzPoLQGUvUmbWcD/im H54kAMU6/8/5ieM6d4HwXVFPoR4YtRI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711150526; a=rsa-sha256; cv=none; b=2iKpsGEHU7nZs2sDMRox2jGICXHQMhw/QU+ZVAhhX7GxHfJe/+W/4H3c2gCXw5laXVR1wA Q1INRKuBRhQ2gzTtsq250wK1XvrCTuOhGHk9ZAb2RXyKD6mSyUHuYHwgKyCIQNnkwSf2Ur /612680YNFzt1j6Zo3CYbjqZrILFzAw= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=lOYgx35l; spf=pass (imf09.hostedemail.com: domain of yosryahmed@google.com designates 209.85.208.170 as permitted sender) smtp.mailfrom=yosryahmed@google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-lj1-f170.google.com with SMTP id 38308e7fff4ca-2d28051376eso52143591fa.0 for ; Fri, 22 Mar 2024 16:35:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711150524; x=1711755324; 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=BWVbgXkqT8zuvZRUiFVVMtFQRaANyf4huWv1ZPYsDbQ=; b=lOYgx35lEo6f5uoMAi7+YjWMeyEqpIxFdYQEVRaocSFy6ojHrI/TctwAe1o7Tbf3Tg Iy5rzbz9D2f+jHcAV6qgn6AMfEF7LcCS8yTSxooEvsYTTVBAZ7vvKokirYvyzyWKYKV9 ikiPCfnyVN35fh70aBXiHZvFzDt6w80M+cjSkdVb6Ot+R7h1LSmT/7OnpqQ5HV18PhqI fHjZAvep9atL/DL/jg/OiQEdJ/XKG8MfIcda6PuMG18Js4q+Vr4rYluo2a00GjSwdyRH tSk2DfYz3S18dPYqQI/+vdQNSXFhxULf95V11mdDbt+06GtKhKBr5KArMvsCbDZ3Toqi +8QQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711150524; x=1711755324; 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=BWVbgXkqT8zuvZRUiFVVMtFQRaANyf4huWv1ZPYsDbQ=; b=IdoUUgYFkZdYuw6ExDlI7xTu5uMmtgUns78xuLzfqHnTO82W38bvGZX6Md7dTz2a++ UivTtbFc0/BEXjHXG+e/jkcpCm8lv8NBHbq+G2gSyOXGgdzPnSuSaKg2GPFDEdZkJf0V 0wx6tiX27lNMgNa1hVzHDQsguFRckynIo6Fz2X5HrRRMiwk4ZuA3YAdOPSt4ZqvPsmua IKqhAncegEMExrsQ2qIX5JIe4ULoygrllZfpV0tSccFGxUUtHr2CIzP414PZ2p0Tj4Ff WKwZhxAwhJB2n9fRTdym+VOJLTBcWNfav1l8cCUfa4w3/Vf9cOp4LgghBILWdy5XSl8+ 1lXQ== X-Forwarded-Encrypted: i=1; AJvYcCWyMUKxQKTuACOQb1KS+uX6vxy3DinJVugzix2VlLZAewQ/3jmCGFN/Cv+gtuGG3Dylpw8V43FvPzDU2F107Nfc7qQ= X-Gm-Message-State: AOJu0YxX6Zm4TsTQAeymeTT33+VbXA4zPVba4hkSF5UcMc8B52rPRXWs XP55Wb+pl0isB4AMHTXNWift5TuPUf66S7eStb6FmaQExz4fi1RdfEF9pPLuC0GTqWsDhTKP5CA klcgGXtkd/wzQ6NVdmBoN7Ro9dfhjTF5rBUfL X-Google-Smtp-Source: AGHT+IHw3m+SzL4hxUYp5y6wiDp6GTuGMiaxFC5l/moh7mVmQ69WM7rkTFBEUxPaodV+F9tSRd2xYi/G8gK7m9H6d+0= X-Received: by 2002:a05:651c:1251:b0:2d6:87d7:601 with SMTP id h17-20020a05651c125100b002d687d70601mr775544ljh.9.1711150523741; Fri, 22 Mar 2024 16:35:23 -0700 (PDT) MIME-Version: 1.0 References: <01b0b8e8-af1d-4fbe-951e-278e882283fd@linux.dev> In-Reply-To: From: Yosry Ahmed Date: Fri, 22 Mar 2024 16:34:47 -0700 Message-ID: Subject: Re: [External] Re: [bug report] mm/zswap :memory corruption after zswap_load(). To: Barry Song <21cnbao@gmail.com> 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-Stat-Signature: dg8boowi574hhzkbi81mgskqiiss1m35 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: CB195140013 X-Rspam-User: X-HE-Tag: 1711150525-39958 X-HE-Meta: U2FsdGVkX19W0X0UFt+kGiR+3vD5HKoxEfcWQdLuSDcF/+fAoOevWpD9YRsd6oSEhvRtBVKmbhZIWXRGOq3w7bW3BoM/FjE7gT662PljTDrYEuGzViFYQKpzp1lMHxeyA88OL0EcjjdiNLb4YkAptzFWZ2cCmFDtdIPcl/eOInlmrrmthH29DXFLX2PQKeMcuP7TPYSnWz3H9MsvYRZ5coQa7Cus5iiWWv5hOzrhdtQdpDbTMQxSP/Sw9rqXE0+Vkrr2Gm8TcYr+sAcf4ot/yypJgXVQ0ruVQOoqmXdb/NDBAI5UnLb0u0tHZDI81An0JLZk1zMYZLE8/P/COpBNTv9aeahPEhMCTA6V/+a52LmI5LLg9XC0+AvgCYBBwyBf3LlnD6QcVmhEQBbPFimtNg2yZJmEIEufw/Yf966A8Wa84bbTKQash3Iy7ejlq7i6p5CNOGhEdR48E/e98Hek/0XdLcxhkyxeY5SeWW1JsAHmFAPmu/eiR0CUgxoemsbW2flpILtGekFAZwUrv0099agcaibnm0nX3Dwcr+UehREis+gebhmRDNbIjM+n+tWl9ZEWPKBHG72HGiDngNkYXSS6Cw0oqnqRWtNWGVSM0YpR43QJCs61FuvPl+KKkbKyrdYoENVDEbUNoOK7N4PuLE0HsGOU/G9mEf08c/TOlomRuDUoWw02lSXp0itA9hWGW9fIpZ3TNkIIQ/XPxhExXL/5NwCySoMCASVZbbg4ruIuI87CrbcoDxU54DCbMLFy0WiGmf8PH8051X9v7ujyNU2ZCUbMFJFB6p7mxWoaZ84JNog+c3vgIOwtnm/r2CQ/2ceumQ+eL20NzQPpz3CmmBOidEELLLIV6dO5eLoYQib96Sivz/EW6l34kyX+jhn8muwTNG4/H40ZwlTc1M0cVukd0Bj5lqf+M7Me+Lgbv7+DbeM8SfXlQIwAz637IWdHof0Rx3zAb4wJ2mSUU3u RPMC9yHK 6rQJzoAhQkf2OrRzjIXnEer8Ch8rZ9LMkxSHvWkKZzT/HvbvJB+VEw62N0DbulsA43JeVted6c8MUnQNdmCAnegPEmh3r6EIP0Jmgv29KvhISN0bubiRY6POuVRUpUFb29MSS/WforN7LIaPXuNf2/4EBQA9O3/KNgZNy172QyRrPuIvxIZr6e6SIrVAIVOS9pefaPkeny5jGIN/JAi7AQgG7OBw1GvXYFG2VDKhPEE1MayxOoTqaQhcmMkeOS8Z9EwxPUobAesBqG4XGuwKsn6n8ESlKzrZ4Ypc79l2GJFfvZQH3yUf2f+Du+0XDBbBrWvcGXnXgtp8An6zPr2elDUXhmuouxb+Z7lLnp+7+3zInyPqCrMgZnoXT5s3nVzVsoDCp 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 Fri, Mar 22, 2024 at 4:32=E2=80=AFPM Barry Song <21cnbao@gmail.com> wrot= e: > > 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.c= om> 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 Zhou > > > > > > > > >>> wrote: > > > > > > > > >>>> > > > > > > > > >>>> On 2024/3/21 12:34, Zhongkun He wrote: > > > > > > > > >>>>> Hey folks, > > > > > > > > >>>>> > > > > > > > > >>>>> Recently, I tested the zswap with memory reclaiming i= n the mainline > > > > > > > > >>>>> (6.8) and found a memory corruption issue related to = exclusive loads. > > > > > > > > >>>> > > > > > > > > >>>> Is this fix included? 13ddaf26be32 ("mm/swap: fix race= when skipping swapcache") > > > > > > > > >>>> This fix avoids concurrent swapin using the same swap = entry. > > > > > > > > >>>> > > > > > > > > >>> > > > > > > > > >>> Yes, This fix avoids concurrent swapin from different c= pu, but the > > > > > > > > >>> reported issue occurs > > > > > > > > >>> on the same cpu. > > > > > > > > >> > > > > > > > > >> I think you may misunderstand the race description in th= is fix changelog, > > > > > > > > >> the CPU0 and CPU1 just mean two concurrent threads, not = real two CPUs. > > > > > > > > >> > > > > > > > > >> Could you verify if the problem still exists with this f= ix? > > > > > > > > > > > > > > > > > > Yes=EF=BC=8CI'm sure the problem still exists with this p= atch. > > > > > > > > > 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 sw= ap 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 sw= apcache to > > > > > > > > swapin the folio (swap entry count =3D=3D 1), but then it c= an't install folio > > > > > > > > to pte entry since some changes happened such as concurrent= 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 oth= er > > > > > > threads or CPUs. swap_read_folio() unlocks it and then returns = and we > > > > > > immediately try to lock it again with folio_lock_or_retry(). Ho= w 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 oth= er > > > > > > concurrent faulting threads should be held off by the newly int= roduced > > > > > > swapcache_prepare() logic. But looking deeper I think this one = 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 instal= l 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() check= . I see > > > > > > how a concurrent MADV_WILLNEED could cause that check to fail. = A > > > > > > concurrent MADV_WILLNEED will block on swapcache_prepare(), but= once > > > > > > the fault resolves it will go ahead and read the folio again in= to the > > > > > > swapcache. It seems like we will end up with two copies of the = same > > > > > > > > > > but zswap has freed the object when the do_swap_page finishes swa= p_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 withou= t > > > > zswap. It seems like we could end up with two copies of the same fo= lio > > > > 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 f= inishes > > > > > swap_read_folio and before swapcache_clear. > > > > > > > > > > > folio? Maybe this is harmless because the folio in the swacache= will > > > > > > never be used, but it is essentially leaked at that point, righ= t? > > > > > > > > > > > > I feel like I am missing something. Adding other folks that wer= e > > > > > > involved in the recent swapcache_prepare() synchronization thre= ad. > > > > > > > > > > > > Anyway, I agree that at least in theory the data corruption cou= ld > > > > > > 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 t= o zswap > > > > > > before unlocking it and before calling swapcache_clear(). The n= eed 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 if = 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_prepare > > > successfully , someone else should have already set the pte and freed > > > 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, pmd,= 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_M= OVABLE, > > > 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 de= tect > real swap slot free :-) > > /* > * Swap entry may have been freed since our caller observ= ed 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.