Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp18280ybh; Tue, 10 Mar 2020 18:19:13 -0700 (PDT) X-Google-Smtp-Source: ADFU+vssBklkvRl12GkXZjei5VI7JqFRnlRlfCQ/PuNEOONpjc2YgFf/LM9PX2OOu/rbtrSne8O0 X-Received: by 2002:a05:6808:298:: with SMTP id z24mr215077oic.67.1583889553440; Tue, 10 Mar 2020 18:19:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1583889553; cv=none; d=google.com; s=arc-20160816; b=hidCbRJWJnSDMXpmNB7cm4in7z82GtUIUYfDQ7ZjKYOmliMCIzb9kV967TtDOFvNVb FCogYLZTA7inv+tcjyODFiYcF7+iTLNdVDhEHURdlbZbjFP/pYFMfDZ1U3ADVRXCTB7k 80YwoGaaevEQkcWR6MEYIqmzn1sED4cPy8tJjo0luswHKBQpgdwO1LbJh5oW2miam2a/ 0w0kro1QNLFkNY9cvPoZLiIuPUJeZY882FJqIEcecn04NKV9JNFOfBKDOxgnsdMkzFr6 bQHVYosIX6p6E7zb1gOaZFfYDRQ00MGfHe8zWRYztNFTKGD4aQ+gwcv4xvH8tiukkblb p17g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:reply-to:message-id:subject:cc:to:from:date; bh=3soh9+WzKe19hMzanZb68pTJv1T90jSDjCRmnf0Ksok=; b=dkZCIpp3I7uOr80/ynVAEcyZb4nUXHbg2UhEl7BJPoO3Uv+I04QRd1+aoPGvfBHSWx uMS9yeebxp636IE1DMvtG37O07FbgP9tvgIfpsFczF9KJRWZEYp/4zn3vQh+4S/HVjnL kzkZj1vn21Uf2zE9kVO4h+wLfQuQYn/xq7MWQRifh6TuitOOu5C4BkzRjzlDEg9Y8Fja ynxKTW+PiYk/uxh7O5WzMRh+dGS3aIpoWst7sbkUuHcxw5a9dcDcVpVRQpVmCCgpPc9A TkrN1zNyMa0jms3PNqQdnGjHZNGJPlARiPKIGQvJDmrGbB8+u2yg2BfATD4sXzq8q615 Q6hA== 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=alibaba.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 96si231743oty.198.2020.03.10.18.19.01; Tue, 10 Mar 2020 18:19:13 -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=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727894AbgCKBRT (ORCPT + 99 others); Tue, 10 Mar 2020 21:17:19 -0400 Received: from out30-131.freemail.mail.aliyun.com ([115.124.30.131]:43914 "EHLO out30-131.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727484AbgCKBRT (ORCPT ); Tue, 10 Mar 2020 21:17:19 -0400 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R381e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04407;MF=richard.weiyang@linux.alibaba.com;NM=1;PH=DS;RN=6;SR=0;TI=SMTPD_---0TsFZ44m_1583889435; Received: from localhost(mailfrom:richard.weiyang@linux.alibaba.com fp:SMTPD_---0TsFZ44m_1583889435) by smtp.aliyun-inc.com(127.0.0.1); Wed, 11 Mar 2020 09:17:15 +0800 Date: Wed, 11 Mar 2020 09:17:15 +0800 From: Wei Yang To: Tim Chen Cc: Wei Yang , 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: <20200311011715.GA47198@L-31X9LVDL-1304.local> 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> <20200310222002.lr2vurqfk6jvfo2z@master> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Mar 10, 2020 at 04:03:07PM -0700, Tim Chen wrote: >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, I would rephrase v2. > >Thanks. > >Tim -- Wei Yang Help you, Help me