From patchwork Thu Feb 27 21:14:09 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Simmons X-Patchwork-Id: 11410739 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9F87E924 for ; Thu, 27 Feb 2020 21:45:41 +0000 (UTC) Received: from pdx1-mailman02.dreamhost.com (pdx1-mailman02.dreamhost.com [64.90.62.194]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 88503246A1 for ; Thu, 27 Feb 2020 21:45:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 88503246A1 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lustre-devel-bounces@lists.lustre.org Received: from pdx1-mailman02.dreamhost.com (localhost [IPv6:::1]) by pdx1-mailman02.dreamhost.com (Postfix) with ESMTP id 209E334B075; Thu, 27 Feb 2020 13:36:22 -0800 (PST) X-Original-To: lustre-devel@lists.lustre.org Delivered-To: lustre-devel-lustre.org@pdx1-mailman02.dreamhost.com Received: from smtp3.ccs.ornl.gov (smtp3.ccs.ornl.gov [160.91.203.39]) by pdx1-mailman02.dreamhost.com (Postfix) with ESMTP id E63E521FDAA for ; Thu, 27 Feb 2020 13:20:16 -0800 (PST) Received: from star.ccs.ornl.gov (star.ccs.ornl.gov [160.91.202.134]) by smtp3.ccs.ornl.gov (Postfix) with ESMTP id C414E8AB4; Thu, 27 Feb 2020 16:18:17 -0500 (EST) Received: by star.ccs.ornl.gov (Postfix, from userid 2004) id C300247C; Thu, 27 Feb 2020 16:18:17 -0500 (EST) From: James Simmons To: Andreas Dilger , Oleg Drokin , NeilBrown Date: Thu, 27 Feb 2020 16:14:09 -0500 Message-Id: <1582838290-17243-382-git-send-email-jsimmons@infradead.org> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1582838290-17243-1-git-send-email-jsimmons@infradead.org> References: <1582838290-17243-1-git-send-email-jsimmons@infradead.org> Subject: [lustre-devel] [PATCH 381/622] lustre: obdclass: generate random u64 max correctly X-BeenThere: lustre-devel@lists.lustre.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "For discussing Lustre software development." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Lai Siyao , Lustre Development List MIME-Version: 1.0 Errors-To: lustre-devel-bounces@lists.lustre.org Sender: "lustre-devel" From: Lai Siyao Generate random u64 max number correctly, and make it an obdclass function lu_prandom_u64_max(). Fixes: bcfa98a507 ("staging: lustre: replace cfs_rand() with prandom_u32_max()") WC-bug-id: https://jira.whamcloud.com/browse/LU-12495 Lustre-commit: 645b72c5c058 ("LU-12495 obdclass: generate random u64 max correctly") Signed-off-by: Lai Siyao Reviewed-on: https://review.whamcloud.com/35394 Reviewed-by: James Simmons Reviewed-by: Andreas Dilger Reviewed-by: Oleg Drokin Signed-off-by: James Simmons --- fs/lustre/include/lu_object.h | 1 + fs/lustre/lmv/lmv_qos.c | 26 +------------------------- fs/lustre/obdclass/lu_qos.c | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 25 deletions(-) diff --git a/fs/lustre/include/lu_object.h b/fs/lustre/include/lu_object.h index 0f3e3be..6b1064a 100644 --- a/fs/lustre/include/lu_object.h +++ b/fs/lustre/include/lu_object.h @@ -1390,6 +1390,7 @@ struct lu_qos { int lqos_add_tgt(struct lu_qos *qos, struct lu_tgt_desc *ltd); int lqos_del_tgt(struct lu_qos *qos, struct lu_tgt_desc *ltd); +u64 lu_prandom_u64_max(u64 ep_ro); /** @} lu */ #endif /* __LUSTRE_LU_OBJECT_H */ diff --git a/fs/lustre/lmv/lmv_qos.c b/fs/lustre/lmv/lmv_qos.c index e323398..85053d2e 100644 --- a/fs/lustre/lmv/lmv_qos.c +++ b/fs/lustre/lmv/lmv_qos.c @@ -370,31 +370,7 @@ struct lu_tgt_desc *lmv_locate_tgt_qos(struct lmv_obd *lmv, u32 *mdt) total_weight += tgt->ltd_qos.ltq_weight; } - if (total_weight) { -#if BITS_PER_LONG == 32 - /* - * If total_weight > 32-bit, first generate the high - * 32 bits of the random number, then add in the low - * 32 bits (truncated to the upper limit, if needed) - */ - if (total_weight > 0xffffffffULL) - rand = (u64)(prandom_u32_max( - (unsigned int)(total_weight >> 32)) << 32; - else - rand = 0; - - if (rand == (total_weight & 0xffffffff00000000ULL)) - rand |= prandom_u32_max((unsigned int)total_weight); - else - rand |= prandom_u32(); - -#else - rand = ((u64)prandom_u32() << 32 | prandom_u32()) % - total_weight; -#endif - } else { - rand = 0; - } + rand = lu_prandom_u64_max(total_weight); for (i = 0; i < lmv->desc.ld_tgt_count; i++) { tgt = lmv->tgts[i]; diff --git a/fs/lustre/obdclass/lu_qos.c b/fs/lustre/obdclass/lu_qos.c index 4ee3f59..9fdcbc2 100644 --- a/fs/lustre/obdclass/lu_qos.c +++ b/fs/lustre/obdclass/lu_qos.c @@ -35,6 +35,7 @@ #include #include +#include #include #include #include @@ -164,3 +165,38 @@ int lqos_del_tgt(struct lu_qos *qos, struct lu_tgt_desc *ltd) return rc; } EXPORT_SYMBOL(lqos_del_tgt); + +/** + * lu_prandom_u64_max - returns a pseudo-random u64 number in interval + * [0, ep_ro) + * + * #ep_ro right open interval endpoint + * + * Return: a pseudo-random 64-bit number that is in interval [0, ep_ro). + */ +u64 lu_prandom_u64_max(u64 ep_ro) +{ + u64 rand = 0; + + if (ep_ro) { +#if BITS_PER_LONG == 32 + /* + * If ep_ro > 32-bit, first generate the high + * 32 bits of the random number, then add in the low + * 32 bits (truncated to the upper limit, if needed) + */ + if (ep_ro > 0xffffffffULL) + rand = prandom_u32_max((u32)(ep_ro >> 32)) << 32; + + if (rand == (ep_ro & 0xffffffff00000000ULL)) + rand |= prandom_u32_max((u32)ep_ro); + else + rand |= prandom_u32(); +#else + rand = ((u64)prandom_u32() << 32 | prandom_u32()) % ep_ro; +#endif + } + + return rand; +} +EXPORT_SYMBOL(lu_prandom_u64_max);