Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp1155872ybh; Tue, 10 Mar 2020 16:05:07 -0700 (PDT) X-Google-Smtp-Source: ADFU+vuM+D6+ywZPRn1YPC+f39nU1Z2FgenjiVsduG1G+5+q57Xo3HrASHG1vmWkpU/kHmQwHoSX X-Received: by 2002:aca:280a:: with SMTP id 10mr32205oix.22.1583881507404; Tue, 10 Mar 2020 16:05:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1583881507; cv=none; d=google.com; s=arc-20160816; b=bmOYaA0ZsXgQfDTgAjNbKefWj8NTfQB02kHIjDN6ZIMnWO7eowZr7DC5889IgjPqeX gWwxiD7yUzqpxdnnrZn4mYx/HdXXqK52A2B7fGWRI5qyV1OIWyoVUFmOqWOCrAINOAyM zQk7p2iZwiwOii9KGSCFPbKZRClAKz6A6V34blVh+B5Z9GNobrRs0awuUqRssORiF7aT ppERY/GLcxGb5Y8E/61pj0SllBz+kf/7z2vzWT14C3+B9XQkBitYIyr8RQTJR3z21+x6 s2woXFsPj8JGCi7TgOH2fv9lAZ8RQVIHLVS5PAtuI75pLm4ULhPTBb2WoP7y47LUiM9u leGQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:autocrypt:from:references:cc:to:subject; bh=v41q4Mw9ayXm0jmcgTIhWJiVkhx6LuhSNbO5lmf7HYg=; b=ZapSr/w/tmcswFjG/x9uahVYErOnwzIBAhppGFz+Ci0FFnQ6b9W3QYzcq1hdI26GNZ Hc9NUN8cx/nJxQXcBlWu/StsEeBcgGS7CRGbg3KfE6/+cHYcS28quazebrTPV9qHTIVv k/t4GfFfPUeqH1w1YWC5coOME0Hp3sCrSk+l07gYU4HNM+GCbaiIadXtBp1uvBK7rhOC 2gEDt0viuWO9UR5wr0yaqIR5ntG1ItoNTfGte9RhTR1SI4iJLHoQ+OP9Qy1avGB/Fnjs Zp0mMlp18Of/Xx7iawaddaEhxO490VWgy+1CnrukrB05SaZOpBfcPJwxF4UfcV7SnUiW hDoQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c21si63448oto.137.2020.03.10.16.04.11; Tue, 10 Mar 2020 16:05:07 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727648AbgCJXDM (ORCPT + 99 others); Tue, 10 Mar 2020 19:03:12 -0400 Received: from mga14.intel.com ([192.55.52.115]:63180 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726325AbgCJXDL (ORCPT ); Tue, 10 Mar 2020 19:03:11 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 10 Mar 2020 16:03:08 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.70,538,1574150400"; d="scan'208";a="415363849" Received: from schen9-desk.jf.intel.com (HELO [10.54.74.162]) ([10.54.74.162]) by orsmga005.jf.intel.com with ESMTP; 10 Mar 2020 16:03:07 -0700 Subject: Re: [PATCH] mm/swap_slots.c: don't reset the cache slot after use To: Wei Yang Cc: Andrew Morton , Wei Yang , linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20200309090940.34130-1-richard.weiyang@linux.alibaba.com> <20200309174854.b6b8c7f019c3dde048c28f94@linux-foundation.org> <005f7454-16db-e8b5-dde2-8f2ddaa42932@linux.intel.com> <20200310222002.lr2vurqfk6jvfo2z@master> From: Tim Chen Autocrypt: addr=tim.c.chen@linux.intel.com; prefer-encrypt=mutual; keydata= mQINBE6ONugBEAC1c8laQ2QrezbYFetwrzD0v8rOqanj5X1jkySQr3hm/rqVcDJudcfdSMv0 BNCCjt2dofFxVfRL0G8eQR4qoSgzDGDzoFva3NjTJ/34TlK9MMouLY7X5x3sXdZtrV4zhKGv 3Rt2osfARdH3QDoTUHujhQxlcPk7cwjTXe4o3aHIFbcIBUmxhqPaz3AMfdCqbhd7uWe9MAZX 7M9vk6PboyO4PgZRAs5lWRoD4ZfROtSViX49KEkO7BDClacVsODITpiaWtZVDxkYUX/D9OxG AkxmqrCxZxxZHDQos1SnS08aKD0QITm/LWQtwx1y0P4GGMXRlIAQE4rK69BDvzSaLB45ppOw AO7kw8aR3eu/sW8p016dx34bUFFTwbILJFvazpvRImdjmZGcTcvRd8QgmhNV5INyGwtfA8sn L4V13aZNZA9eWd+iuB8qZfoFiyAeHNWzLX/Moi8hB7LxFuEGnvbxYByRS83jsxjH2Bd49bTi XOsAY/YyGj6gl8KkjSbKOkj0IRy28nLisFdGBvgeQrvaLaA06VexptmrLjp1Qtyesw6zIJeP oHUImJltjPjFvyfkuIPfVIB87kukpB78bhSRA5mC365LsLRl+nrX7SauEo8b7MX0qbW9pg0f wsiyCCK0ioTTm4IWL2wiDB7PeiJSsViBORNKoxA093B42BWFJQARAQABtDRUaW0gQ2hlbiAo d29yayByZWxhdGVkKSA8dGltLmMuY2hlbkBsaW51eC5pbnRlbC5jb20+iQI+BBMBAgAoAhsD BgsJCAcDAgYVCAIJCgsEFgIDAQIeAQIXgAUCXFIuxAUJ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== Message-ID: Date: Tue, 10 Mar 2020 16:03:07 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 MIME-Version: 1.0 In-Reply-To: <20200310222002.lr2vurqfk6jvfo2z@master> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 3/10/20 3:20 PM, Wei Yang wrote: > On Tue, Mar 10, 2020 at 11:13:13AM -0700, Tim Chen wrote: >> On 3/9/20 5:48 PM, Andrew Morton wrote: >>> On Mon, 9 Mar 2020 17:09:40 +0800 Wei Yang wrote: >>> >>>> Currently we would clear the cache slot if it is used. While this is not >>>> necessary, since this entry would not be used until refilled. >>>> >>>> Leave it untouched and assigned the value directly to entry which makes >>>> the code little more neat. >>>> >>>> Also this patch merges the else and if, since this is the only case we >>>> refill and repeat swap cache. >>> >>> cc Tim, who can hopefully remember how this code works ;) >>> >>>> --- a/mm/swap_slots.c >>>> +++ b/mm/swap_slots.c >>>> @@ -309,7 +309,7 @@ int free_swap_slot(swp_entry_t entry) >>>> >>>> swp_entry_t get_swap_page(struct page *page) >>>> { >>>> - swp_entry_t entry, *pentry; >>>> + swp_entry_t entry; >>>> struct swap_slots_cache *cache; >>>> >>>> entry.val = 0; >>>> @@ -336,13 +336,10 @@ swp_entry_t get_swap_page(struct page *page) >>>> if (cache->slots) { >>>> repeat: >>>> if (cache->nr) { >>>> - pentry = &cache->slots[cache->cur++]; >>>> - entry = *pentry; >>>> - pentry->val = 0; >> >> The cache entry was cleared after assignment for defensive programming, So there's >> little chance I will be using a slot that has been assigned to someone else. >> When I wrote swap_slots.c, this code was new and I want to make sure >> that if something went wrong, and I assigned a swap slot that I shouldn't, >> I will be able to detect quickly as I will only be stepping on entry 0. >> >> Otherwise such bug will be harder to detect as we will have two users of some random >> swap slot stepping on each other. >> >> I'm okay if we want to get rid of this logic, now that the code has been >> working correctly long enough. But I think is good hygiene to clear the >> cached entry after it has been assigned. >> > > This is fine to keep the logic, while I am wondering whether we need to do > this through pointer. cache->slots[] contain the value, we can get and reset > without pointer. > > The following code looks more obvious about the logic. > > entry = cache->slots[cache->cur]; > cache->slots[cache->cur++].val = 0; Yes, this looks pretty good. Thanks. Tim