From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) (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 CD09225949C for ; Sat, 21 Dec 2024 06:15:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.52 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734761761; cv=none; b=hC0VwvfjsOyn5r1sBJ+CQeYXxsP4cn/Uj1g60vpm18aF5Cueh+0aBv8wXvzs3n6sTwFmEAX6qtWuwog3zR4zMkhobj264sx6BHP9aUxBU18CpDq9ePRY40ui4uEYevLHKVa1zEZl/LUgsnMoWmPSRkzJ/Z3AxXTDZiBGcda7Gbg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734761761; c=relaxed/simple; bh=oZCmuGesJi457Y8d7cNq2uv+F+JQqy3iXj9yzuBljZI=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=Pxfaj6ZrxxQiWt+Uo1nh+LuV1ldDxW+lAqIxUMpz2qZtr0HXqTu5KZR+BEVbR8BJel2/Lm4AdXgOzV8pHfJnqQKKoJ5xVdLCbZ4bHjhXIyweUvGlPS7G0AIh/st7TdS7eopmW51O8YAWEfjM6AWbykaFiwEGZY5eTi7Vqw0qu1I= 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=YfOCShHM; arc=none smtp.client-ip=209.85.216.52 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="YfOCShHM" Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-2ef28f07dbaso1840529a91.2 for ; Fri, 20 Dec 2024 22:15:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734761759; x=1735366559; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=oS3tLtH3G2NkjGVXIiGK0F7WTbZAgPJIj5pZgAs1nrc=; b=YfOCShHM52T2shvlHyC+E/eLFzvkZyAfEyVUhjfNm6Ox+6bZQ5zwmwKo+f2ySkdsfW BK8HDMbr3mP4ZANzMcgETQ4p7fApxHWWZz9BGVbjGcg6by/ubDJy4a8TeMtFhOMdRnA5 iQz73WtSrvAiQbo77Uw8Lp1VCNV7pFZgaGWOw++nPHoJC2+Wd9yWFn9msqSxDFl4pXNj DCDqp2H9SAbOI7kmcuaC/uny0QU23OOkv0jpMG1MhlZtOGTdEymmgWpDsQE5eihL7u+J eX7LURBJ/6NiUYnFstIxBaDuYA02x4tGm35Lcd9pTdNWANcY+Og0+KLpJX5Kss27SaDB ajxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734761759; x=1735366559; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=oS3tLtH3G2NkjGVXIiGK0F7WTbZAgPJIj5pZgAs1nrc=; b=o88kFBnqxqlF3owaRuZJWxPsjiAGu0kg4jKqfghr2SVd7gE9K52+4Ek+QzPMQoZS8W aEr0YaBI9CuEsAD+tgCSiFUKOKtFh50f6WJKVpNXQ3oTg6+pu9/z/RoYSPeEmannaAC3 UQbBmrah7tv7a0sJ2Sn23MH5vJsnvsx7R9yg4iuPAvcZnSHWJONoianfJWHrBuyGPV7i e72SIRhkSSao2Yj5t844PZPU7S4f5S4tFFe3qIZDhjN5t5eh9AEiLK8rnrJPMXKkiIWA gKwxgsTXJE8YzgX1jmX1/uxGe5wG+u7PxNmkJbQghU6r5cnSmlJ4MGn6MIQcjkFo7YR/ KIag== X-Forwarded-Encrypted: i=1; AJvYcCV2m00NthGyvYi33206B2UtJfdZ64HcNe4C8GG8o2n3kgY15iPOZZPiUW101EioqmDZP7xCm9nmJcAf4+A=@vger.kernel.org X-Gm-Message-State: AOJu0Yyffusg20bq9cKkIpm2nAkUuMKJC03kEWZQUbIJTu+kXBMkH67r 3Hlb17HzhUay9uoYGKhamRnn6pPgmmTyZmGJlZGMDNpnhWySWenSVW7J0w== X-Gm-Gg: ASbGncuk9NRRsAswF8P4oiA/FCZuCPJeG8otz2UYhYIoB+ioQKfxRLReBjvr1g+Wdl2 Bdmroe269YS3/tDq/lnFBWU6NH3ISacWmnxSKhAgXNd3RmFaIFxx15bR9iNeaOI6QcAbCljpCGq 1YBPaNgK2JOTryGb7JTJr1Zt9frpmLw2rfns8JWiwMyNNHUKLWBqvXofQABy8qB1Flvukn62jxI a/1Ph0jie+70hA+4NFjc4PICIJmHaoMAx/ADnoyYbD1rDgyu8osC4PugJen6g== X-Google-Smtp-Source: AGHT+IGfElaSXHPnR5kdlCvCuITY2Z6qEmm4ipYCTaZft2zacyoP/O4rfsRc+I9+M8TNZ8cr7jvwlA== X-Received: by 2002:a17:90b:274b:b0:2ee:e961:303d with SMTP id 98e67ed59e1d1-2f452ee4d17mr8934228a91.35.1734761758895; Fri, 20 Dec 2024 22:15:58 -0800 (PST) Received: from HOME-PC ([223.185.132.235]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-219dc971814sm38065985ad.79.2024.12.20.22.15.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Dec 2024 22:15:58 -0800 (PST) From: Dheeraj Reddy Jonnalagadda To: edumazet@google.com, jasowang@redhat.com Cc: akpm@linux-foundation.org, surenb@google.com, jack@suse.cz, linux-kernel@vger.kernel.org Subject: [PATCH RFC] possible atomicity issue in ptr_ring_resize_multiple_bh_noprof Date: Sat, 21 Dec 2024 11:45:54 +0530 Message-Id: <20241221061555.1071516-1-dheeraj.linuxdev@gmail.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Hi Maintainers, While reviewing the ptr_ring_resize_multiple_bh_noprof function, I noticed a potential atomicity issue. The function appears to be callable from multiple threads, based on the locking patterns and _bh suffix. The current code frees queues[i] after releasing locks: for (i = 0; i < nrings; ++i) { spin_lock_bh(&(rings[i])->consumer_lock); spin_lock(&(rings[i])->producer_lock); queues[i] = __ptr_ring_swap_queue(rings[i], queues[i], size, gfp, destroy); spin_unlock(&(rings[i])->producer_lock); spin_unlock_bh(&(rings[i])->consumer_lock); } /* Free after releasing locks */ for (i = 0; i < nrings; ++i) kvfree(queues[i]); It seems that there could be a race condition where another thread modifies queues[i] between the unlock and the kvfree. Would it be safer to do the kvfree while still holding the locks and removing the kvfree loop later as shown below? for (i = 0; i < nrings; ++i) { spin_lock_bh(&(rings[i])->consumer_lock); spin_lock(&(rings[i])->producer_lock); queues[i] = __ptr_ring_swap_queue(rings[i], queues[i], size, gfp, destroy); kvfree(queues[i]); spin_unlock(&(rings[i])->producer_lock); spin_unlock_bh(&(rings[i])->consumer_lock); } kfree(queues); return 0; I've attached a potential fix, but would appreciate confirmation on whether this is actually an issue that needs addressing. -Dheeraj