Received: by 2002:a05:7412:ba23:b0:fa:4c10:6cad with SMTP id jp35csp253463rdb; Thu, 18 Jan 2024 02:31:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IHm5rjPfK4lUArXAtxGDODS94Y8wDlqL7zyaeJDNcaXcX00OcLBos209W+BHMiZthEk2Mgj X-Received: by 2002:a0c:aa57:0:b0:681:6c3b:a5a6 with SMTP id e23-20020a0caa57000000b006816c3ba5a6mr417808qvb.10.1705573866284; Thu, 18 Jan 2024 02:31:06 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705573866; cv=pass; d=google.com; s=arc-20160816; b=xII8HrVtESMbufYAhF/kFotOjWTi0mVv+rRBtYsOLr+b57c4IVdfE4TCioWeaZeYbZ TmNK+v27S24r2E+rdD2pJB/RDvOQSwa4wgXxKf6eXAnT3bz+9Xdf+s0/hUEEJk5bVVFa GYuHXy/D/ey72HBxqgnhQn4CvQqhO/5p53XDO8zi8AxIOZFBVRU7ZrOqvfMGAB6dXnoE +imMxdWiYKP5fE3ds4TwKf1ySx4JxIpvBUkhRO0MxZJ+neWH+YUA27Twk73l5K4tKHZl 6NEMmAHm3NM6Es25Om29Tnzp6st4agsCXIPA2CnHFjr74JjzYuufdV0p+sVrLfZ/5CBp soCw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=puh0I2pZNJUghfANUr9RC+vshk+nAWk8JdQSAu06TD0=; fh=qGAKtm2PD+NxJ1Hk/Ovt7q824zeHFTw93aNWsehQ8wc=; b=kVkEAqNzbOCFNicCvE5qAZs9O9WZuaK5bnV5x6O99omq0Gw0eiBh7Lb6qqbuJaSsyr C+qmtmFOSF+jCCLCCWnG3Mfn7qu/wuoo264/bMCB/bflP7gPpNGmsdoUpRN6NSRmluM2 5nmm+2KgLTAhiz0NKrZUCkgQydPw1ZS15Tgz3cb9vvXJccBO3L2CMu/OFDuuFGU+fehD iW0lK8ZJskC9q8PQPvC4JJ4WfgzXLClnxCakk13QTR+5ezgQ6jQhEhe40eK37j22+3Dm MyUpSz5vUUJ0Hk7rMW34sdkMhOIz5Z8zJ1E3DgvpRw/RbjovjTxVvhnhKXzRL8gC3hYD 6w7Q== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=mZcDyamJ; arc=pass (i=1 spf=pass spfdomain=linaro.org dkim=pass dkdomain=linaro.org dmarc=pass fromdomain=linaro.org); spf=pass (google.com: domain of linux-kernel+bounces-30008-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-30008-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id q11-20020a05621419eb00b0068174e47acesi4530060qvc.290.2024.01.18.02.31.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Jan 2024 02:31:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-30008-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=mZcDyamJ; arc=pass (i=1 spf=pass spfdomain=linaro.org dkim=pass dkdomain=linaro.org dmarc=pass fromdomain=linaro.org); spf=pass (google.com: domain of linux-kernel+bounces-30008-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-30008-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 033171C21D1A for ; Thu, 18 Jan 2024 10:31:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E3FAB20B06; Thu, 18 Jan 2024 10:31:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="mZcDyamJ" Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5C50EA42 for ; Thu, 18 Jan 2024 10:30:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705573860; cv=none; b=TNJ5plR/zkMSFTi+0Tr517h/XXJdnJzboKj4syVlGH0Vz2FVPq34ZJ3v8ZTnXvDgQBGOMES7hwn1KEspLkjJ03Zaunp4bhAshdyftRnse/Tm0qAKr3wAM0V/brilU/NtpECVs9bq1o98FbVid0xg0r9PyCcBnfSqat5s8HljJ6I= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705573860; c=relaxed/simple; bh=2LaYni2ALdyLXX8C/p+uyNMU3L7D1TXFe76UtWyOVDY=; h=Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Google-Smtp-Source:X-Received:Received:Date: From:To:Cc:Subject:Message-ID:References:MIME-Version:Content-Type: Content-Disposition:In-Reply-To; b=OzIKhBEqzUK64eLG85JvbXEMYgJF8pIhIw2F0xJfaZO22icssH22YFy6M4g1SsqEZEWtbXc+6DUgBIRS6UJynwe7XBfT5XO0EygyucTWWB2aA1aLRYLIljS/9VNIbCwxAxUzoPZyRaRtLHz9vNwRU8AL6taUqok/boTbO4Ax6vM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=mZcDyamJ; arc=none smtp.client-ip=209.85.128.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-40e7065b7bdso50262705e9.3 for ; Thu, 18 Jan 2024 02:30:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1705573855; x=1706178655; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=puh0I2pZNJUghfANUr9RC+vshk+nAWk8JdQSAu06TD0=; b=mZcDyamJLKo1+JiN+Db7i8s0/rsQBdQmaXt2v4BtbVOLV8lIBExbZlMbBTO8R1k2bM 9sqWm1nLXS5wEeXNcOATUmETgEzr7gZac2PTl9g6F9mN55ZBw86p7IRl2uYnfLqbG5qR xv7aTE49sE5gNwPntHGQB1MGGrnVzz1evVa+FspCCcJ+tPMuXLGpnfOn0eGWI47yC7V2 W3drzTCHqP5mlPnJkXsUED8NqW4FaHWDTpY+KmLUvbhai2Vv9PrEfVFLCbqsVIKvDKJX Robuh101vh587AGqEaB7pPiT9y/cizBcoSkRc31aaPMfzpjJCtv8wKBl5XhMG3ujg6hl y56Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705573855; x=1706178655; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=puh0I2pZNJUghfANUr9RC+vshk+nAWk8JdQSAu06TD0=; b=TFQbjo7prxy1N7v9mCkAt3PeixkpjMW5dewwLohc1gEp6g7ae4fySl8y9gBzHO4ot+ T83h2emdyIQoV9KN26uHRqFjJIq6HRrt4SeJygPPTsdUsD0sjICTsorvniPVcr4fSzSC 92+i50jkA6mpeMA+D4iSGQEZdaLTjFbt39HVmnKVMZi5FDGGE0Dxiyh1ZqsFJ5MuszdX ydtAdmHM1SnVllhf0FCWwPzAiE7FyEtJQBS10l9hd7mMiz4TYhOrJ93tFYwI/ndnDGEq wz+BdrWHhj6KRJl3YeDI+XgRlEqRJFLsDzi3vSXYZX9KDDgxIVEOsrVjtWUIVnbQnMF1 M6Yg== X-Gm-Message-State: AOJu0Yw7yhJzPWjnKfYP3jMuZc92K6PkFgac1fG1DWledq7MEzDVrE8Y xBTq58acZN03UtoO4cv/72MObkbWB25cIBSYrCzgdO6YgR0heojH4soql4vFQFo= X-Received: by 2002:a7b:c399:0:b0:40e:5d1b:b1a1 with SMTP id s25-20020a7bc399000000b0040e5d1bb1a1mr271744wmj.67.1705573855555; Thu, 18 Jan 2024 02:30:55 -0800 (PST) Received: from localhost ([102.140.209.237]) by smtp.gmail.com with ESMTPSA id z12-20020a5d640c000000b00337bcef9193sm3711367wru.44.2024.01.18.02.30.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Jan 2024 02:30:55 -0800 (PST) Date: Thu, 18 Jan 2024 13:30:52 +0300 From: Dan Carpenter To: David Laight Cc: "linux-kernel@vger.kernel.org" , Linus Torvalds , 'Andy Shevchenko' , 'Andrew Morton' , "'Matthew Wilcox (Oracle)'" , 'Christoph Hellwig' , "'Jason A. Donenfeld'" Subject: Re: [PATCH next v4 1/5] minmax: Add umin(a, b) and umax(a, b) Message-ID: References: <41d93ca827a248698ec64bf57e0c05a5@AcuMS.aculab.com> <737627fd-b68b-4c9d-8700-f0e0d6d9cec8@moroto.mountain> <8e45b321c49b4c27a61b2db076ed5383@AcuMS.aculab.com> <02701430-65cf-44ab-8a8b-752c5d973d21@moroto.mountain> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Fri, Jan 12, 2024 at 02:26:33PM +0000, David Laight wrote: > From: Dan Carpenter > > Sent: 12 January 2024 14:03 > > > > On Fri, Jan 12, 2024 at 01:40:30PM +0000, David Laight wrote: > > > From: Dan Carpenter > > > > Sent: 12 January 2024 12:50 > > > > > > > > On Mon, Sep 18, 2023 at 08:16:30AM +0000, David Laight wrote: > > > > > +/** > > > > > + * umin - return minimum of two non-negative values > > > > > + * Signed types are zero extended to match a larger unsigned type. > > > > > + * @x: first value > > > > > + * @y: second value > > > > > + */ > > > > > +#define umin(x, y) \ > > > > > + __careful_cmp((x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull, <) > > > > > > > > Why do we match "a larger unsigned type" instead of ULL_MAX? Presumably > > > > it helps performance somehow... I agree that it's probably fine but I > > > > would be more comfortable if it skipped UINT_MAX and jumped directly to > > > > ULONG_MAX. These days 4 gigs is small potatoes. The vmalloc() function > > > > can allocate 4G so we've had integer overflow bugs with this before. > > > > > > The '+ 0ul*' carefully zero extend signed values without changing > > > unsigned values. > > > The compiler detects when it has zero-extended both sides and > > > uses the smaller compare. > > > In essence: > > > x + 0u converts 'int' to 'unsigned int'. > > > Avoids the sign extension adding 0ul on 64bit. > > > x + 0ul converts a 'long' to 'unsigned long'. > > > Avoids the sign extension adding 0ull on 32bit > > > x + 0ull converts a 'long long' to 'unsigned long long'. > > > You need all three to avoid sign extensions and get an unsigned > > > compare. > > > > So unsigned int compares are faster than unsigned long compares? > > > > It's just sort of weird how it works. > > > > min_t(unsigned long, -1, 10000000000)); => 10000000000 > > umin(umin(-1, 10000000000)); => UINT_MAX > > > > UINT_MAX is just kind of a random value. I would have prefered > > ULONG_MAX, it's equally random but it's more safe because nothing can > > allocate ULONG_MAX bytes. > > umin() is only defined for non-negative values. I'm so confused by this. To me the big selling point of min_t() was that it clamps things to between zero and the max. > So that example is really outside the domain of the function. > > Consider: > int x = some_positive_value; > unsigned long long y; > then: > min_t(unsigned long long, x, y); > Does (unsigned long long)x which is (unsigned long long)(long long)x > and requires that x be sign extended to 64bits. > On 32bit that is quite horrid. I wasn't saying jump straight to ull. I was suggesting jump to ul then ull, but skip uint. On a 32bit system, you can't allocate ULONG_MAX bytes, so it still ends up being quite a safe number. regards, dan carpenter