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 37D00C0218C for ; Mon, 27 Jan 2025 09:19:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0D2B4280138; Mon, 27 Jan 2025 04:19:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 08387280136; Mon, 27 Jan 2025 04:19:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E6629280138; Mon, 27 Jan 2025 04:19:43 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id C7AA3280136 for ; Mon, 27 Jan 2025 04:19:43 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 5F9EFC20BC for ; Mon, 27 Jan 2025 09:19:43 +0000 (UTC) X-FDA: 83052684246.14.343EA54 Received: from mail-lj1-f177.google.com (mail-lj1-f177.google.com [209.85.208.177]) by imf01.hostedemail.com (Postfix) with ESMTP id 6C67740009 for ; Mon, 27 Jan 2025 09:19:41 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=d0GR95HL; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf01.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.208.177 as permitted sender) smtp.mailfrom=ryncsn@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737969581; a=rsa-sha256; cv=none; b=N1RfysWfsF8pTtAQjdXXzID4mY+FqNC3SXAezgYCQSTnTbi/B+hBDXByny0zYgPQulw8dM 0GVyE5T9MkD7zWiE8AhdxIRcZV0WDYdqzSXRLfWzHralp0USN9dRVRMT9kldGO/pCEMJma 9UTcS9chcMeN1aaRW9DXb5I+Ji6W34c= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=d0GR95HL; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf01.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.208.177 as permitted sender) smtp.mailfrom=ryncsn@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737969581; 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=7P50Qor44r+Y0YOYE+JHhJM5Zkvamzl5vHTBff5ozu4=; b=oEoWQLS4GjR2VjXjDf3nDHpPgByqsmD64FP7qpufO8ihMItSvUTRU4mMbszaIge1yw2Ccp 1bKu3jwh9twohjdRRBYVOcv3dnFbAdZKLZomWMC2jj9uXLOVNzmUyVI3nTRHF7UykDTduj gqUuBdIxrk1Kj2DSiGSc4cMcgDWUOnw= Received: by mail-lj1-f177.google.com with SMTP id 38308e7fff4ca-306007227d3so40977061fa.0 for ; Mon, 27 Jan 2025 01:19:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737969579; x=1738574379; 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=7P50Qor44r+Y0YOYE+JHhJM5Zkvamzl5vHTBff5ozu4=; b=d0GR95HLAjwOhPWV8K9ZrcSFKqm4FJaoTfVY9tnscjUO0fjTI3QBBLbKfXrFaFA+gD bsUVr1tyacMPrtIilTQkLCEwm6OmPIb6Fg1rdsGxERKkCEQPumx1nnSd7rLKtB93ePym XkqNvG2A9OrYKr9TBZZv8GK02DuBOufTwHlfVN0f6JhS3LbpOjcy6K1r4gIHFBvmMJW6 GLdH9La/y13OXG6t/vGUHiFmKkP9wHsZ5+qT3zQezgzKGj7N2Wh7ysy3L34aSFjJ9L4u wrhbHN45EI0GjwyTzqVWv0RvON0NqXvtNzcpm9LgeKXmxCnDWsOi3fIzpKJ/Xp3nQbAa Rr0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737969579; x=1738574379; 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=7P50Qor44r+Y0YOYE+JHhJM5Zkvamzl5vHTBff5ozu4=; b=hCAOFOlMOuTaFQan91sTvAH2ujyl7zQCa4BDLhAc3/K1Kq+8rrxyMetrE13EI7UU0I LYaIHOL+Vlz/aaILDuL2zFp+421mMYMyHlM/6BFQvoFmZbmFaG9eWcihAPtypxPJPh0b 1q2Nkk+Nwm8dhwUNr015kPIwVv4lx7b6qJwC+wGuuSDWGA5zYWDXAtFqr7zvfIaitzJX NepGvto6raOK2MpYQA/SwuWih+0hthr8FigRppKgb/bXJeSSE3jKWqKcLPLuIoxpeNrC 12rppPVJAa6jWQ4LV6UBMeKtQRMsT38ubJouSBB94A0ODodKCYMURDKwYAXaJPLV/sWD Hs+A== X-Gm-Message-State: AOJu0YyjGXVS+tYCVc3jxjKv/lQdkmggdTyTNwJngt9uExtfNITM26XD gfkkimnclJzjVUWebc664p29qgxydtsn8Nn7oMydTmvK37MdP+l9l+Ome8WMgST91OcVAaWw1IZ B59j/D2UU0uC5+UpL1tg6uQ4NE58= X-Gm-Gg: ASbGncvoF3uBG+r3CnlJDksnGbiYZjZoNhk/riG1TUsi+5fKCsvyx6ApYdqXiX6e1zy 617V4xDPz+A3Rfn3NTmN+V+rgaTVIU666GIH3AVwD92C7R0fA3Ex/eip0AtHvVA== X-Google-Smtp-Source: AGHT+IH2AHlXU957p9hJvXHgifr7iD6MxkbHQG34jmoHIb+bjwDGXpxLJt3ywn2zf/caNpejtVTtc856Zq5oVa9GVxU= X-Received: by 2002:a2e:b911:0:b0:300:25f6:9b3d with SMTP id 38308e7fff4ca-3072ca6adfdmr127400191fa.10.1737969579245; Mon, 27 Jan 2025 01:19:39 -0800 (PST) MIME-Version: 1.0 References: <20241230174621.61185-1-ryncsn@gmail.com> <20241230174621.61185-8-ryncsn@gmail.com> In-Reply-To: From: Kairui Song Date: Mon, 27 Jan 2025 17:19:22 +0800 X-Gm-Features: AWEUYZmruG0pXLeeozzkGEEDuagF35Cq1SROFoaR1TU6jxnND1esTMBQH_I7wN0 Message-ID: Subject: Re: [PATCH v3 07/13] mm, swap: hold a reference during scan and cleanup flag usage To: Baoquan He Cc: linux-mm@kvack.org, Andrew Morton , Chris Li , Barry Song , Ryan Roberts , Hugh Dickins , Yosry Ahmed , "Huang, Ying" , Nhat Pham , Johannes Weiner , Kalesh Singh , linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 6C67740009 X-Stat-Signature: 9uwb9zz7qiqwhe5de3mrfwgeouizdia9 X-Rspam-User: X-HE-Tag: 1737969581-423256 X-HE-Meta: U2FsdGVkX18N+x6CC9zdMb9I0pae0eLNIxdOQm5FAi3E/mmgB6wBLg2aXY8+L1OvX/z2eNEWxlV6ENM0wJkbffbnhoTiit5djwxEWIrjZXKbbRhRT3VSeGhmWQFt9EFlksUO/j0m/3Pw3wS9llImrYkq5MQ92oYfgtGMYBwyya0x2YNqAS0zzAMeshAfgK59iw7AP+azMewtAG9QTtR3qSDYS+8gQixzkS7rMIqSFHHvQWBx6Ec0y6q1QdAftXUdueUHYifbq7cg/cuQP+vzD/5t1TyCH3m8fcbj6V2GaPOtOf3tzDXmUm5J7oYe2XrhHaPVrxvtndYwHCE/+EUX1bUOCzM81G86rZ3VkvjFGer1P4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k X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Mon, Jan 20, 2025 at 10:39=E2=80=AFAM Baoquan He wrote: > > On 01/13/25 at 01:34pm, Kairui Song wrote: > > On Sat, Jan 4, 2025 at 1:46=E2=80=AFPM Baoquan He wrot= e: > > > > > > On 12/31/24 at 01:46am, Kairui Song wrote: > > > > From: Kairui Song > > > > > > > > The flag SWP_SCANNING was used as an indicator of whether a device > > > > is being scanned for allocation, and prevents swapoff. Combined wit= h > > > > SWP_WRITEOK, they work as a set of barriers for a clean swapoff: > > > > > > > > 1. Swapoff clears SWP_WRITEOK, allocation requests will see > > > > ~SWP_WRITEOK and abort as it's serialized by si->lock. > > > > 2. Swapoff unuses all allocated entries. > > > > 3. Swapoff waits for SWP_SCANNING flag to be cleared, so ongoing > > > > allocations will stop, preventing UAF. > > > > 4. Now swapoff can free everything safely. > > > > > > > > This will make the allocation path have a hard dependency on > > > > si->lock. Allocation always have to acquire si->lock first for > > > > setting SWP_SCANNING and checking SWP_WRITEOK. > > > > > > > > This commit removes this flag, and just uses the existing per-CPU > > > > refcount instead to prevent UAF in step 3, which serves well for > > > > such usage without dependency on si->lock, and scales very well too= . > > > > Just hold a reference during the whole scan and allocation process. > > > > Swapoff will kill and wait for the counter. > > > > > > > > And for preventing any allocation from happening after step 1 so th= e > > > > unuse in step 2 can ensure all slots are free, swapoff will acquire > > > > the ci->lock of each cluster one by one to ensure all allocations > > > > see ~SWP_WRITEOK and abort. > > > > > > Changing to use si->users is great, while wondering why we need acqui= re =3D > > > each ci->lock now. After setup 1, we have cleared SWP_WRITEOK, and ta= ke > > > the si off swap_avail_heads list. No matter what, we just need wait f= or > > > p->comm's completion and continue, why bothering to loop for the > > > ci->lock acquiring? > > > > > > > Hi Baoquan, > > > > Waiting for p->comm's completion must be done after unuse is called > > (unuse will need to take the si->users refcound, so it can't be dead > > yet), but unuse must be called after no one will allocate any new > > entry. That is guaranteed by the loop ci->lock acquiring. > > Sorry for late response, Kairui. I went trought the code flow of swap > allocation several times, however haven't made clear how loop ci->lock > acquiring is needed here. Once si->flags &=3D ~SWP_WRITEOK is executed i= n > del_from_avail_list() when swaping off, even though the allocation > action is still on going, it will be failed in cluster_alloc_range() > by the 'if (!(si->flags & SWP_WRITEOK))' checking. Then that allocation Hi Baoquan, Thanks for the careful review. > requirement will be failed and returned, means no new swap entry|slot > allcation will be done. Then unuse won't be impacted at all. In this > case, why do we care about it? > > Please forgive my stupidity, could you elaborate in which case this kind > of still ongoging swap allocation will happen during its swap device's > off? Could you give an example of the concurrent execution flows? There is no barrier or lock between clear the flag and try_to_unuse, so nothing guarantees the "if (!(si->flags & SWP_WRITEOK))" in cluster_alloc_range will see the updated flag. The loop ci->lock acts like a full memory barrier, ensuring any allocation after the loop lock will definitely see the updated flags, and try_to_unuse will only go on after all allocation have either stopped or will see the updated flags. In practice this problem is almost impossible to happen, but in theory possible. > > Thanks > Baoquan >