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 X-Spam-Level: X-Spam-Status: No, score=-2.3 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_SANE_1 autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 22C7FC10DCE for ; Tue, 10 Mar 2020 22:20:06 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id C0059208E4 for ; Tue, 10 Mar 2020 22:20:05 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="NR3+oVCm" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C0059208E4 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 7232F6B0006; Tue, 10 Mar 2020 18:20:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6FA616B0007; Tue, 10 Mar 2020 18:20:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 60F116B0008; Tue, 10 Mar 2020 18:20:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0231.hostedemail.com [216.40.44.231]) by kanga.kvack.org (Postfix) with ESMTP id 480306B0006 for ; Tue, 10 Mar 2020 18:20:05 -0400 (EDT) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 1602E180AD815 for ; Tue, 10 Mar 2020 22:20:05 +0000 (UTC) X-FDA: 76580871570.28.trip92_751c3ab279517 X-HE-Tag: trip92_751c3ab279517 X-Filterd-Recvd-Size: 5771 Received: from mail-wm1-f67.google.com (mail-wm1-f67.google.com [209.85.128.67]) by imf29.hostedemail.com (Postfix) with ESMTP for ; Tue, 10 Mar 2020 22:20:04 +0000 (UTC) Received: by mail-wm1-f67.google.com with SMTP id 6so3170694wmi.5 for ; Tue, 10 Mar 2020 15:20:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:reply-to:references:mime-version :content-disposition:in-reply-to:user-agent; bh=yoO7MPKHueEjV/qh8JLI48qSY1iAaPqrxv7EaGtGVGc=; b=NR3+oVCmMj1t9szU2jyxVoeWUT/l1V8WjayJ6wk2ewaRsQqYWB4wrYPu++/rxsneqp SCr+y7er87LLQV3x0ruLF1DHFQn8ILKoKV98T67RtvgAEBT/ZW6yWydppJuP2K+Zb4aD crw4t65PpulAIBcOPFA9wY1Gtj+A0bzdvDvdCbTdOdRLcSLJZD9PhG2YqIA0igSQN++G BfdvzOug1i+tQFLdX9vfiQcdbRzYXFl2Fe7vLznqQXgwOkmrcTBg1SrEvVX3+hh6/lfV OaO2eo4/Tni7ty/aodZHxkLKsYUoAcTEXSymFEwUNmRcmqVbV0ASUwldRYs63IaIhGp8 aM0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:reply-to :references:mime-version:content-disposition:in-reply-to:user-agent; bh=yoO7MPKHueEjV/qh8JLI48qSY1iAaPqrxv7EaGtGVGc=; b=Ymkc0Q3/E8hFtjPiMjtqspr+qDRuzc7sseMV0Q8KwtEYX+cXDaReX4Eb1XMrqmHgSC 8L6JUANpVkyW8PhmVutz03PcxeM7bKQxLybMf3hYYhRwSHnYMfjuA9JvCUZ23D2x/b+9 fguDoGlcIsmCjRDxRbeogiKcpF9fFfMxJ5lW50U45ZuLv+BMdjHZkc855Urcxcdc+vb4 7jyVfh50x6Qq6Hc2X2Of3EbPlFwLTN/RAIenlub6V558OiWJY97fhF89Hyo6vErmfCw1 sZKL+JAosgx99sClNN2KqzdMi9mopX2aOh4rNveHFwh9kvfcXbvS527QtTk/7xr2N5HV UQoQ== X-Gm-Message-State: ANhLgQ2L1UrC94HSpQfgbht+xLGnSNdXhLbXH4a7zVL/vSMTfBJMccpJ 67CSml/xYcmbdlKD1t2on8M= X-Google-Smtp-Source: ADFU+vvKwEqVdu5myl9pocEDt3VfQWs/wSH6YmPzJNexxldYLKycnV838myJ0bbStDrO1DwBaIy4xA== X-Received: by 2002:a05:600c:410b:: with SMTP id j11mr4317450wmi.86.1583878803311; Tue, 10 Mar 2020 15:20:03 -0700 (PDT) Received: from localhost ([185.92.221.13]) by smtp.gmail.com with ESMTPSA id k126sm5826795wme.4.2020.03.10.15.20.02 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 10 Mar 2020 15:20:02 -0700 (PDT) Date: Tue, 10 Mar 2020 22:20:02 +0000 From: Wei Yang To: Tim Chen Cc: Andrew Morton , Wei Yang , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] mm/swap_slots.c: don't reset the cache slot after use Message-ID: <20200310222002.lr2vurqfk6jvfo2z@master> Reply-To: Wei Yang References: <20200309090940.34130-1-richard.weiyang@linux.alibaba.com> <20200309174854.b6b8c7f019c3dde048c28f94@linux-foundation.org> <005f7454-16db-e8b5-dde2-8f2ddaa42932@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <005f7454-16db-e8b5-dde2-8f2ddaa42932@linux.intel.com> User-Agent: NeoMutt/20170113 (1.7.2) 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: 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; >>> + entry = cache->slots[cache->cur++]; >>> cache->nr--; >>> - } else { >>> - if (refill_swap_slots_cache(cache)) >>> - goto repeat; >>> + } else if (refill_swap_slots_cache(cache)) { > >This change looks fine. >>> + goto repeat; >>> } > >Tim -- Wei Yang Help you, Help me