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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 907E2CD128A for ; Wed, 3 Apr 2024 16:52:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 05C286B0085; Wed, 3 Apr 2024 12:52:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 03CD66B0087; Wed, 3 Apr 2024 12:52:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E3C776B0089; Wed, 3 Apr 2024 12:52:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id C6E206B0085 for ; Wed, 3 Apr 2024 12:52:09 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 78BB6160995 for ; Wed, 3 Apr 2024 16:52:09 +0000 (UTC) X-FDA: 81968813178.18.5095D57 Received: from frasgout.his.huawei.com (frasgout.his.huawei.com [185.176.79.56]) by imf06.hostedemail.com (Postfix) with ESMTP id 6747818000E for ; Wed, 3 Apr 2024 16:52:06 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf06.hostedemail.com: domain of jonathan.cameron@huawei.com designates 185.176.79.56 as permitted sender) smtp.mailfrom=jonathan.cameron@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712163127; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PMHlQ8Bd5z56MpkmUHt900+91cfIO1dEI/shvXMajMI=; b=0qaD6WNfZTjDuGZclaz2m7EQAqYn8kryP/gMMa1AO48qn0C9OV45BmzUvMTsCL9feyT/wh E2KY4V+dpCYJdQ7xDagt3ieLfD31SQFo/jjptTinOJHA1WOPIkCmh+Zvz/T533j4PyuMpe D/TDOSSxVUj7RDOsdNphruU78X+VP3E= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf06.hostedemail.com: domain of jonathan.cameron@huawei.com designates 185.176.79.56 as permitted sender) smtp.mailfrom=jonathan.cameron@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712163127; a=rsa-sha256; cv=none; b=u8uVolJiEZshSvLfKP+BzjMx30164rh2ZDJvC1CULgT+UDrKmWpf8DngohN8Uo4fJk/cjU lIXFai2Sz2Py2q1vGTM+C+VN6TO7cgqRgmlnby3jhKZIIMueV9I6t4XBz/Mb/VD2c0nH9J 1aC8CLQLibv6OUyHwVSG31p8zJkKSXs= Received: from mail.maildlp.com (unknown [172.18.186.216]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4V8rNM5jKhz6D8Yx; Thu, 4 Apr 2024 00:50:43 +0800 (CST) Received: from lhrpeml500005.china.huawei.com (unknown [7.191.163.240]) by mail.maildlp.com (Postfix) with ESMTPS id 8C7A0140B67; Thu, 4 Apr 2024 00:52:03 +0800 (CST) Received: from localhost (10.202.227.76) by lhrpeml500005.china.huawei.com (7.191.163.240) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Wed, 3 Apr 2024 17:52:02 +0100 Date: Wed, 3 Apr 2024 17:52:01 +0100 From: Jonathan Cameron To: "Ho-Ren (Jack) Chuang" CC: "Huang, Ying" , Gregory Price , , , , , Eishan Mirakhur , Vinicius Tavares Petrucci , Ravis OpenSrc , Alistair Popple , Srinivasulu Thanneeru , SeongJae Park , Dan Williams , Vishal Verma , "Dave Jiang" , Andrew Morton , , , , , "Ho-Ren (Jack) Chuang" , "Ho-Ren (Jack) Chuang" , Subject: Re: [PATCH v10 1/2] memory tier: dax/kmem: introduce an abstract layer for finding, allocating, and putting memory types Message-ID: <20240403175201.00000c2c@Huawei.com> In-Reply-To: <20240402001739.2521623-2-horenchuang@bytedance.com> References: <20240402001739.2521623-1-horenchuang@bytedance.com> <20240402001739.2521623-2-horenchuang@bytedance.com> Organization: Huawei Technologies Research and Development (UK) Ltd. X-Mailer: Claws Mail 4.1.0 (GTK 3.24.33; x86_64-w64-mingw32) MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Originating-IP: [10.202.227.76] X-ClientProxiedBy: lhrpeml500004.china.huawei.com (7.191.163.9) To lhrpeml500005.china.huawei.com (7.191.163.240) X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 6747818000E X-Stat-Signature: 1fc9hf4nagwoajgjtqmkzaoac6dpgsuz X-Rspam-User: X-HE-Tag: 1712163126-377629 X-HE-Meta: U2FsdGVkX1/fC3e2/qX2PBaXE6BvAqeNbuRHObXfKy6zyVMwffFl9TuvXMKD595VY1U0DRPvWSEQ+saSDUexr5DjM+TUqsTuuQwZ8kRxONqu8GmuTn3A4fisXtUw9MrUDOMzcbuo6YOBjv2n5Fb/QmBicgLLurJyjlMhl+gUh9lGGk6jeUAIqsuMNwqMMdzuORl9feeQmAA/iFGGHLRpbD4JXv/ZWimPQhHGXPNBVQD6TpCAim6sXGShwPlNosh4daBmhUMG5b3N2UqQ5nJHTDQSY7ci8H64Vu5twqTdbNKTRGqnsMtKvPkq68V5vqZH8cc/hLwCGMUL53IIqOe8xwELQ46CJ4eVjcTzOL/mPO8ues/Mxe2hiOFpiaa52/ljjEbqCN0v6Hp5QEqm5xWub/Sm1/mq6XuoJt3TASNGqMZRkRto6ZuBavqci/rZMp9vHAXrFsurO1STzhcsQgsqWoEg+uMUYzsKNjWKex0hdbKFJO+mu81wdx17armsGgZgA+gn/TYZQCWVNlQWB1A5a2RAHyZkkdDPI19nK+P9DOcH4UidNUzn86emRNHfxSyuRcOBfULYyURg4Tfa/A1AxlYVzsHpNpv/AAa13hXCc9x6lqG2evWGGSCKI02fu51LfEeS/M/YRP4vJTCQO+cUx/WfcCU6pJkBqf8XTYVjjqfW/baRqiRWb8XvwcPzA1sL6jNto6e6lxpFP2Ijo0h7CAv0F+NVRCEZmG2NiBFDwGHWG7xYmWe6QiyFtAPtuu8c8UPR573vkhhWJfvWBIvav12M617ifJeYgdEPXBvpphLpUJFtgEMHXFU94NmGUeWMUUImi2VulxACNPdzvHDsxVO3IDmPeCnr5B6cdR0It9r1alDPm9AWSKyzAkcQrJAgdgWE+8sekMG2lABfBqcv3FZgokH66YQhoMjtqizsb3wamNw57iYIFR7UAm97Xh4XZRXrKEqiUQo= 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: List-Subscribe: List-Unsubscribe: On Tue, 2 Apr 2024 00:17:37 +0000 "Ho-Ren (Jack) Chuang" wrote: > Since different memory devices require finding, allocating, and putting > memory types, these common steps are abstracted in this patch, > enhancing the scalability and conciseness of the code. > > Signed-off-by: Ho-Ren (Jack) Chuang > Reviewed-by: "Huang, Ying" Hi, I know this is a late entry to the discussion but a few comments inline. (sorry I didn't look earlier!) All opportunities to improve code complexity and readability as a result of your factoring out. Jonathan > --- > drivers/dax/kmem.c | 20 ++------------------ > include/linux/memory-tiers.h | 13 +++++++++++++ > mm/memory-tiers.c | 32 ++++++++++++++++++++++++++++++++ > 3 files changed, 47 insertions(+), 18 deletions(-) > > diff --git a/drivers/dax/kmem.c b/drivers/dax/kmem.c > index 42ee360cf4e3..01399e5b53b2 100644 > --- a/drivers/dax/kmem.c > +++ b/drivers/dax/kmem.c > @@ -55,21 +55,10 @@ static LIST_HEAD(kmem_memory_types); > > static struct memory_dev_type *kmem_find_alloc_memory_type(int adist) > { > - bool found = false; > struct memory_dev_type *mtype; > > mutex_lock(&kmem_memory_type_lock); could use guard(mutex)(&kmem_memory_type_lock); return mt_find_alloc_memory_type(adist, &kmem_memory_types); I'm fine if you ignore this comment though as may be other functions in here that could take advantage of the cleanup.h stuff in a future patch. > - list_for_each_entry(mtype, &kmem_memory_types, list) { > - if (mtype->adistance == adist) { > - found = true; > - break; > - } > - } > - if (!found) { > - mtype = alloc_memory_type(adist); > - if (!IS_ERR(mtype)) > - list_add(&mtype->list, &kmem_memory_types); > - } > + mtype = mt_find_alloc_memory_type(adist, &kmem_memory_types); > mutex_unlock(&kmem_memory_type_lock); > > return mtype; > diff --git a/include/linux/memory-tiers.h b/include/linux/memory-tiers.h > index 69e781900082..a44c03c2ba3a 100644 > --- a/include/linux/memory-tiers.h > +++ b/include/linux/memory-tiers.h > @@ -48,6 +48,9 @@ int mt_calc_adistance(int node, int *adist); > int mt_set_default_dram_perf(int nid, struct access_coordinate *perf, > const char *source); > int mt_perf_to_adistance(struct access_coordinate *perf, int *adist); > +struct memory_dev_type *mt_find_alloc_memory_type(int adist, > + struct list_head *memory_types); That indent looks unusual. Align the start of struct with start of int. > +void mt_put_memory_types(struct list_head *memory_types); > #ifdef CONFIG_MIGRATION > int next_demotion_node(int node); > void node_get_allowed_targets(pg_data_t *pgdat, nodemask_t *targets); > @@ -136,5 +139,15 @@ static inline int mt_perf_to_adistance(struct access_coordinate *perf, int *adis > { > return -EIO; > } > + > +struct memory_dev_type *mt_find_alloc_memory_type(int adist, struct list_head *memory_types) > +{ > + return NULL; > +} > + > +void mt_put_memory_types(struct list_head *memory_types) > +{ > + No blank line needed here. > +} > #endif /* CONFIG_NUMA */ > #endif /* _LINUX_MEMORY_TIERS_H */ > diff --git a/mm/memory-tiers.c b/mm/memory-tiers.c > index 0537664620e5..974af10cfdd8 100644 > --- a/mm/memory-tiers.c > +++ b/mm/memory-tiers.c > @@ -623,6 +623,38 @@ void clear_node_memory_type(int node, struct memory_dev_type *memtype) > } > EXPORT_SYMBOL_GPL(clear_node_memory_type); > > +struct memory_dev_type *mt_find_alloc_memory_type(int adist, struct list_head *memory_types) Breaking this out as a separate function provides opportunity to improve it. Maybe a follow up patch makes sense given it would no longer be a straight forward code move. However in my view it would be simple enough to be obvious even within this patch. > +{ > + bool found = false; > + struct memory_dev_type *mtype; > + > + list_for_each_entry(mtype, memory_types, list) { > + if (mtype->adistance == adist) { > + found = true; Why not return here? return mtype; > + break; > + } > + } > + if (!found) { If returning above, no need for found variable - just do this unconditionally. + I suggest you flip logic for simpler to follow code flow. It's more code but I think a bit easier to read as error handling is out of the main simple flow. mtype = alloc_memory_type(adist); if (IS_ERR(mtype)) return mtype; list_add(&mtype->list, memory_types); return mtype; > + mtype = alloc_memory_type(adist); > + if (!IS_ERR(mtype)) > + list_add(&mtype->list, memory_types); > + } > + > + return mtype; > +} > +EXPORT_SYMBOL_GPL(mt_find_alloc_memory_type); > + > +void mt_put_memory_types(struct list_head *memory_types) > +{ > + struct memory_dev_type *mtype, *mtn; > + > + list_for_each_entry_safe(mtype, mtn, memory_types, list) { > + list_del(&mtype->list); > + put_memory_type(mtype); > + } > +} > +EXPORT_SYMBOL_GPL(mt_put_memory_types); > + > static void dump_hmem_attrs(struct access_coordinate *coord, const char *prefix) > { > pr_info(