Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp4201634rwl; Mon, 3 Apr 2023 01:09:40 -0700 (PDT) X-Google-Smtp-Source: AKy350YeQjWOxdEEB3YlI4MQGmS1VNNFdSvLppmdSLvXs7Sl3xcOAODBkNIZsw44i+kA1qB7BTpL X-Received: by 2002:a62:19cd:0:b0:62d:944e:b0b8 with SMTP id 196-20020a6219cd000000b0062d944eb0b8mr22962535pfz.32.1680509380664; Mon, 03 Apr 2023 01:09:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680509380; cv=none; d=google.com; s=arc-20160816; b=cIe1GiOJ3scEQOwg8XOb1talPXyk1PQBX7s5XAXWwMxY8mZai1VNqaFA/juPBADwXE trlEIK+ZNKh7wBE/sBNqPZS3yjgzkD9pUTUtkysQwDiW2Qj/9G5cQ+A7RcepQgJfTHKz uyt9/xoklNR8QJgG2fMpjabW5KqbDjxSi7R8Lqf21QtbqerYi73uj81delHgwhirJUX5 gnzGRWq0cVQaWtUoC+D5g09NeVdBkyIGy77+UJ8vaz+2zL2pz30NJEkxPrPi5TmxgYRs 9yfu6sBLvhgCpZ7L1uJnGjVBM+9JciZtgO926cdBpR+fBGLdEdXrdCX/p92kFLEF7qVE w3aA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=H+a2++dXfWS+mlkkv4vg4uMFcYoxO8xAGb5AUBZZMKc=; b=X2g5wb9IliFUNFC0aFqUADgqZCwhQUtqSiBR9ahzNldRDwnBrI89yl3hNx+E8myROa ZxG//x1+JDkZiQvPJsxpQzTGyabYCUWSJoI73A/O3OA/YjkfWzZjiMR/bU6jBfEBp4Gk yh34hiR4q/reA9kt/bPygNGe3DXN1km5Pv1KoE4GhQFQCsB+iA2x5DnrsM0cMNN4G7hV G+3cxfQEkaQAv/eAmImenK3xElQsY42kHiyFEPXwz+OOVCp6XcSj/fnO8SXvi8ZvJ3tZ AIXm+qIe/akwnb+yBsKCHU/WDAkxOjbHzG4qQj4X5hnOa9J+DpR0DazquUGf9awDlYcm 3ACA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i1-20020a655b81000000b005130d9f6e66si7992514pgr.366.2023.04.03.01.09.28; Mon, 03 Apr 2023 01:09:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S231706AbjDCICl (ORCPT + 99 others); Mon, 3 Apr 2023 04:02:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55728 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229659AbjDCICj (ORCPT ); Mon, 3 Apr 2023 04:02:39 -0400 Received: from out30-130.freemail.mail.aliyun.com (out30-130.freemail.mail.aliyun.com [115.124.30.130]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0DFC41725; Mon, 3 Apr 2023 01:02:37 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R831e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046059;MF=rongwei.wang@linux.alibaba.com;NM=1;PH=DS;RN=5;SR=0;TI=SMTPD_---0VfFRiwW_1680508954; Received: from 30.24.98.140(mailfrom:rongwei.wang@linux.alibaba.com fp:SMTPD_---0VfFRiwW_1680508954) by smtp.aliyun-inc.com; Mon, 03 Apr 2023 16:02:35 +0800 Message-ID: <4301a7be-354f-183d-a828-01445434a1b3@linux.alibaba.com> Date: Mon, 3 Apr 2023 16:02:34 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] mm/swap: fix swap_info_struct race between swapoff and get_swap_pages() Content-Language: en-US To: Matthew Wilcox Cc: akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org References: <20230401221920.57986-1-rongwei.wang@linux.alibaba.com> From: Rongwei Wang In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-8.0 required=5.0 tests=ENV_AND_HDR_SPF_MATCH, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, UNPARSEABLE_RELAY,USER_IN_DEF_SPF_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 4/3/23 12:10 PM, Matthew Wilcox wrote: > On Sun, Apr 02, 2023 at 06:19:20AM +0800, Rongwei Wang wrote: >> Without this modification, a core will wait (mostly) >> 'swap_info_struct->lock' when completing >> 'del_from_avail_list(p)'. Immediately, other cores >> soon calling 'add_to_avail_list()' to add the same >> object again when acquiring the lock that released >> by former. It's not the desired result but exists >> indeed. This case can be described as below: > This feels like a very verbose way of saying > > "The si->lock must be held when deleting the si from the > available list. Otherwise, another thread can re-add the > si to the available list, which can lead to memory corruption. > The only place we have found where this happens is in the > swapoff path." It looks better than mine. Sorry for my confusing description, it will be fixed in the next version. > >> +++ b/mm/swapfile.c >> @@ -2610,8 +2610,12 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) >> spin_unlock(&swap_lock); >> goto out_dput; >> } >> - del_from_avail_list(p); >> + /* >> + * Here lock is used to protect deleting and SWP_WRITEOK clearing >> + * can be seen concurrently. >> + */ > This comment isn't necessary. But I would add a lockdep assert inside > __del_from_avail_list() that p->lock is held. Thanks. Actually, I have this line in previous test version, but delete for saving one line of code. I will update here as you said. Thanks for your time. > >> spin_lock(&p->lock); >> + del_from_avail_list(p); >> if (p->prio < 0) { >> struct swap_info_struct *si = p; >> int nid; >> -- >> 2.27.0 >> >>