Received: by 2002:ab2:7855:0:b0:1f9:5764:f03e with SMTP id m21csp196681lqp; Wed, 22 May 2024 01:27:57 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWBw4lk4A11mPXrQfFli7SFES8sIPFSpWqfsc8zQO2eMpqRMOvNv+ClOeLE8uGCpW3nANJXIct2LsLBxmFHOmUoQNkmDmxOyitUqm8ATA== X-Google-Smtp-Source: AGHT+IFmBKBQn4l8+lgmzUwIhv8VbAv2KUhCP/tlkQXsQkETVuc4eLluhW1H6552GyPhXcOaS23N X-Received: by 2002:ac8:598e:0:b0:43a:3fce:32c2 with SMTP id d75a77b69052e-43f7a28dfd5mr183246131cf.3.1716366476988; Wed, 22 May 2024 01:27:56 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716366476; cv=pass; d=google.com; s=arc-20160816; b=bDfEYAYZbuXNTJNbMTYKK6PMvVSnrEYEcUr8BhCVhVS2nzWfTUoWTsKvCLC4LTm+8k utdEgNylqR/mO7VF5apTRp4kf7T1lwnKPRbd7ubdgBl00wu+baPU3qRFitOl/GY3zyY3 t6LnzQZVFz9Ja7f427YIvl+vV5sthp1Dpe3UL4suTn+kKR2ssPeIaMK+LpBa2GrA/DFE 0wrXByMdAS11cDqJMD37sgEeks8RjA7nOBsyxoAniKL4sPkliuwXw2fILUuXuKA7VSll A7XEYVtDLShib5Lhl3QoIR/enxlBJ6QtEplBKThZqEQtUn+ql+0YGA3sTpK6pEXNVPUz 9zMQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=yc4M1An1SVE3VhBwugQSFEPj2HdsFrg3eqR9/azRDhc=; fh=jMKrEAqmGprrMN8BEKPvHfuYsclZqQip42uVq2wt130=; b=XTlpktX6z/9ZuZH4BEep8M3cMvVvKaJb44iOBZaTlqrBrKYCAthAWCTF2hzKUKuPoe lOUAnbpjovcDEhWjPkNOIfdvcUegEq/NIF4g/0qNvtcqhNEa1vc9tulNMZenlWypi0AD idJIa7NJzH6ffP8o7k/ayen0E3UOXzjlBYt/vq+5PY7v5mL7wNQt+9WRfbkXRo8ArNn7 K9+Bf6hnJ3QkAMREnqMe4fLIs9gHpzu9nL0XXfP23raH0j2njEIvRR/S1QS3K7yInKZf MvRN7vDCbAoTHmBqNm5FgKCz2yI3++BpGr3gdOQVOa9ARTij8qeRqq498EGfPxkY/Cp6 BOCg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=lBJoDPSR; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-185924-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-185924-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id d75a77b69052e-43f979f7237si29042031cf.463.2024.05.22.01.27.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 May 2024 01:27:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-185924-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=lBJoDPSR; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-185924-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-185924-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com 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 ACD7E1C20D00 for ; Wed, 22 May 2024 08:27:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 68F558004F; Wed, 22 May 2024 08:27:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="lBJoDPSR" Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (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 0C3057FBA2 for ; Wed, 22 May 2024 08:27:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716366457; cv=none; b=mfjS/sJ/uQJgn44m48PstnD4n48IEZBLap6ISNyk9bdbfu2jc7Bybin9SPxNsKYB7KY+bkml7EZ4dRwfFgpZ0aU9u3yPV3iJQqhb/S6JWlraqdrKEsmwXl6T70kwuPMwVD8sgQRNSI4xT3KS8pk1XSatcBei14bFaoxcvDKSaZY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716366457; c=relaxed/simple; bh=y3VxHR/Yb4X/Ib/4X02OEJsh/+G/11i5weZ0MaiR8kM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KOxwdi7+rqsqeSdJ/ZRJUd0EGnmNX+qpkFKFdjelj8tUTGcgPpSacchuM/s9/qxAR+urCTXRlxPpiWbKH5yBVBuUC7gboOqvUCpu7mqj6dEeDpaGkTsIyhlDLSVEnEb/hRo7JvpGVch5/Acb1CBC1hS2XU/S37m7GQHUXqNXMic= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=lBJoDPSR; arc=none smtp.client-ip=209.85.128.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-420180b5897so5032475e9.3 for ; Wed, 22 May 2024 01:27:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716366454; x=1716971254; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=yc4M1An1SVE3VhBwugQSFEPj2HdsFrg3eqR9/azRDhc=; b=lBJoDPSRb9e739kSTyU/6u8vad3xR/PEW0d8F6W1ciB08wHo3v4xtl9hGW+/kpE+Bx +sAM1JvBqs4x8Hr0yGUprMgQwQXRUGfz3LaGIJNyN2ZCKQgoQZkirs3Y260JJxbDNTQg h4bWIyBfHO39Hc7Scpr0nlauXgPZKB/pYbe8dag8d1y5g/WwKvj0RocMHj8VFEOzZVci luWm+ou21C5ylhssXlclh4Mu5R3I9AvTMRjM8+HgsdhvyL2RW0k5VA6ToZ+tXbqBREp2 FNIUT2omJNEhvR3vBlgg3IzCHl+G3SqRufsi4NIZ3FZEtMtdzjSN/s9GoysEi0doC0mQ jiZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716366454; x=1716971254; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=yc4M1An1SVE3VhBwugQSFEPj2HdsFrg3eqR9/azRDhc=; b=Kt7skqaVmpqzcCaYu5LIznfggMYCwCxgGevKlmdi6LYi8QLWze1G1vHia+KcXYnx3r hV7W4R4EQyuda2wLyRkQRv7Jk6DB6yAZOA9MniuS4BM7XqBMerOMPu9lpFrR105GbeOd ebiWhGNk9s67KIpJSxqAFIV7DWBtGsa/WopeplDAxfj2w2Y1KPu3dWxqe5DvIHt1A/0R JypC3VhjqUf3OAiHcWw6VU8jPd6YB4J0M6uKdoM6shCGKvoKgGv85P5nLswGcl8nkMOu 0RembiAfQXvHfdLjJSUHrWSJwQKWPqTcFwxWmZDzdfZj4LzQvb7t4X4PzzggsriLrmRu sWgg== X-Forwarded-Encrypted: i=1; AJvYcCXzmbwG2ubmPToE/eksad2bDiYDukx6OKrOtTAfDHBLKkUmMrJGAhGz+zGBUbBhZBeCjIggooosmGIz9zz+7DNXoRQUrNEN8R4j+w19 X-Gm-Message-State: AOJu0YyPI+UNkpLomStWTnBYKoYAqf2V0pM2Vlw59J1GSaca2m+m4nJF 6st5k+F9lQWCskJHGLexBKxjcFXiluTYp5qXfCXtTM4DEQtwcr7j X-Received: by 2002:a7b:c449:0:b0:420:1db0:53c1 with SMTP id 5b1f17b1804b1-420fd355961mr8286005e9.41.1716366454344; Wed, 22 May 2024 01:27:34 -0700 (PDT) Received: from fedora.iskraemeco.si (84-255-245-182.static.t-2.net. [84.255.245.182]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3502b79bc0asm33964495f8f.1.2024.05.22.01.27.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 May 2024 01:27:33 -0700 (PDT) From: Uros Bizjak To: iommu@lists.linux.dev, linux-kernel@vger.kernel.org Cc: Uros Bizjak , Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy Subject: [PATCH 3/3] iommufd: Use atomic_long_try_cmpxchg() in incr_user_locked_vm() Date: Wed, 22 May 2024 10:26:49 +0200 Message-ID: <20240522082729.971123-3-ubizjak@gmail.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240522082729.971123-1-ubizjak@gmail.com> References: <20240522082729.971123-1-ubizjak@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Use atomic_long_try_cmpxchg() instead of atomic_long_cmpxchg (*ptr, old, new) != old in incr_user_locked_vm(). cmpxchg returns success in ZF flag, so this change saves a compare after cmpxchg (and related move instruction in front of cmpxchg). Also, atomic_long_try_cmpxchg() implicitly assigns old *ptr value to "old" when cmpxchg fails. There is no need to re-read the value in the loop. Signed-off-by: Uros Bizjak Cc: Jason Gunthorpe Cc: Kevin Tian Cc: Joerg Roedel Cc: Will Deacon Cc: Robin Murphy --- drivers/iommu/iommufd/pages.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/drivers/iommu/iommufd/pages.c b/drivers/iommu/iommufd/pages.c index 528f356238b3..117f644a0c5b 100644 --- a/drivers/iommu/iommufd/pages.c +++ b/drivers/iommu/iommufd/pages.c @@ -809,13 +809,14 @@ static int incr_user_locked_vm(struct iopt_pages *pages, unsigned long npages) lock_limit = task_rlimit(pages->source_task, RLIMIT_MEMLOCK) >> PAGE_SHIFT; + + cur_pages = atomic_long_read(&pages->source_user->locked_vm); do { - cur_pages = atomic_long_read(&pages->source_user->locked_vm); new_pages = cur_pages + npages; if (new_pages > lock_limit) return -ENOMEM; - } while (atomic_long_cmpxchg(&pages->source_user->locked_vm, cur_pages, - new_pages) != cur_pages); + } while (!atomic_long_try_cmpxchg(&pages->source_user->locked_vm, + &cur_pages, new_pages)); return 0; } -- 2.45.1