Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp878281rdb; Tue, 23 Jan 2024 19:30:05 -0800 (PST) X-Google-Smtp-Source: AGHT+IEYAGLRizJaIWS4PPgi5YDaleUG0fyI4X2QmU/FQpbIQ4EdaEHITRvAf4SfcYjRXSW/WBEO X-Received: by 2002:a05:6402:5145:b0:55c:7415:ece5 with SMTP id n5-20020a056402514500b0055c7415ece5mr572051edd.6.1706067005494; Tue, 23 Jan 2024 19:30:05 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706067005; cv=pass; d=google.com; s=arc-20160816; b=WwFmwYAlNrNNzIUkePISSysLuZ0H1YkPXnbVvT0GQEv7Am4hiDrdhUop1Cbq6PZ867 hA/jeOzn9ZiNv/hfxPhAfjc6nNzZDyCHmKFIs/y6ZSeQw7Vf3d9v1eA8PifiG5OyIZyt RKetF7JDaWKjjXLs9m1x3F6p+roMa8bh3bsG7TauYga485Bhi/Fuy+wQbtNzdaxG7NtE W8/uaXRRTbclsxlBaWEi5mu1yugw1L1sS5ySmT+Kbe56R3SgvWEBqRcf/YERVSD00tq9 hunDn6HeXmoN8zdXsAsApk34YjK6l2tluXjj1pAHpk/c65Wk9lvAoTIW0GskdeggBsQl 8dHQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :user-agent:message-id:date:references:in-reply-to:subject:cc:to :from:dkim-signature; bh=LtGSXam9i66miX3FSLwK9153T1NSs5XlrKRoz5mJVPs=; fh=Lfo1EYJJq4wdO6QmOfcapJNtHpp5cJqDHETjE8sQ7Zc=; b=Ayd28xtKrx5VsryVF7cvWw2KGIOeojQz4XZKPmxZAgSlFFAysw4GwK6QAk2VOhMwtM jpxHbNyNLHGLxalSA39llxC+SZVXdp+7m/YpcpDW1LRCI0eva4TFlBnedEiMYAcFIVZl qFcugBXZ2In4LrV0ugq/WK/LcIk/d6WR9eNRYo0U+TfzXdyN+zHwh91vX8z//Vg3mRkS 8AwzdzP4xsPEPCmHAy8F+Lutbmdz9NGmmmlwAZh6DUgXja4uiAtfuo7fOEP94rYCgQQS 0SjBzBMGBhB95DVConb2m6HVXjgtBHuN8bVKY+yefzbihD/8NGSlrT4yLbh5T3dkt6v9 hTyw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=kCoUQYNO; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-36400-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-36400-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id m5-20020a509305000000b00559523876a4si9488065eda.338.2024.01.23.19.30.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 19:30:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-36400-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=kCoUQYNO; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-36400-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-36400-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.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 am.mirrors.kernel.org (Postfix) with ESMTPS id 40D2F1F2C7B6 for ; Wed, 24 Jan 2024 03:30:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 688EF6ABB; Wed, 24 Jan 2024 03:29:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="kCoUQYNO" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.14]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2A3CA1381 for ; Wed, 24 Jan 2024 03:29:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.14 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706066997; cv=none; b=ZkvdEsp8+2tLMDB12qrxvTyipeCoSq0a0WP/3PgwxKovIyD7gZU2Yc4lctH6L9qLrU6YkSRgFoSkkCamyBVC/HrEldejBNgmd3lQGlhL0RR7Wo6lJQHu3iWfz8sv8nf///H78SxbaIJS75GxGvJ8BMEBz4MkVySgO73os+3gBmc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706066997; c=relaxed/simple; bh=rSaAlk94pMBR8JXrSQ+PW4+MqsgT6LSmV+ZN3ZU7qKo=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=ld5eQ6lWkcQBlgazvD3eOXU1uLK38oh51YAUkv6SIFjKt0s1NjPiiU7g8adsyEy6RxsCL7axU67cf6qGS9DQd88i8NiQodq6glEg6QBLkiAynOqPW5h35N6PwA1OIk17obm89DSgjpgZ+VzeOdBaGRJS64+Kb4AwNzGEcgQ6qqQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=kCoUQYNO; arc=none smtp.client-ip=198.175.65.14 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1706066996; x=1737602996; h=from:to:cc:subject:in-reply-to:references:date: message-id:mime-version; bh=rSaAlk94pMBR8JXrSQ+PW4+MqsgT6LSmV+ZN3ZU7qKo=; b=kCoUQYNOgStJgcYAzeOXLu3UGUS1EdqI+wssLpIfy6ixlKum6rsIfgep Zsa52R7cTQosKkLMDSJUL2xtgbrYuzF4FEGryO0H8k2TIPEzpIp7d5cXk cyIRIafKrDX50icJoqvGS/YTQ3hKUGUvKjkpOfwSRQld3EyuQ47aBFtgQ WIzvqcxrhGu22g5gW3zlhkS0aV+Q+5VR1J+kK/2AwTUlQ+l1U2PBNjtfP ORxQqd0xuQuUmUNf/AgtN0oyEKtFNK9gCMtcP89rhiNfATMxiJ03Uzk+L A6j6kuvcOodC88ESYQLxvKMZaMWhFfneW65TYJKGt4pX7cvn048AW4Ch0 w==; X-IronPort-AV: E=McAfee;i="6600,9927,10962"; a="1614996" X-IronPort-AV: E=Sophos;i="6.05,215,1701158400"; d="scan'208";a="1614996" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Jan 2024 19:29:55 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,215,1701158400"; d="scan'208";a="28251243" Received: from yhuang6-desk2.sh.intel.com (HELO yhuang6-desk2.ccr.corp.intel.com) ([10.238.208.55]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Jan 2024 19:29:53 -0800 From: "Huang, Ying" To: Yosry Ahmed Cc: Andrew Morton , Johannes Weiner , Nhat Pham , Chris Li , Chengming Zhou , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 1/2] mm: swap: update inuse_pages after all cleanups are done In-Reply-To: (Yosry Ahmed's message of "Tue, 23 Jan 2024 19:20:17 -0800") References: <20240120024007.2850671-1-yosryahmed@google.com> <20240120024007.2850671-2-yosryahmed@google.com> <87wms0toh4.fsf@yhuang6-desk2.ccr.corp.intel.com> <878r4ftodl.fsf@yhuang6-desk2.ccr.corp.intel.com> Date: Wed, 24 Jan 2024 11:27:56 +0800 Message-ID: <874jf3tnpf.fsf@yhuang6-desk2.ccr.corp.intel.com> User-Agent: Gnus/5.13 (Gnus v5.13) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=ascii Yosry Ahmed writes: >> > In swap_range_free, we want to make sure that the write to >> > si->inuse_pages in swap_range_free() happens *after* the cleanups >> > (specifically zswap_invalidate() in this case). >> > In swap_off, we want to make sure that the cleanups following >> > try_to_unuse() (e.g. zswap_swapoff) happen *after* reading >> > si->inuse_pages == 0 in try_to_unuse(). >> > >> > So I think we want smp_wmb() in swap_range_free() and smp_mb() in >> > try_to_unuse(). Does the below look correct to you? >> > >> > diff --git a/mm/swapfile.c b/mm/swapfile.c >> > index 2fedb148b9404..a2fa2f65a8ddd 100644 >> > --- a/mm/swapfile.c >> > +++ b/mm/swapfile.c >> > @@ -750,6 +750,12 @@ static void swap_range_free(struct >> > swap_info_struct *si, unsigned long offset, >> > offset++; >> > } >> > clear_shadow_from_swap_cache(si->type, begin, end); >> > + >> > + /* >> > + * Make sure that try_to_unuse() observes si->inuse_pages reaching 0 >> > + * only after the above cleanups are done. >> > + */ >> > + smp_wmb(); >> > atomic_long_add(nr_entries, &nr_swap_pages); >> > WRITE_ONCE(si->inuse_pages, si->inuse_pages - nr_entries); >> > } >> > @@ -2130,6 +2136,11 @@ static int try_to_unuse(unsigned int type) >> > return -EINTR; >> > } >> > >> > + /* >> > + * Make sure that further cleanups after try_to_unuse() returns happen >> > + * after swap_range_free() reduces si->inuse_pages to 0. >> > + */ >> > + smp_mb(); >> > return 0; >> > } >> >> We need to take care of "si->inuse_pages" checking at the beginning of >> try_to_unuse() too. Otherwise, it looks good to me. > > Hmm, why isn't one barrier at the end of the function enough? I think > all we need is that before we return from try_to_unuse(), all the > cleanups in swap_range_free() are taken care of, which the barrier at > the end should be doing. We just want instructions after > try_to_unuse() to not get re-ordered before si->inuse_pages is read as > 0, right? Because at the begin of try_to_unuse() as below, after reading, function returns directly without any memory barriers. if (!READ_ONCE(si->inuse_pages)) return 0; -- Best Regards, Huang, Ying