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 16284C02194 for ; Wed, 5 Feb 2025 09:18:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 77D24280008; Wed, 5 Feb 2025 04:18:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 72AE3280006; Wed, 5 Feb 2025 04:18:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5CC42280008; Wed, 5 Feb 2025 04:18:39 -0500 (EST) 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 3D015280006 for ; Wed, 5 Feb 2025 04:18:39 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id DE54D160970 for ; Wed, 5 Feb 2025 09:18:38 +0000 (UTC) X-FDA: 83085340716.21.7AA2A5E Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf06.hostedemail.com (Postfix) with ESMTP id C5892180004 for ; Wed, 5 Feb 2025 09:18:36 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=AjsKtdNC; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf06.hostedemail.com: domain of bhe@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=bhe@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738747117; a=rsa-sha256; cv=none; b=A2/tSIZQM1hqTUKaKHxukJ3mFTBrm5SFP1U/bKK1s8r+UVq0zKP/N2Q2+K9JY4zc8bQzyV 10aEwYUfieesDZOqnSMqchPEKxFSGJxlMIywdwVvSR0Td/RPeaqpOaRDJWIOa3YH6gjQz7 0MLrgTVc80LhCkqvfarog2myK9OTAsE= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=AjsKtdNC; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf06.hostedemail.com: domain of bhe@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=bhe@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738747117; 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=pvRGVNikjhLpjvxhSukAM7h/M6H6qv36CH/UV/2N8p0=; b=BUziQtMu9t8byrdd49meKu54vEuEvUJb3HBzAEADxLhuExKkLe2thhzdp2tThKOlNiw9L7 7NeGUcgqe7EcAohRuQykNRr5NtuTJXAZ+c5iLO8KWHiLs8R8OOx+blG+1AL23I2VSDWGLN ZfZoixEU+lKf6wsW/oO8THWrGNiVans= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1738747116; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pvRGVNikjhLpjvxhSukAM7h/M6H6qv36CH/UV/2N8p0=; b=AjsKtdNCdF9xcd15l+1N/iBkJ6Vdvf48Wlrhi1W2EffGlF93CbcY8eI6SK8KUVgT0jpBgO RXheYPTYaVpN/ASaiaEwZGazJ4aEnGj6zltw/MVfXkm35CtcvwphEhfkIiwbVx9FKh8w+f VNt+Js5P+1p7g4OTCGjCKYDz5aAOzO0= Received: from mx-prod-mc-08.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-317-jdOD0sLWOZ2pwEAnAel9Kg-1; Wed, 05 Feb 2025 04:18:31 -0500 X-MC-Unique: jdOD0sLWOZ2pwEAnAel9Kg-1 X-Mimecast-MFC-AGG-ID: jdOD0sLWOZ2pwEAnAel9Kg 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-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 665E71800873; Wed, 5 Feb 2025 09:18:29 +0000 (UTC) Received: from localhost (unknown [10.72.112.190]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 7F48B1800360; Wed, 5 Feb 2025 09:18:27 +0000 (UTC) Date: Wed, 5 Feb 2025 17:18:22 +0800 From: Baoquan He To: Kairui Song 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 Subject: Re: [PATCH v3 07/13] mm, swap: hold a reference during scan and cleanup flag usage Message-ID: References: <20241230174621.61185-1-ryncsn@gmail.com> <20241230174621.61185-8-ryncsn@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: C5892180004 X-Stat-Signature: uzeie36kid6z84apwr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ZVMckyEQk+M24TvMHXUtgdm8QiuSBs7Cud3ZR6uVHly2B4PHXtY++n1XiDXIuPBlfAiI/YmMLRr0f+SZVo0vjJmjp7YSpxUDCc+w2d9n+eNBQNB/+d1R6D5mccwYhROCx5fzrfDSul8tBnLSgIlshF/BauNbhX8BIExU1zyEGys5X7kL4ayra4Eox2wIobFNcGy9tq5GaJzFd9SgxMCKwjOIzbUnlENOgyD4CrqcYsQwXYPZYv6q3wZsGiH+l3Du9gmPUJz7b+dntCTzIDuLlhi9UGda1WjmNwXIaG5NsecQ3L5uPpQmq0nhQIl12gKzNHyFM/H4zpsZNDqbucruP08QfbgFPEz2Z2T9rUPIbFhOeR94XmDGqKuajYcyoJjI= 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 01/27/25 at 05:19pm, Kairui Song wrote: > On Mon, Jan 20, 2025 at 10:39 AM Baoquan He wrote: > > > > On 01/13/25 at 01:34pm, Kairui Song wrote: > > > On Sat, Jan 4, 2025 at 1:46 PM Baoquan He wrote: > > > > > > > > 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 with > > > > > 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 the > > > > > 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 acquire = > > > > each ci->lock now. After setup 1, we have cleared SWP_WRITEOK, and take > > > > the si off swap_avail_heads list. No matter what, we just need wait for > > > > 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 &= ~SWP_WRITEOK is executed in > > 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. Got it now. swap_avail_lock is not taken during allocation, and we don't take it when accessing si->flags in cluster_alloc_range() becasue that could bring in new lock contention. Thanks a lot for patient explanation.