Received: by 2002:ac0:a591:0:0:0:0:0 with SMTP id m17-v6csp1987528imm; Fri, 6 Jul 2018 09:50:15 -0700 (PDT) X-Google-Smtp-Source: AAOMgpcoAY0umVZadQtFatLaMka6gnNJ2FIAFeVix/vybyAP1tpvXw/PhbUo613BOibEiskPYD3f X-Received: by 2002:a63:3348:: with SMTP id z69-v6mr9850611pgz.171.1530895815669; Fri, 06 Jul 2018 09:50:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530895815; cv=none; d=google.com; s=arc-20160816; b=E5JTubJPMfd6h6WB01y+MaD1ZvH94SVpLXoe+lyxopPryPBud5UJ6QirxzB/rv+XR7 /dxkdteu71SAf/eUwEXlxc8Xq/dQPB+rJ1n0Gy5BUsf55qKdUenWDk4C2DT2Jc/NGMfc D4lNQtdkyKmbOQYjYUPx7o8wDEjbwG95pfDZMIjrCNaSG1QnVB6Lx/+373BTVzMaSE7D AgccvvwJVuoZ3O6aBPr+VPW7CbAc3f9ywJL+5bO/8HapETtk1YoT4dHRCMEQAg6jft3p l42y4PKnSrJcmpTFWAifLEeMBaj659D70RXicKq2sZc2moEOD3fRki1pfj++mQY25Y8i 7tAg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=Uoq+9eaRh31rsmOWQakLZnRhbNgrp+HyXztSn+oSv6k=; b=NOt5Tk9Cd8oSwYWfLrTbnAupPlRgyAMOnhKRIRHEcVb+OEuy+cqTZfl1vR9wK30jNs /58jurOj0jqO33sgr+CLHeQD4l0CJpfZ7zEQcKV42VFrqkiMLyN2z8vwMiNyrJDhhVF2 FXDowFxUw3FAJD6w1uSubcYhLuivdNg2sOcx/4YU3GmWlO8R5SDjTKhMl0kHF6vsEL5R o7WF1mcTLb7ApwaQX2PqqqOKWiUdyFXeA6xB/onPMpGufjOgecrkyjgdXz5j2l+MvMiM 7tPeDX0YozSeN4cLI1rS/dCP1wxKF2i/Bc8UfSpJIe3qIyskODrB8NrXzv7d3ey0jSld j6Sg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=gYWFwjvb; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n13-v6si9201331pfd.50.2018.07.06.09.50.00; Fri, 06 Jul 2018 09:50:15 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=gYWFwjvb; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933677AbeGFQtM (ORCPT + 99 others); Fri, 6 Jul 2018 12:49:12 -0400 Received: from mail-vk0-f65.google.com ([209.85.213.65]:39043 "EHLO mail-vk0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933157AbeGFQtL (ORCPT ); Fri, 6 Jul 2018 12:49:11 -0400 Received: by mail-vk0-f65.google.com with SMTP id e139-v6so5703002vkf.6; Fri, 06 Jul 2018 09:49:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=Uoq+9eaRh31rsmOWQakLZnRhbNgrp+HyXztSn+oSv6k=; b=gYWFwjvbX6CctjgeLemurze2n0lurcDgcU3g1DRXojDQROfmepQT5Aq4GGrMcuMTDq OrJ7+ayVU1xdY2yAA3k/SiWSQAym68tYIgBm/aUuVhim0nzsUyE0y7OAPjspcqMQ6bnZ ZU+ohZCAbHiWn0TB+B+QoV2/2Xg0YLVZcWY3N3SXXqn3U5lKWFDRJNIUoVjTfV04rn1b 7OMVOkArkpxuR4z97151LjD+x7okeT7JKGs9DAH0dGmNYP82LomEXbilmtuuNDsvh3Jp 0wvXMlcd9zHbbN9LAYvGQM0x4vGMPgRFY2czPZpyNQn1fbbZAuJCAbk6egyvNUi5UXyY UOCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=Uoq+9eaRh31rsmOWQakLZnRhbNgrp+HyXztSn+oSv6k=; b=gve+R04MJmvrIIp6dh8r4dc1tOTKTNNNIy2tLzsBQ2zgb+bL4wSReq4e96+nqdXprf Tl/jajRH9obZY9jnlNWffa7MrMzNsoCp7/qHvTK3CjSZVo3zAsvVOeyBdCE9/uHlWwUD OI93hyrWZ/uriE7ypYW4G6qS48Bqd/yYmgyZ4nYMIZtVafJ8W8mdp2GuIjNkcbA5QJf9 E+pTN/YxUlaB5b8Oh0z4GneTBQ4wTt9fup8EpQkhwSr0BaFIl/1hPg1HKseeAA9v0YeN 8hU7e1oP6EBWsOCyxlJPECpbYaYUZqt4qsQWn6dh0QhixoKUIJlWYVvvJ4pdo2kIagTq LMxg== X-Gm-Message-State: APt69E324+YvyIp+lBenwtYgOAnPFv4GlIc+tlooOMnWLhvCTA/wgzra xBTlQJoUoDdEnZgwGKYMRjyNk6kaXmZgzVmDs1MfAA== X-Received: by 2002:a1f:50c:: with SMTP id 12-v6mr6173846vkf.26.1530895750526; Fri, 06 Jul 2018 09:49:10 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a67:2149:0:0:0:0:0 with HTTP; Fri, 6 Jul 2018 09:49:09 -0700 (PDT) In-Reply-To: <20180706162457.20489-1-tycho@tycho.ws> References: <20180706143919.GA2344@kroah.com> <20180706162457.20489-1-tycho@tycho.ws> From: Andy Shevchenko Date: Fri, 6 Jul 2018 19:49:09 +0300 Message-ID: Subject: Re: [PATCH v3] uart: fix race between uart_put_char() and uart_shutdown() To: Tycho Andersen Cc: Greg Kroah-Hartman , Jiri Slaby , "open list:SERIAL DRIVERS" , Linux Kernel Mailing List , "Serge E . Hallyn" Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jul 6, 2018 at 7:24 PM, Tycho Andersen wrote: > Looking in uart_port_startup(), it seems that circ->buf (state->xmit.buf) > protected by the "per-port mutex", which based on uart_port_check() is > state->port.mutex. Indeed, the lock acquired in uart_put_char() is > uport->lock, i.e. not the same lock. > > Anyway, since the lock is not acquired, if uart_shutdown() is called, the > last chunk of that function may release state->xmit.buf before its assigned > to null, and cause the race above. > > To fix it, let's lock uport->lock when allocating/deallocating > state->xmit.buf in addition to the per-port mutex. Thanks for fixing this! Reviewed-by: Andy Shevchenko Some nitpicks though. > + unsigned long page, flags = 0; I would rather put on separate lines and btw assignment is not needed. It all goes through macros. > - if (!state->xmit.buf) { > - /* This is protected by the per port mutex */ > - page = get_zeroed_page(GFP_KERNEL); > - if (!page) > - return -ENOMEM; > + page = get_zeroed_page(GFP_KERNEL); > + if (!page) > + return -ENOMEM; > + if (!state->xmit.buf) { > state->xmit.buf = (unsigned char *) page; > uart_circ_clear(&state->xmit); > + } else { > + free_page(page); > } I see original code, but since you are adding else, does it make sense to switch to positive condition? > + unsigned long flags = 0; Ditto about assignment. -- With Best Regards, Andy Shevchenko