Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp3824607rwe; Mon, 29 Aug 2022 22:05:46 -0700 (PDT) X-Google-Smtp-Source: AA6agR6E/tU4UjakkXiDZaixfDcG7y0fbnTNsAqgTLBbHovja+I6js+UcMUlTy/4seYgIkNf2a6d X-Received: by 2002:a17:90b:1a88:b0:1f7:3daa:f2f6 with SMTP id ng8-20020a17090b1a8800b001f73daaf2f6mr22162520pjb.245.1661835945964; Mon, 29 Aug 2022 22:05:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661835945; cv=none; d=google.com; s=arc-20160816; b=l+FimR9atz+owqSKVYF3O5IJ1qI5zbVgp48YxiRaEh7ndlsV3ZrPWXY1yGPkQRaa+b tNgmgmKOupV3Zcph2EDAgkC0rBD5MBzwwEXN5IEyJbeP/sZlbZn7Cd0CV9ED2nUZU3Te jT8M5LmNfgpoaNbfpwShzvUnxvvdtwIf7Ne62XwRns+3GlTRbl6LeAqR18mAyGf3e0od 0jWmmUzXagzxrUwQUNTKCL8f8x0pN5zo8NJfAN3xwoAdAwu5BPCgooKbqvkPaQf0v1tg 1L1Prf+9Sdja0XZXXjQA9i5+Bg+ZWN9XYH6CtAUYWGO2EuQEqgd/7ArHnQvy1qHeh5Lt h4LQ== 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:dkim-signature; bh=gkm9ZF11qnsrXUp6mwF300lhXi7QKePgAYd+Mxulk6k=; b=Xic3P4ztACTTd0LSC/uI8HlVWlnmZ66Gjv8lkj8tKYVRPSRqzRZ0jNCZcxHgzgH9hx VElbZVdZakF1gdGBK1d+AC2uSVGsT1/kKFFeA4rsEfomC1BzYiPtiZorug+slYJpwlxW Lp21BtMmTSmpxxGvJfB5TE8uAePI1z/LJcFrAjdRHsOJ9raSspd4tnMCrb3ChYiLd1Ou omYLxJV1wguqlQowRsFIzvf1camG4nqIWgJu6uiziWbWEnAyzorPxIK/hSutdq1ad8fH TuxMogUPWecGY6nfGW9167q6o/ILhyOrPDJjz5jAifKjnF2i4IEJNPX/KF6A8V1VxWZ9 Rg0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=2Fd+CNOv; 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=bytedance.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v7-20020a170902b7c700b001708e1bb3b8si10176091plz.335.2022.08.29.22.05.34; Mon, 29 Aug 2022 22:05:45 -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; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=2Fd+CNOv; 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=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229685AbiH3D5n (ORCPT + 99 others); Mon, 29 Aug 2022 23:57:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43606 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229591AbiH3D5k (ORCPT ); Mon, 29 Aug 2022 23:57:40 -0400 Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3C3E5AA371 for ; Mon, 29 Aug 2022 20:57:35 -0700 (PDT) Received: by mail-pl1-x636.google.com with SMTP id u22so9885283plq.12 for ; Mon, 29 Aug 2022 20:57:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc; bh=gkm9ZF11qnsrXUp6mwF300lhXi7QKePgAYd+Mxulk6k=; b=2Fd+CNOvbQZUVJI5uw9mDQDrU0f0myoKMYM9K6WPSupS2wBXw1tQYLhPYNXAcscIjJ insioEzVOSwIGGtiPdbICGXI3AO+2pyAGrN5X3nNtcjHe/OcAlKey87XfUILA35nD2Sx doF4rhCYcsTzLGI7PXdLK3hOdLEw1AvJmaq9FRWU1mkZRqpzod5dq+BK+oT+yEMBVYRz X3VA4qvKkuiE7+QXQtKbMx0inlmzWFlXe6TNQpX3tuoCCnx82TbMk7rI87BpAGOUzdEw YkriaqTq8SnfII9CPAlLHlYK9CCTrnrur5HKyCGBumLPD2dke99sAkZd5E18x8vXlA4S hXpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc; bh=gkm9ZF11qnsrXUp6mwF300lhXi7QKePgAYd+Mxulk6k=; b=JUfWE2EiJ3/ZwJmGU7ar0E6BE8rwWAksDGXO8u6QkEOkB9bCDVC5p9lOUHNGDsEs18 9fTj6S7DqvJXA8bEHMh31SNhCIpIDxvtwgc4TSG9pIctYzHkpsVEDmvIUyBFoh4agpVn ke+cmiajK3v1ZAzxPFSZNkIsGdl2ZruZU7XBWt00TpFhwTALIIIjSxfUdMN6Uvx5+anW Hn7yeGMEYgc0gNq9AiyveTOpRqBcHvNpsC33oXXXaLskarAPhJDrERQCrLLY2UW7jadU FNIOBe8wQTuiX3EuwlhTulfJVUuG5jSlEQ2ZnLW/La3J+T+qiJa6rhZhl1ysTwwfQ+Gz haRQ== X-Gm-Message-State: ACgBeo16+XjzfMx/5YUOhm8uc2IdfkzM57RbHEnVvNUhCT5xkmodDLG3 5s5FZqquNlnah1YEpdaSG0hjMQ== X-Received: by 2002:a17:902:9a44:b0:171:3541:2c75 with SMTP id x4-20020a1709029a4400b0017135412c75mr18957412plv.15.1661831854700; Mon, 29 Aug 2022 20:57:34 -0700 (PDT) Received: from [10.4.115.67] ([139.177.225.244]) by smtp.gmail.com with ESMTPSA id k18-20020aa79732000000b00539aa7f0b53sm418163pfg.104.2022.08.29.20.57.30 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 29 Aug 2022 20:57:34 -0700 (PDT) Message-ID: <0752da43-0e5c-54c9-4c82-bb966ff93b43@bytedance.com> Date: Tue, 30 Aug 2022 11:57:24 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.1.2 Subject: Re: [PATCH 1/7] mm: introduce common struct mm_slot Content-Language: en-US To: Andrew Morton Cc: willy@infradead.org, vbabka@suse.cz, hannes@cmpxchg.org, minchan@kernel.org, rppt@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20220829143055.41201-1-zhengqi.arch@bytedance.com> <20220829143055.41201-2-zhengqi.arch@bytedance.com> <20220829125134.9b05f9b8caf5da4bec8f31e8@linux-foundation.org> From: Qi Zheng In-Reply-To: <20220829125134.9b05f9b8caf5da4bec8f31e8@linux-foundation.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham 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 2022/8/30 03:51, Andrew Morton wrote: > On Mon, 29 Aug 2022 22:30:49 +0800 Qi Zheng wrote: > >> At present, both THP and KSM module have similar structures >> mm_slot for organizing and recording the information required >> for scanning mm, and each defines the following exactly the >> same operation functions: >> >> - alloc_mm_slot >> - free_mm_slot >> - get_mm_slot >> - insert_to_mm_slots_hash >> >> In order to de-duplicate these codes, this patch introduces a >> common struct mm_slot, and subsequent patches will let THP and >> KSM to use it. > > Seems like a good idea. > >> --- /dev/null >> +++ b/mm/mm_slot.h >> @@ -0,0 +1,55 @@ >> +// SPDX-License-Identifier: GPL-2.0 >> + >> +#ifndef _LINUX_MM_SLOT_H >> +#define _LINUX_MM_SLOT_H >> + >> +#include >> +#include >> + >> +/* >> + * struct mm_slot - hash lookup from mm to mm_slot >> + * @hash: link to the mm_slots hash list >> + * @mm_node: link into the mm_slots list >> + * @mm: the mm that this information is valid for >> + */ >> +struct mm_slot { >> + struct hlist_node hash; >> + struct list_head mm_node; >> + struct mm_struct *mm; >> +}; > > It appears that the presence of an mm_struct in the hash list does not > contribute to the mm_struct's refcount? That's somewhat unexpected. Hi, The reason is that khugepaged_exit()/ksm_exit() will be called first in __mmput() to remove mm from the linked list. So it is prevented the mm_struct from getting freed while on the list. > > It would be helpful to add some words here describing the means by > which a user of mm_slot would prevent the mm_struct from getting freed > while on the list. I assume "caller must maintain a reference on the > mm_struct while it remains on an mm_slot hash list"? > >> +#define mm_slot_entry(ptr, type, member) \ >> + container_of(ptr, type, member) >> + >> +static inline void *alloc_mm_slot(struct kmem_cache *cache) >> +{ >> + if (!cache) /* initialization failed */ >> + return NULL; >> + return kmem_cache_zalloc(cache, GFP_KERNEL); >> +} >> + >> +static inline void free_mm_slot(struct kmem_cache *cache, void *objp) >> +{ >> + kmem_cache_free(cache, objp); >> +} >> + >> +#define get_mm_slot(_hashtable, _mm) \ >> +({ \ >> + struct mm_slot *tmp_slot, *mm_slot = NULL; \ >> + \ >> + hash_for_each_possible(_hashtable, tmp_slot, hash, (unsigned long)_mm) \ >> + if (_mm == tmp_slot->mm) { \ >> + mm_slot = tmp_slot; \ >> + break; \ >> + } \ >> + \ >> + mm_slot; \ >> +}) > > Is there a reason why this must be implemented as a macro? That's Since _hashtable is an array name, IIUC, this cannot be passed as a function parameter, so I chose to implement it as a macro. > preferable, although this may be overly large for inlining. mm/util.c > might suit. > >> +#define insert_to_mm_slots_hash(_hashtable, _mm, _mm_slot) \ >> +({ \ >> + _mm_slot->mm = _mm; \ >> + hash_add(_hashtable, &_mm_slot->hash, (unsigned long)_mm); \ >> +}) > > Does this need to be a macro? Ditto. > > > And the naming. Can we please have > > mm_slot_entry > mm_slot_alloc > mm_slot_free > mm_slot_get > mm_slot_insert > > Also, "get" usually implies that a refcout is taken on the obtained > object, so mm_slot_lookup() would be more appropriate. These names are better, will modify to it in the next version. Thanks, Qi -- Thanks, Qi