Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp1508618rdb; Mon, 19 Feb 2024 20:01:32 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXAktIQd9cFTURAzy+uZIqDx+ar7vVOILhd6UACS6w6Z8oVkphK+nfsN/Hyek/vgAMwwdBvvzvJelgNfnaL3rfLtvGQCv4Ps478ubfHSA== X-Google-Smtp-Source: AGHT+IGDKRh3CzSX6pHbJ76c4qThaQvYwg1LgNGDDAKyTv9cWer3/DspYOErKFyWL8/OgM4eeb4W X-Received: by 2002:a05:6a00:27a8:b0:6e4:660f:b08b with SMTP id bd40-20020a056a0027a800b006e4660fb08bmr4899889pfb.17.1708401692011; Mon, 19 Feb 2024 20:01:32 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708401691; cv=pass; d=google.com; s=arc-20160816; b=a0uRUHSInGChmnoWpCNBcYsrS8Fjq7uviRIZSj6YpqhgHu0paC/oRtDqK/uotQC7Mt 3dgbE9i7mkXit/2yWNsuKz8UBZL78YwC4OtehBvyaaCQJkfU3BIDxyCnas/O0xvTwhvP QrNEH8oN8E8reMvhDNCJgoE8mu/vcC/kxTrGp6EOJ/59VYfps1ZAMl44nJqfodkDK2uN +ubTaV1AmIy/kJE8oRZ8Pd12Uk7mhagjhu3TPR2WiyuIpHwlSjRYbM3x4wBjeevQm/9q YjPeKcawa0KNbN4snhII+B5wDx2AMqJ3Y9kDgozTW1yxJOI8FtvVKiKH506C8/cKm2f+ Qmkg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=f+7pW6Td4IjBVEl/B0SbCdXWjl+KiOYugwOPe6kZqWU=; fh=gTGhDek9nKlQf66nFW8IiY/LIqq3m3Mve4g4oCCrpwE=; b=vxgSjOLiWIZaMr5D8/DNiitKT+KppE/f7NwBSQK+RIvgG5TaUWyUoynSYwhxYTQt2q 7P+iK7Gr0bXbLrKpg1ZaTpCb+uAvptMrgRbIIieEHILjx7wUVLJZlfSDwp23wwST8hi2 4Q4NoL13WrS2W51OpgaLGfMW43q1G7w/4S+La5UbBgmcB1jDP/2d5bac1L+7w+Ctnujb cJEbPghlas57ZsdXcXMGRi4dvlgBpTYRDM0Tj/P3YJ1eF+Khfoe998wmqesn3YxKdS8e gks8fzWjZhH+6U3FTdg1RDA03+XO4M79IxGVBXGU0E8E/sN8zZYuOu1tek5B/4/MdByY lWqg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=JIvYOsQO; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-72303-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72303-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id b186-20020a6367c3000000b005dc8554d4f6si5431732pgc.49.2024.02.19.20.01.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 20:01:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-72303-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=JIvYOsQO; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-72303-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72303-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id D6355282C0C for ; Tue, 20 Feb 2024 04:01:30 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A083654BC4; Tue, 20 Feb 2024 04:01:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="JIvYOsQO" Received: from mail-vk1-f174.google.com (mail-vk1-f174.google.com [209.85.221.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 359EB52F68; Tue, 20 Feb 2024 04:01:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708401684; cv=none; b=aONJJ83XOMv3GrrdRIdMzRPC6tRoQTBkzwYweq6KGVb+lb6qHFrReKv5WsQqFSN3xicU4Sp3HzkhIaBFDlF9l9TIfdVu9cHxwCooYHOpMWQ8lub0g+cNCAGfRT/8spvmDHC0ofNsjJYZeFLk6sTaP8WwFKF8Yw+A9BLF6TiKzjQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708401684; c=relaxed/simple; bh=EtqPj06BXSieuMRXGbbN4iVl0m4nh6SEwC95Vkqic9A=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=Fr2LNauYJZ970lbYu9EPTFrY/xA5oxsMHYwuNXLzuQK2KdX1kW7+VlhnbQNDEeowDMHH3aVyqRHA4TQsEGtBnF2jq3G82C6VflEnUexDOjswzwoo4qt6STJqwQnbl57R9V6ncNjojo6BAuL3AOwCfG+N3HhIttoSnsgPZAK8PXw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=JIvYOsQO; arc=none smtp.client-ip=209.85.221.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-vk1-f174.google.com with SMTP id 71dfb90a1353d-4cba3807eedso834565e0c.0; Mon, 19 Feb 2024 20:01:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708401682; x=1709006482; darn=vger.kernel.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=f+7pW6Td4IjBVEl/B0SbCdXWjl+KiOYugwOPe6kZqWU=; b=JIvYOsQOhWd59oVlmi5NIkerU3zVPTK4Snltoh/FQuJgGP+xGCPIQI2GD5gBRfkFs9 0NePSD1eK2hmHFi/JUo6YcCLAUvlNBN4E1kP+8ATMYHCPljP1UZZdYdKkmWuQ5eqH6uS dSDgx6af3HNlQSeXyvR733QucRnUtDOjz0Rb9aUxm3WBPti9cLcZlz4Z/I3oFgV4N3QA isgxSBNZ8LDkDVa1mQyIGXpdgq6K+ZhV4EvG7eAfSbCwPj8dUkiRsFxBFzE1zZkn1h5C 3zU2Bo2osjrE9CdusZ/sVSGuMvVVqXJQkCLURzD2J5YcZVznquFD/bRpnyvThHnGRKRH 4u7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708401682; x=1709006482; 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=f+7pW6Td4IjBVEl/B0SbCdXWjl+KiOYugwOPe6kZqWU=; b=ofUI8rv0Jd6uUdwnj/JXR276eOvEM0Fm3nVovbL2b8ZXC519Yt7QMqrm20jgYzd2O2 jeCus0jXqMjZ0FN1hxCPBLy1EckFDlcaWY6u0YgmAQlK1Ux+KDa+C5P003PJEf580AC6 820XhNTtUUyVYGR9ETYKcRAiyHmu9h8cNmQ8Vt7skkKoogcJYWF4fVS4s1vr0cXloJgn Iek/69CGUR72a0K8pF9Bw3pDjr3u/AfqLLjhmG7J700+qinPf1H+V8KejUvSooKjKapL cfMZi3ht2nOsnd2NtyrkhJlu9nluSFh+6JED9GVw+BxpQR1OHDijOwBxzX4vBcN4VKT2 7yiA== X-Forwarded-Encrypted: i=1; AJvYcCXDu/mtmbesFIaTeOqQyzAjXzvx65aarCrg9Z6VAYBvxKXgBLrYMpyxQwvwmA3bpkN4xPibhznllPxek15sO/DFqPpXG+Dafq7RoAU2gnTwtcANgYxuCDzkb1Tr9oPeU309+T7z X-Gm-Message-State: AOJu0Yxg5xv0fLJpxEfTmOZwUVMRmVE9Y/97DB+I903L5WeXQp4STyqP Wd86ffGi5h6s5RVsM8VU/LQG3oc5urABxk4YfCK3vlm4jlKFkfsf47+2bu0WYdbAAhFjAfnV9aS X7eh8ochwTksF6vlbHcmCht0DHu4= X-Received: by 2002:a05:6122:4d0f:b0:4c8:df97:139d with SMTP id fi15-20020a0561224d0f00b004c8df97139dmr4748249vkb.2.1708401681914; Mon, 19 Feb 2024 20:01:21 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240219082040.7495-1-ryncsn@gmail.com> <20240219173147.3f4b50b7c9ae554008f50b66@linux-foundation.org> In-Reply-To: From: Barry Song <21cnbao@gmail.com> Date: Tue, 20 Feb 2024 17:01:10 +1300 Message-ID: Subject: Re: [PATCH v4] mm/swap: fix race when skipping swapcache To: Kairui Song Cc: Andrew Morton , linux-mm@kvack.org, "Huang, Ying" , Chris Li , Minchan Kim , Barry Song , Yu Zhao , SeongJae Park , David Hildenbrand , Hugh Dickins , Johannes Weiner , Matthew Wilcox , Michal Hocko , Yosry Ahmed , Aaron Lu , stable@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, Feb 20, 2024 at 4:42=E2=80=AFPM Kairui Song wrot= e: > > On Tue, Feb 20, 2024 at 9:31=E2=80=AFAM Andrew Morton wrote: > > > > On Mon, 19 Feb 2024 16:20:40 +0800 Kairui Song wrote= : > > > > > From: Kairui Song > > > > > > When skipping swapcache for SWP_SYNCHRONOUS_IO, if two or more thread= s > > > swapin the same entry at the same time, they get different pages (A, = B). > > > Before one thread (T0) finishes the swapin and installs page (A) > > > to the PTE, another thread (T1) could finish swapin of page (B), > > > swap_free the entry, then swap out the possibly modified page > > > reusing the same entry. It breaks the pte_same check in (T0) because > > > PTE value is unchanged, causing ABA problem. Thread (T0) will > > > install a stalled page (A) into the PTE and cause data corruption. > > > > > > @@ -3867,6 +3868,20 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) > > > if (!folio) { > > > if (data_race(si->flags & SWP_SYNCHRONOUS_IO) && > > > __swap_count(entry) =3D=3D 1) { > > > + /* > > > + * Prevent parallel swapin from proceeding with > > > + * the cache flag. Otherwise, another thread ma= y > > > + * finish swapin first, free the entry, and swa= pout > > > + * reusing the same entry. It's undetectable as > > > + * pte_same() returns true due to entry reuse. > > > + */ > > > + if (swapcache_prepare(entry)) { > > > + /* Relax a bit to prevent rapid repeate= d page faults */ > > > + schedule_timeout_uninterruptible(1); > > > > Well this is unpleasant. How often can we expect this to occur? > > > > The chance is very low, using the current mainline kernel and ZRAM, > even with threads set to race on purpose using the reproducer I > provides, for 647132 page faults it occured 1528 times (~0.2%). > > If I run MySQL and sysbench with 128 threads and 16G buffer pool, with > 6G cgroup limit and 32G ZRAM, it occured 1372 times for 40 min, > 109930201 page faults in total (~0.001%). it might not be a problem for throughput. but for real-time and tail latenc= y, this hurts. For example, this might increase dropping frames of UI which is an important parameter to evaluate performance :-) BTW, I wonder if ying's previous proposal - moving swapcache_prepare() after swap_read_folio() will further help decrease the number? Thanks Barry